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 OrcLazyJIT::CallbackManagerBuilder
50 OrcLazyJIT::createCallbackMgrBuilder(Triple T) {
51 switch (T.getArch()) {
52 default: return nullptr;
54 case Triple::x86_64: {
55 typedef orc::JITCompileCallbackManager<IRDumpLayerT,
56 orc::OrcX86_64> CCMgrT;
57 return [](IRDumpLayerT &IRDumpLayer, RuntimeDyld::MemoryManager &MemMgr,
58 LLVMContext &Context) {
59 return llvm::make_unique<CCMgrT>(IRDumpLayer, MemMgr, Context, 0,
66 OrcLazyJIT::IndirectStubsManagerBuilder
67 OrcLazyJIT::createIndirectStubsMgrBuilder(Triple T) {
68 switch (T.getArch()) {
69 default: return nullptr;
73 return llvm::make_unique<orc::IndirectStubsManager<orc::OrcX86_64>>();
78 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
80 switch (OrcDumpKind) {
81 case DumpKind::NoDump:
82 return [](std::unique_ptr<Module> M) { return M; };
84 case DumpKind::DumpFuncsToStdOut:
85 return [](std::unique_ptr<Module> M) {
88 for (const auto &F : *M) {
89 if (F.isDeclaration())
93 std::string Name(F.getName());
94 printf("%s ", Name.c_str());
103 case DumpKind::DumpModsToStdErr:
104 return [](std::unique_ptr<Module> M) {
105 dbgs() << "----- Module Start -----\n" << *M
106 << "----- Module End -----\n";
111 case DumpKind::DumpModsToDisk:
112 return [](std::unique_ptr<Module> M) {
114 raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
117 errs() << "Couldn't open " << M->getModuleIdentifier()
118 << " for dumping.\nError:" << EC.message() << "\n";
125 llvm_unreachable("Unknown DumpKind");
128 // Defined in lli.cpp.
129 CodeGenOpt::Level getOptLevel();
132 template <typename PtrTy>
133 static PtrTy fromTargetAddress(orc::TargetAddress Addr) {
134 return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
137 int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
138 // Add the program's symbols into the JIT's search space.
139 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
140 errs() << "Error loading program symbols.\n";
144 // Grab a target machine and try to build a factory function for the
145 // target-specific Orc callback manager.
147 EB.setOptLevel(getOptLevel());
148 auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
149 auto &Context = getGlobalContext();
150 auto CallbackMgrBuilder =
151 OrcLazyJIT::createCallbackMgrBuilder(Triple(TM->getTargetTriple()));
153 // If we couldn't build the factory function then there must not be a callback
154 // manager for this target. Bail out.
155 if (!CallbackMgrBuilder) {
156 errs() << "No callback manager available for target '"
157 << TM->getTargetTriple().str() << "'.\n";
161 auto IndirectStubsMgrBuilder =
162 OrcLazyJIT::createIndirectStubsMgrBuilder(Triple(TM->getTargetTriple()));
164 // If we couldn't build a stubs-manager-builder for this target then bail out.
165 if (!IndirectStubsMgrBuilder) {
166 errs() << "No indirect stubs manager available for target '"
167 << TM->getTargetTriple().str() << "'.\n";
171 // Everything looks good. Build the JIT.
172 OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder,
173 std::move(IndirectStubsMgrBuilder),
176 // Add the module, look up main and run it.
177 auto MainHandle = J.addModule(std::move(M));
178 auto MainSym = J.findSymbolIn(MainHandle, "main");
181 errs() << "Could not find main function.\n";
185 typedef int (*MainFnPtr)(int, char*[]);
186 auto Main = fromTargetAddress<MainFnPtr>(MainSym.getAddress());
187 return Main(ArgC, ArgV);