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