[MLIR] Add extra locking during cubin generation.
authorStephan Herhut <herhut@google.com>
Tue, 21 Apr 2020 19:21:37 +0000 (21:21 +0200)
committerStephan Herhut <herhut@google.com>
Wed, 22 Apr 2020 06:57:45 +0000 (08:57 +0200)
We also need to lock the LLVMDialect mutex when initializing
LLVM targets or destroying llvm modules concurrently. Added another
scoped lock to that effect.

Differential Revision: https://reviews.llvm.org/D78580

mlir/lib/Conversion/GPUToCUDA/ConvertKernelFuncToCubin.cpp

index 7cdb0dd..3f99c56 100644 (file)
@@ -31,6 +31,7 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/Mutex.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
@@ -57,6 +58,12 @@ public:
   void runOnOperation() override {
     gpu::GPUModuleOp module = getOperation();
 
+    // Lock access to the llvm context.
+    llvm::sys::SmartScopedLock<true> scopedLock(
+        module.getContext()
+            ->getRegisteredDialect<LLVM::LLVMDialect>()
+            ->getLLVMContextMutex());
+
     // Make sure the NVPTX target is initialized.
     LLVMInitializeNVPTXTarget();
     LLVMInitializeNVPTXTargetInfo();