Fix data race: different mutexes were guarding the same data (#8856)
diff --git a/iree/compiler/Codegen/LLVMCPU/ConvertToLLVM.cpp b/iree/compiler/Codegen/LLVMCPU/ConvertToLLVM.cpp
index c0927e1..6f746d9 100644
--- a/iree/compiler/Codegen/LLVMCPU/ConvertToLLVM.cpp
+++ b/iree/compiler/Codegen/LLVMCPU/ConvertToLLVM.cpp
@@ -75,8 +75,7 @@
// Returns a Type representing iree_hal_processor_v0_t.
static LLVM::LLVMStructType getProcessorType(
MLIRContext *context, LLVMTypeConverter *typeConverter) {
- static llvm::sys::Mutex mutex;
- llvm::sys::ScopedLock lock(mutex);
+ llvm::sys::ScopedLock lock(sMutex);
auto structType =
LLVM::LLVMStructType::getIdentified(context, "iree_hal_processor_v0_t");
if (structType.isInitialized()) return structType;
@@ -108,8 +107,7 @@
static LLVM::LLVMStructType getEnvironmentType(
MLIRContext *context, LLVMTypeConverter *typeConverter,
LLVM::LLVMStructType processorType) {
- static llvm::sys::Mutex mutex;
- llvm::sys::ScopedLock lock(mutex);
+ llvm::sys::ScopedLock lock(sMutex);
auto structType = LLVM::LLVMStructType::getIdentified(
context, "iree_hal_executable_environment_v0_t");
if (structType.isInitialized()) return structType;
@@ -164,8 +162,7 @@
// Returns a Type representing iree_hal_executable_dispatch_state_v0_t.
static LLVM::LLVMStructType getDispatchStateType(
MLIRContext *context, LLVMTypeConverter *typeConverter) {
- static llvm::sys::Mutex mutex;
- llvm::sys::ScopedLock lock(mutex);
+ llvm::sys::ScopedLock lock(sMutex);
auto structType = LLVM::LLVMStructType::getIdentified(
context, "iree_hal_executable_dispatch_state_v0_t");
if (structType.isInitialized()) return structType;
@@ -229,8 +226,7 @@
// Returns a Type representing iree_hal_executable_workgroup_state_v0_t.
static LLVM::LLVMStructType getWorkgroupStateType(
MLIRContext *context, LLVMTypeConverter *typeConverter) {
- static llvm::sys::Mutex mutex;
- llvm::sys::ScopedLock lock(mutex);
+ llvm::sys::ScopedLock lock(sMutex);
auto structType = LLVM::LLVMStructType::getIdentified(
context, "iree_hal_executable_workgroup_state_v0_t");
if (structType.isInitialized()) return structType;
@@ -602,8 +598,14 @@
LLVM::LLVMStructType environmentType;
LLVM::LLVMStructType dispatchStateType;
LLVM::LLVMStructType workgroupStateType;
+
+ // Used to lock around mutations of shared LLVM type information, e.g.
+ // mlir::LLVM::LLVMStructType::getIdentified.
+ static llvm::sys::Mutex sMutex;
};
+llvm::sys::Mutex HALDispatchABI::sMutex;
+
/// Converts Standard MLIR FuncOps to LLVMFuncOps matching the IREE HAL ABI.
/// This is an IREE-specific conversion that assumes the input function is
/// `() -> ()` and that hal.interface.* ops are used to access all state.