Revert "[Orc] Directly emit machine code for the x86 resolver block and trampolines."
[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 OrcLazyJIT::CallbackManagerBuilder
50 OrcLazyJIT::createCallbackMgrBuilder(Triple T) {
51   switch (T.getArch()) {
52     default: return nullptr;
53
54     case Triple::x86_64: {
55       typedef orc::JITCompileCallbackManager<IRDumpLayerT,
56                                              orc::OrcX86_64> CCMgrT;
57       return [](IRDumpLayerT &IRDumpLayer, RuntimeDyld::MemoryManager &MemMgr,
58                 LLVMContext &Context) {
59                return llvm::make_unique<CCMgrT>(IRDumpLayer, MemMgr, Context, 0,
60                                                 64);
61              };
62     }
63   }
64 }
65
66 OrcLazyJIT::IndirectStubsManagerBuilder
67 OrcLazyJIT::createIndirectStubsMgrBuilder(Triple T) {
68   switch (T.getArch()) {
69     default: return nullptr;
70
71     case Triple::x86_64:
72       return [](){
73         return llvm::make_unique<orc::IndirectStubsManager<orc::OrcX86_64>>();
74       };
75   }
76 }
77
78 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
79
80   switch (OrcDumpKind) {
81   case DumpKind::NoDump:
82     return [](std::unique_ptr<Module> M) { return M; };
83
84   case DumpKind::DumpFuncsToStdOut:
85     return [](std::unique_ptr<Module> M) {
86       printf("[ ");
87
88       for (const auto &F : *M) {
89         if (F.isDeclaration())
90           continue;
91
92         if (F.hasName()) {
93           std::string Name(F.getName());
94           printf("%s ", Name.c_str());
95         } else
96           printf("<anon> ");
97       }
98
99       printf("]\n");
100       return M;
101     };
102
103   case DumpKind::DumpModsToStdErr:
104     return [](std::unique_ptr<Module> M) {
105              dbgs() << "----- Module Start -----\n" << *M
106                     << "----- Module End -----\n";
107
108              return M;
109            };
110
111   case DumpKind::DumpModsToDisk:
112     return [](std::unique_ptr<Module> M) {
113              std::error_code EC;
114              raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
115                                 sys::fs::F_Text);
116              if (EC) {
117                errs() << "Couldn't open " << M->getModuleIdentifier()
118                       << " for dumping.\nError:" << EC.message() << "\n";
119                exit(1);
120              }
121              Out << *M;
122              return M;
123            };
124   }
125   llvm_unreachable("Unknown DumpKind");
126 }
127
128 // Defined in lli.cpp.
129 CodeGenOpt::Level getOptLevel();
130
131
132 template <typename PtrTy>
133 static PtrTy fromTargetAddress(orc::TargetAddress Addr) {
134   return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
135 }
136
137 int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
138   // Add the program's symbols into the JIT's search space.
139   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
140     errs() << "Error loading program symbols.\n";
141     return 1;
142   }
143
144   // Grab a target machine and try to build a factory function for the
145   // target-specific Orc callback manager.
146   EngineBuilder EB;
147   EB.setOptLevel(getOptLevel());
148   auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
149   auto &Context = getGlobalContext();
150   auto CallbackMgrBuilder =
151     OrcLazyJIT::createCallbackMgrBuilder(Triple(TM->getTargetTriple()));
152
153   // If we couldn't build the factory function then there must not be a callback
154   // manager for this target. Bail out.
155   if (!CallbackMgrBuilder) {
156     errs() << "No callback manager available for target '"
157            << TM->getTargetTriple().str() << "'.\n";
158     return 1;
159   }
160
161   auto IndirectStubsMgrBuilder =
162     OrcLazyJIT::createIndirectStubsMgrBuilder(Triple(TM->getTargetTriple()));
163
164   // If we couldn't build a stubs-manager-builder for this target then bail out.
165   if (!IndirectStubsMgrBuilder) {
166     errs() << "No indirect stubs manager available for target '"
167            << TM->getTargetTriple().str() << "'.\n";
168     return 1;
169   }
170
171   // Everything looks good. Build the JIT.
172   OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder,
173                std::move(IndirectStubsMgrBuilder),
174                OrcInlineStubs);
175
176   // Add the module, look up main and run it.
177   auto MainHandle = J.addModule(std::move(M));
178   auto MainSym = J.findSymbolIn(MainHandle, "main");
179
180   if (!MainSym) {
181     errs() << "Could not find main function.\n";
182     return 1;
183   }
184
185   typedef int (*MainFnPtr)(int, char*[]);
186   auto Main = fromTargetAddress<MainFnPtr>(MainSym.getAddress());
187   return Main(ArgC, ArgV);
188 }