1 //===--------- LLJIT.cpp - An ORC-based JIT for compiling LLVM IR ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ExecutionEngine/Orc/LLJIT.h" 10 #include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h" 11 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h" 12 #include "llvm/ExecutionEngine/Orc/MachOPlatform.h" 13 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" 14 #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h" 15 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" 16 #include "llvm/ExecutionEngine/Orc/Shared/OrcError.h" 17 #include "llvm/ExecutionEngine/Orc/TargetProcessControl.h" 18 #include "llvm/ExecutionEngine/SectionMemoryManager.h" 19 #include "llvm/IR/GlobalVariable.h" 20 #include "llvm/IR/IRBuilder.h" 21 #include "llvm/IR/Mangler.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/Support/DynamicLibrary.h" 24 25 #include <map> 26 27 #define DEBUG_TYPE "orc" 28 29 using namespace llvm; 30 using namespace llvm::orc; 31 32 namespace { 33 34 /// Adds helper function decls and wrapper functions that call the helper with 35 /// some additional prefix arguments. 36 /// 37 /// E.g. For wrapper "foo" with type i8(i8, i64), helper "bar", and prefix 38 /// args i32 4 and i16 12345, this function will add: 39 /// 40 /// declare i8 @bar(i32, i16, i8, i64) 41 /// 42 /// define i8 @foo(i8, i64) { 43 /// entry: 44 /// %2 = call i8 @bar(i32 4, i16 12345, i8 %0, i64 %1) 45 /// ret i8 %2 46 /// } 47 /// 48 Function *addHelperAndWrapper(Module &M, StringRef WrapperName, 49 FunctionType *WrapperFnType, 50 GlobalValue::VisibilityTypes WrapperVisibility, 51 StringRef HelperName, 52 ArrayRef<Value *> HelperPrefixArgs) { 53 std::vector<Type *> HelperArgTypes; 54 for (auto *Arg : HelperPrefixArgs) 55 HelperArgTypes.push_back(Arg->getType()); 56 for (auto *T : WrapperFnType->params()) 57 HelperArgTypes.push_back(T); 58 auto *HelperFnType = 59 FunctionType::get(WrapperFnType->getReturnType(), HelperArgTypes, false); 60 auto *HelperFn = Function::Create(HelperFnType, GlobalValue::ExternalLinkage, 61 HelperName, M); 62 63 auto *WrapperFn = Function::Create( 64 WrapperFnType, GlobalValue::ExternalLinkage, WrapperName, M); 65 WrapperFn->setVisibility(WrapperVisibility); 66 67 auto *EntryBlock = BasicBlock::Create(M.getContext(), "entry", WrapperFn); 68 IRBuilder<> IB(EntryBlock); 69 70 std::vector<Value *> HelperArgs; 71 for (auto *Arg : HelperPrefixArgs) 72 HelperArgs.push_back(Arg); 73 for (auto &Arg : WrapperFn->args()) 74 HelperArgs.push_back(&Arg); 75 auto *HelperResult = IB.CreateCall(HelperFn, HelperArgs); 76 if (HelperFn->getReturnType()->isVoidTy()) 77 IB.CreateRetVoid(); 78 else 79 IB.CreateRet(HelperResult); 80 81 return WrapperFn; 82 } 83 84 class GenericLLVMIRPlatformSupport; 85 86 /// orc::Platform component of Generic LLVM IR Platform support. 87 /// Just forwards calls to the GenericLLVMIRPlatformSupport class below. 88 class GenericLLVMIRPlatform : public Platform { 89 public: 90 GenericLLVMIRPlatform(GenericLLVMIRPlatformSupport &S) : S(S) {} 91 Error setupJITDylib(JITDylib &JD) override; 92 Error notifyAdding(ResourceTracker &RT, 93 const MaterializationUnit &MU) override; 94 Error notifyRemoving(ResourceTracker &RT) override { 95 // Noop -- Nothing to do (yet). 96 return Error::success(); 97 } 98 99 private: 100 GenericLLVMIRPlatformSupport &S; 101 }; 102 103 /// This transform parses llvm.global_ctors to produce a single initialization 104 /// function for the module, records the function, then deletes 105 /// llvm.global_ctors. 106 class GlobalCtorDtorScraper { 107 public: 108 109 GlobalCtorDtorScraper(GenericLLVMIRPlatformSupport &PS, 110 StringRef InitFunctionPrefix) 111 : PS(PS), InitFunctionPrefix(InitFunctionPrefix) {} 112 Expected<ThreadSafeModule> operator()(ThreadSafeModule TSM, 113 MaterializationResponsibility &R); 114 115 private: 116 GenericLLVMIRPlatformSupport &PS; 117 StringRef InitFunctionPrefix; 118 }; 119 120 /// Generic IR Platform Support 121 /// 122 /// Scrapes llvm.global_ctors and llvm.global_dtors and replaces them with 123 /// specially named 'init' and 'deinit'. Injects definitions / interposes for 124 /// some runtime API, including __cxa_atexit, dlopen, and dlclose. 125 class GenericLLVMIRPlatformSupport : public LLJIT::PlatformSupport { 126 public: 127 // GenericLLVMIRPlatform &P) : P(P) { 128 GenericLLVMIRPlatformSupport(LLJIT &J) 129 : J(J), InitFunctionPrefix(J.mangle("__orc_init_func.")) { 130 131 getExecutionSession().setPlatform( 132 std::make_unique<GenericLLVMIRPlatform>(*this)); 133 134 setInitTransform(J, GlobalCtorDtorScraper(*this, InitFunctionPrefix)); 135 136 SymbolMap StdInterposes; 137 138 StdInterposes[J.mangleAndIntern("__lljit.platform_support_instance")] = 139 JITEvaluatedSymbol(pointerToJITTargetAddress(this), 140 JITSymbolFlags::Exported); 141 StdInterposes[J.mangleAndIntern("__lljit.cxa_atexit_helper")] = 142 JITEvaluatedSymbol(pointerToJITTargetAddress(registerAtExitHelper), 143 JITSymbolFlags()); 144 145 cantFail( 146 J.getMainJITDylib().define(absoluteSymbols(std::move(StdInterposes)))); 147 cantFail(setupJITDylib(J.getMainJITDylib())); 148 cantFail(J.addIRModule(J.getMainJITDylib(), createPlatformRuntimeModule())); 149 } 150 151 ExecutionSession &getExecutionSession() { return J.getExecutionSession(); } 152 153 /// Adds a module that defines the __dso_handle global. 154 Error setupJITDylib(JITDylib &JD) { 155 156 // Add per-jitdylib standard interposes. 157 SymbolMap PerJDInterposes; 158 PerJDInterposes[J.mangleAndIntern("__lljit.run_atexits_helper")] = 159 JITEvaluatedSymbol(pointerToJITTargetAddress(runAtExitsHelper), 160 JITSymbolFlags()); 161 cantFail(JD.define(absoluteSymbols(std::move(PerJDInterposes)))); 162 163 auto Ctx = std::make_unique<LLVMContext>(); 164 auto M = std::make_unique<Module>("__standard_lib", *Ctx); 165 M->setDataLayout(J.getDataLayout()); 166 167 auto *Int64Ty = Type::getInt64Ty(*Ctx); 168 auto *DSOHandle = new GlobalVariable( 169 *M, Int64Ty, true, GlobalValue::ExternalLinkage, 170 ConstantInt::get(Int64Ty, reinterpret_cast<uintptr_t>(&JD)), 171 "__dso_handle"); 172 DSOHandle->setVisibility(GlobalValue::DefaultVisibility); 173 DSOHandle->setInitializer( 174 ConstantInt::get(Int64Ty, pointerToJITTargetAddress(&JD))); 175 176 auto *GenericIRPlatformSupportTy = 177 StructType::create(*Ctx, "lljit.GenericLLJITIRPlatformSupport"); 178 179 auto *PlatformInstanceDecl = new GlobalVariable( 180 *M, GenericIRPlatformSupportTy, true, GlobalValue::ExternalLinkage, 181 nullptr, "__lljit.platform_support_instance"); 182 183 auto *VoidTy = Type::getVoidTy(*Ctx); 184 addHelperAndWrapper( 185 *M, "__lljit_run_atexits", FunctionType::get(VoidTy, {}, false), 186 GlobalValue::HiddenVisibility, "__lljit.run_atexits_helper", 187 {PlatformInstanceDecl, DSOHandle}); 188 189 return J.addIRModule(JD, ThreadSafeModule(std::move(M), std::move(Ctx))); 190 } 191 192 Error notifyAdding(ResourceTracker &RT, const MaterializationUnit &MU) { 193 auto &JD = RT.getJITDylib(); 194 if (auto &InitSym = MU.getInitializerSymbol()) 195 InitSymbols[&JD].add(InitSym, SymbolLookupFlags::WeaklyReferencedSymbol); 196 else { 197 // If there's no identified init symbol attached, but there is a symbol 198 // with the GenericIRPlatform::InitFunctionPrefix, then treat that as 199 // an init function. Add the symbol to both the InitSymbols map (which 200 // will trigger a lookup to materialize the module) and the InitFunctions 201 // map (which holds the names of the symbols to execute). 202 for (auto &KV : MU.getSymbols()) 203 if ((*KV.first).startswith(InitFunctionPrefix)) { 204 InitSymbols[&JD].add(KV.first, 205 SymbolLookupFlags::WeaklyReferencedSymbol); 206 InitFunctions[&JD].add(KV.first); 207 } 208 } 209 return Error::success(); 210 } 211 212 Error initialize(JITDylib &JD) override { 213 LLVM_DEBUG({ 214 dbgs() << "GenericLLVMIRPlatformSupport getting initializers to run\n"; 215 }); 216 if (auto Initializers = getInitializers(JD)) { 217 LLVM_DEBUG( 218 { dbgs() << "GenericLLVMIRPlatformSupport running initializers\n"; }); 219 for (auto InitFnAddr : *Initializers) { 220 LLVM_DEBUG({ 221 dbgs() << " Running init " << formatv("{0:x16}", InitFnAddr) 222 << "...\n"; 223 }); 224 auto *InitFn = jitTargetAddressToFunction<void (*)()>(InitFnAddr); 225 InitFn(); 226 } 227 } else 228 return Initializers.takeError(); 229 return Error::success(); 230 } 231 232 Error deinitialize(JITDylib &JD) override { 233 LLVM_DEBUG({ 234 dbgs() << "GenericLLVMIRPlatformSupport getting deinitializers to run\n"; 235 }); 236 if (auto Deinitializers = getDeinitializers(JD)) { 237 LLVM_DEBUG({ 238 dbgs() << "GenericLLVMIRPlatformSupport running deinitializers\n"; 239 }); 240 for (auto DeinitFnAddr : *Deinitializers) { 241 LLVM_DEBUG({ 242 dbgs() << " Running deinit " << formatv("{0:x16}", DeinitFnAddr) 243 << "...\n"; 244 }); 245 auto *DeinitFn = jitTargetAddressToFunction<void (*)()>(DeinitFnAddr); 246 DeinitFn(); 247 } 248 } else 249 return Deinitializers.takeError(); 250 251 return Error::success(); 252 } 253 254 void registerInitFunc(JITDylib &JD, SymbolStringPtr InitName) { 255 getExecutionSession().runSessionLocked([&]() { 256 InitFunctions[&JD].add(InitName); 257 }); 258 } 259 260 private: 261 262 Expected<std::vector<JITTargetAddress>> getInitializers(JITDylib &JD) { 263 if (auto Err = issueInitLookups(JD)) 264 return std::move(Err); 265 266 DenseMap<JITDylib *, SymbolLookupSet> LookupSymbols; 267 std::vector<JITDylibSP> DFSLinkOrder; 268 269 getExecutionSession().runSessionLocked([&]() { 270 DFSLinkOrder = JD.getDFSLinkOrder(); 271 272 for (auto &NextJD : DFSLinkOrder) { 273 auto IFItr = InitFunctions.find(NextJD.get()); 274 if (IFItr != InitFunctions.end()) { 275 LookupSymbols[NextJD.get()] = std::move(IFItr->second); 276 InitFunctions.erase(IFItr); 277 } 278 } 279 }); 280 281 LLVM_DEBUG({ 282 dbgs() << "JITDylib init order is [ "; 283 for (auto &JD : llvm::reverse(DFSLinkOrder)) 284 dbgs() << "\"" << JD->getName() << "\" "; 285 dbgs() << "]\n"; 286 dbgs() << "Looking up init functions:\n"; 287 for (auto &KV : LookupSymbols) 288 dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n"; 289 }); 290 291 auto &ES = getExecutionSession(); 292 auto LookupResult = Platform::lookupInitSymbols(ES, LookupSymbols); 293 294 if (!LookupResult) 295 return LookupResult.takeError(); 296 297 std::vector<JITTargetAddress> Initializers; 298 while (!DFSLinkOrder.empty()) { 299 auto &NextJD = *DFSLinkOrder.back(); 300 DFSLinkOrder.pop_back(); 301 auto InitsItr = LookupResult->find(&NextJD); 302 if (InitsItr == LookupResult->end()) 303 continue; 304 for (auto &KV : InitsItr->second) 305 Initializers.push_back(KV.second.getAddress()); 306 } 307 308 return Initializers; 309 } 310 311 Expected<std::vector<JITTargetAddress>> getDeinitializers(JITDylib &JD) { 312 auto &ES = getExecutionSession(); 313 314 auto LLJITRunAtExits = J.mangleAndIntern("__lljit_run_atexits"); 315 316 DenseMap<JITDylib *, SymbolLookupSet> LookupSymbols; 317 std::vector<JITDylibSP> DFSLinkOrder; 318 319 ES.runSessionLocked([&]() { 320 DFSLinkOrder = JD.getDFSLinkOrder(); 321 322 for (auto &NextJD : DFSLinkOrder) { 323 auto &JDLookupSymbols = LookupSymbols[NextJD.get()]; 324 auto DIFItr = DeInitFunctions.find(NextJD.get()); 325 if (DIFItr != DeInitFunctions.end()) { 326 LookupSymbols[NextJD.get()] = std::move(DIFItr->second); 327 DeInitFunctions.erase(DIFItr); 328 } 329 JDLookupSymbols.add(LLJITRunAtExits, 330 SymbolLookupFlags::WeaklyReferencedSymbol); 331 } 332 }); 333 334 LLVM_DEBUG({ 335 dbgs() << "JITDylib deinit order is [ "; 336 for (auto &JD : DFSLinkOrder) 337 dbgs() << "\"" << JD->getName() << "\" "; 338 dbgs() << "]\n"; 339 dbgs() << "Looking up deinit functions:\n"; 340 for (auto &KV : LookupSymbols) 341 dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n"; 342 }); 343 344 auto LookupResult = Platform::lookupInitSymbols(ES, LookupSymbols); 345 346 if (!LookupResult) 347 return LookupResult.takeError(); 348 349 std::vector<JITTargetAddress> DeInitializers; 350 for (auto &NextJD : DFSLinkOrder) { 351 auto DeInitsItr = LookupResult->find(NextJD.get()); 352 assert(DeInitsItr != LookupResult->end() && 353 "Every JD should have at least __lljit_run_atexits"); 354 355 auto RunAtExitsItr = DeInitsItr->second.find(LLJITRunAtExits); 356 if (RunAtExitsItr != DeInitsItr->second.end()) 357 DeInitializers.push_back(RunAtExitsItr->second.getAddress()); 358 359 for (auto &KV : DeInitsItr->second) 360 if (KV.first != LLJITRunAtExits) 361 DeInitializers.push_back(KV.second.getAddress()); 362 } 363 364 return DeInitializers; 365 } 366 367 /// Issue lookups for all init symbols required to initialize JD (and any 368 /// JITDylibs that it depends on). 369 Error issueInitLookups(JITDylib &JD) { 370 DenseMap<JITDylib *, SymbolLookupSet> RequiredInitSymbols; 371 std::vector<JITDylibSP> DFSLinkOrder; 372 373 getExecutionSession().runSessionLocked([&]() { 374 DFSLinkOrder = JD.getDFSLinkOrder(); 375 376 for (auto &NextJD : DFSLinkOrder) { 377 auto ISItr = InitSymbols.find(NextJD.get()); 378 if (ISItr != InitSymbols.end()) { 379 RequiredInitSymbols[NextJD.get()] = std::move(ISItr->second); 380 InitSymbols.erase(ISItr); 381 } 382 } 383 }); 384 385 return Platform::lookupInitSymbols(getExecutionSession(), 386 RequiredInitSymbols) 387 .takeError(); 388 } 389 390 static void registerAtExitHelper(void *Self, void (*F)(void *), void *Ctx, 391 void *DSOHandle) { 392 LLVM_DEBUG({ 393 dbgs() << "Registering atexit function " << (void *)F << " for JD " 394 << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n"; 395 }); 396 static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.registerAtExit( 397 F, Ctx, DSOHandle); 398 } 399 400 static void runAtExitsHelper(void *Self, void *DSOHandle) { 401 LLVM_DEBUG({ 402 dbgs() << "Running atexit functions for JD " 403 << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n"; 404 }); 405 static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.runAtExits( 406 DSOHandle); 407 } 408 409 // Constructs an LLVM IR module containing platform runtime globals, 410 // functions, and interposes. 411 ThreadSafeModule createPlatformRuntimeModule() { 412 auto Ctx = std::make_unique<LLVMContext>(); 413 auto M = std::make_unique<Module>("__standard_lib", *Ctx); 414 M->setDataLayout(J.getDataLayout()); 415 416 auto *GenericIRPlatformSupportTy = 417 StructType::create(*Ctx, "lljit.GenericLLJITIRPlatformSupport"); 418 419 auto *PlatformInstanceDecl = new GlobalVariable( 420 *M, GenericIRPlatformSupportTy, true, GlobalValue::ExternalLinkage, 421 nullptr, "__lljit.platform_support_instance"); 422 423 auto *Int8Ty = Type::getInt8Ty(*Ctx); 424 auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT); 425 auto *VoidTy = Type::getVoidTy(*Ctx); 426 auto *BytePtrTy = PointerType::getUnqual(Int8Ty); 427 auto *AtExitCallbackTy = FunctionType::get(VoidTy, {BytePtrTy}, false); 428 auto *AtExitCallbackPtrTy = PointerType::getUnqual(AtExitCallbackTy); 429 430 addHelperAndWrapper( 431 *M, "__cxa_atexit", 432 FunctionType::get(IntTy, {AtExitCallbackPtrTy, BytePtrTy, BytePtrTy}, 433 false), 434 GlobalValue::DefaultVisibility, "__lljit.cxa_atexit_helper", 435 {PlatformInstanceDecl}); 436 437 return ThreadSafeModule(std::move(M), std::move(Ctx)); 438 } 439 440 LLJIT &J; 441 std::string InitFunctionPrefix; 442 DenseMap<JITDylib *, SymbolLookupSet> InitSymbols; 443 DenseMap<JITDylib *, SymbolLookupSet> InitFunctions; 444 DenseMap<JITDylib *, SymbolLookupSet> DeInitFunctions; 445 ItaniumCXAAtExitSupport AtExitMgr; 446 }; 447 448 Error GenericLLVMIRPlatform::setupJITDylib(JITDylib &JD) { 449 return S.setupJITDylib(JD); 450 } 451 452 Error GenericLLVMIRPlatform::notifyAdding(ResourceTracker &RT, 453 const MaterializationUnit &MU) { 454 return S.notifyAdding(RT, MU); 455 } 456 457 Expected<ThreadSafeModule> 458 GlobalCtorDtorScraper::operator()(ThreadSafeModule TSM, 459 MaterializationResponsibility &R) { 460 auto Err = TSM.withModuleDo([&](Module &M) -> Error { 461 auto &Ctx = M.getContext(); 462 auto *GlobalCtors = M.getNamedGlobal("llvm.global_ctors"); 463 464 // If there's no llvm.global_ctors or it's just a decl then skip. 465 if (!GlobalCtors || GlobalCtors->isDeclaration()) 466 return Error::success(); 467 468 std::string InitFunctionName; 469 raw_string_ostream(InitFunctionName) 470 << InitFunctionPrefix << M.getModuleIdentifier(); 471 472 MangleAndInterner Mangle(PS.getExecutionSession(), M.getDataLayout()); 473 auto InternedName = Mangle(InitFunctionName); 474 if (auto Err = 475 R.defineMaterializing({{InternedName, JITSymbolFlags::Callable}})) 476 return Err; 477 478 auto *InitFunc = 479 Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {}, false), 480 GlobalValue::ExternalLinkage, InitFunctionName, &M); 481 InitFunc->setVisibility(GlobalValue::HiddenVisibility); 482 std::vector<std::pair<Function *, unsigned>> Inits; 483 for (auto E : getConstructors(M)) 484 Inits.push_back(std::make_pair(E.Func, E.Priority)); 485 llvm::sort(Inits, [](const std::pair<Function *, unsigned> &LHS, 486 const std::pair<Function *, unsigned> &RHS) { 487 return LHS.first < RHS.first; 488 }); 489 auto *EntryBlock = BasicBlock::Create(Ctx, "entry", InitFunc); 490 IRBuilder<> IB(EntryBlock); 491 for (auto &KV : Inits) 492 IB.CreateCall(KV.first); 493 IB.CreateRetVoid(); 494 495 PS.registerInitFunc(R.getTargetJITDylib(), InternedName); 496 GlobalCtors->eraseFromParent(); 497 return Error::success(); 498 }); 499 500 if (Err) 501 return std::move(Err); 502 503 return std::move(TSM); 504 } 505 506 class MachOPlatformSupport : public LLJIT::PlatformSupport { 507 public: 508 using DLOpenType = void *(*)(const char *Name, int Mode); 509 using DLCloseType = int (*)(void *Handle); 510 using DLSymType = void *(*)(void *Handle, const char *Name); 511 using DLErrorType = const char *(*)(); 512 513 struct DlFcnValues { 514 Optional<void *> RTLDDefault; 515 DLOpenType dlopen = nullptr; 516 DLCloseType dlclose = nullptr; 517 DLSymType dlsym = nullptr; 518 DLErrorType dlerror = nullptr; 519 }; 520 521 static Expected<std::unique_ptr<MachOPlatformSupport>> 522 Create(LLJIT &J, JITDylib &PlatformJITDylib) { 523 524 // Make process symbols visible. 525 { 526 std::string ErrMsg; 527 auto Lib = sys::DynamicLibrary::getPermanentLibrary(nullptr, &ErrMsg); 528 if (!Lib.isValid()) 529 return make_error<StringError>(std::move(ErrMsg), 530 inconvertibleErrorCode()); 531 } 532 533 DlFcnValues DlFcn; 534 535 // Add support for RTLDDefault on known platforms. 536 #ifdef __APPLE__ 537 DlFcn.RTLDDefault = reinterpret_cast<void *>(-2); 538 #endif // __APPLE__ 539 540 if (auto Err = hookUpFunction(DlFcn.dlopen, "dlopen")) 541 return std::move(Err); 542 if (auto Err = hookUpFunction(DlFcn.dlclose, "dlclose")) 543 return std::move(Err); 544 if (auto Err = hookUpFunction(DlFcn.dlsym, "dlsym")) 545 return std::move(Err); 546 if (auto Err = hookUpFunction(DlFcn.dlerror, "dlerror")) 547 return std::move(Err); 548 549 std::unique_ptr<MachOPlatformSupport> MP( 550 new MachOPlatformSupport(J, PlatformJITDylib, DlFcn)); 551 return std::move(MP); 552 } 553 554 Error initialize(JITDylib &JD) override { 555 LLVM_DEBUG({ 556 dbgs() << "MachOPlatformSupport initializing \"" << JD.getName() 557 << "\"\n"; 558 }); 559 560 auto InitSeq = MP.getInitializerSequence(JD); 561 if (!InitSeq) 562 return InitSeq.takeError(); 563 564 // If ObjC is not enabled but there are JIT'd ObjC inits then return 565 // an error. 566 if (!objCRegistrationEnabled()) 567 for (auto &KV : *InitSeq) { 568 if (!KV.second.getObjCSelRefsSections().empty() || 569 !KV.second.getObjCClassListSections().empty()) 570 return make_error<StringError>("JITDylib " + KV.first->getName() + 571 " contains objc metadata but objc" 572 " is not enabled", 573 inconvertibleErrorCode()); 574 } 575 576 // Run the initializers. 577 for (auto &KV : *InitSeq) { 578 if (objCRegistrationEnabled()) { 579 KV.second.registerObjCSelectors(); 580 if (auto Err = KV.second.registerObjCClasses()) { 581 // FIXME: Roll back registrations on error? 582 return Err; 583 } 584 } 585 KV.second.runModInits(); 586 } 587 588 return Error::success(); 589 } 590 591 Error deinitialize(JITDylib &JD) override { 592 auto &ES = J.getExecutionSession(); 593 if (auto DeinitSeq = MP.getDeinitializerSequence(JD)) { 594 for (auto &KV : *DeinitSeq) { 595 auto DSOHandleName = ES.intern("___dso_handle"); 596 597 // FIXME: Run DeInits here. 598 auto Result = ES.lookup( 599 {{KV.first, JITDylibLookupFlags::MatchAllSymbols}}, 600 SymbolLookupSet(DSOHandleName, 601 SymbolLookupFlags::WeaklyReferencedSymbol)); 602 if (!Result) 603 return Result.takeError(); 604 if (Result->empty()) 605 continue; 606 assert(Result->count(DSOHandleName) && 607 "Result does not contain __dso_handle"); 608 auto *DSOHandle = jitTargetAddressToPointer<void *>( 609 Result->begin()->second.getAddress()); 610 AtExitMgr.runAtExits(DSOHandle); 611 } 612 } else 613 return DeinitSeq.takeError(); 614 return Error::success(); 615 } 616 617 private: 618 template <typename FunctionPtrTy> 619 static Error hookUpFunction(FunctionPtrTy &Fn, const char *Name) { 620 if (auto *FnAddr = sys::DynamicLibrary::SearchForAddressOfSymbol(Name)) { 621 Fn = reinterpret_cast<FunctionPtrTy>(Fn); 622 return Error::success(); 623 } 624 625 return make_error<StringError>((Twine("Can not enable MachO JIT Platform: " 626 "missing function: ") + 627 Name) 628 .str(), 629 inconvertibleErrorCode()); 630 } 631 632 MachOPlatformSupport(LLJIT &J, JITDylib &PlatformJITDylib, DlFcnValues DlFcn) 633 : J(J), MP(setupPlatform(J)), DlFcn(std::move(DlFcn)) { 634 635 SymbolMap HelperSymbols; 636 637 // platform and atexit helpers. 638 HelperSymbols[J.mangleAndIntern("__lljit.platform_support_instance")] = 639 JITEvaluatedSymbol(pointerToJITTargetAddress(this), JITSymbolFlags()); 640 HelperSymbols[J.mangleAndIntern("__lljit.cxa_atexit_helper")] = 641 JITEvaluatedSymbol(pointerToJITTargetAddress(registerAtExitHelper), 642 JITSymbolFlags()); 643 HelperSymbols[J.mangleAndIntern("__lljit.run_atexits_helper")] = 644 JITEvaluatedSymbol(pointerToJITTargetAddress(runAtExitsHelper), 645 JITSymbolFlags()); 646 647 // dlfcn helpers. 648 HelperSymbols[J.mangleAndIntern("__lljit.dlopen_helper")] = 649 JITEvaluatedSymbol(pointerToJITTargetAddress(dlopenHelper), 650 JITSymbolFlags()); 651 HelperSymbols[J.mangleAndIntern("__lljit.dlclose_helper")] = 652 JITEvaluatedSymbol(pointerToJITTargetAddress(dlcloseHelper), 653 JITSymbolFlags()); 654 HelperSymbols[J.mangleAndIntern("__lljit.dlsym_helper")] = 655 JITEvaluatedSymbol(pointerToJITTargetAddress(dlsymHelper), 656 JITSymbolFlags()); 657 HelperSymbols[J.mangleAndIntern("__lljit.dlerror_helper")] = 658 JITEvaluatedSymbol(pointerToJITTargetAddress(dlerrorHelper), 659 JITSymbolFlags()); 660 661 cantFail( 662 PlatformJITDylib.define(absoluteSymbols(std::move(HelperSymbols)))); 663 cantFail(MP.setupJITDylib(J.getMainJITDylib())); 664 cantFail(J.addIRModule(PlatformJITDylib, createPlatformRuntimeModule())); 665 } 666 667 static MachOPlatform &setupPlatform(LLJIT &J) { 668 auto Tmp = std::make_unique<MachOPlatform>( 669 J.getExecutionSession(), 670 static_cast<ObjectLinkingLayer &>(J.getObjLinkingLayer()), 671 createStandardSymbolsObject(J)); 672 auto &MP = *Tmp; 673 J.getExecutionSession().setPlatform(std::move(Tmp)); 674 return MP; 675 } 676 677 static std::unique_ptr<MemoryBuffer> createStandardSymbolsObject(LLJIT &J) { 678 LLVMContext Ctx; 679 Module M("__standard_symbols", Ctx); 680 M.setDataLayout(J.getDataLayout()); 681 682 auto *Int64Ty = Type::getInt64Ty(Ctx); 683 684 auto *DSOHandle = 685 new GlobalVariable(M, Int64Ty, true, GlobalValue::ExternalLinkage, 686 ConstantInt::get(Int64Ty, 0), "__dso_handle"); 687 DSOHandle->setVisibility(GlobalValue::DefaultVisibility); 688 689 return cantFail(J.getIRCompileLayer().getCompiler()(M)); 690 } 691 692 ThreadSafeModule createPlatformRuntimeModule() { 693 auto Ctx = std::make_unique<LLVMContext>(); 694 auto M = std::make_unique<Module>("__standard_lib", *Ctx); 695 M->setDataLayout(J.getDataLayout()); 696 697 auto *MachOPlatformSupportTy = 698 StructType::create(*Ctx, "lljit.MachOPlatformSupport"); 699 700 auto *PlatformInstanceDecl = new GlobalVariable( 701 *M, MachOPlatformSupportTy, true, GlobalValue::ExternalLinkage, nullptr, 702 "__lljit.platform_support_instance"); 703 704 auto *Int8Ty = Type::getInt8Ty(*Ctx); 705 auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT); 706 auto *VoidTy = Type::getVoidTy(*Ctx); 707 auto *BytePtrTy = PointerType::getUnqual(Int8Ty); 708 auto *AtExitCallbackTy = FunctionType::get(VoidTy, {BytePtrTy}, false); 709 auto *AtExitCallbackPtrTy = PointerType::getUnqual(AtExitCallbackTy); 710 711 addHelperAndWrapper( 712 *M, "__cxa_atexit", 713 FunctionType::get(IntTy, {AtExitCallbackPtrTy, BytePtrTy, BytePtrTy}, 714 false), 715 GlobalValue::DefaultVisibility, "__lljit.cxa_atexit_helper", 716 {PlatformInstanceDecl}); 717 718 addHelperAndWrapper(*M, "dlopen", 719 FunctionType::get(BytePtrTy, {BytePtrTy, IntTy}, false), 720 GlobalValue::DefaultVisibility, "__lljit.dlopen_helper", 721 {PlatformInstanceDecl}); 722 723 addHelperAndWrapper(*M, "dlclose", 724 FunctionType::get(IntTy, {BytePtrTy}, false), 725 GlobalValue::DefaultVisibility, 726 "__lljit.dlclose_helper", {PlatformInstanceDecl}); 727 728 addHelperAndWrapper( 729 *M, "dlsym", 730 FunctionType::get(BytePtrTy, {BytePtrTy, BytePtrTy}, false), 731 GlobalValue::DefaultVisibility, "__lljit.dlsym_helper", 732 {PlatformInstanceDecl}); 733 734 addHelperAndWrapper(*M, "dlerror", FunctionType::get(BytePtrTy, {}, false), 735 GlobalValue::DefaultVisibility, 736 "__lljit.dlerror_helper", {PlatformInstanceDecl}); 737 738 return ThreadSafeModule(std::move(M), std::move(Ctx)); 739 } 740 741 static void registerAtExitHelper(void *Self, void (*F)(void *), void *Ctx, 742 void *DSOHandle) { 743 static_cast<MachOPlatformSupport *>(Self)->AtExitMgr.registerAtExit( 744 F, Ctx, DSOHandle); 745 } 746 747 static void runAtExitsHelper(void *Self, void *DSOHandle) { 748 static_cast<MachOPlatformSupport *>(Self)->AtExitMgr.runAtExits(DSOHandle); 749 } 750 751 void *jit_dlopen(const char *Path, int Mode) { 752 JITDylib *JDToOpen = nullptr; 753 // FIXME: Do the right thing with Mode flags. 754 { 755 std::lock_guard<std::mutex> Lock(PlatformSupportMutex); 756 757 // Clear any existing error messages. 758 dlErrorMsgs.erase(std::this_thread::get_id()); 759 760 if (auto *JD = J.getExecutionSession().getJITDylibByName(Path)) { 761 auto I = JDRefCounts.find(JD); 762 if (I != JDRefCounts.end()) { 763 ++I->second; 764 return JD; 765 } 766 767 JDRefCounts[JD] = 1; 768 JDToOpen = JD; 769 } 770 } 771 772 if (JDToOpen) { 773 if (auto Err = initialize(*JDToOpen)) { 774 recordError(std::move(Err)); 775 return 0; 776 } 777 } 778 779 // Fall through to dlopen if no JITDylib found for Path. 780 return DlFcn.dlopen(Path, Mode); 781 } 782 783 static void *dlopenHelper(void *Self, const char *Path, int Mode) { 784 return static_cast<MachOPlatformSupport *>(Self)->jit_dlopen(Path, Mode); 785 } 786 787 int jit_dlclose(void *Handle) { 788 JITDylib *JDToClose = nullptr; 789 790 { 791 std::lock_guard<std::mutex> Lock(PlatformSupportMutex); 792 793 // Clear any existing error messages. 794 dlErrorMsgs.erase(std::this_thread::get_id()); 795 796 auto I = JDRefCounts.find(Handle); 797 if (I != JDRefCounts.end()) { 798 --I->second; 799 if (I->second == 0) { 800 JDRefCounts.erase(I); 801 JDToClose = static_cast<JITDylib *>(Handle); 802 } else 803 return 0; 804 } 805 } 806 807 if (JDToClose) { 808 if (auto Err = deinitialize(*JDToClose)) { 809 recordError(std::move(Err)); 810 return -1; 811 } 812 return 0; 813 } 814 815 // Fall through to dlclose if no JITDylib found for Path. 816 return DlFcn.dlclose(Handle); 817 } 818 819 static int dlcloseHelper(void *Self, void *Handle) { 820 return static_cast<MachOPlatformSupport *>(Self)->jit_dlclose(Handle); 821 } 822 823 void *jit_dlsym(void *Handle, const char *Name) { 824 JITDylibSearchOrder JITSymSearchOrder; 825 826 // FIXME: RTLD_NEXT, RTLD_SELF not supported. 827 { 828 std::lock_guard<std::mutex> Lock(PlatformSupportMutex); 829 830 // Clear any existing error messages. 831 dlErrorMsgs.erase(std::this_thread::get_id()); 832 833 if (JDRefCounts.count(Handle)) { 834 JITSymSearchOrder.push_back( 835 {static_cast<JITDylib *>(Handle), 836 JITDylibLookupFlags::MatchExportedSymbolsOnly}); 837 } else if (Handle == DlFcn.RTLDDefault) { 838 for (auto &KV : JDRefCounts) 839 JITSymSearchOrder.push_back( 840 {static_cast<JITDylib *>(KV.first), 841 JITDylibLookupFlags::MatchExportedSymbolsOnly}); 842 } 843 } 844 845 if (!JITSymSearchOrder.empty()) { 846 auto MangledName = J.mangleAndIntern(Name); 847 SymbolLookupSet Syms(MangledName, 848 SymbolLookupFlags::WeaklyReferencedSymbol); 849 if (auto Result = J.getExecutionSession().lookup(JITSymSearchOrder, Syms, 850 LookupKind::DLSym)) { 851 auto I = Result->find(MangledName); 852 if (I != Result->end()) 853 return jitTargetAddressToPointer<void *>(I->second.getAddress()); 854 } else { 855 recordError(Result.takeError()); 856 return 0; 857 } 858 } 859 860 // Fall through to dlsym. 861 return DlFcn.dlsym(Handle, Name); 862 } 863 864 static void *dlsymHelper(void *Self, void *Handle, const char *Name) { 865 return static_cast<MachOPlatformSupport *>(Self)->jit_dlsym(Handle, Name); 866 } 867 868 const char *jit_dlerror() { 869 { 870 std::lock_guard<std::mutex> Lock(PlatformSupportMutex); 871 auto I = dlErrorMsgs.find(std::this_thread::get_id()); 872 if (I != dlErrorMsgs.end()) 873 return I->second->c_str(); 874 } 875 return DlFcn.dlerror(); 876 } 877 878 static const char *dlerrorHelper(void *Self) { 879 return static_cast<MachOPlatformSupport *>(Self)->jit_dlerror(); 880 } 881 882 void recordError(Error Err) { 883 std::lock_guard<std::mutex> Lock(PlatformSupportMutex); 884 dlErrorMsgs[std::this_thread::get_id()] = 885 std::make_unique<std::string>(toString(std::move(Err))); 886 } 887 888 std::mutex PlatformSupportMutex; 889 LLJIT &J; 890 MachOPlatform &MP; 891 DlFcnValues DlFcn; 892 ItaniumCXAAtExitSupport AtExitMgr; 893 DenseMap<void *, unsigned> JDRefCounts; 894 std::map<std::thread::id, std::unique_ptr<std::string>> dlErrorMsgs; 895 }; 896 897 } // end anonymous namespace 898 899 namespace llvm { 900 namespace orc { 901 902 void LLJIT::PlatformSupport::setInitTransform( 903 LLJIT &J, IRTransformLayer::TransformFunction T) { 904 J.InitHelperTransformLayer->setTransform(std::move(T)); 905 } 906 907 LLJIT::PlatformSupport::~PlatformSupport() {} 908 909 Error LLJITBuilderState::prepareForConstruction() { 910 911 LLVM_DEBUG(dbgs() << "Preparing to create LLJIT instance...\n"); 912 913 if (!JTMB) { 914 LLVM_DEBUG({ 915 dbgs() << " No explicitly set JITTargetMachineBuilder. " 916 "Detecting host...\n"; 917 }); 918 if (auto JTMBOrErr = JITTargetMachineBuilder::detectHost()) 919 JTMB = std::move(*JTMBOrErr); 920 else 921 return JTMBOrErr.takeError(); 922 } 923 924 LLVM_DEBUG({ 925 dbgs() << " JITTargetMachineBuilder is " << JTMB << "\n" 926 << " Pre-constructed ExecutionSession: " << (ES ? "Yes" : "No") 927 << "\n" 928 << " DataLayout: "; 929 if (DL) 930 dbgs() << DL->getStringRepresentation() << "\n"; 931 else 932 dbgs() << "None (will be created by JITTargetMachineBuilder)\n"; 933 934 dbgs() << " Custom object-linking-layer creator: " 935 << (CreateObjectLinkingLayer ? "Yes" : "No") << "\n" 936 << " Custom compile-function creator: " 937 << (CreateCompileFunction ? "Yes" : "No") << "\n" 938 << " Custom platform-setup function: " 939 << (SetUpPlatform ? "Yes" : "No") << "\n" 940 << " Number of compile threads: " << NumCompileThreads; 941 if (!NumCompileThreads) 942 dbgs() << " (code will be compiled on the execution thread)\n"; 943 else 944 dbgs() << "\n"; 945 }); 946 947 // If the client didn't configure any linker options then auto-configure the 948 // JIT linker. 949 if (!CreateObjectLinkingLayer) { 950 auto &TT = JTMB->getTargetTriple(); 951 if (TT.isOSBinFormatMachO() && 952 (TT.getArch() == Triple::aarch64 || TT.getArch() == Triple::x86_64)) { 953 954 JTMB->setRelocationModel(Reloc::PIC_); 955 JTMB->setCodeModel(CodeModel::Small); 956 CreateObjectLinkingLayer = 957 [TPC = this->TPC]( 958 ExecutionSession &ES, 959 const Triple &) -> Expected<std::unique_ptr<ObjectLayer>> { 960 std::unique_ptr<ObjectLinkingLayer> ObjLinkingLayer; 961 if (TPC) 962 ObjLinkingLayer = 963 std::make_unique<ObjectLinkingLayer>(ES, TPC->getMemMgr()); 964 else 965 ObjLinkingLayer = std::make_unique<ObjectLinkingLayer>( 966 ES, std::make_unique<jitlink::InProcessMemoryManager>()); 967 ObjLinkingLayer->addPlugin(std::make_unique<EHFrameRegistrationPlugin>( 968 ES, std::make_unique<jitlink::InProcessEHFrameRegistrar>())); 969 return std::move(ObjLinkingLayer); 970 }; 971 } 972 } 973 974 return Error::success(); 975 } 976 977 LLJIT::~LLJIT() { 978 if (CompileThreads) 979 CompileThreads->wait(); 980 if (auto Err = ES->endSession()) 981 ES->reportError(std::move(Err)); 982 } 983 984 Error LLJIT::addIRModule(ResourceTrackerSP RT, ThreadSafeModule TSM) { 985 assert(TSM && "Can not add null module"); 986 987 if (auto Err = 988 TSM.withModuleDo([&](Module &M) { return applyDataLayout(M); })) 989 return Err; 990 991 return InitHelperTransformLayer->add(std::move(RT), std::move(TSM)); 992 } 993 994 Error LLJIT::addIRModule(JITDylib &JD, ThreadSafeModule TSM) { 995 return addIRModule(JD.getDefaultResourceTracker(), std::move(TSM)); 996 } 997 998 Error LLJIT::addObjectFile(ResourceTrackerSP RT, 999 std::unique_ptr<MemoryBuffer> Obj) { 1000 assert(Obj && "Can not add null object"); 1001 1002 return ObjTransformLayer->add(std::move(RT), std::move(Obj)); 1003 } 1004 1005 Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) { 1006 return addObjectFile(JD.getDefaultResourceTracker(), std::move(Obj)); 1007 } 1008 1009 Expected<JITEvaluatedSymbol> LLJIT::lookupLinkerMangled(JITDylib &JD, 1010 SymbolStringPtr Name) { 1011 return ES->lookup( 1012 makeJITDylibSearchOrder(&JD, JITDylibLookupFlags::MatchAllSymbols), Name); 1013 } 1014 1015 Expected<std::unique_ptr<ObjectLayer>> 1016 LLJIT::createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES) { 1017 1018 // If the config state provided an ObjectLinkingLayer factory then use it. 1019 if (S.CreateObjectLinkingLayer) 1020 return S.CreateObjectLinkingLayer(ES, S.JTMB->getTargetTriple()); 1021 1022 // Otherwise default to creating an RTDyldObjectLinkingLayer that constructs 1023 // a new SectionMemoryManager for each object. 1024 auto GetMemMgr = []() { return std::make_unique<SectionMemoryManager>(); }; 1025 auto ObjLinkingLayer = 1026 std::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr)); 1027 1028 if (S.JTMB->getTargetTriple().isOSBinFormatCOFF()) { 1029 ObjLinkingLayer->setOverrideObjectFlagsWithResponsibilityFlags(true); 1030 ObjLinkingLayer->setAutoClaimResponsibilityForObjectSymbols(true); 1031 } 1032 1033 // FIXME: Explicit conversion to std::unique_ptr<ObjectLayer> added to silence 1034 // errors from some GCC / libstdc++ bots. Remove this conversion (i.e. 1035 // just return ObjLinkingLayer) once those bots are upgraded. 1036 return std::unique_ptr<ObjectLayer>(std::move(ObjLinkingLayer)); 1037 } 1038 1039 Expected<std::unique_ptr<IRCompileLayer::IRCompiler>> 1040 LLJIT::createCompileFunction(LLJITBuilderState &S, 1041 JITTargetMachineBuilder JTMB) { 1042 1043 /// If there is a custom compile function creator set then use it. 1044 if (S.CreateCompileFunction) 1045 return S.CreateCompileFunction(std::move(JTMB)); 1046 1047 // Otherwise default to creating a SimpleCompiler, or ConcurrentIRCompiler, 1048 // depending on the number of threads requested. 1049 if (S.NumCompileThreads > 0) 1050 return std::make_unique<ConcurrentIRCompiler>(std::move(JTMB)); 1051 1052 auto TM = JTMB.createTargetMachine(); 1053 if (!TM) 1054 return TM.takeError(); 1055 1056 return std::make_unique<TMOwningSimpleCompiler>(std::move(*TM)); 1057 } 1058 1059 LLJIT::LLJIT(LLJITBuilderState &S, Error &Err) 1060 : ES(S.ES ? std::move(S.ES) : std::make_unique<ExecutionSession>()), Main(), 1061 DL(""), TT(S.JTMB->getTargetTriple()) { 1062 1063 ErrorAsOutParameter _(&Err); 1064 1065 if (auto MainOrErr = this->ES->createJITDylib("main")) 1066 Main = &*MainOrErr; 1067 else { 1068 Err = MainOrErr.takeError(); 1069 return; 1070 } 1071 1072 if (S.DL) 1073 DL = std::move(*S.DL); 1074 else if (auto DLOrErr = S.JTMB->getDefaultDataLayoutForTarget()) 1075 DL = std::move(*DLOrErr); 1076 else { 1077 Err = DLOrErr.takeError(); 1078 return; 1079 } 1080 1081 auto ObjLayer = createObjectLinkingLayer(S, *ES); 1082 if (!ObjLayer) { 1083 Err = ObjLayer.takeError(); 1084 return; 1085 } 1086 ObjLinkingLayer = std::move(*ObjLayer); 1087 ObjTransformLayer = 1088 std::make_unique<ObjectTransformLayer>(*ES, *ObjLinkingLayer); 1089 1090 { 1091 auto CompileFunction = createCompileFunction(S, std::move(*S.JTMB)); 1092 if (!CompileFunction) { 1093 Err = CompileFunction.takeError(); 1094 return; 1095 } 1096 CompileLayer = std::make_unique<IRCompileLayer>( 1097 *ES, *ObjTransformLayer, std::move(*CompileFunction)); 1098 TransformLayer = std::make_unique<IRTransformLayer>(*ES, *CompileLayer); 1099 InitHelperTransformLayer = 1100 std::make_unique<IRTransformLayer>(*ES, *TransformLayer); 1101 } 1102 1103 if (S.NumCompileThreads > 0) { 1104 InitHelperTransformLayer->setCloneToNewContextOnEmit(true); 1105 CompileThreads = 1106 std::make_unique<ThreadPool>(hardware_concurrency(S.NumCompileThreads)); 1107 ES->setDispatchMaterialization( 1108 [this](std::unique_ptr<MaterializationUnit> MU, 1109 std::unique_ptr<MaterializationResponsibility> MR) { 1110 // FIXME: We should be able to use move-capture here, but ThreadPool's 1111 // AsyncTaskTys are std::functions rather than unique_functions 1112 // (because MSVC's std::packaged_tasks don't support move-only types). 1113 // Fix this when all the above gets sorted out. 1114 CompileThreads->async( 1115 [UnownedMU = MU.release(), UnownedMR = MR.release()]() mutable { 1116 std::unique_ptr<MaterializationUnit> MU(UnownedMU); 1117 std::unique_ptr<MaterializationResponsibility> MR(UnownedMR); 1118 MU->materialize(std::move(MR)); 1119 }); 1120 }); 1121 } 1122 1123 if (S.SetUpPlatform) 1124 Err = S.SetUpPlatform(*this); 1125 else 1126 setUpGenericLLVMIRPlatform(*this); 1127 } 1128 1129 std::string LLJIT::mangle(StringRef UnmangledName) const { 1130 std::string MangledName; 1131 { 1132 raw_string_ostream MangledNameStream(MangledName); 1133 Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL); 1134 } 1135 return MangledName; 1136 } 1137 1138 Error LLJIT::applyDataLayout(Module &M) { 1139 if (M.getDataLayout().isDefault()) 1140 M.setDataLayout(DL); 1141 1142 if (M.getDataLayout() != DL) 1143 return make_error<StringError>( 1144 "Added modules have incompatible data layouts: " + 1145 M.getDataLayout().getStringRepresentation() + " (module) vs " + 1146 DL.getStringRepresentation() + " (jit)", 1147 inconvertibleErrorCode()); 1148 1149 return Error::success(); 1150 } 1151 1152 void setUpGenericLLVMIRPlatform(LLJIT &J) { 1153 LLVM_DEBUG( 1154 { dbgs() << "Setting up GenericLLVMIRPlatform support for LLJIT\n"; }); 1155 J.setPlatformSupport(std::make_unique<GenericLLVMIRPlatformSupport>(J)); 1156 } 1157 1158 Error setUpMachOPlatform(LLJIT &J) { 1159 LLVM_DEBUG({ dbgs() << "Setting up MachOPlatform support for LLJIT\n"; }); 1160 auto MP = MachOPlatformSupport::Create(J, J.getMainJITDylib()); 1161 if (!MP) 1162 return MP.takeError(); 1163 J.setPlatformSupport(std::move(*MP)); 1164 return Error::success(); 1165 } 1166 1167 Error LLLazyJITBuilderState::prepareForConstruction() { 1168 if (auto Err = LLJITBuilderState::prepareForConstruction()) 1169 return Err; 1170 TT = JTMB->getTargetTriple(); 1171 return Error::success(); 1172 } 1173 1174 Error LLLazyJIT::addLazyIRModule(JITDylib &JD, ThreadSafeModule TSM) { 1175 assert(TSM && "Can not add null module"); 1176 1177 if (auto Err = TSM.withModuleDo( 1178 [&](Module &M) -> Error { return applyDataLayout(M); })) 1179 return Err; 1180 1181 return CODLayer->add(JD, std::move(TSM)); 1182 } 1183 1184 LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) { 1185 1186 // If LLJIT construction failed then bail out. 1187 if (Err) 1188 return; 1189 1190 ErrorAsOutParameter _(&Err); 1191 1192 /// Take/Create the lazy-compile callthrough manager. 1193 if (S.LCTMgr) 1194 LCTMgr = std::move(S.LCTMgr); 1195 else { 1196 if (auto LCTMgrOrErr = createLocalLazyCallThroughManager( 1197 S.TT, *ES, S.LazyCompileFailureAddr)) 1198 LCTMgr = std::move(*LCTMgrOrErr); 1199 else { 1200 Err = LCTMgrOrErr.takeError(); 1201 return; 1202 } 1203 } 1204 1205 // Take/Create the indirect stubs manager builder. 1206 auto ISMBuilder = std::move(S.ISMBuilder); 1207 1208 // If none was provided, try to build one. 1209 if (!ISMBuilder) 1210 ISMBuilder = createLocalIndirectStubsManagerBuilder(S.TT); 1211 1212 // No luck. Bail out. 1213 if (!ISMBuilder) { 1214 Err = make_error<StringError>("Could not construct " 1215 "IndirectStubsManagerBuilder for target " + 1216 S.TT.str(), 1217 inconvertibleErrorCode()); 1218 return; 1219 } 1220 1221 // Create the COD layer. 1222 CODLayer = std::make_unique<CompileOnDemandLayer>( 1223 *ES, *InitHelperTransformLayer, *LCTMgr, std::move(ISMBuilder)); 1224 1225 if (S.NumCompileThreads > 0) 1226 CODLayer->setCloneToNewContextOnEmit(true); 1227 } 1228 1229 } // End namespace orc. 1230 } // End namespace llvm. 1231