LLVMJIT: Remove the TargetMachine cache. TargetMachine have internal fields mutated during compilation. In order to support concurrent implementation, each compile needs a new TargetMachine. Bug: b/153803432 Change-Id: I1a46d8e0193e6176c777d82ce10008afca91ec94 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/44013 Reviewed-by: Antonio Maiorano <amaiorano@google.com> Tested-by: Ben Clayton <bclayton@google.com> Kokoro-Result: kokoro <noreply+kokoro@google.com>
diff --git a/src/Reactor/LLVMJIT.cpp b/src/Reactor/LLVMJIT.cpp index 6dd5c48..bbd91e3 100644 --- a/src/Reactor/LLVMJIT.cpp +++ b/src/Reactor/LLVMJIT.cpp
@@ -75,41 +75,6 @@ namespace { -// Cache provides a simple, thread-safe key-value store. -template<typename KEY, typename VALUE> -class Cache -{ -public: - Cache() = default; - Cache(const Cache &other); - VALUE getOrCreate(KEY key, std::function<VALUE()> create); - -private: - mutable std::mutex mutex; // mutable required for copy constructor. - std::unordered_map<KEY, VALUE> map; -}; - -template<typename KEY, typename VALUE> -Cache<KEY, VALUE>::Cache(const Cache &other) -{ - std::unique_lock<std::mutex> lock(other.mutex); - map = other.map; -} - -template<typename KEY, typename VALUE> -VALUE Cache<KEY, VALUE>::getOrCreate(KEY key, std::function<VALUE()> create) -{ - std::unique_lock<std::mutex> lock(mutex); - auto it = map.find(key); - if(it != map.end()) - { - return it->second; - } - auto value = create(); - map.emplace(key, value); - return value; -} - // JITGlobals is a singleton that holds all the immutable machine specific // information for the host device. class JITGlobals @@ -125,7 +90,7 @@ const llvm::TargetOptions targetOptions; const llvm::DataLayout dataLayout; - TargetMachineSPtr getTargetMachine(rr::Optimization::Level optlevel); + TargetMachineSPtr createTargetMachine(rr::Optimization::Level optlevel); private: static JITGlobals create(); @@ -136,8 +101,6 @@ const llvm::TargetOptions &targetOptions, const llvm::DataLayout &dataLayout); JITGlobals(const JITGlobals &) = default; - - Cache<rr::Optimization::Level, TargetMachineSPtr> targetMachines; }; JITGlobals *JITGlobals::get() @@ -146,7 +109,7 @@ return &instance; } -JITGlobals::TargetMachineSPtr JITGlobals::getTargetMachine(rr::Optimization::Level optlevel) +JITGlobals::TargetMachineSPtr JITGlobals::createTargetMachine(rr::Optimization::Level optlevel) { #ifdef ENABLE_RR_DEBUG_INFO auto llvmOptLevel = toLLVM(rr::Optimization::Level::None); @@ -154,15 +117,13 @@ auto llvmOptLevel = toLLVM(optlevel); #endif // ENABLE_RR_DEBUG_INFO - return targetMachines.getOrCreate(optlevel, [&]() { - return TargetMachineSPtr(llvm::EngineBuilder() - .setOptLevel(llvmOptLevel) - .setMCPU(mcpu) - .setMArch(march) - .setMAttrs(mattrs) - .setTargetOptions(targetOptions) - .selectTarget()); - }); + return TargetMachineSPtr(llvm::EngineBuilder() + .setOptLevel(llvmOptLevel) + .setMCPU(mcpu) + .setMArch(march) + .setMAttrs(mattrs) + .setTargetOptions(targetOptions) + .selectTarget()); } JITGlobals JITGlobals::create() @@ -618,7 +579,7 @@ return; } })) - , targetMachine(JITGlobals::get()->getTargetMachine(config.getOptimization().getLevel())) + , targetMachine(JITGlobals::get()->createTargetMachine(config.getOptimization().getLevel())) , compileLayer(objLayer, llvm::orc::SimpleCompiler(*targetMachine)) , objLayer( session,