1 //===------ OrcLazyJIT.cpp - Basic Orc-based JIT for lazy execution -------===//
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 #include "OrcLazyJIT.h"
11 #include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/DynamicLibrary.h"
15 #include <system_error>
21 enum class DumpKind { NoDump, DumpFuncsToStdOut, DumpModsToStdOut,
24 cl::opt<DumpKind> OrcDumpKind("orc-lazy-debug",
25 cl::desc("Debug dumping for the orc-lazy JIT."),
26 cl::init(DumpKind::NoDump),
28 clEnumValN(DumpKind::NoDump, "no-dump",
29 "Don't dump anything."),
30 clEnumValN(DumpKind::DumpFuncsToStdOut,
32 "Dump function names to stdout."),
33 clEnumValN(DumpKind::DumpModsToStdOut,
35 "Dump modules to stdout."),
36 clEnumValN(DumpKind::DumpModsToDisk,
38 "Dump modules to the current "
39 "working directory. (WARNING: "
40 "will overwrite existing files).")),
43 cl::opt<bool> OrcInlineStubs("orc-lazy-inline-stubs",
44 cl::desc("Try to inline stubs"),
45 cl::init(true), cl::Hidden);
48 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
50 switch (OrcDumpKind) {
51 case DumpKind::NoDump:
52 return [](std::unique_ptr<Module> M) { return M; };
54 case DumpKind::DumpFuncsToStdOut:
55 return [](std::unique_ptr<Module> M) {
58 for (const auto &F : *M) {
59 if (F.isDeclaration())
63 std::string Name(F.getName());
64 printf("%s ", Name.c_str());
73 case DumpKind::DumpModsToStdOut:
74 return [](std::unique_ptr<Module> M) {
75 outs() << "----- Module Start -----\n" << *M
76 << "----- Module End -----\n";
81 case DumpKind::DumpModsToDisk:
82 return [](std::unique_ptr<Module> M) {
84 raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
87 errs() << "Couldn't open " << M->getModuleIdentifier()
88 << " for dumping.\nError:" << EC.message() << "\n";
95 llvm_unreachable("Unknown DumpKind");
98 // Defined in lli.cpp.
99 CodeGenOpt::Level getOptLevel();
102 template <typename PtrTy>
103 static PtrTy fromTargetAddress(JITTargetAddress Addr) {
104 return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
107 int llvm::runOrcLazyJIT(std::vector<std::unique_ptr<Module>> Ms,
108 const std::vector<std::string> &Args) {
109 // Add the program's symbols into the JIT's search space.
110 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
111 errs() << "Error loading program symbols.\n";
115 // Grab a target machine and try to build a factory function for the
116 // target-specific Orc callback manager.
118 EB.setOptLevel(getOptLevel());
119 auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
120 Triple T(TM->getTargetTriple());
121 auto CompileCallbackMgr = orc::createLocalCompileCallbackManager(T, 0);
123 // If we couldn't build the factory function then there must not be a callback
124 // manager for this target. Bail out.
125 if (!CompileCallbackMgr) {
126 errs() << "No callback manager available for target '"
127 << TM->getTargetTriple().str() << "'.\n";
131 auto IndirectStubsMgrBuilder = orc::createLocalIndirectStubsManagerBuilder(T);
133 // If we couldn't build a stubs-manager-builder for this target then bail out.
134 if (!IndirectStubsMgrBuilder) {
135 errs() << "No indirect stubs manager available for target '"
136 << TM->getTargetTriple().str() << "'.\n";
140 // Everything looks good. Build the JIT.
141 OrcLazyJIT J(std::move(TM), std::move(CompileCallbackMgr),
142 std::move(IndirectStubsMgrBuilder),
145 // Add the module, look up main and run it.
146 J.addModuleSet(std::move(Ms));
147 auto MainSym = J.findSymbol("main");
150 errs() << "Could not find main function.\n";
154 typedef int (*MainFnPtr)(int, const char*[]);
155 std::vector<const char *> ArgV;
156 for (auto &Arg : Args)
157 ArgV.push_back(Arg.c_str());
158 auto Main = fromTargetAddress<MainFnPtr>(MainSym.getAddress());
159 return Main(ArgV.size(), (const char**)ArgV.data());