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