1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===// 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 // This utility provides a simple wrapper around the LLVM Execution Engines, 10 // which allow the direct execution of LLVM programs through a Just-In-Time 11 // compiler, or through an interpreter if no JIT is available for this platform. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "RemoteJITUtils.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/ADT/Triple.h" 18 #include "llvm/Bitcode/BitcodeReader.h" 19 #include "llvm/CodeGen/CommandFlags.inc" 20 #include "llvm/CodeGen/LinkAllCodegenComponents.h" 21 #include "llvm/Config/llvm-config.h" 22 #include "llvm/ExecutionEngine/GenericValue.h" 23 #include "llvm/ExecutionEngine/Interpreter.h" 24 #include "llvm/ExecutionEngine/JITEventListener.h" 25 #include "llvm/ExecutionEngine/MCJIT.h" 26 #include "llvm/ExecutionEngine/ObjectCache.h" 27 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" 28 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" 29 #include "llvm/ExecutionEngine/Orc/LLJIT.h" 30 #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h" 31 #include "llvm/ExecutionEngine/OrcMCJITReplacement.h" 32 #include "llvm/ExecutionEngine/SectionMemoryManager.h" 33 #include "llvm/IR/IRBuilder.h" 34 #include "llvm/IR/LLVMContext.h" 35 #include "llvm/IR/Module.h" 36 #include "llvm/IR/Type.h" 37 #include "llvm/IR/Verifier.h" 38 #include "llvm/IRReader/IRReader.h" 39 #include "llvm/Object/Archive.h" 40 #include "llvm/Object/ObjectFile.h" 41 #include "llvm/Support/CommandLine.h" 42 #include "llvm/Support/Debug.h" 43 #include "llvm/Support/DynamicLibrary.h" 44 #include "llvm/Support/Format.h" 45 #include "llvm/Support/InitLLVM.h" 46 #include "llvm/Support/ManagedStatic.h" 47 #include "llvm/Support/MathExtras.h" 48 #include "llvm/Support/Memory.h" 49 #include "llvm/Support/MemoryBuffer.h" 50 #include "llvm/Support/Path.h" 51 #include "llvm/Support/PluginLoader.h" 52 #include "llvm/Support/Process.h" 53 #include "llvm/Support/Program.h" 54 #include "llvm/Support/SourceMgr.h" 55 #include "llvm/Support/TargetSelect.h" 56 #include "llvm/Support/WithColor.h" 57 #include "llvm/Support/raw_ostream.h" 58 #include "llvm/Transforms/Instrumentation.h" 59 #include <cerrno> 60 61 #ifdef __CYGWIN__ 62 #include <cygwin/version.h> 63 #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007 64 #define DO_NOTHING_ATEXIT 1 65 #endif 66 #endif 67 68 using namespace llvm; 69 70 #define DEBUG_TYPE "lli" 71 72 namespace { 73 74 enum class JITKind { MCJIT, OrcMCJITReplacement, OrcLazy }; 75 76 cl::opt<std::string> 77 InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-")); 78 79 cl::list<std::string> 80 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>...")); 81 82 cl::opt<bool> ForceInterpreter("force-interpreter", 83 cl::desc("Force interpretation: disable JIT"), 84 cl::init(false)); 85 86 cl::opt<JITKind> UseJITKind("jit-kind", 87 cl::desc("Choose underlying JIT kind."), 88 cl::init(JITKind::MCJIT), 89 cl::values( 90 clEnumValN(JITKind::MCJIT, "mcjit", 91 "MCJIT"), 92 clEnumValN(JITKind::OrcMCJITReplacement, 93 "orc-mcjit", 94 "Orc-based MCJIT replacement"), 95 clEnumValN(JITKind::OrcLazy, 96 "orc-lazy", 97 "Orc-based lazy JIT."))); 98 99 cl::opt<unsigned> 100 LazyJITCompileThreads("compile-threads", 101 cl::desc("Choose the number of compile threads " 102 "(jit-kind=orc-lazy only)"), 103 cl::init(0)); 104 105 cl::list<std::string> 106 ThreadEntryPoints("thread-entry", 107 cl::desc("calls the given entry-point on a new thread " 108 "(jit-kind=orc-lazy only)")); 109 110 cl::opt<bool> PerModuleLazy( 111 "per-module-lazy", 112 cl::desc("Performs lazy compilation on whole module boundaries " 113 "rather than individual functions"), 114 cl::init(false)); 115 116 cl::list<std::string> 117 JITDylibs("jd", 118 cl::desc("Specifies the JITDylib to be used for any subsequent " 119 "-extra-module arguments.")); 120 121 // The MCJIT supports building for a target address space separate from 122 // the JIT compilation process. Use a forked process and a copying 123 // memory manager with IPC to execute using this functionality. 124 cl::opt<bool> RemoteMCJIT("remote-mcjit", 125 cl::desc("Execute MCJIT'ed code in a separate process."), 126 cl::init(false)); 127 128 // Manually specify the child process for remote execution. This overrides 129 // the simulated remote execution that allocates address space for child 130 // execution. The child process will be executed and will communicate with 131 // lli via stdin/stdout pipes. 132 cl::opt<std::string> 133 ChildExecPath("mcjit-remote-process", 134 cl::desc("Specify the filename of the process to launch " 135 "for remote MCJIT execution. If none is specified," 136 "\n\tremote execution will be simulated in-process."), 137 cl::value_desc("filename"), cl::init("")); 138 139 // Determine optimization level. 140 cl::opt<char> 141 OptLevel("O", 142 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " 143 "(default = '-O2')"), 144 cl::Prefix, 145 cl::ZeroOrMore, 146 cl::init(' ')); 147 148 cl::opt<std::string> 149 TargetTriple("mtriple", cl::desc("Override target triple for module")); 150 151 cl::opt<std::string> 152 EntryFunc("entry-function", 153 cl::desc("Specify the entry function (default = 'main') " 154 "of the executable"), 155 cl::value_desc("function"), 156 cl::init("main")); 157 158 cl::list<std::string> 159 ExtraModules("extra-module", 160 cl::desc("Extra modules to be loaded"), 161 cl::value_desc("input bitcode")); 162 163 cl::list<std::string> 164 ExtraObjects("extra-object", 165 cl::desc("Extra object files to be loaded"), 166 cl::value_desc("input object")); 167 168 cl::list<std::string> 169 ExtraArchives("extra-archive", 170 cl::desc("Extra archive files to be loaded"), 171 cl::value_desc("input archive")); 172 173 cl::opt<bool> 174 EnableCacheManager("enable-cache-manager", 175 cl::desc("Use cache manager to save/load modules"), 176 cl::init(false)); 177 178 cl::opt<std::string> 179 ObjectCacheDir("object-cache-dir", 180 cl::desc("Directory to store cached object files " 181 "(must be user writable)"), 182 cl::init("")); 183 184 cl::opt<std::string> 185 FakeArgv0("fake-argv0", 186 cl::desc("Override the 'argv[0]' value passed into the executing" 187 " program"), cl::value_desc("executable")); 188 189 cl::opt<bool> 190 DisableCoreFiles("disable-core-files", cl::Hidden, 191 cl::desc("Disable emission of core files if possible")); 192 193 cl::opt<bool> 194 NoLazyCompilation("disable-lazy-compilation", 195 cl::desc("Disable JIT lazy compilation"), 196 cl::init(false)); 197 198 cl::opt<bool> 199 GenerateSoftFloatCalls("soft-float", 200 cl::desc("Generate software floating point library calls"), 201 cl::init(false)); 202 203 enum class DumpKind { 204 NoDump, 205 DumpFuncsToStdOut, 206 DumpModsToStdOut, 207 DumpModsToDisk 208 }; 209 210 cl::opt<DumpKind> OrcDumpKind( 211 "orc-lazy-debug", cl::desc("Debug dumping for the orc-lazy JIT."), 212 cl::init(DumpKind::NoDump), 213 cl::values(clEnumValN(DumpKind::NoDump, "no-dump", 214 "Don't dump anything."), 215 clEnumValN(DumpKind::DumpFuncsToStdOut, "funcs-to-stdout", 216 "Dump function names to stdout."), 217 clEnumValN(DumpKind::DumpModsToStdOut, "mods-to-stdout", 218 "Dump modules to stdout."), 219 clEnumValN(DumpKind::DumpModsToDisk, "mods-to-disk", 220 "Dump modules to the current " 221 "working directory. (WARNING: " 222 "will overwrite existing files).")), 223 cl::Hidden); 224 225 ExitOnError ExitOnErr; 226 } 227 228 //===----------------------------------------------------------------------===// 229 // Object cache 230 // 231 // This object cache implementation writes cached objects to disk to the 232 // directory specified by CacheDir, using a filename provided in the module 233 // descriptor. The cache tries to load a saved object using that path if the 234 // file exists. CacheDir defaults to "", in which case objects are cached 235 // alongside their originating bitcodes. 236 // 237 class LLIObjectCache : public ObjectCache { 238 public: 239 LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) { 240 // Add trailing '/' to cache dir if necessary. 241 if (!this->CacheDir.empty() && 242 this->CacheDir[this->CacheDir.size() - 1] != '/') 243 this->CacheDir += '/'; 244 } 245 ~LLIObjectCache() override {} 246 247 void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override { 248 const std::string &ModuleID = M->getModuleIdentifier(); 249 std::string CacheName; 250 if (!getCacheFilename(ModuleID, CacheName)) 251 return; 252 if (!CacheDir.empty()) { // Create user-defined cache dir. 253 SmallString<128> dir(sys::path::parent_path(CacheName)); 254 sys::fs::create_directories(Twine(dir)); 255 } 256 std::error_code EC; 257 raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None); 258 outfile.write(Obj.getBufferStart(), Obj.getBufferSize()); 259 outfile.close(); 260 } 261 262 std::unique_ptr<MemoryBuffer> getObject(const Module* M) override { 263 const std::string &ModuleID = M->getModuleIdentifier(); 264 std::string CacheName; 265 if (!getCacheFilename(ModuleID, CacheName)) 266 return nullptr; 267 // Load the object from the cache filename 268 ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer = 269 MemoryBuffer::getFile(CacheName, -1, false); 270 // If the file isn't there, that's OK. 271 if (!IRObjectBuffer) 272 return nullptr; 273 // MCJIT will want to write into this buffer, and we don't want that 274 // because the file has probably just been mmapped. Instead we make 275 // a copy. The filed-based buffer will be released when it goes 276 // out of scope. 277 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer()); 278 } 279 280 private: 281 std::string CacheDir; 282 283 bool getCacheFilename(const std::string &ModID, std::string &CacheName) { 284 std::string Prefix("file:"); 285 size_t PrefixLength = Prefix.length(); 286 if (ModID.substr(0, PrefixLength) != Prefix) 287 return false; 288 std::string CacheSubdir = ModID.substr(PrefixLength); 289 #if defined(_WIN32) 290 // Transform "X:\foo" => "/X\foo" for convenience. 291 if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') { 292 CacheSubdir[1] = CacheSubdir[0]; 293 CacheSubdir[0] = '/'; 294 } 295 #endif 296 CacheName = CacheDir + CacheSubdir; 297 size_t pos = CacheName.rfind('.'); 298 CacheName.replace(pos, CacheName.length() - pos, ".o"); 299 return true; 300 } 301 }; 302 303 // On Mingw and Cygwin, an external symbol named '__main' is called from the 304 // generated 'main' function to allow static initialization. To avoid linking 305 // problems with remote targets (because lli's remote target support does not 306 // currently handle external linking) we add a secondary module which defines 307 // an empty '__main' function. 308 static void addCygMingExtraModule(ExecutionEngine &EE, LLVMContext &Context, 309 StringRef TargetTripleStr) { 310 IRBuilder<> Builder(Context); 311 Triple TargetTriple(TargetTripleStr); 312 313 // Create a new module. 314 std::unique_ptr<Module> M = make_unique<Module>("CygMingHelper", Context); 315 M->setTargetTriple(TargetTripleStr); 316 317 // Create an empty function named "__main". 318 Type *ReturnTy; 319 if (TargetTriple.isArch64Bit()) 320 ReturnTy = Type::getInt64Ty(Context); 321 else 322 ReturnTy = Type::getInt32Ty(Context); 323 Function *Result = 324 Function::Create(FunctionType::get(ReturnTy, {}, false), 325 GlobalValue::ExternalLinkage, "__main", M.get()); 326 327 BasicBlock *BB = BasicBlock::Create(Context, "__main", Result); 328 Builder.SetInsertPoint(BB); 329 Value *ReturnVal = ConstantInt::get(ReturnTy, 0); 330 Builder.CreateRet(ReturnVal); 331 332 // Add this new module to the ExecutionEngine. 333 EE.addModule(std::move(M)); 334 } 335 336 CodeGenOpt::Level getOptLevel() { 337 switch (OptLevel) { 338 default: 339 WithColor::error(errs(), "lli") << "invalid optimization level.\n"; 340 exit(1); 341 case '0': return CodeGenOpt::None; 342 case '1': return CodeGenOpt::Less; 343 case ' ': 344 case '2': return CodeGenOpt::Default; 345 case '3': return CodeGenOpt::Aggressive; 346 } 347 llvm_unreachable("Unrecognized opt level."); 348 } 349 350 LLVM_ATTRIBUTE_NORETURN 351 static void reportError(SMDiagnostic Err, const char *ProgName) { 352 Err.print(ProgName, errs()); 353 exit(1); 354 } 355 356 int runOrcLazyJIT(const char *ProgName); 357 void disallowOrcOptions(); 358 359 //===----------------------------------------------------------------------===// 360 // main Driver function 361 // 362 int main(int argc, char **argv, char * const *envp) { 363 InitLLVM X(argc, argv); 364 365 if (argc > 1) 366 ExitOnErr.setBanner(std::string(argv[0]) + ": "); 367 368 // If we have a native target, initialize it to ensure it is linked in and 369 // usable by the JIT. 370 InitializeNativeTarget(); 371 InitializeNativeTargetAsmPrinter(); 372 InitializeNativeTargetAsmParser(); 373 374 cl::ParseCommandLineOptions(argc, argv, 375 "llvm interpreter & dynamic compiler\n"); 376 377 // If the user doesn't want core files, disable them. 378 if (DisableCoreFiles) 379 sys::Process::PreventCoreFiles(); 380 381 if (UseJITKind == JITKind::OrcLazy) 382 return runOrcLazyJIT(argv[0]); 383 else 384 disallowOrcOptions(); 385 386 LLVMContext Context; 387 388 // Load the bitcode... 389 SMDiagnostic Err; 390 std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context); 391 Module *Mod = Owner.get(); 392 if (!Mod) 393 reportError(Err, argv[0]); 394 395 if (EnableCacheManager) { 396 std::string CacheName("file:"); 397 CacheName.append(InputFile); 398 Mod->setModuleIdentifier(CacheName); 399 } 400 401 // If not jitting lazily, load the whole bitcode file eagerly too. 402 if (NoLazyCompilation) { 403 // Use *argv instead of argv[0] to work around a wrong GCC warning. 404 ExitOnError ExitOnErr(std::string(*argv) + 405 ": bitcode didn't read correctly: "); 406 ExitOnErr(Mod->materializeAll()); 407 } 408 409 std::string ErrorMsg; 410 EngineBuilder builder(std::move(Owner)); 411 builder.setMArch(MArch); 412 builder.setMCPU(getCPUStr()); 413 builder.setMAttrs(getFeatureList()); 414 if (RelocModel.getNumOccurrences()) 415 builder.setRelocationModel(RelocModel); 416 if (CMModel.getNumOccurrences()) 417 builder.setCodeModel(CMModel); 418 builder.setErrorStr(&ErrorMsg); 419 builder.setEngineKind(ForceInterpreter 420 ? EngineKind::Interpreter 421 : EngineKind::JIT); 422 builder.setUseOrcMCJITReplacement(UseJITKind == JITKind::OrcMCJITReplacement); 423 424 // If we are supposed to override the target triple, do so now. 425 if (!TargetTriple.empty()) 426 Mod->setTargetTriple(Triple::normalize(TargetTriple)); 427 428 // Enable MCJIT if desired. 429 RTDyldMemoryManager *RTDyldMM = nullptr; 430 if (!ForceInterpreter) { 431 if (RemoteMCJIT) 432 RTDyldMM = new ForwardingMemoryManager(); 433 else 434 RTDyldMM = new SectionMemoryManager(); 435 436 // Deliberately construct a temp std::unique_ptr to pass in. Do not null out 437 // RTDyldMM: We still use it below, even though we don't own it. 438 builder.setMCJITMemoryManager( 439 std::unique_ptr<RTDyldMemoryManager>(RTDyldMM)); 440 } else if (RemoteMCJIT) { 441 WithColor::error(errs(), argv[0]) 442 << "remote process execution does not work with the interpreter.\n"; 443 exit(1); 444 } 445 446 builder.setOptLevel(getOptLevel()); 447 448 TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); 449 if (FloatABIForCalls != FloatABI::Default) 450 Options.FloatABIType = FloatABIForCalls; 451 452 builder.setTargetOptions(Options); 453 454 std::unique_ptr<ExecutionEngine> EE(builder.create()); 455 if (!EE) { 456 if (!ErrorMsg.empty()) 457 WithColor::error(errs(), argv[0]) 458 << "error creating EE: " << ErrorMsg << "\n"; 459 else 460 WithColor::error(errs(), argv[0]) << "unknown error creating EE!\n"; 461 exit(1); 462 } 463 464 std::unique_ptr<LLIObjectCache> CacheManager; 465 if (EnableCacheManager) { 466 CacheManager.reset(new LLIObjectCache(ObjectCacheDir)); 467 EE->setObjectCache(CacheManager.get()); 468 } 469 470 // Load any additional modules specified on the command line. 471 for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) { 472 std::unique_ptr<Module> XMod = parseIRFile(ExtraModules[i], Err, Context); 473 if (!XMod) 474 reportError(Err, argv[0]); 475 if (EnableCacheManager) { 476 std::string CacheName("file:"); 477 CacheName.append(ExtraModules[i]); 478 XMod->setModuleIdentifier(CacheName); 479 } 480 EE->addModule(std::move(XMod)); 481 } 482 483 for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) { 484 Expected<object::OwningBinary<object::ObjectFile>> Obj = 485 object::ObjectFile::createObjectFile(ExtraObjects[i]); 486 if (!Obj) { 487 // TODO: Actually report errors helpfully. 488 consumeError(Obj.takeError()); 489 reportError(Err, argv[0]); 490 } 491 object::OwningBinary<object::ObjectFile> &O = Obj.get(); 492 EE->addObjectFile(std::move(O)); 493 } 494 495 for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) { 496 ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr = 497 MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]); 498 if (!ArBufOrErr) 499 reportError(Err, argv[0]); 500 std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get(); 501 502 Expected<std::unique_ptr<object::Archive>> ArOrErr = 503 object::Archive::create(ArBuf->getMemBufferRef()); 504 if (!ArOrErr) { 505 std::string Buf; 506 raw_string_ostream OS(Buf); 507 logAllUnhandledErrors(ArOrErr.takeError(), OS); 508 OS.flush(); 509 errs() << Buf; 510 exit(1); 511 } 512 std::unique_ptr<object::Archive> &Ar = ArOrErr.get(); 513 514 object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf)); 515 516 EE->addArchive(std::move(OB)); 517 } 518 519 // If the target is Cygwin/MingW and we are generating remote code, we 520 // need an extra module to help out with linking. 521 if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) { 522 addCygMingExtraModule(*EE, Context, Mod->getTargetTriple()); 523 } 524 525 // The following functions have no effect if their respective profiling 526 // support wasn't enabled in the build configuration. 527 EE->RegisterJITEventListener( 528 JITEventListener::createOProfileJITEventListener()); 529 EE->RegisterJITEventListener( 530 JITEventListener::createIntelJITEventListener()); 531 if (!RemoteMCJIT) 532 EE->RegisterJITEventListener( 533 JITEventListener::createPerfJITEventListener()); 534 535 if (!NoLazyCompilation && RemoteMCJIT) { 536 WithColor::warning(errs(), argv[0]) 537 << "remote mcjit does not support lazy compilation\n"; 538 NoLazyCompilation = true; 539 } 540 EE->DisableLazyCompilation(NoLazyCompilation); 541 542 // If the user specifically requested an argv[0] to pass into the program, 543 // do it now. 544 if (!FakeArgv0.empty()) { 545 InputFile = static_cast<std::string>(FakeArgv0); 546 } else { 547 // Otherwise, if there is a .bc suffix on the executable strip it off, it 548 // might confuse the program. 549 if (StringRef(InputFile).endswith(".bc")) 550 InputFile.erase(InputFile.length() - 3); 551 } 552 553 // Add the module's name to the start of the vector of arguments to main(). 554 InputArgv.insert(InputArgv.begin(), InputFile); 555 556 // Call the main function from M as if its signature were: 557 // int main (int argc, char **argv, const char **envp) 558 // using the contents of Args to determine argc & argv, and the contents of 559 // EnvVars to determine envp. 560 // 561 Function *EntryFn = Mod->getFunction(EntryFunc); 562 if (!EntryFn) { 563 WithColor::error(errs(), argv[0]) 564 << '\'' << EntryFunc << "\' function not found in module.\n"; 565 return -1; 566 } 567 568 // Reset errno to zero on entry to main. 569 errno = 0; 570 571 int Result = -1; 572 573 // Sanity check use of remote-jit: LLI currently only supports use of the 574 // remote JIT on Unix platforms. 575 if (RemoteMCJIT) { 576 #ifndef LLVM_ON_UNIX 577 WithColor::warning(errs(), argv[0]) 578 << "host does not support external remote targets.\n"; 579 WithColor::note() << "defaulting to local execution\n"; 580 return -1; 581 #else 582 if (ChildExecPath.empty()) { 583 WithColor::error(errs(), argv[0]) 584 << "-remote-mcjit requires -mcjit-remote-process.\n"; 585 exit(1); 586 } else if (!sys::fs::can_execute(ChildExecPath)) { 587 WithColor::error(errs(), argv[0]) 588 << "unable to find usable child executable: '" << ChildExecPath 589 << "'\n"; 590 return -1; 591 } 592 #endif 593 } 594 595 if (!RemoteMCJIT) { 596 // If the program doesn't explicitly call exit, we will need the Exit 597 // function later on to make an explicit call, so get the function now. 598 FunctionCallee Exit = Mod->getOrInsertFunction( 599 "exit", Type::getVoidTy(Context), Type::getInt32Ty(Context)); 600 601 // Run static constructors. 602 if (!ForceInterpreter) { 603 // Give MCJIT a chance to apply relocations and set page permissions. 604 EE->finalizeObject(); 605 } 606 EE->runStaticConstructorsDestructors(false); 607 608 // Trigger compilation separately so code regions that need to be 609 // invalidated will be known. 610 (void)EE->getPointerToFunction(EntryFn); 611 // Clear instruction cache before code will be executed. 612 if (RTDyldMM) 613 static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache(); 614 615 // Run main. 616 Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp); 617 618 // Run static destructors. 619 EE->runStaticConstructorsDestructors(true); 620 621 // If the program didn't call exit explicitly, we should call it now. 622 // This ensures that any atexit handlers get called correctly. 623 if (Function *ExitF = 624 dyn_cast<Function>(Exit.getCallee()->stripPointerCasts())) { 625 if (ExitF->getFunctionType() == Exit.getFunctionType()) { 626 std::vector<GenericValue> Args; 627 GenericValue ResultGV; 628 ResultGV.IntVal = APInt(32, Result); 629 Args.push_back(ResultGV); 630 EE->runFunction(ExitF, Args); 631 WithColor::error(errs(), argv[0]) 632 << "exit(" << Result << ") returned!\n"; 633 abort(); 634 } 635 } 636 WithColor::error(errs(), argv[0]) << "exit defined with wrong prototype!\n"; 637 abort(); 638 } else { 639 // else == "if (RemoteMCJIT)" 640 641 // Remote target MCJIT doesn't (yet) support static constructors. No reason 642 // it couldn't. This is a limitation of the LLI implementation, not the 643 // MCJIT itself. FIXME. 644 645 // Lanch the remote process and get a channel to it. 646 std::unique_ptr<FDRawChannel> C = launchRemote(); 647 if (!C) { 648 WithColor::error(errs(), argv[0]) << "failed to launch remote JIT.\n"; 649 exit(1); 650 } 651 652 // Create a remote target client running over the channel. 653 llvm::orc::ExecutionSession ES; 654 ES.setErrorReporter([&](Error Err) { ExitOnErr(std::move(Err)); }); 655 typedef orc::remote::OrcRemoteTargetClient MyRemote; 656 auto R = ExitOnErr(MyRemote::Create(*C, ES)); 657 658 // Create a remote memory manager. 659 auto RemoteMM = ExitOnErr(R->createRemoteMemoryManager()); 660 661 // Forward MCJIT's memory manager calls to the remote memory manager. 662 static_cast<ForwardingMemoryManager*>(RTDyldMM)->setMemMgr( 663 std::move(RemoteMM)); 664 665 // Forward MCJIT's symbol resolution calls to the remote. 666 static_cast<ForwardingMemoryManager *>(RTDyldMM)->setResolver( 667 orc::createLambdaResolver( 668 [](const std::string &Name) { return nullptr; }, 669 [&](const std::string &Name) { 670 if (auto Addr = ExitOnErr(R->getSymbolAddress(Name))) 671 return JITSymbol(Addr, JITSymbolFlags::Exported); 672 return JITSymbol(nullptr); 673 })); 674 675 // Grab the target address of the JIT'd main function on the remote and call 676 // it. 677 // FIXME: argv and envp handling. 678 JITTargetAddress Entry = EE->getFunctionAddress(EntryFn->getName().str()); 679 EE->finalizeObject(); 680 LLVM_DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x" 681 << format("%llx", Entry) << "\n"); 682 Result = ExitOnErr(R->callIntVoid(Entry)); 683 684 // Like static constructors, the remote target MCJIT support doesn't handle 685 // this yet. It could. FIXME. 686 687 // Delete the EE - we need to tear it down *before* we terminate the session 688 // with the remote, otherwise it'll crash when it tries to release resources 689 // on a remote that has already been disconnected. 690 EE.reset(); 691 692 // Signal the remote target that we're done JITing. 693 ExitOnErr(R->terminateSession()); 694 } 695 696 return Result; 697 } 698 699 static orc::IRTransformLayer::TransformFunction createDebugDumper() { 700 switch (OrcDumpKind) { 701 case DumpKind::NoDump: 702 return [](orc::ThreadSafeModule TSM, 703 const orc::MaterializationResponsibility &R) { return TSM; }; 704 705 case DumpKind::DumpFuncsToStdOut: 706 return [](orc::ThreadSafeModule TSM, 707 const orc::MaterializationResponsibility &R) { 708 printf("[ "); 709 710 for (const auto &F : *TSM.getModule()) { 711 if (F.isDeclaration()) 712 continue; 713 714 if (F.hasName()) { 715 std::string Name(F.getName()); 716 printf("%s ", Name.c_str()); 717 } else 718 printf("<anon> "); 719 } 720 721 printf("]\n"); 722 return TSM; 723 }; 724 725 case DumpKind::DumpModsToStdOut: 726 return [](orc::ThreadSafeModule TSM, 727 const orc::MaterializationResponsibility &R) { 728 outs() << "----- Module Start -----\n" 729 << *TSM.getModule() << "----- Module End -----\n"; 730 731 return TSM; 732 }; 733 734 case DumpKind::DumpModsToDisk: 735 return [](orc::ThreadSafeModule TSM, 736 const orc::MaterializationResponsibility &R) { 737 std::error_code EC; 738 raw_fd_ostream Out(TSM.getModule()->getModuleIdentifier() + ".ll", EC, 739 sys::fs::F_Text); 740 if (EC) { 741 errs() << "Couldn't open " << TSM.getModule()->getModuleIdentifier() 742 << " for dumping.\nError:" << EC.message() << "\n"; 743 exit(1); 744 } 745 Out << *TSM.getModule(); 746 return TSM; 747 }; 748 } 749 llvm_unreachable("Unknown DumpKind"); 750 } 751 752 static void exitOnLazyCallThroughFailure() { exit(1); } 753 754 int runOrcLazyJIT(const char *ProgName) { 755 // Start setting up the JIT environment. 756 757 // Parse the main module. 758 orc::ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>()); 759 SMDiagnostic Err; 760 auto MainModule = orc::ThreadSafeModule( 761 parseIRFile(InputFile, Err, *TSCtx.getContext()), TSCtx); 762 if (!MainModule) 763 reportError(Err, ProgName); 764 765 const auto &TT = MainModule.getModule()->getTargetTriple(); 766 orc::LLLazyJITBuilder Builder; 767 768 Builder.setJITTargetMachineBuilder( 769 TT.empty() ? ExitOnErr(orc::JITTargetMachineBuilder::detectHost()) 770 : orc::JITTargetMachineBuilder(Triple(TT))); 771 772 if (!MArch.empty()) 773 Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(MArch); 774 775 Builder.getJITTargetMachineBuilder() 776 ->setCPU(getCPUStr()) 777 .addFeatures(getFeatureList()) 778 .setRelocationModel(RelocModel.getNumOccurrences() 779 ? Optional<Reloc::Model>(RelocModel) 780 : None) 781 .setCodeModel(CMModel.getNumOccurrences() 782 ? Optional<CodeModel::Model>(CMModel) 783 : None); 784 785 Builder.setLazyCompileFailureAddr( 786 pointerToJITTargetAddress(exitOnLazyCallThroughFailure)); 787 Builder.setNumCompileThreads(LazyJITCompileThreads); 788 789 auto J = ExitOnErr(Builder.create()); 790 791 if (PerModuleLazy) 792 J->setPartitionFunction(orc::CompileOnDemandLayer::compileWholeModule); 793 794 auto Dump = createDebugDumper(); 795 796 J->setLazyCompileTransform([&](orc::ThreadSafeModule TSM, 797 const orc::MaterializationResponsibility &R) { 798 if (verifyModule(*TSM.getModule(), &dbgs())) { 799 dbgs() << "Bad module: " << *TSM.getModule() << "\n"; 800 exit(1); 801 } 802 return Dump(std::move(TSM), R); 803 }); 804 J->getMainJITDylib().setGenerator( 805 ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess( 806 J->getDataLayout().getGlobalPrefix()))); 807 808 orc::MangleAndInterner Mangle(J->getExecutionSession(), J->getDataLayout()); 809 orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides; 810 ExitOnErr(CXXRuntimeOverrides.enable(J->getMainJITDylib(), Mangle)); 811 812 // Add the main module. 813 ExitOnErr(J->addLazyIRModule(std::move(MainModule))); 814 815 // Create JITDylibs and add any extra modules. 816 { 817 // Create JITDylibs, keep a map from argument index to dylib. We will use 818 // -extra-module argument indexes to determine what dylib to use for each 819 // -extra-module. 820 std::map<unsigned, orc::JITDylib *> IdxToDylib; 821 IdxToDylib[0] = &J->getMainJITDylib(); 822 for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end(); 823 JDItr != JDEnd; ++JDItr) { 824 orc::JITDylib *JD = J->getJITDylibByName(*JDItr); 825 if (!JD) 826 JD = &J->createJITDylib(*JDItr); 827 IdxToDylib[JITDylibs.getPosition(JDItr - JITDylibs.begin())] = JD; 828 } 829 830 for (auto EMItr = ExtraModules.begin(), EMEnd = ExtraModules.end(); 831 EMItr != EMEnd; ++EMItr) { 832 auto M = parseIRFile(*EMItr, Err, *TSCtx.getContext()); 833 if (!M) 834 reportError(Err, ProgName); 835 836 auto EMIdx = ExtraModules.getPosition(EMItr - ExtraModules.begin()); 837 assert(EMIdx != 0 && "ExtraModule should have index > 0"); 838 auto JDItr = std::prev(IdxToDylib.lower_bound(EMIdx)); 839 auto &JD = *JDItr->second; 840 ExitOnErr( 841 J->addLazyIRModule(JD, orc::ThreadSafeModule(std::move(M), TSCtx))); 842 } 843 } 844 845 // Add the objects. 846 for (auto &ObjPath : ExtraObjects) { 847 auto Obj = ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath))); 848 ExitOnErr(J->addObjectFile(std::move(Obj))); 849 } 850 851 // Generate a argument string. 852 std::vector<std::string> Args; 853 Args.push_back(InputFile); 854 for (auto &Arg : InputArgv) 855 Args.push_back(Arg); 856 857 // Run any static constructors. 858 ExitOnErr(J->runConstructors()); 859 860 // Run any -thread-entry points. 861 std::vector<std::thread> AltEntryThreads; 862 for (auto &ThreadEntryPoint : ThreadEntryPoints) { 863 auto EntryPointSym = ExitOnErr(J->lookup(ThreadEntryPoint)); 864 typedef void (*EntryPointPtr)(); 865 auto EntryPoint = 866 reinterpret_cast<EntryPointPtr>(static_cast<uintptr_t>(EntryPointSym.getAddress())); 867 AltEntryThreads.push_back(std::thread([EntryPoint]() { EntryPoint(); })); 868 } 869 870 // Run main. 871 auto MainSym = ExitOnErr(J->lookup("main")); 872 typedef int (*MainFnPtr)(int, const char *[]); 873 std::vector<const char *> ArgV; 874 for (auto &Arg : Args) 875 ArgV.push_back(Arg.c_str()); 876 ArgV.push_back(nullptr); 877 878 int ArgC = ArgV.size() - 1; 879 auto Main = 880 reinterpret_cast<MainFnPtr>(static_cast<uintptr_t>(MainSym.getAddress())); 881 auto Result = Main(ArgC, (const char **)ArgV.data()); 882 883 // Wait for -entry-point threads. 884 for (auto &AltEntryThread : AltEntryThreads) 885 AltEntryThread.join(); 886 887 // Run destructors. 888 ExitOnErr(J->runDestructors()); 889 CXXRuntimeOverrides.runDestructors(); 890 891 return Result; 892 } 893 894 void disallowOrcOptions() { 895 // Make sure nobody used an orc-lazy specific option accidentally. 896 897 if (LazyJITCompileThreads != 0) { 898 errs() << "-compile-threads requires -jit-kind=orc-lazy\n"; 899 exit(1); 900 } 901 902 if (!ThreadEntryPoints.empty()) { 903 errs() << "-thread-entry requires -jit-kind=orc-lazy\n"; 904 exit(1); 905 } 906 907 if (PerModuleLazy) { 908 errs() << "-per-module-lazy requires -jit-kind=orc-lazy\n"; 909 exit(1); 910 } 911 } 912 913 std::unique_ptr<FDRawChannel> launchRemote() { 914 #ifndef LLVM_ON_UNIX 915 llvm_unreachable("launchRemote not supported on non-Unix platforms"); 916 #else 917 int PipeFD[2][2]; 918 pid_t ChildPID; 919 920 // Create two pipes. 921 if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0) 922 perror("Error creating pipe: "); 923 924 ChildPID = fork(); 925 926 if (ChildPID == 0) { 927 // In the child... 928 929 // Close the parent ends of the pipes 930 close(PipeFD[0][1]); 931 close(PipeFD[1][0]); 932 933 934 // Execute the child process. 935 std::unique_ptr<char[]> ChildPath, ChildIn, ChildOut; 936 { 937 ChildPath.reset(new char[ChildExecPath.size() + 1]); 938 std::copy(ChildExecPath.begin(), ChildExecPath.end(), &ChildPath[0]); 939 ChildPath[ChildExecPath.size()] = '\0'; 940 std::string ChildInStr = utostr(PipeFD[0][0]); 941 ChildIn.reset(new char[ChildInStr.size() + 1]); 942 std::copy(ChildInStr.begin(), ChildInStr.end(), &ChildIn[0]); 943 ChildIn[ChildInStr.size()] = '\0'; 944 std::string ChildOutStr = utostr(PipeFD[1][1]); 945 ChildOut.reset(new char[ChildOutStr.size() + 1]); 946 std::copy(ChildOutStr.begin(), ChildOutStr.end(), &ChildOut[0]); 947 ChildOut[ChildOutStr.size()] = '\0'; 948 } 949 950 char * const args[] = { &ChildPath[0], &ChildIn[0], &ChildOut[0], nullptr }; 951 int rc = execv(ChildExecPath.c_str(), args); 952 if (rc != 0) 953 perror("Error executing child process: "); 954 llvm_unreachable("Error executing child process"); 955 } 956 // else we're the parent... 957 958 // Close the child ends of the pipes 959 close(PipeFD[0][0]); 960 close(PipeFD[1][1]); 961 962 // Return an RPC channel connected to our end of the pipes. 963 return llvm::make_unique<FDRawChannel>(PipeFD[1][0], PipeFD[0][1]); 964 #endif 965 } 966