2015-03-25 20:11:48 +08: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-01-11 08:56:15 +08:00
|
|
|
#include "llvm/ExecutionEngine/Orc/OrcArchitectureSupport.h"
|
2015-04-14 06:12:54 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-04-01 12:42:56 +08:00
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
2015-04-17 18:01:30 +08:00
|
|
|
#include <cstdio>
|
2015-04-14 06:12:54 +08:00
|
|
|
#include <system_error>
|
2015-03-25 20:11:48 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-04-14 06:12:54 +08:00
|
|
|
namespace {
|
|
|
|
|
2015-04-15 00:58:05 +08:00
|
|
|
enum class DumpKind { NoDump, DumpFuncsToStdOut, DumpModsToStdErr,
|
2015-04-14 06:12:54 +08: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."),
|
2015-04-15 00:58:05 +08:00
|
|
|
clEnumValN(DumpKind::DumpFuncsToStdOut,
|
|
|
|
"funcs-to-stdout",
|
|
|
|
"Dump function names to stdout."),
|
2015-04-14 06:12:54 +08:00
|
|
|
clEnumValN(DumpKind::DumpModsToStdErr,
|
|
|
|
"mods-to-stderr",
|
|
|
|
"Dump modules to stderr."),
|
|
|
|
clEnumValN(DumpKind::DumpModsToDisk,
|
|
|
|
"mods-to-disk",
|
|
|
|
"Dump modules to the current "
|
|
|
|
"working directory. (WARNING: "
|
|
|
|
"will overwrite existing files)."),
|
2015-10-20 01:43:51 +08:00
|
|
|
clEnumValEnd),
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
cl::opt<bool> OrcInlineStubs("orc-lazy-inline-stubs",
|
|
|
|
cl::desc("Try to inline stubs"),
|
|
|
|
cl::init(true), cl::Hidden);
|
2015-04-14 06:12:54 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 00:40:37 +08:00
|
|
|
std::unique_ptr<OrcLazyJIT::CompileCallbackMgr>
|
|
|
|
OrcLazyJIT::createCompileCallbackMgr(Triple T) {
|
2015-03-25 20:11:48 +08:00
|
|
|
switch (T.getArch()) {
|
2015-03-31 02:37:01 +08:00
|
|
|
default: return nullptr;
|
2015-03-25 20:11:48 +08:00
|
|
|
|
|
|
|
case Triple::x86_64: {
|
2015-12-04 10:15:39 +08:00
|
|
|
typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64> CCMgrT;
|
2015-11-04 00:40:37 +08:00
|
|
|
return llvm::make_unique<CCMgrT>(0);
|
2015-03-25 20:11:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 01:43:51 +08:00
|
|
|
OrcLazyJIT::IndirectStubsManagerBuilder
|
|
|
|
OrcLazyJIT::createIndirectStubsMgrBuilder(Triple T) {
|
|
|
|
switch (T.getArch()) {
|
|
|
|
default: return nullptr;
|
|
|
|
|
|
|
|
case Triple::x86_64:
|
|
|
|
return [](){
|
2015-12-07 03:44:45 +08:00
|
|
|
return llvm::make_unique<
|
|
|
|
orc::LocalIndirectStubsManager<orc::OrcX86_64>>();
|
2015-10-20 01:43:51 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-14 06:12:54 +08:00
|
|
|
OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
|
|
|
|
|
|
|
|
switch (OrcDumpKind) {
|
|
|
|
case DumpKind::NoDump:
|
2015-05-01 07:07:00 +08:00
|
|
|
return [](std::unique_ptr<Module> M) { return M; };
|
2015-04-14 06:12:54 +08:00
|
|
|
|
2015-04-15 00:58:05 +08:00
|
|
|
case DumpKind::DumpFuncsToStdOut:
|
2015-04-14 06:12:54 +08:00
|
|
|
return [](std::unique_ptr<Module> M) {
|
2015-04-15 00:58:05 +08:00
|
|
|
printf("[ ");
|
2015-04-14 06:12:54 +08:00
|
|
|
|
|
|
|
for (const auto &F : *M) {
|
|
|
|
if (F.isDeclaration())
|
|
|
|
continue;
|
|
|
|
|
2015-04-15 00:58:05 +08:00
|
|
|
if (F.hasName()) {
|
|
|
|
std::string Name(F.getName());
|
|
|
|
printf("%s ", Name.c_str());
|
|
|
|
} else
|
|
|
|
printf("<anon> ");
|
2015-04-14 06:12:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 00:58:05 +08:00
|
|
|
printf("]\n");
|
2015-05-01 07:07:00 +08:00
|
|
|
return M;
|
2015-04-14 06:12:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
case DumpKind::DumpModsToStdErr:
|
|
|
|
return [](std::unique_ptr<Module> M) {
|
|
|
|
dbgs() << "----- Module Start -----\n" << *M
|
|
|
|
<< "----- Module End -----\n";
|
|
|
|
|
2015-05-01 07:07:00 +08:00
|
|
|
return M;
|
2015-04-14 06:12:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2015-05-01 07:07:00 +08:00
|
|
|
return M;
|
2015-04-14 06:12:54 +08:00
|
|
|
};
|
|
|
|
}
|
2015-04-14 21:26:11 +08:00
|
|
|
llvm_unreachable("Unknown DumpKind");
|
2015-04-14 06:12:54 +08:00
|
|
|
}
|
|
|
|
|
2015-06-09 10:43:27 +08:00
|
|
|
// Defined in lli.cpp.
|
|
|
|
CodeGenOpt::Level getOptLevel();
|
|
|
|
|
2015-10-20 01:43:51 +08:00
|
|
|
|
|
|
|
template <typename PtrTy>
|
|
|
|
static PtrTy fromTargetAddress(orc::TargetAddress Addr) {
|
|
|
|
return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:11:48 +08:00
|
|
|
int llvm::runOrcLazyJIT(std::unique_ptr<Module> M, int ArgC, char* ArgV[]) {
|
2015-04-01 12:42:56 +08: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 10:43:27 +08:00
|
|
|
EngineBuilder EB;
|
|
|
|
EB.setOptLevel(getOptLevel());
|
|
|
|
auto TM = std::unique_ptr<TargetMachine>(EB.selectTarget());
|
2015-11-04 00:40:37 +08:00
|
|
|
auto CompileCallbackMgr =
|
|
|
|
OrcLazyJIT::createCompileCallbackMgr(Triple(TM->getTargetTriple()));
|
2015-03-31 02:37:01 +08:00
|
|
|
|
2015-04-01 12:42:56 +08:00
|
|
|
// If we couldn't build the factory function then there must not be a callback
|
|
|
|
// manager for this target. Bail out.
|
2015-11-04 00:40:37 +08:00
|
|
|
if (!CompileCallbackMgr) {
|
2015-03-31 02:37:01 +08:00
|
|
|
errs() << "No callback manager available for target '"
|
2015-06-16 21:15:50 +08:00
|
|
|
<< TM->getTargetTriple().str() << "'.\n";
|
2015-03-25 20:11:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-20 01:43:51 +08:00
|
|
|
auto IndirectStubsMgrBuilder =
|
|
|
|
OrcLazyJIT::createIndirectStubsMgrBuilder(Triple(TM->getTargetTriple()));
|
|
|
|
|
|
|
|
// 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-04-01 12:42:56 +08:00
|
|
|
// Everything looks good. Build the JIT.
|
2015-11-04 00:40:37 +08:00
|
|
|
OrcLazyJIT J(std::move(TM), std::move(CompileCallbackMgr),
|
2015-10-20 01:43:51 +08:00
|
|
|
std::move(IndirectStubsMgrBuilder),
|
|
|
|
OrcInlineStubs);
|
2015-03-31 02:37:01 +08:00
|
|
|
|
2015-04-01 12:42:56 +08:00
|
|
|
// Add the module, look up main and run it.
|
2015-03-25 20:11:48 +08:00
|
|
|
auto MainHandle = J.addModule(std::move(M));
|
|
|
|
auto MainSym = J.findSymbolIn(MainHandle, "main");
|
|
|
|
|
|
|
|
if (!MainSym) {
|
|
|
|
errs() << "Could not find main function.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef int (*MainFnPtr)(int, char*[]);
|
2015-10-20 01:43:51 +08:00
|
|
|
auto Main = fromTargetAddress<MainFnPtr>(MainSym.getAddress());
|
2015-03-25 20:11:48 +08:00
|
|
|
return Main(ArgC, ArgV);
|
|
|
|
}
|