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: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Reactor/LLVMJIT.cpp b/src/Reactor/LLVMJIT.cpp
index d156be8..7ee430f 100644
--- a/src/Reactor/LLVMJIT.cpp
+++ b/src/Reactor/LLVMJIT.cpp
@@ -24,31 +24,12 @@
__pragma(warning(disable : 4146)) // unary minus operator applied to unsigned type, result still unsigned
#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/IRCompileLayer.h"
-#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
-#include "llvm/ExecutionEngine/RTDyldMemoryManager.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/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/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/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
@@ -57,9 +38,6 @@
__pragma(warning(pop))
#endif
-#include <atomic>
-#include <unordered_map>
-
#if defined(_WIN64)
extern "C" void __chkstk();
#elif defined(_WIN32)
@@ -81,119 +59,57 @@
class JITGlobals
{
public:
- using TargetMachineSPtr = std::shared_ptr<llvm::TargetMachine>;
-
static JITGlobals *get();
- const std::string mcpu;
- const std::vector<std::string> mattrs;
- const char *const march;
- const llvm::TargetOptions targetOptions;
- const llvm::DataLayout dataLayout;
-
- TargetMachineSPtr createTargetMachine(rr::Optimization::Level optlevel);
+ llvm::orc::JITTargetMachineBuilder getTargetMachineBuilder(rr::Optimization::Level optLevel) const;
+ const llvm::DataLayout &getDataLayout() const;
+ const llvm::Triple getTargetTriple() const;
private:
- static JITGlobals create();
+ JITGlobals(const llvm::orc::JITTargetMachineBuilder &jtmb, const llvm::DataLayout &dataLayout);
+
static llvm::CodeGenOpt::Level toLLVM(rr::Optimization::Level level);
- JITGlobals(const char *mcpu,
- const std::vector<std::string> &mattrs,
- const char *march,
- const llvm::TargetOptions &targetOptions,
- const llvm::DataLayout &dataLayout);
- JITGlobals(const JITGlobals &) = default;
+ const llvm::orc::JITTargetMachineBuilder jtmb;
+ const llvm::DataLayout dataLayout;
};
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;
}
-JITGlobals::TargetMachineSPtr JITGlobals::createTargetMachine(rr::Optimization::Level optlevel)
+llvm::orc::JITTargetMachineBuilder JITGlobals::getTargetMachineBuilder(rr::Optimization::Level optLevel) const
{
-#ifdef ENABLE_RR_DEBUG_INFO
- auto llvmOptLevel = toLLVM(rr::Optimization::Level::None);
-#else // ENABLE_RR_DEBUG_INFO
- auto llvmOptLevel = toLLVM(optlevel);
-#endif // ENABLE_RR_DEBUG_INFO
-
- return TargetMachineSPtr(llvm::EngineBuilder()
- .setOptLevel(llvmOptLevel)
- .setMCPU(mcpu)
- .setMArch(march)
- .setMAttrs(mattrs)
- .setTargetOptions(targetOptions)
- .selectTarget());
+ llvm::orc::JITTargetMachineBuilder out = jtmb;
+ out.setCodeGenOptLevel(toLLVM(optLevel));
+ return out;
}
-JITGlobals JITGlobals::create()
+const llvm::DataLayout &JITGlobals::getDataLayout() const
{
- struct LLVMInitializer
- {
- LLVMInitializer()
- {
- llvm::InitializeNativeTarget();
- llvm::InitializeNativeTargetAsmPrinter();
- llvm::InitializeNativeTargetAsmParser();
- }
- };
- static LLVMInitializer initializeLLVM;
+ return dataLayout;
+}
- auto mcpu = llvm::sys::getHostCPUName();
+const llvm::Triple JITGlobals::getTargetTriple() const
+{
+ return jtmb.getTargetTriple();
+}
- 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();
-
- 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)
@@ -209,19 +125,6 @@
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
{
public:
@@ -349,7 +252,7 @@
}
#endif
-void *resolveExternalSymbol(const char *name)
+class ExternalSymbolGenerator : public llvm::orc::JITDylib::DefinitionGenerator
{
struct Atomic
{
@@ -379,255 +282,272 @@
}
};
- 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_free_frame(void *ptr) { alignedFree(ptr); }
+ static void *coroutine_alloc_frame(size_t size) { return alignedAlloc(size, 16); }
+ static void coroutine_free_frame(void *ptr) { alignedFree(ptr); }
#ifdef __ANDROID__
- // forwarders since we can't take address of builtins
- 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_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_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_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); }
+ // forwarders since we can't take address of builtins
+ 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_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_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_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_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); });
- }
- 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); });
- }
- 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); });
- }
- 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); });
- }
+ 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); });
+ }
+ 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); });
+ }
+ 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); });
+ }
+ 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); });
+ }
#endif
- };
-
class Resolver
{
public:
- using FunctionMap = std::unordered_map<std::string, void *>;
+ using FunctionMap = llvm::StringMap<void *>;
FunctionMap functions;
Resolver()
{
#ifdef ENABLE_RR_PRINT
- functions.emplace("rr::DebugPrintf", reinterpret_cast<void *>(rr::DebugPrintf));
+ functions.try_emplace("rr::DebugPrintf", reinterpret_cast<void *>(rr::DebugPrintf));
#endif
- functions.emplace("nop", reinterpret_cast<void *>(F::nop));
- functions.emplace("floorf", reinterpret_cast<void *>(floorf));
- functions.emplace("nearbyintf", reinterpret_cast<void *>(nearbyintf));
- functions.emplace("truncf", reinterpret_cast<void *>(truncf));
- functions.emplace("printf", reinterpret_cast<void *>(printf));
- functions.emplace("puts", reinterpret_cast<void *>(puts));
- functions.emplace("fmodf", reinterpret_cast<void *>(fmodf));
+ functions.try_emplace("nop", reinterpret_cast<void *>(nop));
+ functions.try_emplace("floorf", reinterpret_cast<void *>(floorf));
+ functions.try_emplace("nearbyintf", reinterpret_cast<void *>(nearbyintf));
+ functions.try_emplace("truncf", reinterpret_cast<void *>(truncf));
+ functions.try_emplace("printf", reinterpret_cast<void *>(printf));
+ functions.try_emplace("puts", reinterpret_cast<void *>(puts));
+ functions.try_emplace("fmodf", reinterpret_cast<void *>(fmodf));
- functions.emplace("sinf", reinterpret_cast<void *>(sinf));
- functions.emplace("cosf", reinterpret_cast<void *>(cosf));
- functions.emplace("asinf", reinterpret_cast<void *>(asinf));
- functions.emplace("acosf", reinterpret_cast<void *>(acosf));
- functions.emplace("atanf", reinterpret_cast<void *>(atanf));
- functions.emplace("sinhf", reinterpret_cast<void *>(sinhf));
- functions.emplace("coshf", reinterpret_cast<void *>(coshf));
- functions.emplace("tanhf", reinterpret_cast<void *>(tanhf));
- functions.emplace("asinhf", reinterpret_cast<void *>(asinhf));
- functions.emplace("acoshf", reinterpret_cast<void *>(acoshf));
- functions.emplace("atanhf", reinterpret_cast<void *>(atanhf));
- functions.emplace("atan2f", reinterpret_cast<void *>(atan2f));
- functions.emplace("powf", reinterpret_cast<void *>(powf));
- functions.emplace("expf", reinterpret_cast<void *>(expf));
- functions.emplace("logf", reinterpret_cast<void *>(logf));
- functions.emplace("exp2f", reinterpret_cast<void *>(exp2f));
- functions.emplace("log2f", reinterpret_cast<void *>(log2f));
+ functions.try_emplace("sinf", reinterpret_cast<void *>(sinf));
+ functions.try_emplace("cosf", reinterpret_cast<void *>(cosf));
+ functions.try_emplace("asinf", reinterpret_cast<void *>(asinf));
+ functions.try_emplace("acosf", reinterpret_cast<void *>(acosf));
+ functions.try_emplace("atanf", reinterpret_cast<void *>(atanf));
+ functions.try_emplace("sinhf", reinterpret_cast<void *>(sinhf));
+ functions.try_emplace("coshf", reinterpret_cast<void *>(coshf));
+ functions.try_emplace("tanhf", reinterpret_cast<void *>(tanhf));
+ functions.try_emplace("asinhf", reinterpret_cast<void *>(asinhf));
+ functions.try_emplace("acoshf", reinterpret_cast<void *>(acoshf));
+ functions.try_emplace("atanhf", reinterpret_cast<void *>(atanhf));
+ functions.try_emplace("atan2f", reinterpret_cast<void *>(atan2f));
+ functions.try_emplace("powf", reinterpret_cast<void *>(powf));
+ functions.try_emplace("expf", reinterpret_cast<void *>(expf));
+ functions.try_emplace("logf", reinterpret_cast<void *>(logf));
+ functions.try_emplace("exp2f", reinterpret_cast<void *>(exp2f));
+ functions.try_emplace("log2f", reinterpret_cast<void *>(log2f));
- functions.emplace("sin", reinterpret_cast<void *>(static_cast<double (*)(double)>(sin)));
- functions.emplace("cos", reinterpret_cast<void *>(static_cast<double (*)(double)>(cos)));
- functions.emplace("asin", reinterpret_cast<void *>(static_cast<double (*)(double)>(asin)));
- functions.emplace("acos", reinterpret_cast<void *>(static_cast<double (*)(double)>(acos)));
- functions.emplace("atan", reinterpret_cast<void *>(static_cast<double (*)(double)>(atan)));
- functions.emplace("sinh", reinterpret_cast<void *>(static_cast<double (*)(double)>(sinh)));
- functions.emplace("cosh", reinterpret_cast<void *>(static_cast<double (*)(double)>(cosh)));
- functions.emplace("tanh", reinterpret_cast<void *>(static_cast<double (*)(double)>(tanh)));
- functions.emplace("asinh", reinterpret_cast<void *>(static_cast<double (*)(double)>(asinh)));
- functions.emplace("acosh", reinterpret_cast<void *>(static_cast<double (*)(double)>(acosh)));
- functions.emplace("atanh", reinterpret_cast<void *>(static_cast<double (*)(double)>(atanh)));
- functions.emplace("atan2", reinterpret_cast<void *>(static_cast<double (*)(double, double)>(atan2)));
- functions.emplace("pow", reinterpret_cast<void *>(static_cast<double (*)(double, double)>(pow)));
- functions.emplace("exp", reinterpret_cast<void *>(static_cast<double (*)(double)>(exp)));
- functions.emplace("log", reinterpret_cast<void *>(static_cast<double (*)(double)>(log)));
- functions.emplace("exp2", reinterpret_cast<void *>(static_cast<double (*)(double)>(exp2)));
- functions.emplace("log2", reinterpret_cast<void *>(static_cast<double (*)(double)>(log2)));
+ functions.try_emplace("sin", reinterpret_cast<void *>(static_cast<double (*)(double)>(sin)));
+ functions.try_emplace("cos", reinterpret_cast<void *>(static_cast<double (*)(double)>(cos)));
+ functions.try_emplace("asin", reinterpret_cast<void *>(static_cast<double (*)(double)>(asin)));
+ functions.try_emplace("acos", reinterpret_cast<void *>(static_cast<double (*)(double)>(acos)));
+ functions.try_emplace("atan", reinterpret_cast<void *>(static_cast<double (*)(double)>(atan)));
+ functions.try_emplace("sinh", reinterpret_cast<void *>(static_cast<double (*)(double)>(sinh)));
+ functions.try_emplace("cosh", reinterpret_cast<void *>(static_cast<double (*)(double)>(cosh)));
+ functions.try_emplace("tanh", reinterpret_cast<void *>(static_cast<double (*)(double)>(tanh)));
+ functions.try_emplace("asinh", reinterpret_cast<void *>(static_cast<double (*)(double)>(asinh)));
+ functions.try_emplace("acosh", reinterpret_cast<void *>(static_cast<double (*)(double)>(acosh)));
+ functions.try_emplace("atanh", reinterpret_cast<void *>(static_cast<double (*)(double)>(atanh)));
+ functions.try_emplace("atan2", reinterpret_cast<void *>(static_cast<double (*)(double, double)>(atan2)));
+ functions.try_emplace("pow", reinterpret_cast<void *>(static_cast<double (*)(double, double)>(pow)));
+ functions.try_emplace("exp", reinterpret_cast<void *>(static_cast<double (*)(double)>(exp)));
+ functions.try_emplace("log", reinterpret_cast<void *>(static_cast<double (*)(double)>(log)));
+ functions.try_emplace("exp2", reinterpret_cast<void *>(static_cast<double (*)(double)>(exp2)));
+ functions.try_emplace("log2", reinterpret_cast<void *>(static_cast<double (*)(double)>(log2)));
- functions.emplace("atomic_load", reinterpret_cast<void *>(Atomic::load));
- functions.emplace("atomic_store", reinterpret_cast<void *>(Atomic::store));
+ functions.try_emplace("atomic_load", reinterpret_cast<void *>(Atomic::load));
+ functions.try_emplace("atomic_store", reinterpret_cast<void *>(Atomic::store));
// 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.emplace("coroutine_free_frame", reinterpret_cast<void *>(F::coroutine_free_frame));
+ functions.try_emplace("coroutine_alloc_frame", reinterpret_cast<void *>(coroutine_alloc_frame));
+ functions.try_emplace("coroutine_free_frame", reinterpret_cast<void *>(coroutine_free_frame));
#ifdef __APPLE__
- functions.emplace("sincosf_stret", reinterpret_cast<void *>(__sincosf_stret));
+ functions.try_emplace("sincosf_stret", reinterpret_cast<void *>(__sincosf_stret));
#elif defined(__linux__)
- functions.emplace("sincosf", reinterpret_cast<void *>(sincosf));
+ functions.try_emplace("sincosf", reinterpret_cast<void *>(sincosf));
#elif defined(_WIN64)
- functions.emplace("chkstk", reinterpret_cast<void *>(__chkstk));
+ functions.try_emplace("chkstk", reinterpret_cast<void *>(__chkstk));
#elif defined(_WIN32)
- functions.emplace("chkstk", reinterpret_cast<void *>(_chkstk));
+ functions.try_emplace("chkstk", reinterpret_cast<void *>(_chkstk));
#endif
#ifdef __ARM_EABI__
- functions.emplace("aeabi_idivmod", reinterpret_cast<void *>(__aeabi_idivmod));
+ functions.try_emplace("aeabi_idivmod", reinterpret_cast<void *>(__aeabi_idivmod));
#endif
#ifdef __ANDROID__
- functions.emplace("aeabi_unwind_cpp_pr0", reinterpret_cast<void *>(F::neverCalled));
- functions.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.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.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.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.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.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("aeabi_unwind_cpp_pr0", reinterpret_cast<void *>(F::neverCalled));
+ functions.try_emplace("sync_synchronize", reinterpret_cast<void *>(F::sync_synchronize));
+ functions.try_emplace("sync_fetch_and_add_4", reinterpret_cast<void *>(F::sync_fetch_and_add_4));
+ functions.try_emplace("sync_fetch_and_and_4", reinterpret_cast<void *>(F::sync_fetch_and_and_4));
+ functions.try_emplace("sync_fetch_and_or_4", reinterpret_cast<void *>(F::sync_fetch_and_or_4));
+ functions.try_emplace("sync_fetch_and_xor_4", reinterpret_cast<void *>(F::sync_fetch_and_xor_4));
+ functions.try_emplace("sync_fetch_and_sub_4", reinterpret_cast<void *>(F::sync_fetch_and_sub_4));
+ functions.try_emplace("sync_lock_test_and_set_4", reinterpret_cast<void *>(F::sync_lock_test_and_set_4));
+ functions.try_emplace("sync_val_compare_and_swap_4", reinterpret_cast<void *>(F::sync_val_compare_and_swap_4));
+ functions.try_emplace("sync_fetch_and_max_4", reinterpret_cast<void *>(F::sync_fetch_and_max_4));
+ functions.try_emplace("sync_fetch_and_min_4", reinterpret_cast<void *>(F::sync_fetch_and_min_4));
+ functions.try_emplace("sync_fetch_and_umax_4", reinterpret_cast<void *>(F::sync_fetch_and_umax_4));
+ functions.try_emplace("sync_fetch_and_umin_4", reinterpret_cast<void *>(F::sync_fetch_and_umin_4));
#endif
#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
}
};
- 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
- // to append these on macOS.
- const char *trimmed = name;
- while(trimmed[0] == '_') { trimmed++; }
+ llvm::orc::SymbolMap symbols;
- auto it = resolver.functions.find(trimmed);
- // Missing functions will likely make the module fail in exciting non-obvious ways.
- ASSERT_MSG(it != resolver.functions.end(), "Missing external function: '%s'", name);
- return it->second;
-}
+#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
+ std::string missing;
+#endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
+
+ 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
// object layer as each routine may require different target machine
// settings and no Reactor routine directly links against another.
class JITRoutine : public rr::Routine
{
- using ObjLayer = llvm::orc::LegacyRTDyldObjectLinkingLayer;
- using CompileLayer = llvm::orc::LegacyIRCompileLayer<ObjLayer, llvm::orc::SimpleCompiler>;
+ using ObjLayer = llvm::orc::RTDyldObjectLinkingLayer;
+ using CompileLayer = llvm::orc::IRCompileLayer;
+
+ llvm::orc::RTDyldObjectLinkingLayer objectLayer;
+ llvm::orc::IRCompileLayer compileLayer;
+ llvm::orc::MangleAndInterner mangle;
+ llvm::orc::ThreadSafeContext ctx;
+ llvm::orc::ExecutionSession session;
+ llvm::orc::JITDylib &dylib;
+ std::vector<const void *> addresses;
public:
-#if defined(__clang__)
-// TODO(bclayton): Switch to new JIT
-// error: 'LegacyIRCompileLayer' is deprecated: ORCv1 layers (layers with the 'Legacy' prefix) are deprecated.
-// Please use the ORCv2 IRCompileLayer instead [-Werror,-Wdeprecated-declarations]
-# pragma clang diagnostic push
-# pragma clang diagnostic ignored "-Wdeprecated-declarations"
-#elif defined(__GNUC__)
-# pragma GCC diagnostic push
-# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-#endif
-
JITRoutine(
std::unique_ptr<llvm::Module> module,
llvm::Function **funcs,
size_t count,
const rr::Config &config)
- : resolver(createLegacyLookupResolver(
- session,
- [&](const llvm::StringRef &name) {
- void *func = resolveExternalSymbol(name.str().c_str());
- if(func != nullptr)
- {
- 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
- })
+ : objectLayer(session, []() { return std::make_unique<llvm::SectionMemoryManager>(new MemoryMapper()); })
+ , compileLayer(session, objectLayer, std::make_unique<llvm::orc::ConcurrentIRCompiler>(JITGlobals::get()->getTargetMachineBuilder(config.getOptimization().getLevel())))
+ , mangle(session, JITGlobals::get()->getDataLayout())
+ , ctx(std::make_unique<llvm::LLVMContext>())
+ , dylib(session.createJITDylib("<routine>"))
, addresses(count)
{
-#if defined(__clang__)
-# pragma clang diagnostic pop
-#elif defined(__GNUC__)
-# pragma GCC diagnostic pop
-#endif
+#ifdef ENABLE_RR_DEBUG_INFO
+ // TODO(b/165000222): Update this on next LLVM roll.
+ // https://github.com/llvm/llvm-project/commit/98f2bb4461072347dcca7d2b1b9571b3a6525801
+ // introduces RTDyldObjectLinkingLayer::registerJITEventListener().
+ // 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
- std::vector<std::string> mangledNames(count);
+ 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);
+ }
+
+ dylib.addGenerator(std::make_unique<ExternalSymbolGenerator>());
+
+ llvm::SmallVector<llvm::orc::SymbolStringPtr, 8> names(count);
for(size_t i = 0; i < count; 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->setDoesNotThrow();
-
- llvm::raw_string_ostream mangledNameStream(mangledNames[i]);
- llvm::Mangler::getNameWithPrefix(mangledNameStream, name, JITGlobals::get()->dataLayout);
+ if(!func->hasName())
+ {
+ 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
// llvm::Functions are freed. Make sure funcs are not referenced
// after this point.
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.
for(size_t i = 0; i < count; i++)
{
- auto symbol = compileLayer.findSymbolIn(moduleKey, mangledNames[i], false);
- if(auto address = symbol.getAddress())
- {
- addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(address.get()));
- }
+ auto symbol = session.lookup({ &dylib }, names[i]);
+ 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>(symbol->getAddress()));
}
}
@@ -635,15 +555,6 @@
{
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
@@ -655,12 +566,11 @@
, module(new llvm::Module("", context))
, builder(new llvm::IRBuilder<>(context))
{
- module->setDataLayout(JITGlobals::get()->dataLayout);
+ module->setDataLayout(JITGlobals::get()->getDataLayout());
}
void JITBuilder::optimize(const rr::Config &cfg)
{
-
#ifdef ENABLE_RR_DEBUG_INFO
if(debugInfo != nullptr)
{
@@ -668,30 +578,29 @@
}
#endif // ENABLE_RR_DEBUG_INFO
- std::unique_ptr<llvm::legacy::PassManager> passManager(
- new llvm::legacy::PassManager());
+ llvm::legacy::PassManager passManager;
for(auto pass : cfg.getOptimization().getPasses())
{
switch(pass)
{
case rr::Optimization::Pass::Disabled: 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::AggressiveDCE: passManager->add(llvm::createAggressiveDCEPass()); 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::Reassociate: passManager->add(llvm::createReassociatePass()); 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::ScalarReplAggregates: passManager->add(llvm::createSROAPass()); break;
- case rr::Optimization::Pass::EarlyCSEPass: passManager->add(llvm::createEarlyCSEPass()); 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::AggressiveDCE: passManager.add(llvm::createAggressiveDCEPass()); 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::Reassociate: passManager.add(llvm::createReassociatePass()); 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::ScalarReplAggregates: passManager.add(llvm::createSROAPass()); break;
+ case rr::Optimization::Pass::EarlyCSEPass: passManager.add(llvm::createEarlyCSEPass()); break;
default:
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)
diff --git a/src/Reactor/LLVMReactorDebugInfo.cpp b/src/Reactor/LLVMReactorDebugInfo.cpp
index 04c12e5..7d10734 100644
--- a/src/Reactor/LLVMReactorDebugInfo.cpp
+++ b/src/Reactor/LLVMReactorDebugInfo.cpp
@@ -56,7 +56,12 @@
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.
@@ -82,11 +87,11 @@
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));
diCU = diBuilder->createCompileUnit(
llvm::dwarf::DW_LANG_C,
- diBuilder->createFile(fileAndDir.first, fileAndDir.second),
+ diBuilder->createFile(dirAndFile.second, dirAndFile.first),
"Reactor",
0, "", 0);
@@ -135,7 +140,10 @@
void DebugInfo::Flush()
{
- emitPending(diScope.back(), builder);
+ if(!diScope.empty())
+ {
+ emitPending(diScope.back(), builder);
+ }
}
void DebugInfo::syncScope(Backtrace const &backtrace)
@@ -376,17 +384,15 @@
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);
- 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);
- auto key = reinterpret_cast<llvm::JITEventListener::ObjectKey>(&Obj);
jitEventListener->notifyFreeingObject(key);
}
@@ -432,7 +438,8 @@
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
diff --git a/src/Reactor/LLVMReactorDebugInfo.hpp b/src/Reactor/LLVMReactorDebugInfo.hpp
index 276f44a..677e81b 100644
--- a/src/Reactor/LLVMReactorDebugInfo.hpp
+++ b/src/Reactor/LLVMReactorDebugInfo.hpp
@@ -92,11 +92,11 @@
// NotifyObjectEmitted informs any attached debuggers of the JIT'd
// 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
// object is now invalid.
- static void NotifyFreeingObject(const llvm::object::ObjectFile &Obj);
+ static void NotifyFreeingObject(uint64_t key);
private:
struct Token