llvm.org GIT mirror llvm / 5ef21dc
[ORC] Hook up the LLVMOrcAddObjectFile function in the Orc C Bindings. This can be used to add a relocatable object to the JIT session. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@313474 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 2 years ago
4 changed file(s) with 155 addition(s) and 63 deletion(s). Raw diff Collapse all Expand all
2929 #endif
3030
3131 typedef struct LLVMOpaqueSharedModule *LLVMSharedModuleRef;
32 typedef struct LLVMOpaqueSharedObjectBuffer *LLVMSharedObjectBufferRef;
3332 typedef struct LLVMOrcOpaqueJITStack *LLVMOrcJITStackRef;
3433 typedef uint32_t LLVMOrcModuleHandle;
3534 typedef uint64_t LLVMOrcTargetAddress;
6564 */
6665
6766 void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod);
68
69 /**
70 * Get an LLVMSharedObjectBufferRef from an LLVMMemoryBufferRef.
71 */
72 LLVMSharedObjectBufferRef
73 LLVMOrcMakeSharedObjectBuffer(LLVMMemoryBufferRef ObjBuffer);
74
75 /**
76 * Dispose of a shared object buffer.
77 */
78 void
79 LLVMOrcDisposeSharedObjectBufferRef(LLVMSharedObjectBufferRef SharedObjBuffer);
8067
8168 /**
8269 * Create an ORC JIT stack.
154141
155142 /**
156143 * Add an object file.
144 *
145 * This method takes ownership of the given memory buffer and attempts to add
146 * it to the JIT as an object file.
147 * Clients should *not* dispose of the 'Obj' argument: the JIT will manage it
148 * from this call onwards.
157149 */
158150 LLVMOrcErrorCode LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack,
159151 LLVMOrcModuleHandle *RetHandle,
160 LLVMSharedObjectBufferRef Obj,
152 LLVMMemoryBufferRef Obj,
161153 LLVMOrcSymbolResolverFn SymbolResolver,
162154 void *SymbolResolverCtx);
163155
1717
1818 void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod) {
1919 delete unwrap(SharedMod);
20 }
21
22 LLVMSharedObjectBufferRef
23 LLVMOrcMakeSharedObjectBuffer(LLVMMemoryBufferRef ObjBuffer) {
24 return wrap(new std::shared_ptr(unwrap(ObjBuffer)));
25 }
26
27 void
28 LLVMOrcDisposeSharedObjectBufferRef(LLVMSharedObjectBufferRef SharedObjBuffer) {
29 delete unwrap(SharedObjBuffer);
3020 }
3121
3222 LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) {
10494 return J.addIRModuleLazy(*RetHandle, *M, SymbolResolver, SymbolResolverCtx);
10595 }
10696
97 LLVMOrcErrorCode
98 LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack,
99 LLVMOrcModuleHandle *RetHandle,
100 LLVMMemoryBufferRef Obj,
101 LLVMOrcSymbolResolverFn SymbolResolver,
102 void *SymbolResolverCtx) {
103 OrcCBindingsStack &J = *unwrap(JITStack);
104 std::unique_ptr O(unwrap(Obj));
105 return J.addObject(*RetHandle, std::move(O), SymbolResolver,
106 SymbolResolverCtx);
107 }
108
107109 LLVMOrcErrorCode LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack,
108110 LLVMOrcModuleHandle H) {
109111 OrcCBindingsStack &J = *unwrap(JITStack);
4343
4444 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::shared_ptr,
4545 LLVMSharedModuleRef)
46 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::shared_ptr,
47 LLVMSharedObjectBufferRef)
4846 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcCBindingsStack, LLVMOrcJITStackRef)
4947 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
48
49 namespace detail {
50
51
52 class GenericHandle {
53 public:
54 virtual ~GenericHandle() = default;
55
56 virtual JITSymbol findSymbolIn(const std::string &Name,
57 bool ExportedSymbolsOnly) = 0;
58 virtual Error removeModule() = 0;
59 };
60
61 template class GenericHandleImpl : public GenericHandle {
62 public:
63 GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleHandleT Handle)
64 : Layer(Layer), Handle(std::move(Handle)) {}
65
66 JITSymbol findSymbolIn(const std::string &Name,
67 bool ExportedSymbolsOnly) override {
68 return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
69 }
70
71 Error removeModule() override { return Layer.removeModule(Handle); }
72
73 private:
74 LayerT &Layer;
75 typename LayerT::ModuleHandleT Handle;
76 };
77
78 template <>
79 class GenericHandleImpl
80 : public GenericHandle {
81 private:
82 using LayerT = orc::RTDyldObjectLinkingLayer;
83 public:
84
85 GenericHandleImpl(LayerT &Layer, typename LayerT::ObjHandleT Handle)
86 : Layer(Layer), Handle(std::move(Handle)) {}
87
88 JITSymbol findSymbolIn(const std::string &Name,
89 bool ExportedSymbolsOnly) override {
90 return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
91 }
92
93 Error removeModule() override { return Layer.removeObject(Handle); }
94
95 private:
96 LayerT &Layer;
97 typename LayerT::ObjHandleT Handle;
98 };
99
100
101 template
102 std::unique_ptr>
103 createGenericHandle(LayerT &Layer, HandleT Handle) {
104 return llvm::make_unique>(Layer,
105 std::move(Handle));
106 }
107
108 } // end namespace detail
50109
51110 class OrcCBindingsStack {
52111 public:
63122 using IndirectStubsManagerBuilder = CODLayerT::IndirectStubsManagerBuilderT;
64123
65124 private:
66 class GenericHandle {
67 public:
68 virtual ~GenericHandle() = default;
69
70 virtual JITSymbol findSymbolIn(const std::string &Name,
71 bool ExportedSymbolsOnly) = 0;
72 virtual Error removeModule() = 0;
73 };
74
75 template class GenericHandleImpl : public GenericHandle {
76 public:
77 GenericHandleImpl(LayerT &Layer, typename LayerT::ModuleHandleT Handle)
78 : Layer(Layer), Handle(std::move(Handle)) {}
79
80 JITSymbol findSymbolIn(const std::string &Name,
81 bool ExportedSymbolsOnly) override {
82 return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
83 }
84
85 Error removeModule() override { return Layer.removeModule(Handle); }
86
87 private:
88 LayerT &Layer;
89 typename LayerT::ModuleHandleT Handle;
90 };
91
92 template
93 std::unique_ptr>
94 createGenericHandle(LayerT &Layer, typename LayerT::ModuleHandleT Handle) {
95 return llvm::make_unique>(Layer,
96 std::move(Handle));
97 }
125
126 using OwningObject = object::OwningBinary;
98127
99128 public:
100129 using ModuleHandleT = unsigned;
263292 GenericHandles[H] = nullptr;
264293 FreeHandleIndexes.push_back(H);
265294 return LLVMOrcErrSuccess;
295 }
296
297 LLVMOrcErrorCode addObject(ModuleHandleT &RetHandle,
298 std::unique_ptr ObjBuffer,
299 LLVMOrcSymbolResolverFn ExternalResolver,
300 void *ExternalResolverCtx) {
301 if (auto ObjOrErr =
302 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef())) {
303 auto &Obj = *ObjOrErr;
304 auto OwningObj =
305 std::make_shared(std::move(Obj), std::move(ObjBuffer));
306
307 // Create the resolver.
308 auto Resolver = createResolver(ExternalResolver, ExternalResolverCtx);
309
310 ModuleHandleT H;
311 if (auto HOrErr = ObjectLayer.addObject(std::move(OwningObj),
312 std::move(Resolver)))
313 H = createHandle(ObjectLayer, *HOrErr);
314 else
315 return mapError(HOrErr.takeError());
316
317 RetHandle = H;
318
319 return LLVMOrcErrSuccess;
320 } else
321 return mapError(ObjOrErr.takeError());
266322 }
267323
268324 JITSymbol findSymbol(const std::string &Name,
300356 const std::string &getErrorMessage() const { return ErrMsg; }
301357
302358 private:
303 template
304 unsigned createHandle(LayerT &Layer, typename LayerT::ModuleHandleT Handle) {
359 template
360 unsigned createHandle(LayerT &Layer, HandleT Handle) {
305361 unsigned NewHandle;
306362 if (!FreeHandleIndexes.empty()) {
307363 NewHandle = FreeHandleIndexes.back();
308364 FreeHandleIndexes.pop_back();
309 GenericHandles[NewHandle] = createGenericHandle(Layer, std::move(Handle));
365 GenericHandles[NewHandle] =
366 detail::createGenericHandle(Layer, std::move(Handle));
310367 return NewHandle;
311368 } else {
312369 NewHandle = GenericHandles.size();
313 GenericHandles.push_back(createGenericHandle(Layer, std::move(Handle)));
370 GenericHandles.push_back(
371 detail::createGenericHandle(Layer, std::move(Handle)));
314372 }
315373 return NewHandle;
316374 }
337395 CompileLayerT CompileLayer;
338396 CODLayerT CODLayer;
339397
340 std::vectorGenericHandle>> GenericHandles;
398 std::vectordetail::GenericHandle>> GenericHandles;
341399 std::vector FreeHandleIndexes;
342400
343401 orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
77 //===----------------------------------------------------------------------===//
88
99 #include "OrcTestCommon.h"
10 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
1011 #include "llvm-c/Core.h"
1112 #include "llvm-c/OrcBindings.h"
1213 #include "llvm-c/Target.h"
3435 B.CreateRet(Result);
3536
3637 return MB.takeModule();
38 }
39
40 std::shared_ptr>
41 createTestObject() {
42 orc::SimpleCompiler IRCompiler(*TM);
43 auto M = createTestModule(TM->getTargetTriple());
44 M->setDataLayout(TM->createDataLayout());
45 return std::make_shared>(
46 IRCompiler(*M));
3747 }
3848
3949 typedef int (*MainFnTy)();
133143 LLVMOrcDisposeInstance(JIT);
134144 }
135145
146 TEST_F(OrcCAPIExecutionTest, TestAddObjectFile) {
147 if (!TM)
148 return;
149
150 std::unique_ptr ObjBuffer;
151 {
152 auto OwningObj = createTestObject();
153 auto ObjAndBuffer = OwningObj->takeBinary();
154 ObjBuffer = std::move(ObjAndBuffer.second);
155 }
156
157 LLVMOrcJITStackRef JIT =
158 LLVMOrcCreateInstance(wrap(TM.get()));
159 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
160
161 LLVMOrcModuleHandle H;
162 LLVMOrcAddObjectFile(JIT, &H, wrap(ObjBuffer.release()), myResolver, nullptr);
163 LLVMOrcTargetAddress MainAddr;
164 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main");
165 MainFnTy MainFn = (MainFnTy)MainAddr;
166 int Result = MainFn();
167 EXPECT_EQ(Result, 42)
168 << "Lazily JIT'd code did not return expected result";
169
170 LLVMOrcRemoveModule(JIT, H);
171
172 LLVMOrcDisposeMangledSymbol(testFuncName);
173 LLVMOrcDisposeInstance(JIT);
174 }
175
136176 TEST_F(OrcCAPIExecutionTest, TestDirectCallbacksAPI) {
137177 if (!TM)
138178 return;