1 //===------ OrcLazyJIT.cpp - Basic Orc-based JIT for lazy execution -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "OrcLazyJIT.h"
11 #include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/DynamicLibrary.h"
15 #include <system_error>
21 enum class DumpKind { NoDump, DumpFuncsToStdOut, DumpModsToStdErr,
24 cl::opt<DumpKind> OrcDumpKind("orc-lazy-debug",
25 cl::desc("Debug dumping for the orc-lazy JIT."),
26 cl::init(DumpKind::NoDump),
28 clEnumValN(DumpKind::NoDump, "no-dump",
29 "Don't dump anything."),
30 clEnumValN(DumpKind::DumpFuncsToStdOut,
32 "Dump function names to stdout."),
33 clEnumValN(DumpKind::DumpModsToStdErr,
35 "Dump modules to stderr."),
36 clEnumValN(DumpKind::DumpModsToDisk,
38 "Dump modules to the current "
39 "working directory. (WARNING: "
40 "will overwrite existing files)."),
44 cl::opt<bool> OrcInlineStubs("orc-lazy-inline-stubs",
45 cl::desc("Try to inline stubs"),
46 cl::init(true), cl::Hidden);
49 std::unique_ptr<OrcLazyJIT::CompileCallbackMgr>
50 OrcLazyJIT::createCompileCallbackMgr(Triple T) {
51 switch (T.getArch()) {
52 default: return nullptr;
54 case Triple::x86_64: {
55 typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64> CCMgrT;
56 return llvm::make_unique<CCMgrT>(0);
61 OrcLazyJIT::IndirectStubsManagerBuilder
62 OrcLazyJIT::createIndirectStubsMgrBuilder(Triple T) {
63 switch (T.getArch()) {
64 default: return nullptr;
68 return llvm::make_unique<
69 orc::LocalIndirectStubsManager<orc::OrcX86_64>>();
74 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
76 switch (OrcDumpKind) {
77 case DumpKind::NoDump:
78 return [](std::unique_ptr<Module> M) { return M; };
80 case DumpKind::DumpFuncsToStdOut:
81 return [](std::unique_ptr<Module> M) {
84 for (const auto &F : *M) {
85 if (F.isDeclaration())
89 std::string Name(F.getName());
90 printf("%s ", Name.c_str());
99 case DumpKind::DumpModsToStdErr:
100 return [](std::unique_ptr<Module> M) {
101 dbgs() << "----- Module Start -----\n" << *M
102 << "----- Module End -----\n";
107 case DumpKind::DumpModsToDisk:
108 return [](std::unique_ptr<Module> M) {
110 raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
113 errs() << "Couldn't open " << M->getModuleIdentifier()
114 << " for dumping.\nError:" << EC.message() << "\n";
121 llvm_unreachable("Unknown DumpKind");
124 // Defined in lli.cpp.
125 CodeGenOpt::Level getOptLevel();
128 template <typename PtrTy>
129 static PtrTy fromTargetAddress(orc::TargetAddress Addr) {
130 return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
133 int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
134 // Add the program's symbols into the JIT's search space.
135 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
136 errs() << "Error loading program symbols.\n";
140 // Grab a target machine and try to build a factory function for the
141 // target-specific Orc callback manager.
143 EB.setOptLevel(getOptLevel());
144 auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
145 auto CompileCallbackMgr =
146 OrcLazyJIT::createCompileCallbackMgr(Triple(TM->getTargetTriple()));
148 // If we couldn't build the factory function then there must not be a callback
149 // manager for this target. Bail out.
150 if (!CompileCallbackMgr) {
151 errs() << "No callback manager available for target '"
152 << TM->getTargetTriple().str() << "'.\n";
156 auto IndirectStubsMgrBuilder =
157 OrcLazyJIT::createIndirectStubsMgrBuilder(Triple(TM->getTargetTriple()));
159 // If we couldn't build a stubs-manager-builder for this target then bail out.
160 if (!IndirectStubsMgrBuilder) {
161 errs() << "No indirect stubs manager available for target '"
162 << TM->getTargetTriple().str() << "'.\n";
166 // Everything looks good. Build the JIT.
167 OrcLazyJIT J(std::move(TM), std::move(CompileCallbackMgr),
168 std::move(IndirectStubsMgrBuilder),
171 // Add the module, look up main and run it.
172 auto MainHandle = J.addModule(std::move(M));
173 auto MainSym = J.findSymbolIn(MainHandle, "main");
176 errs() << "Could not find main function.\n";
180 typedef int (*MainFnPtr)(int, char*[]);
181 auto Main = fromTargetAddress<MainFnPtr>(MainSym.getAddress());
182 return Main(ArgC, ArgV);