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