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