[compiler-rt] [NFC][TSan] Make Context::nreported atomic (PR #228634)

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 18:51:40 PDT 2026


https://github.com/vitalybuka created https://github.com/llvm/llvm-project/pull/228634

Make Context::nreported an atomic_uint32_t so that Finalize and
__tsan_report_count can read it without relying on report_mtx.

Assisted-by: Gemini


>From b84da7df4b180844b59c9b60467c932e265c79e9 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Fri, 2 Oct 2026 18:51:25 -0700
Subject: [PATCH] Convert to cherry-pick

Created using spr 1.3.7
---
 compiler-rt/lib/tsan/go/tsan_go.cpp          | 3 +--
 compiler-rt/lib/tsan/rtl/tsan_rtl.cpp        | 6 +++---
 compiler-rt/lib/tsan/rtl/tsan_rtl.h          | 2 +-
 compiler-rt/lib/tsan/rtl/tsan_rtl_report.cpp | 2 +-
 4 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/compiler-rt/lib/tsan/go/tsan_go.cpp b/compiler-rt/lib/tsan/go/tsan_go.cpp
index c987ceacaaea0d3..5f6cd0465d7a48a 100644
--- a/compiler-rt/lib/tsan/go/tsan_go.cpp
+++ b/compiler-rt/lib/tsan/go/tsan_go.cpp
@@ -303,8 +303,7 @@ void __tsan_go_ignore_sync_begin(ThreadState *thr) {
 void __tsan_go_ignore_sync_end(ThreadState *thr) { ThreadIgnoreSyncEnd(thr); }
 
 void __tsan_report_count(u64 *pn) {
-  Lock lock(&ctx->report_mtx);
-  *pn = ctx->nreported;
+  *pn = atomic_load_relaxed(&ctx->nreported);
 }
 
 }  // extern "C"
diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp b/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp
index a9147f09e34cba3..e00f2ab5917357d 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp
@@ -819,12 +819,12 @@ int Finalize(ThreadState *thr) {
 
   ThreadFinalize(thr);
 
-  if (ctx->nreported) {
+  if (u32 nreported = atomic_load_relaxed(&ctx->nreported)) {
     failed = true;
 #if !SANITIZER_GO
-    Printf("ThreadSanitizer: reported %d warnings\n", ctx->nreported);
+    Printf("ThreadSanitizer: reported %u warnings\n", nreported);
 #else
-    Printf("Found %d data race(s)\n", ctx->nreported);
+    Printf("Found %u data race(s)\n", nreported);
 #endif
   }
 
diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl.h b/compiler-rt/lib/tsan/rtl/tsan_rtl.h
index 3d1018accafc402..db3c879e9e030b3 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl.h
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl.h
@@ -318,7 +318,7 @@ struct Context {
   MetaMap metamap;
 
   Mutex report_mtx;
-  int nreported;
+  atomic_uint32_t nreported;
   atomic_uint64_t last_symbolize_time_ns;
 
   void *background_thread;
diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cpp b/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cpp
index 4e58305b582d5a0..3bf1d6a5a81e115 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl_report.cpp
@@ -713,7 +713,7 @@ bool OutputReport(ThreadState *thr, ScopedReport &srep) {
   }
   PrintReport(rep);
   __tsan_on_report(rep);
-  ctx->nreported++;
+  atomic_fetch_add(&ctx->nreported, 1, memory_order_relaxed);
   if (flags()->halt_on_error)
     Die();
   thr->current_report = nullptr;



More information about the llvm-commits mailing list