[PowerPC] Recommit r340016 after fixing the reported issue
[llvm-core.git] / tools / lli / lli.cpp
blob7c2360b02474eded8369efd4a67447f8f5e25922
1 //===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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"
60 #include <cerrno>
62 #ifdef __CYGWIN__
63 #include <cygwin/version.h>
64 #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
65 #define DO_NOTHING_ATEXIT 1
66 #endif
67 #endif
69 using namespace llvm;
71 #define DEBUG_TYPE "lli"
73 namespace {
75 enum class JITKind { MCJIT, OrcMCJITReplacement, OrcLazy };
77 cl::opt<std::string>
78 InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
80 cl::list<std::string>
81 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
83 cl::opt<bool> ForceInterpreter("force-interpreter",
84 cl::desc("Force interpretation: disable JIT"),
85 cl::init(false));
87 cl::opt<JITKind> UseJITKind("jit-kind",
88 cl::desc("Choose underlying JIT kind."),
89 cl::init(JITKind::MCJIT),
90 cl::values(
91 clEnumValN(JITKind::MCJIT, "mcjit",
92 "MCJIT"),
93 clEnumValN(JITKind::OrcMCJITReplacement,
94 "orc-mcjit",
95 "Orc-based MCJIT replacement"),
96 clEnumValN(JITKind::OrcLazy,
97 "orc-lazy",
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."),
105 cl::init(false));
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.
111 cl::opt<std::string>
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.
119 cl::opt<char>
120 OptLevel("O",
121 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
122 "(default = '-O2')"),
123 cl::Prefix,
124 cl::ZeroOrMore,
125 cl::init(' '));
127 cl::opt<std::string>
128 TargetTriple("mtriple", cl::desc("Override target triple for module"));
130 cl::opt<std::string>
131 EntryFunc("entry-function",
132 cl::desc("Specify the entry function (default = 'main') "
133 "of the executable"),
134 cl::value_desc("function"),
135 cl::init("main"));
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"));
152 cl::opt<bool>
153 EnableCacheManager("enable-cache-manager",
154 cl::desc("Use cache manager to save/load mdoules"),
155 cl::init(false));
157 cl::opt<std::string>
158 ObjectCacheDir("object-cache-dir",
159 cl::desc("Directory to store cached object files "
160 "(must be user writable)"),
161 cl::init(""));
163 cl::opt<std::string>
164 FakeArgv0("fake-argv0",
165 cl::desc("Override the 'argv[0]' value passed into the executing"
166 " program"), cl::value_desc("executable"));
168 cl::opt<bool>
169 DisableCoreFiles("disable-core-files", cl::Hidden,
170 cl::desc("Disable emission of core files if possible"));
172 cl::opt<bool>
173 NoLazyCompilation("disable-lazy-compilation",
174 cl::desc("Disable JIT lazy compilation"),
175 cl::init(false));
177 cl::opt<bool>
178 GenerateSoftFloatCalls("soft-float",
179 cl::desc("Generate software floating point library calls"),
180 cl::init(false));
182 enum class DumpKind {
183 NoDump,
184 DumpFuncsToStdOut,
185 DumpModsToStdOut,
186 DumpModsToDisk
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).")),
202 cl::Hidden);
204 ExitOnError ExitOnErr;
207 //===----------------------------------------------------------------------===//
208 // Object cache
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 {
217 public:
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))
230 return;
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));
235 std::error_code EC;
236 raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
237 outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
238 outfile.close();
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))
245 return nullptr;
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.
250 if (!IRObjectBuffer)
251 return nullptr;
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
255 // out of scope.
256 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
259 private:
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)
266 return false;
267 std::string CacheSubdir = ModID.substr(PrefixLength);
268 #if defined(_WIN32)
269 // Transform "X:\foo" => "/X\foo" for convenience.
270 if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
271 CacheSubdir[1] = CacheSubdir[0];
272 CacheSubdir[0] = '/';
274 #endif
275 CacheName = CacheDir + CacheSubdir;
276 size_t pos = CacheName.rfind('.');
277 CacheName.replace(pos, CacheName.length() - pos, ".o");
278 return true;
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".
297 Function *Result;
298 if (TargetTriple.isArch64Bit()) {
299 Result = Function::Create(
300 TypeBuilder<int64_t(void), false>::get(Context),
301 GlobalValue::ExternalLinkage, "__main", M.get());
302 } else {
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);
309 Value *ReturnVal;
310 if (TargetTriple.isArch64Bit())
311 ReturnVal = ConstantInt::get(Context, APInt(64, 0));
312 else
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() {
321 switch (OptLevel) {
322 default:
323 WithColor::error(errs(), "lli") << "invalid optimization level.\n";
324 exit(1);
325 case '0': return CodeGenOpt::None;
326 case '1': return CodeGenOpt::Less;
327 case ' ':
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());
337 exit(1);
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);
349 if (argc > 1)
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();
365 LLVMContext Context;
367 // Load the bitcode...
368 SMDiagnostic Err;
369 std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context);
370 Module *Mod = Owner.get();
371 if (!Mod)
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));
379 if (!Ms.back())
380 reportError(Err, argv[0]);
382 std::vector<std::string> Args;
383 Args.push_back(InputFile);
384 for (auto &Arg : InputArgv)
385 Args.push_back(Arg);
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
415 : EngineKind::JIT);
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) {
425 if (RemoteMCJIT)
426 RTDyldMM = new ForwardingMemoryManager();
427 else
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";
437 exit(1);
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());
449 if (!EE) {
450 if (!ErrorMsg.empty())
451 WithColor::error(errs(), argv[0])
452 << "error creating EE: " << ErrorMsg << "\n";
453 else
454 WithColor::error(errs(), argv[0]) << "unknown error creating EE!\n";
455 exit(1);
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);
467 if (!XMod)
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]);
480 if (!Obj) {
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]);
492 if (!ArBufOrErr)
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());
498 if (!ArOrErr) {
499 std::string Buf;
500 raw_string_ostream OS(Buf);
501 logAllUnhandledErrors(ArOrErr.takeError(), OS, "");
502 OS.flush();
503 errs() << Buf;
504 exit(1);
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());
525 if (!RemoteMCJIT)
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,
537 // do it now.
538 if (!FakeArgv0.empty()) {
539 InputFile = static_cast<std::string>(FakeArgv0);
540 } else {
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);
556 if (!EntryFn) {
557 WithColor::error(errs(), argv[0])
558 << '\'' << EntryFunc << "\' function not found in module.\n";
559 return -1;
562 // Reset errno to zero on entry to main.
563 errno = 0;
565 int Result = -1;
567 // Sanity check use of remote-jit: LLI currently only supports use of the
568 // remote JIT on Unix platforms.
569 if (RemoteMCJIT) {
570 #ifndef LLVM_ON_UNIX
571 WithColor::warning(errs(), argv[0])
572 << "host does not support external remote targets.\n";
573 WithColor::note() << "defaulting to local execution\n";
574 return -1;
575 #else
576 if (ChildExecPath.empty()) {
577 WithColor::error(errs(), argv[0])
578 << "-remote-mcjit requires -mcjit-remote-process.\n";
579 exit(1);
580 } else if (!sys::fs::can_execute(ChildExecPath)) {
581 WithColor::error(errs(), argv[0])
582 << "unable to find usable child executable: '" << ChildExecPath
583 << "'\n";
584 return -1;
586 #endif
589 if (!RemoteMCJIT) {
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.
606 if (RTDyldMM)
607 static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
609 // Run main.
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";
624 abort();
625 } else {
626 WithColor::error(errs(), argv[0])
627 << "exit defined with wrong prototype!\n";
628 abort();
630 } else {
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();
639 if (!C) {
640 WithColor::error(errs(), argv[0]) << "failed to launch remote JIT.\n";
641 exit(1);
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);
665 }));
667 // Grab the target address of the JIT'd main function on the remote and call
668 // it.
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.
682 EE.reset();
684 // Signal the remote target that we're done JITing.
685 ExitOnErr(R->terminateSession());
688 return Result;
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) {
698 printf("[ ");
700 for (const auto &F : *M) {
701 if (F.isDeclaration())
702 continue;
704 if (F.hasName()) {
705 std::string Name(F.getName());
706 printf("%s ", Name.c_str());
707 } else
708 printf("<anon> ");
711 printf("]\n");
712 return M;
715 case DumpKind::DumpModsToStdOut:
716 return [](std::unique_ptr<Module> M) {
717 outs() << "----- Module Start -----\n"
718 << *M << "----- Module End -----\n";
720 return M;
723 case DumpKind::DumpModsToDisk:
724 return [](std::unique_ptr<Module> M) {
725 std::error_code EC;
726 raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC, sys::fs::F_Text);
727 if (EC) {
728 errs() << "Couldn't open " << M->getModuleIdentifier()
729 << " for dumping.\nError:" << EC.message() << "\n";
730 exit(1);
732 Out << *M;
733 return M;
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.
742 if (Ms.empty())
743 return 0;
745 // Add lli's symbols into the JIT's search space.
746 std::string ErrMsg;
747 sys::DynamicLibrary LibLLI =
748 sys::DynamicLibrary::getPermanentLibrary(nullptr, &ErrMsg);
749 if (!LibLLI.isValid()) {
750 errs() << "Error loading lli symbols: " << ErrMsg << ".\n";
751 return 1;
754 const auto &TT = Ms.front()->getTargetTriple();
755 orc::JITTargetMachineBuilder TMD =
756 TT.empty() ? ExitOnErr(orc::JITTargetMachineBuilder::detectHost())
757 : orc::JITTargetMachineBuilder(Triple(TT));
759 TMD.setArch(MArch)
760 .setCPU(getCPUStr())
761 .addFeatures(getFeatureList())
762 .setRelocationModel(RelocModel.getNumOccurrences()
763 ? Optional<Reloc::Model>(RelocModel)
764 : None)
765 .setCodeModel(CMModel.getNumOccurrences()
766 ? Optional<CodeModel::Model>(CMModel)
767 : None);
768 auto TM = ExitOnErr(TMD.createTargetMachine());
769 auto DL = TM->createDataLayout();
770 auto ES = llvm::make_unique<orc::ExecutionSession>();
771 auto J =
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";
780 exit(1);
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));
792 for (auto &M : Ms) {
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());
804 auto Main =
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();
812 return Result;
815 std::unique_ptr<FDRawChannel> launchRemote() {
816 #ifndef LLVM_ON_UNIX
817 llvm_unreachable("launchRemote not supported on non-Unix platforms");
818 #else
819 int PipeFD[2][2];
820 pid_t ChildPID;
822 // Create two pipes.
823 if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
824 perror("Error creating pipe: ");
826 ChildPID = fork();
828 if (ChildPID == 0) {
829 // In the child...
831 // Close the parent ends of the pipes
832 close(PipeFD[0][1]);
833 close(PipeFD[1][0]);
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);
854 if (rc != 0)
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
861 close(PipeFD[0][0]);
862 close(PipeFD[1][1]);
864 // Return an RPC channel connected to our end of the pipes.
865 return llvm::make_unique<FDRawChannel>(PipeFD[1][0], PipeFD[0][1]);
866 #endif