Commit 41a7302e by Nicolas Capens Committed by Nicolas Capens

Split JIT creation from LLVMReactor.cpp into LLVMJIT.cpp

This makes a rough distinction between code that mainly only interacts with LLVM's IRBuilder interface, and the part that sets up the JIT and manages module and function creation, memory management, and external symbol resolution. The latter much more often requires changes when LLVM is upgraded than the former, so it's useful to keep them apart as we'll have multiple versions of LLVM in different build environments. Bug: b/148561024 Change-Id: I2ac2d00b4f3bcd277289a4ec6856c9eb7bb6aa24 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/40688 Presubmit-Ready: Nicolas Capens <nicolascapens@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarAntonio Maiorano <amaiorano@google.com>
parent 55890e1a
...@@ -1745,6 +1745,7 @@ set(REACTOR_LLVM_LIST ...@@ -1745,6 +1745,7 @@ set(REACTOR_LLVM_LIST
${SOURCE_DIR}/Reactor/EmulatedReactor.cpp ${SOURCE_DIR}/Reactor/EmulatedReactor.cpp
${SOURCE_DIR}/Reactor/ExecutableMemory.cpp ${SOURCE_DIR}/Reactor/ExecutableMemory.cpp
${SOURCE_DIR}/Reactor/ExecutableMemory.hpp ${SOURCE_DIR}/Reactor/ExecutableMemory.hpp
${SOURCE_DIR}/Reactor/LLVMJIT.cpp
${SOURCE_DIR}/Reactor/LLVMReactor.cpp ${SOURCE_DIR}/Reactor/LLVMReactor.cpp
${SOURCE_DIR}/Reactor/LLVMReactor.hpp ${SOURCE_DIR}/Reactor/LLVMReactor.hpp
${SOURCE_DIR}/Reactor/LLVMReactorDebugInfo.cpp ${SOURCE_DIR}/Reactor/LLVMReactorDebugInfo.cpp
......
...@@ -179,6 +179,7 @@ cc_defaults { ...@@ -179,6 +179,7 @@ cc_defaults {
srcs: [ srcs: [
"Reactor/Reactor.cpp", "Reactor/Reactor.cpp",
"Reactor/LLVMJIT.cpp",
"Reactor/LLVMReactor.cpp", "Reactor/LLVMReactor.cpp",
"Reactor/Debug.cpp", "Reactor/Debug.cpp",
"Reactor/CPUID.cpp", "Reactor/CPUID.cpp",
...@@ -200,6 +201,7 @@ cc_defaults { ...@@ -200,6 +201,7 @@ cc_defaults {
srcs: [ srcs: [
"Reactor/Reactor.cpp", "Reactor/Reactor.cpp",
"Reactor/LLVMJIT.cpp",
"Reactor/LLVMReactor.cpp", "Reactor/LLVMReactor.cpp",
"Reactor/Debug.cpp", "Reactor/Debug.cpp",
"Reactor/CPUID.cpp", "Reactor/CPUID.cpp",
......
...@@ -330,6 +330,7 @@ if (supports_llvm) { ...@@ -330,6 +330,7 @@ if (supports_llvm) {
sources = [ sources = [
"CPUID.cpp", "CPUID.cpp",
"LLVMJIT.cpp",
"LLVMReactor.cpp", "LLVMReactor.cpp",
] ]
......
...@@ -15,10 +15,31 @@ ...@@ -15,10 +15,31 @@
#ifndef rr_LLVMReactor_hpp #ifndef rr_LLVMReactor_hpp
#define rr_LLVMReactor_hpp #define rr_LLVMReactor_hpp
namespace llvm { #include "Nucleus.hpp"
class Type; #include "Debug.hpp"
class Value; #include "LLVMReactorDebugInfo.hpp"
#include "Print.hpp"
#ifdef _MSC_VER
__pragma(warning(push))
__pragma(warning(disable : 4146)) // unary minus operator applied to unsigned type, result still unsigned
#endif
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#ifdef _MSC_VER
__pragma(warning(pop))
#endif
#include <memory>
namespace llvm
{
class Type;
class Value;
} // namespace llvm } // namespace llvm
...@@ -49,6 +70,81 @@ inline Value *V(llvm::Value *t) ...@@ -49,6 +70,81 @@ inline Value *V(llvm::Value *t)
// effect. // effect.
void Nop(); void Nop();
class Routine;
class Config;
// JITBuilder holds all the LLVM state for building routines.
class JITBuilder
{
public:
JITBuilder(const rr::Config &config);
void optimize(const rr::Config &cfg);
std::shared_ptr<rr::Routine> acquireRoutine(llvm::Function **funcs, size_t count, const rr::Config &cfg);
const Config config;
llvm::LLVMContext context;
std::unique_ptr<llvm::Module> module;
std::unique_ptr<llvm::IRBuilder<>> builder;
llvm::Function *function = nullptr;
struct CoroutineState
{
llvm::Function *await = nullptr;
llvm::Function *destroy = nullptr;
llvm::Value *handle = nullptr;
llvm::Value *id = nullptr;
llvm::Value *promise = nullptr;
llvm::Type *yieldType = nullptr;
llvm::BasicBlock *entryBlock = nullptr;
llvm::BasicBlock *suspendBlock = nullptr;
llvm::BasicBlock *endBlock = nullptr;
llvm::BasicBlock *destroyBlock = nullptr;
};
CoroutineState coroutine;
#ifdef ENABLE_RR_DEBUG_INFO
std::unique_ptr<rr::DebugInfo> debugInfo;
#endif
};
inline std::memory_order atomicOrdering(llvm::AtomicOrdering memoryOrder)
{
switch(memoryOrder)
{
case llvm::AtomicOrdering::Monotonic: return std::memory_order_relaxed; // https://llvm.org/docs/Atomics.html#monotonic
case llvm::AtomicOrdering::Acquire: return std::memory_order_acquire;
case llvm::AtomicOrdering::Release: return std::memory_order_release;
case llvm::AtomicOrdering::AcquireRelease: return std::memory_order_acq_rel;
case llvm::AtomicOrdering::SequentiallyConsistent: return std::memory_order_seq_cst;
default:
UNREACHABLE("memoryOrder: %d", int(memoryOrder));
return std::memory_order_acq_rel;
}
}
inline llvm::AtomicOrdering atomicOrdering(bool atomic, std::memory_order memoryOrder)
{
if(!atomic)
{
return llvm::AtomicOrdering::NotAtomic;
}
switch(memoryOrder)
{
case std::memory_order_relaxed: return llvm::AtomicOrdering::Monotonic; // https://llvm.org/docs/Atomics.html#monotonic
case std::memory_order_consume: return llvm::AtomicOrdering::Acquire; // https://llvm.org/docs/Atomics.html#acquire: "It should also be used for C++11/C11 memory_order_consume."
case std::memory_order_acquire: return llvm::AtomicOrdering::Acquire;
case std::memory_order_release: return llvm::AtomicOrdering::Release;
case std::memory_order_acq_rel: return llvm::AtomicOrdering::AcquireRelease;
case std::memory_order_seq_cst: return llvm::AtomicOrdering::SequentiallyConsistent;
default:
UNREACHABLE("memoryOrder: %d", int(memoryOrder));
return llvm::AtomicOrdering::AcquireRelease;
}
}
} // namespace rr } // namespace rr
#endif // rr_LLVMReactor_hpp #endif // rr_LLVMReactor_hpp
...@@ -21,12 +21,22 @@ ...@@ -21,12 +21,22 @@
# include "boost/stacktrace.hpp" # include "boost/stacktrace.hpp"
// TODO(b/143539525): Eliminate when warning has been fixed.
# ifdef _MSC_VER
__pragma(warning(push))
__pragma(warning(disable : 4146)) // unary minus operator applied to unsigned type, result still unsigned
# endif
# include "llvm/Demangle/Demangle.h" # include "llvm/Demangle/Demangle.h"
# include "llvm/ExecutionEngine/JITEventListener.h" # include "llvm/ExecutionEngine/JITEventListener.h"
# include "llvm/IR/DIBuilder.h" # include "llvm/IR/DIBuilder.h"
# include "llvm/IR/IRBuilder.h" # include "llvm/IR/IRBuilder.h"
# include "llvm/IR/Intrinsics.h" # include "llvm/IR/Intrinsics.h"
# ifdef _MSC_VER
__pragma(warning(pop))
# endif
# include <cctype> # include <cctype>
# include <fstream> # include <fstream>
# include <mutex> # include <mutex>
...@@ -40,17 +50,18 @@ ...@@ -40,17 +50,18 @@
# define LOG(msg, ...) # define LOG(msg, ...)
# endif # endif
namespace { namespace
std::pair<llvm::StringRef, llvm::StringRef> splitPath(const char *path)
{ {
return llvm::StringRef(path).rsplit('/');
}
// Note: createGDBRegistrationListener() returns a pointer to a singleton. std::pair<llvm::StringRef, llvm::StringRef> splitPath(const char *path)
// Nothing is actually created. {
auto jitEventListener = llvm::JITEventListener::createGDBRegistrationListener(); // guarded by jitEventListenerMutex return llvm::StringRef(path).rsplit('/');
std::mutex jitEventListenerMutex; }
// Note: createGDBRegistrationListener() returns a pointer to a singleton.
// Nothing is actually created.
auto jitEventListener = llvm::JITEventListener::createGDBRegistrationListener(); // guarded by jitEventListenerMutex
std::mutex jitEventListenerMutex;
} // namespace } // namespace
......
...@@ -287,6 +287,7 @@ ...@@ -287,6 +287,7 @@
<ClCompile Include="CPUID.cpp" /> <ClCompile Include="CPUID.cpp" />
<ClCompile Include="Debug.cpp" /> <ClCompile Include="Debug.cpp" />
<ClCompile Include="LLVMReactor.cpp" /> <ClCompile Include="LLVMReactor.cpp" />
<ClCompile Include="LLVMJIT.cpp" />
<ClCompile Include="LLVMReactorDebugInfo.cpp" /> <ClCompile Include="LLVMReactorDebugInfo.cpp" />
<ClCompile Include="ExecutableMemory.cpp" /> <ClCompile Include="ExecutableMemory.cpp" />
<ClCompile Include="Reactor.cpp" /> <ClCompile Include="Reactor.cpp" />
......
...@@ -18,6 +18,9 @@ ...@@ -18,6 +18,9 @@
<ClCompile Include="LLVMReactor.cpp"> <ClCompile Include="LLVMReactor.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="LLVMJIT.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LLVMReactorDebugInfo.cpp"> <ClCompile Include="LLVMReactorDebugInfo.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
......
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