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::JITCompileCallbackManager<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<orc::IndirectStubsManager<orc::OrcX86_64>>();
73 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
75 switch (OrcDumpKind) {
76 case DumpKind::NoDump:
77 return [](std::unique_ptr<Module> M) { return M; };
79 case DumpKind::DumpFuncsToStdOut:
80 return [](std::unique_ptr<Module> M) {
83 for (const auto &F : *M) {
84 if (F.isDeclaration())
88 std::string Name(F.getName());
89 printf("%s ", Name.c_str());
98 case DumpKind::DumpModsToStdErr:
99 return [](std::unique_ptr<Module> M) {
100 dbgs() << "----- Module Start -----\n" << *M
101 << "----- Module End -----\n";
106 case DumpKind::DumpModsToDisk:
107 return [](std::unique_ptr<Module> M) {
109 raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
112 errs() << "Couldn't open " << M->getModuleIdentifier()
113 << " for dumping.\nError:" << EC.message() << "\n";
120 llvm_unreachable("Unknown DumpKind");
123 // Defined in lli.cpp.
124 CodeGenOpt::Level getOptLevel();
127 template <typename PtrTy>
128 static PtrTy fromTargetAddress(orc::TargetAddress Addr) {
129 return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
132 int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
133 // Add the program's symbols into the JIT's search space.
134 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
135 errs() << "Error loading program symbols.\n";
139 // Grab a target machine and try to build a factory function for the
140 // target-specific Orc callback manager.
142 EB.setOptLevel(getOptLevel());
143 auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
144 auto CompileCallbackMgr =
145 OrcLazyJIT::createCompileCallbackMgr(Triple(TM->getTargetTriple()));
147 // If we couldn't build the factory function then there must not be a callback
148 // manager for this target. Bail out.
149 if (!CompileCallbackMgr) {
150 errs() << "No callback manager available for target '"
151 << TM->getTargetTriple().str() << "'.\n";
155 auto IndirectStubsMgrBuilder =
156 OrcLazyJIT::createIndirectStubsMgrBuilder(Triple(TM->getTargetTriple()));
158 // If we couldn't build a stubs-manager-builder for this target then bail out.
159 if (!IndirectStubsMgrBuilder) {
160 errs() << "No indirect stubs manager available for target '"
161 << TM->getTargetTriple().str() << "'.\n";
165 // Everything looks good. Build the JIT.
166 OrcLazyJIT J(std::move(TM), std::move(CompileCallbackMgr),
167 std::move(IndirectStubsMgrBuilder),
170 // Add the module, look up main and run it.
171 auto MainHandle = J.addModule(std::move(M));
172 auto MainSym = J.findSymbolIn(MainHandle, "main");
175 errs() << "Could not find main function.\n";
179 typedef int (*MainFnPtr)(int, char*[]);
180 auto Main = fromTargetAddress<MainFnPtr>(MainSym.getAddress());
181 return Main(ArgC, ArgV);