2015-05-27 18:44:32 +00:00
|
|
|
//===------ OrcLazyJIT.cpp - Basic Orc-based JIT for lazy execution -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "OrcLazyJIT.h"
|
2016-07-23 20:41:05 +00:00
|
|
|
#include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
|
2015-05-27 18:44:32 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
|
|
|
#include <cstdio>
|
|
|
|
#include <system_error>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
enum class DumpKind { NoDump, DumpFuncsToStdOut, DumpModsToStdOut,
|
2015-05-27 18:44:32 +00:00
|
|
|
DumpModsToDisk };
|
|
|
|
|
|
|
|
cl::opt<DumpKind> OrcDumpKind("orc-lazy-debug",
|
|
|
|
cl::desc("Debug dumping for the orc-lazy JIT."),
|
|
|
|
cl::init(DumpKind::NoDump),
|
|
|
|
cl::values(
|
|
|
|
clEnumValN(DumpKind::NoDump, "no-dump",
|
|
|
|
"Don't dump anything."),
|
|
|
|
clEnumValN(DumpKind::DumpFuncsToStdOut,
|
|
|
|
"funcs-to-stdout",
|
|
|
|
"Dump function names to stdout."),
|
2017-01-02 19:17:04 +00:00
|
|
|
clEnumValN(DumpKind::DumpModsToStdOut,
|
|
|
|
"mods-to-stdout",
|
|
|
|
"Dump modules to stdout."),
|
2015-05-27 18:44:32 +00:00
|
|
|
clEnumValN(DumpKind::DumpModsToDisk,
|
|
|
|
"mods-to-disk",
|
|
|
|
"Dump modules to the current "
|
|
|
|
"working directory. (WARNING: "
|
2017-01-02 19:17:04 +00:00
|
|
|
"will overwrite existing files).")),
|
2015-12-30 11:46:15 +00:00
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
cl::opt<bool> OrcInlineStubs("orc-lazy-inline-stubs",
|
|
|
|
cl::desc("Try to inline stubs"),
|
|
|
|
cl::init(true), cl::Hidden);
|
2015-05-27 18:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
|
|
|
|
|
|
|
|
switch (OrcDumpKind) {
|
|
|
|
case DumpKind::NoDump:
|
|
|
|
return [](std::unique_ptr<Module> M) { return M; };
|
|
|
|
|
|
|
|
case DumpKind::DumpFuncsToStdOut:
|
|
|
|
return [](std::unique_ptr<Module> M) {
|
|
|
|
printf("[ ");
|
|
|
|
|
|
|
|
for (const auto &F : *M) {
|
|
|
|
if (F.isDeclaration())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (F.hasName()) {
|
|
|
|
std::string Name(F.getName());
|
|
|
|
printf("%s ", Name.c_str());
|
|
|
|
} else
|
|
|
|
printf("<anon> ");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("]\n");
|
|
|
|
return M;
|
|
|
|
};
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
case DumpKind::DumpModsToStdOut:
|
2015-05-27 18:44:32 +00:00
|
|
|
return [](std::unique_ptr<Module> M) {
|
2017-01-02 19:17:04 +00:00
|
|
|
outs() << "----- Module Start -----\n" << *M
|
2015-05-27 18:44:32 +00:00
|
|
|
<< "----- Module End -----\n";
|
|
|
|
|
|
|
|
return M;
|
|
|
|
};
|
|
|
|
|
|
|
|
case DumpKind::DumpModsToDisk:
|
|
|
|
return [](std::unique_ptr<Module> M) {
|
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream Out(M->getModuleIdentifier() + ".ll", EC,
|
|
|
|
sys::fs::F_Text);
|
|
|
|
if (EC) {
|
|
|
|
errs() << "Couldn't open " << M->getModuleIdentifier()
|
|
|
|
<< " for dumping.\nError:" << EC.message() << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Out << *M;
|
|
|
|
return M;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown DumpKind");
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:06:30 +00:00
|
|
|
// Defined in lli.cpp.
|
|
|
|
CodeGenOpt::Level getOptLevel();
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
|
|
|
|
template <typename PtrTy>
|
2017-01-02 19:17:04 +00:00
|
|
|
static PtrTy fromTargetAddress(JITTargetAddress Addr) {
|
2015-12-30 11:46:15 +00:00
|
|
|
return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
int llvm::runOrcLazyJIT(std::vector<std::unique_ptr<Module>> Ms,
|
|
|
|
const std::vector<std::string> &Args) {
|
2015-05-27 18:44:32 +00:00
|
|
|
// Add the program's symbols into the JIT's search space.
|
|
|
|
if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
|
|
|
|
errs() << "Error loading program symbols.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab a target machine and try to build a factory function for the
|
|
|
|
// target-specific Orc callback manager.
|
2015-06-09 19:06:30 +00:00
|
|
|
EngineBuilder EB;
|
|
|
|
EB.setOptLevel(getOptLevel());
|
|
|
|
auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
|
2016-07-23 20:41:05 +00:00
|
|
|
Triple T(TM->getTargetTriple());
|
|
|
|
auto CompileCallbackMgr = orc::createLocalCompileCallbackManager(T, 0);
|
2015-05-27 18:44:32 +00:00
|
|
|
|
|
|
|
// If we couldn't build the factory function then there must not be a callback
|
|
|
|
// manager for this target. Bail out.
|
2015-12-30 11:46:15 +00:00
|
|
|
if (!CompileCallbackMgr) {
|
2015-05-27 18:44:32 +00:00
|
|
|
errs() << "No callback manager available for target '"
|
2015-06-21 13:59:01 +00:00
|
|
|
<< TM->getTargetTriple().str() << "'.\n";
|
2015-05-27 18:44:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
auto IndirectStubsMgrBuilder = orc::createLocalIndirectStubsManagerBuilder(T);
|
2015-12-30 11:46:15 +00:00
|
|
|
|
|
|
|
// If we couldn't build a stubs-manager-builder for this target then bail out.
|
|
|
|
if (!IndirectStubsMgrBuilder) {
|
|
|
|
errs() << "No indirect stubs manager available for target '"
|
|
|
|
<< TM->getTargetTriple().str() << "'.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
// Everything looks good. Build the JIT.
|
2015-12-30 11:46:15 +00:00
|
|
|
OrcLazyJIT J(std::move(TM), std::move(CompileCallbackMgr),
|
|
|
|
std::move(IndirectStubsMgrBuilder),
|
|
|
|
OrcInlineStubs);
|
2015-05-27 18:44:32 +00:00
|
|
|
|
|
|
|
// Add the module, look up main and run it.
|
2017-01-02 19:17:04 +00:00
|
|
|
J.addModuleSet(std::move(Ms));
|
|
|
|
auto MainSym = J.findSymbol("main");
|
2015-05-27 18:44:32 +00:00
|
|
|
|
|
|
|
if (!MainSym) {
|
|
|
|
errs() << "Could not find main function.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
typedef int (*MainFnPtr)(int, const char*[]);
|
|
|
|
std::vector<const char *> ArgV;
|
|
|
|
for (auto &Arg : Args)
|
|
|
|
ArgV.push_back(Arg.c_str());
|
2015-12-30 11:46:15 +00:00
|
|
|
auto Main = fromTargetAddress<MainFnPtr>(MainSym.getAddress());
|
2017-01-02 19:17:04 +00:00
|
|
|
return Main(ArgV.size(), (const char**)ArgV.data());
|
2015-05-27 18:44:32 +00:00
|
|
|
}
|
2016-07-23 20:41:05 +00:00
|
|
|
|