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"
14 #include <system_error>
20 enum class DumpKind { NoDump, DumpFuncsToStdErr, DumpModsToStdErr,
23 cl::opt<DumpKind> OrcDumpKind("orc-lazy-debug",
24 cl::desc("Debug dumping for the orc-lazy JIT."),
25 cl::init(DumpKind::NoDump),
27 clEnumValN(DumpKind::NoDump, "no-dump",
28 "Don't dump anything."),
29 clEnumValN(DumpKind::DumpFuncsToStdErr,
31 "Dump function names to stderr."),
32 clEnumValN(DumpKind::DumpModsToStdErr,
34 "Dump modules to stderr."),
35 clEnumValN(DumpKind::DumpModsToDisk,
37 "Dump modules to the current "
38 "working directory. (WARNING: "
39 "will overwrite existing files)."),
43 OrcLazyJIT::CallbackManagerBuilder
44 OrcLazyJIT::createCallbackManagerBuilder(Triple T) {
45 switch (T.getArch()) {
46 default: return nullptr;
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 make_unique<CCMgrT>(IRDumpLayer, MemMgr, Context, 0, 64);
59 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
61 switch (OrcDumpKind) {
62 case DumpKind::NoDump:
63 return [](std::unique_ptr<Module> M) { return std::move(M); };
65 case DumpKind::DumpFuncsToStdErr:
66 return [](std::unique_ptr<Module> M) {
69 for (const auto &F : *M) {
70 if (F.isDeclaration())
74 dbgs() << F.getName() << " ";
83 case DumpKind::DumpModsToStdErr:
84 return [](std::unique_ptr<Module> M) {
85 dbgs() << "----- Module Start -----\n" << *M
86 << "----- Module End -----\n";
91 case DumpKind::DumpModsToDisk:
92 return [](std::unique_ptr<Module> M) {
94 raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
97 errs() << "Couldn't open " << M->getModuleIdentifier()
98 << " for dumping.\nError:" << EC.message() << "\n";
107 int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
108 // Add the program's symbols into the JIT's search space.
109 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
110 errs() << "Error loading program symbols.\n";
114 // Grab a target machine and try to build a factory function for the
115 // target-specific Orc callback manager.
116 auto TM = std::unique_ptr<TargetMachine>(EngineBuilder().selectTarget());
117 auto &Context = getGlobalContext();
118 auto CallbackMgrBuilder =
119 OrcLazyJIT::createCallbackManagerBuilder(Triple(TM->getTargetTriple()));
121 // If we couldn't build the factory function then there must not be a callback
122 // manager for this target. Bail out.
123 if (!CallbackMgrBuilder) {
124 errs() << "No callback manager available for target '"
125 << TM->getTargetTriple() << "'.\n";
129 // Everything looks good. Build the JIT.
130 OrcLazyJIT J(std::move(TM), Context, CallbackMgrBuilder);
132 // Add the module, look up main and run it.
133 auto MainHandle = J.addModule(std::move(M));
134 auto MainSym = J.findSymbolIn(MainHandle, "main");
137 errs() << "Could not find main function.\n";
141 typedef int (*MainFnPtr)(int, char*[]);
142 auto Main = OrcLazyJIT::fromTargetAddress<MainFnPtr>(MainSym.getAddress());
143 return Main(ArgC, ArgV);