mirror of
https://github.com/ldc-developers/ldc.git
synced 2025-05-02 16:11:08 +03:00
clang-format
This commit is contained in:
parent
c49e261fa7
commit
5bbfb93ec2
13 changed files with 510 additions and 592 deletions
|
@ -1,71 +1,67 @@
|
|||
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "optimizer.h"
|
||||
#include "context.h"
|
||||
#include "utils.h"
|
||||
#include "callback_ostream.h"
|
||||
#include "context.h"
|
||||
#include "optimizer.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include "llvm/Support/ManagedStatic.h"
|
||||
|
||||
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
||||
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
||||
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
||||
#include "llvm/ExecutionEngine/JITSymbol.h"
|
||||
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
|
||||
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
|
||||
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
|
||||
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
||||
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
||||
|
||||
#if LDC_LLVM_VER >= 500
|
||||
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
|
||||
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
|
||||
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
|
||||
#else
|
||||
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
|
||||
#endif
|
||||
|
||||
#include "llvm/Support/DynamicLibrary.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include "llvm/Support/TargetSelect.h"
|
||||
#include "llvm/Support/Host.h"
|
||||
#include "llvm/Target/TargetMachine.h"
|
||||
#include "llvm/Bitcode/BitcodeReader.h"
|
||||
#include "llvm/Support/DynamicLibrary.h"
|
||||
#include "llvm/Support/Host.h"
|
||||
#include "llvm/Support/TargetSelect.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include "llvm/Target/TargetMachine.h"
|
||||
|
||||
namespace {
|
||||
|
||||
#pragma pack(push,1)
|
||||
#pragma pack(push, 1)
|
||||
|
||||
struct RtCompileFuncList
|
||||
{
|
||||
const char* name;
|
||||
void** func;
|
||||
struct RtCompileFuncList {
|
||||
const char *name;
|
||||
void **func;
|
||||
};
|
||||
|
||||
struct RtCompileSymList
|
||||
{
|
||||
const char* name;
|
||||
void* sym;
|
||||
struct RtCompileSymList {
|
||||
const char *name;
|
||||
void *sym;
|
||||
};
|
||||
|
||||
struct RtCompileVarList
|
||||
{
|
||||
const char* name;
|
||||
const void* init;
|
||||
struct RtCompileVarList {
|
||||
const char *name;
|
||||
const void *init;
|
||||
};
|
||||
|
||||
struct RtCompileModuleList
|
||||
{
|
||||
RtCompileModuleList* next;
|
||||
const char* irData;
|
||||
struct RtCompileModuleList {
|
||||
RtCompileModuleList *next;
|
||||
const char *irData;
|
||||
int irDataSize;
|
||||
RtCompileFuncList* funcList;
|
||||
RtCompileFuncList *funcList;
|
||||
int funcListSize;
|
||||
RtCompileSymList* symList;
|
||||
RtCompileSymList *symList;
|
||||
int symListSize;
|
||||
RtCompileVarList* varList;
|
||||
RtCompileVarList *varList;
|
||||
int varListSize;
|
||||
};
|
||||
|
||||
|
@ -82,7 +78,7 @@ llvm::SmallVector<std::string, 4> getHostAttrs() {
|
|||
return features;
|
||||
}
|
||||
|
||||
using SymMap = std::map<std::string, void*>;
|
||||
using SymMap = std::map<std::string, void *>;
|
||||
|
||||
struct llvm_init_obj {
|
||||
llvm_init_obj() {
|
||||
|
@ -91,7 +87,7 @@ struct llvm_init_obj {
|
|||
}
|
||||
};
|
||||
|
||||
std::string decorate(const std::string& name) {
|
||||
std::string decorate(const std::string &name) {
|
||||
#if defined(__APPLE__)
|
||||
return "_" + name;
|
||||
#elif defined(_WIN32) && defined(_M_IX86)
|
||||
|
@ -105,8 +101,8 @@ std::string decorate(const std::string& name) {
|
|||
#endif
|
||||
}
|
||||
|
||||
auto getSymbolInProcess(const std::string& name)
|
||||
->decltype (llvm::RTDyldMemoryManager::getSymbolAddressInProcess(name)) {
|
||||
auto getSymbolInProcess(const std::string &name)
|
||||
-> decltype(llvm::RTDyldMemoryManager::getSymbolAddressInProcess(name)) {
|
||||
assert(!name.empty());
|
||||
#if defined(_WIN32)
|
||||
if ('_' == name[0]) {
|
||||
|
@ -126,7 +122,8 @@ private:
|
|||
const llvm::DataLayout dataLayout;
|
||||
#if LDC_LLVM_VER >= 500
|
||||
using ObjectLayerT = llvm::orc::RTDyldObjectLinkingLayer;
|
||||
using CompileLayerT = llvm::orc::IRCompileLayer<ObjectLayerT, llvm::orc::SimpleCompiler>;
|
||||
using CompileLayerT =
|
||||
llvm::orc::IRCompileLayer<ObjectLayerT, llvm::orc::SimpleCompiler>;
|
||||
using ModuleHandleT = std::vector<CompileLayerT::ModuleHandleT>;
|
||||
#else
|
||||
using ObjectLayerT = llvm::orc::ObjectLinkingLayer<>;
|
||||
|
@ -140,64 +137,63 @@ private:
|
|||
ModuleHandleT moduleHandle;
|
||||
|
||||
public:
|
||||
|
||||
MyJIT():
|
||||
targetmachine(llvm::EngineBuilder()
|
||||
.setRelocationModel(llvm::Reloc::Static)
|
||||
.selectTarget(llvm::Triple(llvm::sys::getProcessTriple()),
|
||||
llvm::StringRef(),
|
||||
llvm::sys::getHostCPUName(),
|
||||
getHostAttrs())),
|
||||
dataLayout(targetmachine->createDataLayout()),
|
||||
MyJIT()
|
||||
: targetmachine(
|
||||
llvm::EngineBuilder()
|
||||
.setRelocationModel(llvm::Reloc::Static)
|
||||
.selectTarget(llvm::Triple(llvm::sys::getProcessTriple()),
|
||||
llvm::StringRef(), llvm::sys::getHostCPUName(),
|
||||
getHostAttrs())),
|
||||
dataLayout(targetmachine->createDataLayout()),
|
||||
#if LDC_LLVM_VER >= 500
|
||||
objectLayer([]() { return std::make_shared<llvm::SectionMemoryManager>(); }),
|
||||
objectLayer(
|
||||
[]() { return std::make_shared<llvm::SectionMemoryManager>(); }),
|
||||
#endif
|
||||
compileLayer(objectLayer, llvm::orc::SimpleCompiler(*targetmachine))
|
||||
{
|
||||
compileLayer(objectLayer, llvm::orc::SimpleCompiler(*targetmachine)) {
|
||||
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
|
||||
}
|
||||
|
||||
llvm::TargetMachine& getTargetMachine() { return *targetmachine; }
|
||||
llvm::TargetMachine &getTargetMachine() { return *targetmachine; }
|
||||
|
||||
void addModules(std::vector<std::unique_ptr<llvm::Module>> &&modules,
|
||||
const SymMap& symMap) {
|
||||
const SymMap &symMap) {
|
||||
reset();
|
||||
// Build our symbol resolver:
|
||||
// Lambda 1: Look back into the JIT itself to find symbols that are part of
|
||||
// the same "logical dylib".
|
||||
// Lambda 2: Search for external symbols in the host process.
|
||||
auto Resolver = llvm::orc::createLambdaResolver(
|
||||
[&](const std::string& name) {
|
||||
if (auto Sym = compileLayer.findSymbol(name, false)) {
|
||||
return Sym;
|
||||
}
|
||||
return llvm::JITSymbol(nullptr);
|
||||
},
|
||||
[&](const std::string& name) {
|
||||
auto it = symMap.find(name);
|
||||
if (symMap.end() != it) {
|
||||
return llvm::JITSymbol(reinterpret_cast<llvm::JITTargetAddress>(it->second),
|
||||
llvm::JITSymbolFlags::Exported);
|
||||
}
|
||||
if (auto SymAddr = getSymbolInProcess(name)) {
|
||||
return llvm::JITSymbol(SymAddr, llvm::JITSymbolFlags::Exported);
|
||||
}
|
||||
return llvm::JITSymbol(nullptr);
|
||||
});
|
||||
[&](const std::string &name) {
|
||||
if (auto Sym = compileLayer.findSymbol(name, false)) {
|
||||
return Sym;
|
||||
}
|
||||
return llvm::JITSymbol(nullptr);
|
||||
},
|
||||
[&](const std::string &name) {
|
||||
auto it = symMap.find(name);
|
||||
if (symMap.end() != it) {
|
||||
return llvm::JITSymbol(
|
||||
reinterpret_cast<llvm::JITTargetAddress>(it->second),
|
||||
llvm::JITSymbolFlags::Exported);
|
||||
}
|
||||
if (auto SymAddr = getSymbolInProcess(name)) {
|
||||
return llvm::JITSymbol(SymAddr, llvm::JITSymbolFlags::Exported);
|
||||
}
|
||||
return llvm::JITSymbol(nullptr);
|
||||
});
|
||||
|
||||
// Add the set to the JIT with the resolver we created above
|
||||
#if LDC_LLVM_VER >= 500
|
||||
for (auto&& module: modules) {
|
||||
auto handle = compileLayer.addModule(std::move(module),
|
||||
Resolver);
|
||||
for (auto &&module : modules) {
|
||||
auto handle = compileLayer.addModule(std::move(module), Resolver);
|
||||
assert(handle);
|
||||
moduleHandle.push_back(handle.get());
|
||||
}
|
||||
modules.clear();
|
||||
#else
|
||||
moduleHandle = compileLayer.addModuleSet(std::move(modules),
|
||||
llvm::make_unique<llvm::SectionMemoryManager>(),
|
||||
std::move(Resolver));
|
||||
moduleHandle = compileLayer.addModuleSet(
|
||||
std::move(modules), llvm::make_unique<llvm::SectionMemoryManager>(),
|
||||
std::move(Resolver));
|
||||
#endif
|
||||
compiled = true;
|
||||
}
|
||||
|
@ -206,11 +202,11 @@ public:
|
|||
return compileLayer.findSymbol(name, false);
|
||||
}
|
||||
|
||||
llvm::LLVMContext& getContext() { return context; }
|
||||
llvm::LLVMContext &getContext() { return context; }
|
||||
|
||||
void removeModule(const ModuleHandleT& H) {
|
||||
void removeModule(const ModuleHandleT &H) {
|
||||
#if LDC_LLVM_VER >= 500
|
||||
for (auto&& handle: H) {
|
||||
for (auto &&handle : H) {
|
||||
cantFail(compileLayer.removeModule(handle));
|
||||
}
|
||||
#else
|
||||
|
@ -225,42 +221,37 @@ public:
|
|||
compiled = false;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
void setRtCompileVars(const Context &context,
|
||||
llvm::Module& module,
|
||||
void setRtCompileVars(const Context &context, llvm::Module &module,
|
||||
llvm::ArrayRef<RtCompileVarList> vals) {
|
||||
for (auto&& val: vals) {
|
||||
for (auto &&val : vals) {
|
||||
setRtCompileVar(context, module, val.name, val.init);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
llvm::ArrayRef<T> toArray(T* ptr, size_t size) {
|
||||
template <typename T> llvm::ArrayRef<T> toArray(T *ptr, size_t size) {
|
||||
return llvm::ArrayRef<T>(ptr, size);
|
||||
}
|
||||
|
||||
void* resolveSymbol(llvm::JITSymbol& symbol) {
|
||||
void *resolveSymbol(llvm::JITSymbol &symbol) {
|
||||
auto addr = symbol.getAddress();
|
||||
#if LDC_LLVM_VER >= 500
|
||||
if (!addr) {
|
||||
consumeError(addr.takeError());
|
||||
return nullptr;
|
||||
}
|
||||
else {
|
||||
return reinterpret_cast<void*>(addr.get());
|
||||
} else {
|
||||
return reinterpret_cast<void *>(addr.get());
|
||||
}
|
||||
#else
|
||||
return reinterpret_cast<void*>(addr);
|
||||
return reinterpret_cast<void *>(addr);
|
||||
#endif
|
||||
}
|
||||
|
||||
struct JitFinaliser final {
|
||||
MyJIT& jit;
|
||||
MyJIT &jit;
|
||||
bool finalized = false;
|
||||
explicit JitFinaliser(MyJIT& j):
|
||||
jit(j) {}
|
||||
explicit JitFinaliser(MyJIT &j) : jit(j) {}
|
||||
~JitFinaliser() {
|
||||
if (!finalized) {
|
||||
jit.reset();
|
||||
|
@ -270,18 +261,18 @@ struct JitFinaliser final {
|
|||
void finalze() { finalized = true; }
|
||||
};
|
||||
|
||||
MyJIT& getJit()
|
||||
{
|
||||
MyJIT &getJit() {
|
||||
static MyJIT jit;
|
||||
return jit;
|
||||
}
|
||||
|
||||
void rtCompileProcessImplSoInternal(const RtCompileModuleList* modlist_head, const Context& context) {
|
||||
void rtCompileProcessImplSoInternal(const RtCompileModuleList *modlist_head,
|
||||
const Context &context) {
|
||||
interruptPoint(context, "Init");
|
||||
MyJIT& myJit = getJit();
|
||||
MyJIT &myJit = getJit();
|
||||
auto current = modlist_head;
|
||||
|
||||
std::vector<std::pair<std::string, void**> > functions;
|
||||
std::vector<std::pair<std::string, void **>> functions;
|
||||
std::vector<std::unique_ptr<llvm::Module>> ms;
|
||||
SymMap symMap;
|
||||
OptimizerSettings settings;
|
||||
|
@ -289,22 +280,21 @@ void rtCompileProcessImplSoInternal(const RtCompileModuleList* modlist_head, con
|
|||
settings.sizeLevel = context.sizeLevel;
|
||||
while (nullptr != current) {
|
||||
interruptPoint(context, "load IR");
|
||||
auto buff = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(current->irData, current->irDataSize), "", false);
|
||||
auto buff = llvm::MemoryBuffer::getMemBuffer(
|
||||
llvm::StringRef(current->irData, current->irDataSize), "", false);
|
||||
interruptPoint(context, "parse IR");
|
||||
auto mod = llvm::parseBitcodeFile(*buff, myJit.getContext());
|
||||
if (!mod) {
|
||||
fatal(context, "Unable to parse IR");
|
||||
}
|
||||
else {
|
||||
llvm::Module& module = **mod;
|
||||
} else {
|
||||
llvm::Module &module = **mod;
|
||||
const auto name = module.getName();
|
||||
interruptPoint(context,"Verify module", name.data());
|
||||
interruptPoint(context, "Verify module", name.data());
|
||||
::verifyModule(context, module);
|
||||
module.setDataLayout(myJit.getTargetMachine().createDataLayout());
|
||||
|
||||
interruptPoint(context, "setRtCompileVars", name.data());
|
||||
setRtCompileVars(context,
|
||||
module,
|
||||
setRtCompileVars(context, module,
|
||||
toArray(current->varList, current->varListSize));
|
||||
|
||||
interruptPoint(context, "Optimize module", name.data());
|
||||
|
@ -313,7 +303,7 @@ void rtCompileProcessImplSoInternal(const RtCompileModuleList* modlist_head, con
|
|||
interruptPoint(context, "Verify module", name.data());
|
||||
::verifyModule(context, module);
|
||||
if (nullptr != context.dumpHandler) {
|
||||
auto callback =[&](const char* str, size_t len) {
|
||||
auto callback = [&](const char *str, size_t len) {
|
||||
context.dumpHandler(context.dumpHandlerData, str, len);
|
||||
};
|
||||
|
||||
|
@ -322,11 +312,11 @@ void rtCompileProcessImplSoInternal(const RtCompileModuleList* modlist_head, con
|
|||
}
|
||||
ms.push_back(std::move(*mod));
|
||||
|
||||
for (auto&& fun: toArray(current->funcList, current->funcListSize)) {
|
||||
for (auto &&fun : toArray(current->funcList, current->funcListSize)) {
|
||||
functions.push_back(std::make_pair(fun.name, fun.func));
|
||||
}
|
||||
|
||||
for (auto&& sym: toArray(current->symList, current->symListSize)) {
|
||||
for (auto &&sym : toArray(current->symList, current->symListSize)) {
|
||||
symMap.insert(std::make_pair(decorate(sym.name), sym.sym));
|
||||
}
|
||||
}
|
||||
|
@ -338,15 +328,15 @@ void rtCompileProcessImplSoInternal(const RtCompileModuleList* modlist_head, con
|
|||
|
||||
JitFinaliser jitFinalizer(myJit);
|
||||
interruptPoint(context, "Resolve functions");
|
||||
for (auto&& fun: functions) {
|
||||
for (auto &&fun : functions) {
|
||||
auto decorated = decorate(fun.first);
|
||||
auto symbol = myJit.findSymbol(decorated);
|
||||
auto addr = resolveSymbol(symbol);
|
||||
if (nullptr == addr) {
|
||||
std::string desc = std::string("Symbol not found in jitted code: \"") + fun.first + "\" (\"" + decorated + "\")";
|
||||
std::string desc = std::string("Symbol not found in jitted code: \"") +
|
||||
fun.first + "\" (\"" + decorated + "\")";
|
||||
fatal(context, desc);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
*fun.second = addr;
|
||||
}
|
||||
|
||||
|
@ -367,14 +357,11 @@ extern "C" {
|
|||
#ifdef _WIN32
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void rtCompileProcessImplSo(const void* modlist_head,
|
||||
const Context* context,
|
||||
size_t contextSize) {
|
||||
void rtCompileProcessImplSo(const void *modlist_head,
|
||||
const Context *context, size_t contextSize) {
|
||||
assert(nullptr != context);
|
||||
assert(sizeof(*context) == contextSize);
|
||||
rtCompileProcessImplSoInternal(
|
||||
static_cast<const RtCompileModuleList*>(modlist_head),
|
||||
*context);
|
||||
static_cast<const RtCompileModuleList *>(modlist_head), *context);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue