[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-30 Thread Arnold Schwaighofer via Phabricator via cfe-commits
aschwaighofer created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Swift would like to use clang's abis to emit protocol declarations.

It needs to a hook to register when inherited protocols are emitted.

This commits adds the public API:

emitProtocolDecl(CodeGenModule &CGM, const ObjCProtocolDecl *p,

  llvm::function_ref
  createProtocolReference);

rdar://60888524


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77077

Files:
  clang/include/clang/CodeGen/CodeGenABITypes.h
  clang/lib/CodeGen/CGObjCGNU.cpp
  clang/lib/CodeGen/CGObjCMac.cpp
  clang/lib/CodeGen/CGObjCRuntime.cpp
  clang/lib/CodeGen/CGObjCRuntime.h

Index: clang/lib/CodeGen/CGObjCRuntime.h
===
--- clang/lib/CodeGen/CGObjCRuntime.h
+++ clang/lib/CodeGen/CGObjCRuntime.h
@@ -211,6 +211,16 @@
   /// implementations.
   virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
 
+  /// getOrEmitProtocol - Get the protocol object for the given
+  /// declaration, emitting it if necessary. The return value has type
+  /// ProtocolPtrTy.
+  /// \p getProtocolReference is called by the implementation when a
+  /// reference to another protocol is needed.
+  virtual llvm::Constant *getOrEmitProtocol(
+  const ObjCProtocolDecl *OPD,
+  llvm::function_ref
+  getProtocolReference) = 0;
+
   /// Generate a function preamble for a method with the specified
   /// types.
 
Index: clang/lib/CodeGen/CGObjCRuntime.cpp
===
--- clang/lib/CodeGen/CGObjCRuntime.cpp
+++ clang/lib/CodeGen/CGObjCRuntime.cpp
@@ -13,14 +13,15 @@
 //===--===//
 
 #include "CGObjCRuntime.h"
-#include "CGCleanup.h"
 #include "CGCXXABI.h"
+#include "CGCleanup.h"
 #include "CGRecordLayout.h"
 #include "CodeGenFunction.h"
 #include "CodeGenModule.h"
 #include "clang/AST/RecordLayout.h"
 #include "clang/AST/StmtObjC.h"
 #include "clang/CodeGen/CGFunctionInfo.h"
+#include "clang/CodeGen/CodeGenABITypes.h"
 #include "llvm/Support/SaveAndRestore.h"
 
 using namespace clang;
@@ -383,3 +384,11 @@
 CGM.getTypes().GetFunctionType(argsInfo)->getPointerTo();
   return MessageSendInfo(argsInfo, signatureType);
 }
+
+llvm::Constant *clang::CodeGen::emitProtocolDecl(
+CodeGenModule &CGM, const ObjCProtocolDecl *protocol,
+llvm::function_ref
+createProtocolReference) {
+  return CGM.getObjCRuntime().getOrEmitProtocol(protocol,
+createProtocolReference);
+}
Index: clang/lib/CodeGen/CGObjCMac.cpp
===
--- clang/lib/CodeGen/CGObjCMac.cpp
+++ clang/lib/CodeGen/CGObjCMac.cpp
@@ -1110,7 +1110,7 @@
   /// GetOrEmitProtocol - Get the protocol object for the given
   /// declaration, emitting it if necessary. The return value has type
   /// ProtocolPtrTy.
-  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
+  llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
 
   /// GetOrEmitProtocolRef - Get a forward reference to the protocol
   /// object for the given declaration, emitting it if needed. These
@@ -1288,10 +1288,15 @@
   llvm::Constant *emitMethodList(Twine Name, MethodListType MLT,
  ArrayRef Methods);
 
-  /// GetOrEmitProtocol - Get the protocol object for the given
+  /// getOrEmitProtocol - Get the protocol object for the given
   /// declaration, emitting it if necessary. The return value has type
   /// ProtocolPtrTy.
-  llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) override;
+  /// \p getProtocolReference is called by the implementation when a
+  /// reference to another protocol is needed.
+  llvm::Constant *getOrEmitProtocol(
+  const ObjCProtocolDecl *OPD,
+  llvm::function_ref
+  getProtocolReference) override;
 
   /// GetOrEmitProtocolRef - Get a forward reference to the protocol
   /// object for the given declaration, emitting it if needed. These
@@ -1309,9 +1314,11 @@
 
   /// EmitProtocolList - Generate the list of referenced
   /// protocols. The return value has type ProtocolListPtrTy.
-  llvm::Constant *EmitProtocolList(Twine Name,
-   ObjCProtocolDecl::protocol_iterator begin,
-   ObjCProtocolDecl::protocol_iterator end);
+  llvm::Constant *EmitProtocolList(
+  Twine Name, ObjCProtocolDecl::protocol_iterator begin,
+  ObjCProtocolDecl::protocol_iterator end,
+  llvm::function_ref
+  getProtocolRef);
 
   /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
   /// for the given selector.
@@ -1471,10 +1478,15 @@
 const ObjCIvarDecl *Ivar,
 unsigned long int offset);
 
-  /// GetOrEmitProtocol - Get the protocol ob

[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-30 Thread John McCall via Phabricator via cfe-commits
rjmccall added inline comments.



Comment at: clang/include/clang/CodeGen/CodeGenABITypes.h:148
+ llvm::function_ref
+ createProtocolReference);
 }  // end namespace CodeGen

I would call this `emitObjCProtocolObject` or something, and maybe say in the 
comment:

> Get a pointer to a protocol object for the given declaration, emitting it if 
> it hasn't already been emitted in this translation unit.  Note that the ABI 
> for emitting a protocol reference in code (e.g. for a `@protocol` expression) 
> in most runtimes is not as simple as just materializing a pointer to this 
> object.

Can you explain the need for the callback?   Are you expecting to use this for 
Swift-declared protocols by synthesizing an ObjC protocol declaration for them? 
 I can see why you'd need a callback in that case.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-30 Thread Arnold Schwaighofer via Phabricator via cfe-commits
aschwaighofer added a comment.

> Can you explain the need for the callback? Are you expecting to use this for 
> Swift-declared protocols by synthesizing an ObjC protocol declaration for 
> them? I can see why you'd need a callback in that case.

The objective C protocol references other protocols in its inherited list. 
Swift has an internal list that keeps track of those protocols references and 
makes sure to initialized them for the debugger and also makes sure that the 
underlying protocol structure is emitted. The call back is to keep this list in 
sync.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-30 Thread Arnold Schwaighofer via Phabricator via cfe-commits
aschwaighofer marked an inline comment as done.
aschwaighofer added inline comments.



Comment at: clang/include/clang/CodeGen/CodeGenABITypes.h:148
+ llvm::function_ref
+ createProtocolReference);
 }  // end namespace CodeGen

rjmccall wrote:
> I would call this `emitObjCProtocolObject` or something, and maybe say in the 
> comment:
> 
> > Get a pointer to a protocol object for the given declaration, emitting it 
> > if it hasn't already been emitted in this translation unit.  Note that the 
> > ABI for emitting a protocol reference in code (e.g. for a `@protocol` 
> > expression) in most runtimes is not as simple as just materializing a 
> > pointer to this object.
> 
> Can you explain the need for the callback?   Are you expecting to use this 
> for Swift-declared protocols by synthesizing an ObjC protocol declaration for 
> them?  I can see why you'd need a callback in that case.
> Can you explain the need for the callback? Are you expecting to use this for 
> Swift-declared protocols by synthesizing an ObjC protocol declaration for 
> them? I can see why you'd need a callback in that case.

The objective C protocol references other protocols in its inherited list. 
Swift has an internal list that keeps track of those protocols references and 
makes sure to initialized them for the debugger and also makes sure that the 
underlying protocol structure is emitted. The call back is to keep this list in 
sync.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-30 Thread Arnold Schwaighofer via Phabricator via cfe-commits
aschwaighofer updated this revision to Diff 253712.
aschwaighofer added a comment.

- Change API name to emitObjCProtocolObject.
- Make stuff compile on current ToT.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077

Files:
  clang/include/clang/CodeGen/CodeGenABITypes.h
  clang/lib/CodeGen/CGObjCGNU.cpp
  clang/lib/CodeGen/CGObjCMac.cpp
  clang/lib/CodeGen/CGObjCRuntime.cpp
  clang/lib/CodeGen/CGObjCRuntime.h

Index: clang/lib/CodeGen/CGObjCRuntime.h
===
--- clang/lib/CodeGen/CGObjCRuntime.h
+++ clang/lib/CodeGen/CGObjCRuntime.h
@@ -211,6 +211,16 @@
   /// implementations.
   virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
 
+  /// getOrEmitProtocol - Get the protocol object for the given
+  /// declaration, emitting it if necessary. The return value has type
+  /// ProtocolPtrTy.
+  /// \p getProtocolReference is called by the implementation when a
+  /// reference to another protocol is needed.
+  virtual llvm::Constant *getOrEmitProtocol(
+  const ObjCProtocolDecl *OPD,
+  llvm::function_ref
+  getProtocolReference) = 0;
+
   /// Generate a function preamble for a method with the specified
   /// types.
 
Index: clang/lib/CodeGen/CGObjCRuntime.cpp
===
--- clang/lib/CodeGen/CGObjCRuntime.cpp
+++ clang/lib/CodeGen/CGObjCRuntime.cpp
@@ -13,14 +13,15 @@
 //===--===//
 
 #include "CGObjCRuntime.h"
-#include "CGCleanup.h"
 #include "CGCXXABI.h"
+#include "CGCleanup.h"
 #include "CGRecordLayout.h"
 #include "CodeGenFunction.h"
 #include "CodeGenModule.h"
 #include "clang/AST/RecordLayout.h"
 #include "clang/AST/StmtObjC.h"
 #include "clang/CodeGen/CGFunctionInfo.h"
+#include "clang/CodeGen/CodeGenABITypes.h"
 #include "llvm/Support/SaveAndRestore.h"
 
 using namespace clang;
@@ -383,3 +384,11 @@
 CGM.getTypes().GetFunctionType(argsInfo)->getPointerTo();
   return MessageSendInfo(argsInfo, signatureType);
 }
+
+llvm::Constant *clang::CodeGen::emitObjCProtocolObject(
+CodeGenModule &CGM, const ObjCProtocolDecl *protocol,
+llvm::function_ref
+createProtocolReference) {
+  return CGM.getObjCRuntime().getOrEmitProtocol(protocol,
+createProtocolReference);
+}
Index: clang/lib/CodeGen/CGObjCMac.cpp
===
--- clang/lib/CodeGen/CGObjCMac.cpp
+++ clang/lib/CodeGen/CGObjCMac.cpp
@@ -1110,7 +1110,7 @@
   /// GetOrEmitProtocol - Get the protocol object for the given
   /// declaration, emitting it if necessary. The return value has type
   /// ProtocolPtrTy.
-  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
+  llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
 
   /// GetOrEmitProtocolRef - Get a forward reference to the protocol
   /// object for the given declaration, emitting it if needed. These
@@ -1288,10 +1288,15 @@
   llvm::Constant *emitMethodList(Twine Name, MethodListType MLT,
  ArrayRef Methods);
 
-  /// GetOrEmitProtocol - Get the protocol object for the given
+  /// getOrEmitProtocol - Get the protocol object for the given
   /// declaration, emitting it if necessary. The return value has type
   /// ProtocolPtrTy.
-  llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) override;
+  /// \p getProtocolReference is called by the implementation when a
+  /// reference to another protocol is needed.
+  llvm::Constant *getOrEmitProtocol(
+  const ObjCProtocolDecl *OPD,
+  llvm::function_ref
+  getProtocolReference) override;
 
   /// GetOrEmitProtocolRef - Get a forward reference to the protocol
   /// object for the given declaration, emitting it if needed. These
@@ -1309,9 +1314,11 @@
 
   /// EmitProtocolList - Generate the list of referenced
   /// protocols. The return value has type ProtocolListPtrTy.
-  llvm::Constant *EmitProtocolList(Twine Name,
-   ObjCProtocolDecl::protocol_iterator begin,
-   ObjCProtocolDecl::protocol_iterator end);
+  llvm::Constant *EmitProtocolList(
+  Twine Name, ObjCProtocolDecl::protocol_iterator begin,
+  ObjCProtocolDecl::protocol_iterator end,
+  llvm::function_ref
+  getProtocolRef);
 
   /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
   /// for the given selector.
@@ -1471,10 +1478,15 @@
 const ObjCIvarDecl *Ivar,
 unsigned long int offset);
 
-  /// GetOrEmitProtocol - Get the protocol object for the given
+  /// getOrEmitProtocol - Get the protocol object for the given
   /// declaration, emitting it if necessary. The return value has type
   /// ProtocolPtrTy.
- 

[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-30 Thread John McCall via Phabricator via cfe-commits
rjmccall added inline comments.



Comment at: clang/include/clang/CodeGen/CodeGenABITypes.h:148
+ llvm::function_ref
+ createProtocolReference);
 }  // end namespace CodeGen

aschwaighofer wrote:
> rjmccall wrote:
> > I would call this `emitObjCProtocolObject` or something, and maybe say in 
> > the comment:
> > 
> > > Get a pointer to a protocol object for the given declaration, emitting it 
> > > if it hasn't already been emitted in this translation unit.  Note that 
> > > the ABI for emitting a protocol reference in code (e.g. for a `@protocol` 
> > > expression) in most runtimes is not as simple as just materializing a 
> > > pointer to this object.
> > 
> > Can you explain the need for the callback?   Are you expecting to use this 
> > for Swift-declared protocols by synthesizing an ObjC protocol declaration 
> > for them?  I can see why you'd need a callback in that case.
> > Can you explain the need for the callback? Are you expecting to use this 
> > for Swift-declared protocols by synthesizing an ObjC protocol declaration 
> > for them? I can see why you'd need a callback in that case.
> 
> The objective C protocol references other protocols in its inherited list. 
> Swift has an internal list that keeps track of those protocols references and 
> makes sure to initialized them for the debugger and also makes sure that the 
> underlying protocol structure is emitted. The call back is to keep this list 
> in sync.
The problem is that this is really, really intertwined with the ABI logic.  
This callback basically has to know exactly how the caller is calling it and 
and what it's expected to build in response.

Swift code can use arbitrary inline Objective-C functions and therefore emit 
arbitrary Objective-C code.  Is there an approach that handles that while also 
being less invasive for Clang IRGen?  Can we, e.g., inspect the built 
llvm::Module to figure out retroactively what protocol refs we need to update 
dynamically instead of trying to track them during the build?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-31 Thread Arnold Schwaighofer via Phabricator via cfe-commits
aschwaighofer updated this revision to Diff 253938.
aschwaighofer added a comment.

- Remove callback. Swift can inspect the generated IR and update it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077

Files:
  clang/include/clang/CodeGen/CodeGenABITypes.h
  clang/lib/CodeGen/CGObjCGNU.cpp
  clang/lib/CodeGen/CGObjCMac.cpp
  clang/lib/CodeGen/CGObjCRuntime.cpp
  clang/lib/CodeGen/CGObjCRuntime.h

Index: clang/lib/CodeGen/CGObjCRuntime.h
===
--- clang/lib/CodeGen/CGObjCRuntime.h
+++ clang/lib/CodeGen/CGObjCRuntime.h
@@ -211,6 +211,11 @@
   /// implementations.
   virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
 
+  /// GetOrEmitProtocol - Get the protocol object for the given
+  /// declaration, emitting it if necessary. The return value has type
+  /// ProtocolPtrTy.
+  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) = 0;
+
   /// Generate a function preamble for a method with the specified
   /// types.
 
Index: clang/lib/CodeGen/CGObjCRuntime.cpp
===
--- clang/lib/CodeGen/CGObjCRuntime.cpp
+++ clang/lib/CodeGen/CGObjCRuntime.cpp
@@ -13,14 +13,15 @@
 //===--===//
 
 #include "CGObjCRuntime.h"
-#include "CGCleanup.h"
 #include "CGCXXABI.h"
+#include "CGCleanup.h"
 #include "CGRecordLayout.h"
 #include "CodeGenFunction.h"
 #include "CodeGenModule.h"
 #include "clang/AST/RecordLayout.h"
 #include "clang/AST/StmtObjC.h"
 #include "clang/CodeGen/CGFunctionInfo.h"
+#include "clang/CodeGen/CodeGenABITypes.h"
 #include "llvm/Support/SaveAndRestore.h"
 
 using namespace clang;
@@ -383,3 +384,9 @@
 CGM.getTypes().GetFunctionType(argsInfo)->getPointerTo();
   return MessageSendInfo(argsInfo, signatureType);
 }
+
+llvm::Constant *
+clang::CodeGen::emitObjCProtocolObject(CodeGenModule &CGM,
+   const ObjCProtocolDecl *protocol) {
+  return CGM.getObjCRuntime().GetOrEmitProtocol(protocol);
+}
Index: clang/lib/CodeGen/CGObjCMac.cpp
===
--- clang/lib/CodeGen/CGObjCMac.cpp
+++ clang/lib/CodeGen/CGObjCMac.cpp
@@ -1107,11 +1107,6 @@
 
   void GenerateProtocol(const ObjCProtocolDecl *PD) override;
 
-  /// GetOrEmitProtocol - Get the protocol object for the given
-  /// declaration, emitting it if necessary. The return value has type
-  /// ProtocolPtrTy.
-  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
-
   /// GetOrEmitProtocolRef - Get a forward reference to the protocol
   /// object for the given declaration, emitting it if needed. These
   /// forward references will be filled in with empty bodies if no
Index: clang/lib/CodeGen/CGObjCGNU.cpp
===
--- clang/lib/CodeGen/CGObjCGNU.cpp
+++ clang/lib/CodeGen/CGObjCGNU.cpp
@@ -617,6 +617,11 @@
   llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
const ObjCProtocolDecl *PD) override;
   void GenerateProtocol(const ObjCProtocolDecl *PD) override;
+
+  llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) override {
+llvm_unreachable("not implemented");
+  }
+
   llvm::Function *ModuleInitFunction() override;
   llvm::FunctionCallee GetPropertyGetFunction() override;
   llvm::FunctionCallee GetPropertySetFunction() override;
Index: clang/include/clang/CodeGen/CodeGenABITypes.h
===
--- clang/include/clang/CodeGen/CodeGenABITypes.h
+++ clang/include/clang/CodeGen/CodeGenABITypes.h
@@ -28,11 +28,12 @@
 #include "clang/CodeGen/CGFunctionInfo.h"
 
 namespace llvm {
-  class DataLayout;
-  class Module;
-  class Function;
-  class FunctionType;
-  class Type;
+class Constant;
+class DataLayout;
+class Module;
+class Function;
+class FunctionType;
+class Type;
 }
 
 namespace clang {
@@ -44,6 +45,7 @@
 class DiagnosticsEngine;
 class HeaderSearchOptions;
 class ObjCMethodDecl;
+class ObjCProtocolDecl;
 class PreprocessorOptions;
 
 namespace CodeGen {
@@ -137,6 +139,13 @@
CharUnits DstAlignment,
bool IsVolatile, QualType QT);
 
+/// Get a pointer to a protocol object for the given declaration, emitting it if
+/// it hasn't already been emitted in this translation unit. Note that the ABI
+/// for emitting a protocol reference in code (e.g. for a protocol expression)
+/// in most runtimes is not as simple as just materializing a pointer to this
+/// object.
+llvm::Constant *emitObjCProtocolObject(CodeGenModule &CGM,
+   const ObjCProtocolDecl *p);
 }  // end namespace CodeGen
 }  // end namespace clang
 
_

[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-31 Thread Arnold Schwaighofer via Phabricator via cfe-commits
aschwaighofer marked an inline comment as done.
aschwaighofer added inline comments.



Comment at: clang/include/clang/CodeGen/CodeGenABITypes.h:148
+ llvm::function_ref
+ createProtocolReference);
 }  // end namespace CodeGen

rjmccall wrote:
> aschwaighofer wrote:
> > rjmccall wrote:
> > > I would call this `emitObjCProtocolObject` or something, and maybe say in 
> > > the comment:
> > > 
> > > > Get a pointer to a protocol object for the given declaration, emitting 
> > > > it if it hasn't already been emitted in this translation unit.  Note 
> > > > that the ABI for emitting a protocol reference in code (e.g. for a 
> > > > `@protocol` expression) in most runtimes is not as simple as just 
> > > > materializing a pointer to this object.
> > > 
> > > Can you explain the need for the callback?   Are you expecting to use 
> > > this for Swift-declared protocols by synthesizing an ObjC protocol 
> > > declaration for them?  I can see why you'd need a callback in that case.
> > > Can you explain the need for the callback? Are you expecting to use this 
> > > for Swift-declared protocols by synthesizing an ObjC protocol declaration 
> > > for them? I can see why you'd need a callback in that case.
> > 
> > The objective C protocol references other protocols in its inherited list. 
> > Swift has an internal list that keeps track of those protocols references 
> > and makes sure to initialized them for the debugger and also makes sure 
> > that the underlying protocol structure is emitted. The call back is to keep 
> > this list in sync.
> The problem is that this is really, really intertwined with the ABI logic.  
> This callback basically has to know exactly how the caller is calling it and 
> and what it's expected to build in response.
> 
> Swift code can use arbitrary inline Objective-C functions and therefore emit 
> arbitrary Objective-C code.  Is there an approach that handles that while 
> also being less invasive for Clang IRGen?  Can we, e.g., inspect the built 
> llvm::Module to figure out retroactively what protocol refs we need to update 
> dynamically instead of trying to track them during the build?
Yes. I think you are right. Swift can inspect the generated IR and update it.

https://github.com/aschwaighofer/swift/commit/d29daf41ec3a51405df31591dad6fea97dbc58e0

I have removed the callback.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-31 Thread John McCall via Phabricator via cfe-commits
rjmccall added inline comments.



Comment at: clang/lib/CodeGen/CGObjCGNU.cpp:623
+llvm_unreachable("not implemented");
+  }
+

I think this is just the (unfortunately-named) `GenerateProtocolRef` (the one 
that just takes a protocol and not a CGF).



Comment at: clang/lib/CodeGen/CGObjCRuntime.h:217
+  /// ProtocolPtrTy.
+  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) = 0;
+

Can this name more closely parallel the "external" name, like 
`getOrEmitProtocolObject`?

Also, I think we're incrementally lowercasing new method names.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-31 Thread Arnold Schwaighofer via Phabricator via cfe-commits
aschwaighofer marked an inline comment as done.
aschwaighofer added inline comments.



Comment at: clang/lib/CodeGen/CGObjCRuntime.h:217
+  /// ProtocolPtrTy.
+  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) = 0;
+

rjmccall wrote:
> Can this name more closely parallel the "external" name, like 
> `getOrEmitProtocolObject`?
> 
> Also, I think we're incrementally lowercasing new method names.
This is an already existing method. I just moved it higher in the class 
hierarchy to CGObjCRuntime from CGObjCMac so that it is callable from 
getOrEmitProtocolObject.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-31 Thread Arnold Schwaighofer via Phabricator via cfe-commits
aschwaighofer marked an inline comment as done.
aschwaighofer added inline comments.



Comment at: clang/lib/CodeGen/CGObjCRuntime.h:217
+  /// ProtocolPtrTy.
+  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) = 0;
+

aschwaighofer wrote:
> rjmccall wrote:
> > Can this name more closely parallel the "external" name, like 
> > `getOrEmitProtocolObject`?
> > 
> > Also, I think we're incrementally lowercasing new method names.
> This is an already existing method. I just moved it higher in the class 
> hierarchy to CGObjCRuntime from CGObjCMac so that it is callable from 
> getOrEmitProtocolObject.
s/getOrEmitProtocolObject/emitObjCProtocolObject


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-03-31 Thread John McCall via Phabricator via cfe-commits
rjmccall added inline comments.



Comment at: clang/lib/CodeGen/CGObjCRuntime.h:217
+  /// ProtocolPtrTy.
+  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) = 0;
+

aschwaighofer wrote:
> aschwaighofer wrote:
> > rjmccall wrote:
> > > Can this name more closely parallel the "external" name, like 
> > > `getOrEmitProtocolObject`?
> > > 
> > > Also, I think we're incrementally lowercasing new method names.
> > This is an already existing method. I just moved it higher in the class 
> > hierarchy to CGObjCRuntime from CGObjCMac so that it is callable from 
> > getOrEmitProtocolObject.
> s/getOrEmitProtocolObject/emitObjCProtocolObject
I see.  Well, in that case, I won't ask you to rename it, but please do 
implement it for the GNU runtime as I mentioned.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-04-01 Thread Arnold Schwaighofer via Phabricator via cfe-commits
aschwaighofer updated this revision to Diff 254171.
aschwaighofer added a comment.

- Implement for the GNU runtimes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077

Files:
  clang/include/clang/CodeGen/CodeGenABITypes.h
  clang/lib/CodeGen/CGObjCGNU.cpp
  clang/lib/CodeGen/CGObjCMac.cpp
  clang/lib/CodeGen/CGObjCRuntime.cpp
  clang/lib/CodeGen/CGObjCRuntime.h

Index: clang/lib/CodeGen/CGObjCRuntime.h
===
--- clang/lib/CodeGen/CGObjCRuntime.h
+++ clang/lib/CodeGen/CGObjCRuntime.h
@@ -211,6 +211,11 @@
   /// implementations.
   virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
 
+  /// GetOrEmitProtocol - Get the protocol object for the given
+  /// declaration, emitting it if necessary. The return value has type
+  /// ProtocolPtrTy.
+  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) = 0;
+
   /// Generate a function preamble for a method with the specified
   /// types.
 
Index: clang/lib/CodeGen/CGObjCRuntime.cpp
===
--- clang/lib/CodeGen/CGObjCRuntime.cpp
+++ clang/lib/CodeGen/CGObjCRuntime.cpp
@@ -13,14 +13,15 @@
 //===--===//
 
 #include "CGObjCRuntime.h"
-#include "CGCleanup.h"
 #include "CGCXXABI.h"
+#include "CGCleanup.h"
 #include "CGRecordLayout.h"
 #include "CodeGenFunction.h"
 #include "CodeGenModule.h"
 #include "clang/AST/RecordLayout.h"
 #include "clang/AST/StmtObjC.h"
 #include "clang/CodeGen/CGFunctionInfo.h"
+#include "clang/CodeGen/CodeGenABITypes.h"
 #include "llvm/Support/SaveAndRestore.h"
 
 using namespace clang;
@@ -383,3 +384,9 @@
 CGM.getTypes().GetFunctionType(argsInfo)->getPointerTo();
   return MessageSendInfo(argsInfo, signatureType);
 }
+
+llvm::Constant *
+clang::CodeGen::emitObjCProtocolObject(CodeGenModule &CGM,
+   const ObjCProtocolDecl *protocol) {
+  return CGM.getObjCRuntime().GetOrEmitProtocol(protocol);
+}
Index: clang/lib/CodeGen/CGObjCMac.cpp
===
--- clang/lib/CodeGen/CGObjCMac.cpp
+++ clang/lib/CodeGen/CGObjCMac.cpp
@@ -1107,11 +1107,6 @@
 
   void GenerateProtocol(const ObjCProtocolDecl *PD) override;
 
-  /// GetOrEmitProtocol - Get the protocol object for the given
-  /// declaration, emitting it if necessary. The return value has type
-  /// ProtocolPtrTy.
-  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
-
   /// GetOrEmitProtocolRef - Get a forward reference to the protocol
   /// object for the given declaration, emitting it if needed. These
   /// forward references will be filled in with empty bodies if no
Index: clang/lib/CodeGen/CGObjCGNU.cpp
===
--- clang/lib/CodeGen/CGObjCGNU.cpp
+++ clang/lib/CodeGen/CGObjCGNU.cpp
@@ -617,6 +617,13 @@
   llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
const ObjCProtocolDecl *PD) override;
   void GenerateProtocol(const ObjCProtocolDecl *PD) override;
+
+  virtual llvm::Constant *GenerateProtocolRef(const ObjCProtocolDecl *PD);
+
+  llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) override {
+return GenerateProtocolRef(PD);
+  }
+
   llvm::Function *ModuleInitFunction() override;
   llvm::FunctionCallee GetPropertyGetFunction() override;
   llvm::FunctionCallee GetPropertySetFunction() override;
@@ -1348,7 +1355,7 @@
   void GenerateProtocol(const ObjCProtocolDecl *PD) override {
 // Do nothing - we only emit referenced protocols.
   }
-  llvm::Constant *GenerateProtocolRef(const ObjCProtocolDecl *PD) {
+  llvm::Constant *GenerateProtocolRef(const ObjCProtocolDecl *PD) override {
 std::string ProtocolName = PD->getNameAsString();
 auto *&Protocol = ExistingProtocols[ProtocolName];
 if (Protocol)
@@ -3039,13 +3046,18 @@
 
 llvm::Value *CGObjCGNU::GenerateProtocolRef(CodeGenFunction &CGF,
 const ObjCProtocolDecl *PD) {
+  auto protocol = GenerateProtocolRef(PD);
+  llvm::Type *T =
+  CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
+  return CGF.Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
+}
+
+llvm::Constant *CGObjCGNU::GenerateProtocolRef(const ObjCProtocolDecl *PD) {
   llvm::Constant *&protocol = ExistingProtocols[PD->getNameAsString()];
   if (!protocol)
 GenerateProtocol(PD);
   assert(protocol && "Unknown protocol");
-  llvm::Type *T =
-CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
-  return CGF.Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
+  return protocol;
 }
 
 llvm::Constant *
Index: clang/include/clang/CodeGen/CodeGenABITypes.h
==

[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-04-01 Thread Arnold Schwaighofer via Phabricator via cfe-commits
aschwaighofer marked an inline comment as done.
aschwaighofer added inline comments.



Comment at: clang/lib/CodeGen/CGObjCRuntime.h:217
+  /// ProtocolPtrTy.
+  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) = 0;
+

rjmccall wrote:
> aschwaighofer wrote:
> > aschwaighofer wrote:
> > > rjmccall wrote:
> > > > Can this name more closely parallel the "external" name, like 
> > > > `getOrEmitProtocolObject`?
> > > > 
> > > > Also, I think we're incrementally lowercasing new method names.
> > > This is an already existing method. I just moved it higher in the class 
> > > hierarchy to CGObjCRuntime from CGObjCMac so that it is callable from 
> > > getOrEmitProtocolObject.
> > s/getOrEmitProtocolObject/emitObjCProtocolObject
> I see.  Well, in that case, I won't ask you to rename it, but please do 
> implement it for the GNU runtime as I mentioned.
Done. I introduced 
`CGObjCGNU::GenerateProtocolRef(const ObjCProtocolDecl *PD)` because it only 
existed in the `CGObjCGNUstep2` subclass.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-04-01 Thread John McCall via Phabricator via cfe-commits
rjmccall accepted this revision.
rjmccall added a comment.
This revision is now accepted and ready to land.

Thanks, LGTM.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77077: [clang] CodeGen: Make getOrEmitProtocol public for Swift

2020-04-01 Thread Arnold Schwaighofer via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG153dadf3a3ca: [clang] CodeGen: Make getOrEmitProtocol public 
for Swift (authored by aschwaighofer).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77077/new/

https://reviews.llvm.org/D77077

Files:
  clang/include/clang/CodeGen/CodeGenABITypes.h
  clang/lib/CodeGen/CGObjCGNU.cpp
  clang/lib/CodeGen/CGObjCMac.cpp
  clang/lib/CodeGen/CGObjCRuntime.cpp
  clang/lib/CodeGen/CGObjCRuntime.h

Index: clang/lib/CodeGen/CGObjCRuntime.h
===
--- clang/lib/CodeGen/CGObjCRuntime.h
+++ clang/lib/CodeGen/CGObjCRuntime.h
@@ -211,6 +211,11 @@
   /// implementations.
   virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
 
+  /// GetOrEmitProtocol - Get the protocol object for the given
+  /// declaration, emitting it if necessary. The return value has type
+  /// ProtocolPtrTy.
+  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) = 0;
+
   /// Generate a function preamble for a method with the specified
   /// types.
 
Index: clang/lib/CodeGen/CGObjCRuntime.cpp
===
--- clang/lib/CodeGen/CGObjCRuntime.cpp
+++ clang/lib/CodeGen/CGObjCRuntime.cpp
@@ -13,14 +13,15 @@
 //===--===//
 
 #include "CGObjCRuntime.h"
-#include "CGCleanup.h"
 #include "CGCXXABI.h"
+#include "CGCleanup.h"
 #include "CGRecordLayout.h"
 #include "CodeGenFunction.h"
 #include "CodeGenModule.h"
 #include "clang/AST/RecordLayout.h"
 #include "clang/AST/StmtObjC.h"
 #include "clang/CodeGen/CGFunctionInfo.h"
+#include "clang/CodeGen/CodeGenABITypes.h"
 #include "llvm/Support/SaveAndRestore.h"
 
 using namespace clang;
@@ -383,3 +384,9 @@
 CGM.getTypes().GetFunctionType(argsInfo)->getPointerTo();
   return MessageSendInfo(argsInfo, signatureType);
 }
+
+llvm::Constant *
+clang::CodeGen::emitObjCProtocolObject(CodeGenModule &CGM,
+   const ObjCProtocolDecl *protocol) {
+  return CGM.getObjCRuntime().GetOrEmitProtocol(protocol);
+}
Index: clang/lib/CodeGen/CGObjCMac.cpp
===
--- clang/lib/CodeGen/CGObjCMac.cpp
+++ clang/lib/CodeGen/CGObjCMac.cpp
@@ -1107,11 +1107,6 @@
 
   void GenerateProtocol(const ObjCProtocolDecl *PD) override;
 
-  /// GetOrEmitProtocol - Get the protocol object for the given
-  /// declaration, emitting it if necessary. The return value has type
-  /// ProtocolPtrTy.
-  virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
-
   /// GetOrEmitProtocolRef - Get a forward reference to the protocol
   /// object for the given declaration, emitting it if needed. These
   /// forward references will be filled in with empty bodies if no
Index: clang/lib/CodeGen/CGObjCGNU.cpp
===
--- clang/lib/CodeGen/CGObjCGNU.cpp
+++ clang/lib/CodeGen/CGObjCGNU.cpp
@@ -617,6 +617,13 @@
   llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
const ObjCProtocolDecl *PD) override;
   void GenerateProtocol(const ObjCProtocolDecl *PD) override;
+
+  virtual llvm::Constant *GenerateProtocolRef(const ObjCProtocolDecl *PD);
+
+  llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) override {
+return GenerateProtocolRef(PD);
+  }
+
   llvm::Function *ModuleInitFunction() override;
   llvm::FunctionCallee GetPropertyGetFunction() override;
   llvm::FunctionCallee GetPropertySetFunction() override;
@@ -1348,7 +1355,7 @@
   void GenerateProtocol(const ObjCProtocolDecl *PD) override {
 // Do nothing - we only emit referenced protocols.
   }
-  llvm::Constant *GenerateProtocolRef(const ObjCProtocolDecl *PD) {
+  llvm::Constant *GenerateProtocolRef(const ObjCProtocolDecl *PD) override {
 std::string ProtocolName = PD->getNameAsString();
 auto *&Protocol = ExistingProtocols[ProtocolName];
 if (Protocol)
@@ -3039,13 +3046,18 @@
 
 llvm::Value *CGObjCGNU::GenerateProtocolRef(CodeGenFunction &CGF,
 const ObjCProtocolDecl *PD) {
+  auto protocol = GenerateProtocolRef(PD);
+  llvm::Type *T =
+  CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
+  return CGF.Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
+}
+
+llvm::Constant *CGObjCGNU::GenerateProtocolRef(const ObjCProtocolDecl *PD) {
   llvm::Constant *&protocol = ExistingProtocols[PD->getNameAsString()];
   if (!protocol)
 GenerateProtocol(PD);
   assert(protocol && "Unknown protocol");
-  llvm::Type *T =
-CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
-  return CGF.Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
+  return protocol;
 }
 
 llvm::Constant *
Index: clang/include/clang/CodeGen/CodeGenA