llvm.org GIT mirror llvm / a817935
[ORC] Errorize the ORC APIs. This patch updates the ORC layers and utilities to return and propagate llvm::Errors where appropriate. This is necessary to allow ORC to safely handle error cases in cross-process and remote JITing. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@307350 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 2 years ago
42 changed file(s) with 580 addition(s) and 291 deletion(s). Raw diff Collapse all Expand all
7474
7575 // Add the set to the JIT with the resolver we created above and a newly
7676 // created SectionMemoryManager.
77 return CompileLayer.addModule(std::move(M),
78 std::move(Resolver));
77 return cantFail(CompileLayer.addModule(std::move(M),
78 std::move(Resolver)));
7979 }
8080
8181 JITSymbol findSymbol(const std::string Name) {
8686 }
8787
8888 void removeModule(ModuleHandle H) {
89 CompileLayer.removeModule(H);
89 cantFail(CompileLayer.removeModule(H));
9090 }
9191 };
9292
11491149
11501150 // Get the symbol's address and cast it to the right type (takes no
11511151 // arguments, returns a double) so we can call it as a native function.
1152 double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
1152 double (*FP)() = (double (*)())(intptr_t)cantFail(ExprSymbol.getAddress());
11531153 fprintf(stderr, "Evaluated to %f\n", FP());
11541154
11551155 // Delete the anonymous expression module from the JIT.
8787
8888 // Add the set to the JIT with the resolver we created above and a newly
8989 // created SectionMemoryManager.
90 return OptimizeLayer.addModule(std::move(M),
91 std::move(Resolver));
90 return cantFail(OptimizeLayer.addModule(std::move(M),
91 std::move(Resolver)));
9292 }
9393
9494 JITSymbol findSymbol(const std::string Name) {
9999 }
100100
101101 void removeModule(ModuleHandle H) {
102 OptimizeLayer.removeModule(H);
102 cantFail(OptimizeLayer.removeModule(H));
103103 }
104104
105105 private:
11491149
11501150 // Get the symbol's address and cast it to the right type (takes no
11511151 // arguments, returns a double) so we can call it as a native function.
1152 double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
1152 double (*FP)() = (double (*)())(intptr_t)cantFail(ExprSymbol.getAddress());
11531153 fprintf(stderr, "Evaluated to %f\n", FP());
11541154
11551155 // Delete the anonymous expression module from the JIT.
100100
101101 // Add the set to the JIT with the resolver we created above and a newly
102102 // created SectionMemoryManager.
103 return CODLayer.addModule(std::move(M),
104 std::move(Resolver));
103 return cantFail(CODLayer.addModule(std::move(M), std::move(Resolver)));
105104 }
106105
107106 JITSymbol findSymbol(const std::string Name) {
112111 }
113112
114113 void removeModule(ModuleHandle H) {
115 CODLayer.removeModule(H);
114 cantFail(CODLayer.removeModule(H));
116115 }
117116
118117 private:
11491149
11501150 // Get the symbol's address and cast it to the right type (takes no
11511151 // arguments, returns a double) so we can call it as a native function.
1152 double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
1152 double (*FP)() = (double (*)())(intptr_t)cantFail(ExprSymbol.getAddress());
11531153 fprintf(stderr, "Evaluated to %f\n", FP());
11541154
11551155 // Delete the anonymous expression module from the JIT.
127127
128128 // Add the set to the JIT with the resolver we created above and a newly
129129 // created SectionMemoryManager.
130 return OptimizeLayer.addModule(std::move(M),
131 std::move(Resolver));
130 return cantFail(OptimizeLayer.addModule(std::move(M),
131 std::move(Resolver)));
132132 }
133133
134134 Error addFunctionAST(std::unique_ptr FnAST) {
174174 addModule(std::move(M));
175175 auto Sym = findSymbol(SharedFnAST->getName() + "$impl");
176176 assert(Sym && "Couldn't find compiled function?");
177 JITTargetAddress SymAddr = Sym.getAddress();
177 JITTargetAddress SymAddr = cantFail(Sym.getAddress());
178178 if (auto Err =
179179 IndirectStubsMgr->updatePointer(mangle(SharedFnAST->getName()),
180180 SymAddr)) {
194194 }
195195
196196 void removeModule(ModuleHandle H) {
197 OptimizeLayer.removeModule(H);
197 cantFail(OptimizeLayer.removeModule(H));
198198 }
199199
200200 private:
11521152
11531153 // Get the symbol's address and cast it to the right type (takes no
11541154 // arguments, returns a double) so we can call it as a native function.
1155 double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
1155 double (*FP)() = (double (*)())(intptr_t)cantFail(ExprSymbol.getAddress());
11561156 fprintf(stderr, "Evaluated to %f\n", FP());
11571157
11581158 // Delete the anonymous expression module from the JIT.
156156
157157 // Add the set to the JIT with the resolver we created above and a newly
158158 // created SectionMemoryManager.
159 return OptimizeLayer.addModule(std::move(M),
160 std::move(Resolver));
159 return cantFail(OptimizeLayer.addModule(std::move(M),
160 std::move(Resolver)));
161161 }
162162
163163 Error addFunctionAST(std::unique_ptr FnAST) {
203203 addModule(std::move(M));
204204 auto Sym = findSymbol(SharedFnAST->getName() + "$impl");
205205 assert(Sym && "Couldn't find compiled function?");
206 JITTargetAddress SymAddr = Sym.getAddress();
206 JITTargetAddress SymAddr = cantFail(Sym.getAddress());
207207 if (auto Err =
208208 IndirectStubsMgr->updatePointer(mangle(SharedFnAST->getName()),
209209 SymAddr)) {
227227 }
228228
229229 void removeModule(ModuleHandle H) {
230 OptimizeLayer.removeModule(H);
230 cantFail(OptimizeLayer.removeModule(H));
231231 }
232232
233233 private:
11761176
11771177 // Get the symbol's address and cast it to the right type (takes no
11781178 // arguments, returns a double) so we can call it as a native function.
1179 ExitOnErr(TheJIT->executeRemoteExpr(ExprSymbol.getAddress()));
1179 ExitOnErr(TheJIT->executeRemoteExpr(cantFail(ExprSymbol.getAddress())));
11801180
11811181 // Delete the anonymous expression module from the JIT.
11821182 TheJIT->removeModule(H);
610610
611611 // Get the symbol's address and cast it to the right type (takes no
612612 // arguments, returns a double) so we can call it as a native function.
613 double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
613 double (*FP)() = (double (*)())(intptr_t)cantFail(ExprSymbol.getAddress());
614614 fprintf(stderr, "Evaluated to %f\n", FP());
615615
616616 // Delete the anonymous expression module from the JIT.
884884
885885 // Get the symbol's address and cast it to the right type (takes no
886886 // arguments, returns a double) so we can call it as a native function.
887 double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
887 double (*FP)() = (double (*)())(intptr_t)cantFail(ExprSymbol.getAddress());
888888 fprintf(stderr, "Evaluated to %f\n", FP());
889889
890890 // Delete the anonymous expression module from the JIT.
10031003
10041004 // Get the symbol's address and cast it to the right type (takes no
10051005 // arguments, returns a double) so we can call it as a native function.
1006 double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
1006 double (*FP)() = (double (*)())(intptr_t)cantFail(ExprSymbol.getAddress());
10071007 fprintf(stderr, "Evaluated to %f\n", FP());
10081008
10091009 // Delete the anonymous expression module from the JIT.
11721172
11731173 // Get the symbol's address and cast it to the right type (takes no
11741174 // arguments, returns a double) so we can call it as a native function.
1175 double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
1175 double (*FP)() = (double (*)())(intptr_t)cantFail(ExprSymbol.getAddress());
11761176 fprintf(stderr, "Evaluated to %f\n", FP());
11771177
11781178 // Delete the anonymous expression module from the JIT.
6262 return JITSymbol(nullptr);
6363 },
6464 [](const std::string &S) { return nullptr; });
65 auto H = CompileLayer.addModule(std::move(M),
66 std::move(Resolver));
65 auto H = cantFail(CompileLayer.addModule(std::move(M),
66 std::move(Resolver)));
6767
6868 ModuleHandles.push_back(H);
6969 return H;
7171
7272 void removeModule(ModuleHandleT H) {
7373 ModuleHandles.erase(find(ModuleHandles, H));
74 CompileLayer.removeModule(H);
74 cantFail(CompileLayer.removeModule(H));
7575 }
7676
7777 JITSymbol findSymbol(const std::string Name) {
2020 #include
2121 #include
2222
23 #include "llvm/Support/Error.h"
24
2325 namespace llvm {
2426
2527 class GlobalValue;
4042
4143 enum FlagNames : UnderlyingType {
4244 None = 0,
43 Weak = 1U << 0,
44 Common = 1U << 1,
45 Absolute = 1U << 2,
46 Exported = 1U << 3
45 HasError = 1U << 0,
46 Weak = 1U << 1,
47 Common = 1U << 2,
48 Absolute = 1U << 3,
49 Exported = 1U << 4
4750 };
4851
4952 /// @brief Default-construct a JITSymbolFlags instance.
5154
5255 /// @brief Construct a JITSymbolFlags instance from the given flags.
5356 JITSymbolFlags(FlagNames Flags) : Flags(Flags) {}
57
58 /// @brief Return true if there was an error retrieving this symbol.
59 bool hasError() const {
60 return (Flags & HasError) == HasError;
61 }
5462
5563 /// @brief Returns true is the Weak flag is set.
5664 bool isWeak() const {
112120 /// @brief Represents a symbol in the JIT.
113121 class JITSymbol {
114122 public:
115 using GetAddressFtor = std::function;
116
117 /// @brief Create a 'null' symbol that represents failure to find a symbol
118 /// definition.
119 JITSymbol(std::nullptr_t) {}
123 using GetAddressFtor = std::function()>;
124
125 /// @brief Create a 'null' symbol, used to represent a "symbol not found"
126 /// result from a successful (non-erroneous) lookup.
127 JITSymbol(std::nullptr_t)
128 : CachedAddr(0) {}
129
130 /// @brief Create a JITSymbol representing an error in the symbol lookup
131 /// process (e.g. a network failure during a remote lookup).
132 JITSymbol(Error Err)
133 : Err(std::move(Err)), Flags(JITSymbolFlags::HasError) {}
120134
121135 /// @brief Create a symbol for a definition with a known address.
122136 JITSymbol(JITTargetAddress Addr, JITSymbolFlags Flags)
138152 JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags)
139153 : GetAddress(std::move(GetAddress)), Flags(Flags) {}
140154
155 JITSymbol(const JITSymbol&) = delete;
156 JITSymbol& operator=(const JITSymbol&) = delete;
157
158 JITSymbol(JITSymbol &&Other)
159 : GetAddress(std::move(Other.GetAddress)), Flags(std::move(Other.Flags)) {
160 if (Flags.hasError())
161 Err = std::move(Other.Err);
162 else
163 CachedAddr = std::move(Other.CachedAddr);
164 }
165
166 JITSymbol& operator=(JITSymbol &&Other) {
167 GetAddress = std::move(Other.GetAddress);
168 Flags = std::move(Other.Flags);
169 if (Flags.hasError())
170 Err = std::move(Other.Err);
171 else
172 CachedAddr = std::move(Other.CachedAddr);
173 return *this;
174 }
175
176 ~JITSymbol() {
177 if (Flags.hasError())
178 Err.~Error();
179 else
180 CachedAddr.~JITTargetAddress();
181 }
182
141183 /// @brief Returns true if the symbol exists, false otherwise.
142 explicit operator bool() const { return CachedAddr || GetAddress; }
184 explicit operator bool() const {
185 return !Flags.hasError() && (CachedAddr || GetAddress);
186 }
187
188 /// @brief Move the error field value out of this JITSymbol.
189 Error takeError() {
190 if (Flags.hasError())
191 return std::move(Err);
192 return Error::success();
193 }
143194
144195 /// @brief Get the address of the symbol in the target address space. Returns
145196 /// '0' if the symbol does not exist.
146 JITTargetAddress getAddress() {
197 Expected getAddress() {
198 assert(!Flags.hasError() && "getAddress called on error value");
147199 if (GetAddress) {
148 CachedAddr = GetAddress();
149 assert(CachedAddr && "Symbol could not be materialized.");
150 GetAddress = nullptr;
200 if (auto CachedAddrOrErr = GetAddress()) {
201 GetAddress = nullptr;
202 CachedAddr = *CachedAddrOrErr;
203 assert(CachedAddr && "Symbol could not be materialized.");
204 } else
205 return CachedAddrOrErr.takeError();
151206 }
152207 return CachedAddr;
153208 }
156211
157212 private:
158213 GetAddressFtor GetAddress;
159 JITTargetAddress CachedAddr = 0;
214 union {
215 JITTargetAddress CachedAddr;
216 Error Err;
217 };
160218 JITSymbolFlags Flags;
161219 };
162220
175175 for (auto BLH : BaseLayerHandles)
176176 if (auto Sym = BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly))
177177 return Sym;
178 else if (auto Err = Sym.takeError())
179 return std::move(Err);
178180 return nullptr;
179181 }
180182
181 void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
183 Error removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
182184 for (auto &BLH : BaseLayerHandles)
183 BaseLayer.removeModule(BLH);
185 if (auto Err = BaseLayer.removeModule(BLH))
186 return Err;
187 return Error::success();
184188 }
185189
186190 std::shared_ptr ExternalSymbolResolver;
215219 CloneStubsIntoPartitions(CloneStubsIntoPartitions) {}
216220
217221 ~CompileOnDemandLayer() {
222 // FIXME: Report error on log.
218223 while (!LogicalDylibs.empty())
219 removeModule(LogicalDylibs.begin());
224 consumeError(removeModule(LogicalDylibs.begin()));
220225 }
221226
222227 /// @brief Add a module to the compile-on-demand layer.
223 ModuleHandleT addModule(std::shared_ptr M,
224 std::shared_ptr Resolver) {
228 Expected
229 addModule(std::shared_ptr M,
230 std::shared_ptr Resolver) {
225231
226232 LogicalDylibs.push_back(LogicalDylib());
227233 auto &LD = LogicalDylibs.back();
229235 LD.StubsMgr = CreateIndirectStubsManager();
230236
231237 // Process each of the modules in this module set.
232 addLogicalModule(LD, std::move(M));
238 if (auto Err = addLogicalModule(LD, std::move(M)))
239 return std::move(Err);
233240
234241 return std::prev(LogicalDylibs.end());
235242 }
236243
237244 /// @brief Add extra modules to an existing logical module.
238 void addExtraModule(ModuleHandleT H, std::shared_ptr M) {
239 addLogicalModule(*H, std::move(M));
245 Error addExtraModule(ModuleHandleT H, std::shared_ptr M) {
246 return addLogicalModule(*H, std::move(M));
240247 }
241248
242249 /// @brief Remove the module represented by the given handle.
243250 ///
244251 /// This will remove all modules in the layers below that were derived from
245252 /// the module represented by H.
246 void removeModule(ModuleHandleT H) {
247 H->removeModulesFromBaseLayer(BaseLayer);
253 Error removeModule(ModuleHandleT H) {
254 auto Err = H->removeModulesFromBaseLayer(BaseLayer);
248255 LogicalDylibs.erase(H);
256 return Err;
249257 }
250258
251259 /// @brief Search for the given named symbol.
259267 return Sym;
260268 if (auto Sym = findSymbolIn(LDI, Name, ExportedSymbolsOnly))
261269 return Sym;
270 else if (auto Err = Sym.takeError())
271 return std::move(Err);
262272 }
263273 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
264274 }
297307
298308 private:
299309
300 void addLogicalModule(LogicalDylib &LD, std::shared_ptr SrcMPtr) {
310 Error addLogicalModule(LogicalDylib &LD, std::shared_ptr SrcMPtr) {
301311
302312 // Rename all static functions / globals to $static.X :
303313 // This will unique the names across all modules in the logical dylib,
323333
324334 // Skip weak functions for which we already have definitions.
325335 auto MangledName = mangle(F.getName(), DL);
326 if (F.hasWeakLinkage() || F.hasLinkOnceLinkage())
336 if (F.hasWeakLinkage() || F.hasLinkOnceLinkage()) {
327337 if (auto Sym = LD.findSymbol(BaseLayer, MangledName, false))
328338 continue;
339 else if (auto Err = Sym.takeError())
340 return std::move(Err);
341 }
329342
330343 // Record all functions defined by this module.
331344 if (CloneStubsIntoPartitions)
338351 StubInits[MangledName] =
339352 std::make_pair(CCInfo.getAddress(),
340353 JITSymbolFlags::fromGlobalValue(F));
341 CCInfo.setCompileAction([this, &LD, LMId, &F]() {
342 return this->extractAndCompile(LD, LMId, F);
343 });
354 CCInfo.setCompileAction([this, &LD, LMId, &F]() -> JITTargetAddress {
355 if (auto FnImplAddrOrErr = this->extractAndCompile(LD, LMId, F))
356 return *FnImplAddrOrErr;
357 else {
358 // FIXME: Report error, return to 'abort' or something similar.
359 consumeError(FnImplAddrOrErr.takeError());
360 return 0;
361 }
362 });
344363 }
345364
346365 auto EC = LD.StubsMgr->createStubs(StubInits);
355374 // empty globals module.
356375 if (SrcM.global_empty() && SrcM.alias_empty() &&
357376 !SrcM.getModuleFlagsMetadata())
358 return;
377 return Error::success();
359378
360379 // Create the GlobalValues module.
361380 auto GVsM = llvm::make_unique((SrcM.getName() + ".globals").str(),
381400
382401 // Initializers may refer to functions declared (but not defined) in this
383402 // module. Build a materializer to clone decls on demand.
403 Error MaterializerErrors = Error::success();
384404 auto Materializer = createLambdaMaterializer(
385 [&LD, &GVsM](Value *V) -> Value* {
405 [&LD, &GVsM, &MaterializerErrors](Value *V) -> Value* {
386406 if (auto *F = dyn_cast(V)) {
387407 // Decls in the original module just get cloned.
388408 if (F->isDeclaration())
393413 // instead.
394414 const DataLayout &DL = GVsM->getDataLayout();
395415 std::string FName = mangle(F->getName(), DL);
396 auto StubSym = LD.StubsMgr->findStub(FName, false);
397416 unsigned PtrBitWidth = DL.getPointerTypeSizeInBits(F->getType());
398 ConstantInt *StubAddr =
399 ConstantInt::get(GVsM->getContext(),
400 APInt(PtrBitWidth, StubSym.getAddress()));
417 JITTargetAddress StubAddr = 0;
418
419 // Get the address for the stub. If we encounter an error while
420 // doing so, stash it in the MaterializerErrors variable and use a
421 // null address as a placeholder.
422 if (auto StubSym = LD.StubsMgr->findStub(FName, false)) {
423 if (auto StubAddrOrErr = StubSym.getAddress())
424 StubAddr = *StubAddrOrErr;
425 else
426 MaterializerErrors = joinErrors(std::move(MaterializerErrors),
427 StubAddrOrErr.takeError());
428 }
429
430 ConstantInt *StubAddrCI =
431 ConstantInt::get(GVsM->getContext(), APInt(PtrBitWidth, StubAddr));
401432 Constant *Init = ConstantExpr::getCast(Instruction::IntToPtr,
402 StubAddr, F->getType());
433 StubAddrCI, F->getType());
403434 return GlobalAlias::create(F->getFunctionType(),
404435 F->getType()->getAddressSpace(),
405436 F->getLinkage(), F->getName(),
423454 NewA->setAliasee(cast(Init));
424455 }
425456
457 if (MaterializerErrors)
458 return MaterializerErrors;
459
426460 // Build a resolver for the globals module and add it to the base layer.
427461 auto GVsResolver = createLambdaResolver(
428 [this, &LD](const std::string &Name) {
462 [this, &LD](const std::string &Name) -> JITSymbol {
429463 if (auto Sym = LD.StubsMgr->findStub(Name, false))
430464 return Sym;
431465 if (auto Sym = LD.findSymbol(BaseLayer, Name, false))
432466 return Sym;
467 else if (auto Err = Sym.takeError())
468 return std::move(Err);
433469 return LD.ExternalSymbolResolver->findSymbolInLogicalDylib(Name);
434470 },
435471 [&LD](const std::string &Name) {
436472 return LD.ExternalSymbolResolver->findSymbol(Name);
437473 });
438474
439 auto GVsH = BaseLayer.addModule(std::move(GVsM), std::move(GVsResolver));
440 LD.BaseLayerHandles.push_back(GVsH);
475 if (auto GVsHOrErr =
476 BaseLayer.addModule(std::move(GVsM), std::move(GVsResolver)))
477 LD.BaseLayerHandles.push_back(*GVsHOrErr);
478 else
479 return GVsHOrErr.takeError();
480
481 return Error::success();
441482 }
442483
443484 static std::string mangle(StringRef Name, const DataLayout &DL) {
449490 return MangledName;
450491 }
451492
452 JITTargetAddress
493 Expected
453494 extractAndCompile(LogicalDylib &LD,
454495 typename LogicalDylib::SourceModuleHandle LMId,
455496 Function &F) {
462503 // Grab the name of the function being called here.
463504 std::string CalledFnName = mangle(F.getName(), SrcM.getDataLayout());
464505
506 JITTargetAddress CalledAddr = 0;
465507 auto Part = Partition(F);
466 auto PartH = emitPartition(LD, LMId, Part);
467
468 JITTargetAddress CalledAddr = 0;
469 for (auto *SubF : Part) {
470 std::string FnName = mangle(SubF->getName(), SrcM.getDataLayout());
471 auto FnBodySym = BaseLayer.findSymbolIn(PartH, FnName, false);
472 assert(FnBodySym && "Couldn't find function body.");
473
474 JITTargetAddress FnBodyAddr = FnBodySym.getAddress();
475
476 // If this is the function we're calling record the address so we can
477 // return it from this function.
478 if (SubF == &F)
479 CalledAddr = FnBodyAddr;
480
481 // Update the function body pointer for the stub.
482 if (auto EC = LD.StubsMgr->updatePointer(FnName, FnBodyAddr))
483 return 0;
484 }
485
486 LD.BaseLayerHandles.push_back(PartH);
508 if (auto PartHOrErr = emitPartition(LD, LMId, Part)) {
509 auto &PartH = *PartHOrErr;
510 for (auto *SubF : Part) {
511 std::string FnName = mangle(SubF->getName(), SrcM.getDataLayout());
512 if (auto FnBodySym = BaseLayer.findSymbolIn(PartH, FnName, false)) {
513 if (auto FnBodyAddrOrErr = FnBodySym.getAddress()) {
514 JITTargetAddress FnBodyAddr = *FnBodyAddrOrErr;
515
516 // If this is the function we're calling record the address so we can
517 // return it from this function.
518 if (SubF == &F)
519 CalledAddr = FnBodyAddr;
520
521 // Update the function body pointer for the stub.
522 if (auto EC = LD.StubsMgr->updatePointer(FnName, FnBodyAddr))
523 return 0;
524
525 } else
526 return FnBodyAddrOrErr.takeError();
527 } else if (auto Err = FnBodySym.takeError())
528 return std::move(Err);
529 else
530 llvm_unreachable("Function not emitted for partition");
531 }
532
533 LD.BaseLayerHandles.push_back(PartH);
534 } else
535 return PartHOrErr.takeError();
487536
488537 return CalledAddr;
489538 }
490539
491540 template
492 BaseLayerModuleHandleT
541 Expected
493542 emitPartition(LogicalDylib &LD,
494543 typename LogicalDylib::SourceModuleHandle LMId,
495544 const PartitionT &Part) {
553602
554603 // Create memory manager and symbol resolver.
555604 auto Resolver = createLambdaResolver(
556 [this, &LD](const std::string &Name) {
605 [this, &LD](const std::string &Name) -> JITSymbol {
557606 if (auto Sym = LD.findSymbol(BaseLayer, Name, false))
558607 return Sym;
608 else if (auto Err = Sym.takeError())
609 return std::move(Err);
559610 return LD.ExternalSymbolResolver->findSymbolInLogicalDylib(Name);
560611 },
561612 [&LD](const std::string &Name) {
1616 #include "llvm/ADT/StringMap.h"
1717 #include "llvm/ADT/iterator_range.h"
1818 #include "llvm/ExecutionEngine/JITSymbol.h"
19 #include "llvm/ExecutionEngine/RuntimeDyld.h"
20 #include "llvm/ExecutionEngine/Orc/OrcError.h"
1921 #include
2022 #include
2123 #include
98100
99101 /// @brief Run the recorded constructors/destructors through the given JIT
100102 /// layer.
101 bool runViaLayer(JITLayerT &JITLayer) const {
103 Error runViaLayer(JITLayerT &JITLayer) const {
102104 using CtorDtorTy = void (*)();
103105
104 bool Error = false;
105106 for (const auto &CtorDtorName : CtorDtorNames)
106107 if (auto CtorDtorSym = JITLayer.findSymbolIn(H, CtorDtorName, false)) {
107 CtorDtorTy CtorDtor =
108 reinterpret_cast(
109 static_cast(CtorDtorSym.getAddress()));
110 CtorDtor();
111 } else
112 Error = true;
113 return !Error;
108 if (auto AddrOrErr = CtorDtorSym.getAddress()) {
109 CtorDtorTy CtorDtor =
110 reinterpret_cast(static_cast(*AddrOrErr));
111 CtorDtor();
112 } else
113 return AddrOrErr.takeError();
114 } else {
115 if (auto Err = CtorDtorSym.takeError())
116 return Err;
117 else
118 return make_error(CtorDtorName);
119 }
120 return Error::success();
114121 }
115122
116123 private:
4949 /// along with the given memory manager and symbol resolver.
5050 ///
5151 /// @return A handle for the added module.
52 ModuleHandleT addModule(std::shared_ptr M,
53 std::shared_ptr Resolver) {
52 Expected
53 addModule(std::shared_ptr M,
54 std::shared_ptr Resolver) {
5455 using CompileResult = decltype(Compile(*M));
5556 auto Obj = std::make_shared(Compile(*M));
5657 return BaseLayer.addObject(std::move(Obj), std::move(Resolver));
5758 }
5859
5960 /// @brief Remove the module associated with the handle H.
60 void removeModule(ModuleHandleT H) { BaseLayer.removeObject(H); }
61 Error removeModule(ModuleHandleT H) {
62 return BaseLayer.removeObject(H);
63 }
6164
6265 /// @brief Search for the given named symbol.
6366 /// @param Name The name of the symbol to search for.
8386 /// @brief Immediately emit and finalize the module represented by the given
8487 /// handle.
8588 /// @param H Handle for module to emit/finalize.
86 void emitAndFinalize(ModuleHandleT H) {
87 BaseLayer.emitAndFinalize(H);
89 Error emitAndFinalize(ModuleHandleT H) {
90 return BaseLayer.emitAndFinalize(H);
8891 }
8992
9093 private:
4141 /// the layer below, along with the memory manager and symbol resolver.
4242 ///
4343 /// @return A handle for the added modules.
44 ModuleHandleT addModule(std::shared_ptr M,
45 std::shared_ptr Resolver) {
44 Expected
45 addModule(std::shared_ptr M,
46 std::shared_ptr Resolver) {
4647 return BaseLayer.addModule(Transform(std::move(M)), std::move(Resolver));
4748 }
4849
4950 /// @brief Remove the module associated with the handle H.
50 void removeModule(ModuleHandleT H) { BaseLayer.removeModule(H); }
51 Error removeModule(ModuleHandleT H) { return BaseLayer.removeModule(H); }
5152
5253 /// @brief Search for the given named symbol.
5354 /// @param Name The name of the symbol to search for.
7374 /// @brief Immediately emit and finalize the module represented by the given
7475 /// handle.
7576 /// @param H Handle for module to emit/finalize.
76 void emitAndFinalize(ModuleHandleT H) {
77 BaseLayer.emitAndFinalize(H);
77 Error emitAndFinalize(ModuleHandleT H) {
78 return BaseLayer.emitAndFinalize(H);
7879 }
7980
8081 /// @brief Access the transform functor directly.
5959 std::string PName = Name;
6060 JITSymbolFlags Flags = JITSymbolFlags::fromGlobalValue(*GV);
6161 auto GetAddress =
62 [this, ExportedSymbolsOnly, PName, &B]() -> JITTargetAddress {
62 [this, ExportedSymbolsOnly, PName, &B]() -> Expected {
6363 if (this->EmitState == Emitting)
6464 return 0;
6565 else if (this->EmitState == NotEmitted) {
6666 this->EmitState = Emitting;
67 Handle = this->emitToBaseLayer(B);
67 if (auto HandleOrErr = this->emitToBaseLayer(B))
68 Handle = std::move(*HandleOrErr);
69 else
70 return HandleOrErr.takeError();
6871 this->EmitState = Emitted;
6972 }
70 auto Sym = B.findSymbolIn(Handle, PName, ExportedSymbolsOnly);
71 return Sym.getAddress();
73 if (auto Sym = B.findSymbolIn(Handle, PName, ExportedSymbolsOnly))
74 return Sym.getAddress();
75 else if (auto Err = Sym.takeError())
76 return std::move(Err);
77 else
78 llvm_unreachable("Successful symbol lookup should return "
79 "definition address here");
7280 };
7381 return JITSymbol(std::move(GetAddress), Flags);
7482 } else
126134 return buildMangledSymbols(Name, ExportedSymbolsOnly);
127135 }
128136
129 BaseLayerHandleT emitToBaseLayer(BaseLayerT &BaseLayer) {
137 Expected emitToBaseLayer(BaseLayerT &BaseLayer) {
130138 // We don't need the mangled names set any more: Once we've emitted this
131139 // to the base layer we'll just look for symbols there.
132140 MangledSymbols.reset();
203211 LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
204212
205213 /// @brief Add the given module to the lazy emitting layer.
206 ModuleHandleT addModule(std::shared_ptr M,
207 std::shared_ptr Resolver) {
214 Expected
215 addModule(std::shared_ptr M,
216 std::shared_ptr Resolver) {
208217 return ModuleList.insert(
209218 ModuleList.end(),
210219 llvm::make_unique(std::move(M),
215224 ///
216225 /// This method will free the memory associated with the given module, both
217226 /// in this layer, and the base layer.
218 void removeModule(ModuleHandleT H) {
227 Error removeModule(ModuleHandleT H) {
219228 (*H)->removeModuleFromBaseLayer(BaseLayer);
220229 ModuleList.erase(H);
221230 }
250259 /// @brief Immediately emit and finalize the module represented by the given
251260 /// handle.
252261 /// @param H Handle for module to emit/finalize.
253 void emitAndFinalize(ModuleHandleT H) {
254 (*H)->emitAndFinalize(BaseLayer);
262 Error emitAndFinalize(ModuleHandleT H) {
263 return (*H)->emitAndFinalize(BaseLayer);
255264 }
256265 };
257266
4343 ///
4444 /// @return A handle for the added objects.
4545 template
46 ObjHandleT addObject(ObjectPtr Obj,
47 std::shared_ptr Resolver) {
46 Expected addObject(ObjectPtr Obj,
47 std::shared_ptr Resolver) {
4848 return BaseLayer.addObject(Transform(std::move(Obj)), std::move(Resolver));
4949 }
5050
5151 /// @brief Remove the object set associated with the handle H.
52 void removeObject(ObjHandleT H) { BaseLayer.removeObject(H); }
52 Error removeObject(ObjHandleT H) { return BaseLayer.removeObject(H); }
5353
5454 /// @brief Search for the given named symbol.
5555 /// @param Name The name of the symbol to search for.
7575 /// @brief Immediately emit and finalize the object set represented by the
7676 /// given handle.
7777 /// @param H Handle for object set to emit/finalize.
78 void emitAndFinalize(ObjHandleT H) { BaseLayer.emitAndFinalize(H); }
78 Error emitAndFinalize(ObjHandleT H) {
79 return BaseLayer.emitAndFinalize(H);
80 }
7981
8082 /// @brief Map section addresses for the objects associated with the handle H.
8183 void mapSectionAddress(ObjHandleT H, const void *LocalAddress,
2121
2222 enum class OrcErrorCode : int {
2323 // RPC Errors
24 RemoteAllocatorDoesNotExist = 1,
24 JITSymbolNotFound = 1,
25 RemoteAllocatorDoesNotExist,
2526 RemoteAllocatorIdAlreadyInUse,
2627 RemoteMProtectAddrUnrecognized,
2728 RemoteIndirectStubsOwnerDoesNotExist,
3637
3738 std::error_code orcError(OrcErrorCode ErrCode);
3839
40 class JITSymbolNotFound : public ErrorInfo {
41 public:
42 static char ID;
43
44 JITSymbolNotFound(std::string SymbolName);
45 std::error_code convertToErrorCode() const override;
46 void log(raw_ostream &OS) const override;
47 const std::string &getSymbolName() const;
48 private:
49 std::string SymbolName;
50 };
51
3952 } // End namespace orc.
4053 } // End namespace llvm.
4154
257257 ///
258258 /// @return A handle that can be used to refer to the loaded objects (for
259259 /// symbol searching, finalization, freeing memory, etc.).
260 ObjHandleT addObject(ObjectPtr Obj,
261 std::shared_ptr Resolver) {
260 Expected addObject(ObjectPtr Obj,
261 std::shared_ptr Resolver) {
262262 auto Finalizer = [&](ObjHandleT H, RuntimeDyld &RTDyld,
263263 const ObjectPtr &ObjToLoad,
264264 std::function LOSHandleLoad) {
298298 /// indirectly) will result in undefined behavior. If dependence tracking is
299299 /// required to detect or resolve such issues it should be added at a higher
300300 /// layer.
301 void removeObject(ObjHandleT H) {
301 Error removeObject(ObjHandleT H) {
302302 // How do we invalidate the symbols in H?
303303 LinkedObjList.erase(H);
304 return Error::success();
304305 }
305306
306307 /// @brief Search for the given named symbol.
337338 /// @brief Immediately emit and finalize the object set represented by the
338339 /// given handle.
339340 /// @param H Handle for object set to emit/finalize.
340 void emitAndFinalize(ObjHandleT H) {
341 Error emitAndFinalize(ObjHandleT H) {
341342 (*H)->finalize();
343 return Error::success();
342344 }
343345
344346 private:
112112 /**
113113 * Create a lazy compile callback.
114114 */
115 LLVMOrcTargetAddress
115 LLVMOrcErrorCode
116116 LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,
117 LLVMOrcTargetAddress *RetAddr,
117118 LLVMOrcLazyCompileCallbackFn Callback,
118119 void *CallbackCtx);
119120
134135 /**
135136 * Add module to be eagerly compiled.
136137 */
137 LLVMOrcModuleHandle
138 LLVMOrcErrorCode
138139 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
140 LLVMOrcModuleHandle *RetHandle,
139141 LLVMSharedModuleRef Mod,
140142 LLVMOrcSymbolResolverFn SymbolResolver,
141143 void *SymbolResolverCtx);
143145 /**
144146 * Add module to be lazily compiled one function at a time.
145147 */
146 LLVMOrcModuleHandle
148 LLVMOrcErrorCode
147149 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
150 LLVMOrcModuleHandle *RetHandle,
148151 LLVMSharedModuleRef Mod,
149152 LLVMOrcSymbolResolverFn SymbolResolver,
150153 void *SymbolResolverCtx);
152155 /**
153156 * Add an object file.
154157 */
155 LLVMOrcModuleHandle LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack,
156 LLVMSharedObjectBufferRef Obj,
157 LLVMOrcSymbolResolverFn SymbolResolver,
158 void *SymbolResolverCtx);
158 LLVMOrcErrorCode LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack,
159 LLVMOrcModuleHandle *RetHandle,
160 LLVMSharedObjectBufferRef Obj,
161 LLVMOrcSymbolResolverFn SymbolResolver,
162 void *SymbolResolverCtx);
159163
160164 /**
161165 * Remove a module set from the JIT.
163167 * This works for all modules that can be added via OrcAdd*, including object
164168 * files.
165169 */
166 void LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle H);
170 LLVMOrcErrorCode LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack,
171 LLVMOrcModuleHandle H);
167172
168173 /**
169174 * Get symbol address from JIT instance.
170175 */
171 LLVMOrcTargetAddress LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,
172 const char *SymbolName);
176 LLVMOrcErrorCode LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,
177 LLVMOrcTargetAddress *RetAddr,
178 const char *SymbolName);
173179
174180 /**
175181 * Dispose of an ORC JIT stack.
176182 */
177 void LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack);
183 LLVMOrcErrorCode LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack);
178184
179185 #ifdef __cplusplus
180186 }
316316 raw_string_ostream MangledNameStream(MangledName);
317317 Mangler::getNameWithPrefix(MangledNameStream, Name, getDataLayout());
318318 }
319 return findSymbol(MangledName, CheckFunctionsOnly).getAddress();
319 if (auto Sym = findSymbol(MangledName, CheckFunctionsOnly)) {
320 if (auto AddrOrErr = Sym.getAddress())
321 return *AddrOrErr;
322 else
323 report_fatal_error(AddrOrErr.takeError());
324 } else
325 report_fatal_error(Sym.takeError());
320326 }
321327
322328 JITSymbol MCJIT::findSymbol(const std::string &Name,
598604
599605 void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
600606 if (!isSymbolSearchingDisabled()) {
601 void *ptr =
602 reinterpret_cast(
603 static_cast(Resolver.findSymbol(Name).getAddress()));
604 if (ptr)
605 return ptr;
607 if (auto Sym = Resolver.findSymbol(Name)) {
608 if (auto AddrOrErr = Sym.getAddress())
609 return reinterpret_cast(
610 static_cast(*AddrOrErr));
611 } else if (auto Err = Sym.takeError())
612 report_fatal_error(std::move(Err));
606613 }
607614
608615 /// If a LazyFunctionCreator is installed, use it to get/create the function.
5959
6060 void LLVMOrcDisposeMangledSymbol(char *MangledName) { delete[] MangledName; }
6161
62 LLVMOrcTargetAddress
62 LLVMOrcErrorCode
6363 LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,
64 LLVMOrcTargetAddress *RetAddr,
6465 LLVMOrcLazyCompileCallbackFn Callback,
6566 void *CallbackCtx) {
6667 OrcCBindingsStack &J = *unwrap(JITStack);
67 return J.createLazyCompileCallback(Callback, CallbackCtx);
68 return J.createLazyCompileCallback(*RetAddr, Callback, CallbackCtx);
6869 }
6970
7071 LLVMOrcErrorCode LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,
8182 return J.setIndirectStubPointer(StubName, NewAddr);
8283 }
8384
84 LLVMOrcModuleHandle
85 LLVMOrcErrorCode
8586 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
87 LLVMOrcModuleHandle *RetHandle,
8688 LLVMSharedModuleRef Mod,
8789 LLVMOrcSymbolResolverFn SymbolResolver,
8890 void *SymbolResolverCtx) {
8991 OrcCBindingsStack &J = *unwrap(JITStack);
9092 std::shared_ptr *M(unwrap(Mod));
91 return J.addIRModuleEager(*M, SymbolResolver, SymbolResolverCtx);
93 return J.addIRModuleEager(*RetHandle, *M, SymbolResolver, SymbolResolverCtx);
9294 }
9395
94 LLVMOrcModuleHandle
96 LLVMOrcErrorCode
9597 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
98 LLVMOrcModuleHandle *RetHandle,
9699 LLVMSharedModuleRef Mod,
97100 LLVMOrcSymbolResolverFn SymbolResolver,
98101 void *SymbolResolverCtx) {
99102 OrcCBindingsStack &J = *unwrap(JITStack);
100103 std::shared_ptr *M(unwrap(Mod));
101 return J.addIRModuleLazy(*M, SymbolResolver, SymbolResolverCtx);
104 return J.addIRModuleLazy(*RetHandle, *M, SymbolResolver, SymbolResolverCtx);
102105 }
103106
104 void LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle H) {
107 LLVMOrcErrorCode LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack,
108 LLVMOrcModuleHandle H) {
105109 OrcCBindingsStack &J = *unwrap(JITStack);
106 J.removeModule(H);
110 return J.removeModule(H);
107111 }
108112
109 LLVMOrcTargetAddress LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,
110 const char *SymbolName) {
113 LLVMOrcErrorCode LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,
114 LLVMOrcTargetAddress *RetAddr,
115 const char *SymbolName) {
111116 OrcCBindingsStack &J = *unwrap(JITStack);
112 auto Sym = J.findSymbol(SymbolName, true);
113 return Sym.getAddress();
117 return J.findSymbolAddress(*RetAddr, SymbolName, true);
114118 }
115119
116 void LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) {
117 delete unwrap(JITStack);
120 LLVMOrcErrorCode LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) {
121 auto *J = unwrap(JITStack);
122 auto Err = J->shutdown();
123 delete J;
124 return Err;
118125 }
6969
7070 virtual JITSymbol findSymbolIn(const std::string &Name,
7171 bool ExportedSymbolsOnly) = 0;
72 virtual void removeModule() = 0;
72 virtual Error removeModule() = 0;
7373 };
7474
7575 template class GenericHandleImpl : public GenericHandle {
8282 return Layer.findSymbolIn(Handle, Name, ExportedSymbolsOnly);
8383 }
8484
85 void removeModule() override { return Layer.removeModule(Handle); }
85 Error removeModule() override { return Layer.removeModule(Handle); }
8686
8787 private:
8888 LayerT &Layer;
115115 CXXRuntimeOverrides(
116116 [this](const std::string &S) { return mangle(S); }) {}
117117
118 ~OrcCBindingsStack() {
118 LLVMOrcErrorCode shutdown() {
119119 // Run any destructors registered with __cxa_atexit.
120120 CXXRuntimeOverrides.runDestructors();
121121 // Run any IR destructors.
122122 for (auto &DtorRunner : IRStaticDestructorRunners)
123 DtorRunner.runViaLayer(*this);
123 if (auto Err = DtorRunner.runViaLayer(*this))
124 return mapError(std::move(Err));
125 return LLVMOrcErrSuccess;
124126 }
125127
126128 std::string mangle(StringRef Name) {
137139 return reinterpret_cast(static_cast(Addr));
138140 }
139141
140 JITTargetAddress
141 createLazyCompileCallback(LLVMOrcLazyCompileCallbackFn Callback,
142
143 LLVMOrcErrorCode
144 createLazyCompileCallback(JITTargetAddress &RetAddr,
145 LLVMOrcLazyCompileCallbackFn Callback,
142146 void *CallbackCtx) {
143147 auto CCInfo = CCMgr->getCompileCallback();
144148 CCInfo.setCompileAction([=]() -> JITTargetAddress {
145149 return Callback(wrap(this), CallbackCtx);
146150 });
147 return CCInfo.getAddress();
151 RetAddr = CCInfo.getAddress();
152 return LLVMOrcErrSuccess;
148153 }
149154
150155 LLVMOrcErrorCode createIndirectStub(StringRef StubName,
163168 void *ExternalResolverCtx) {
164169 return orc::createLambdaResolver(
165170 [this, ExternalResolver, ExternalResolverCtx](const std::string &Name)
166 -> JITSymbol {
171 -> JITSymbol {
167172 // Search order:
168173 // 1. JIT'd symbols.
169174 // 2. Runtime overrides.
171176
172177 if (auto Sym = CODLayer.findSymbol(Name, true))
173178 return Sym;
179 else if (auto Err = Sym.takeError())
180 return Sym.takeError();
181
174182 if (auto Sym = CXXRuntimeOverrides.searchOverrides(Name))
175183 return Sym;
176184
181189
182190 return JITSymbol(nullptr);
183191 },
184 [](const std::string &Name) {
192 [](const std::string &Name) -> JITSymbol {
185193 return JITSymbol(nullptr);
186194 });
187195 }
188196
189197 template
190 ModuleHandleT addIRModule(LayerT &Layer, std::shared_ptr M,
191 std::unique_ptr MemMgr,
192 LLVMOrcSymbolResolverFn ExternalResolver,
193 void *ExternalResolverCtx) {
198 LLVMOrcErrorCode
199 addIRModule(ModuleHandleT &RetHandle, LayerT &Layer,
200 std::shared_ptr M,
201 std::unique_ptr MemMgr,
202 LLVMOrcSymbolResolverFn ExternalResolver,
203 void *ExternalResolverCtx) {
204
194205 // Attach a data-layout if one isn't already present.
195206 if (M->getDataLayout().isDefault())
196207 M->setDataLayout(DL);
207218 auto Resolver = createResolver(ExternalResolver, ExternalResolverCtx);
208219
209220 // Add the module to the JIT.
210 auto LH = Layer.addModule(std::move(M), std::move(Resolver));
211 ModuleHandleT H = createHandle(Layer, LH);
221 ModuleHandleT H;
222 if (auto LHOrErr = Layer.addModule(std::move(M), std::move(Resolver)))
223 H = createHandle(Layer, *LHOrErr);
224 else
225 return mapError(LHOrErr.takeError());
212226
213227 // Run the static constructors, and save the static destructor runner for
214228 // execution when the JIT is torn down.
215229 orc::CtorDtorRunner CtorRunner(std::move(CtorNames), H);
216 CtorRunner.runViaLayer(*this);
230 if (auto Err = CtorRunner.runViaLayer(*this))
231 return mapError(std::move(Err));
217232
218233 IRStaticDestructorRunners.emplace_back(std::move(DtorNames), H);
219234
220 return H;
221 }
222
223 ModuleHandleT addIRModuleEager(std::shared_ptr M,
224 LLVMOrcSymbolResolverFn ExternalResolver,
225 void *ExternalResolverCtx) {
226 return addIRModule(CompileLayer, std::move(M),
235 RetHandle = H;
236 return LLVMOrcErrSuccess;
237 }
238
239 LLVMOrcErrorCode addIRModuleEager(ModuleHandleT &RetHandle,
240 std::shared_ptr M,
241 LLVMOrcSymbolResolverFn ExternalResolver,
242 void *ExternalResolverCtx) {
243 return addIRModule(RetHandle, CompileLayer, std::move(M),
227244 llvm::make_unique(),
228245 std::move(ExternalResolver), ExternalResolverCtx);
229246 }
230247
231 ModuleHandleT addIRModuleLazy(std::shared_ptr M,
232 LLVMOrcSymbolResolverFn ExternalResolver,
233 void *ExternalResolverCtx) {
234 return addIRModule(CODLayer, std::move(M),
248 LLVMOrcErrorCode addIRModuleLazy(ModuleHandleT &RetHandle,
249 std::shared_ptr M,
250 LLVMOrcSymbolResolverFn ExternalResolver,
251 void *ExternalResolverCtx) {
252 return addIRModule(RetHandle, CODLayer, std::move(M),
235253 llvm::make_unique(),
236254 std::move(ExternalResolver), ExternalResolverCtx);
237255 }
238256
239 void removeModule(ModuleHandleT H) {
240 GenericHandles[H]->removeModule();
257 LLVMOrcErrorCode removeModule(ModuleHandleT H) {
258 if (auto Err = GenericHandles[H]->removeModule())
259 return mapError(std::move(Err));
241260 GenericHandles[H] = nullptr;
242261 FreeHandleIndexes.push_back(H);
243 }
244
245 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
262 return LLVMOrcErrSuccess;
263 }
264
265 JITSymbol findSymbol(const std::string &Name,
266 bool ExportedSymbolsOnly) {
246267 if (auto Sym = IndirectStubsMgr->findStub(Name, ExportedSymbolsOnly))
247268 return Sym;
248269 return CODLayer.findSymbol(mangle(Name), ExportedSymbolsOnly);
251272 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
252273 bool ExportedSymbolsOnly) {
253274 return GenericHandles[H]->findSymbolIn(Name, ExportedSymbolsOnly);
275 }
276
277 LLVMOrcErrorCode findSymbolAddress(JITTargetAddress &RetAddr,
278 const std::string &Name,
279 bool ExportedSymbolsOnly) {
280 RetAddr = 0;
281 if (auto Sym = findSymbol(Name, ExportedSymbolsOnly)) {
282 // Successful lookup, non-null symbol:
283 if (auto AddrOrErr = Sym.getAddress()) {
284 RetAddr = *AddrOrErr;
285 return LLVMOrcErrSuccess;
286 } else
287 return mapError(AddrOrErr.takeError());
288 } else if (auto Err = Sym.takeError()) {
289 // Lookup failure - report error.
290 return mapError(std::move(Err));
291 }
292 // Otherwise we had a successful lookup but got a null result. We already
293 // set RetAddr to '0' above, so just return success.
294 return LLVMOrcErrSuccess;
254295 }
255296
256297 const std::string &getErrorMessage() const { return ErrMsg; }
4444 return "Could not negotiate RPC function";
4545 case OrcErrorCode::RPCResponseAbandoned:
4646 return "RPC response abandoned";
47 case OrcErrorCode::JITSymbolNotFound:
48 return "JIT symbol not found";
4749 case OrcErrorCode::UnexpectedRPCCall:
4850 return "Unexpected RPC call";
4951 case OrcErrorCode::UnexpectedRPCResponse:
6264 namespace llvm {
6365 namespace orc {
6466
67 char JITSymbolNotFound::ID = 0;
68
6569 std::error_code orcError(OrcErrorCode ErrCode) {
6670 typedef std::underlying_type::type UT;
6771 return std::error_code(static_cast(ErrCode), *OrcErrCat);
6872 }
6973
74 JITSymbolNotFound::JITSymbolNotFound(std::string SymbolName)
75 : SymbolName(std::move(SymbolName)) {}
76
77 std::error_code JITSymbolNotFound::convertToErrorCode() const {
78 typedef std::underlying_type::type UT;
79 return std::error_code(static_cast(OrcErrorCode::JITSymbolNotFound),
80 *OrcErrCat);
81 }
82
83 void JITSymbolNotFound::log(raw_ostream &OS) const {
84 OS << "Could not find symbol '" << SymbolName << "'";
85 }
86
87 const std::string &JITSymbolNotFound::getSymbolName() const {
88 return SymbolName;
89 }
90
7091 }
7192 }
201201 delete Mod;
202202 };
203203 LocalModules.push_back(std::shared_ptr(MPtr, std::move(Deleter)));
204 LazyEmitLayer.addModule(LocalModules.back(), Resolver);
204 cantFail(LazyEmitLayer.addModule(LocalModules.back(), Resolver));
205205 }
206206
207207 void addObjectFile(std::unique_ptr O) override {
208208 auto Obj =
209209 std::make_shared>(std::move(O),
210210 nullptr);
211 ObjectLayer.addObject(std::move(Obj), Resolver);
211 cantFail(ObjectLayer.addObject(std::move(Obj), Resolver));
212212 }
213213
214214 void addObjectFile(object::OwningBinary O) override {
215215 auto Obj =
216216 std::make_shared>(std::move(O));
217 ObjectLayer.addObject(std::move(Obj), Resolver);
217 cantFail(ObjectLayer.addObject(std::move(Obj), Resolver));
218218 }
219219
220220 void addArchive(object::OwningBinary A) override {
233233 }
234234
235235 uint64_t getSymbolAddress(StringRef Name) {
236 return findSymbol(Name).getAddress();
236 return cantFail(findSymbol(Name).getAddress());
237237 }
238238
239239 JITSymbol findSymbol(StringRef Name) {
322322 auto Obj =
323323 std::make_shared>(
324324 std::move(ChildObj), nullptr);
325 ObjectLayer.addObject(std::move(Obj), Resolver);
325 cantFail(ObjectLayer.addObject(std::move(Obj), Resolver));
326326 if (auto Sym = ObjectLayer.findSymbol(Name, true))
327327 return Sym;
328328 }
127127 );
128128
129129 // First, resolve relocations associated with external symbols.
130 resolveExternalSymbols();
130 if (auto Err = resolveExternalSymbols()) {
131 HasError = true;
132 ErrorStr = toString(std::move(Err));
133 }
131134
132135 // Iterate over all outstanding relocations
133136 for (auto it = Relocations.begin(), e = Relocations.end(); it != e; ++it) {
242245 continue;
243246 // Then check the symbol resolver to see if there's a definition
244247 // elsewhere in this logical dylib.
245 if (auto Sym = Resolver.findSymbolInLogicalDylib(Name))
248 if (auto Sym = Resolver.findSymbolInLogicalDylib(Name)) {
246249 if (Sym.getFlags().isStrongDefinition())
247250 continue;
251 } else if (auto Err = Sym.takeError())
252 return std::move(Err);
248253 // else
249254 JITSymFlags &= ~JITSymbolFlags::Weak;
250255 }
952957 }
953958 }
954959
955 void RuntimeDyldImpl::resolveExternalSymbols() {
960 Error RuntimeDyldImpl::resolveExternalSymbols() {
956961 while (!ExternalSymbolRelocations.empty()) {
957962 StringMap::iterator i = ExternalSymbolRelocations.begin();
958963
970975 // This is an external symbol, try to get its address from the symbol
971976 // resolver.
972977 // First search for the symbol in this logical dylib.
973 Addr = Resolver.findSymbolInLogicalDylib(Name.data()).getAddress();
978 if (auto Sym = Resolver.findSymbolInLogicalDylib(Name.data())) {
979 if (auto AddrOrErr = Sym.getAddress())
980 Addr = *AddrOrErr;
981 else
982 return AddrOrErr.takeError();
983 } else if (auto Err = Sym.takeError())
984 return Err;
985
974986 // If that fails, try searching for an external symbol.
975 if (!Addr)
976 Addr = Resolver.findSymbol(Name.data()).getAddress();
987 if (!Addr) {
988 if (auto Sym = Resolver.findSymbol(Name.data())) {
989 if (auto AddrOrErr = Sym.getAddress())
990 Addr = *AddrOrErr;
991 else
992 return AddrOrErr.takeError();
993 } else if (auto Err = Sym.takeError())
994 return Err;
995 }
977996 // The call to getSymbolAddress may have caused additional modules to
978997 // be loaded, which may have added new entries to the
979998 // ExternalSymbolRelocations map. Consquently, we need to update our
10081027
10091028 ExternalSymbolRelocations.erase(i);
10101029 }
1030
1031 return Error::success();
10111032 }
10121033
10131034 //===----------------------------------------------------------------------===//
741741 uint64_t RuntimeDyldCheckerImpl::getSymbolRemoteAddr(StringRef Symbol) const {
742742 if (auto InternalSymbol = getRTDyld().getSymbol(Symbol))
743743 return InternalSymbol.getAddress();
744 return getRTDyld().Resolver.findSymbol(Symbol).getAddress();
744 return cantFail(getRTDyld().Resolver.findSymbol(Symbol).getAddress());
745745 }
746746
747747 uint64_t RuntimeDyldCheckerImpl::readMemoryAtAddr(uint64_t SrcAddr,
416416 StubMap &Stubs) = 0;
417417
418418 /// \brief Resolve relocations to external symbols.
419 void resolveExternalSymbols();
419 Error resolveExternalSymbols();
420420
421421 // \brief Compute an upper bound of the memory that is required to load all
422422 // sections
147147
148148 // Add the module, look up main and run it.
149149 for (auto &M : Ms)
150 J.addModule(std::shared_ptr(std::move(M)));
151 auto MainSym = J.findSymbol("main");
150 cantFail(J.addModule(std::shared_ptr(std::move(M))));
152151
153 if (!MainSym) {
152 if (auto MainSym = J.findSymbol("main")) {
153 typedef int (*MainFnPtr)(int, const char*[]);
154 std::vector ArgV;
155 for (auto &Arg : Args)
156 ArgV.push_back(Arg.c_str());
157 auto Main = fromTargetAddress(cantFail(MainSym.getAddress()));
158 return Main(ArgV.size(), (const char**)ArgV.data());
159 } else if (auto Err = MainSym.takeError())
160 logAllUnhandledErrors(std::move(Err), llvm::errs(), "");
161 else
154162 errs() << "Could not find main function.\n";
155 return 1;
156 }
157163
158 using MainFnPtr = int (*)(int, const char*[]);
159 std::vector ArgV;
160 for (auto &Arg : Args)
161 ArgV.push_back(Arg.c_str());
162 auto Main = fromTargetAddress(MainSym.getAddress());
163 return Main(ArgV.size(), (const char**)ArgV.data());
164 return 1;
164165 }
7474 CXXRuntimeOverrides.runDestructors();
7575 // Run any IR destructors.
7676 for (auto &DtorRunner : IRStaticDestructorRunners)
77 DtorRunner.runViaLayer(CODLayer);
78 }
79
80 void addModule(std::shared_ptr M) {
77 if (auto Err = DtorRunner.runViaLayer(CODLayer)) {
78 // FIXME: OrcLazyJIT should probably take a "shutdownError" callback to
79 // report these errors on.
80 report_fatal_error(std::move(Err));
81 }
82 }
83
84 Error addModule(std::shared_ptr M) {
8185 if (M->getDataLayout().isDefault())
8286 M->setDataLayout(DL);
8387
124128 );
125129
126130 // Add the module to the JIT.
127 ModulesHandle =
128 CODLayer.addModule(std::move(M), std::move(Resolver));
131 if (auto ModulesHandleOrErr =
132 CODLayer.addModule(std::move(M), std::move(Resolver)))
133 ModulesHandle = std::move(*ModulesHandleOrErr);
134 else
135 return ModulesHandleOrErr.takeError();
136
129137 } else
130 CODLayer.addExtraModule(ModulesHandle, std::move(M));
138 if (auto Err = CODLayer.addExtraModule(ModulesHandle, std::move(M)))
139 return Err;
131140
132141 // Run the static constructors, and save the static destructor runner for
133142 // execution when the JIT is torn down.
134143 orc::CtorDtorRunner CtorRunner(std::move(CtorNames),
135144 ModulesHandle);
136 CtorRunner.runViaLayer(CODLayer);
145 if (auto Err = CtorRunner.runViaLayer(CODLayer))
146 return Err;
137147
138148 IRStaticDestructorRunners.emplace_back(std::move(DtorNames),
139149 ModulesHandle);
150
151 return Error::success();
140152 }
141153
142154 JITSymbol findSymbol(const std::string &Name) {
4949
5050 TEST(CompileOnDemandLayerTest, FindSymbol) {
5151 auto MockBaseLayer = createMockBaseLayer(
52 DoNothingAndReturn(0), DoNothingAndReturn(),
52 DoNothingAndReturn(0),
53 [](int Handle) { return Error::success(); },
5354 [](const std::string &Name, bool) {
5455 if (Name == "foo")
5556 return JITSymbol(1, JITSymbolFlags::Exported);
5657 return JITSymbol(nullptr);
5758 },
58 DoNothingAndReturn(nullptr));
59 ReturnNullJITSymbol());
5960
6061 typedef decltype(MockBaseLayer) MockBaseLayerT;
6162 DummyCallbackManager CallbackMgr;
3636
3737 // Test fall-through for symbol in base layer.
3838 auto BarSym = L.findSymbol("bar", true);
39 EXPECT_EQ(BarSym.getAddress(), static_cast(0x4567))
39 EXPECT_EQ(cantFail(BarSym.getAddress()),
40 static_cast(0x4567))
4041 << "Symbol lookup fall-through failed.";
4142
4243 // Test setup of a global mapping.
4344 L.setGlobalMapping("foo", 0x0123);
4445 auto FooSym2 = L.findSymbol("foo", true);
45 EXPECT_EQ(FooSym2.getAddress(), static_cast(0x0123))
46 EXPECT_EQ(cantFail(FooSym2.getAddress()),
47 static_cast(0x0123))
4648 << "Symbol mapping setup failed.";
4749
4850 // Test removal of a global mapping.
2626 TEST(LazyEmittingLayerTest, Empty) {
2727 MockBaseLayer M;
2828 llvm::orc::LazyEmittingLayer L(M);
29 L.addModule(std::unique_ptr(), nullptr);
29 cantFail(L.addModule(std::unique_ptr(), nullptr));
3030 }
3131
3232 }
4949 MockBaseLayer() : MockSymbol(nullptr) { resetExpectations(); }
5050
5151 template
52 ObjHandleT addObject(ObjPtrT Obj,
53 std::shared_ptr Resolver) {
52 llvm::Expected
53 addObject(ObjPtrT Obj,
54 std::shared_ptr Resolver) {
5455 EXPECT_EQ(MockResolver, Resolver) << "Resolver should pass through";
5556 EXPECT_EQ(MockObject + 1, *Obj) << "Transform should be applied";
5657 LastCalled = "addObject";
7273 resetExpectations();
7374 }
7475
75 void removeObject(ObjHandleT H) {
76 llvm::Error removeObject(ObjHandleT H) {
7677 EXPECT_EQ(MockObjHandle, H);
7778 LastCalled = "removeObject";
78 }
79 return llvm::Error::success();
80 }
81
7982 void expectRemoveObject(ObjHandleT H) { MockObjHandle = H; }
8083 void verifyRemoveObject() {
8184 EXPECT_EQ("removeObject", LastCalled);
8891 EXPECT_EQ(MockBool, ExportedSymbolsOnly) << "Flag should pass through";
8992 LastCalled = "findSymbol";
9093 MockSymbol = llvm::JITSymbol(122, llvm::JITSymbolFlags::None);
91 return MockSymbol;
94 return llvm::JITSymbol(122, llvm::JITSymbolFlags::None);
9295 }
9396 void expectFindSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
9497 MockName = Name;
9699 }
97100 void verifyFindSymbol(llvm::JITSymbol Returned) {
98101 EXPECT_EQ("findSymbol", LastCalled);
99 EXPECT_EQ(MockSymbol.getAddress(), Returned.getAddress())
102 EXPECT_EQ(cantFail(MockSymbol.getAddress()),
103 cantFail(Returned.getAddress()))
100104 << "Return should pass through";
101105 resetExpectations();
102106 }
108112 EXPECT_EQ(MockBool, ExportedSymbolsOnly) << "Flag should pass through";
109113 LastCalled = "findSymbolIn";
110114 MockSymbol = llvm::JITSymbol(122, llvm::JITSymbolFlags::None);
111 return MockSymbol;
115 return llvm::JITSymbol(122, llvm::JITSymbolFlags::None);
112116 }
113117 void expectFindSymbolIn(ObjHandleT H, const std::string &Name,
114118 bool ExportedSymbolsOnly) {
118122 }
119123 void verifyFindSymbolIn(llvm::JITSymbol Returned) {
120124 EXPECT_EQ("findSymbolIn", LastCalled);
121 EXPECT_EQ(MockSymbol.getAddress(), Returned.getAddress())
125 EXPECT_EQ(cantFail(MockSymbol.getAddress()),
126 cantFail(Returned.getAddress()))
122127 << "Return should pass through";
123128 resetExpectations();
124129 }
125130
126 void emitAndFinalize(ObjHandleT H) {
131 llvm::Error emitAndFinalize(ObjHandleT H) {
127132 EXPECT_EQ(MockObjHandle, H) << "Handle should pass through";
128133 LastCalled = "emitAndFinalize";
129 }
134 return llvm::Error::success();
135 }
136
130137 void expectEmitAndFinalize(ObjHandleT H) { MockObjHandle = H; }
138
131139 void verifyEmitAndFinalize() {
132140 EXPECT_EQ("emitAndFinalize", LastCalled);
133141 resetExpectations();
200208 auto Obj1 = std::make_shared(211);
201209 auto SR = std::make_shared();
202210 M.expectAddObject(Obj1, SR);
203 auto H = T1.addObject(std::move(Obj1), SR);
211 auto H = cantFail(T1.addObject(std::move(Obj1), SR));
204212 M.verifyAddObject(H);
205213
206214 // Test addObjectSet with T2 (mutating)
207215 auto Obj2 = std::make_shared(222);
208216 M.expectAddObject(Obj2, SR);
209 H = T2.addObject(Obj2, SR);
217 H = cantFail(T2.addObject(Obj2, SR));
210218 M.verifyAddObject(H);
211219 EXPECT_EQ(223, *Obj2) << "Expected mutation";
212220
213221 // Test removeObjectSet
214222 M.expectRemoveObject(H);
215 T1.removeObject(H);
223 cantFail(T1.removeObject(H));
216224 M.verifyRemoveObject();
217225
218226 // Test findSymbol
219227 std::string Name = "foo";
220228 bool ExportedOnly = true;
221229 M.expectFindSymbol(Name, ExportedOnly);
222 llvm::JITSymbol Symbol = T2.findSymbol(Name, ExportedOnly);
223 M.verifyFindSymbol(Symbol);
230 llvm::JITSymbol Sym1 = T2.findSymbol(Name, ExportedOnly);
231 M.verifyFindSymbol(std::move(Sym1));
224232
225233 // Test findSymbolIn
226234 Name = "bar";
227235 ExportedOnly = false;
228236 M.expectFindSymbolIn(H, Name, ExportedOnly);
229 Symbol = T1.findSymbolIn(H, Name, ExportedOnly);
230 M.verifyFindSymbolIn(Symbol);
237 llvm::JITSymbol Sym2 = T1.findSymbolIn(H, Name, ExportedOnly);
238 M.verifyFindSymbolIn(std::move(Sym2));
231239
232240 // Test emitAndFinalize
233241 M.expectEmitAndFinalize(H);
234 T2.emitAndFinalize(H);
242 cantFail(T2.emitAndFinalize(H));
235243 M.verifyEmitAndFinalize();
236244
237245 // Test mapSectionAddress
304312 // Make sure that the calls from IRCompileLayer to ObjectTransformLayer
305313 // compile.
306314 auto Resolver = std::make_shared();
307 CompileLayer.addModule(std::shared_ptr(), Resolver);
315 cantFail(CompileLayer.addModule(std::shared_ptr(), Resolver));
308316
309317 // Make sure that the calls from ObjectTransformLayer to ObjectLinkingLayer
310318 // compile.
311319 decltype(TransformLayer)::ObjHandleT H2;
312 TransformLayer.emitAndFinalize(H2);
320 cantFail(TransformLayer.emitAndFinalize(H2));
313321 TransformLayer.findSymbolIn(H2, Name, false);
314322 TransformLayer.findSymbol(Name, true);
315323 TransformLayer.mapSectionAddress(H2, nullptr, 0);
316 TransformLayer.removeObject(H2);
324 cantFail(TransformLayer.removeObject(H2));
317325 }
318326 }
6565 auto *ET = CCtx->APIExecTest;
6666 CCtx->M = ET->createTestModule(ET->TM->getTargetTriple());
6767 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(CCtx->M.release()));
68 CCtx->H = LLVMOrcAddEagerlyCompiledIR(JITStack, SM, myResolver, nullptr);
68 LLVMOrcAddEagerlyCompiledIR(JITStack, &CCtx->H, SM, myResolver, nullptr);
6969 LLVMOrcDisposeSharedModuleRef(SM);
7070 CCtx->Compiled = true;
71 LLVMOrcTargetAddress MainAddr = LLVMOrcGetSymbolAddress(JITStack, "main");
71 LLVMOrcTargetAddress MainAddr;
72 LLVMOrcGetSymbolAddress(JITStack, &MainAddr, "main");
7273 LLVMOrcSetIndirectStubPointer(JITStack, "foo", MainAddr);
7374 return MainAddr;
7475 }
8889 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
8990
9091 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
91 LLVMOrcModuleHandle H =
92 LLVMOrcAddEagerlyCompiledIR(JIT, SM, myResolver, nullptr);
92 LLVMOrcModuleHandle H;
93 LLVMOrcAddEagerlyCompiledIR(JIT, &H, SM, myResolver, nullptr);
9394 LLVMOrcDisposeSharedModuleRef(SM);
94 MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main");
95 LLVMOrcTargetAddress MainAddr;
96 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main");
97 MainFnTy MainFn = (MainFnTy)MainAddr;
9598 int Result = MainFn();
9699 EXPECT_EQ(Result, 42)
97100 << "Eagerly JIT'd code did not return expected result";
114117 LLVMOrcGetMangledSymbol(JIT, &testFuncName, "testFunc");
115118
116119 LLVMSharedModuleRef SM = LLVMOrcMakeSharedModule(wrap(M.release()));
117 LLVMOrcModuleHandle H =
118 LLVMOrcAddLazilyCompiledIR(JIT, SM, myResolver, nullptr);
120 LLVMOrcModuleHandle H;
121 LLVMOrcAddLazilyCompiledIR(JIT, &H, SM, myResolver, nullptr);
119122 LLVMOrcDisposeSharedModuleRef(SM);
120 MainFnTy MainFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "main");
123 LLVMOrcTargetAddress MainAddr;
124 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "main");
125 MainFnTy MainFn = (MainFnTy)MainAddr;
121126 int Result = MainFn();
122127 EXPECT_EQ(Result, 42)
123128 << "Lazily JIT'd code did not return expected result";
139144
140145 CompileContext C;
141146 C.APIExecTest = this;
142 LLVMOrcCreateIndirectStub(JIT, "foo",
143 LLVMOrcCreateLazyCompileCallback(JIT,
144 myCompileCallback,
145 &C));
146 MainFnTy FooFn = (MainFnTy)LLVMOrcGetSymbolAddress(JIT, "foo");
147 LLVMOrcTargetAddress CCAddr;
148 LLVMOrcCreateLazyCompileCallback(JIT, &CCAddr, myCompileCallback, &C);
149 LLVMOrcCreateIndirectStub(JIT, "foo", CCAddr);
150 LLVMOrcTargetAddress MainAddr;
151 LLVMOrcGetSymbolAddress(JIT, &MainAddr, "foo");
152 MainFnTy FooFn = (MainFnTy)MainAddr;
147153 int Result = FooFn();
148154 EXPECT_TRUE(C.Compiled)
149155 << "Function wasn't lazily compiled";
118118 RemoveModuleFtor &&RemoveModule,
119119 FindSymbolFtor &&FindSymbol,
120120 FindSymbolInFtor &&FindSymbolIn)
121 : AddModule(AddModule), RemoveModule(RemoveModule),
122 FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
121 : AddModule(std::move(AddModule)),
122 RemoveModule(std::move(RemoveModule)),
123 FindSymbol(std::move(FindSymbol)),
124 FindSymbolIn(std::move(FindSymbolIn))
123125 {}
124126
125127 template
126128 typename SymbolResolverPtrT>
127 ModuleHandleT addModule(ModuleT Ms, MemoryManagerPtrT MemMgr,
128 SymbolResolverPtrT Resolver) {
129 Expected addModule(ModuleT Ms, MemoryManagerPtrT MemMgr,
130 SymbolResolverPtrT Resolver) {
129131 return AddModule(std::move(Ms), std::move(MemMgr), std::move(Resolver));
130132 }
131133
132 void removeModule(ModuleHandleT H) {
133 RemoveModule(H);
134 Error removeModule(ModuleHandleT H) {
135 return RemoveModule(H);
134136 }
135137
136138 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
168170 std::forward(FindSymbolIn));
169171 }
170172
173
174 class ReturnNullJITSymbol {
175 public:
176 template
177 JITSymbol operator()(Args...) const {
178 return nullptr;
179 }
180 };
181
171182 template
172183 class DoNothingAndReturn {
173184 public:
174 DoNothingAndReturn(ReturnT Val) : Val(Val) {}
185 DoNothingAndReturn(ReturnT Ret) : Ret(std::move(Ret)) {}
175186
176187 template
177 ReturnT operator()(Args...) const { return Val; }
178 private:
179 ReturnT Val;
188 void operator()(Args...) const { return Ret; }
189 private:
190 ReturnT Ret;
180191 };
181192
182193 template <>
102102
103103 {
104104 // Test with ProcessAllSections = false (the default).
105 auto H = ObjLayer.addObject(Obj, Resolver);
106 ObjLayer.emitAndFinalize(H);
105 auto H = cantFail(ObjLayer.addObject(Obj, Resolver));
106 cantFail(ObjLayer.emitAndFinalize(H));
107107 EXPECT_EQ(DebugSectionSeen, false)
108108 << "Unexpected debug info section";
109 ObjLayer.removeObject(H);
109 cantFail(ObjLayer.removeObject(H));
110110 }
111111
112112 {
113113 // Test with ProcessAllSections = true.
114114 ObjLayer.setProcessAllSections(true);
115 auto H = ObjLayer.addObject(Obj, Resolver);
116 ObjLayer.emitAndFinalize(H);
115 auto H = cantFail(ObjLayer.addObject(Obj, Resolver));
116 cantFail(ObjLayer.emitAndFinalize(H));
117117 EXPECT_EQ(DebugSectionSeen, true)
118118 << "Expected debug info section not seen";
119 ObjLayer.removeObject(H);
119 cantFail(ObjLayer.removeObject(H));
120120 }
121121 }
122122
180180 return JITSymbol(nullptr);
181181 });
182182
183 ObjLayer.addObject(std::move(Obj1), Resolver);
184 auto H = ObjLayer.addObject(std::move(Obj2), Resolver);
185 ObjLayer.emitAndFinalize(H);
186 ObjLayer.removeObject(H);
187
183 cantFail(ObjLayer.addObject(std::move(Obj1), Resolver));
184 auto H = cantFail(ObjLayer.addObject(std::move(Obj2), Resolver));
185 cantFail(ObjLayer.emitAndFinalize(H));
186 cantFail(ObjLayer.removeObject(H));
187
188188 // Finalization of module 2 should trigger finalization of module 1.
189189 // Verify that finalize on SMMW is only called once.
190190 EXPECT_EQ(MM->FinalizationCount, 1)
243243 Compile(*MB2.getModule()));
244244
245245 auto NR = std::make_shared();
246 auto H = ObjLayer.addObject(std::move(Obj1), NR);
247 ObjLayer.addObject(std::move(Obj2), NR);
248 ObjLayer.emitAndFinalize(H);
249 ObjLayer.removeObject(H);
250
246 auto H = cantFail(ObjLayer.addObject(std::move(Obj1), NR));
247 cantFail(ObjLayer.addObject(std::move(Obj2), NR));
248 cantFail(ObjLayer.emitAndFinalize(H));
249 cantFail(ObjLayer.removeObject(H));
250
251251 // Only one call to needsToReserveAllocationSpace should have been made.
252252 EXPECT_EQ(MM->NeedsToReserveAllocationSpaceCount, 1)
253253 << "More than one call to needsToReserveAllocationSpace "