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