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 OrcLazyJIT::CallbackManagerBuilder
45 OrcLazyJIT::createCallbackManagerBuilder(Triple T) {
46 switch (T.getArch()) {
47 default: return nullptr;
49 case Triple::x86_64: {
50 typedef orc::JITCompileCallbackManager<IRDumpLayerT,
51 orc::OrcX86_64> CCMgrT;
52 return [](IRDumpLayerT &IRDumpLayer, RuntimeDyld::MemoryManager &MemMgr,
53 LLVMContext &Context) {
54 return llvm::make_unique<CCMgrT>(IRDumpLayer, MemMgr, Context, 0,
61 OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
63 switch (OrcDumpKind) {
64 case DumpKind::NoDump:
65 return [](std::unique_ptr<Module> M) { return M; };
67 case DumpKind::DumpFuncsToStdOut:
68 return [](std::unique_ptr<Module> M) {
71 for (const auto &F : *M) {
72 if (F.isDeclaration())
76 std::string Name(F.getName());
77 printf("%s ", Name.c_str());
86 case DumpKind::DumpModsToStdErr:
87 return [](std::unique_ptr<Module> M) {
88 dbgs() << "----- Module Start -----\n" << *M
89 << "----- Module End -----\n";
94 case DumpKind::DumpModsToDisk:
95 return [](std::unique_ptr<Module> M) {
97 raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
100 errs() << "Couldn't open " << M->getModuleIdentifier()
101 << " for dumping.\nError:" << EC.message() << "\n";
108 llvm_unreachable("Unknown DumpKind");
111 // Defined in lli.cpp.
112 CodeGenOpt::Level getOptLevel();
114 int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
115 // Add the program's symbols into the JIT's search space.
116 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
117 errs() << "Error loading program symbols.\n";
121 // Grab a target machine and try to build a factory function for the
122 // target-specific Orc callback manager.
124 EB.setOptLevel(getOptLevel());
125 auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
126 auto &Context = getGlobalContext();
127 auto CallbackMgrBuilder =
128 OrcLazyJIT::createCallbackManagerBuilder(Triple(TM->getTargetTriple()));
130 // If we couldn't build the factory function then there must not be a callback
131 // manager for this target. Bail out.
132 if (!CallbackMgrBuilder) {
133 errs() << "No callback manager available for target '"
134 << TM->getTargetTriple().str() << "'.\n";
138 // Everything looks good. Build the JIT.
139 auto &DL = M->getDataLayout();
140 OrcLazyJIT J(std::move(TM), DL, Context, CallbackMgrBuilder);
142 // Add the module, look up main and run it.
143 auto MainHandle = J.addModule(std::move(M));
144 auto MainSym = J.findSymbolIn(MainHandle, "main");
147 errs() << "Could not find main function.\n";
151 typedef int (*MainFnPtr)(int, char*[]);
152 auto Main = OrcLazyJIT::fromTargetAddress<MainFnPtr>(MainSym.getAddress());
153 return Main(ArgC, ArgV);