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