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

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.

>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] [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());
 }

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

Reply via email to