1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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"
72 #if !defined(_MSC_VER) && !defined(__MINGW32__)
79 #include <cygwin/version.h>
80 #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
81 #define DO_NOTHING_ATEXIT 1
87 static codegen::RegisterCodeGenFlags CGF
;
89 #define DEBUG_TYPE "lli"
93 enum class JITKind
{ MCJIT
, Orc
, OrcLazy
};
94 enum class JITLinkerKind
{ Default
, RuntimeDyld
, JITLink
};
97 InputFile(cl::desc("<input bitcode>"), cl::Positional
, cl::init("-"));
100 InputArgv(cl::ConsumeAfter
, cl::desc("<program arguments>..."));
102 cl::opt
<bool> ForceInterpreter("force-interpreter",
103 cl::desc("Force interpretation: disable JIT"),
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.")));
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",
121 clEnumValN(JITLinkerKind::JITLink
, "jitlink",
122 "Orc-specific linker")));
125 LazyJITCompileThreads("compile-threads",
126 cl::desc("Choose the number of compile threads "
127 "(jit-kind=orc-lazy only)"),
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)"));
135 cl::opt
<bool> PerModuleLazy(
137 cl::desc("Performs lazy compilation on whole module boundaries "
138 "rather than individual functions"),
141 cl::list
<std::string
>
143 cl::desc("Specifies the JITDylib to be used for any subsequent "
144 "-extra-module arguments."));
146 cl::list
<std::string
>
147 Dylibs("dlopen", cl::desc("Dynamic libraries to load before linking"),
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."),
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.
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(""));
168 // Determine optimization level.
171 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
172 "(default = '-O2')"),
178 TargetTriple("mtriple", cl::desc("Override target triple for module"));
181 EntryFunc("entry-function",
182 cl::desc("Specify the entry function (default = 'main') "
183 "of the executable"),
184 cl::value_desc("function"),
187 cl::list
<std::string
>
188 ExtraModules("extra-module",
189 cl::desc("Extra modules to be loaded"),
190 cl::value_desc("input bitcode"));
192 cl::list
<std::string
>
193 ExtraObjects("extra-object",
194 cl::desc("Extra object files to be loaded"),
195 cl::value_desc("input object"));
197 cl::list
<std::string
>
198 ExtraArchives("extra-archive",
199 cl::desc("Extra archive files to be loaded"),
200 cl::value_desc("input archive"));
203 EnableCacheManager("enable-cache-manager",
204 cl::desc("Use cache manager to save/load modules"),
208 ObjectCacheDir("object-cache-dir",
209 cl::desc("Directory to store cached object files "
210 "(must be user writable)"),
214 FakeArgv0("fake-argv0",
215 cl::desc("Override the 'argv[0]' value passed into the executing"
216 " program"), cl::value_desc("executable"));
219 DisableCoreFiles("disable-core-files", cl::Hidden
,
220 cl::desc("Disable emission of core files if possible"));
223 NoLazyCompilation("disable-lazy-compilation",
224 cl::desc("Disable JIT lazy compilation"),
228 GenerateSoftFloatCalls("soft-float",
229 cl::desc("Generate software floating point library calls"),
232 cl::opt
<bool> NoProcessSymbols(
234 cl::desc("Do not resolve lli process symbols in JIT'd code"),
237 enum class LLJITPlatform
{ Inactive
, DetectHost
, GenericIR
};
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")),
250 enum class DumpKind
{
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).")),
272 cl::list
<BuiltinFunctionKind
> GenerateBuiltinFunctions(
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")),
285 ExitOnError ExitOnErr
;
288 LLVM_ATTRIBUTE_USED
void linkComponents() {
289 errs() << (void *)&llvm_orc_registerEHFrameSectionWrapper
290 << (void *)&llvm_orc_deregisterEHFrameSectionWrapper
291 << (void *)&llvm_orc_registerJITLoaderGDBWrapper
;
294 //===----------------------------------------------------------------------===//
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.
303 class LLIObjectCache
: public ObjectCache
{
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
+= '/';
311 ~LLIObjectCache() override
{}
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
))
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
));
324 raw_fd_ostream
outfile(CacheName
, EC
, sys::fs::OF_None
);
325 outfile
.write(Obj
.getBufferStart(), Obj
.getBufferSize());
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
))
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.
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
345 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer
.get()->getBuffer());
349 std::string CacheDir
;
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
)
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] = '/';
365 CacheName
= CacheDir
+ CacheSubdir
;
366 size_t pos
= CacheName
.rfind('.');
367 CacheName
.replace(pos
, CacheName
.length() - pos
, ".o");
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
);
382 // Create a new module.
383 std::unique_ptr
<Module
> M
= std::make_unique
<Module
>("CygMingHelper", Context
);
384 M
->setTargetTriple(TargetTripleStr
);
386 // Create an empty function named "__main".
388 if (TargetTriple
.isArch64Bit())
389 ReturnTy
= Type::getInt64Ty(Context
);
391 ReturnTy
= Type::getInt32Ty(Context
);
393 Function::Create(FunctionType::get(ReturnTy
, {}, false),
394 GlobalValue::ExternalLinkage
, "__main", M
.get());
396 BasicBlock
*BB
= BasicBlock::Create(Context
, "__main", Result
);
397 Builder
.SetInsertPoint(BB
);
398 Value
*ReturnVal
= ConstantInt::get(ReturnTy
, 0);
399 Builder
.CreateRet(ReturnVal
);
401 // Add this new module to the ExecutionEngine.
402 EE
.addModule(std::move(M
));
405 CodeGenOpt::Level
getOptLevel() {
408 WithColor::error(errs(), "lli") << "invalid optimization level.\n";
410 case '0': return CodeGenOpt::None
;
411 case '1': return CodeGenOpt::Less
;
413 case '2': return CodeGenOpt::Default
;
414 case '3': return CodeGenOpt::Aggressive
;
416 llvm_unreachable("Unrecognized opt level.");
419 [[noreturn
]] static void reportError(SMDiagnostic Err
, const char *ProgName
) {
420 Err
.print(ProgName
, errs());
425 int runOrcJIT(const char *ProgName
);
426 void disallowOrcOptions();
427 Expected
<std::unique_ptr
<orc::ExecutorProcessControl
>> launchRemote();
429 //===----------------------------------------------------------------------===//
430 // main Driver function
432 int main(int argc
, char **argv
, char * const *envp
) {
433 InitLLVM
X(argc
, argv
);
436 ExitOnErr
.setBanner(std::string(argv
[0]) + ": ");
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();
444 cl::ParseCommandLineOptions(argc
, argv
,
445 "llvm interpreter & dynamic compiler\n");
447 // If the user doesn't want core files, disable them.
448 if (DisableCoreFiles
)
449 sys::Process::PreventCoreFiles();
451 ExitOnErr(loadDylibs());
453 if (UseJITKind
== JITKind::MCJIT
)
454 disallowOrcOptions();
456 return runOrcJIT(argv
[0]);
458 // Old lli implementation based on ExecutionEngine and MCJIT.
461 // Load the bitcode...
463 std::unique_ptr
<Module
> Owner
= parseIRFile(InputFile
, Err
, Context
);
464 Module
*Mod
= Owner
.get();
466 reportError(Err
, argv
[0]);
468 if (EnableCacheManager
) {
469 std::string
CacheName("file:");
470 CacheName
.append(InputFile
);
471 Mod
->setModuleIdentifier(CacheName
);
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());
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
496 // If we are supposed to override the target triple, do so now.
497 if (!TargetTriple
.empty())
498 Mod
->setTargetTriple(Triple::normalize(TargetTriple
));
500 // Enable MCJIT if desired.
501 RTDyldMemoryManager
*RTDyldMM
= nullptr;
502 if (!ForceInterpreter
) {
504 RTDyldMM
= new ForwardingMemoryManager();
506 RTDyldMM
= new SectionMemoryManager();
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";
518 builder
.setOptLevel(getOptLevel());
520 TargetOptions Options
=
521 codegen::InitTargetOptionsFromCodeGenFlags(Triple(TargetTriple
));
522 if (codegen::getFloatABIForCalls() != FloatABI::Default
)
523 Options
.FloatABIType
= codegen::getFloatABIForCalls();
525 builder
.setTargetOptions(Options
);
527 std::unique_ptr
<ExecutionEngine
> EE(builder
.create());
529 if (!ErrorMsg
.empty())
530 WithColor::error(errs(), argv
[0])
531 << "error creating EE: " << ErrorMsg
<< "\n";
533 WithColor::error(errs(), argv
[0]) << "unknown error creating EE!\n";
537 std::unique_ptr
<LLIObjectCache
> CacheManager
;
538 if (EnableCacheManager
) {
539 CacheManager
.reset(new LLIObjectCache(ObjectCacheDir
));
540 EE
->setObjectCache(CacheManager
.get());
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
);
547 reportError(Err
, argv
[0]);
548 if (EnableCacheManager
) {
549 std::string
CacheName("file:");
550 CacheName
.append(ExtraModules
[i
]);
551 XMod
->setModuleIdentifier(CacheName
);
553 EE
->addModule(std::move(XMod
));
556 for (unsigned i
= 0, e
= ExtraObjects
.size(); i
!= e
; ++i
) {
557 Expected
<object::OwningBinary
<object::ObjectFile
>> Obj
=
558 object::ObjectFile::createObjectFile(ExtraObjects
[i
]);
560 // TODO: Actually report errors helpfully.
561 consumeError(Obj
.takeError());
562 reportError(Err
, argv
[0]);
564 object::OwningBinary
<object::ObjectFile
> &O
= Obj
.get();
565 EE
->addObjectFile(std::move(O
));
568 for (unsigned i
= 0, e
= ExtraArchives
.size(); i
!= e
; ++i
) {
569 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> ArBufOrErr
=
570 MemoryBuffer::getFileOrSTDIN(ExtraArchives
[i
]);
572 reportError(Err
, argv
[0]);
573 std::unique_ptr
<MemoryBuffer
> &ArBuf
= ArBufOrErr
.get();
575 Expected
<std::unique_ptr
<object::Archive
>> ArOrErr
=
576 object::Archive::create(ArBuf
->getMemBufferRef());
579 raw_string_ostream
OS(Buf
);
580 logAllUnhandledErrors(ArOrErr
.takeError(), OS
);
585 std::unique_ptr
<object::Archive
> &Ar
= ArOrErr
.get();
587 object::OwningBinary
<object::Archive
> OB(std::move(Ar
), std::move(ArBuf
));
589 EE
->addArchive(std::move(OB
));
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());
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());
605 EE
->RegisterJITEventListener(
606 JITEventListener::createPerfJITEventListener());
608 if (!NoLazyCompilation
&& RemoteMCJIT
) {
609 WithColor::warning(errs(), argv
[0])
610 << "remote mcjit does not support lazy compilation\n";
611 NoLazyCompilation
= true;
613 EE
->DisableLazyCompilation(NoLazyCompilation
);
615 // If the user specifically requested an argv[0] to pass into the program,
617 if (!FakeArgv0
.empty()) {
618 InputFile
= static_cast<std::string
>(FakeArgv0
);
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);
626 // Add the module's name to the start of the vector of arguments to main().
627 InputArgv
.insert(InputArgv
.begin(), InputFile
);
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.
634 Function
*EntryFn
= Mod
->getFunction(EntryFunc
);
636 WithColor::error(errs(), argv
[0])
637 << '\'' << EntryFunc
<< "\' function not found in module.\n";
641 // Reset errno to zero on entry to main.
646 // Sanity check use of remote-jit: LLI currently only supports use of the
647 // remote JIT on Unix platforms.
650 WithColor::warning(errs(), argv
[0])
651 << "host does not support external remote targets.\n";
652 WithColor::note() << "defaulting to local execution\n";
655 if (ChildExecPath
.empty()) {
656 WithColor::error(errs(), argv
[0])
657 << "-remote-mcjit requires -mcjit-remote-process.\n";
659 } else if (!sys::fs::can_execute(ChildExecPath
)) {
660 WithColor::error(errs(), argv
[0])
661 << "unable to find usable child executable: '" << ChildExecPath
668 std::unique_ptr
<orc::ExecutorProcessControl
> EPC
=
669 RemoteMCJIT
? ExitOnErr(launchRemote())
670 : ExitOnErr(orc::SelfExecutorProcessControl::Create());
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
));
678 // Run static constructors.
679 if (!ForceInterpreter
) {
680 // Give MCJIT a chance to apply relocations and set page permissions.
681 EE
->finalizeObject();
683 EE
->runStaticConstructorsDestructors(false);
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.
690 static_cast<SectionMemoryManager
*>(RTDyldMM
)->invalidateInstructionCache();
693 Result
= EE
->runFunctionAsMain(EntryFn
, InputArgv
, envp
);
695 // Run static destructors.
696 EE
->runStaticConstructorsDestructors(true);
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";
713 WithColor::error(errs(), argv
[0]) << "exit defined with wrong prototype!\n";
716 // else == "if (RemoteMCJIT)"
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.
722 // Create a remote memory manager.
723 auto RemoteMM
= ExitOnErr(
724 orc::EPCGenericRTDyldMemoryManager::CreateWithDefaultBootstrapSymbols(
727 // Forward MCJIT's memory manager calls to the remote memory manager.
728 static_cast<ForwardingMemoryManager
*>(RTDyldMM
)->setMemMgr(
729 std::move(RemoteMM
));
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
736 // FIXME: argv and envp handling.
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
, {}));
744 // Like static constructors, the remote target MCJIT support doesn't handle
745 // this yet. It could. FIXME.
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.
752 // Signal the remote target that we're done JITing.
753 ExitOnErr(EPC
->disconnect());
759 static std::function
<void(Module
&)> createDebugDumper() {
760 switch (OrcDumpKind
) {
761 case DumpKind::NoDump
:
762 return [](Module
&M
) {};
764 case DumpKind::DumpFuncsToStdOut
:
765 return [](Module
&M
) {
768 for (const auto &F
: M
) {
769 if (F
.isDeclaration())
773 std::string
Name(std::string(F
.getName()));
774 printf("%s ", Name
.c_str());
782 case DumpKind::DumpModsToStdOut
:
783 return [](Module
&M
) {
784 outs() << "----- Module Start -----\n" << M
<< "----- Module End -----\n";
787 case DumpKind::DumpModsToDisk
:
788 return [](Module
&M
) {
790 raw_fd_ostream
Out(M
.getModuleIdentifier() + ".ll", EC
,
791 sys::fs::OF_TextWithCRLF
);
793 errs() << "Couldn't open " << M
.getModuleIdentifier()
794 << " for dumping.\nError:" << EC
.message() << "\n";
800 llvm_unreachable("Unknown DumpKind");
804 for (const auto &Dylib
: Dylibs
) {
806 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib
.c_str(), &ErrMsg
))
807 return make_error
<StringError
>(ErrMsg
, inconvertibleErrorCode());
810 return Error::success();
813 static void exitOnLazyCallThroughFailure() { exit(1); }
815 Expected
<orc::ThreadSafeModule
>
816 loadModule(StringRef Path
, orc::ThreadSafeContext TSCtx
) {
818 auto M
= parseIRFile(Path
, Err
, *TSCtx
.getContext());
822 raw_string_ostream
ErrMsgStream(ErrMsg
);
823 Err
.print("lli", ErrMsgStream
);
825 return make_error
<StringError
>(std::move(ErrMsg
), inconvertibleErrorCode());
828 if (EnableCacheManager
)
829 M
->setModuleIdentifier("file:" + M
->getModuleIdentifier());
831 return orc::ThreadSafeModule(std::move(M
), std::move(TSCtx
));
834 int runOrcJIT(const char *ProgName
) {
835 // Start setting up the JIT environment.
837 // Parse the main module.
838 orc::ThreadSafeContext
TSCtx(std::make_unique
<LLVMContext
>());
839 auto MainModule
= ExitOnErr(loadModule(InputFile
, TSCtx
));
841 // Get TargetTriple and DataLayout from the main module if they're explicitly
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();
852 orc::LLLazyJITBuilder Builder
;
854 Builder
.setJITTargetMachineBuilder(
855 TT
? orc::JITTargetMachineBuilder(*TT
)
856 : ExitOnErr(orc::JITTargetMachineBuilder::detectHost()));
858 TT
= Builder
.getJITTargetMachineBuilder()->getTargetTriple();
860 Builder
.setDataLayout(DL
);
862 if (!codegen::getMArch().empty())
863 Builder
.getJITTargetMachineBuilder()->getTargetTriple().setArchName(
864 codegen::getMArch());
866 Builder
.getJITTargetMachineBuilder()
867 ->setCPU(codegen::getCPUStr())
868 .addFeatures(codegen::getFeatureList())
869 .setRelocationModel(codegen::getExplicitRelocModel())
870 .setCodeModel(codegen::getExplicitCodeModel());
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
));
883 Builder
.setLazyCompileFailureAddr(
884 pointerToJITTargetAddress(exitOnLazyCallThroughFailure
));
885 Builder
.setNumCompileThreads(LazyJITCompileThreads
);
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
) {
892 CacheManager
= std::make_unique
<LLIObjectCache
>(ObjectCacheDir
);
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
),
901 auto TM
= JTMB
.createTargetMachine();
903 return TM
.takeError();
905 return std::make_unique
<orc::TMOwningSimpleCompiler
>(std::move(*TM
),
910 // Set up LLJIT platform.
912 LLJITPlatform P
= Platform
;
913 if (P
== LLJITPlatform::DetectHost
)
914 P
= LLJITPlatform::GenericIR
;
917 case LLJITPlatform::GenericIR
:
918 // Nothing to do: LLJITBuilder will use this by default.
920 case LLJITPlatform::Inactive
:
921 Builder
.setPlatformSetUp(orc::setUpInactivePlatform
);
924 llvm_unreachable("Unrecognized platform value");
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
>()));
933 Builder
.setObjectLinkingLayerCreator([&EPC
](orc::ExecutionSession
&ES
,
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
))));
944 auto J
= ExitOnErr(Builder
.create());
946 auto *ObjLayer
= &J
->getObjLinkingLayer();
947 if (auto *RTDyldObjLayer
= dyn_cast
<orc::RTDyldObjectLinkingLayer
>(ObjLayer
))
948 RTDyldObjLayer
->registerJITEventListener(
949 *JITEventListener::createGDBRegistrationListener());
952 J
->setPartitionFunction(orc::CompileOnDemandLayer::compileWholeModule
);
954 auto Dump
= createDebugDumper();
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";
969 orc::MangleAndInterner
Mangle(J
->getExecutionSession(), J
->getDataLayout());
971 // Unless they've been explicitly disabled, make process symbols available to
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
;
981 if (GenerateBuiltinFunctions
.size() > 0)
982 J
->getMainJITDylib().addGenerator(
983 std::make_unique
<LLIBuiltinFunctionGenerator
>(GenerateBuiltinFunctions
,
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
));
995 // Add the main module.
996 ExitOnErr(AddModule(J
->getMainJITDylib(), std::move(MainModule
)));
998 // Create JITDylibs and add any extra modules.
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
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
);
1009 JD
= &ExitOnErr(J
->createJITDylib(*JDItr
));
1010 J
->getMainJITDylib().addToLinkOrder(*JD
);
1011 JD
->addToLinkOrder(J
->getMainJITDylib());
1013 IdxToDylib
[JITDylibs
.getPosition(JDItr
- JITDylibs
.begin())] = JD
;
1016 for (auto EMItr
= ExtraModules
.begin(), EMEnd
= ExtraModules
.end();
1017 EMItr
!= EMEnd
; ++EMItr
) {
1018 auto M
= ExitOnErr(loadModule(*EMItr
, TSCtx
));
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
)));
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
)));
1039 for (auto &ObjPath
: ExtraObjects
) {
1040 auto Obj
= ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath
)));
1041 ExitOnErr(J
->addObjectFile(std::move(Obj
)));
1044 // Run any static constructors.
1045 ExitOnErr(J
->initialize(J
->getMainJITDylib()));
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
)();
1053 reinterpret_cast<EntryPointPtr
>(static_cast<uintptr_t>(EntryPointSym
.getAddress()));
1054 AltEntryThreads
.push_back(std::thread([EntryPoint
]() { EntryPoint(); }));
1057 // Resolve and run the main function.
1058 JITEvaluatedSymbol MainSym
= ExitOnErr(J
->lookup(EntryFunc
));
1062 // ExecutorProcessControl-based execution with JITLink.
1064 EPC
->runAsMain(orc::ExecutorAddr(MainSym
.getAddress()), InputArgv
));
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
));
1072 // Wait for -entry-point threads.
1073 for (auto &AltEntryThread
: AltEntryThreads
)
1074 AltEntryThread
.join();
1077 ExitOnErr(J
->deinitialize(J
->getMainJITDylib()));
1082 void disallowOrcOptions() {
1083 // Make sure nobody used an orc-lazy specific option accidentally.
1085 if (LazyJITCompileThreads
!= 0) {
1086 errs() << "-compile-threads requires -jit-kind=orc-lazy\n";
1090 if (!ThreadEntryPoints
.empty()) {
1091 errs() << "-thread-entry requires -jit-kind=orc-lazy\n";
1095 if (PerModuleLazy
) {
1096 errs() << "-per-module-lazy requires -jit-kind=orc-lazy\n";
1101 Expected
<std::unique_ptr
<orc::ExecutorProcessControl
>> launchRemote() {
1102 #ifndef LLVM_ON_UNIX
1103 llvm_unreachable("launchRemote not supported on non-Unix platforms");
1108 // Create two pipes.
1109 if (pipe(PipeFD
[0]) != 0 || pipe(PipeFD
[1]) != 0)
1110 perror("Error creating pipe: ");
1114 if (ChildPID
== 0) {
1117 // Close the parent ends of the pipes
1118 close(PipeFD
[0][1]);
1119 close(PipeFD
[1][0]);
1122 // Execute the child process.
1123 std::unique_ptr
<char[]> ChildPath
, ChildIn
, ChildOut
;
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';
1138 char * const args
[] = { &ChildPath
[0], &ChildIn
[0], &ChildOut
[0], nullptr };
1139 int rc
= execv(ChildExecPath
.c_str(), args
);
1141 perror("Error executing child process: ");
1142 llvm_unreachable("Error executing child process");
1144 // else we're the parent...
1146 // Close the child ends of the pipes
1147 close(PipeFD
[0][0]);
1148 close(PipeFD
[1][1]);
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]);