[llvm] r251482 - [Orc] Re-add C bindings for the Orc APIs, with a fix to remove the union that

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 27 19:40:04 PDT 2015


Author: lhames
Date: Tue Oct 27 21:40:04 2015
New Revision: 251482

URL: http://llvm.org/viewvc/llvm-project?rev=251482&view=rev
Log:
[Orc] Re-add C bindings for the Orc APIs, with a fix to remove the union that
was causing builder failures.

The bindings were originally added in r251472, and reverted in r251473 due to
the builder failures.


Added:
    llvm/trunk/include/llvm-c/OrcBindings.h
      - copied unchanged from r251472, llvm/trunk/include/llvm-c/OrcBindings.h
    llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindings.cpp
      - copied unchanged from r251472, llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindings.cpp
    llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.cpp
      - copied unchanged from r251472, llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.cpp
    llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h
      - copied, changed from r251472, llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h
    llvm/trunk/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp
      - copied unchanged from r251472, llvm/trunk/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp
Modified:
    llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt
    llvm/trunk/unittests/ExecutionEngine/Orc/CMakeLists.txt
    llvm/trunk/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp
    llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.cpp
    llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.h

Modified: llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt?rev=251482&r1=251481&r2=251482&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt (original)
+++ llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt Tue Oct 27 21:40:04 2015
@@ -2,6 +2,8 @@ add_llvm_library(LLVMOrcJIT
   ExecutionUtils.cpp
   IndirectionUtils.cpp
   NullResolver.cpp
+  OrcCBindings.cpp
+  OrcCBindingsStack.cpp
   OrcMCJITReplacement.cpp
   OrcTargetSupport.cpp
 

Copied: llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h (from r251472, llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h)
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h?p2=llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h&p1=llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h&r1=251472&r2=251482&rev=251482&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h (original)
+++ llvm/trunk/lib/ExecutionEngine/Orc/OrcCBindingsStack.h Tue Oct 27 21:40:04 2015
@@ -43,31 +43,41 @@ public:
 
 private:
 
-  typedef enum { Invalid, CODLayerHandle, ObjectLayerHandle } HandleType;
-  union RawHandleUnion {
-    RawHandleUnion() { memset(this, 0, sizeof(RawHandleUnion)); }
-    ObjLayerT::ObjSetHandleT Obj;
-    CODLayerT::ModuleSetHandleT COD;
+  class GenericHandle {
+  public:
+    virtual ~GenericHandle() {}
+    virtual orc::JITSymbol findSymbolIn(const std::string &Name,
+                                        bool ExportedSymbolsOnly) = 0;
+    virtual void removeModule() = 0;
   };
 
-  struct ModuleHandleData {
-
-    ModuleHandleData() : Type(Invalid) {}
+  template <typename LayerT>
+  class GenericHandleImpl : public GenericHandle {
+  public:
+    GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleSetHandleT Handle)
+      : Layer(Layer), Handle(std::move(Handle)) {}
 
-    ModuleHandleData(ObjLayerT::ObjSetHandleT H)
-        : Type(ObjectLayerHandle) {
-      RawHandle.Obj = std::move(H);
+    orc::JITSymbol findSymbolIn(const std::string &Name,
+                                bool ExportedSymbolsOnly) override {
+      return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
     }
 
-    ModuleHandleData(CODLayerT::ModuleSetHandleT H)
-      : Type(CODLayerHandle) {
-      RawHandle.COD = std::move(H);
+    void removeModule() override {
+      return Layer.removeModuleSet(Handle);
     }
 
-    HandleType Type;
-    RawHandleUnion RawHandle;
+  private:
+    LayerT &Layer;
+    typename LayerT::ModuleSetHandleT Handle;
   };
 
+  template <typename LayerT>
+  std::unique_ptr<GenericHandleImpl<LayerT>>
+  createGenericHandle(LayerT &Layer, typename LayerT::ModuleSetHandleT Handle) {
+    return llvm::make_unique<GenericHandleImpl<LayerT>>(Layer,
+                                                        std::move(Handle));
+  }
+
 public:
 
   // We need a 'ModuleSetHandleT' to conform to the layer concept.
@@ -171,7 +181,7 @@ public:
 
     auto LH = Layer.addModuleSet(std::move(S), std::move(MemMgr),
                                  std::move(Resolver));
-    ModuleHandleT H = createHandle(LH);
+    ModuleHandleT H = createHandle(Layer, LH);
 
     // Run the static constructors, and save the static destructor runner for
     // execution when the JIT is torn down.
@@ -199,17 +209,9 @@ public:
   }
 
   void removeModule(ModuleHandleT H) {
-    auto &HD = HandleData[H];
-    switch (HD.Type) {
-    case ObjectLayerHandle:
-      ObjectLayer.removeObjectSet(HD.RawHandle.Obj);
-      break;
-    case CODLayerHandle:
-      CODLayer.removeModuleSet(HD.RawHandle.COD);
-      break;
-    default:
-      llvm_unreachable("removeModule called on invalid handle type");
-    }
+    GenericHandles[H]->removeModule();
+    GenericHandles[H] = nullptr;
+    FreeHandleIndexes.push_back(H);
   }
 
   orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
@@ -218,32 +220,23 @@ public:
 
   orc::JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
                               bool ExportedSymbolsOnly) {
-    auto &HD = HandleData[H];
-    switch (HD.Type) {
-    case ObjectLayerHandle:
-      return ObjectLayer.findSymbolIn(HD.RawHandle.Obj, mangle(Name),
-                                      ExportedSymbolsOnly);
-    case CODLayerHandle:
-      return CODLayer.findSymbolIn(HD.RawHandle.COD, mangle(Name),
-                                   ExportedSymbolsOnly);
-    default:
-      llvm_unreachable("removeModule called on invalid handle type");
-    }
+    return GenericHandles[H]->findSymbolIn(Name, ExportedSymbolsOnly);
   }
 
 private:
 
-  template <typename LayerHandleT>
-  unsigned createHandle(LayerHandleT LH) {
+  template <typename LayerT>
+  unsigned createHandle(LayerT &Layer,
+                        typename LayerT::ModuleSetHandleT Handle) {
     unsigned NewHandle;
-    if (!FreeHandles.empty()) {
-      NewHandle = FreeHandles.back();
-      FreeHandles.pop_back();
-      HandleData[NewHandle] = ModuleHandleData(std::move(LH));
+    if (!FreeHandleIndexes.empty()) {
+      NewHandle = FreeHandleIndexes.back();
+      FreeHandleIndexes.pop_back();
+      GenericHandles[NewHandle] = createGenericHandle(Layer, std::move(Handle));
       return NewHandle;
     } else {
-      NewHandle = HandleData.size();
-      HandleData.push_back(ModuleHandleData(std::move(LH)));
+      NewHandle = GenericHandles.size();
+      GenericHandles.push_back(createGenericHandle(Layer, std::move(Handle)));
     }
     return NewHandle;
   }
@@ -256,8 +249,8 @@ private:
   std::unique_ptr<CompileCallbackMgr> CCMgr;
   CODLayerT CODLayer;
 
-  std::vector<ModuleHandleData> HandleData;
-  std::vector<unsigned> FreeHandles;
+  std::vector<std::unique_ptr<GenericHandle>> GenericHandles;
+  std::vector<unsigned> FreeHandleIndexes;
 
   orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
   std::vector<orc::CtorDtorRunner<OrcCBindingsStack>> IRStaticDestructorRunners;

Modified: llvm/trunk/unittests/ExecutionEngine/Orc/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/CMakeLists.txt?rev=251482&r1=251481&r2=251482&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/Orc/CMakeLists.txt (original)
+++ llvm/trunk/unittests/ExecutionEngine/Orc/CMakeLists.txt Tue Oct 27 21:40:04 2015
@@ -1,7 +1,11 @@
+
 set(LLVM_LINK_COMPONENTS
   Core
   OrcJIT
+  MC
   Support
+  Target
+  native
   )
 
 add_llvm_unittest(OrcJITTests
@@ -10,5 +14,6 @@ add_llvm_unittest(OrcJITTests
   GlobalMappingLayerTest.cpp
   LazyEmittingLayerTest.cpp
   ObjectTransformLayerTest.cpp
+  OrcCAPITest.cpp
   OrcTestCommon.cpp
   )

Modified: llvm/trunk/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp?rev=251482&r1=251481&r2=251482&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp Tue Oct 27 21:40:04 2015
@@ -18,7 +18,7 @@ namespace {
 
 TEST(IndirectionUtilsTest, MakeStub) {
   ModuleBuilder MB(getGlobalContext(), "x86_64-apple-macosx10.10", "");
-  Function *F = MB.createFunctionDecl<void(DummyStruct, DummyStruct)>(MB.getModule(), "");
+  Function *F = MB.createFunctionDecl<void(DummyStruct, DummyStruct)>("");
   SmallVector<AttributeSet, 4> Attrs;
   Attrs.push_back(
     AttributeSet::get(MB.getModule()->getContext(), 1U,

Modified: llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.cpp?rev=251482&r1=251481&r2=251482&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.cpp Tue Oct 27 21:40:04 2015
@@ -15,6 +15,8 @@
 
 using namespace llvm;
 
+bool OrcExecutionTest::NativeTargetInitialized = false;
+
 ModuleBuilder::ModuleBuilder(LLVMContext &Context, StringRef Triple,
                              StringRef Name)
   : M(new Module(Name, Context)),

Modified: llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.h?rev=251482&r1=251481&r2=251482&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.h (original)
+++ llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.h Tue Oct 27 21:40:04 2015
@@ -20,21 +20,52 @@
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/TypeBuilder.h"
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
+#include "llvm/Support/TargetSelect.h"
 #include <memory>
 
 namespace llvm {
 
+// Base class for Orc tests that will execute code.
+class OrcExecutionTest {
+public:
+
+  OrcExecutionTest() {
+    if (!NativeTargetInitialized) {
+      InitializeNativeTarget();
+      InitializeNativeTargetAsmParser();
+      InitializeNativeTargetAsmPrinter();
+      NativeTargetInitialized = true;
+    }
+  };
+
+  // Get a target machine for the host if it supports JIT execution.
+  std::unique_ptr<TargetMachine> getHostTargetMachineIfSupported() {
+    std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget());
+
+    const Triple& TT = TM->getTargetTriple();
+
+    if (TT.getArch() == Triple::x86_64)
+      return std::move(TM);
+
+    return nullptr;
+  }
+
+private:
+  static bool NativeTargetInitialized;
+};
+
 class ModuleBuilder {
 public:
   ModuleBuilder(LLVMContext &Context, StringRef Triple,
                 StringRef Name);
 
   template <typename FuncType>
-  Function* createFunctionDecl(Module *M, StringRef Name) {
+  Function* createFunctionDecl(StringRef Name) {
     return Function::Create(
              TypeBuilder<FuncType, false>::get(M->getContext()),
-             GlobalValue::ExternalLinkage, Name, M);
+             GlobalValue::ExternalLinkage, Name, M.get());
   }
 
   Module* getModule() { return M.get(); }




More information about the llvm-commits mailing list