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 <system_error>
15
16 using namespace llvm;
17
18 namespace {
19
20 enum class DumpKind { NoDump, DumpFuncsToStdOut, DumpModsToStdErr,
21 DumpModsToDisk };
22
23 cl::opt<DumpKind> OrcDumpKind("orc-lazy-debug",
24 cl::desc("Debug dumping for the orc-lazy JIT."),
25 cl::init(DumpKind::NoDump),
26 cl::values(
27 clEnumValN(DumpKind::NoDump, "no-dump",
28 "Don't dump anything."),
29 clEnumValN(DumpKind::DumpFuncsToStdOut,
30 "funcs-to-stdout",
31 "Dump function names to stdout."),
32 clEnumValN(DumpKind::DumpModsToStdErr,
33 "mods-to-stderr",
34 "Dump modules to stderr."),
35 clEnumValN(DumpKind::DumpModsToDisk,
36 "mods-to-disk",
37 "Dump modules to the current "
38 "working directory. (WARNING: "
39 "will overwrite existing files)."),
40 clEnumValEnd));
41 }
42
43 OrcLazyJIT::CallbackManagerBuilder
createCallbackManagerBuilder(Triple T)44 OrcLazyJIT::createCallbackManagerBuilder(Triple T) {
45 switch (T.getArch()) {
46 default: return nullptr;
47
48 case Triple::x86_64: {
49 typedef orc::JITCompileCallbackManager<IRDumpLayerT,
50 orc::OrcX86_64> CCMgrT;
51 return [](IRDumpLayerT &IRDumpLayer, RuntimeDyld::MemoryManager &MemMgr,
52 LLVMContext &Context) {
53 return llvm::make_unique<CCMgrT>(IRDumpLayer, MemMgr, Context, 0,
54 64);
55 };
56 }
57 }
58 }
59
createDebugDumper()60 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
61
62 switch (OrcDumpKind) {
63 case DumpKind::NoDump:
64 return [](std::unique_ptr<Module> M) { return std::move(M); };
65
66 case DumpKind::DumpFuncsToStdOut:
67 return [](std::unique_ptr<Module> M) {
68 printf("[ ");
69
70 for (const auto &F : *M) {
71 if (F.isDeclaration())
72 continue;
73
74 if (F.hasName()) {
75 std::string Name(F.getName());
76 printf("%s ", Name.c_str());
77 } else
78 printf("<anon> ");
79 }
80
81 printf("]\n");
82 return std::move(M);
83 };
84
85 case DumpKind::DumpModsToStdErr:
86 return [](std::unique_ptr<Module> M) {
87 dbgs() << "----- Module Start -----\n" << *M
88 << "----- Module End -----\n";
89
90 return std::move(M);
91 };
92
93 case DumpKind::DumpModsToDisk:
94 return [](std::unique_ptr<Module> M) {
95 std::error_code EC;
96 raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
97 sys::fs::F_Text);
98 if (EC) {
99 errs() << "Couldn't open " << M->getModuleIdentifier()
100 << " for dumping.\nError:" << EC.message() << "\n";
101 exit(1);
102 }
103 Out << *M;
104 return std::move(M);
105 };
106 }
107 llvm_unreachable("Unknown DumpKind");
108 }
109
runOrcLazyJIT(std::unique_ptr<Module> M,int ArgC,char * ArgV[])110 int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
111 // Add the program's symbols into the JIT's search space.
112 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
113 errs() << "Error loading program symbols.\n";
114 return 1;
115 }
116
117 // Grab a target machine and try to build a factory function for the
118 // target-specific Orc callback manager.
119 auto TM = std::unique_ptr<TargetMachine>(EngineBuilder().selectTarget());
120 auto &Context = getGlobalContext();
121 auto CallbackMgrBuilder =
122 OrcLazyJIT::createCallbackManagerBuilder(Triple(TM->getTargetTriple()));
123
124 // If we couldn't build the factory function then there must not be a callback
125 // manager for this target. Bail out.
126 if (!CallbackMgrBuilder) {
127 errs() << "No callback manager available for target '"
128 << TM->getTargetTriple() << "'.\n";
129 return 1;
130 }
131
132 // Everything looks good. Build the JIT.
133 OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder);
134
135 // Add the module, look up main and run it.
136 auto MainHandle = J.addModule(std::move(M));
137 auto MainSym = J.findSymbolIn(MainHandle, "main");
138
139 if (!MainSym) {
140 errs() << "Could not find main function.\n";
141 return 1;
142 }
143
144 typedef int (*MainFnPtr)(int, char*[]);
145 auto Main = OrcLazyJIT::fromTargetAddress<MainFnPtr>(MainSym.getAddress());
146 return Main(ArgC, ArgV);
147 }
148