Commit 567e560d by Nicolas Capens Committed by Nicolas Capens

Eliminate duplicate LLVM contexts

The LLVMContext object is used for caching LLVM types, eliminating duplicate constants, and things like handling diagnostics information. Previously we created one for the JITBuilder as well as the JITRoutine. While having multiple ones is mostly benign from a correctness point of view, it adds unnecessary overhead. This change reuses the context created for the JITBuilder object (which is used during IR construction), for the JITRoutine (which uses it for actual compilation). Bug: b/177024837 Change-Id: Iefbbd7fbeb53e67ab41914fad471dd94d9755311 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/51568 Kokoro-Result: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarAntonio Maiorano <amaiorano@google.com>
parent 0985120b
...@@ -627,13 +627,13 @@ class JITRoutine : public rr::Routine ...@@ -627,13 +627,13 @@ class JITRoutine : public rr::Routine
llvm::orc::RTDyldObjectLinkingLayer objectLayer; llvm::orc::RTDyldObjectLinkingLayer objectLayer;
llvm::orc::IRCompileLayer compileLayer; llvm::orc::IRCompileLayer compileLayer;
llvm::orc::MangleAndInterner mangle; llvm::orc::MangleAndInterner mangle;
llvm::orc::ThreadSafeContext ctx;
llvm::orc::JITDylib &dylib; llvm::orc::JITDylib &dylib;
std::vector<const void *> addresses; std::vector<const void *> addresses;
public: public:
JITRoutine( JITRoutine(
std::unique_ptr<llvm::Module> module, std::unique_ptr<llvm::Module> module,
std::unique_ptr<llvm::LLVMContext> context,
const char *name, const char *name,
llvm::Function **funcs, llvm::Function **funcs,
size_t count, size_t count,
...@@ -645,11 +645,9 @@ public: ...@@ -645,11 +645,9 @@ public:
}) })
, compileLayer(session, objectLayer, std::make_unique<llvm::orc::ConcurrentIRCompiler>(JITGlobals::get()->getTargetMachineBuilder(config.getOptimization().getLevel()))) , compileLayer(session, objectLayer, std::make_unique<llvm::orc::ConcurrentIRCompiler>(JITGlobals::get()->getTargetMachineBuilder(config.getOptimization().getLevel())))
, mangle(session, JITGlobals::get()->getDataLayout()) , mangle(session, JITGlobals::get()->getDataLayout())
, ctx(std::make_unique<llvm::LLVMContext>())
, dylib(Unwrap(session.createJITDylib("<routine>"))) , dylib(Unwrap(session.createJITDylib("<routine>")))
, addresses(count) , addresses(count)
{ {
#ifdef ENABLE_RR_DEBUG_INFO #ifdef ENABLE_RR_DEBUG_INFO
// TODO(b/165000222): Update this on next LLVM roll. // TODO(b/165000222): Update this on next LLVM roll.
// https://github.com/llvm/llvm-project/commit/98f2bb4461072347dcca7d2b1b9571b3a6525801 // https://github.com/llvm/llvm-project/commit/98f2bb4461072347dcca7d2b1b9571b3a6525801
...@@ -698,7 +696,7 @@ public: ...@@ -698,7 +696,7 @@ public:
// after this point. // after this point.
funcs = nullptr; funcs = nullptr;
llvm::cantFail(compileLayer.add(dylib, llvm::orc::ThreadSafeModule(std::move(module), ctx))); llvm::cantFail(compileLayer.add(dylib, llvm::orc::ThreadSafeModule(std::move(module), std::move(context))));
// Resolve the function addresses. // Resolve the function addresses.
for(size_t i = 0; i < count; i++) for(size_t i = 0; i < count; i++)
...@@ -736,8 +734,9 @@ namespace rr { ...@@ -736,8 +734,9 @@ namespace rr {
JITBuilder::JITBuilder(const rr::Config &config) JITBuilder::JITBuilder(const rr::Config &config)
: config(config) : config(config)
, module(new llvm::Module("", context)) , context(new llvm::LLVMContext())
, builder(new llvm::IRBuilder<>(context)) , module(new llvm::Module("", *context))
, builder(new llvm::IRBuilder<>(*context))
{ {
module->setTargetTriple(LLVM_DEFAULT_TARGET_TRIPLE); module->setTargetTriple(LLVM_DEFAULT_TARGET_TRIPLE);
module->setDataLayout(JITGlobals::get()->getDataLayout()); module->setDataLayout(JITGlobals::get()->getDataLayout());
...@@ -787,7 +786,7 @@ void JITBuilder::optimize(const rr::Config &cfg) ...@@ -787,7 +786,7 @@ void JITBuilder::optimize(const rr::Config &cfg)
std::shared_ptr<rr::Routine> JITBuilder::acquireRoutine(const char *name, llvm::Function **funcs, size_t count, const rr::Config &cfg) std::shared_ptr<rr::Routine> JITBuilder::acquireRoutine(const char *name, llvm::Function **funcs, size_t count, const rr::Config &cfg)
{ {
ASSERT(module); ASSERT(module);
return std::make_shared<JITRoutine>(std::move(module), name, funcs, count, cfg); return std::make_shared<JITRoutine>(std::move(module), std::move(context), name, funcs, count, cfg);
} }
} // namespace rr } // namespace rr
...@@ -95,7 +95,7 @@ public: ...@@ -95,7 +95,7 @@ public:
std::shared_ptr<rr::Routine> acquireRoutine(const char *name, llvm::Function **funcs, size_t count, const rr::Config &cfg); std::shared_ptr<rr::Routine> acquireRoutine(const char *name, llvm::Function **funcs, size_t count, const rr::Config &cfg);
const Config config; const Config config;
llvm::LLVMContext context; std::unique_ptr<llvm::LLVMContext> context;
std::unique_ptr<llvm::Module> module; std::unique_ptr<llvm::Module> module;
std::unique_ptr<llvm::IRBuilder<>> builder; std::unique_ptr<llvm::IRBuilder<>> builder;
llvm::Function *function = nullptr; llvm::Function *function = nullptr;
......
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