[PATCH] D51126: [ORC] Lock mutex in IRCompileLayer2::emit() to avoid calling into PassManager::run() in parallel.

Stefan Gränitz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 22 14:04:23 PDT 2018


sgraenitz created this revision.
sgraenitz added a reviewer: lhames.
Herald added a subscriber: llvm-commits.

I always thought that support for multithreaded codegen is implicitly given when using different LLVMContext instances. It doesn't look like that here. 
Maybe there's more requirements to it? Otherwise I don't see how to avoid a lock like this.

What do you think?


Repository:
  rL LLVM

https://reviews.llvm.org/D51126

Files:
  lib/ExecutionEngine/Orc/IRCompileLayer.cpp


Index: lib/ExecutionEngine/Orc/IRCompileLayer.cpp
===================================================================
--- lib/ExecutionEngine/Orc/IRCompileLayer.cpp
+++ lib/ExecutionEngine/Orc/IRCompileLayer.cpp
@@ -21,23 +21,34 @@
   this->NotifyCompiled = std::move(NotifyCompiled);
 }
 
+// FIXME: Quick hack to avoid calling into PassManager::run() in parallel.
+static std::mutex PassManagerRunMutex;
+  
 void IRCompileLayer2::emit(MaterializationResponsibility R, VModuleKey K,
                            std::unique_ptr<Module> M) {
   assert(M && "Module must not be null");
 
-  if (auto Obj = Compile(*M)) {
-    {
-      std::lock_guard<std::mutex> Lock(IRLayerMutex);
-      if (NotifyCompiled)
-        NotifyCompiled(K, std::move(M));
-      else
-        M = nullptr;
+  std::unique_ptr<MemoryBuffer> Obj;
+  {
+    std::lock_guard<std::mutex> Lock(PassManagerRunMutex);
+    if (auto CompileRes = Compile(*M)) {
+      Obj = std::move(*CompileRes);
+      assert(Obj && "In success case Obj cannot be null");
+    } else {
+      R.failMaterialization();
+      getExecutionSession().reportError(CompileRes.takeError());
+      return;
     }
-    BaseLayer.emit(std::move(R), std::move(K), std::move(*Obj));
-  } else {
-    R.failMaterialization();
-    getExecutionSession().reportError(Obj.takeError());
   }
+
+  {
+    std::lock_guard<std::mutex> Lock(IRLayerMutex);
+    if (NotifyCompiled)
+      NotifyCompiled(K, std::move(M));
+    else
+      M = nullptr;
+  }
+  BaseLayer.emit(std::move(R), std::move(K), std::move(Obj));
 }
 
 } // End namespace orc.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51126.162049.patch
Type: text/x-patch
Size: 1584 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180822/40840293/attachment.bin>


More information about the llvm-commits mailing list