Commit ee18f390 by Ben Clayton Committed by Antonio Maiorano

LLVM: Replace legacy ORC JIT with new API

Also fix crashes when building with `REACTOR_EMIT_DEBUG_INFO`, but with a no-debug-info build. Change-Id: I183473a78235b938b44bf4a017f5239c78827285 Bug: b/171236524 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/43811 Kokoro-Result: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarAntonio Maiorano <amaiorano@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent df17a761
...@@ -24,31 +24,12 @@ __pragma(warning(push)) ...@@ -24,31 +24,12 @@ __pragma(warning(push))
__pragma(warning(disable : 4146)) // unary minus operator applied to unsigned type, result still unsigned __pragma(warning(disable : 4146)) // unary minus operator applied to unsigned type, result still unsigned
#endif #endif
#include "llvm/Analysis/LoopPass.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h" #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h" #include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/TargetSelect.h" #include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Transforms/Coroutines.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Transforms/InstCombine/InstCombine.h" #include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h" #include "llvm/Transforms/Scalar/GVN.h"
...@@ -57,9 +38,6 @@ __pragma(warning(push)) ...@@ -57,9 +38,6 @@ __pragma(warning(push))
__pragma(warning(pop)) __pragma(warning(pop))
#endif #endif
#include <atomic>
#include <unordered_map>
#if defined(_WIN64) #if defined(_WIN64)
extern "C" void __chkstk(); extern "C" void __chkstk();
#elif defined(_WIN32) #elif defined(_WIN32)
...@@ -81,119 +59,57 @@ namespace { ...@@ -81,119 +59,57 @@ namespace {
class JITGlobals class JITGlobals
{ {
public: public:
using TargetMachineSPtr = std::shared_ptr<llvm::TargetMachine>;
static JITGlobals *get(); static JITGlobals *get();
const std::string mcpu; llvm::orc::JITTargetMachineBuilder getTargetMachineBuilder(rr::Optimization::Level optLevel) const;
const std::vector<std::string> mattrs; const llvm::DataLayout &getDataLayout() const;
const char *const march; const llvm::Triple getTargetTriple() const;
const llvm::TargetOptions targetOptions;
const llvm::DataLayout dataLayout;
TargetMachineSPtr createTargetMachine(rr::Optimization::Level optlevel);
private: private:
static JITGlobals create(); JITGlobals(const llvm::orc::JITTargetMachineBuilder &jtmb, const llvm::DataLayout &dataLayout);
static llvm::CodeGenOpt::Level toLLVM(rr::Optimization::Level level); static llvm::CodeGenOpt::Level toLLVM(rr::Optimization::Level level);
JITGlobals(const char *mcpu, const llvm::orc::JITTargetMachineBuilder jtmb;
const std::vector<std::string> &mattrs, const llvm::DataLayout dataLayout;
const char *march,
const llvm::TargetOptions &targetOptions,
const llvm::DataLayout &dataLayout);
JITGlobals(const JITGlobals &) = default;
}; };
JITGlobals *JITGlobals::get() JITGlobals *JITGlobals::get()
{ {
static JITGlobals instance = create(); static JITGlobals instance = [] {
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
auto jtmb = llvm::orc::JITTargetMachineBuilder::detectHost();
ASSERT_MSG(jtmb, "JITTargetMachineBuilder::detectHost() failed");
auto dataLayout = jtmb->getDefaultDataLayoutForTarget();
ASSERT_MSG(dataLayout, "JITTargetMachineBuilder::getDefaultDataLayoutForTarget() failed");
return JITGlobals(jtmb.get(), dataLayout.get());
}();
return &instance; return &instance;
} }
JITGlobals::TargetMachineSPtr JITGlobals::createTargetMachine(rr::Optimization::Level optlevel) llvm::orc::JITTargetMachineBuilder JITGlobals::getTargetMachineBuilder(rr::Optimization::Level optLevel) const
{ {
#ifdef ENABLE_RR_DEBUG_INFO llvm::orc::JITTargetMachineBuilder out = jtmb;
auto llvmOptLevel = toLLVM(rr::Optimization::Level::None); out.setCodeGenOptLevel(toLLVM(optLevel));
#else // ENABLE_RR_DEBUG_INFO return out;
auto llvmOptLevel = toLLVM(optlevel);
#endif // ENABLE_RR_DEBUG_INFO
return TargetMachineSPtr(llvm::EngineBuilder()
.setOptLevel(llvmOptLevel)
.setMCPU(mcpu)
.setMArch(march)
.setMAttrs(mattrs)
.setTargetOptions(targetOptions)
.selectTarget());
} }
JITGlobals JITGlobals::create() const llvm::DataLayout &JITGlobals::getDataLayout() const
{ {
struct LLVMInitializer return dataLayout;
{ }
LLVMInitializer()
{
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
}
};
static LLVMInitializer initializeLLVM;
auto mcpu = llvm::sys::getHostCPUName();
llvm::StringMap<bool> features;
bool ok = llvm::sys::getHostCPUFeatures(features);
#if defined(__i386__) || defined(__x86_64__) || \
(defined(__linux__) && (defined(__arm__) || defined(__aarch64__)))
ASSERT_MSG(ok, "llvm::sys::getHostCPUFeatures returned false");
#else
(void)ok; // getHostCPUFeatures always returns false on other platforms
#endif
std::vector<std::string> mattrs;
for(auto &feature : features)
{
if(feature.second) { mattrs.push_back(feature.first().str()); }
}
const char *march = nullptr;
#if defined(__x86_64__)
march = "x86-64";
#elif defined(__i386__)
march = "x86";
#elif defined(__aarch64__)
march = "arm64";
#elif defined(__arm__)
march = "arm";
#elif defined(__mips__)
# if defined(__mips64)
march = "mips64el";
# else
march = "mipsel";
# endif
#elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
march = "ppc64le";
#else
# error "unknown architecture"
#endif
llvm::TargetOptions targetOptions;
targetOptions.UnsafeFPMath = false;
auto targetMachine = std::unique_ptr<llvm::TargetMachine>(
llvm::EngineBuilder()
.setOptLevel(llvm::CodeGenOpt::None)
.setMCPU(mcpu)
.setMArch(march)
.setMAttrs(mattrs)
.setTargetOptions(targetOptions)
.selectTarget());
auto dataLayout = targetMachine->createDataLayout(); const llvm::Triple JITGlobals::getTargetTriple() const
{
return jtmb.getTargetTriple();
}
return JITGlobals(mcpu.data(), mattrs, march, targetOptions, dataLayout); JITGlobals::JITGlobals(const llvm::orc::JITTargetMachineBuilder &jtmb, const llvm::DataLayout &dataLayout)
: jtmb(jtmb)
, dataLayout(dataLayout)
{
} }
llvm::CodeGenOpt::Level JITGlobals::toLLVM(rr::Optimization::Level level) llvm::CodeGenOpt::Level JITGlobals::toLLVM(rr::Optimization::Level level)
...@@ -209,19 +125,6 @@ llvm::CodeGenOpt::Level JITGlobals::toLLVM(rr::Optimization::Level level) ...@@ -209,19 +125,6 @@ llvm::CodeGenOpt::Level JITGlobals::toLLVM(rr::Optimization::Level level)
return ::llvm::CodeGenOpt::Default; return ::llvm::CodeGenOpt::Default;
} }
JITGlobals::JITGlobals(const char *mcpu,
const std::vector<std::string> &mattrs,
const char *march,
const llvm::TargetOptions &targetOptions,
const llvm::DataLayout &dataLayout)
: mcpu(mcpu)
, mattrs(mattrs)
, march(march)
, targetOptions(targetOptions)
, dataLayout(dataLayout)
{
}
class MemoryMapper final : public llvm::SectionMemoryManager::MemoryMapper class MemoryMapper final : public llvm::SectionMemoryManager::MemoryMapper
{ {
public: public:
...@@ -349,7 +252,7 @@ static uint32_t sync_fetch_and_op(uint32_t volatile *ptr, uint32_t val, F f) ...@@ -349,7 +252,7 @@ static uint32_t sync_fetch_and_op(uint32_t volatile *ptr, uint32_t val, F f)
} }
#endif #endif
void *resolveExternalSymbol(const char *name) class ExternalSymbolGenerator : public llvm::orc::JITDylib::DefinitionGenerator
{ {
struct Atomic struct Atomic
{ {
...@@ -379,255 +282,272 @@ void *resolveExternalSymbol(const char *name) ...@@ -379,255 +282,272 @@ void *resolveExternalSymbol(const char *name)
} }
}; };
struct F static void nop() {}
{ static void neverCalled() { UNREACHABLE("Should never be called"); }
static void nop() {}
static void neverCalled() { UNREACHABLE("Should never be called"); }
static void *coroutine_alloc_frame(size_t size) { return alignedAlloc(size, 16); } static void *coroutine_alloc_frame(size_t size) { return alignedAlloc(size, 16); }
static void coroutine_free_frame(void *ptr) { alignedFree(ptr); } static void coroutine_free_frame(void *ptr) { alignedFree(ptr); }
#ifdef __ANDROID__ #ifdef __ANDROID__
// forwarders since we can't take address of builtins // forwarders since we can't take address of builtins
static void sync_synchronize() { __sync_synchronize(); } static void sync_synchronize() { __sync_synchronize(); }
static uint32_t sync_fetch_and_add_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_add_4(ptr, val); } static uint32_t sync_fetch_and_add_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_add_4(ptr, val); }
static uint32_t sync_fetch_and_and_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_and_4(ptr, val); } static uint32_t sync_fetch_and_and_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_and_4(ptr, val); }
static uint32_t sync_fetch_and_or_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_or_4(ptr, val); } static uint32_t sync_fetch_and_or_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_or_4(ptr, val); }
static uint32_t sync_fetch_and_xor_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_xor_4(ptr, val); } static uint32_t sync_fetch_and_xor_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_xor_4(ptr, val); }
static uint32_t sync_fetch_and_sub_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_sub_4(ptr, val); } static uint32_t sync_fetch_and_sub_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_sub_4(ptr, val); }
static uint32_t sync_lock_test_and_set_4(uint32_t *ptr, uint32_t val) { return __sync_lock_test_and_set_4(ptr, val); } static uint32_t sync_lock_test_and_set_4(uint32_t *ptr, uint32_t val) { return __sync_lock_test_and_set_4(ptr, val); }
static uint32_t sync_val_compare_and_swap_4(uint32_t *ptr, uint32_t expected, uint32_t desired) { return __sync_val_compare_and_swap_4(ptr, expected, desired); } static uint32_t sync_val_compare_and_swap_4(uint32_t *ptr, uint32_t expected, uint32_t desired) { return __sync_val_compare_and_swap_4(ptr, expected, desired); }
static uint32_t sync_fetch_and_max_4(uint32_t *ptr, uint32_t val) static uint32_t sync_fetch_and_max_4(uint32_t *ptr, uint32_t val)
{ {
return sync_fetch_and_op(ptr, val, [](int32_t a, int32_t b) { return std::max(a, b); }); return sync_fetch_and_op(ptr, val, [](int32_t a, int32_t b) { return std::max(a, b); });
} }
static uint32_t sync_fetch_and_min_4(uint32_t *ptr, uint32_t val) static uint32_t sync_fetch_and_min_4(uint32_t *ptr, uint32_t val)
{ {
return sync_fetch_and_op(ptr, val, [](int32_t a, int32_t b) { return std::min(a, b); }); return sync_fetch_and_op(ptr, val, [](int32_t a, int32_t b) { return std::min(a, b); });
} }
static uint32_t sync_fetch_and_umax_4(uint32_t *ptr, uint32_t val) static uint32_t sync_fetch_and_umax_4(uint32_t *ptr, uint32_t val)
{ {
return sync_fetch_and_op(ptr, val, [](uint32_t a, uint32_t b) { return std::max(a, b); }); return sync_fetch_and_op(ptr, val, [](uint32_t a, uint32_t b) { return std::max(a, b); });
} }
static uint32_t sync_fetch_and_umin_4(uint32_t *ptr, uint32_t val) static uint32_t sync_fetch_and_umin_4(uint32_t *ptr, uint32_t val)
{ {
return sync_fetch_and_op(ptr, val, [](uint32_t a, uint32_t b) { return std::min(a, b); }); return sync_fetch_and_op(ptr, val, [](uint32_t a, uint32_t b) { return std::min(a, b); });
} }
#endif #endif
};
class Resolver class Resolver
{ {
public: public:
using FunctionMap = std::unordered_map<std::string, void *>; using FunctionMap = llvm::StringMap<void *>;
FunctionMap functions; FunctionMap functions;
Resolver() Resolver()
{ {
#ifdef ENABLE_RR_PRINT #ifdef ENABLE_RR_PRINT
functions.emplace("rr::DebugPrintf", reinterpret_cast<void *>(rr::DebugPrintf)); functions.try_emplace("rr::DebugPrintf", reinterpret_cast<void *>(rr::DebugPrintf));
#endif #endif
functions.emplace("nop", reinterpret_cast<void *>(F::nop)); functions.try_emplace("nop", reinterpret_cast<void *>(nop));
functions.emplace("floorf", reinterpret_cast<void *>(floorf)); functions.try_emplace("floorf", reinterpret_cast<void *>(floorf));
functions.emplace("nearbyintf", reinterpret_cast<void *>(nearbyintf)); functions.try_emplace("nearbyintf", reinterpret_cast<void *>(nearbyintf));
functions.emplace("truncf", reinterpret_cast<void *>(truncf)); functions.try_emplace("truncf", reinterpret_cast<void *>(truncf));
functions.emplace("printf", reinterpret_cast<void *>(printf)); functions.try_emplace("printf", reinterpret_cast<void *>(printf));
functions.emplace("puts", reinterpret_cast<void *>(puts)); functions.try_emplace("puts", reinterpret_cast<void *>(puts));
functions.emplace("fmodf", reinterpret_cast<void *>(fmodf)); functions.try_emplace("fmodf", reinterpret_cast<void *>(fmodf));
functions.emplace("sinf", reinterpret_cast<void *>(sinf)); functions.try_emplace("sinf", reinterpret_cast<void *>(sinf));
functions.emplace("cosf", reinterpret_cast<void *>(cosf)); functions.try_emplace("cosf", reinterpret_cast<void *>(cosf));
functions.emplace("asinf", reinterpret_cast<void *>(asinf)); functions.try_emplace("asinf", reinterpret_cast<void *>(asinf));
functions.emplace("acosf", reinterpret_cast<void *>(acosf)); functions.try_emplace("acosf", reinterpret_cast<void *>(acosf));
functions.emplace("atanf", reinterpret_cast<void *>(atanf)); functions.try_emplace("atanf", reinterpret_cast<void *>(atanf));
functions.emplace("sinhf", reinterpret_cast<void *>(sinhf)); functions.try_emplace("sinhf", reinterpret_cast<void *>(sinhf));
functions.emplace("coshf", reinterpret_cast<void *>(coshf)); functions.try_emplace("coshf", reinterpret_cast<void *>(coshf));
functions.emplace("tanhf", reinterpret_cast<void *>(tanhf)); functions.try_emplace("tanhf", reinterpret_cast<void *>(tanhf));
functions.emplace("asinhf", reinterpret_cast<void *>(asinhf)); functions.try_emplace("asinhf", reinterpret_cast<void *>(asinhf));
functions.emplace("acoshf", reinterpret_cast<void *>(acoshf)); functions.try_emplace("acoshf", reinterpret_cast<void *>(acoshf));
functions.emplace("atanhf", reinterpret_cast<void *>(atanhf)); functions.try_emplace("atanhf", reinterpret_cast<void *>(atanhf));
functions.emplace("atan2f", reinterpret_cast<void *>(atan2f)); functions.try_emplace("atan2f", reinterpret_cast<void *>(atan2f));
functions.emplace("powf", reinterpret_cast<void *>(powf)); functions.try_emplace("powf", reinterpret_cast<void *>(powf));
functions.emplace("expf", reinterpret_cast<void *>(expf)); functions.try_emplace("expf", reinterpret_cast<void *>(expf));
functions.emplace("logf", reinterpret_cast<void *>(logf)); functions.try_emplace("logf", reinterpret_cast<void *>(logf));
functions.emplace("exp2f", reinterpret_cast<void *>(exp2f)); functions.try_emplace("exp2f", reinterpret_cast<void *>(exp2f));
functions.emplace("log2f", reinterpret_cast<void *>(log2f)); functions.try_emplace("log2f", reinterpret_cast<void *>(log2f));
functions.emplace("sin", reinterpret_cast<void *>(static_cast<double (*)(double)>(sin))); functions.try_emplace("sin", reinterpret_cast<void *>(static_cast<double (*)(double)>(sin)));
functions.emplace("cos", reinterpret_cast<void *>(static_cast<double (*)(double)>(cos))); functions.try_emplace("cos", reinterpret_cast<void *>(static_cast<double (*)(double)>(cos)));
functions.emplace("asin", reinterpret_cast<void *>(static_cast<double (*)(double)>(asin))); functions.try_emplace("asin", reinterpret_cast<void *>(static_cast<double (*)(double)>(asin)));
functions.emplace("acos", reinterpret_cast<void *>(static_cast<double (*)(double)>(acos))); functions.try_emplace("acos", reinterpret_cast<void *>(static_cast<double (*)(double)>(acos)));
functions.emplace("atan", reinterpret_cast<void *>(static_cast<double (*)(double)>(atan))); functions.try_emplace("atan", reinterpret_cast<void *>(static_cast<double (*)(double)>(atan)));
functions.emplace("sinh", reinterpret_cast<void *>(static_cast<double (*)(double)>(sinh))); functions.try_emplace("sinh", reinterpret_cast<void *>(static_cast<double (*)(double)>(sinh)));
functions.emplace("cosh", reinterpret_cast<void *>(static_cast<double (*)(double)>(cosh))); functions.try_emplace("cosh", reinterpret_cast<void *>(static_cast<double (*)(double)>(cosh)));
functions.emplace("tanh", reinterpret_cast<void *>(static_cast<double (*)(double)>(tanh))); functions.try_emplace("tanh", reinterpret_cast<void *>(static_cast<double (*)(double)>(tanh)));
functions.emplace("asinh", reinterpret_cast<void *>(static_cast<double (*)(double)>(asinh))); functions.try_emplace("asinh", reinterpret_cast<void *>(static_cast<double (*)(double)>(asinh)));
functions.emplace("acosh", reinterpret_cast<void *>(static_cast<double (*)(double)>(acosh))); functions.try_emplace("acosh", reinterpret_cast<void *>(static_cast<double (*)(double)>(acosh)));
functions.emplace("atanh", reinterpret_cast<void *>(static_cast<double (*)(double)>(atanh))); functions.try_emplace("atanh", reinterpret_cast<void *>(static_cast<double (*)(double)>(atanh)));
functions.emplace("atan2", reinterpret_cast<void *>(static_cast<double (*)(double, double)>(atan2))); functions.try_emplace("atan2", reinterpret_cast<void *>(static_cast<double (*)(double, double)>(atan2)));
functions.emplace("pow", reinterpret_cast<void *>(static_cast<double (*)(double, double)>(pow))); functions.try_emplace("pow", reinterpret_cast<void *>(static_cast<double (*)(double, double)>(pow)));
functions.emplace("exp", reinterpret_cast<void *>(static_cast<double (*)(double)>(exp))); functions.try_emplace("exp", reinterpret_cast<void *>(static_cast<double (*)(double)>(exp)));
functions.emplace("log", reinterpret_cast<void *>(static_cast<double (*)(double)>(log))); functions.try_emplace("log", reinterpret_cast<void *>(static_cast<double (*)(double)>(log)));
functions.emplace("exp2", reinterpret_cast<void *>(static_cast<double (*)(double)>(exp2))); functions.try_emplace("exp2", reinterpret_cast<void *>(static_cast<double (*)(double)>(exp2)));
functions.emplace("log2", reinterpret_cast<void *>(static_cast<double (*)(double)>(log2))); functions.try_emplace("log2", reinterpret_cast<void *>(static_cast<double (*)(double)>(log2)));
functions.emplace("atomic_load", reinterpret_cast<void *>(Atomic::load)); functions.try_emplace("atomic_load", reinterpret_cast<void *>(Atomic::load));
functions.emplace("atomic_store", reinterpret_cast<void *>(Atomic::store)); functions.try_emplace("atomic_store", reinterpret_cast<void *>(Atomic::store));
// FIXME(b/119409619): use an allocator here so we can control all memory allocations // FIXME(b/119409619): use an allocator here so we can control all memory allocations
functions.emplace("coroutine_alloc_frame", reinterpret_cast<void *>(F::coroutine_alloc_frame)); functions.try_emplace("coroutine_alloc_frame", reinterpret_cast<void *>(coroutine_alloc_frame));
functions.emplace("coroutine_free_frame", reinterpret_cast<void *>(F::coroutine_free_frame)); functions.try_emplace("coroutine_free_frame", reinterpret_cast<void *>(coroutine_free_frame));
#ifdef __APPLE__ #ifdef __APPLE__
functions.emplace("sincosf_stret", reinterpret_cast<void *>(__sincosf_stret)); functions.try_emplace("sincosf_stret", reinterpret_cast<void *>(__sincosf_stret));
#elif defined(__linux__) #elif defined(__linux__)
functions.emplace("sincosf", reinterpret_cast<void *>(sincosf)); functions.try_emplace("sincosf", reinterpret_cast<void *>(sincosf));
#elif defined(_WIN64) #elif defined(_WIN64)
functions.emplace("chkstk", reinterpret_cast<void *>(__chkstk)); functions.try_emplace("chkstk", reinterpret_cast<void *>(__chkstk));
#elif defined(_WIN32) #elif defined(_WIN32)
functions.emplace("chkstk", reinterpret_cast<void *>(_chkstk)); functions.try_emplace("chkstk", reinterpret_cast<void *>(_chkstk));
#endif #endif
#ifdef __ARM_EABI__ #ifdef __ARM_EABI__
functions.emplace("aeabi_idivmod", reinterpret_cast<void *>(__aeabi_idivmod)); functions.try_emplace("aeabi_idivmod", reinterpret_cast<void *>(__aeabi_idivmod));
#endif #endif
#ifdef __ANDROID__ #ifdef __ANDROID__
functions.emplace("aeabi_unwind_cpp_pr0", reinterpret_cast<void *>(F::neverCalled)); functions.try_emplace("aeabi_unwind_cpp_pr0", reinterpret_cast<void *>(F::neverCalled));
functions.emplace("sync_synchronize", reinterpret_cast<void *>(F::sync_synchronize)); functions.try_emplace("sync_synchronize", reinterpret_cast<void *>(F::sync_synchronize));
functions.emplace("sync_fetch_and_add_4", reinterpret_cast<void *>(F::sync_fetch_and_add_4)); functions.try_emplace("sync_fetch_and_add_4", reinterpret_cast<void *>(F::sync_fetch_and_add_4));
functions.emplace("sync_fetch_and_and_4", reinterpret_cast<void *>(F::sync_fetch_and_and_4)); functions.try_emplace("sync_fetch_and_and_4", reinterpret_cast<void *>(F::sync_fetch_and_and_4));
functions.emplace("sync_fetch_and_or_4", reinterpret_cast<void *>(F::sync_fetch_and_or_4)); functions.try_emplace("sync_fetch_and_or_4", reinterpret_cast<void *>(F::sync_fetch_and_or_4));
functions.emplace("sync_fetch_and_xor_4", reinterpret_cast<void *>(F::sync_fetch_and_xor_4)); functions.try_emplace("sync_fetch_and_xor_4", reinterpret_cast<void *>(F::sync_fetch_and_xor_4));
functions.emplace("sync_fetch_and_sub_4", reinterpret_cast<void *>(F::sync_fetch_and_sub_4)); functions.try_emplace("sync_fetch_and_sub_4", reinterpret_cast<void *>(F::sync_fetch_and_sub_4));
functions.emplace("sync_lock_test_and_set_4", reinterpret_cast<void *>(F::sync_lock_test_and_set_4)); functions.try_emplace("sync_lock_test_and_set_4", reinterpret_cast<void *>(F::sync_lock_test_and_set_4));
functions.emplace("sync_val_compare_and_swap_4", reinterpret_cast<void *>(F::sync_val_compare_and_swap_4)); functions.try_emplace("sync_val_compare_and_swap_4", reinterpret_cast<void *>(F::sync_val_compare_and_swap_4));
functions.emplace("sync_fetch_and_max_4", reinterpret_cast<void *>(F::sync_fetch_and_max_4)); functions.try_emplace("sync_fetch_and_max_4", reinterpret_cast<void *>(F::sync_fetch_and_max_4));
functions.emplace("sync_fetch_and_min_4", reinterpret_cast<void *>(F::sync_fetch_and_min_4)); functions.try_emplace("sync_fetch_and_min_4", reinterpret_cast<void *>(F::sync_fetch_and_min_4));
functions.emplace("sync_fetch_and_umax_4", reinterpret_cast<void *>(F::sync_fetch_and_umax_4)); functions.try_emplace("sync_fetch_and_umax_4", reinterpret_cast<void *>(F::sync_fetch_and_umax_4));
functions.emplace("sync_fetch_and_umin_4", reinterpret_cast<void *>(F::sync_fetch_and_umin_4)); functions.try_emplace("sync_fetch_and_umin_4", reinterpret_cast<void *>(F::sync_fetch_and_umin_4));
#endif #endif
#if __has_feature(memory_sanitizer) #if __has_feature(memory_sanitizer)
functions.emplace("msan_unpoison", reinterpret_cast<void *>(__msan_unpoison)); functions.try_emplace("msan_unpoison", reinterpret_cast<void *>(__msan_unpoison));
#endif #endif
} }
}; };
static Resolver resolver; llvm::Error tryToGenerate(llvm::orc::LookupKind kind,
llvm::orc::JITDylib &dylib,
llvm::orc::JITDylibLookupFlags flags,
const llvm::orc::SymbolLookupSet &set) override
{
static Resolver resolver;
// Trim off any underscores from the start of the symbol. LLVM likes llvm::orc::SymbolMap symbols;
// to append these on macOS.
const char *trimmed = name;
while(trimmed[0] == '_') { trimmed++; }
auto it = resolver.functions.find(trimmed); #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
// Missing functions will likely make the module fail in exciting non-obvious ways. std::string missing;
ASSERT_MSG(it != resolver.functions.end(), "Missing external function: '%s'", name); #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
return it->second;
} for(auto symbol : set)
{
auto name = symbol.first;
// Trim off any underscores from the start of the symbol. LLVM likes
// to append these on macOS.
auto trimmed = (*name).drop_while([](char c) { return c == '_'; });
auto it = resolver.functions.find(trimmed.str());
if(it != resolver.functions.end())
{
symbols[name] = llvm::JITEvaluatedSymbol(
static_cast<llvm::JITTargetAddress>(reinterpret_cast<uintptr_t>(it->second)),
llvm::JITSymbolFlags::Exported);
}
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
else
{
missing += (missing.empty() ? "'" : ", '") + (*name).str() + "'";
}
#endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
}
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
// Missing functions will likely make the module fail in exciting non-obvious ways.
if(!missing.empty())
{
WARN("Missing external functions: %s", missing.c_str());
}
#endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
if(symbols.empty())
{
return llvm::Error::success();
}
return dylib.define(llvm::orc::absoluteSymbols(std::move(symbols)));
}
};
// JITRoutine is a rr::Routine that holds a LLVM JIT session, compiler and // JITRoutine is a rr::Routine that holds a LLVM JIT session, compiler and
// object layer as each routine may require different target machine // object layer as each routine may require different target machine
// settings and no Reactor routine directly links against another. // settings and no Reactor routine directly links against another.
class JITRoutine : public rr::Routine class JITRoutine : public rr::Routine
{ {
using ObjLayer = llvm::orc::LegacyRTDyldObjectLinkingLayer; using ObjLayer = llvm::orc::RTDyldObjectLinkingLayer;
using CompileLayer = llvm::orc::LegacyIRCompileLayer<ObjLayer, llvm::orc::SimpleCompiler>; using CompileLayer = llvm::orc::IRCompileLayer;
public: llvm::orc::RTDyldObjectLinkingLayer objectLayer;
#if defined(__clang__) llvm::orc::IRCompileLayer compileLayer;
// TODO(bclayton): Switch to new JIT llvm::orc::MangleAndInterner mangle;
// error: 'LegacyIRCompileLayer' is deprecated: ORCv1 layers (layers with the 'Legacy' prefix) are deprecated. llvm::orc::ThreadSafeContext ctx;
// Please use the ORCv2 IRCompileLayer instead [-Werror,-Wdeprecated-declarations] llvm::orc::ExecutionSession session;
# pragma clang diagnostic push llvm::orc::JITDylib &dylib;
# pragma clang diagnostic ignored "-Wdeprecated-declarations" std::vector<const void *> addresses;
#elif defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
public:
JITRoutine( JITRoutine(
std::unique_ptr<llvm::Module> module, std::unique_ptr<llvm::Module> module,
llvm::Function **funcs, llvm::Function **funcs,
size_t count, size_t count,
const rr::Config &config) const rr::Config &config)
: resolver(createLegacyLookupResolver( : objectLayer(session, []() { return std::make_unique<llvm::SectionMemoryManager>(new MemoryMapper()); })
session, , compileLayer(session, objectLayer, std::make_unique<llvm::orc::ConcurrentIRCompiler>(JITGlobals::get()->getTargetMachineBuilder(config.getOptimization().getLevel())))
[&](const llvm::StringRef &name) { , mangle(session, JITGlobals::get()->getDataLayout())
void *func = resolveExternalSymbol(name.str().c_str()); , ctx(std::make_unique<llvm::LLVMContext>())
if(func != nullptr) , dylib(session.createJITDylib("<routine>"))
{
return llvm::JITSymbol(
reinterpret_cast<uintptr_t>(func), llvm::JITSymbolFlags::Absolute);
}
return objLayer.findSymbol(name, true);
},
[](llvm::Error err) {
if(err)
{
// TODO: Log the symbol resolution errors.
return;
}
}))
, targetMachine(JITGlobals::get()->createTargetMachine(config.getOptimization().getLevel()))
, compileLayer(objLayer, llvm::orc::SimpleCompiler(*targetMachine))
, objLayer(
session,
[this](llvm::orc::VModuleKey) {
return ObjLayer::Resources{ std::make_shared<llvm::SectionMemoryManager>(&memoryMapper), resolver };
},
ObjLayer::NotifyLoadedFtor(),
[](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj, const llvm::RuntimeDyld::LoadedObjectInfo &L) {
#ifdef ENABLE_RR_DEBUG_INFO
rr::DebugInfo::NotifyObjectEmitted(Obj, L);
#endif // ENABLE_RR_DEBUG_INFO
},
[](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj) {
#ifdef ENABLE_RR_DEBUG_INFO
rr::DebugInfo::NotifyFreeingObject(Obj);
#endif // ENABLE_RR_DEBUG_INFO
})
, addresses(count) , addresses(count)
{ {
#if defined(__clang__) #ifdef ENABLE_RR_DEBUG_INFO
# pragma clang diagnostic pop // TODO(b/165000222): Update this on next LLVM roll.
#elif defined(__GNUC__) // https://github.com/llvm/llvm-project/commit/98f2bb4461072347dcca7d2b1b9571b3a6525801
# pragma GCC diagnostic pop // introduces RTDyldObjectLinkingLayer::registerJITEventListener().
#endif // The current API does not appear to have any way to bind the
// rr::DebugInfo::NotifyFreeingObject event.
objectLayer.setNotifyLoaded([](llvm::orc::VModuleKey,
const llvm::object::ObjectFile &obj,
const llvm::RuntimeDyld::LoadedObjectInfo &l) {
static std::atomic<uint64_t> unique_key{ 0 };
rr::DebugInfo::NotifyObjectEmitted(unique_key++, obj, l);
});
#endif // ENABLE_RR_DEBUG_INFO
if(JITGlobals::get()->getTargetTriple().isOSBinFormatCOFF())
{
// Hack to support symbol visibility in COFF.
// Matches hack in llvm::orc::LLJIT::createObjectLinkingLayer().
// See documentation on these functions for more detail.
objectLayer.setOverrideObjectFlagsWithResponsibilityFlags(true);
objectLayer.setAutoClaimResponsibilityForObjectSymbols(true);
}
std::vector<std::string> mangledNames(count); dylib.addGenerator(std::make_unique<ExternalSymbolGenerator>());
llvm::SmallVector<llvm::orc::SymbolStringPtr, 8> names(count);
for(size_t i = 0; i < count; i++) for(size_t i = 0; i < count; i++)
{ {
auto func = funcs[i]; auto func = funcs[i];
static std::atomic<size_t> numEmittedFunctions = { 0 };
std::string name = "f" + llvm::Twine(numEmittedFunctions++).str();
func->setName(name);
func->setLinkage(llvm::GlobalValue::ExternalLinkage); func->setLinkage(llvm::GlobalValue::ExternalLinkage);
func->setDoesNotThrow(); func->setDoesNotThrow();
if(!func->hasName())
llvm::raw_string_ostream mangledNameStream(mangledNames[i]); {
llvm::Mangler::getNameWithPrefix(mangledNameStream, name, JITGlobals::get()->dataLayout); func->setName("f" + llvm::Twine(i).str());
}
names[i] = mangle(func->getName());
} }
auto moduleKey = session.allocateVModule();
// Once the module is passed to the compileLayer, the // Once the module is passed to the compileLayer, the
// llvm::Functions are freed. Make sure funcs are not referenced // llvm::Functions are freed. Make sure funcs are not referenced
// after this point. // after this point.
funcs = nullptr; funcs = nullptr;
llvm::cantFail(compileLayer.addModule(moduleKey, std::move(module))); llvm::cantFail(compileLayer.add(dylib, llvm::orc::ThreadSafeModule(std::move(module), ctx)));
// Resolve the function addresses. // Resolve the function addresses.
for(size_t i = 0; i < count; i++) for(size_t i = 0; i < count; i++)
{ {
auto symbol = compileLayer.findSymbolIn(moduleKey, mangledNames[i], false); auto symbol = session.lookup({ &dylib }, names[i]);
if(auto address = symbol.getAddress()) ASSERT_MSG(symbol, "Failed to lookup address of routine function %d: %s",
{ (int)i, llvm::toString(symbol.takeError()).c_str());
addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(address.get())); addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(symbol->getAddress()));
}
} }
} }
...@@ -635,15 +555,6 @@ public: ...@@ -635,15 +555,6 @@ public:
{ {
return addresses[index]; return addresses[index];
} }
private:
std::shared_ptr<llvm::orc::SymbolResolver> resolver;
std::shared_ptr<llvm::TargetMachine> targetMachine;
llvm::orc::ExecutionSession session;
CompileLayer compileLayer;
MemoryMapper memoryMapper;
ObjLayer objLayer;
std::vector<const void *> addresses;
}; };
} // anonymous namespace } // anonymous namespace
...@@ -655,12 +566,11 @@ JITBuilder::JITBuilder(const rr::Config &config) ...@@ -655,12 +566,11 @@ JITBuilder::JITBuilder(const rr::Config &config)
, module(new llvm::Module("", context)) , module(new llvm::Module("", context))
, builder(new llvm::IRBuilder<>(context)) , builder(new llvm::IRBuilder<>(context))
{ {
module->setDataLayout(JITGlobals::get()->dataLayout); module->setDataLayout(JITGlobals::get()->getDataLayout());
} }
void JITBuilder::optimize(const rr::Config &cfg) void JITBuilder::optimize(const rr::Config &cfg)
{ {
#ifdef ENABLE_RR_DEBUG_INFO #ifdef ENABLE_RR_DEBUG_INFO
if(debugInfo != nullptr) if(debugInfo != nullptr)
{ {
...@@ -668,30 +578,29 @@ void JITBuilder::optimize(const rr::Config &cfg) ...@@ -668,30 +578,29 @@ void JITBuilder::optimize(const rr::Config &cfg)
} }
#endif // ENABLE_RR_DEBUG_INFO #endif // ENABLE_RR_DEBUG_INFO
std::unique_ptr<llvm::legacy::PassManager> passManager( llvm::legacy::PassManager passManager;
new llvm::legacy::PassManager());
for(auto pass : cfg.getOptimization().getPasses()) for(auto pass : cfg.getOptimization().getPasses())
{ {
switch(pass) switch(pass)
{ {
case rr::Optimization::Pass::Disabled: break; case rr::Optimization::Pass::Disabled: break;
case rr::Optimization::Pass::CFGSimplification: passManager->add(llvm::createCFGSimplificationPass()); break; case rr::Optimization::Pass::CFGSimplification: passManager.add(llvm::createCFGSimplificationPass()); break;
case rr::Optimization::Pass::LICM: passManager->add(llvm::createLICMPass()); break; case rr::Optimization::Pass::LICM: passManager.add(llvm::createLICMPass()); break;
case rr::Optimization::Pass::AggressiveDCE: passManager->add(llvm::createAggressiveDCEPass()); break; case rr::Optimization::Pass::AggressiveDCE: passManager.add(llvm::createAggressiveDCEPass()); break;
case rr::Optimization::Pass::GVN: passManager->add(llvm::createGVNPass()); break; case rr::Optimization::Pass::GVN: passManager.add(llvm::createGVNPass()); break;
case rr::Optimization::Pass::InstructionCombining: passManager->add(llvm::createInstructionCombiningPass()); break; case rr::Optimization::Pass::InstructionCombining: passManager.add(llvm::createInstructionCombiningPass()); break;
case rr::Optimization::Pass::Reassociate: passManager->add(llvm::createReassociatePass()); break; case rr::Optimization::Pass::Reassociate: passManager.add(llvm::createReassociatePass()); break;
case rr::Optimization::Pass::DeadStoreElimination: passManager->add(llvm::createDeadStoreEliminationPass()); break; case rr::Optimization::Pass::DeadStoreElimination: passManager.add(llvm::createDeadStoreEliminationPass()); break;
case rr::Optimization::Pass::SCCP: passManager->add(llvm::createSCCPPass()); break; case rr::Optimization::Pass::SCCP: passManager.add(llvm::createSCCPPass()); break;
case rr::Optimization::Pass::ScalarReplAggregates: passManager->add(llvm::createSROAPass()); break; case rr::Optimization::Pass::ScalarReplAggregates: passManager.add(llvm::createSROAPass()); break;
case rr::Optimization::Pass::EarlyCSEPass: passManager->add(llvm::createEarlyCSEPass()); break; case rr::Optimization::Pass::EarlyCSEPass: passManager.add(llvm::createEarlyCSEPass()); break;
default: default:
UNREACHABLE("pass: %d", int(pass)); UNREACHABLE("pass: %d", int(pass));
} }
} }
passManager->run(*module); passManager.run(*module);
} }
std::shared_ptr<rr::Routine> JITBuilder::acquireRoutine(llvm::Function **funcs, size_t count, const rr::Config &cfg) std::shared_ptr<rr::Routine> JITBuilder::acquireRoutine(llvm::Function **funcs, size_t count, const rr::Config &cfg)
......
...@@ -56,7 +56,12 @@ __pragma(warning(push)) ...@@ -56,7 +56,12 @@ __pragma(warning(push))
std::pair<llvm::StringRef, llvm::StringRef> splitPath(const char *path) std::pair<llvm::StringRef, llvm::StringRef> splitPath(const char *path)
{ {
return llvm::StringRef(path).rsplit('/'); auto dirAndFile = llvm::StringRef(path).rsplit('/');
if(dirAndFile.second == "")
{
dirAndFile.second = "<unknown>";
}
return dirAndFile;
} }
// Note: createGDBRegistrationListener() returns a pointer to a singleton. // Note: createGDBRegistrationListener() returns a pointer to a singleton.
...@@ -82,11 +87,11 @@ DebugInfo::DebugInfo( ...@@ -82,11 +87,11 @@ DebugInfo::DebugInfo(
auto location = getCallerLocation(); auto location = getCallerLocation();
auto fileAndDir = splitPath(location.function.file.c_str()); auto dirAndFile = splitPath(location.function.file.c_str());
diBuilder.reset(new llvm::DIBuilder(*module)); diBuilder.reset(new llvm::DIBuilder(*module));
diCU = diBuilder->createCompileUnit( diCU = diBuilder->createCompileUnit(
llvm::dwarf::DW_LANG_C, llvm::dwarf::DW_LANG_C,
diBuilder->createFile(fileAndDir.first, fileAndDir.second), diBuilder->createFile(dirAndFile.second, dirAndFile.first),
"Reactor", "Reactor",
0, "", 0); 0, "", 0);
...@@ -135,7 +140,10 @@ void DebugInfo::EmitLocation() ...@@ -135,7 +140,10 @@ void DebugInfo::EmitLocation()
void DebugInfo::Flush() void DebugInfo::Flush()
{ {
emitPending(diScope.back(), builder); if(!diScope.empty())
{
emitPending(diScope.back(), builder);
}
} }
void DebugInfo::syncScope(Backtrace const &backtrace) void DebugInfo::syncScope(Backtrace const &backtrace)
...@@ -376,17 +384,15 @@ void DebugInfo::emitPending(Scope &scope, IRBuilder *builder) ...@@ -376,17 +384,15 @@ void DebugInfo::emitPending(Scope &scope, IRBuilder *builder)
scope.pending = Pending{}; scope.pending = Pending{};
} }
void DebugInfo::NotifyObjectEmitted(const llvm::object::ObjectFile &Obj, const llvm::LoadedObjectInfo &L) void DebugInfo::NotifyObjectEmitted(uint64_t key, const llvm::object::ObjectFile &obj, const llvm::LoadedObjectInfo &l)
{ {
std::unique_lock<std::mutex> lock(jitEventListenerMutex); std::unique_lock<std::mutex> lock(jitEventListenerMutex);
auto key = reinterpret_cast<llvm::JITEventListener::ObjectKey>(&Obj); jitEventListener->notifyObjectLoaded(key, obj, static_cast<const llvm::RuntimeDyld::LoadedObjectInfo &>(l));
jitEventListener->notifyObjectLoaded(key, Obj, static_cast<const llvm::RuntimeDyld::LoadedObjectInfo &>(L));
} }
void DebugInfo::NotifyFreeingObject(const llvm::object::ObjectFile &Obj) void DebugInfo::NotifyFreeingObject(uint64_t key)
{ {
std::unique_lock<std::mutex> lock(jitEventListenerMutex); std::unique_lock<std::mutex> lock(jitEventListenerMutex);
auto key = reinterpret_cast<llvm::JITEventListener::ObjectKey>(&Obj);
jitEventListener->notifyFreeingObject(key); jitEventListener->notifyFreeingObject(key);
} }
...@@ -432,7 +438,8 @@ void DebugInfo::registerBasicTypes() ...@@ -432,7 +438,8 @@ void DebugInfo::registerBasicTypes()
Location DebugInfo::getCallerLocation() const Location DebugInfo::getCallerLocation() const
{ {
return getCallerBacktrace(1)[0]; auto backtrace = getCallerBacktrace(1);
return backtrace.empty() ? Location{} : backtrace[0];
} }
Backtrace DebugInfo::getCallerBacktrace(size_t limit /* = 0 */) const Backtrace DebugInfo::getCallerBacktrace(size_t limit /* = 0 */) const
......
...@@ -92,11 +92,11 @@ public: ...@@ -92,11 +92,11 @@ public:
// NotifyObjectEmitted informs any attached debuggers of the JIT'd // NotifyObjectEmitted informs any attached debuggers of the JIT'd
// object. // object.
static void NotifyObjectEmitted(const llvm::object::ObjectFile &Obj, const llvm::LoadedObjectInfo &L); static void NotifyObjectEmitted(uint64_t key, const llvm::object::ObjectFile &obj, const llvm::LoadedObjectInfo &l);
// NotifyFreeingObject informs any attached debuggers that the JIT'd // NotifyFreeingObject informs any attached debuggers that the JIT'd
// object is now invalid. // object is now invalid.
static void NotifyFreeingObject(const llvm::object::ObjectFile &Obj); static void NotifyFreeingObject(uint64_t key);
private: private:
struct Token struct Token
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment