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 "llvm/IR/LLVMContext.h"
17 #include "RemoteMemoryManager.h"
18 #include "RemoteTarget.h"
19 #include "RemoteTargetExternal.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/CodeGen/LinkAllCodegenComponents.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/SectionMemoryManager.h"
29 #include "llvm/IR/IRBuilder.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/Type.h"
32 #include "llvm/IR/TypeBuilder.h"
33 #include "llvm/IRReader/IRReader.h"
34 #include "llvm/Object/Archive.h"
35 #include "llvm/Object/ObjectFile.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/DynamicLibrary.h"
39 #include "llvm/Support/Format.h"
40 #include "llvm/Support/ManagedStatic.h"
41 #include "llvm/Support/MathExtras.h"
42 #include "llvm/Support/Memory.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/PluginLoader.h"
45 #include "llvm/Support/PrettyStackTrace.h"
46 #include "llvm/Support/Process.h"
47 #include "llvm/Support/Program.h"
48 #include "llvm/Support/Signals.h"
49 #include "llvm/Support/SourceMgr.h"
50 #include "llvm/Support/TargetSelect.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include "llvm/Transforms/Instrumentation.h"
56 #include <cygwin/version.h>
57 #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
58 #define DO_NOTHING_ATEXIT 1
64 #define DEBUG_TYPE "lli"
68 InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
71 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
73 cl::opt<bool> ForceInterpreter("force-interpreter",
74 cl::desc("Force interpretation: disable JIT"),
77 cl::opt<bool> DebugIR(
78 "debug-ir", cl::desc("Generate debug information to allow debugging IR."),
81 // The MCJIT supports building for a target address space separate from
82 // the JIT compilation process. Use a forked process and a copying
83 // memory manager with IPC to execute using this functionality.
84 cl::opt<bool> RemoteMCJIT("remote-mcjit",
85 cl::desc("Execute MCJIT'ed code in a separate process."),
88 // Manually specify the child process for remote execution. This overrides
89 // the simulated remote execution that allocates address space for child
90 // execution. The child process will be executed and will communicate with
91 // lli via stdin/stdout pipes.
93 ChildExecPath("mcjit-remote-process",
94 cl::desc("Specify the filename of the process to launch "
95 "for remote MCJIT execution. If none is specified,"
96 "\n\tremote execution will be simulated in-process."),
97 cl::value_desc("filename"), cl::init(""));
99 // Determine optimization level.
102 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
103 "(default = '-O2')"),
109 TargetTriple("mtriple", cl::desc("Override target triple for module"));
113 cl::desc("Architecture to generate assembly for (see --version)"));
117 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
118 cl::value_desc("cpu-name"),
121 cl::list<std::string>
124 cl::desc("Target specific attributes (-mattr=help for details)"),
125 cl::value_desc("a1,+a2,-a3,..."));
128 EntryFunc("entry-function",
129 cl::desc("Specify the entry function (default = 'main') "
130 "of the executable"),
131 cl::value_desc("function"),
134 cl::list<std::string>
135 ExtraModules("extra-module",
136 cl::desc("Extra modules to be loaded"),
137 cl::value_desc("input bitcode"));
139 cl::list<std::string>
140 ExtraObjects("extra-object",
141 cl::desc("Extra object files to be loaded"),
142 cl::value_desc("input object"));
144 cl::list<std::string>
145 ExtraArchives("extra-archive",
146 cl::desc("Extra archive files to be loaded"),
147 cl::value_desc("input archive"));
150 EnableCacheManager("enable-cache-manager",
151 cl::desc("Use cache manager to save/load mdoules"),
155 ObjectCacheDir("object-cache-dir",
156 cl::desc("Directory to store cached object files "
157 "(must be user writable)"),
161 FakeArgv0("fake-argv0",
162 cl::desc("Override the 'argv[0]' value passed into the executing"
163 " program"), cl::value_desc("executable"));
166 DisableCoreFiles("disable-core-files", cl::Hidden,
167 cl::desc("Disable emission of core files if possible"));
170 NoLazyCompilation("disable-lazy-compilation",
171 cl::desc("Disable JIT lazy compilation"),
174 cl::opt<Reloc::Model>
175 RelocModel("relocation-model",
176 cl::desc("Choose relocation model"),
177 cl::init(Reloc::Default),
179 clEnumValN(Reloc::Default, "default",
180 "Target default relocation model"),
181 clEnumValN(Reloc::Static, "static",
182 "Non-relocatable code"),
183 clEnumValN(Reloc::PIC_, "pic",
184 "Fully relocatable, position independent code"),
185 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
186 "Relocatable external references, non-relocatable code"),
189 cl::opt<llvm::CodeModel::Model>
190 CMModel("code-model",
191 cl::desc("Choose code model"),
192 cl::init(CodeModel::JITDefault),
193 cl::values(clEnumValN(CodeModel::JITDefault, "default",
194 "Target default JIT code model"),
195 clEnumValN(CodeModel::Small, "small",
197 clEnumValN(CodeModel::Kernel, "kernel",
198 "Kernel code model"),
199 clEnumValN(CodeModel::Medium, "medium",
200 "Medium code model"),
201 clEnumValN(CodeModel::Large, "large",
206 GenerateSoftFloatCalls("soft-float",
207 cl::desc("Generate software floating point library calls"),
210 cl::opt<llvm::FloatABI::ABIType>
211 FloatABIForCalls("float-abi",
212 cl::desc("Choose float ABI type"),
213 cl::init(FloatABI::Default),
215 clEnumValN(FloatABI::Default, "default",
216 "Target default float ABI type"),
217 clEnumValN(FloatABI::Soft, "soft",
218 "Soft float ABI (implied by -soft-float)"),
219 clEnumValN(FloatABI::Hard, "hard",
220 "Hard float ABI (uses FP registers)"),
223 // In debug builds, make this default to true.
225 #define EMIT_DEBUG false
227 #define EMIT_DEBUG true
229 EmitJitDebugInfo("jit-emit-debug",
230 cl::desc("Emit debug information to debugger"),
231 cl::init(EMIT_DEBUG));
235 EmitJitDebugInfoToDisk("jit-emit-debug-to-disk",
237 cl::desc("Emit debug info objfiles to disk"),
241 //===----------------------------------------------------------------------===//
244 // This object cache implementation writes cached objects to disk to the
245 // directory specified by CacheDir, using a filename provided in the module
246 // descriptor. The cache tries to load a saved object using that path if the
247 // file exists. CacheDir defaults to "", in which case objects are cached
248 // alongside their originating bitcodes.
250 class LLIObjectCache : public ObjectCache {
252 LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
253 // Add trailing '/' to cache dir if necessary.
254 if (!this->CacheDir.empty() &&
255 this->CacheDir[this->CacheDir.size() - 1] != '/')
256 this->CacheDir += '/';
258 virtual ~LLIObjectCache() {}
260 void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
261 const std::string ModuleID = M->getModuleIdentifier();
262 std::string CacheName;
263 if (!getCacheFilename(ModuleID, CacheName))
265 if (!CacheDir.empty()) { // Create user-defined cache dir.
266 SmallString<128> dir(CacheName);
267 sys::path::remove_filename(dir);
268 sys::fs::create_directories(Twine(dir));
271 raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
272 outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
276 std::unique_ptr<MemoryBuffer> getObject(const Module* M) override {
277 const std::string ModuleID = M->getModuleIdentifier();
278 std::string CacheName;
279 if (!getCacheFilename(ModuleID, CacheName))
281 // Load the object from the cache filename
282 ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer =
283 MemoryBuffer::getFile(CacheName.c_str(), -1, false);
284 // If the file isn't there, that's OK.
287 // MCJIT will want to write into this buffer, and we don't want that
288 // because the file has probably just been mmapped. Instead we make
289 // a copy. The filed-based buffer will be released when it goes
291 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
295 std::string CacheDir;
297 bool getCacheFilename(const std::string &ModID, std::string &CacheName) {
298 std::string Prefix("file:");
299 size_t PrefixLength = Prefix.length();
300 if (ModID.substr(0, PrefixLength) != Prefix)
302 std::string CacheSubdir = ModID.substr(PrefixLength);
304 // Transform "X:\foo" => "/X\foo" for convenience.
305 if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
306 CacheSubdir[1] = CacheSubdir[0];
307 CacheSubdir[0] = '/';
310 CacheName = CacheDir + CacheSubdir;
311 size_t pos = CacheName.rfind('.');
312 CacheName.replace(pos, CacheName.length() - pos, ".o");
317 static ExecutionEngine *EE = nullptr;
318 static LLIObjectCache *CacheManager = nullptr;
320 static void do_shutdown() {
321 // Cygwin-1.5 invokes DLL's dtors before atexit handler.
322 #ifndef DO_NOTHING_ATEXIT
330 // On Mingw and Cygwin, an external symbol named '__main' is called from the
331 // generated 'main' function to allow static intialization. To avoid linking
332 // problems with remote targets (because lli's remote target support does not
333 // currently handle external linking) we add a secondary module which defines
334 // an empty '__main' function.
335 static void addCygMingExtraModule(ExecutionEngine *EE,
336 LLVMContext &Context,
337 StringRef TargetTripleStr) {
338 IRBuilder<> Builder(Context);
339 Triple TargetTriple(TargetTripleStr);
341 // Create a new module.
342 std::unique_ptr<Module> M = make_unique<Module>("CygMingHelper", Context);
343 M->setTargetTriple(TargetTripleStr);
345 // Create an empty function named "__main".
347 if (TargetTriple.isArch64Bit()) {
348 Result = Function::Create(
349 TypeBuilder<int64_t(void), false>::get(Context),
350 GlobalValue::ExternalLinkage, "__main", M.get());
352 Result = Function::Create(
353 TypeBuilder<int32_t(void), false>::get(Context),
354 GlobalValue::ExternalLinkage, "__main", M.get());
356 BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
357 Builder.SetInsertPoint(BB);
359 if (TargetTriple.isArch64Bit())
360 ReturnVal = ConstantInt::get(Context, APInt(64, 0));
362 ReturnVal = ConstantInt::get(Context, APInt(32, 0));
363 Builder.CreateRet(ReturnVal);
365 // Add this new module to the ExecutionEngine.
366 EE->addModule(std::move(M));
370 //===----------------------------------------------------------------------===//
371 // main Driver function
373 int main(int argc, char **argv, char * const *envp) {
374 sys::PrintStackTraceOnErrorSignal();
375 PrettyStackTraceProgram X(argc, argv);
377 LLVMContext &Context = getGlobalContext();
378 atexit(do_shutdown); // Call llvm_shutdown() on exit.
380 // If we have a native target, initialize it to ensure it is linked in and
381 // usable by the JIT.
382 InitializeNativeTarget();
383 InitializeNativeTargetAsmPrinter();
384 InitializeNativeTargetAsmParser();
386 cl::ParseCommandLineOptions(argc, argv,
387 "llvm interpreter & dynamic compiler\n");
389 // If the user doesn't want core files, disable them.
390 if (DisableCoreFiles)
391 sys::Process::PreventCoreFiles();
393 // Load the bitcode...
395 std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context);
396 Module *Mod = Owner.get();
398 Err.print(argv[0], errs());
402 if (EnableCacheManager) {
403 std::string CacheName("file:");
404 CacheName.append(InputFile);
405 Mod->setModuleIdentifier(CacheName);
408 // If not jitting lazily, load the whole bitcode file eagerly too.
409 if (NoLazyCompilation) {
410 if (std::error_code EC = Mod->materializeAllPermanently()) {
411 errs() << argv[0] << ": bitcode didn't read correctly.\n";
412 errs() << "Reason: " << EC.message() << "\n";
418 ModulePass *DebugIRPass = createDebugIRPass();
419 DebugIRPass->runOnModule(*Mod);
422 std::string ErrorMsg;
423 EngineBuilder builder(std::move(Owner));
424 builder.setMArch(MArch);
425 builder.setMCPU(MCPU);
426 builder.setMAttrs(MAttrs);
427 builder.setRelocationModel(RelocModel);
428 builder.setCodeModel(CMModel);
429 builder.setErrorStr(&ErrorMsg);
430 builder.setEngineKind(ForceInterpreter
431 ? EngineKind::Interpreter
434 // If we are supposed to override the target triple, do so now.
435 if (!TargetTriple.empty())
436 Mod->setTargetTriple(Triple::normalize(TargetTriple));
438 // Enable MCJIT if desired.
439 RTDyldMemoryManager *RTDyldMM = nullptr;
440 if (!ForceInterpreter) {
442 RTDyldMM = new RemoteMemoryManager();
444 RTDyldMM = new SectionMemoryManager();
445 builder.setMCJITMemoryManager(RTDyldMM);
446 } else if (RemoteMCJIT) {
447 errs() << "error: Remote process execution does not work with the "
452 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
455 errs() << argv[0] << ": invalid optimization level.\n";
458 case '0': OLvl = CodeGenOpt::None; break;
459 case '1': OLvl = CodeGenOpt::Less; break;
460 case '2': OLvl = CodeGenOpt::Default; break;
461 case '3': OLvl = CodeGenOpt::Aggressive; break;
463 builder.setOptLevel(OLvl);
465 TargetOptions Options;
466 Options.UseSoftFloat = GenerateSoftFloatCalls;
467 if (FloatABIForCalls != FloatABI::Default)
468 Options.FloatABIType = FloatABIForCalls;
469 if (GenerateSoftFloatCalls)
470 FloatABIForCalls = FloatABI::Soft;
472 // Remote target execution doesn't handle EH or debug registration.
474 Options.JITEmitDebugInfo = EmitJitDebugInfo;
475 Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk;
478 builder.setTargetOptions(Options);
480 EE = builder.create();
482 if (!ErrorMsg.empty())
483 errs() << argv[0] << ": error creating EE: " << ErrorMsg << "\n";
485 errs() << argv[0] << ": unknown error creating EE!\n";
489 if (EnableCacheManager) {
490 CacheManager = new LLIObjectCache(ObjectCacheDir);
491 EE->setObjectCache(CacheManager);
494 // Load any additional modules specified on the command line.
495 for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
496 std::unique_ptr<Module> XMod = parseIRFile(ExtraModules[i], Err, Context);
498 Err.print(argv[0], errs());
501 if (EnableCacheManager) {
502 std::string CacheName("file:");
503 CacheName.append(ExtraModules[i]);
504 XMod->setModuleIdentifier(CacheName);
506 EE->addModule(std::move(XMod));
509 for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
510 ErrorOr<object::OwningBinary<object::ObjectFile>> Obj =
511 object::ObjectFile::createObjectFile(ExtraObjects[i]);
513 Err.print(argv[0], errs());
516 object::OwningBinary<object::ObjectFile> &O = Obj.get();
517 EE->addObjectFile(std::move(O));
520 for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
521 ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr =
522 MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]);
524 Err.print(argv[0], errs());
527 std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get();
529 ErrorOr<std::unique_ptr<object::Archive>> ArOrErr =
530 object::Archive::create(ArBuf->getMemBufferRef());
531 if (std::error_code EC = ArOrErr.getError()) {
532 errs() << EC.message();
535 std::unique_ptr<object::Archive> &Ar = ArOrErr.get();
537 object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf));
539 EE->addArchive(std::move(OB));
542 // If the target is Cygwin/MingW and we are generating remote code, we
543 // need an extra module to help out with linking.
544 if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) {
545 addCygMingExtraModule(EE, Context, Mod->getTargetTriple());
548 // The following functions have no effect if their respective profiling
549 // support wasn't enabled in the build configuration.
550 EE->RegisterJITEventListener(
551 JITEventListener::createOProfileJITEventListener());
552 EE->RegisterJITEventListener(
553 JITEventListener::createIntelJITEventListener());
555 if (!NoLazyCompilation && RemoteMCJIT) {
556 errs() << "warning: remote mcjit does not support lazy compilation\n";
557 NoLazyCompilation = true;
559 EE->DisableLazyCompilation(NoLazyCompilation);
561 // If the user specifically requested an argv[0] to pass into the program,
563 if (!FakeArgv0.empty()) {
564 InputFile = FakeArgv0;
566 // Otherwise, if there is a .bc suffix on the executable strip it off, it
567 // might confuse the program.
568 if (StringRef(InputFile).endswith(".bc"))
569 InputFile.erase(InputFile.length() - 3);
572 // Add the module's name to the start of the vector of arguments to main().
573 InputArgv.insert(InputArgv.begin(), InputFile);
575 // Call the main function from M as if its signature were:
576 // int main (int argc, char **argv, const char **envp)
577 // using the contents of Args to determine argc & argv, and the contents of
578 // EnvVars to determine envp.
580 Function *EntryFn = Mod->getFunction(EntryFunc);
582 errs() << '\'' << EntryFunc << "\' function not found in module.\n";
586 // Reset errno to zero on entry to main.
592 // If the program doesn't explicitly call exit, we will need the Exit
593 // function later on to make an explicit call, so get the function now.
594 Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context),
595 Type::getInt32Ty(Context),
598 // Run static constructors.
599 if (!ForceInterpreter) {
600 // Give MCJIT a chance to apply relocations and set page permissions.
601 EE->finalizeObject();
603 EE->runStaticConstructorsDestructors(false);
605 // Trigger compilation separately so code regions that need to be
606 // invalidated will be known.
607 (void)EE->getPointerToFunction(EntryFn);
608 // Clear instruction cache before code will be executed.
610 static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
613 Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp);
615 // Run static destructors.
616 EE->runStaticConstructorsDestructors(true);
618 // If the program didn't call exit explicitly, we should call it now.
619 // This ensures that any atexit handlers get called correctly.
620 if (Function *ExitF = dyn_cast<Function>(Exit)) {
621 std::vector<GenericValue> Args;
622 GenericValue ResultGV;
623 ResultGV.IntVal = APInt(32, Result);
624 Args.push_back(ResultGV);
625 EE->runFunction(ExitF, Args);
626 errs() << "ERROR: exit(" << Result << ") returned!\n";
629 errs() << "ERROR: exit defined with wrong prototype!\n";
633 // else == "if (RemoteMCJIT)"
635 // Remote target MCJIT doesn't (yet) support static constructors. No reason
636 // it couldn't. This is a limitation of the LLI implemantation, not the
637 // MCJIT itself. FIXME.
639 RemoteMemoryManager *MM = static_cast<RemoteMemoryManager*>(RTDyldMM);
640 // Everything is prepared now, so lay out our program for the target
641 // address space, assign the section addresses to resolve any relocations,
642 // and send it to the target.
644 std::unique_ptr<RemoteTarget> Target;
645 if (!ChildExecPath.empty()) { // Remote execution on a child process
647 // FIXME: Remove this pointless fallback mode which causes tests to "pass"
648 // on platforms where they should XFAIL.
649 errs() << "Warning: host does not support external remote targets.\n"
650 << " Defaulting to simulated remote execution\n";
651 Target.reset(new RemoteTarget);
653 if (!sys::fs::can_execute(ChildExecPath)) {
654 errs() << "Unable to find usable child executable: '" << ChildExecPath
658 Target.reset(new RemoteTargetExternal(ChildExecPath));
661 // No child process name provided, use simulated remote execution.
662 Target.reset(new RemoteTarget);
665 // Give the memory manager a pointer to our remote target interface object.
666 MM->setRemoteTarget(Target.get());
668 // Create the remote target.
669 if (!Target->create()) {
670 errs() << "ERROR: " << Target->getErrorMsg() << "\n";
674 // Since we're executing in a (at least simulated) remote address space,
675 // we can't use the ExecutionEngine::runFunctionAsMain(). We have to
676 // grab the function address directly here and tell the remote target
677 // to execute the function.
679 // Our memory manager will map generated code into the remote address
680 // space as it is loaded and copy the bits over during the finalizeMemory
683 // FIXME: argv and envp handling.
684 uint64_t Entry = EE->getFunctionAddress(EntryFn->getName().str());
686 DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x"
687 << format("%llx", Entry) << "\n");
689 if (!Target->executeCode(Entry, Result))
690 errs() << "ERROR: " << Target->getErrorMsg() << "\n";
692 // Like static constructors, the remote target MCJIT support doesn't handle
693 // this yet. It could. FIXME.
695 // Stop the remote target