1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This utility provides a simple wrapper around the LLVM Execution Engines,
11 // which allow the direct execution of LLVM programs through a Just-In-Time
12 // compiler, or through an interpreter if no JIT is available for this platform.
14 //===----------------------------------------------------------------------===//
16 #include "RemoteJITUtils.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Bitcode/BitcodeReader.h"
20 #include "llvm/CodeGen/CommandFlags.inc"
21 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
22 #include "llvm/Config/llvm-config.h"
23 #include "llvm/ExecutionEngine/GenericValue.h"
24 #include "llvm/ExecutionEngine/Interpreter.h"
25 #include "llvm/ExecutionEngine/JITEventListener.h"
26 #include "llvm/ExecutionEngine/MCJIT.h"
27 #include "llvm/ExecutionEngine/ObjectCache.h"
28 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
29 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
30 #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
31 #include "llvm/ExecutionEngine/OrcMCJITReplacement.h"
32 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
33 #include "llvm/IR/IRBuilder.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/IR/TypeBuilder.h"
38 #include "llvm/IR/Verifier.h"
39 #include "llvm/IRReader/IRReader.h"
40 #include "llvm/Object/Archive.h"
41 #include "llvm/Object/ObjectFile.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/DynamicLibrary.h"
45 #include "llvm/Support/Format.h"
46 #include "llvm/Support/InitLLVM.h"
47 #include "llvm/Support/ManagedStatic.h"
48 #include "llvm/Support/MathExtras.h"
49 #include "llvm/Support/Memory.h"
50 #include "llvm/Support/MemoryBuffer.h"
51 #include "llvm/Support/Path.h"
52 #include "llvm/Support/PluginLoader.h"
53 #include "llvm/Support/Process.h"
54 #include "llvm/Support/Program.h"
55 #include "llvm/Support/SourceMgr.h"
56 #include "llvm/Support/TargetSelect.h"
57 #include "llvm/Support/WithColor.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include "llvm/Transforms/Instrumentation.h"
63 #include <cygwin/version.h>
64 #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
65 #define DO_NOTHING_ATEXIT 1
71 #define DEBUG_TYPE "lli"
75 enum class JITKind
{ MCJIT
, OrcMCJITReplacement
, OrcLazy
};
78 InputFile(cl::desc("<input bitcode>"), cl::Positional
, cl::init("-"));
81 InputArgv(cl::ConsumeAfter
, cl::desc("<program arguments>..."));
83 cl::opt
<bool> ForceInterpreter("force-interpreter",
84 cl::desc("Force interpretation: disable JIT"),
87 cl::opt
<JITKind
> UseJITKind("jit-kind",
88 cl::desc("Choose underlying JIT kind."),
89 cl::init(JITKind::MCJIT
),
91 clEnumValN(JITKind::MCJIT
, "mcjit",
93 clEnumValN(JITKind::OrcMCJITReplacement
,
95 "Orc-based MCJIT replacement"),
96 clEnumValN(JITKind::OrcLazy
,
98 "Orc-based lazy JIT.")));
100 // The MCJIT supports building for a target address space separate from
101 // the JIT compilation process. Use a forked process and a copying
102 // memory manager with IPC to execute using this functionality.
103 cl::opt
<bool> RemoteMCJIT("remote-mcjit",
104 cl::desc("Execute MCJIT'ed code in a separate process."),
107 // Manually specify the child process for remote execution. This overrides
108 // the simulated remote execution that allocates address space for child
109 // execution. The child process will be executed and will communicate with
110 // lli via stdin/stdout pipes.
112 ChildExecPath("mcjit-remote-process",
113 cl::desc("Specify the filename of the process to launch "
114 "for remote MCJIT execution. If none is specified,"
115 "\n\tremote execution will be simulated in-process."),
116 cl::value_desc("filename"), cl::init(""));
118 // Determine optimization level.
121 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
122 "(default = '-O2')"),
128 TargetTriple("mtriple", cl::desc("Override target triple for module"));
131 EntryFunc("entry-function",
132 cl::desc("Specify the entry function (default = 'main') "
133 "of the executable"),
134 cl::value_desc("function"),
137 cl::list
<std::string
>
138 ExtraModules("extra-module",
139 cl::desc("Extra modules to be loaded"),
140 cl::value_desc("input bitcode"));
142 cl::list
<std::string
>
143 ExtraObjects("extra-object",
144 cl::desc("Extra object files to be loaded"),
145 cl::value_desc("input object"));
147 cl::list
<std::string
>
148 ExtraArchives("extra-archive",
149 cl::desc("Extra archive files to be loaded"),
150 cl::value_desc("input archive"));
153 EnableCacheManager("enable-cache-manager",
154 cl::desc("Use cache manager to save/load mdoules"),
158 ObjectCacheDir("object-cache-dir",
159 cl::desc("Directory to store cached object files "
160 "(must be user writable)"),
164 FakeArgv0("fake-argv0",
165 cl::desc("Override the 'argv[0]' value passed into the executing"
166 " program"), cl::value_desc("executable"));
169 DisableCoreFiles("disable-core-files", cl::Hidden
,
170 cl::desc("Disable emission of core files if possible"));
173 NoLazyCompilation("disable-lazy-compilation",
174 cl::desc("Disable JIT lazy compilation"),
178 GenerateSoftFloatCalls("soft-float",
179 cl::desc("Generate software floating point library calls"),
182 enum class DumpKind
{
189 cl::opt
<DumpKind
> OrcDumpKind(
190 "orc-lazy-debug", cl::desc("Debug dumping for the orc-lazy JIT."),
191 cl::init(DumpKind::NoDump
),
192 cl::values(clEnumValN(DumpKind::NoDump
, "no-dump",
193 "Don't dump anything."),
194 clEnumValN(DumpKind::DumpFuncsToStdOut
, "funcs-to-stdout",
195 "Dump function names to stdout."),
196 clEnumValN(DumpKind::DumpModsToStdOut
, "mods-to-stdout",
197 "Dump modules to stdout."),
198 clEnumValN(DumpKind::DumpModsToDisk
, "mods-to-disk",
199 "Dump modules to the current "
200 "working directory. (WARNING: "
201 "will overwrite existing files).")),
204 ExitOnError ExitOnErr
;
207 //===----------------------------------------------------------------------===//
210 // This object cache implementation writes cached objects to disk to the
211 // directory specified by CacheDir, using a filename provided in the module
212 // descriptor. The cache tries to load a saved object using that path if the
213 // file exists. CacheDir defaults to "", in which case objects are cached
214 // alongside their originating bitcodes.
216 class LLIObjectCache
: public ObjectCache
{
218 LLIObjectCache(const std::string
& CacheDir
) : CacheDir(CacheDir
) {
219 // Add trailing '/' to cache dir if necessary.
220 if (!this->CacheDir
.empty() &&
221 this->CacheDir
[this->CacheDir
.size() - 1] != '/')
222 this->CacheDir
+= '/';
224 ~LLIObjectCache() override
{}
226 void notifyObjectCompiled(const Module
*M
, MemoryBufferRef Obj
) override
{
227 const std::string
&ModuleID
= M
->getModuleIdentifier();
228 std::string CacheName
;
229 if (!getCacheFilename(ModuleID
, CacheName
))
231 if (!CacheDir
.empty()) { // Create user-defined cache dir.
232 SmallString
<128> dir(sys::path::parent_path(CacheName
));
233 sys::fs::create_directories(Twine(dir
));
236 raw_fd_ostream
outfile(CacheName
, EC
, sys::fs::F_None
);
237 outfile
.write(Obj
.getBufferStart(), Obj
.getBufferSize());
241 std::unique_ptr
<MemoryBuffer
> getObject(const Module
* M
) override
{
242 const std::string
&ModuleID
= M
->getModuleIdentifier();
243 std::string CacheName
;
244 if (!getCacheFilename(ModuleID
, CacheName
))
246 // Load the object from the cache filename
247 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> IRObjectBuffer
=
248 MemoryBuffer::getFile(CacheName
, -1, false);
249 // If the file isn't there, that's OK.
252 // MCJIT will want to write into this buffer, and we don't want that
253 // because the file has probably just been mmapped. Instead we make
254 // a copy. The filed-based buffer will be released when it goes
256 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer
.get()->getBuffer());
260 std::string CacheDir
;
262 bool getCacheFilename(const std::string
&ModID
, std::string
&CacheName
) {
263 std::string
Prefix("file:");
264 size_t PrefixLength
= Prefix
.length();
265 if (ModID
.substr(0, PrefixLength
) != Prefix
)
267 std::string CacheSubdir
= ModID
.substr(PrefixLength
);
269 // Transform "X:\foo" => "/X\foo" for convenience.
270 if (isalpha(CacheSubdir
[0]) && CacheSubdir
[1] == ':') {
271 CacheSubdir
[1] = CacheSubdir
[0];
272 CacheSubdir
[0] = '/';
275 CacheName
= CacheDir
+ CacheSubdir
;
276 size_t pos
= CacheName
.rfind('.');
277 CacheName
.replace(pos
, CacheName
.length() - pos
, ".o");
282 // On Mingw and Cygwin, an external symbol named '__main' is called from the
283 // generated 'main' function to allow static initialization. To avoid linking
284 // problems with remote targets (because lli's remote target support does not
285 // currently handle external linking) we add a secondary module which defines
286 // an empty '__main' function.
287 static void addCygMingExtraModule(ExecutionEngine
&EE
, LLVMContext
&Context
,
288 StringRef TargetTripleStr
) {
289 IRBuilder
<> Builder(Context
);
290 Triple
TargetTriple(TargetTripleStr
);
292 // Create a new module.
293 std::unique_ptr
<Module
> M
= make_unique
<Module
>("CygMingHelper", Context
);
294 M
->setTargetTriple(TargetTripleStr
);
296 // Create an empty function named "__main".
298 if (TargetTriple
.isArch64Bit()) {
299 Result
= Function::Create(
300 TypeBuilder
<int64_t(void), false>::get(Context
),
301 GlobalValue::ExternalLinkage
, "__main", M
.get());
303 Result
= Function::Create(
304 TypeBuilder
<int32_t(void), false>::get(Context
),
305 GlobalValue::ExternalLinkage
, "__main", M
.get());
307 BasicBlock
*BB
= BasicBlock::Create(Context
, "__main", Result
);
308 Builder
.SetInsertPoint(BB
);
310 if (TargetTriple
.isArch64Bit())
311 ReturnVal
= ConstantInt::get(Context
, APInt(64, 0));
313 ReturnVal
= ConstantInt::get(Context
, APInt(32, 0));
314 Builder
.CreateRet(ReturnVal
);
316 // Add this new module to the ExecutionEngine.
317 EE
.addModule(std::move(M
));
320 CodeGenOpt::Level
getOptLevel() {
323 WithColor::error(errs(), "lli") << "invalid optimization level.\n";
325 case '0': return CodeGenOpt::None
;
326 case '1': return CodeGenOpt::Less
;
328 case '2': return CodeGenOpt::Default
;
329 case '3': return CodeGenOpt::Aggressive
;
331 llvm_unreachable("Unrecognized opt level.");
334 LLVM_ATTRIBUTE_NORETURN
335 static void reportError(SMDiagnostic Err
, const char *ProgName
) {
336 Err
.print(ProgName
, errs());
340 int runOrcLazyJIT(LLVMContext
&Ctx
, std::vector
<std::unique_ptr
<Module
>> Ms
,
341 const std::vector
<std::string
> &Args
);
343 //===----------------------------------------------------------------------===//
344 // main Driver function
346 int main(int argc
, char **argv
, char * const *envp
) {
347 InitLLVM
X(argc
, argv
);
350 ExitOnErr
.setBanner(std::string(argv
[0]) + ": ");
352 // If we have a native target, initialize it to ensure it is linked in and
353 // usable by the JIT.
354 InitializeNativeTarget();
355 InitializeNativeTargetAsmPrinter();
356 InitializeNativeTargetAsmParser();
358 cl::ParseCommandLineOptions(argc
, argv
,
359 "llvm interpreter & dynamic compiler\n");
361 // If the user doesn't want core files, disable them.
362 if (DisableCoreFiles
)
363 sys::Process::PreventCoreFiles();
367 // Load the bitcode...
369 std::unique_ptr
<Module
> Owner
= parseIRFile(InputFile
, Err
, Context
);
370 Module
*Mod
= Owner
.get();
372 reportError(Err
, argv
[0]);
374 if (UseJITKind
== JITKind::OrcLazy
) {
375 std::vector
<std::unique_ptr
<Module
>> Ms
;
376 Ms
.push_back(std::move(Owner
));
377 for (auto &ExtraMod
: ExtraModules
) {
378 Ms
.push_back(parseIRFile(ExtraMod
, Err
, Context
));
380 reportError(Err
, argv
[0]);
382 std::vector
<std::string
> Args
;
383 Args
.push_back(InputFile
);
384 for (auto &Arg
: InputArgv
)
386 return runOrcLazyJIT(Context
, std::move(Ms
), Args
);
389 if (EnableCacheManager
) {
390 std::string
CacheName("file:");
391 CacheName
.append(InputFile
);
392 Mod
->setModuleIdentifier(CacheName
);
395 // If not jitting lazily, load the whole bitcode file eagerly too.
396 if (NoLazyCompilation
) {
397 // Use *argv instead of argv[0] to work around a wrong GCC warning.
398 ExitOnError
ExitOnErr(std::string(*argv
) +
399 ": bitcode didn't read correctly: ");
400 ExitOnErr(Mod
->materializeAll());
403 std::string ErrorMsg
;
404 EngineBuilder
builder(std::move(Owner
));
405 builder
.setMArch(MArch
);
406 builder
.setMCPU(getCPUStr());
407 builder
.setMAttrs(getFeatureList());
408 if (RelocModel
.getNumOccurrences())
409 builder
.setRelocationModel(RelocModel
);
410 if (CMModel
.getNumOccurrences())
411 builder
.setCodeModel(CMModel
);
412 builder
.setErrorStr(&ErrorMsg
);
413 builder
.setEngineKind(ForceInterpreter
414 ? EngineKind::Interpreter
416 builder
.setUseOrcMCJITReplacement(UseJITKind
== JITKind::OrcMCJITReplacement
);
418 // If we are supposed to override the target triple, do so now.
419 if (!TargetTriple
.empty())
420 Mod
->setTargetTriple(Triple::normalize(TargetTriple
));
422 // Enable MCJIT if desired.
423 RTDyldMemoryManager
*RTDyldMM
= nullptr;
424 if (!ForceInterpreter
) {
426 RTDyldMM
= new ForwardingMemoryManager();
428 RTDyldMM
= new SectionMemoryManager();
430 // Deliberately construct a temp std::unique_ptr to pass in. Do not null out
431 // RTDyldMM: We still use it below, even though we don't own it.
432 builder
.setMCJITMemoryManager(
433 std::unique_ptr
<RTDyldMemoryManager
>(RTDyldMM
));
434 } else if (RemoteMCJIT
) {
435 WithColor::error(errs(), argv
[0])
436 << "remote process execution does not work with the interpreter.\n";
440 builder
.setOptLevel(getOptLevel());
442 TargetOptions Options
= InitTargetOptionsFromCodeGenFlags();
443 if (FloatABIForCalls
!= FloatABI::Default
)
444 Options
.FloatABIType
= FloatABIForCalls
;
446 builder
.setTargetOptions(Options
);
448 std::unique_ptr
<ExecutionEngine
> EE(builder
.create());
450 if (!ErrorMsg
.empty())
451 WithColor::error(errs(), argv
[0])
452 << "error creating EE: " << ErrorMsg
<< "\n";
454 WithColor::error(errs(), argv
[0]) << "unknown error creating EE!\n";
458 std::unique_ptr
<LLIObjectCache
> CacheManager
;
459 if (EnableCacheManager
) {
460 CacheManager
.reset(new LLIObjectCache(ObjectCacheDir
));
461 EE
->setObjectCache(CacheManager
.get());
464 // Load any additional modules specified on the command line.
465 for (unsigned i
= 0, e
= ExtraModules
.size(); i
!= e
; ++i
) {
466 std::unique_ptr
<Module
> XMod
= parseIRFile(ExtraModules
[i
], Err
, Context
);
468 reportError(Err
, argv
[0]);
469 if (EnableCacheManager
) {
470 std::string
CacheName("file:");
471 CacheName
.append(ExtraModules
[i
]);
472 XMod
->setModuleIdentifier(CacheName
);
474 EE
->addModule(std::move(XMod
));
477 for (unsigned i
= 0, e
= ExtraObjects
.size(); i
!= e
; ++i
) {
478 Expected
<object::OwningBinary
<object::ObjectFile
>> Obj
=
479 object::ObjectFile::createObjectFile(ExtraObjects
[i
]);
481 // TODO: Actually report errors helpfully.
482 consumeError(Obj
.takeError());
483 reportError(Err
, argv
[0]);
485 object::OwningBinary
<object::ObjectFile
> &O
= Obj
.get();
486 EE
->addObjectFile(std::move(O
));
489 for (unsigned i
= 0, e
= ExtraArchives
.size(); i
!= e
; ++i
) {
490 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> ArBufOrErr
=
491 MemoryBuffer::getFileOrSTDIN(ExtraArchives
[i
]);
493 reportError(Err
, argv
[0]);
494 std::unique_ptr
<MemoryBuffer
> &ArBuf
= ArBufOrErr
.get();
496 Expected
<std::unique_ptr
<object::Archive
>> ArOrErr
=
497 object::Archive::create(ArBuf
->getMemBufferRef());
500 raw_string_ostream
OS(Buf
);
501 logAllUnhandledErrors(ArOrErr
.takeError(), OS
, "");
506 std::unique_ptr
<object::Archive
> &Ar
= ArOrErr
.get();
508 object::OwningBinary
<object::Archive
> OB(std::move(Ar
), std::move(ArBuf
));
510 EE
->addArchive(std::move(OB
));
513 // If the target is Cygwin/MingW and we are generating remote code, we
514 // need an extra module to help out with linking.
515 if (RemoteMCJIT
&& Triple(Mod
->getTargetTriple()).isOSCygMing()) {
516 addCygMingExtraModule(*EE
, Context
, Mod
->getTargetTriple());
519 // The following functions have no effect if their respective profiling
520 // support wasn't enabled in the build configuration.
521 EE
->RegisterJITEventListener(
522 JITEventListener::createOProfileJITEventListener());
523 EE
->RegisterJITEventListener(
524 JITEventListener::createIntelJITEventListener());
526 EE
->RegisterJITEventListener(
527 JITEventListener::createPerfJITEventListener());
529 if (!NoLazyCompilation
&& RemoteMCJIT
) {
530 WithColor::warning(errs(), argv
[0])
531 << "remote mcjit does not support lazy compilation\n";
532 NoLazyCompilation
= true;
534 EE
->DisableLazyCompilation(NoLazyCompilation
);
536 // If the user specifically requested an argv[0] to pass into the program,
538 if (!FakeArgv0
.empty()) {
539 InputFile
= static_cast<std::string
>(FakeArgv0
);
541 // Otherwise, if there is a .bc suffix on the executable strip it off, it
542 // might confuse the program.
543 if (StringRef(InputFile
).endswith(".bc"))
544 InputFile
.erase(InputFile
.length() - 3);
547 // Add the module's name to the start of the vector of arguments to main().
548 InputArgv
.insert(InputArgv
.begin(), InputFile
);
550 // Call the main function from M as if its signature were:
551 // int main (int argc, char **argv, const char **envp)
552 // using the contents of Args to determine argc & argv, and the contents of
553 // EnvVars to determine envp.
555 Function
*EntryFn
= Mod
->getFunction(EntryFunc
);
557 WithColor::error(errs(), argv
[0])
558 << '\'' << EntryFunc
<< "\' function not found in module.\n";
562 // Reset errno to zero on entry to main.
567 // Sanity check use of remote-jit: LLI currently only supports use of the
568 // remote JIT on Unix platforms.
571 WithColor::warning(errs(), argv
[0])
572 << "host does not support external remote targets.\n";
573 WithColor::note() << "defaulting to local execution\n";
576 if (ChildExecPath
.empty()) {
577 WithColor::error(errs(), argv
[0])
578 << "-remote-mcjit requires -mcjit-remote-process.\n";
580 } else if (!sys::fs::can_execute(ChildExecPath
)) {
581 WithColor::error(errs(), argv
[0])
582 << "unable to find usable child executable: '" << ChildExecPath
590 // If the program doesn't explicitly call exit, we will need the Exit
591 // function later on to make an explicit call, so get the function now.
592 Constant
*Exit
= Mod
->getOrInsertFunction("exit", Type::getVoidTy(Context
),
593 Type::getInt32Ty(Context
));
595 // Run static constructors.
596 if (!ForceInterpreter
) {
597 // Give MCJIT a chance to apply relocations and set page permissions.
598 EE
->finalizeObject();
600 EE
->runStaticConstructorsDestructors(false);
602 // Trigger compilation separately so code regions that need to be
603 // invalidated will be known.
604 (void)EE
->getPointerToFunction(EntryFn
);
605 // Clear instruction cache before code will be executed.
607 static_cast<SectionMemoryManager
*>(RTDyldMM
)->invalidateInstructionCache();
610 Result
= EE
->runFunctionAsMain(EntryFn
, InputArgv
, envp
);
612 // Run static destructors.
613 EE
->runStaticConstructorsDestructors(true);
615 // If the program didn't call exit explicitly, we should call it now.
616 // This ensures that any atexit handlers get called correctly.
617 if (Function
*ExitF
= dyn_cast
<Function
>(Exit
)) {
618 std::vector
<GenericValue
> Args
;
619 GenericValue ResultGV
;
620 ResultGV
.IntVal
= APInt(32, Result
);
621 Args
.push_back(ResultGV
);
622 EE
->runFunction(ExitF
, Args
);
623 WithColor::error(errs(), argv
[0]) << "exit(" << Result
<< ") returned!\n";
626 WithColor::error(errs(), argv
[0])
627 << "exit defined with wrong prototype!\n";
631 // else == "if (RemoteMCJIT)"
633 // Remote target MCJIT doesn't (yet) support static constructors. No reason
634 // it couldn't. This is a limitation of the LLI implementation, not the
635 // MCJIT itself. FIXME.
637 // Lanch the remote process and get a channel to it.
638 std::unique_ptr
<FDRawChannel
> C
= launchRemote();
640 WithColor::error(errs(), argv
[0]) << "failed to launch remote JIT.\n";
644 // Create a remote target client running over the channel.
645 llvm::orc::ExecutionSession ES
;
646 ES
.setErrorReporter([&](Error Err
) { ExitOnErr(std::move(Err
)); });
647 typedef orc::remote::OrcRemoteTargetClient MyRemote
;
648 auto R
= ExitOnErr(MyRemote::Create(*C
, ES
));
650 // Create a remote memory manager.
651 auto RemoteMM
= ExitOnErr(R
->createRemoteMemoryManager());
653 // Forward MCJIT's memory manager calls to the remote memory manager.
654 static_cast<ForwardingMemoryManager
*>(RTDyldMM
)->setMemMgr(
655 std::move(RemoteMM
));
657 // Forward MCJIT's symbol resolution calls to the remote.
658 static_cast<ForwardingMemoryManager
*>(RTDyldMM
)->setResolver(
659 orc::createLambdaResolver(
660 [](const std::string
&Name
) { return nullptr; },
661 [&](const std::string
&Name
) {
662 if (auto Addr
= ExitOnErr(R
->getSymbolAddress(Name
)))
663 return JITSymbol(Addr
, JITSymbolFlags::Exported
);
664 return JITSymbol(nullptr);
667 // Grab the target address of the JIT'd main function on the remote and call
669 // FIXME: argv and envp handling.
670 JITTargetAddress Entry
= EE
->getFunctionAddress(EntryFn
->getName().str());
671 EE
->finalizeObject();
672 LLVM_DEBUG(dbgs() << "Executing '" << EntryFn
->getName() << "' at 0x"
673 << format("%llx", Entry
) << "\n");
674 Result
= ExitOnErr(R
->callIntVoid(Entry
));
676 // Like static constructors, the remote target MCJIT support doesn't handle
677 // this yet. It could. FIXME.
679 // Delete the EE - we need to tear it down *before* we terminate the session
680 // with the remote, otherwise it'll crash when it tries to release resources
681 // on a remote that has already been disconnected.
684 // Signal the remote target that we're done JITing.
685 ExitOnErr(R
->terminateSession());
691 static orc::IRTransformLayer2::TransformFunction
createDebugDumper() {
692 switch (OrcDumpKind
) {
693 case DumpKind::NoDump
:
694 return [](std::unique_ptr
<Module
> M
) { return M
; };
696 case DumpKind::DumpFuncsToStdOut
:
697 return [](std::unique_ptr
<Module
> M
) {
700 for (const auto &F
: *M
) {
701 if (F
.isDeclaration())
705 std::string
Name(F
.getName());
706 printf("%s ", Name
.c_str());
715 case DumpKind::DumpModsToStdOut
:
716 return [](std::unique_ptr
<Module
> M
) {
717 outs() << "----- Module Start -----\n"
718 << *M
<< "----- Module End -----\n";
723 case DumpKind::DumpModsToDisk
:
724 return [](std::unique_ptr
<Module
> M
) {
726 raw_fd_ostream
Out(M
->getModuleIdentifier() + ".ll", EC
, sys::fs::F_Text
);
728 errs() << "Couldn't open " << M
->getModuleIdentifier()
729 << " for dumping.\nError:" << EC
.message() << "\n";
736 llvm_unreachable("Unknown DumpKind");
739 int runOrcLazyJIT(LLVMContext
&Ctx
, std::vector
<std::unique_ptr
<Module
>> Ms
,
740 const std::vector
<std::string
> &Args
) {
741 // Bail out early if no modules loaded.
745 // Add lli's symbols into the JIT's search space.
747 sys::DynamicLibrary LibLLI
=
748 sys::DynamicLibrary::getPermanentLibrary(nullptr, &ErrMsg
);
749 if (!LibLLI
.isValid()) {
750 errs() << "Error loading lli symbols: " << ErrMsg
<< ".\n";
754 const auto &TT
= Ms
.front()->getTargetTriple();
755 orc::JITTargetMachineBuilder TMD
=
756 TT
.empty() ? ExitOnErr(orc::JITTargetMachineBuilder::detectHost())
757 : orc::JITTargetMachineBuilder(Triple(TT
));
761 .addFeatures(getFeatureList())
762 .setRelocationModel(RelocModel
.getNumOccurrences()
763 ? Optional
<Reloc::Model
>(RelocModel
)
765 .setCodeModel(CMModel
.getNumOccurrences()
766 ? Optional
<CodeModel::Model
>(CMModel
)
768 auto TM
= ExitOnErr(TMD
.createTargetMachine());
769 auto DL
= TM
->createDataLayout();
770 auto ES
= llvm::make_unique
<orc::ExecutionSession
>();
772 ExitOnErr(orc::LLLazyJIT::Create(std::move(ES
), std::move(TM
), DL
, Ctx
));
774 auto Dump
= createDebugDumper();
776 J
->setLazyCompileTransform(
777 [&](std::unique_ptr
<Module
> M
) {
778 if (verifyModule(*M
, &dbgs())) {
779 dbgs() << "Bad module: " << *M
<< "\n";
782 return Dump(std::move(M
));
784 J
->getMainJITDylib().setFallbackDefinitionGenerator(
785 orc::DynamicLibraryFallbackGenerator(
786 std::move(LibLLI
), DL
, [](orc::SymbolStringPtr
) { return true; }));
788 orc::MangleAndInterner
Mangle(J
->getExecutionSession(), DL
);
789 orc::LocalCXXRuntimeOverrides2 CXXRuntimeOverrides
;
790 ExitOnErr(CXXRuntimeOverrides
.enable(J
->getMainJITDylib(), Mangle
));
793 orc::makeAllSymbolsExternallyAccessible(*M
);
794 ExitOnErr(J
->addLazyIRModule(std::move(M
)));
797 ExitOnErr(J
->runConstructors());
799 auto MainSym
= ExitOnErr(J
->lookup("main"));
800 typedef int (*MainFnPtr
)(int, const char *[]);
801 std::vector
<const char *> ArgV
;
802 for (auto &Arg
: Args
)
803 ArgV
.push_back(Arg
.c_str());
805 reinterpret_cast<MainFnPtr
>(static_cast<uintptr_t>(MainSym
.getAddress()));
806 auto Result
= Main(ArgV
.size(), (const char **)ArgV
.data());
808 ExitOnErr(J
->runDestructors());
810 CXXRuntimeOverrides
.runDestructors();
815 std::unique_ptr
<FDRawChannel
> launchRemote() {
817 llvm_unreachable("launchRemote not supported on non-Unix platforms");
823 if (pipe(PipeFD
[0]) != 0 || pipe(PipeFD
[1]) != 0)
824 perror("Error creating pipe: ");
831 // Close the parent ends of the pipes
836 // Execute the child process.
837 std::unique_ptr
<char[]> ChildPath
, ChildIn
, ChildOut
;
839 ChildPath
.reset(new char[ChildExecPath
.size() + 1]);
840 std::copy(ChildExecPath
.begin(), ChildExecPath
.end(), &ChildPath
[0]);
841 ChildPath
[ChildExecPath
.size()] = '\0';
842 std::string ChildInStr
= utostr(PipeFD
[0][0]);
843 ChildIn
.reset(new char[ChildInStr
.size() + 1]);
844 std::copy(ChildInStr
.begin(), ChildInStr
.end(), &ChildIn
[0]);
845 ChildIn
[ChildInStr
.size()] = '\0';
846 std::string ChildOutStr
= utostr(PipeFD
[1][1]);
847 ChildOut
.reset(new char[ChildOutStr
.size() + 1]);
848 std::copy(ChildOutStr
.begin(), ChildOutStr
.end(), &ChildOut
[0]);
849 ChildOut
[ChildOutStr
.size()] = '\0';
852 char * const args
[] = { &ChildPath
[0], &ChildIn
[0], &ChildOut
[0], nullptr };
853 int rc
= execv(ChildExecPath
.c_str(), args
);
855 perror("Error executing child process: ");
856 llvm_unreachable("Error executing child process");
858 // else we're the parent...
860 // Close the child ends of the pipes
864 // Return an RPC channel connected to our end of the pipes.
865 return llvm::make_unique
<FDRawChannel
>(PipeFD
[1][0], PipeFD
[0][1]);