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