edac10b86556624710ffbbc094c8f0bc12df9b09
[oota-llvm.git] / tools / lli / OrcLazyJIT.cpp
1 //===------ OrcLazyJIT.cpp - Basic Orc-based JIT for lazy execution -------===//
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 #include "OrcLazyJIT.h"
11 #include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/DynamicLibrary.h"
14 #include <cstdio>
15 #include <system_error>
16
17 using namespace llvm;
18
19 namespace {
20
21   enum class DumpKind { NoDump, DumpFuncsToStdOut, DumpModsToStdErr,
22                         DumpModsToDisk };
23
24   cl::opt<DumpKind> OrcDumpKind("orc-lazy-debug",
25                                 cl::desc("Debug dumping for the orc-lazy JIT."),
26                                 cl::init(DumpKind::NoDump),
27                                 cl::values(
28                                   clEnumValN(DumpKind::NoDump, "no-dump",
29                                              "Don't dump anything."),
30                                   clEnumValN(DumpKind::DumpFuncsToStdOut,
31                                              "funcs-to-stdout",
32                                              "Dump function names to stdout."),
33                                   clEnumValN(DumpKind::DumpModsToStdErr,
34                                              "mods-to-stderr",
35                                              "Dump modules to stderr."),
36                                   clEnumValN(DumpKind::DumpModsToDisk,
37                                              "mods-to-disk",
38                                              "Dump modules to the current "
39                                              "working directory. (WARNING: "
40                                              "will overwrite existing files)."),
41                                   clEnumValEnd),
42                                 cl::Hidden);
43
44   cl::opt<bool> OrcInlineStubs("orc-lazy-inline-stubs",
45                                cl::desc("Try to inline stubs"),
46                                cl::init(true), cl::Hidden);
47 }
48
49 std::unique_ptr<OrcLazyJIT::CompileCallbackMgr>
50 OrcLazyJIT::createCompileCallbackMgr(Triple T) {
51   switch (T.getArch()) {
52     default: return nullptr;
53
54     case Triple::x86_64: {
55       typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64> CCMgrT;
56       return llvm::make_unique<CCMgrT>(0);
57     }
58   }
59 }
60
61 OrcLazyJIT::IndirectStubsManagerBuilder
62 OrcLazyJIT::createIndirectStubsMgrBuilder(Triple T) {
63   switch (T.getArch()) {
64     default: return nullptr;
65
66     case Triple::x86_64:
67       return [](){
68         return llvm::make_unique<orc::IndirectStubsManager<orc::OrcX86_64>>();
69       };
70   }
71 }
72
73 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
74
75   switch (OrcDumpKind) {
76   case DumpKind::NoDump:
77     return [](std::unique_ptr<Module> M) { return M; };
78
79   case DumpKind::DumpFuncsToStdOut:
80     return [](std::unique_ptr<Module> M) {
81       printf("[ ");
82
83       for (const auto &F : *M) {
84         if (F.isDeclaration())
85           continue;
86
87         if (F.hasName()) {
88           std::string Name(F.getName());
89           printf("%s ", Name.c_str());
90         } else
91           printf("<anon> ");
92       }
93
94       printf("]\n");
95       return M;
96     };
97
98   case DumpKind::DumpModsToStdErr:
99     return [](std::unique_ptr<Module> M) {
100              dbgs() << "----- Module Start -----\n" << *M
101                     << "----- Module End -----\n";
102
103              return M;
104            };
105
106   case DumpKind::DumpModsToDisk:
107     return [](std::unique_ptr<Module> M) {
108              std::error_code EC;
109              raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
110                                 sys::fs::F_Text);
111              if (EC) {
112                errs() << "Couldn't open " << M->getModuleIdentifier()
113                       << " for dumping.\nError:" << EC.message() << "\n";
114                exit(1);
115              }
116              Out << *M;
117              return M;
118            };
119   }
120   llvm_unreachable("Unknown DumpKind");
121 }
122
123 // Defined in lli.cpp.
124 CodeGenOpt::Level getOptLevel();
125
126
127 template <typename PtrTy>
128 static PtrTy fromTargetAddress(orc::TargetAddress Addr) {
129   return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
130 }
131
132 int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
133   // Add the program's symbols into the JIT's search space.
134   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
135     errs() << "Error loading program symbols.\n";
136     return 1;
137   }
138
139   // Grab a target machine and try to build a factory function for the
140   // target-specific Orc callback manager.
141   EngineBuilder EB;
142   EB.setOptLevel(getOptLevel());
143   auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
144   auto CompileCallbackMgr =
145     OrcLazyJIT::createCompileCallbackMgr(Triple(TM->getTargetTriple()));
146
147   // If we couldn't build the factory function then there must not be a callback
148   // manager for this target. Bail out.
149   if (!CompileCallbackMgr) {
150     errs() << "No callback manager available for target '"
151            << TM->getTargetTriple().str() << "'.\n";
152     return 1;
153   }
154
155   auto IndirectStubsMgrBuilder =
156     OrcLazyJIT::createIndirectStubsMgrBuilder(Triple(TM->getTargetTriple()));
157
158   // If we couldn't build a stubs-manager-builder for this target then bail out.
159   if (!IndirectStubsMgrBuilder) {
160     errs() << "No indirect stubs manager available for target '"
161            << TM->getTargetTriple().str() << "'.\n";
162     return 1;
163   }
164
165   // Everything looks good. Build the JIT.
166   OrcLazyJIT J(std::move(TM), std::move(CompileCallbackMgr),
167                std::move(IndirectStubsMgrBuilder),
168                OrcInlineStubs);
169
170   // Add the module, look up main and run it.
171   auto MainHandle = J.addModule(std::move(M));
172   auto MainSym = J.findSymbolIn(MainHandle, "main");
173
174   if (!MainSym) {
175     errs() << "Could not find main function.\n";
176     return 1;
177   }
178
179   typedef int (*MainFnPtr)(int, char*[]);
180   auto Main = fromTargetAddress<MainFnPtr>(MainSym.getAddress());
181   return Main(ArgC, ArgV);
182 }