]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lli/OrcLazyJIT.cpp
Merge bmake-20170510
[FreeBSD/FreeBSD.git] / contrib / llvm / 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/OrcABISupport.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, DumpModsToStdOut,
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::DumpModsToStdOut,
34                                              "mods-to-stdout",
35                                              "Dump modules to stdout."),
36                                   clEnumValN(DumpKind::DumpModsToDisk,
37                                              "mods-to-disk",
38                                              "Dump modules to the current "
39                                              "working directory. (WARNING: "
40                                              "will overwrite existing files).")),
41                                 cl::Hidden);
42
43   cl::opt<bool> OrcInlineStubs("orc-lazy-inline-stubs",
44                                cl::desc("Try to inline stubs"),
45                                cl::init(true), cl::Hidden);
46 }
47
48 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
49
50   switch (OrcDumpKind) {
51   case DumpKind::NoDump:
52     return [](std::unique_ptr<Module> M) { return M; };
53
54   case DumpKind::DumpFuncsToStdOut:
55     return [](std::unique_ptr<Module> M) {
56       printf("[ ");
57
58       for (const auto &F : *M) {
59         if (F.isDeclaration())
60           continue;
61
62         if (F.hasName()) {
63           std::string Name(F.getName());
64           printf("%s ", Name.c_str());
65         } else
66           printf("<anon> ");
67       }
68
69       printf("]\n");
70       return M;
71     };
72
73   case DumpKind::DumpModsToStdOut:
74     return [](std::unique_ptr<Module> M) {
75              outs() << "----- Module Start -----\n" << *M
76                     << "----- Module End -----\n";
77
78              return M;
79            };
80
81   case DumpKind::DumpModsToDisk:
82     return [](std::unique_ptr<Module> M) {
83              std::error_code EC;
84              raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
85                                 sys::fs::F_Text);
86              if (EC) {
87                errs() << "Couldn't open " << M->getModuleIdentifier()
88                       << " for dumping.\nError:" << EC.message() << "\n";
89                exit(1);
90              }
91              Out << *M;
92              return M;
93            };
94   }
95   llvm_unreachable("Unknown DumpKind");
96 }
97
98 // Defined in lli.cpp.
99 CodeGenOpt::Level getOptLevel();
100
101
102 template <typename PtrTy>
103 static PtrTy fromTargetAddress(JITTargetAddress Addr) {
104   return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
105 }
106
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";
112     return 1;
113   }
114
115   // Grab a target machine and try to build a factory function for the
116   // target-specific Orc callback manager.
117   EngineBuilder EB;
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);
122
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";
128     return 1;
129   }
130
131   auto IndirectStubsMgrBuilder = orc::createLocalIndirectStubsManagerBuilder(T);
132
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";
137     return 1;
138   }
139
140   // Everything looks good. Build the JIT.
141   OrcLazyJIT J(std::move(TM), std::move(CompileCallbackMgr),
142                std::move(IndirectStubsMgrBuilder),
143                OrcInlineStubs);
144
145   // Add the module, look up main and run it.
146   J.addModuleSet(std::move(Ms));
147   auto MainSym = J.findSymbol("main");
148
149   if (!MainSym) {
150     errs() << "Could not find main function.\n";
151     return 1;
152   }
153
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());
160 }
161