1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This utility provides a simple wrapper around the LLVM Execution Engines,
10 // which allow the direct execution of LLVM programs through a Just-In-Time
11 // compiler, or through an interpreter if no JIT is available for this platform.
13 //===----------------------------------------------------------------------===//
15 #include "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/DebugUtils.h"
28 #include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
29 #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
30 #include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
31 #include "llvm/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.h"
32 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
33 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
34 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
35 #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
36 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
37 #include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
38 #include "llvm/ExecutionEngine/Orc/SymbolStringPool.h"
39 #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
40 #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
41 #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
42 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
43 #include "llvm/IR/IRBuilder.h"
44 #include "llvm/IR/LLVMContext.h"
45 #include "llvm/IR/Module.h"
46 #include "llvm/IR/Type.h"
47 #include "llvm/IR/Verifier.h"
48 #include "llvm/IRReader/IRReader.h"
49 #include "llvm/Object/Archive.h"
50 #include "llvm/Object/ObjectFile.h"
51 #include "llvm/Support/CommandLine.h"
52 #include "llvm/Support/Debug.h"
53 #include "llvm/Support/DynamicLibrary.h"
54 #include "llvm/Support/Format.h"
55 #include "llvm/Support/InitLLVM.h"
56 #include "llvm/Support/MathExtras.h"
57 #include "llvm/Support/Memory.h"
58 #include "llvm/Support/MemoryBuffer.h"
59 #include "llvm/Support/Path.h"
60 #include "llvm/Support/PluginLoader.h"
61 #include "llvm/Support/Process.h"
62 #include "llvm/Support/Program.h"
63 #include "llvm/Support/SourceMgr.h"
64 #include "llvm/Support/TargetSelect.h"
65 #include "llvm/Support/ToolOutputFile.h"
66 #include "llvm/Support/WithColor.h"
67 #include "llvm/Support/raw_ostream.h"
68 #include "llvm/TargetParser/Triple.h"
69 #include "llvm/Transforms/Instrumentation.h"
73 #if !defined(_MSC_VER) && !defined(__MINGW32__)
80 #include <cygwin/version.h>
81 #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
82 #define DO_NOTHING_ATEXIT 1
88 static codegen::RegisterCodeGenFlags CGF
;
90 #define DEBUG_TYPE "lli"
94 enum class JITKind
{ MCJIT
, Orc
, OrcLazy
};
95 enum class JITLinkerKind
{ Default
, RuntimeDyld
, JITLink
};
98 InputFile(cl::desc("<input bitcode>"), cl::Positional
, cl::init("-"));
100 cl::list
<std::string
>
101 InputArgv(cl::ConsumeAfter
, cl::desc("<program arguments>..."));
103 cl::opt
<bool> ForceInterpreter("force-interpreter",
104 cl::desc("Force interpretation: disable JIT"),
107 cl::opt
<JITKind
> UseJITKind(
108 "jit-kind", cl::desc("Choose underlying JIT kind."),
109 cl::init(JITKind::Orc
),
110 cl::values(clEnumValN(JITKind::MCJIT
, "mcjit", "MCJIT"),
111 clEnumValN(JITKind::Orc
, "orc", "Orc JIT"),
112 clEnumValN(JITKind::OrcLazy
, "orc-lazy",
113 "Orc-based lazy JIT.")));
115 cl::opt
<JITLinkerKind
>
116 JITLinker("jit-linker", cl::desc("Choose the dynamic linker/loader."),
117 cl::init(JITLinkerKind::Default
),
118 cl::values(clEnumValN(JITLinkerKind::Default
, "default",
119 "Default for platform and JIT-kind"),
120 clEnumValN(JITLinkerKind::RuntimeDyld
, "rtdyld",
122 clEnumValN(JITLinkerKind::JITLink
, "jitlink",
123 "Orc-specific linker")));
124 cl::opt
<std::string
> OrcRuntime("orc-runtime",
125 cl::desc("Use ORC runtime from given path"),
129 LazyJITCompileThreads("compile-threads",
130 cl::desc("Choose the number of compile threads "
131 "(jit-kind=orc-lazy only)"),
134 cl::list
<std::string
>
135 ThreadEntryPoints("thread-entry",
136 cl::desc("calls the given entry-point on a new thread "
137 "(jit-kind=orc-lazy only)"));
139 cl::opt
<bool> PerModuleLazy(
141 cl::desc("Performs lazy compilation on whole module boundaries "
142 "rather than individual functions"),
145 cl::list
<std::string
>
147 cl::desc("Specifies the JITDylib to be used for any subsequent "
148 "-extra-module arguments."));
150 cl::list
<std::string
>
151 Dylibs("dlopen", cl::desc("Dynamic libraries to load before linking"));
153 // The MCJIT supports building for a target address space separate from
154 // the JIT compilation process. Use a forked process and a copying
155 // memory manager with IPC to execute using this functionality.
156 cl::opt
<bool> RemoteMCJIT("remote-mcjit",
157 cl::desc("Execute MCJIT'ed code in a separate process."),
160 // Manually specify the child process for remote execution. This overrides
161 // the simulated remote execution that allocates address space for child
162 // execution. The child process will be executed and will communicate with
163 // lli via stdin/stdout pipes.
165 ChildExecPath("mcjit-remote-process",
166 cl::desc("Specify the filename of the process to launch "
167 "for remote MCJIT execution. If none is specified,"
168 "\n\tremote execution will be simulated in-process."),
169 cl::value_desc("filename"), cl::init(""));
171 // Determine optimization level.
172 cl::opt
<char> OptLevel("O",
173 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
174 "(default = '-O2')"),
175 cl::Prefix
, cl::init('2'));
178 TargetTriple("mtriple", cl::desc("Override target triple for module"));
181 EntryFunc("entry-function",
182 cl::desc("Specify the entry function (default = 'main') "
183 "of the executable"),
184 cl::value_desc("function"),
187 cl::list
<std::string
>
188 ExtraModules("extra-module",
189 cl::desc("Extra modules to be loaded"),
190 cl::value_desc("input bitcode"));
192 cl::list
<std::string
>
193 ExtraObjects("extra-object",
194 cl::desc("Extra object files to be loaded"),
195 cl::value_desc("input object"));
197 cl::list
<std::string
>
198 ExtraArchives("extra-archive",
199 cl::desc("Extra archive files to be loaded"),
200 cl::value_desc("input archive"));
203 EnableCacheManager("enable-cache-manager",
204 cl::desc("Use cache manager to save/load modules"),
208 ObjectCacheDir("object-cache-dir",
209 cl::desc("Directory to store cached object files "
210 "(must be user writable)"),
214 FakeArgv0("fake-argv0",
215 cl::desc("Override the 'argv[0]' value passed into the executing"
216 " program"), cl::value_desc("executable"));
219 DisableCoreFiles("disable-core-files", cl::Hidden
,
220 cl::desc("Disable emission of core files if possible"));
223 NoLazyCompilation("disable-lazy-compilation",
224 cl::desc("Disable JIT lazy compilation"),
228 GenerateSoftFloatCalls("soft-float",
229 cl::desc("Generate software floating point library calls"),
232 cl::opt
<bool> NoProcessSymbols(
234 cl::desc("Do not resolve lli process symbols in JIT'd code"),
237 enum class LLJITPlatform
{ Inactive
, Auto
, ExecutorNative
, GenericIR
};
239 cl::opt
<LLJITPlatform
> Platform(
240 "lljit-platform", cl::desc("Platform to use with LLJIT"),
241 cl::init(LLJITPlatform::Auto
),
242 cl::values(clEnumValN(LLJITPlatform::Auto
, "Auto",
243 "Like 'ExecutorNative' if ORC runtime "
244 "provided, otherwise like 'GenericIR'"),
245 clEnumValN(LLJITPlatform::ExecutorNative
, "ExecutorNative",
246 "Use the native platform for the executor."
247 "Requires -orc-runtime"),
248 clEnumValN(LLJITPlatform::GenericIR
, "GenericIR",
249 "Use LLJITGenericIRPlatform"),
250 clEnumValN(LLJITPlatform::Inactive
, "Inactive",
251 "Disable platform support explicitly")),
254 enum class DumpKind
{
263 cl::opt
<DumpKind
> OrcDumpKind(
264 "orc-lazy-debug", cl::desc("Debug dumping for the orc-lazy JIT."),
265 cl::init(DumpKind::NoDump
),
267 clEnumValN(DumpKind::NoDump
, "no-dump", "Don't dump anything."),
268 clEnumValN(DumpKind::DumpFuncsToStdOut
, "funcs-to-stdout",
269 "Dump function names to stdout."),
270 clEnumValN(DumpKind::DumpModsToStdOut
, "mods-to-stdout",
271 "Dump modules to stdout."),
272 clEnumValN(DumpKind::DumpModsToDisk
, "mods-to-disk",
273 "Dump modules to the current "
274 "working directory. (WARNING: "
275 "will overwrite existing files)."),
276 clEnumValN(DumpKind::DumpDebugDescriptor
, "jit-debug-descriptor",
277 "Dump __jit_debug_descriptor contents to stdout"),
278 clEnumValN(DumpKind::DumpDebugObjects
, "jit-debug-objects",
279 "Dump __jit_debug_descriptor in-memory debug "
280 "objects as tool output")),
283 ExitOnError ExitOnErr
;
286 LLVM_ATTRIBUTE_USED
void linkComponents() {
287 errs() << (void *)&llvm_orc_registerEHFrameSectionWrapper
288 << (void *)&llvm_orc_deregisterEHFrameSectionWrapper
289 << (void *)&llvm_orc_registerJITLoaderGDBWrapper
290 << (void *)&llvm_orc_registerJITLoaderGDBAllocAction
;
293 //===----------------------------------------------------------------------===//
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.
302 class LLIObjectCache
: public ObjectCache
{
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
+= '/';
310 ~LLIObjectCache() override
{}
312 void notifyObjectCompiled(const Module
*M
, MemoryBufferRef Obj
) override
{
313 const std::string
&ModuleID
= M
->getModuleIdentifier();
314 std::string CacheName
;
315 if (!getCacheFilename(ModuleID
, CacheName
))
317 if (!CacheDir
.empty()) { // Create user-defined cache dir.
318 SmallString
<128> dir(sys::path::parent_path(CacheName
));
319 sys::fs::create_directories(Twine(dir
));
323 raw_fd_ostream
outfile(CacheName
, EC
, sys::fs::OF_None
);
324 outfile
.write(Obj
.getBufferStart(), Obj
.getBufferSize());
328 std::unique_ptr
<MemoryBuffer
> getObject(const Module
* M
) override
{
329 const std::string
&ModuleID
= M
->getModuleIdentifier();
330 std::string CacheName
;
331 if (!getCacheFilename(ModuleID
, CacheName
))
333 // Load the object from the cache filename
334 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> IRObjectBuffer
=
335 MemoryBuffer::getFile(CacheName
, /*IsText=*/false,
336 /*RequiresNullTerminator=*/false);
337 // If the file isn't there, that's OK.
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
344 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer
.get()->getBuffer());
348 std::string CacheDir
;
350 bool getCacheFilename(StringRef ModID
, std::string
&CacheName
) {
351 if (!ModID
.consume_front("file:"))
354 std::string CacheSubdir
= std::string(ModID
);
355 // Transform "X:\foo" => "/X\foo" for convenience on Windows.
356 if (is_style_windows(llvm::sys::path::Style::native
) &&
357 isalpha(CacheSubdir
[0]) && CacheSubdir
[1] == ':') {
358 CacheSubdir
[1] = CacheSubdir
[0];
359 CacheSubdir
[0] = '/';
362 CacheName
= CacheDir
+ CacheSubdir
;
363 size_t pos
= CacheName
.rfind('.');
364 CacheName
.replace(pos
, CacheName
.length() - pos
, ".o");
369 // On Mingw and Cygwin, an external symbol named '__main' is called from the
370 // generated 'main' function to allow static initialization. To avoid linking
371 // problems with remote targets (because lli's remote target support does not
372 // currently handle external linking) we add a secondary module which defines
373 // an empty '__main' function.
374 static void addCygMingExtraModule(ExecutionEngine
&EE
, LLVMContext
&Context
,
375 StringRef TargetTripleStr
) {
376 IRBuilder
<> Builder(Context
);
377 Triple
TargetTriple(TargetTripleStr
);
379 // Create a new module.
380 std::unique_ptr
<Module
> M
= std::make_unique
<Module
>("CygMingHelper", Context
);
381 M
->setTargetTriple(TargetTripleStr
);
383 // Create an empty function named "__main".
385 if (TargetTriple
.isArch64Bit())
386 ReturnTy
= Type::getInt64Ty(Context
);
388 ReturnTy
= Type::getInt32Ty(Context
);
390 Function::Create(FunctionType::get(ReturnTy
, {}, false),
391 GlobalValue::ExternalLinkage
, "__main", M
.get());
393 BasicBlock
*BB
= BasicBlock::Create(Context
, "__main", Result
);
394 Builder
.SetInsertPoint(BB
);
395 Value
*ReturnVal
= ConstantInt::get(ReturnTy
, 0);
396 Builder
.CreateRet(ReturnVal
);
398 // Add this new module to the ExecutionEngine.
399 EE
.addModule(std::move(M
));
402 CodeGenOptLevel
getOptLevel() {
403 if (auto Level
= CodeGenOpt::parseLevel(OptLevel
))
405 WithColor::error(errs(), "lli") << "invalid optimization level.\n";
409 [[noreturn
]] static void reportError(SMDiagnostic Err
, const char *ProgName
) {
410 Err
.print(ProgName
, errs());
415 int runOrcJIT(const char *ProgName
);
416 void disallowOrcOptions();
417 Expected
<std::unique_ptr
<orc::ExecutorProcessControl
>> launchRemote();
419 //===----------------------------------------------------------------------===//
420 // main Driver function
422 int main(int argc
, char **argv
, char * const *envp
) {
423 InitLLVM
X(argc
, argv
);
426 ExitOnErr
.setBanner(std::string(argv
[0]) + ": ");
428 // If we have a native target, initialize it to ensure it is linked in and
429 // usable by the JIT.
430 InitializeNativeTarget();
431 InitializeNativeTargetAsmPrinter();
432 InitializeNativeTargetAsmParser();
434 cl::ParseCommandLineOptions(argc
, argv
,
435 "llvm interpreter & dynamic compiler\n");
437 // If the user doesn't want core files, disable them.
438 if (DisableCoreFiles
)
439 sys::Process::PreventCoreFiles();
441 ExitOnErr(loadDylibs());
443 if (EntryFunc
.empty()) {
444 WithColor::error(errs(), argv
[0])
445 << "--entry-function name cannot be empty\n";
449 if (UseJITKind
== JITKind::MCJIT
|| ForceInterpreter
)
450 disallowOrcOptions();
452 return runOrcJIT(argv
[0]);
454 // Old lli implementation based on ExecutionEngine and MCJIT.
457 // Load the bitcode...
459 std::unique_ptr
<Module
> Owner
= parseIRFile(InputFile
, Err
, Context
);
460 Module
*Mod
= Owner
.get();
462 reportError(Err
, argv
[0]);
464 if (EnableCacheManager
) {
465 std::string
CacheName("file:");
466 CacheName
.append(InputFile
);
467 Mod
->setModuleIdentifier(CacheName
);
470 // If not jitting lazily, load the whole bitcode file eagerly too.
471 if (NoLazyCompilation
) {
472 // Use *argv instead of argv[0] to work around a wrong GCC warning.
473 ExitOnError
ExitOnErr(std::string(*argv
) +
474 ": bitcode didn't read correctly: ");
475 ExitOnErr(Mod
->materializeAll());
478 std::string ErrorMsg
;
479 EngineBuilder
builder(std::move(Owner
));
480 builder
.setMArch(codegen::getMArch());
481 builder
.setMCPU(codegen::getCPUStr());
482 builder
.setMAttrs(codegen::getFeatureList());
483 if (auto RM
= codegen::getExplicitRelocModel())
484 builder
.setRelocationModel(*RM
);
485 if (auto CM
= codegen::getExplicitCodeModel())
486 builder
.setCodeModel(*CM
);
487 builder
.setErrorStr(&ErrorMsg
);
488 builder
.setEngineKind(ForceInterpreter
489 ? EngineKind::Interpreter
492 // If we are supposed to override the target triple, do so now.
493 if (!TargetTriple
.empty())
494 Mod
->setTargetTriple(Triple::normalize(TargetTriple
));
496 // Enable MCJIT if desired.
497 RTDyldMemoryManager
*RTDyldMM
= nullptr;
498 if (!ForceInterpreter
) {
500 RTDyldMM
= new ForwardingMemoryManager();
502 RTDyldMM
= new SectionMemoryManager();
504 // Deliberately construct a temp std::unique_ptr to pass in. Do not null out
505 // RTDyldMM: We still use it below, even though we don't own it.
506 builder
.setMCJITMemoryManager(
507 std::unique_ptr
<RTDyldMemoryManager
>(RTDyldMM
));
508 } else if (RemoteMCJIT
) {
509 WithColor::error(errs(), argv
[0])
510 << "remote process execution does not work with the interpreter.\n";
514 builder
.setOptLevel(getOptLevel());
516 TargetOptions Options
=
517 codegen::InitTargetOptionsFromCodeGenFlags(Triple(TargetTriple
));
518 if (codegen::getFloatABIForCalls() != FloatABI::Default
)
519 Options
.FloatABIType
= codegen::getFloatABIForCalls();
521 builder
.setTargetOptions(Options
);
523 std::unique_ptr
<ExecutionEngine
> EE(builder
.create());
525 if (!ErrorMsg
.empty())
526 WithColor::error(errs(), argv
[0])
527 << "error creating EE: " << ErrorMsg
<< "\n";
529 WithColor::error(errs(), argv
[0]) << "unknown error creating EE!\n";
533 std::unique_ptr
<LLIObjectCache
> CacheManager
;
534 if (EnableCacheManager
) {
535 CacheManager
.reset(new LLIObjectCache(ObjectCacheDir
));
536 EE
->setObjectCache(CacheManager
.get());
539 // Load any additional modules specified on the command line.
540 for (unsigned i
= 0, e
= ExtraModules
.size(); i
!= e
; ++i
) {
541 std::unique_ptr
<Module
> XMod
= parseIRFile(ExtraModules
[i
], Err
, Context
);
543 reportError(Err
, argv
[0]);
544 if (EnableCacheManager
) {
545 std::string
CacheName("file:");
546 CacheName
.append(ExtraModules
[i
]);
547 XMod
->setModuleIdentifier(CacheName
);
549 EE
->addModule(std::move(XMod
));
552 for (unsigned i
= 0, e
= ExtraObjects
.size(); i
!= e
; ++i
) {
553 Expected
<object::OwningBinary
<object::ObjectFile
>> Obj
=
554 object::ObjectFile::createObjectFile(ExtraObjects
[i
]);
556 // TODO: Actually report errors helpfully.
557 consumeError(Obj
.takeError());
558 reportError(Err
, argv
[0]);
560 object::OwningBinary
<object::ObjectFile
> &O
= Obj
.get();
561 EE
->addObjectFile(std::move(O
));
564 for (unsigned i
= 0, e
= ExtraArchives
.size(); i
!= e
; ++i
) {
565 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> ArBufOrErr
=
566 MemoryBuffer::getFileOrSTDIN(ExtraArchives
[i
]);
568 reportError(Err
, argv
[0]);
569 std::unique_ptr
<MemoryBuffer
> &ArBuf
= ArBufOrErr
.get();
571 Expected
<std::unique_ptr
<object::Archive
>> ArOrErr
=
572 object::Archive::create(ArBuf
->getMemBufferRef());
575 raw_string_ostream
OS(Buf
);
576 logAllUnhandledErrors(ArOrErr
.takeError(), OS
);
581 std::unique_ptr
<object::Archive
> &Ar
= ArOrErr
.get();
583 object::OwningBinary
<object::Archive
> OB(std::move(Ar
), std::move(ArBuf
));
585 EE
->addArchive(std::move(OB
));
588 // If the target is Cygwin/MingW and we are generating remote code, we
589 // need an extra module to help out with linking.
590 if (RemoteMCJIT
&& Triple(Mod
->getTargetTriple()).isOSCygMing()) {
591 addCygMingExtraModule(*EE
, Context
, Mod
->getTargetTriple());
594 // The following functions have no effect if their respective profiling
595 // support wasn't enabled in the build configuration.
596 EE
->RegisterJITEventListener(
597 JITEventListener::createOProfileJITEventListener());
598 EE
->RegisterJITEventListener(
599 JITEventListener::createIntelJITEventListener());
601 EE
->RegisterJITEventListener(
602 JITEventListener::createPerfJITEventListener());
604 if (!NoLazyCompilation
&& RemoteMCJIT
) {
605 WithColor::warning(errs(), argv
[0])
606 << "remote mcjit does not support lazy compilation\n";
607 NoLazyCompilation
= true;
609 EE
->DisableLazyCompilation(NoLazyCompilation
);
611 // If the user specifically requested an argv[0] to pass into the program,
613 if (!FakeArgv0
.empty()) {
614 InputFile
= static_cast<std::string
>(FakeArgv0
);
616 // Otherwise, if there is a .bc suffix on the executable strip it off, it
617 // might confuse the program.
618 if (StringRef(InputFile
).ends_with(".bc"))
619 InputFile
.erase(InputFile
.length() - 3);
622 // Add the module's name to the start of the vector of arguments to main().
623 InputArgv
.insert(InputArgv
.begin(), InputFile
);
625 // Call the main function from M as if its signature were:
626 // int main (int argc, char **argv, const char **envp)
627 // using the contents of Args to determine argc & argv, and the contents of
628 // EnvVars to determine envp.
630 Function
*EntryFn
= Mod
->getFunction(EntryFunc
);
632 WithColor::error(errs(), argv
[0])
633 << '\'' << EntryFunc
<< "\' function not found in module.\n";
637 // Reset errno to zero on entry to main.
642 // Sanity check use of remote-jit: LLI currently only supports use of the
643 // remote JIT on Unix platforms.
646 WithColor::warning(errs(), argv
[0])
647 << "host does not support external remote targets.\n";
648 WithColor::note() << "defaulting to local execution\n";
651 if (ChildExecPath
.empty()) {
652 WithColor::error(errs(), argv
[0])
653 << "-remote-mcjit requires -mcjit-remote-process.\n";
655 } else if (!sys::fs::can_execute(ChildExecPath
)) {
656 WithColor::error(errs(), argv
[0])
657 << "unable to find usable child executable: '" << ChildExecPath
665 // If the program doesn't explicitly call exit, we will need the Exit
666 // function later on to make an explicit call, so get the function now.
667 FunctionCallee Exit
= Mod
->getOrInsertFunction(
668 "exit", Type::getVoidTy(Context
), Type::getInt32Ty(Context
));
670 // Run static constructors.
671 if (!ForceInterpreter
) {
672 // Give MCJIT a chance to apply relocations and set page permissions.
673 EE
->finalizeObject();
675 EE
->runStaticConstructorsDestructors(false);
677 // Trigger compilation separately so code regions that need to be
678 // invalidated will be known.
679 (void)EE
->getPointerToFunction(EntryFn
);
680 // Clear instruction cache before code will be executed.
682 static_cast<SectionMemoryManager
*>(RTDyldMM
)->invalidateInstructionCache();
685 Result
= EE
->runFunctionAsMain(EntryFn
, InputArgv
, envp
);
687 // Run static destructors.
688 EE
->runStaticConstructorsDestructors(true);
690 // If the program didn't call exit explicitly, we should call it now.
691 // This ensures that any atexit handlers get called correctly.
692 if (Function
*ExitF
=
693 dyn_cast
<Function
>(Exit
.getCallee()->stripPointerCasts())) {
694 if (ExitF
->getFunctionType() == Exit
.getFunctionType()) {
695 std::vector
<GenericValue
> Args
;
696 GenericValue ResultGV
;
697 ResultGV
.IntVal
= APInt(32, Result
);
698 Args
.push_back(ResultGV
);
699 EE
->runFunction(ExitF
, Args
);
700 WithColor::error(errs(), argv
[0])
701 << "exit(" << Result
<< ") returned!\n";
705 WithColor::error(errs(), argv
[0]) << "exit defined with wrong prototype!\n";
708 // else == "if (RemoteMCJIT)"
709 std::unique_ptr
<orc::ExecutorProcessControl
> EPC
= ExitOnErr(launchRemote());
711 // Remote target MCJIT doesn't (yet) support static constructors. No reason
712 // it couldn't. This is a limitation of the LLI implementation, not the
713 // MCJIT itself. FIXME.
715 // Create a remote memory manager.
716 auto RemoteMM
= ExitOnErr(
717 orc::EPCGenericRTDyldMemoryManager::CreateWithDefaultBootstrapSymbols(
720 // Forward MCJIT's memory manager calls to the remote memory manager.
721 static_cast<ForwardingMemoryManager
*>(RTDyldMM
)->setMemMgr(
722 std::move(RemoteMM
));
724 // Forward MCJIT's symbol resolution calls to the remote.
725 static_cast<ForwardingMemoryManager
*>(RTDyldMM
)->setResolver(
726 ExitOnErr(RemoteResolver::Create(*EPC
)));
727 // Grab the target address of the JIT'd main function on the remote and call
729 // FIXME: argv and envp handling.
731 orc::ExecutorAddr(EE
->getFunctionAddress(EntryFn
->getName().str()));
732 EE
->finalizeObject();
733 LLVM_DEBUG(dbgs() << "Executing '" << EntryFn
->getName() << "' at 0x"
734 << format("%llx", Entry
.getValue()) << "\n");
735 Result
= ExitOnErr(EPC
->runAsMain(Entry
, {}));
737 // Like static constructors, the remote target MCJIT support doesn't handle
738 // this yet. It could. FIXME.
740 // Delete the EE - we need to tear it down *before* we terminate the session
741 // with the remote, otherwise it'll crash when it tries to release resources
742 // on a remote that has already been disconnected.
745 // Signal the remote target that we're done JITing.
746 ExitOnErr(EPC
->disconnect());
752 // JITLink debug support plugins put information about JITed code in this GDB
753 // JIT Interface global from OrcTargetProcess.
754 extern "C" struct jit_descriptor __jit_debug_descriptor
;
756 static struct jit_code_entry
*
757 findNextDebugDescriptorEntry(struct jit_code_entry
*Latest
) {
758 if (Latest
== nullptr)
759 return __jit_debug_descriptor
.first_entry
;
760 if (Latest
->next_entry
)
761 return Latest
->next_entry
;
765 static ToolOutputFile
&claimToolOutput() {
766 static std::unique_ptr
<ToolOutputFile
> ToolOutput
= nullptr;
768 WithColor::error(errs(), "lli")
769 << "Can not claim stdout for tool output twice\n";
773 ToolOutput
= std::make_unique
<ToolOutputFile
>("-", EC
, sys::fs::OF_None
);
775 WithColor::error(errs(), "lli")
776 << "Failed to create tool output file: " << EC
.message() << "\n";
782 static std::function
<void(Module
&)> createIRDebugDumper() {
783 switch (OrcDumpKind
) {
784 case DumpKind::NoDump
:
785 case DumpKind::DumpDebugDescriptor
:
786 case DumpKind::DumpDebugObjects
:
787 return [](Module
&M
) {};
789 case DumpKind::DumpFuncsToStdOut
:
790 return [](Module
&M
) {
793 for (const auto &F
: M
) {
794 if (F
.isDeclaration())
798 std::string
Name(std::string(F
.getName()));
799 printf("%s ", Name
.c_str());
807 case DumpKind::DumpModsToStdOut
:
808 return [](Module
&M
) {
809 outs() << "----- Module Start -----\n" << M
<< "----- Module End -----\n";
812 case DumpKind::DumpModsToDisk
:
813 return [](Module
&M
) {
815 raw_fd_ostream
Out(M
.getModuleIdentifier() + ".ll", EC
,
816 sys::fs::OF_TextWithCRLF
);
818 errs() << "Couldn't open " << M
.getModuleIdentifier()
819 << " for dumping.\nError:" << EC
.message() << "\n";
825 llvm_unreachable("Unknown DumpKind");
828 static std::function
<void(MemoryBuffer
&)> createObjDebugDumper() {
829 switch (OrcDumpKind
) {
830 case DumpKind::NoDump
:
831 case DumpKind::DumpFuncsToStdOut
:
832 case DumpKind::DumpModsToStdOut
:
833 case DumpKind::DumpModsToDisk
:
834 return [](MemoryBuffer
&) {};
836 case DumpKind::DumpDebugDescriptor
: {
837 // Dump the empty descriptor at startup once
838 fprintf(stderr
, "jit_debug_descriptor 0x%016" PRIx64
"\n",
839 pointerToJITTargetAddress(__jit_debug_descriptor
.first_entry
));
840 return [](MemoryBuffer
&) {
841 // Dump new entries as they appear
842 static struct jit_code_entry
*Latest
= nullptr;
843 while (auto *NewEntry
= findNextDebugDescriptorEntry(Latest
)) {
844 fprintf(stderr
, "jit_debug_descriptor 0x%016" PRIx64
"\n",
845 pointerToJITTargetAddress(NewEntry
));
851 case DumpKind::DumpDebugObjects
: {
852 return [](MemoryBuffer
&Obj
) {
853 static struct jit_code_entry
*Latest
= nullptr;
854 static ToolOutputFile
&ToolOutput
= claimToolOutput();
855 while (auto *NewEntry
= findNextDebugDescriptorEntry(Latest
)) {
856 ToolOutput
.os().write(NewEntry
->symfile_addr
, NewEntry
->symfile_size
);
862 llvm_unreachable("Unknown DumpKind");
866 for (const auto &Dylib
: Dylibs
) {
868 if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib
.c_str(), &ErrMsg
))
869 return make_error
<StringError
>(ErrMsg
, inconvertibleErrorCode());
872 return Error::success();
875 static void exitOnLazyCallThroughFailure() { exit(1); }
877 Expected
<orc::ThreadSafeModule
>
878 loadModule(StringRef Path
, orc::ThreadSafeContext TSCtx
) {
880 auto M
= parseIRFile(Path
, Err
, *TSCtx
.getContext());
884 raw_string_ostream
ErrMsgStream(ErrMsg
);
885 Err
.print("lli", ErrMsgStream
);
887 return make_error
<StringError
>(std::move(ErrMsg
), inconvertibleErrorCode());
890 if (EnableCacheManager
)
891 M
->setModuleIdentifier("file:" + M
->getModuleIdentifier());
893 return orc::ThreadSafeModule(std::move(M
), std::move(TSCtx
));
896 int mingw_noop_main(void) {
897 // Cygwin and MinGW insert calls from the main function to the runtime
898 // function __main. The __main function is responsible for setting up main's
899 // environment (e.g. running static constructors), however this is not needed
900 // when running under lli: the executor process will have run non-JIT ctors,
901 // and ORC will take care of running JIT'd ctors. To avoid a missing symbol
902 // error we just implement __main as a no-op.
904 // FIXME: Move this to ORC-RT (and the ORC-RT substitution library once it
905 // exists). That will allow it to work out-of-process, and for all
906 // ORC tools (the problem isn't lli specific).
910 // Try to enable debugger support for the given instance.
911 // This alway returns success, but prints a warning if it's not able to enable
913 Error
tryEnableDebugSupport(orc::LLJIT
&J
) {
914 if (auto Err
= enableDebuggerSupport(J
)) {
915 [[maybe_unused
]] std::string ErrMsg
= toString(std::move(Err
));
916 LLVM_DEBUG(dbgs() << "lli: " << ErrMsg
<< "\n");
918 return Error::success();
921 int runOrcJIT(const char *ProgName
) {
922 // Start setting up the JIT environment.
924 // Parse the main module.
925 orc::ThreadSafeContext
TSCtx(std::make_unique
<LLVMContext
>());
926 auto MainModule
= ExitOnErr(loadModule(InputFile
, TSCtx
));
928 // Get TargetTriple and DataLayout from the main module if they're explicitly
930 std::optional
<Triple
> TT
;
931 std::optional
<DataLayout
> DL
;
932 MainModule
.withModuleDo([&](Module
&M
) {
933 if (!M
.getTargetTriple().empty())
934 TT
= Triple(M
.getTargetTriple());
935 if (!M
.getDataLayout().isDefault())
936 DL
= M
.getDataLayout();
939 orc::LLLazyJITBuilder Builder
;
941 Builder
.setJITTargetMachineBuilder(
942 TT
? orc::JITTargetMachineBuilder(*TT
)
943 : ExitOnErr(orc::JITTargetMachineBuilder::detectHost()));
945 TT
= Builder
.getJITTargetMachineBuilder()->getTargetTriple();
947 Builder
.setDataLayout(DL
);
949 if (!codegen::getMArch().empty())
950 Builder
.getJITTargetMachineBuilder()->getTargetTriple().setArchName(
951 codegen::getMArch());
953 Builder
.getJITTargetMachineBuilder()
954 ->setCPU(codegen::getCPUStr())
955 .addFeatures(codegen::getFeatureList())
956 .setRelocationModel(codegen::getExplicitRelocModel())
957 .setCodeModel(codegen::getExplicitCodeModel());
959 // Link process symbols unless NoProcessSymbols is set.
960 Builder
.setLinkProcessSymbolsByDefault(!NoProcessSymbols
);
962 // FIXME: Setting a dummy call-through manager in non-lazy mode prevents the
963 // JIT builder to instantiate a default (which would fail with an error for
964 // unsupported architectures).
965 if (UseJITKind
!= JITKind::OrcLazy
) {
966 auto ES
= std::make_unique
<orc::ExecutionSession
>(
967 ExitOnErr(orc::SelfExecutorProcessControl::Create()));
968 Builder
.setLazyCallthroughManager(
969 std::make_unique
<orc::LazyCallThroughManager
>(*ES
, orc::ExecutorAddr(),
971 Builder
.setExecutionSession(std::move(ES
));
974 Builder
.setLazyCompileFailureAddr(
975 orc::ExecutorAddr::fromPtr(exitOnLazyCallThroughFailure
));
976 Builder
.setNumCompileThreads(LazyJITCompileThreads
);
978 // If the object cache is enabled then set a custom compile function
979 // creator to use the cache.
980 std::unique_ptr
<LLIObjectCache
> CacheManager
;
981 if (EnableCacheManager
) {
983 CacheManager
= std::make_unique
<LLIObjectCache
>(ObjectCacheDir
);
985 Builder
.setCompileFunctionCreator(
986 [&](orc::JITTargetMachineBuilder JTMB
)
987 -> Expected
<std::unique_ptr
<orc::IRCompileLayer::IRCompiler
>> {
988 if (LazyJITCompileThreads
> 0)
989 return std::make_unique
<orc::ConcurrentIRCompiler
>(std::move(JTMB
),
992 auto TM
= JTMB
.createTargetMachine();
994 return TM
.takeError();
996 return std::make_unique
<orc::TMOwningSimpleCompiler
>(std::move(*TM
),
1001 // Enable debugging of JIT'd code (only works on JITLink for ELF and MachO).
1002 Builder
.setPrePlatformSetup(tryEnableDebugSupport
);
1004 // Set up LLJIT platform.
1005 LLJITPlatform P
= Platform
;
1006 if (P
== LLJITPlatform::Auto
)
1007 P
= OrcRuntime
.empty() ? LLJITPlatform::GenericIR
1008 : LLJITPlatform::ExecutorNative
;
1011 case LLJITPlatform::ExecutorNative
: {
1012 Builder
.setPlatformSetUp(orc::ExecutorNativePlatform(OrcRuntime
));
1015 case LLJITPlatform::GenericIR
:
1016 // Nothing to do: LLJITBuilder will use this by default.
1018 case LLJITPlatform::Inactive
:
1019 Builder
.setPlatformSetUp(orc::setUpInactivePlatform
);
1022 llvm_unreachable("Unrecognized platform value");
1025 std::unique_ptr
<orc::ExecutorProcessControl
> EPC
= nullptr;
1026 if (JITLinker
== JITLinkerKind::JITLink
) {
1027 EPC
= ExitOnErr(orc::SelfExecutorProcessControl::Create(
1028 std::make_shared
<orc::SymbolStringPool
>()));
1030 Builder
.getJITTargetMachineBuilder()
1031 ->setRelocationModel(Reloc::PIC_
)
1032 .setCodeModel(CodeModel::Small
);
1033 Builder
.setObjectLinkingLayerCreator([&P
](orc::ExecutionSession
&ES
,
1035 auto L
= std::make_unique
<orc::ObjectLinkingLayer
>(ES
);
1036 if (P
!= LLJITPlatform::ExecutorNative
)
1037 L
->addPlugin(std::make_unique
<orc::EHFrameRegistrationPlugin
>(
1038 ES
, ExitOnErr(orc::EPCEHFrameRegistrar::Create(ES
))));
1043 auto J
= ExitOnErr(Builder
.create());
1045 auto *ObjLayer
= &J
->getObjLinkingLayer();
1046 if (auto *RTDyldObjLayer
= dyn_cast
<orc::RTDyldObjectLinkingLayer
>(ObjLayer
)) {
1047 RTDyldObjLayer
->registerJITEventListener(
1048 *JITEventListener::createGDBRegistrationListener());
1049 #if LLVM_USE_OPROFILE
1050 RTDyldObjLayer
->registerJITEventListener(
1051 *JITEventListener::createOProfileJITEventListener());
1053 #if LLVM_USE_INTEL_JITEVENTS
1054 RTDyldObjLayer
->registerJITEventListener(
1055 *JITEventListener::createIntelJITEventListener());
1058 RTDyldObjLayer
->registerJITEventListener(
1059 *JITEventListener::createPerfJITEventListener());
1064 J
->setPartitionFunction(orc::CompileOnDemandLayer::compileWholeModule
);
1066 auto IRDump
= createIRDebugDumper();
1067 J
->getIRTransformLayer().setTransform(
1068 [&](orc::ThreadSafeModule TSM
,
1069 const orc::MaterializationResponsibility
&R
) {
1070 TSM
.withModuleDo([&](Module
&M
) {
1071 if (verifyModule(M
, &dbgs())) {
1072 dbgs() << "Bad module: " << &M
<< "\n";
1080 auto ObjDump
= createObjDebugDumper();
1081 J
->getObjTransformLayer().setTransform(
1082 [&](std::unique_ptr
<MemoryBuffer
> Obj
)
1083 -> Expected
<std::unique_ptr
<MemoryBuffer
>> {
1085 return std::move(Obj
);
1088 // If this is a Mingw or Cygwin executor then we need to alias __main to
1089 // orc_rt_int_void_return_0.
1090 if (J
->getTargetTriple().isOSCygMing())
1091 ExitOnErr(J
->getProcessSymbolsJITDylib()->define(
1092 orc::absoluteSymbols({{J
->mangleAndIntern("__main"),
1093 {orc::ExecutorAddr::fromPtr(mingw_noop_main
),
1094 JITSymbolFlags::Exported
}}})));
1096 // Regular modules are greedy: They materialize as a whole and trigger
1097 // materialization for all required symbols recursively. Lazy modules go
1098 // through partitioning and they replace outgoing calls with reexport stubs
1099 // that resolve on call-through.
1100 auto AddModule
= [&](orc::JITDylib
&JD
, orc::ThreadSafeModule M
) {
1101 return UseJITKind
== JITKind::OrcLazy
? J
->addLazyIRModule(JD
, std::move(M
))
1102 : J
->addIRModule(JD
, std::move(M
));
1105 // Add the main module.
1106 ExitOnErr(AddModule(J
->getMainJITDylib(), std::move(MainModule
)));
1108 // Create JITDylibs and add any extra modules.
1110 // Create JITDylibs, keep a map from argument index to dylib. We will use
1111 // -extra-module argument indexes to determine what dylib to use for each
1113 std::map
<unsigned, orc::JITDylib
*> IdxToDylib
;
1114 IdxToDylib
[0] = &J
->getMainJITDylib();
1115 for (auto JDItr
= JITDylibs
.begin(), JDEnd
= JITDylibs
.end();
1116 JDItr
!= JDEnd
; ++JDItr
) {
1117 orc::JITDylib
*JD
= J
->getJITDylibByName(*JDItr
);
1119 JD
= &ExitOnErr(J
->createJITDylib(*JDItr
));
1120 J
->getMainJITDylib().addToLinkOrder(*JD
);
1121 JD
->addToLinkOrder(J
->getMainJITDylib());
1123 IdxToDylib
[JITDylibs
.getPosition(JDItr
- JITDylibs
.begin())] = JD
;
1126 for (auto EMItr
= ExtraModules
.begin(), EMEnd
= ExtraModules
.end();
1127 EMItr
!= EMEnd
; ++EMItr
) {
1128 auto M
= ExitOnErr(loadModule(*EMItr
, TSCtx
));
1130 auto EMIdx
= ExtraModules
.getPosition(EMItr
- ExtraModules
.begin());
1131 assert(EMIdx
!= 0 && "ExtraModule should have index > 0");
1132 auto JDItr
= std::prev(IdxToDylib
.lower_bound(EMIdx
));
1133 auto &JD
= *JDItr
->second
;
1134 ExitOnErr(AddModule(JD
, std::move(M
)));
1137 for (auto EAItr
= ExtraArchives
.begin(), EAEnd
= ExtraArchives
.end();
1138 EAItr
!= EAEnd
; ++EAItr
) {
1139 auto EAIdx
= ExtraArchives
.getPosition(EAItr
- ExtraArchives
.begin());
1140 assert(EAIdx
!= 0 && "ExtraArchive should have index > 0");
1141 auto JDItr
= std::prev(IdxToDylib
.lower_bound(EAIdx
));
1142 auto &JD
= *JDItr
->second
;
1143 ExitOnErr(J
->linkStaticLibraryInto(JD
, EAItr
->c_str()));
1148 for (auto &ObjPath
: ExtraObjects
) {
1149 auto Obj
= ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath
)));
1150 ExitOnErr(J
->addObjectFile(std::move(Obj
)));
1153 // Run any static constructors.
1154 ExitOnErr(J
->initialize(J
->getMainJITDylib()));
1156 // Run any -thread-entry points.
1157 std::vector
<std::thread
> AltEntryThreads
;
1158 for (auto &ThreadEntryPoint
: ThreadEntryPoints
) {
1159 auto EntryPointSym
= ExitOnErr(J
->lookup(ThreadEntryPoint
));
1160 typedef void (*EntryPointPtr
)();
1161 auto EntryPoint
= EntryPointSym
.toPtr
<EntryPointPtr
>();
1162 AltEntryThreads
.push_back(std::thread([EntryPoint
]() { EntryPoint(); }));
1165 // Resolve and run the main function.
1166 auto MainAddr
= ExitOnErr(J
->lookup(EntryFunc
));
1170 // ExecutorProcessControl-based execution with JITLink.
1171 Result
= ExitOnErr(EPC
->runAsMain(MainAddr
, InputArgv
));
1173 // Manual in-process execution with RuntimeDyld.
1174 using MainFnTy
= int(int, char *[]);
1175 auto MainFn
= MainAddr
.toPtr
<MainFnTy
*>();
1176 Result
= orc::runAsMain(MainFn
, InputArgv
, StringRef(InputFile
));
1179 // Wait for -entry-point threads.
1180 for (auto &AltEntryThread
: AltEntryThreads
)
1181 AltEntryThread
.join();
1184 ExitOnErr(J
->deinitialize(J
->getMainJITDylib()));
1189 void disallowOrcOptions() {
1190 // Make sure nobody used an orc-lazy specific option accidentally.
1192 if (LazyJITCompileThreads
!= 0) {
1193 errs() << "-compile-threads requires -jit-kind=orc-lazy\n";
1197 if (!ThreadEntryPoints
.empty()) {
1198 errs() << "-thread-entry requires -jit-kind=orc-lazy\n";
1202 if (PerModuleLazy
) {
1203 errs() << "-per-module-lazy requires -jit-kind=orc-lazy\n";
1208 Expected
<std::unique_ptr
<orc::ExecutorProcessControl
>> launchRemote() {
1209 #ifndef LLVM_ON_UNIX
1210 llvm_unreachable("launchRemote not supported on non-Unix platforms");
1215 // Create two pipes.
1216 if (pipe(PipeFD
[0]) != 0 || pipe(PipeFD
[1]) != 0)
1217 perror("Error creating pipe: ");
1221 if (ChildPID
== 0) {
1224 // Close the parent ends of the pipes
1225 close(PipeFD
[0][1]);
1226 close(PipeFD
[1][0]);
1229 // Execute the child process.
1230 std::unique_ptr
<char[]> ChildPath
, ChildIn
, ChildOut
;
1232 ChildPath
.reset(new char[ChildExecPath
.size() + 1]);
1233 std::copy(ChildExecPath
.begin(), ChildExecPath
.end(), &ChildPath
[0]);
1234 ChildPath
[ChildExecPath
.size()] = '\0';
1235 std::string ChildInStr
= utostr(PipeFD
[0][0]);
1236 ChildIn
.reset(new char[ChildInStr
.size() + 1]);
1237 std::copy(ChildInStr
.begin(), ChildInStr
.end(), &ChildIn
[0]);
1238 ChildIn
[ChildInStr
.size()] = '\0';
1239 std::string ChildOutStr
= utostr(PipeFD
[1][1]);
1240 ChildOut
.reset(new char[ChildOutStr
.size() + 1]);
1241 std::copy(ChildOutStr
.begin(), ChildOutStr
.end(), &ChildOut
[0]);
1242 ChildOut
[ChildOutStr
.size()] = '\0';
1245 char * const args
[] = { &ChildPath
[0], &ChildIn
[0], &ChildOut
[0], nullptr };
1246 int rc
= execv(ChildExecPath
.c_str(), args
);
1248 perror("Error executing child process: ");
1249 llvm_unreachable("Error executing child process");
1251 // else we're the parent...
1253 // Close the child ends of the pipes
1254 close(PipeFD
[0][0]);
1255 close(PipeFD
[1][1]);
1257 // Return a SimpleRemoteEPC instance connected to our end of the pipes.
1258 return orc::SimpleRemoteEPC::Create
<orc::FDSimpleRemoteEPCTransport
>(
1259 std::make_unique
<llvm::orc::InPlaceTaskDispatcher
>(),
1260 llvm::orc::SimpleRemoteEPC::Setup(), PipeFD
[1][0], PipeFD
[0][1]);
1264 // For MinGW environments, manually export the __chkstk function from the lli
1267 // Normally, this function is provided by compiler-rt builtins or libgcc.
1268 // It is named "_alloca" on i386, "___chkstk_ms" on x86_64, and "__chkstk" on
1269 // arm/aarch64. In MSVC configurations, it's named "__chkstk" in all
1272 // When Orc tries to resolve symbols at runtime, this succeeds in MSVC
1273 // configurations, somewhat by accident/luck; kernelbase.dll does export a
1274 // symbol named "__chkstk" which gets found by Orc, even if regular applications
1275 // never link against that function from that DLL (it's linked in statically
1276 // from a compiler support library).
1278 // The MinGW specific symbol names aren't available in that DLL though.
1279 // Therefore, manually export the relevant symbol from lli, to let it be
1280 // found at runtime during tests.
1282 // For real JIT uses, the real compiler support libraries should be linked
1283 // in, somehow; this is a workaround to let tests pass.
1285 // We need to make sure that this symbol actually is linked in when we
1286 // try to export it; if no functions allocate a large enough stack area,
1287 // nothing would reference it. Therefore, manually declare it and add a
1288 // reference to it. (Note, the declarations of _alloca/___chkstk_ms/__chkstk
1289 // are somewhat bogus, these functions use a different custom calling
1292 // TODO: Move this into libORC at some point, see
1293 // https://github.com/llvm/llvm-project/issues/56603.
1295 // This is a MinGW version of #pragma comment(linker, "...") that doesn't
1296 // require compiling with -fms-extensions.
1297 #if defined(__i386__)
1299 extern "C" void _alloca(void);
1300 static __attribute__((used
)) void (*const ref_func
)(void) = _alloca
;
1301 static __attribute__((section(".drectve"), used
)) const char export_chkstk
[] =
1303 #elif defined(__x86_64__)
1304 extern "C" void ___chkstk_ms(void);
1305 static __attribute__((used
)) void (*const ref_func
)(void) = ___chkstk_ms
;
1306 static __attribute__((section(".drectve"), used
)) const char export_chkstk
[] =
1307 "-export:___chkstk_ms";
1309 extern "C" void __chkstk(void);
1310 static __attribute__((used
)) void (*const ref_func
)(void) = __chkstk
;
1311 static __attribute__((section(".drectve"), used
)) const char export_chkstk
[] =