https://github.com/wenju-he updated 
https://github.com/llvm/llvm-project/pull/200102

>From 304f1655b344dee99b0c80a20154fcd78bdfe8e3 Mon Sep 17 00:00:00 2001
From: Wenju He <[email protected]>
Date: Wed, 27 May 2026 13:58:26 +0200
Subject: [PATCH 1/2] [Clang] Workaround write-to-global race condition in
 BackendConsumer ctor

When multiple threads launch multiple clang::CompilerInstance to compile
sources, there is segfault and thread-sanitizer reports race condition
in simultaneously writing to global variables llvm::TimePassesIsEnabled
and llvm::TimePassesPerRun in BackendConsumer constructor.

This PR workarounds the issue by skipping the write when TimePasses is
false. The actual values assigned to TimePassesIsEnabled and
TimePassesPerRun are not changed.

Race condition still exists when TimePasses is true, but that is less
common scenario.
---
 clang/lib/CodeGen/CodeGenAction.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenAction.cpp 
b/clang/lib/CodeGen/CodeGenAction.cpp
index fb1a410ce1761..c7db71fdfc9f5 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -122,8 +122,10 @@ BackendConsumer::BackendConsumer(CompilerInstance &CI, 
BackendAction Action,
       Gen(CreateLLVMCodeGen(CI, InFile, C, CoverageInfo)),
       LinkModules(std::move(LinkModules)), CurLinkModule(CurLinkModule) {
   TimerIsEnabled = CodeGenOpts.TimePasses;
-  llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
-  llvm::TimePassesPerRun = CodeGenOpts.TimePassesPerRun;
+  if (CodeGenOpts.TimePasses) {
+    llvm::TimePassesIsEnabled = true;
+    llvm::TimePassesPerRun = CodeGenOpts.TimePassesPerRun;
+  }
   if (CodeGenOpts.TimePasses)
     LLVMIRGeneration.init("irgen", "LLVM IR generation", CI.getTimerGroup());
 }

>From a5ad9911091450d84f69b940186f532abcccff93 Mon Sep 17 00:00:00 2001
From: Wenju He <[email protected]>
Date: Fri, 29 May 2026 05:12:25 +0200
Subject: [PATCH 2/2] use ManagedStatic SmartMutex and SmartScopedLock

---
 clang/lib/CodeGen/CodeGenAction.cpp | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenAction.cpp 
b/clang/lib/CodeGen/CodeGenAction.cpp
index c7db71fdfc9f5..1371fd48cb524 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -46,7 +46,9 @@
 #include "llvm/LTO/LTOBackend.h"
 #include "llvm/Linker/Linker.h"
 #include "llvm/Pass.h"
+#include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Mutex.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TimeProfiler.h"
 #include "llvm/Support/Timer.h"
@@ -60,6 +62,10 @@ using namespace llvm;
 
 #define DEBUG_TYPE "codegenaction"
 
+namespace {
+llvm::ManagedStatic<llvm::sys::SmartMutex<true>> TimePassesMutex;
+}
+
 namespace clang {
 class BackendConsumer;
 class ClangDiagnosticHandler final : public DiagnosticHandler {
@@ -122,8 +128,9 @@ BackendConsumer::BackendConsumer(CompilerInstance &CI, 
BackendAction Action,
       Gen(CreateLLVMCodeGen(CI, InFile, C, CoverageInfo)),
       LinkModules(std::move(LinkModules)), CurLinkModule(CurLinkModule) {
   TimerIsEnabled = CodeGenOpts.TimePasses;
-  if (CodeGenOpts.TimePasses) {
-    llvm::TimePassesIsEnabled = true;
+  {
+    llvm::sys::SmartScopedLock<true> Lock(*TimePassesMutex);
+    llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
     llvm::TimePassesPerRun = CodeGenOpts.TimePassesPerRun;
   }
   if (CodeGenOpts.TimePasses)

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to