Commit cee3dffa by Ben Clayton

LLVMReactor: Mutex calls to llvm::orc::IRCompileLayer.

TSAN claims there are data races in the compileLayer. Specifically, it takes objection to compileLayer.addModule() being called at the same time as compileLayer.removeModule(). Bug: b/133127573 Change-Id: I19196d0c952bc308c2e47a848c93b092d5eea1ea Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/31832 Presubmit-Ready: Ben Clayton <bclayton@google.com> Tested-by: 's avatarBen Clayton <bclayton@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
parent 88816fa8
...@@ -76,9 +76,10 @@ ...@@ -76,9 +76,10 @@
#include <unordered_map> #include <unordered_map>
#include <fstream> #include <fstream>
#include <iostream>
#include <mutex>
#include <numeric> #include <numeric>
#include <thread> #include <thread>
#include <iostream>
#if defined(__i386__) || defined(__x86_64__) #if defined(__i386__) || defined(__x86_64__)
#include <xmmintrin.h> #include <xmmintrin.h>
...@@ -599,7 +600,8 @@ namespace rr ...@@ -599,7 +600,8 @@ namespace rr
std::unique_ptr<llvm::TargetMachine> targetMachine; std::unique_ptr<llvm::TargetMachine> targetMachine;
const llvm::DataLayout dataLayout; const llvm::DataLayout dataLayout;
ObjLayer objLayer; ObjLayer objLayer;
CompileLayer compileLayer; CompileLayer compileLayer; // guarded by mutex
std::mutex mutex;
size_t emittedFunctionsNum; size_t emittedFunctionsNum;
public: public:
...@@ -690,23 +692,28 @@ namespace rr ...@@ -690,23 +692,28 @@ namespace rr
mod->setDataLayout(dataLayout); mod->setDataLayout(dataLayout);
auto moduleKey = session.allocateVModule(); auto moduleKey = session.allocateVModule();
// Resolve the function symbols - needs to be performed under mutex lock.
std::vector<llvm::JITSymbol> symbols;
{
std::unique_lock<std::mutex> lock(mutex);
llvm::cantFail(compileLayer.addModule(moduleKey, std::move(mod))); llvm::cantFail(compileLayer.addModule(moduleKey, std::move(mod)));
funcs = nullptr; // Now points to released memory. funcs = nullptr; // Now points to released memory.
for (size_t i = 0; i < count; i++)
{
symbols.push_back(compileLayer.findSymbolIn(moduleKey, mangledNames[i], false));
}
}
// Resolve the function addresses. // Resolve the function addresses.
std::vector<void*> addresses(count); std::vector<void*> addresses(count);
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)
{ {
llvm::JITSymbol symbol = compileLayer.findSymbolIn(moduleKey, mangledNames[i], false); if(auto expectAddr = symbols[i].getAddress())
llvm::Expected<llvm::JITTargetAddress> expectAddr = symbol.getAddress();
if(!expectAddr)
{ {
return nullptr;
}
addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(expectAddr.get())); addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(expectAddr.get()));
} }
}
return new LLVMRoutine(addresses.data(), count, releaseRoutineCallback, this, moduleKey); return new LLVMRoutine(addresses.data(), count, releaseRoutineCallback, this, moduleKey);
} }
...@@ -750,6 +757,7 @@ namespace rr ...@@ -750,6 +757,7 @@ namespace rr
private: private:
void releaseRoutineModule(llvm::orc::VModuleKey moduleKey) void releaseRoutineModule(llvm::orc::VModuleKey moduleKey)
{ {
std::unique_lock<std::mutex> lock(mutex);
llvm::cantFail(compileLayer.removeModule(moduleKey)); llvm::cantFail(compileLayer.removeModule(moduleKey));
} }
......
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