llvm.org GIT mirror llvm / 6531c31
[ORC] Re-apply r327566 with a fix for test-global-ctors.ll. Also clang-formats the patch, which I should have done the first time around. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@327594 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 1 year, 4 months ago
19 changed file(s) with 140 addition(s) and 145 deletion(s). Raw diff Collapse all Expand all
5050 IRCompileLayer CompileLayer;
5151
5252 using OptimizeFunction =
53 std::functionshared_ptr(std::shared_ptr)>;
53 std::functionunique_ptr(std::unique_ptr)>;
5454
5555 IRTransformLayer OptimizeLayer;
5656
7676 std::make_shared(), Resolver};
7777 }),
7878 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
79 OptimizeLayer(CompileLayer, [this](std::shared_ptr M) {
79 OptimizeLayer(CompileLayer, [this](std::unique_ptr M) {
8080 return optimizeModule(std::move(M));
8181 }) {
8282 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
103103 }
104104
105105 private:
106 std::shared_ptr optimizeModule(std::shared_ptr M) {
106 std::unique_ptr optimizeModule(std::unique_ptr M) {
107107 // Create a function pass manager.
108108 auto FPM = llvm::make_unique(M.get());
109109
5454 IRCompileLayer CompileLayer;
5555
5656 using OptimizeFunction =
57 std::functionshared_ptr(std::shared_ptr)>;
57 std::functionunique_ptr(std::unique_ptr)>;
5858
5959 IRTransformLayer OptimizeLayer;
6060
7272 }),
7373 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
7474 OptimizeLayer(CompileLayer,
75 [this](std::shared_ptr M) {
75 [this](std::unique_ptr M) {
7676 return optimizeModule(std::move(M));
7777 }),
7878 CompileCallbackManager(
126126 }
127127
128128 private:
129 std::shared_ptr optimizeModule(std::shared_ptr M) {
129 std::unique_ptr optimizeModule(std::unique_ptr M) {
130130 // Create a function pass manager.
131131 auto FPM = llvm::make_unique(M.get());
132132
8080 IRCompileLayer CompileLayer;
8181
8282 using OptimizeFunction =
83 std::functionshared_ptr(std::shared_ptr)>;
83 std::functionunique_ptr(std::unique_ptr)>;
8484
8585 IRTransformLayer OptimizeLayer;
8686
112112 }),
113113 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
114114 OptimizeLayer(CompileLayer,
115 [this](std::shared_ptr M) {
115 [this](std::unique_ptr M) {
116116 return optimizeModule(std::move(M));
117117 }),
118118 CompileCallbackMgr(
206206 return MangledNameStream.str();
207207 }
208208
209 std::shared_ptr optimizeModule(std::shared_ptr M) {
209 std::unique_ptr optimizeModule(std::unique_ptr M) {
210210 // Create a function pass manager.
211211 auto FPM = llvm::make_unique(M.get());
212212
8585 IRCompileLayer CompileLayer;
8686
8787 using OptimizeFunction =
88 std::functionshared_ptr(std::shared_ptr)>;
88 std::functionunique_ptr(std::unique_ptr)>;
8989
9090 IRTransformLayer OptimizeLayer;
9191
120120 }),
121121 CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
122122 OptimizeLayer(CompileLayer,
123 [this](std::shared_ptr M) {
123 [this](std::unique_ptr M) {
124124 return optimizeModule(std::move(M));
125125 }),
126126 Remote(Remote) {
222222 return MangledNameStream.str();
223223 }
224224
225 std::shared_ptr optimizeModule(std::shared_ptr M) {
225 std::unique_ptr optimizeModule(std::unique_ptr M) {
226226 // Create a function pass manager.
227227 auto FPM = llvm::make_unique(M.get());
228228
137137
138138 struct LogicalDylib {
139139 struct SourceModuleEntry {
140 std::shared_ptr SourceMod;
140 std::unique_ptr SourceMod;
141141 std::set StubsToClone;
142142 };
143143
151151 : K(std::move(K)), BackingResolver(std::move(BackingResolver)),
152152 StubsMgr(std::move(StubsMgr)) {}
153153
154 SourceModuleHandle
155 addSourceModule(std::shared_ptr M) {
154 SourceModuleHandle addSourceModule(std::unique_ptr M) {
156155 SourceModuleHandle H = SourceModules.size();
157156 SourceModules.push_back(SourceModuleEntry());
158157 SourceModules.back().SourceMod = std::move(M);
231230 }
232231
233232 /// @brief Add a module to the compile-on-demand layer.
234 Error addModule(VModuleKey K, std::shared_ptr M) {
233 Error addModule(VModuleKey K, std::unique_ptr M) {
235234
236235 assert(!LogicalDylibs.count(K) && "VModuleKey K already in use");
237236 auto I = LogicalDylibs.insert(
243242 }
244243
245244 /// @brief Add extra modules to an existing logical module.
246 Error addExtraModule(VModuleKey K, std::shared_ptr M) {
245 Error addExtraModule(VModuleKey K, std::unique_ptr M) {
247246 return addLogicalModule(LogicalDylibs[K], std::move(M));
248247 }
249248
309308 }
310309
311310 private:
312
313 Error addLogicalModule(LogicalDylib &LD, std::shared_ptr SrcMPtr) {
311 Error addLogicalModule(LogicalDylib &LD, std::unique_ptr SrcMPtr) {
314312
315313 // Rename all static functions / globals to $static.X :
316314 // This will unique the names across all modules in the logical dylib,
104104 using CtorDtorTy = void (*)();
105105
106106 for (const auto &CtorDtorName : CtorDtorNames) {
107 dbgs() << "Searching for ctor/dtor: " << CtorDtorName << "...";
108107 if (auto CtorDtorSym = JITLayer.findSymbolIn(K, CtorDtorName, false)) {
109 dbgs() << " found symbol...";
110108 if (auto AddrOrErr = CtorDtorSym.getAddress()) {
111 dbgs() << " at addr " << format("0x%016x", *AddrOrErr) << "\n";
112109 CtorDtorTy CtorDtor =
113110 reinterpret_cast(static_cast(*AddrOrErr));
114111 CtorDtor();
115 } else {
116 dbgs() << " failed materialization!\n";
112 } else
117113 return AddrOrErr.takeError();
118 }
119114 } else {
120 dbgs() << " failed to find symbol...";
121115 if (auto Err = CtorDtorSym.takeError())
122116 return Err;
123117 else
3434 template
3535 class IRCompileLayer {
3636 public:
37 /// @brief Callback type for notifications when modules are compiled.
38 using NotifyCompiledCallback =
39 std::function)>;
3740
3841 /// @brief Construct an IRCompileLayer with the given BaseLayer, which must
3942 /// implement the ObjectLayer concept.
40 IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile)
41 : BaseLayer(BaseLayer), Compile(std::move(Compile)) {}
43 IRCompileLayer(
44 BaseLayerT &BaseLayer, CompileFtor Compile,
45 NotifyCompiledCallback NotifyCompiled = NotifyCompiledCallback())
46 : BaseLayer(BaseLayer), Compile(std::move(Compile)),
47 NotifyCompiled(std::move(NotifyCompiled)) {}
4248
4349 /// @brief Get a reference to the compiler functor.
4450 CompileFtor& getCompiler() { return Compile; }
4551
52 /// @brief (Re)set the NotifyCompiled callback.
53 void setNotifyCompiled(NotifyCompiledCallback NotifyCompiled) {
54 this->NotifyCompiled = std::move(NotifyCompiled);
55 }
56
4657 /// @brief Compile the module, and add the resulting object to the base layer
4758 /// along with the given memory manager and symbol resolver.
48 Error addModule(VModuleKey K, std::shared_ptr M) {
49 return BaseLayer.addObject(std::move(K), Compile(*M));
59 Error addModule(VModuleKey K, std::unique_ptr M) {
60 if (auto Err = BaseLayer.addObject(std::move(K), Compile(*M)))
61 return Err;
62 if (NotifyCompiled)
63 NotifyCompiled(std::move(K), std::move(M));
64 return Error::success();
5065 }
5166
5267 /// @brief Remove the module associated with the VModuleKey K.
8196 private:
8297 BaseLayerT &BaseLayer;
8398 CompileFtor Compile;
99 NotifyCompiledCallback NotifyCompiled;
84100 };
85101
86102 } // end namespace orc
3939 /// the layer below, along with the memory manager and symbol resolver.
4040 ///
4141 /// @return A handle for the added modules.
42 Error addModule(VModuleKey K, std::shared_ptr M) {
42 Error addModule(VModuleKey K, std::unique_ptr M) {
4343 return BaseLayer.addModule(std::move(K), Transform(std::move(M)));
4444 }
4545
4242 private:
4343 class EmissionDeferredModule {
4444 public:
45 EmissionDeferredModule(VModuleKey K, std::shared_ptr M)
45 EmissionDeferredModule(VModuleKey K, std::unique_ptr M)
4646 : K(std::move(K)), M(std::move(M)) {}
4747
4848 JITSymbol find(StringRef Name, bool ExportedSymbolsOnly, BaseLayerT &B) {
186186
187187 enum { NotEmitted, Emitting, Emitted } EmitState = NotEmitted;
188188 VModuleKey K;
189 std::shared_ptr M;
189 std::unique_ptr M;
190190 mutable std::unique_ptr> MangledSymbols;
191191 };
192192
199199 LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
200200
201201 /// @brief Add the given module to the lazy emitting layer.
202 Error addModule(VModuleKey K, std::shared_ptr M) {
202 Error addModule(VModuleKey K, std::unique_ptr M) {
203203 assert(!ModuleMap.count(K) && "VModuleKey K already in use");
204204 ModuleMap[K] =
205205 llvm::make_unique(std::move(K), std::move(M));
2828 extern "C" {
2929 #endif
3030
31 typedef struct LLVMOpaqueSharedModule *LLVMSharedModuleRef;
3231 typedef struct LLVMOrcOpaqueJITStack *LLVMOrcJITStackRef;
3332 typedef uint64_t LLVMOrcModuleHandle;
3433 typedef uint64_t LLVMOrcTargetAddress;
3736 void *CallbackCtx);
3837
3938 typedef enum { LLVMOrcErrSuccess = 0, LLVMOrcErrGeneric } LLVMOrcErrorCode;
40
41 /**
42 * Turn an LLVMModuleRef into an LLVMSharedModuleRef.
43 *
44 * The JIT uses shared ownership for LLVM modules, since it is generally
45 * difficult to know when the JIT will be finished with a module (and the JIT
46 * has no way of knowing when a user may be finished with one).
47 *
48 * Calling this method with an LLVMModuleRef creates a shared-pointer to the
49 * module, and returns a reference to this shared pointer.
50 *
51 * The shared module should be disposed when finished with by calling
52 * LLVMOrcDisposeSharedModule (not LLVMDisposeModule). The Module will be
53 * deleted when the last shared pointer owner relinquishes it.
54 */
55
56 LLVMSharedModuleRef LLVMOrcMakeSharedModule(LLVMModuleRef Mod);
57
58 /**
59 * Dispose of a shared module.
60 *
61 * The module should not be accessed after this call. The module will be
62 * deleted once all clients (including the JIT itself) have released their
63 * shared pointers.
64 */
65
66 void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod);
6739
6840 /**
6941 * Create an ORC JIT stack.
12496 */
12597 LLVMOrcErrorCode
12698 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
127 LLVMOrcModuleHandle *RetHandle,
128 LLVMSharedModuleRef Mod,
99 LLVMOrcModuleHandle *RetHandle, LLVMModuleRef Mod,
129100 LLVMOrcSymbolResolverFn SymbolResolver,
130101 void *SymbolResolverCtx);
131102
134105 */
135106 LLVMOrcErrorCode
136107 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
137 LLVMOrcModuleHandle *RetHandle,
138 LLVMSharedModuleRef Mod,
108 LLVMOrcModuleHandle *RetHandle, LLVMModuleRef Mod,
139109 LLVMOrcSymbolResolverFn SymbolResolver,
140110 void *SymbolResolverCtx);
141111
6666 }
6767
6868 ConstantInt *Priority = dyn_cast(CS->getOperand(0));
69 Value *Data = CS->getOperand(2);
69 Value *Data = CS->getNumOperands() == 3 ? CS->getOperand(2) : nullptr;
7070 return Element(Priority->getZExtValue(), Func, Data);
7171 }
7272
1010 #include "llvm-c/OrcBindings.h"
1111
1212 using namespace llvm;
13
14 LLVMSharedModuleRef LLVMOrcMakeSharedModule(LLVMModuleRef Mod) {
15 return wrap(new std::shared_ptr(unwrap(Mod)));
16 }
17
18 void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod) {
19 delete unwrap(SharedMod);
20 }
2113
2214 LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) {
2315 TargetMachine *TM2(unwrap(TM));
7466
7567 LLVMOrcErrorCode
7668 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
77 LLVMOrcModuleHandle *RetHandle,
78 LLVMSharedModuleRef Mod,
69 LLVMOrcModuleHandle *RetHandle, LLVMModuleRef Mod,
7970 LLVMOrcSymbolResolverFn SymbolResolver,
8071 void *SymbolResolverCtx) {
8172 OrcCBindingsStack &J = *unwrap(JITStack);
82 std::shared_ptr *M(unwrap(Mod));
83 return J.addIRModuleEager(*RetHandle, *M, SymbolResolver, SymbolResolverCtx);
73 std::unique_ptr M(unwrap(Mod));
74 return J.addIRModuleEager(*RetHandle, std::move(M), SymbolResolver,
75 SymbolResolverCtx);
8476 }
8577
8678 LLVMOrcErrorCode
8779 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
88 LLVMOrcModuleHandle *RetHandle,
89 LLVMSharedModuleRef Mod,
80 LLVMOrcModuleHandle *RetHandle, LLVMModuleRef Mod,
9081 LLVMOrcSymbolResolverFn SymbolResolver,
9182 void *SymbolResolverCtx) {
9283 OrcCBindingsStack &J = *unwrap(JITStack);
93 std::shared_ptr *M(unwrap(Mod));
94 return J.addIRModuleLazy(*RetHandle, *M, SymbolResolver, SymbolResolverCtx);
84 std::unique_ptr M(unwrap(Mod));
85 return J.addIRModuleLazy(*RetHandle, std::move(M), SymbolResolver,
86 SymbolResolverCtx);
9587 }
9688
9789 LLVMOrcErrorCode
4242
4343 class OrcCBindingsStack;
4444
45 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::shared_ptr,
46 LLVMSharedModuleRef)
4745 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcCBindingsStack, LLVMOrcJITStackRef)
4846 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
4947
283281 }
284282 template
285283 LLVMOrcErrorCode
286 addIRModule(orc::VModuleKey &RetKey, LayerT &Layer, std::shared_ptr M,
284 addIRModule(orc::VModuleKey &RetKey, LayerT &Layer, std::unique_ptr M,
287285 std::unique_ptr MemMgr,
288286 LLVMOrcSymbolResolverFn ExternalResolver,
289287 void *ExternalResolverCtx) {
322320 }
323321
324322 LLVMOrcErrorCode addIRModuleEager(orc::VModuleKey &RetKey,
325 std::shared_ptr M,
323 std::unique_ptr M,
326324 LLVMOrcSymbolResolverFn ExternalResolver,
327325 void *ExternalResolverCtx) {
328326 return addIRModule(RetKey, CompileLayer, std::move(M),
331329 }
332330
333331 LLVMOrcErrorCode addIRModuleLazy(orc::VModuleKey &RetKey,
334 std::shared_ptr M,
332 std::unique_ptr M,
335333 LLVMOrcSymbolResolverFn ExternalResolver,
336334 void *ExternalResolverCtx) {
337335 return addIRModule(RetKey, CODLayer, std::move(M),
124124 }
125125
126126 void OrcMCJITReplacement::runStaticConstructorsDestructors(bool isDtors) {
127 for (auto &M : LocalModules)
128 ExecutionEngine::runStaticConstructorsDestructors(*M, isDtors);
127 auto &CtorDtorsMap = isDtors ? UnexecutedDestructors : UnexecutedConstructors;
128
129 for (auto &KV : CtorDtorsMap)
130 cantFail(CtorDtorRunner(std::move(KV.second), KV.first)
131 .runViaLayer(LazyEmitLayer));
132
133 CtorDtorsMap.clear();
129134 }
130135
131136 } // End namespace orc.
2020 #include "llvm/ExecutionEngine/GenericValue.h"
2121 #include "llvm/ExecutionEngine/JITSymbol.h"
2222 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
23 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
2324 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2425 #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
2526 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
5354 namespace orc {
5455
5556 class OrcMCJITReplacement : public ExecutionEngine {
57
5658 // OrcMCJITReplacement needs to do a little extra book-keeping to ensure that
5759 // Orc's automatic finalization doesn't kick in earlier than MCJIT clients are
5860 // expecting - see finalizeMemory.
234236 return ObjectLayerT::Resources{this->MemMgr, this->Resolver};
235237 },
236238 NotifyObjectLoaded, NotifyFinalized),
237 CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)),
239 CompileLayer(ObjectLayer, SimpleCompiler(*this->TM),
240 [this](VModuleKey K, std::unique_ptr M) {
241 Modules.push_back(std::move(M));
242 }),
238243 LazyEmitLayer(CompileLayer) {}
239244
240245 static void Register() {
249254 } else {
250255 assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
251256 }
252 auto *MPtr = M.release();
253 ShouldDelete[MPtr] = true;
254 auto Deleter = [this](Module *Mod) {
255 auto I = ShouldDelete.find(Mod);
256 if (I != ShouldDelete.end() && I->second)
257 delete Mod;
258 };
259 LocalModules.push_back(std::shared_ptr(MPtr, std::move(Deleter)));
260 cantFail(
261 LazyEmitLayer.addModule(ES.allocateVModule(), LocalModules.back()));
257
258 // Rename, bump linkage and record static constructors and destructors.
259 // We have to do this before we hand over ownership of the module to the
260 // JIT.
261 std::vector CtorNames, DtorNames;
262 {
263 unsigned CtorId = 0, DtorId = 0;
264 for (auto Ctor : orc::getConstructors(*M)) {
265 std::string NewCtorName = ("$static_ctor." + Twine(CtorId++)).str();
266 Ctor.Func->setName(NewCtorName);
267 Ctor.Func->setLinkage(GlobalValue::ExternalLinkage);
268 Ctor.Func->setVisibility(GlobalValue::HiddenVisibility);
269 CtorNames.push_back(mangle(NewCtorName));
270 }
271 for (auto Dtor : orc::getDestructors(*M)) {
272 std::string NewDtorName = ("$static_dtor." + Twine(DtorId++)).str();
273 dbgs() << "Found dtor: " << NewDtorName << "\n";
274 Dtor.Func->setName(NewDtorName);
275 Dtor.Func->setLinkage(GlobalValue::ExternalLinkage);
276 Dtor.Func->setVisibility(GlobalValue::HiddenVisibility);
277 DtorNames.push_back(mangle(NewDtorName));
278 }
279 }
280
281 auto K = ES.allocateVModule();
282
283 UnexecutedConstructors[K] = std::move(CtorNames);
284 UnexecutedDestructors[K] = std::move(DtorNames);
285
286 cantFail(LazyEmitLayer.addModule(K, std::move(M)));
262287 }
263288
264289 void addObjectFile(std::unique_ptr O) override {
276301 void addArchive(object::OwningBinary A) override {
277302 Archives.push_back(std::move(A));
278303 }
279
304
280305 bool removeModule(Module *M) override {
281 for (auto I = LocalModules.begin(), E = LocalModules.end(); I != E; ++I) {
282 if (I->get() == M) {
283 ShouldDelete[M] = false;
284 LocalModules.erase(I);
285 return true;
286 }
287 }
288 return false;
306 auto I = Modules.begin();
307 for (auto E = Modules.end(); I != E; ++I)
308 if (I->get() == M)
309 break;
310 if (I == Modules.end())
311 return false;
312 Modules.erase(I);
313 return true;
289314 }
290315
291316 uint64_t getSymbolAddress(StringRef Name) {
293318 }
294319
295320 JITSymbol findSymbol(StringRef Name) {
296 return findMangledSymbol(Mangle(Name));
321 return findMangledSymbol(mangle(Name));
297322 }
298323
299324 void finalizeObject() override {
411436 OrcMCJITReplacement &M;
412437 };
413438
414 std::string Mangle(StringRef Name) {
439 std::string mangle(StringRef Name) {
415440 std::string MangledName;
416441 {
417442 raw_string_ostream MangledNameStream(MangledName);
437462 // delete blocks in LocalModules refer to the ShouldDelete map, so
438463 // LocalModules needs to be destructed before ShouldDelete.
439464 std::map ShouldDelete;
440 std::vector> LocalModules;
441465
442466 NotifyObjectLoadedT NotifyObjectLoaded;
443467 NotifyFinalizedT NotifyFinalized;
445469 ObjectLayerT ObjectLayer;
446470 CompileLayerT CompileLayer;
447471 LazyEmitLayerT LazyEmitLayer;
472
473 std::map> UnexecutedConstructors;
474 std::map> UnexecutedDestructors;
448475
449476 // We need to store ObjLayerT::ObjSetHandles for each of the object sets
450477 // that have been emitted but not yet finalized so that we can forward the
5353 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
5454 switch (OrcDumpKind) {
5555 case DumpKind::NoDump:
56 return [](std::shared_ptr M) { return M; };
56 return [](std::unique_ptr M) { return M; };
5757
5858 case DumpKind::DumpFuncsToStdOut:
59 return [](std::shared_ptr M) {
59 return [](std::unique_ptr M) {
6060 printf("[ ");
6161
6262 for (const auto &F : *M) {
7575 };
7676
7777 case DumpKind::DumpModsToStdOut:
78 return [](std::shared_ptr M) {
79 outs() << "----- Module Start -----\n" << *M
80 << "----- Module End -----\n";
78 return [](std::unique_ptr M) {
79 outs() << "----- Module Start -----\n"
80 << *M << "----- Module End -----\n";
8181
82 return M;
83 };
82 return M;
83 };
8484
8585 case DumpKind::DumpModsToDisk:
86 return [](std::shared_ptr M) {
87 std::error_code EC;
88 raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
89 sys::fs::F_Text);
90 if (EC) {
91 errs() << "Couldn't open " << M->getModuleIdentifier()
92 << " for dumping.\nError:" << EC.message() << "\n";
93 exit(1);
94 }
95 Out << *M;
96 return M;
97 };
86 return [](std::unique_ptr M) {
87 std::error_code EC;
88 raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC, sys::fs::F_Text);
89 if (EC) {
90 errs() << "Couldn't open " << M->getModuleIdentifier()
91 << " for dumping.\nError:" << EC.message() << "\n";
92 exit(1);
93 }
94 Out << *M;
95 return M;
96 };
9897 }
9998 llvm_unreachable("Unknown DumpKind");
10099 }
147146
148147 // Add the module, look up main and run it.
149148 for (auto &M : Ms)
150 cantFail(J.addModule(std::shared_ptr(std::move(M))));
149 cantFail(J.addModule(std::move(M)));
151150
152151 if (auto MainSym = J.findSymbol("main")) {
153152 typedef int (*MainFnPtr)(int, const char*[]);
5050 using ObjLayerT = orc::RTDyldObjectLinkingLayer;
5151 using CompileLayerT = orc::IRCompileLayer;
5252 using TransformFtor =
53 std::function(std::shared_ptr)>;
53 std::function(std::unique_ptr)>;
5454 using IRDumpLayerT = orc::IRTransformLayer;
5555 using CODLayerT = orc::CompileOnDemandLayer;
5656 using IndirectStubsManagerBuilder = CODLayerT::IndirectStubsManagerBuilderT;
105105 }
106106 }
107107
108 Error addModule(std::shared_ptr M) {
108 Error addModule(std::unique_ptr M) {
109109 if (M->getDataLayout().isDefault())
110110 M->setDataLayout(DL);
111111
1313 #include "llvm/ExecutionEngine/Orc/NullResolver.h"
1414 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
1515 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
16 #include "llvm/IR/Module.h"
1617 #include "llvm/Object/ObjectFile.h"
1718 #include "gtest/gtest.h"
1819
301302 // Make sure that the calls from IRCompileLayer to ObjectTransformLayer
302303 // compile.
303304 cantFail(CompileLayer.addModule(ES.allocateVModule(),
304 std::shared_ptr()));
305 std::unique_ptr()));
305306
306307 // Make sure that the calls from ObjectTransformLayer to ObjectLinkingLayer
307308 // compile.
7272 CompileContext *CCtx = static_cast(Ctx);
7373 auto *ET = CCtx->APIExecTest;
7474 CCtx->M = ET->createTestModule(ET->TM->getTargetTriple());
75 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(CCtx->M.release()));
76 LLVMOrcAddEagerlyCompiledIR(JITStack, &CCtx->H, SM, myResolver, nullptr);
77 LLVMOrcDisposeSharedModuleRef(SM);
75 LLVMOrcAddEagerlyCompiledIR(JITStack, &CCtx->H, wrap(CCtx->M.release()),
76 myResolver, nullptr);
7877 CCtx->Compiled = true;
7978 LLVMOrcTargetAddress MainAddr;
8079 LLVMOrcGetSymbolAddress(JITStack, &MainAddr, "main");
9695
9796 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
9897
99 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
10098 LLVMOrcModuleHandle H;
101 LLVMOrcAddEagerlyCompiledIR(JIT, &H, SM, myResolver, nullptr);
102 LLVMOrcDisposeSharedModuleRef(SM);
99 LLVMOrcAddEagerlyCompiledIR(JIT, &H, wrap(M.release()), myResolver, nullptr);
103100 LLVMOrcTargetAddress MainAddr;
104101 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main");
105102 MainFnTy MainFn = (MainFnTy)MainAddr;
124121
125122 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
126123
127 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
128124 LLVMOrcModuleHandle H;
129 LLVMOrcAddLazilyCompiledIR(JIT, &H, SM, myResolver, nullptr);
130 LLVMOrcDisposeSharedModuleRef(SM);
125 LLVMOrcAddLazilyCompiledIR(JIT, &H, wrap(M.release()), myResolver, nullptr);
131126 LLVMOrcTargetAddress MainAddr;
132127 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main");
133128 MainFnTy MainFn = (MainFnTy)MainAddr;