mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-15 04:30:12 +00:00
63cc4f56a9
This patch adds a new set of JIT APIs to LLVM. The aim of these new APIs is to cleanly support a wider range of JIT use cases in LLVM, and encourage the development and contribution of re-usable infrastructure for LLVM JIT use-cases. These APIs are intended to live alongside the MCJIT APIs, and should not affect existing clients. Included in this patch: 1) New headers in include/llvm/ExecutionEngine/Orc that provide a set of components for building JIT infrastructure. Implementation code for these headers lives in lib/ExecutionEngine/Orc. 2) A prototype re-implementation of MCJIT (OrcMCJITReplacement) built out of the new components. 3) Minor changes to RTDyldMemoryManager needed to support the new components. These changes should not impact existing clients. 4) A new flag for lli, -use-orcmcjit, which will cause lli to use the OrcMCJITReplacement class as its underlying execution engine, rather than MCJIT itself. Tests to follow shortly. Special thanks to Michael Ilseman, Pete Cooper, David Blaikie, Eric Christopher, Justin Bogner, and Jim Grosbach for extensive feedback and discussion. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226940 91177308-0d34-0410-b5e6-96231b3b80d8
158 lines
5.3 KiB
C++
158 lines
5.3 KiB
C++
#include "llvm/ADT/Triple.h"
|
|
#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
|
|
#include "llvm/ExecutionEngine/Orc/CloneSubModule.h"
|
|
#include "llvm/IR/CallSite.h"
|
|
#include "llvm/IR/IRBuilder.h"
|
|
#include <set>
|
|
|
|
using namespace llvm;
|
|
|
|
namespace llvm {
|
|
|
|
JITIndirections makeCallsSingleIndirect(
|
|
Module &M, const std::function<bool(const Function &)> &ShouldIndirect,
|
|
const char *JITImplSuffix, const char *JITAddrSuffix) {
|
|
std::vector<Function *> Worklist;
|
|
std::vector<std::string> FuncNames;
|
|
|
|
for (auto &F : M)
|
|
if (ShouldIndirect(F) && (F.user_begin() != F.user_end())) {
|
|
Worklist.push_back(&F);
|
|
FuncNames.push_back(F.getName());
|
|
}
|
|
|
|
for (auto *F : Worklist) {
|
|
GlobalVariable *FImplAddr = new GlobalVariable(
|
|
M, F->getType(), false, GlobalValue::ExternalLinkage,
|
|
Constant::getNullValue(F->getType()), F->getName() + JITAddrSuffix,
|
|
nullptr, GlobalValue::NotThreadLocal, 0, true);
|
|
FImplAddr->setVisibility(GlobalValue::HiddenVisibility);
|
|
|
|
for (auto *U : F->users()) {
|
|
assert(isa<Instruction>(U) && "Cannot indirect non-instruction use");
|
|
IRBuilder<> Builder(cast<Instruction>(U));
|
|
U->replaceUsesOfWith(F, Builder.CreateLoad(FImplAddr));
|
|
}
|
|
}
|
|
|
|
return JITIndirections(
|
|
FuncNames, [=](StringRef S) -> std::string { return std::string(S); },
|
|
[=](StringRef S)
|
|
-> std::string { return std::string(S) + JITAddrSuffix; });
|
|
}
|
|
|
|
JITIndirections makeCallsDoubleIndirect(
|
|
Module &M, const std::function<bool(const Function &)> &ShouldIndirect,
|
|
const char *JITImplSuffix, const char *JITAddrSuffix) {
|
|
|
|
std::vector<Function *> Worklist;
|
|
std::vector<std::string> FuncNames;
|
|
|
|
for (auto &F : M)
|
|
if (!F.isDeclaration() && !F.hasAvailableExternallyLinkage() &&
|
|
ShouldIndirect(F))
|
|
Worklist.push_back(&F);
|
|
|
|
for (auto *F : Worklist) {
|
|
std::string OrigName = F->getName();
|
|
F->setName(OrigName + JITImplSuffix);
|
|
FuncNames.push_back(OrigName);
|
|
|
|
GlobalVariable *FImplAddr = new GlobalVariable(
|
|
M, F->getType(), false, GlobalValue::ExternalLinkage,
|
|
Constant::getNullValue(F->getType()), OrigName + JITAddrSuffix, nullptr,
|
|
GlobalValue::NotThreadLocal, 0, true);
|
|
FImplAddr->setVisibility(GlobalValue::HiddenVisibility);
|
|
|
|
Function *FRedirect =
|
|
Function::Create(F->getFunctionType(), F->getLinkage(), OrigName, &M);
|
|
|
|
F->replaceAllUsesWith(FRedirect);
|
|
|
|
BasicBlock *EntryBlock =
|
|
BasicBlock::Create(M.getContext(), "entry", FRedirect);
|
|
|
|
IRBuilder<> Builder(EntryBlock);
|
|
LoadInst *FImplLoadedAddr = Builder.CreateLoad(FImplAddr);
|
|
|
|
std::vector<Value *> CallArgs;
|
|
for (Value &Arg : FRedirect->args())
|
|
CallArgs.push_back(&Arg);
|
|
CallInst *Call = Builder.CreateCall(FImplLoadedAddr, CallArgs);
|
|
Call->setTailCall();
|
|
Builder.CreateRet(Call);
|
|
}
|
|
|
|
return JITIndirections(
|
|
FuncNames, [=](StringRef S)
|
|
-> std::string { return std::string(S) + JITImplSuffix; },
|
|
[=](StringRef S)
|
|
-> std::string { return std::string(S) + JITAddrSuffix; });
|
|
}
|
|
|
|
std::vector<std::unique_ptr<Module>>
|
|
explode(const Module &OrigMod,
|
|
const std::function<bool(const Function &)> &ShouldExtract) {
|
|
|
|
std::vector<std::unique_ptr<Module>> NewModules;
|
|
|
|
// Split all the globals, non-indirected functions, etc. into a single module.
|
|
auto ExtractGlobalVars = [&](GlobalVariable &New, const GlobalVariable &Orig,
|
|
ValueToValueMapTy &VMap) {
|
|
copyGVInitializer(New, Orig, VMap);
|
|
if (New.getLinkage() == GlobalValue::PrivateLinkage) {
|
|
New.setLinkage(GlobalValue::ExternalLinkage);
|
|
New.setVisibility(GlobalValue::HiddenVisibility);
|
|
}
|
|
};
|
|
|
|
auto ExtractNonImplFunctions =
|
|
[&](Function &New, const Function &Orig, ValueToValueMapTy &VMap) {
|
|
if (!ShouldExtract(New))
|
|
copyFunctionBody(New, Orig, VMap);
|
|
};
|
|
|
|
NewModules.push_back(CloneSubModule(OrigMod, ExtractGlobalVars,
|
|
ExtractNonImplFunctions, true));
|
|
|
|
// Preserve initializers for Common linkage vars, and make private linkage
|
|
// globals external: they are now provided by the globals module extracted
|
|
// above.
|
|
auto DropGlobalVars = [&](GlobalVariable &New, const GlobalVariable &Orig,
|
|
ValueToValueMapTy &VMap) {
|
|
if (New.getLinkage() == GlobalValue::CommonLinkage)
|
|
copyGVInitializer(New, Orig, VMap);
|
|
else if (New.getLinkage() == GlobalValue::PrivateLinkage)
|
|
New.setLinkage(GlobalValue::ExternalLinkage);
|
|
};
|
|
|
|
// Split each 'impl' function out in to its own module.
|
|
for (const auto &Func : OrigMod) {
|
|
if (Func.isDeclaration() || !ShouldExtract(Func))
|
|
continue;
|
|
|
|
auto ExtractNamedFunction =
|
|
[&](Function &New, const Function &Orig, ValueToValueMapTy &VMap) {
|
|
if (New.getName() == Func.getName())
|
|
copyFunctionBody(New, Orig, VMap);
|
|
};
|
|
|
|
NewModules.push_back(
|
|
CloneSubModule(OrigMod, DropGlobalVars, ExtractNamedFunction, false));
|
|
}
|
|
|
|
return NewModules;
|
|
}
|
|
|
|
std::vector<std::unique_ptr<Module>>
|
|
explode(const Module &OrigMod, const JITIndirections &Indirections) {
|
|
std::set<std::string> ImplNames;
|
|
|
|
for (const auto &FuncName : Indirections.IndirectedNames)
|
|
ImplNames.insert(Indirections.GetImplName(FuncName));
|
|
|
|
return explode(
|
|
OrigMod, [&](const Function &F) { return ImplNames.count(F.getName()); });
|
|
}
|
|
}
|