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