[clang] [compiler-rt] [llvm] [InstrProf][Driver] Make InstrProfiling behave with TSan (PR #230582)

Dan Blackwell via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 09:47:20 PST 2026


https://github.com/DanBlackwell updated https://github.com/llvm/llvm-project/pull/230582

>From 5b578a157c1cdbb42475c06e0cf6bbd3d9ccc468 Mon Sep 17 00:00:00 2001
From: Dan Blackwell <dan_blackwell at apple.com>
Date: Fri, 9 Oct 2026 08:20:43 -0700
Subject: [PATCH 01/10] [InstrProf] Make InstrProfiling behave with TSan

I have been seeing an issue whereby TSan reports a race on (non-atomic) profiling counters. While the TSan pass does try to avoid instrumenting these, it is possible for there to be a phi node joining a counter address with a regular variable address - in which case we do want to instrument the access.

This patch follows in GCOVProfiling.cpp's footsteps and adds nosanitize metadata to the counter accesses, and also promotes them to atomics (note that the TSan runtime already incurs a huge overhead). Note that the phi join could still occur between an atomic counter access and an atomic variable access, however TSan does not treat atomic accesses as data races.

Assisted by: claude

rdar://188406188
---
 .../Instrumentation/InstrProfiling.cpp        | 72 +++++++++++++------
 .../Instrumentation/InstrProfiling/mcdc.ll    |  2 +-
 2 files changed, 52 insertions(+), 22 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
index 6a7bda565ef212..7a0a4e2d64f42b 100644
--- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -118,8 +118,10 @@ static void makeAtomic(Instruction *Load, Instruction *Store) {
   auto *Addend = Addition->getOperand(1);
 
   IRBuilder<> Builder(Load);
-  Builder.CreateAtomicRMW(AtomicRMWInst::Add, Store->getOperand(1), Addend,
-                          MaybeAlign(), AtomicOrdering::Monotonic);
+  Builder
+      .CreateAtomicRMW(AtomicRMWInst::Add, Store->getOperand(1), Addend,
+                       MaybeAlign(), AtomicOrdering::Monotonic)
+      ->setNoSanitizeMetadata();
   Store->eraseFromParent();
   Addition->eraseFromParent();
   Load->eraseFromParent();
@@ -227,8 +229,8 @@ class InstrLowerer final {
   /// Returns true if profile counter update register promotion is enabled.
   bool isCounterPromotionEnabled() const;
 
-  /// Returns true if profile counter updates should be atomic.
-  bool isAtomic() const;
+  /// Returns true if profile counter updates in \p F should be atomic.
+  bool isAtomic(const Function &F) const;
 
   /// Return true if profile sampling is enabled.
   bool isSamplingEnabled() const;
@@ -393,12 +395,16 @@ class PGOCounterPromoterHelper : public LoadAndStorePromoter {
       // Generate the relaxed atomic RMW if we've asked for it and no more
       // promotion is possible.
       if ((IsAtomic && !TargetLoop) || Opts.atomic_counter_update_promoted)
-        Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue,
-                                MaybeAlign(), AtomicOrdering::Monotonic);
+        Builder
+            .CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue,
+                             MaybeAlign(), AtomicOrdering::Monotonic)
+            ->setNoSanitizeMetadata();
       else {
         LoadInst *OldVal = Builder.CreateLoad(Ty, Addr, "pgocount.promoted");
+        OldVal->setNoSanitizeMetadata();
         auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue);
         auto *NewStore = Builder.CreateStore(NewVal, Addr);
+        NewStore->setNoSanitizeMetadata();
 
         // Now update the parent loop's candidate list:
         if (TargetLoop)
@@ -826,8 +832,11 @@ bool InstrLowerer::isCounterPromotionEnabled() const {
   return valueOr(Opts.do_counter_promotion, Options.DoCounterPromotion);
 }
 
-bool InstrLowerer::isAtomic() const {
-  return Options.Atomic || Opts.instrprof_atomic_counter_update_all;
+bool InstrLowerer::isAtomic(const Function &F) const {
+  // Non-atomic counter updates are data races, which ThreadSanitizer would
+  // report. Use atomic updates in functions that TSan instruments.
+  return Options.Atomic || Opts.instrprof_atomic_counter_update_all ||
+         F.hasFnAttribute(Attribute::SanitizeThread);
 }
 
 static void doAtomicCheck(Function *F) {
@@ -869,7 +878,7 @@ void InstrLowerer::promoteCounterLoadStores(Function *F) {
     BasicBlock *BB = CounterLoad->getParent();
     Loop *ParentLoop = LI.getLoopFor(BB);
     if (!ParentLoop) {
-      if (isAtomic())
+      if (isAtomic(*F))
         makeAtomic(CounterLoad, CounterStore);
       continue;
     }
@@ -882,11 +891,11 @@ void InstrLowerer::promoteCounterLoadStores(Function *F) {
   // iteratively hoisted outside the loop nest.
   for (auto *Loop : llvm::reverse(Loops)) {
     PGOCounterPromoter Promoter(Opts, LoopPromotionCandidates, *Loop, LI,
-                                BFI.get(), isAtomic());
+                                BFI.get(), isAtomic(*F));
     Promoter.run(&TotalCountersPromoted);
   }
 
-  if (isAtomic() && Opts.verify_atomic_counter_promoted)
+  if (isAtomic(*F) && Opts.verify_atomic_counter_promoted)
     doAtomicCheck(F);
 }
 
@@ -1147,11 +1156,19 @@ Value *InstrLowerer::getBitmapAddress(InstrProfMCDCTVBitmapUpdate *I) {
 void InstrLowerer::lowerCover(InstrProfCoverInst *CoverInstruction) {
   auto *Addr = getCounterAddress(CoverInstruction);
   IRBuilder<> Builder(CoverInstruction);
+  // Optimizations can merge the counter update with a user access, and then
+  // ThreadSanitizer ends up instrumenting the counter access - and can report
+  // it as a data race. Making updates atomic cures this.
+  bool TSanInstrumented = CoverInstruction->getFunction()->hasFnAttribute(
+      Attribute::SanitizeThread);
   if (Opts.conditional_counter_update) {
     Instruction *SplitBefore = CoverInstruction->getNextNode();
     auto &Ctx = CoverInstruction->getParent()->getContext();
     auto *Int8Ty = llvm::Type::getInt8Ty(Ctx);
-    Value *Load = Builder.CreateLoad(Int8Ty, Addr, "pgocount");
+    auto *Load = Builder.CreateLoad(Int8Ty, Addr, "pgocount");
+    Load->setNoSanitizeMetadata();
+    if (TSanInstrumented)
+      Load->setAtomic(AtomicOrdering::Monotonic);
     Value *Cmp = Builder.CreateIsNotNull(Load, "pgocount.ifnonzero");
     Instruction *ThenBranch =
         SplitBlockAndInsertIfThen(Cmp, SplitBefore, false);
@@ -1159,7 +1176,10 @@ void InstrLowerer::lowerCover(InstrProfCoverInst *CoverInstruction) {
   }
 
   // We store zero to represent that this block is covered.
-  Builder.CreateStore(Builder.getInt8(0), Addr);
+  auto *Store = Builder.CreateStore(Builder.getInt8(0), Addr);
+  Store->setNoSanitizeMetadata();
+  if (TSanInstrumented)
+    Store->setAtomic(AtomicOrdering::Monotonic);
   CoverInstruction->eraseFromParent();
 }
 
@@ -1296,15 +1316,19 @@ void InstrLowerer::lowerIncrement(InstrProfIncrementInst *Inc) {
   auto *Addr = getCounterAddress(Inc);
   // If promotion is enabled then delay generating atomic updates until
   // after promotion is done.
-  if ((!isCounterPromotionEnabled() && isAtomic()) ||
+  if ((!isCounterPromotionEnabled() && isAtomic(*Inc->getFunction())) ||
       (Inc->getIndex()->isNullValue() && Opts.atomic_first_counter)) {
-    Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(),
-                            MaybeAlign(), AtomicOrdering::Monotonic);
+    Builder
+        .CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(), MaybeAlign(),
+                         AtomicOrdering::Monotonic)
+        ->setNoSanitizeMetadata();
   } else {
     Value *IncStep = Inc->getStep();
-    Value *Load = Builder.CreateLoad(IncStep->getType(), Addr, "pgocount");
+    auto *Load = Builder.CreateLoad(IncStep->getType(), Addr, "pgocount");
+    Load->setNoSanitizeMetadata();
     auto *Count = Builder.CreateAdd(Load, Inc->getStep());
     auto *Store = Builder.CreateStore(Count, Addr);
+    Store->setNoSanitizeMetadata();
     if (isCounterPromotionEnabled())
       PromotionCandidates.emplace_back(cast<Instruction>(Load), Store);
   }
@@ -1364,8 +1388,12 @@ void InstrLowerer::lowerMCDCTestVectorBitmapUpdate(
   // Load profile bitmap byte.
   //  %mcdc.bits = load i8, ptr %4, align 1
   auto *Bitmap = Builder.CreateLoad(Int8Ty, BitmapByteAddr, "mcdc.bits");
+  Bitmap->setNoSanitizeMetadata();
+
+  if (isAtomic(*Update->getFunction())) {
+    if (Update->getFunction()->hasFnAttribute(Attribute::SanitizeThread))
+      Bitmap->setAtomic(AtomicOrdering::Monotonic);
 
-  if (isAtomic()) {
     // If ((Bitmap & Val) != Val), then execute atomic (Bitmap |= Val).
     // Note, just-loaded Bitmap might not be up-to-date. Use it just for
     // early testing.
@@ -1379,8 +1407,10 @@ void InstrLowerer::lowerMCDCTestVectorBitmapUpdate(
 
     // Execute if (unlikely(ShouldStore)).
     Builder.SetInsertPoint(ThenBranch);
-    Builder.CreateAtomicRMW(AtomicRMWInst::Or, BitmapByteAddr, ShiftedVal,
-                            MaybeAlign(), AtomicOrdering::Monotonic);
+    Builder
+        .CreateAtomicRMW(AtomicRMWInst::Or, BitmapByteAddr, ShiftedVal,
+                         MaybeAlign(), AtomicOrdering::Monotonic)
+        ->setNoSanitizeMetadata();
   } else {
     // Perform logical OR of profile bitmap byte and shifted bit offset.
     //  %8 = or i8 %mcdc.bits, %7
@@ -1388,7 +1418,7 @@ void InstrLowerer::lowerMCDCTestVectorBitmapUpdate(
 
     // Store the updated profile bitmap byte.
     //  store i8 %8, ptr %3, align 1
-    Builder.CreateStore(Result, BitmapByteAddr);
+    Builder.CreateStore(Result, BitmapByteAddr)->setNoSanitizeMetadata();
   }
 
   Update->eraseFromParent();
diff --git a/llvm/test/Instrumentation/InstrProfiling/mcdc.ll b/llvm/test/Instrumentation/InstrProfiling/mcdc.ll
index ee294a96b04bae..5979f73cb03921 100644
--- a/llvm/test/Instrumentation/InstrProfiling/mcdc.ll
+++ b/llvm/test/Instrumentation/InstrProfiling/mcdc.ll
@@ -36,7 +36,7 @@ entry:
   ; CHECK-NEXT: %[[LAB8:[0-9]+]] = and i32 %[[TEMP]], 7
   ; CHECK-NEXT: %[[LAB9:[0-9]+]] = trunc i32 %[[LAB8]] to i8
   ; CHECK-NEXT: %[[LAB10:[0-9]+]] = shl i8 1, %[[LAB9]]
-  ; CHECK-NEXT: %[[BITS:.+]] = load i8, ptr %[[LAB7]], align 1
+  ; CHECK-NEXT: %[[BITS:.+]] = load i8, ptr %[[LAB7]], align 1, !nosanitize
   ; ATOMIC-NEXT: %[[MASKED:.+]] = and i8 %[[BITS]], %[[LAB10]]
   ; ATOMIC-NEXT: %[[SHOULDWRITE:.+]] = icmp ne i8 %[[MASKED]], %[[LAB10]]
   ; ATOMIC-NEXT: br i1 %[[SHOULDWRITE]], label %[[WRITE:.+]], label %[[SKIP:.+]], !prof ![[MDPROF:[0-9]+]]

>From 722d3e805040c8b904800b3168944c91ff5225c3 Mon Sep 17 00:00:00 2001
From: Dan Blackwell <dan_blackwell at apple.com>
Date: Mon, 12 Oct 2026 10:34:43 -0700
Subject: [PATCH 02/10] Add TSan regression lit test

---
 .../test/tsan/profile-counter-norace.cpp      | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 compiler-rt/test/tsan/profile-counter-norace.cpp

diff --git a/compiler-rt/test/tsan/profile-counter-norace.cpp b/compiler-rt/test/tsan/profile-counter-norace.cpp
new file mode 100644
index 00000000000000..2eb2630b0c07cb
--- /dev/null
+++ b/compiler-rt/test/tsan/profile-counter-norace.cpp
@@ -0,0 +1,36 @@
+// RUN: %clangxx_tsan -fprofile-instr-generate -O3 %s -o %t
+// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
+
+// Check that we don't report data races on accesses to profile counters that
+// have been PHI'd together
+
+#include "test.h"
+
+struct Thing1 {
+  void *x;
+  Thing1(void *x) : x(x) {}
+};
+
+struct Thing2 {
+  void *x;
+  Thing2(void *x) : x(x) {}
+};
+
+static void *thread(void *arg) {
+  if (arg)
+    return Thing1(arg).x;
+  return Thing2(arg).x;
+}
+
+int main() {
+  pthread_t t[2];
+  for (pthread_t &th : t)
+    pthread_create(&th, nullptr, thread, nullptr);
+  for (pthread_t &th : t)
+    pthread_join(th, nullptr);
+
+  fprintf(stderr, "DONE\n");
+  return 0;
+}
+
+// CHECK: DONE

>From 01bedae3157278252460cbd0ae7d1347bf2fbdc7 Mon Sep 17 00:00:00 2001
From: Dan Blackwell <dan_blackwell at apple.com>
Date: Wed, 14 Oct 2026 15:35:52 -0700
Subject: [PATCH 03/10] Add lit feature to make test dependent on profile being
 built

---
 compiler-rt/test/tsan/CMakeLists.txt             | 7 +++++++
 compiler-rt/test/tsan/lit.cfg.py                 | 3 +++
 compiler-rt/test/tsan/lit.site.cfg.py.in         | 1 +
 compiler-rt/test/tsan/profile-counter-norace.cpp | 1 +
 4 files changed, 12 insertions(+)

diff --git a/compiler-rt/test/tsan/CMakeLists.txt b/compiler-rt/test/tsan/CMakeLists.txt
index fa924e09429684..46bbf67443823a 100644
--- a/compiler-rt/test/tsan/CMakeLists.txt
+++ b/compiler-rt/test/tsan/CMakeLists.txt
@@ -15,6 +15,13 @@ else()
   set(TSAN_HAS_LIBCXX False)
 endif()
 
+if(COMPILER_RT_BUILD_PROFILE AND COMPILER_RT_HAS_PROFILE)
+  list(APPEND TSAN_TEST_DEPS profile)
+  set(TSAN_HAS_PROFILE True)
+else()
+  set(TSAN_HAS_PROFILE False)
+endif()
+
 set(TSAN_DYNAMIC_TEST_DEPS ${TSAN_TEST_DEPS})
 set(TSAN_TESTSUITES)
 set(TSAN_DYNAMIC_TESTSUITES)
diff --git a/compiler-rt/test/tsan/lit.cfg.py b/compiler-rt/test/tsan/lit.cfg.py
index 8803a7bda9aa5e..178efa1dee00a7 100644
--- a/compiler-rt/test/tsan/lit.cfg.py
+++ b/compiler-rt/test/tsan/lit.cfg.py
@@ -112,3 +112,6 @@ def build_invocation(compile_flags):
 
 if config.target_os == "NetBSD":
     config.substitutions.insert(0, ("%run", config.netbsd_noaslr_prefix))
+
+if config.has_profile:
+    config.available_features.add("profile")
diff --git a/compiler-rt/test/tsan/lit.site.cfg.py.in b/compiler-rt/test/tsan/lit.site.cfg.py.in
index 458da18a171473..a45288e5dae978 100644
--- a/compiler-rt/test/tsan/lit.site.cfg.py.in
+++ b/compiler-rt/test/tsan/lit.site.cfg.py.in
@@ -3,6 +3,7 @@
 config.name_suffix = "@TSAN_TEST_CONFIG_SUFFIX@"
 config.tsan_lit_source_dir = "@TSAN_LIT_SOURCE_DIR@"
 config.has_libcxx = @TSAN_HAS_LIBCXX@
+config.has_profile = @TSAN_HAS_PROFILE@
 config.apple_platform = "@TSAN_TEST_APPLE_PLATFORM@"
 config.apple_platform_min_deployment_target_flag = "@TSAN_TEST_MIN_DEPLOYMENT_TARGET_FLAG@"
 config.apple_target_is_host = @TSAN_TEST_APPLE_TARGET_IS_HOST_PYBOOL@
diff --git a/compiler-rt/test/tsan/profile-counter-norace.cpp b/compiler-rt/test/tsan/profile-counter-norace.cpp
index 2eb2630b0c07cb..4cddcaec069dcc 100644
--- a/compiler-rt/test/tsan/profile-counter-norace.cpp
+++ b/compiler-rt/test/tsan/profile-counter-norace.cpp
@@ -1,3 +1,4 @@
+// REQUIRES: profile
 // RUN: %clangxx_tsan -fprofile-instr-generate -O3 %s -o %t
 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
 

>From eb0ad19fc6dfab64db1dbfb37647669882ba03e5 Mon Sep 17 00:00:00 2001
From: Dan Blackwell <dan_blackwell at apple.com>
Date: Thu, 15 Oct 2026 12:16:46 -0700
Subject: [PATCH 04/10] Add InstrProfiling IR test for TSan compatibility

---
 .../InstrProfiling/tsan-compatibility.ll      | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll

diff --git a/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll b/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
new file mode 100644
index 00000000000000..bc2c34a18f5481
--- /dev/null
+++ b/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
@@ -0,0 +1,27 @@
+;; Counter increments under TSan should be atomic to prevent it reporting
+;; them as data races. The !nosanitize attribute should also be added to
+;; prevent unnecessary (expensive) calls into the runtime.
+
+; RUN: opt < %s -passes=instrprof -S | FileCheck %s
+
+target triple = "aarch64-unknown-linux-gnu"
+
+ at __profn_foo_with_tsan = private constant [13 x i8] c"foo_with_tsan"
+ at __profn_foo_without_tsan = private constant [16 x i8] c"foo_without_tsan"
+
+; Function Attrs: noinline nounwind optnone sanitize_thread ssp uwtable
+define void @foo_with_tsan() sanitize_thread #0 {
+  ; CHECK-LABEL: define void @foo_with_tsan
+  call void @llvm.instrprof.increment(ptr @__profn_foo_with_tsan, i64 0, i32 1, i32 0)
+  ; CHECK-NEXT: atomicrmw add ptr @__profc_foo_with_tsan, i64 1 monotonic, align 8, !nosanitize
+  ret void
+}
+
+define void @foo_without_tsan() #0 {
+  ; CHECK-LABEL: define void @foo_without_tsan
+  call void @llvm.instrprof.increment(ptr @__profn_foo_without_tsan, i64 0, i32 1, i32 0)
+  ; CHECK-NEXT: [[REGISTER1:%[a-zA-Z0-9_.]+]] = load i64, ptr @__profc_foo_without_tsan, align 8, !nosanitize
+  ; CHECK-NEXT: [[REGISTER2:%[a-zA-Z0-9_.]+]] = add i64 [[REGISTER1]], 1
+  ; CHECK-NEXT: store i64 [[REGISTER2]], ptr @__profc_foo_without_tsan, align 8, !nosanitize
+  ret void
+}
\ No newline at end of file

>From 852e926119e9f9790df2620d95d00a39cbd2d4f9 Mon Sep 17 00:00:00 2001
From: Dan Blackwell <dan_blackwell at apple.com>
Date: Thu, 15 Oct 2026 12:33:01 -0700
Subject: [PATCH 05/10] Add loop checks to the test

---
 .../InstrProfiling/tsan-compatibility.ll      | 103 +++++++++++++++---
 1 file changed, 88 insertions(+), 15 deletions(-)

diff --git a/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll b/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
index bc2c34a18f5481..1ec4e78478a824 100644
--- a/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
+++ b/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
@@ -1,27 +1,100 @@
 ;; Counter increments under TSan should be atomic to prevent it reporting
-;; them as data races. The !nosanitize attribute should also be added to
+;; them as data races. The !nosanitize metadata should also be added to
 ;; prevent unnecessary (expensive) calls into the runtime.
 
 ; RUN: opt < %s -passes=instrprof -S | FileCheck %s
+; RUN: opt < %s -passes=instrprof -do-counter-promotion -S | FileCheck %s --check-prefix=PROMO
 
 target triple = "aarch64-unknown-linux-gnu"
 
- at __profn_foo_with_tsan = private constant [13 x i8] c"foo_with_tsan"
- at __profn_foo_without_tsan = private constant [16 x i8] c"foo_without_tsan"
+ at __profn_straight_line_tsan = private constant [18 x i8] c"straight_line_tsan"
+ at __profn_straight_line_no_tsan = private constant [21 x i8] c"straight_line_no_tsan"
+ at __profn_loop_tsan = private constant [9 x i8] c"loop_tsan"
+ at __profn_loop_no_tsan = private constant [12 x i8] c"loop_no_tsan"
 
-; Function Attrs: noinline nounwind optnone sanitize_thread ssp uwtable
-define void @foo_with_tsan() sanitize_thread #0 {
-  ; CHECK-LABEL: define void @foo_with_tsan
-  call void @llvm.instrprof.increment(ptr @__profn_foo_with_tsan, i64 0, i32 1, i32 0)
-  ; CHECK-NEXT: atomicrmw add ptr @__profc_foo_with_tsan, i64 1 monotonic, align 8, !nosanitize
+define void @straight_line_tsan() sanitize_thread {
+; CHECK-LABEL: define void @straight_line_tsan(
+; CHECK-NEXT:    atomicrmw add ptr @__profc_straight_line_tsan, i64 1 monotonic, align 8, !nosanitize
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.instrprof.increment(ptr @__profn_straight_line_tsan, i64 0, i32 1, i32 0)
   ret void
 }
 
-define void @foo_without_tsan() #0 {
-  ; CHECK-LABEL: define void @foo_without_tsan
-  call void @llvm.instrprof.increment(ptr @__profn_foo_without_tsan, i64 0, i32 1, i32 0)
-  ; CHECK-NEXT: [[REGISTER1:%[a-zA-Z0-9_.]+]] = load i64, ptr @__profc_foo_without_tsan, align 8, !nosanitize
-  ; CHECK-NEXT: [[REGISTER2:%[a-zA-Z0-9_.]+]] = add i64 [[REGISTER1]], 1
-  ; CHECK-NEXT: store i64 [[REGISTER2]], ptr @__profc_foo_without_tsan, align 8, !nosanitize
+define void @straight_line_no_tsan() {
+; CHECK-LABEL: define void @straight_line_no_tsan() {
+; CHECK-NEXT:    %[[LOAD:.+]] = load i64, ptr @__profc_straight_line_no_tsan, align 8, !nosanitize
+; CHECK-NEXT:    %[[ADD:.+]] = add i64 %[[LOAD]], 1
+; CHECK-NEXT:    store i64 %[[ADD]], ptr @__profc_straight_line_no_tsan, align 8, !nosanitize
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.instrprof.increment(ptr @__profn_straight_line_no_tsan, i64 0, i32 1, i32 0)
   ret void
-}
\ No newline at end of file
+}
+
+;; With counter promotion, the loop counter is kept in a register and written
+;; back once at the loop exit. Under TSan that write-back must also be atomic.
+
+define void @loop_tsan(i32 %n) sanitize_thread {
+; PROMO-LABEL: define void @loop_tsan(
+; PROMO:       loop:
+; PROMO-NEXT:    %[[PHI:.+]] = phi i64 [ 0, %preheader ], [ %[[NEXT:.+]], %loop ]
+; PROMO-NOT:     atomicrmw
+; PROMO:         %[[NEXT]] = add i64 %[[PHI]], 1
+; PROMO-NOT:     atomicrmw
+; PROMO:       loop.exit:
+; PROMO-NEXT:    atomicrmw add ptr @__profc_loop_tsan, i64 %[[NEXT]] monotonic, align 8, !nosanitize
+;
+entry:
+  call void @llvm.instrprof.increment(ptr @__profn_loop_tsan, i64 0, i32 2, i32 1)
+  %enter = icmp sgt i32 %n, 0
+  br i1 %enter, label %preheader, label %exit
+
+preheader:
+  br label %loop
+
+loop:
+  %i = phi i32 [ 0, %preheader ], [ %inc, %loop ]
+  call void @llvm.instrprof.increment(ptr @__profn_loop_tsan, i64 0, i32 2, i32 0)
+  %inc = add i32 %i, 1
+  %cmp = icmp slt i32 %inc, %n
+  br i1 %cmp, label %loop, label %loop.exit
+
+loop.exit:
+  br label %exit
+
+exit:
+  ret void
+}
+
+define void @loop_no_tsan(i32 %n) {
+; PROMO-LABEL: define void @loop_no_tsan(
+; PROMO:       loop:
+; PROMO-NEXT:    %[[PHI2:.+]] = phi i64 [ 0, %preheader ], [ %[[NEXT2:.+]], %loop ]
+; PROMO:         %[[NEXT2]] = add i64 %[[PHI2]], 1
+; PROMO:       loop.exit:
+; PROMO-NEXT:    %[[PROMOTED:.+]] = load i64, ptr @__profc_loop_no_tsan, align 8, !nosanitize
+; PROMO-NEXT:    %[[SUM:.+]] = add i64 %[[PROMOTED]], %[[NEXT2]]
+; PROMO-NEXT:    store i64 %[[SUM]], ptr @__profc_loop_no_tsan, align 8, !nosanitize
+;
+entry:
+  call void @llvm.instrprof.increment(ptr @__profn_loop_no_tsan, i64 0, i32 2, i32 1)
+  %enter = icmp sgt i32 %n, 0
+  br i1 %enter, label %preheader, label %exit
+
+preheader:
+  br label %loop
+
+loop:
+  %i = phi i32 [ 0, %preheader ], [ %inc, %loop ]
+  call void @llvm.instrprof.increment(ptr @__profn_loop_no_tsan, i64 0, i32 2, i32 0)
+  %inc = add i32 %i, 1
+  %cmp = icmp slt i32 %inc, %n
+  br i1 %cmp, label %loop, label %loop.exit
+
+loop.exit:
+  br label %exit
+
+exit:
+  ret void
+}

>From 0df64626a768a53519ae424c7adfc6b16e17c53e Mon Sep 17 00:00:00 2001
From: Dan Blackwell <dan_blackwell at apple.com>
Date: Thu, 15 Oct 2026 15:36:13 -0700
Subject: [PATCH 06/10] Add mcdc and conditional counter updates test functions

---
 .../InstrProfiling/tsan-compatibility.ll      | 68 ++++++++++++++++++-
 1 file changed, 67 insertions(+), 1 deletion(-)

diff --git a/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll b/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
index 1ec4e78478a824..a97b633671d0df 100644
--- a/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
+++ b/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
@@ -2,7 +2,8 @@
 ;; them as data races. The !nosanitize metadata should also be added to
 ;; prevent unnecessary (expensive) calls into the runtime.
 
-; RUN: opt < %s -passes=instrprof -S | FileCheck %s
+; RUN: opt < %s -passes=instrprof -S | FileCheck %s --check-prefixes=CHECK,NOCOND
+; RUN: opt < %s -passes=instrprof -conditional-counter-update -S | FileCheck %s --check-prefixes=CHECK,COND
 ; RUN: opt < %s -passes=instrprof -do-counter-promotion -S | FileCheck %s --check-prefix=PROMO
 
 target triple = "aarch64-unknown-linux-gnu"
@@ -11,6 +12,8 @@ target triple = "aarch64-unknown-linux-gnu"
 @__profn_straight_line_no_tsan = private constant [21 x i8] c"straight_line_no_tsan"
 @__profn_loop_tsan = private constant [9 x i8] c"loop_tsan"
 @__profn_loop_no_tsan = private constant [12 x i8] c"loop_no_tsan"
+ at __profn_mcdc_tsan = private constant [9 x i8] c"mcdc_tsan"
+ at __profn_mcdc_no_tsan = private constant [12 x i8] c"mcdc_no_tsan"
 
 define void @straight_line_tsan() sanitize_thread {
 ; CHECK-LABEL: define void @straight_line_tsan(
@@ -98,3 +101,66 @@ loop.exit:
 exit:
   ret void
 }
+
+;; Under TSan the coverage store and the MC/DC bitmap load and update should be
+;; atomic. With -conditional-counter-update the coverage store is guarded by a
+;; load of the counter, which must also be atomic.
+
+define dso_local void @mcdc_tsan(i32 noundef %A) sanitize_thread {
+; CHECK-LABEL: define dso_local void @mcdc_tsan(
+; NOCOND:        store atomic i8 0, ptr @__profc_mcdc_tsan monotonic, align 1, !nosanitize
+; COND:          %pgocount = load atomic i8, ptr @__profc_mcdc_tsan monotonic, align 1, !nosanitize
+; COND-NEXT:     %pgocount.ifnonzero = icmp ne i8 %pgocount, 0
+; COND-NEXT:     br i1 %pgocount.ifnonzero, label %[[COVER:.+]], label %[[CONT:.+]]
+; COND:        [[COVER]]:
+; COND-NEXT:     store atomic i8 0, ptr @__profc_mcdc_tsan monotonic, align 1, !nosanitize
+; COND-NEXT:     br label %[[CONT]]
+; COND:        [[CONT]]:
+; CHECK:         %[[ADDR:.+]] = getelementptr inbounds i8, ptr @__profbm_mcdc_tsan, i32 %{{.+}}
+; CHECK:         %[[SHIFTED:.+]] = shl i8 1, %{{.+}}
+; CHECK-NEXT:    %[[BITS:.+]] = load atomic i8, ptr %[[ADDR]] monotonic, align 1, !nosanitize
+; CHECK-NEXT:    %[[MASKED:.+]] = and i8 %[[BITS]], %[[SHIFTED]]
+; CHECK-NEXT:    %[[SHOULDWRITE:.+]] = icmp ne i8 %[[MASKED]], %[[SHIFTED]]
+; CHECK-NEXT:    br i1 %[[SHOULDWRITE]], label %[[WRITE:.+]], label %[[SKIP:.+]], !prof
+; CHECK:       [[WRITE]]:
+; CHECK-NEXT:    atomicrmw or ptr %[[ADDR]], i8 %[[SHIFTED]] monotonic, align 1, !nosanitize
+;
+entry:
+  %A.addr = alloca i32, align 4
+  %mcdc.addr = alloca i32, align 4
+  call void @llvm.instrprof.cover(ptr @__profn_mcdc_tsan, i64 99278, i32 5, i32 0)
+  call void @llvm.instrprof.mcdc.parameters(ptr @__profn_mcdc_tsan, i64 99278, i32 1)
+  store i32 0, ptr %mcdc.addr, align 4
+  %0 = load i32, ptr %A.addr, align 4
+  %tobool = icmp ne i32 %0, 0
+  call void @llvm.instrprof.mcdc.tvbitmap.update(ptr @__profn_mcdc_tsan, i64 99278, i32 0, ptr %mcdc.addr)
+  ret void
+}
+
+define dso_local void @mcdc_no_tsan(i32 noundef %A) {
+; CHECK-LABEL: define dso_local void @mcdc_no_tsan(
+; NOCOND:        store i8 0, ptr @__profc_mcdc_no_tsan, align 1, !nosanitize
+; COND:          %pgocount = load i8, ptr @__profc_mcdc_no_tsan, align 1, !nosanitize
+; COND-NEXT:     %pgocount.ifnonzero = icmp ne i8 %pgocount, 0
+; COND-NEXT:     br i1 %pgocount.ifnonzero, label %[[COVER:.+]], label %[[CONT:.+]]
+; COND:        [[COVER]]:
+; COND-NEXT:     store i8 0, ptr @__profc_mcdc_no_tsan, align 1, !nosanitize
+; COND-NEXT:     br label %[[CONT]]
+; COND:        [[CONT]]:
+; CHECK:         %[[ADDR:.+]] = getelementptr inbounds i8, ptr @__profbm_mcdc_no_tsan, i32 %{{.+}}
+; CHECK:         %[[SHIFTED:.+]] = shl i8 1, %{{.+}}
+; CHECK-NEXT:    %[[BITS:.+]] = load i8, ptr %[[ADDR]], align 1, !nosanitize
+; CHECK-NEXT:    %[[OR:.+]] = or i8 %[[BITS]], %[[SHIFTED]]
+; CHECK-NEXT:    store i8 %[[OR]], ptr %[[ADDR]], align 1, !nosanitize
+;
+entry:
+  %A.addr = alloca i32, align 4
+  %mcdc.addr = alloca i32, align 4
+  call void @llvm.instrprof.cover(ptr @__profn_mcdc_no_tsan, i64 99278, i32 5, i32 0)
+  call void @llvm.instrprof.mcdc.parameters(ptr @__profn_mcdc_no_tsan, i64 99278, i32 1)
+  store i32 0, ptr %mcdc.addr, align 4
+  %0 = load i32, ptr %A.addr, align 4
+  %tobool = icmp ne i32 %0, 0
+  call void @llvm.instrprof.mcdc.tvbitmap.update(ptr @__profn_mcdc_no_tsan, i64 99278, i32 0, ptr %mcdc.addr)
+  ret void
+}

>From 82df70451314b323affff6a483c0043356033f62 Mon Sep 17 00:00:00 2001
From: Dan Blackwell <dan_blackwell at apple.com>
Date: Mon, 19 Oct 2026 09:59:58 -0700
Subject: [PATCH 07/10] Make test REQUIRES Clang

---
 compiler-rt/test/tsan/profile-counter-norace.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/test/tsan/profile-counter-norace.cpp b/compiler-rt/test/tsan/profile-counter-norace.cpp
index 4cddcaec069dcc..f2a8090324ae03 100644
--- a/compiler-rt/test/tsan/profile-counter-norace.cpp
+++ b/compiler-rt/test/tsan/profile-counter-norace.cpp
@@ -1,4 +1,4 @@
-// REQUIRES: profile
+// REQUIRES: Clang, profile
 // RUN: %clangxx_tsan -fprofile-instr-generate -O3 %s -o %t
 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
 

>From 769f49820a28c3d3b186f196b75eb4b280a2568a Mon Sep 17 00:00:00 2001
From: Dan Blackwell <dan_blackwell at apple.com>
Date: Tue, 3 Nov 2026 14:15:56 -0800
Subject: [PATCH 08/10] Warn when -fprofile-update=single is ignored under
 -fsanitize=thread

---
 clang/docs/UsersManual.md                     | 15 ++++++++-----
 .../clang/Basic/DiagnosticDriverKinds.td      |  4 ++++
 clang/lib/Driver/ToolChains/Clang.cpp         |  8 +++++++
 clang/test/Driver/fprofile-update.c           | 22 +++++++++++++++++++
 4 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/clang/docs/UsersManual.md b/clang/docs/UsersManual.md
index 96d1c4e009c71a..441ff756b3b65b 100644
--- a/clang/docs/UsersManual.md
+++ b/clang/docs/UsersManual.md
@@ -3693,11 +3693,16 @@ it reads from `pathname/default.profdata`.
 
 :::{option} -fprofile-update[=<method>]
 
-Unless `-fsanitize=thread` is specified, the default is `single`, which
-uses non-atomic increments. The counters can be inaccurate under thread
-contention. `atomic` uses atomic increments which is accurate but has
-overhead. `prefer-atomic` will be transformed to `atomic` when supported
-by the target, or `single` otherwise.
+The default is `single`, which uses non-atomic increments. The counters can
+be inaccurate under thread contention. `atomic` uses atomic increments which
+is accurate but has overhead. `prefer-atomic` will be transformed to `atomic`
+when supported by the target, or `single` otherwise.
+
+When `-fsanitize=thread` is used with `-fprofile-instr-generate`,
+`-fprofile-generate` or `-fcs-profile-generate`, counter updates in
+functions instrumented by ThreadSanitizer are always atomic. An explicit
+`-fprofile-update=single` is ignored for those functions, and Clang emits a
+warning.
 :::
 
 :::{option} -fprofile-continuous
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 79859e724a8179..11e517ebd0b70a 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -168,6 +168,10 @@ def warn_drv_unsupported_option_for_flang : Warning<
 def warn_drv_unsupported_diag_option_for_flang : Warning<
   "the warning option '-%0' is not supported">,
   InGroup<OptionIgnored>;
+def warn_drv_fprofile_update_single_ignored_tsan : Warning<
+  "'%0' is ignored for functions instrumented by '-fsanitize=thread'; profile "
+  "counter updates in those functions are atomic">,
+  InGroup<OptionIgnored>;
 def warn_drv_unsupported_option_for_processor : Warning<
   "ignoring '%0' option as it is not currently supported for processor '%1'">,
   InGroup<OptionIgnored>;
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index ffb225be471800..bcc2ebec0a78aa 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -607,6 +607,14 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
     else if (Val != "single")
       D.Diag(diag::err_drv_unsupported_option_argument)
           << A->getSpelling() << Val;
+    else if (SanArgs.needsTsanRt() &&
+             (PGOGenerateArg || CSPGOGenerateArg || ProfileGenerateArg)) {
+      // Non-atomic counter updates would be reported as data races, so the
+      // instrprof lowering forces atomic updates in TSan-instrumented
+      // functions.
+      D.Diag(diag::warn_drv_fprofile_update_single_ignored_tsan)
+          << A->getAsString(Args);
+    }
   }
   if (const auto *A = Args.getLastArg(options::OPT_fprofile_continuous)) {
     if (!PGOGenerateArg && !CSPGOGenerateArg && !ProfileGenerateArg)
diff --git a/clang/test/Driver/fprofile-update.c b/clang/test/Driver/fprofile-update.c
index c297b2b4931f88..d8423234baa813 100644
--- a/clang/test/Driver/fprofile-update.c
+++ b/clang/test/Driver/fprofile-update.c
@@ -13,6 +13,28 @@
 
 // ERROR: error: unsupported argument 'unknown' to option '-fprofile-update='
 
+/// Under -fsanitize=thread, instrprof counter updates are always atomic in
+/// TSan-instrumented functions, so an explicit -fprofile-update=single is
+/// ignored with a warning.
+// RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=TSAN-SINGLE
+// RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-generate -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=TSAN-SINGLE
+// RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fcs-profile-generate -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=TSAN-SINGLE
+// RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate -fprofile-update=atomic -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=TSAN-SINGLE
+// TSAN-SINGLE: warning: '-fprofile-update=single' is ignored for functions instrumented by '-fsanitize=thread'; profile counter updates in those functions are atomic [-Woption-ignored]
+// TSAN-SINGLE-NOT: "-fprofile-update=atomic"
+
+/// No warning without -fsanitize=thread
+// RUN: %clang -### %s -c --target=x86_64-linux-gnu -fprofile-instr-generate -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=NO-WARN
+
+/// No warning with -fsanitize=thread and non-single updates
+// RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate -fprofile-update=atomic 2>&1 | FileCheck %s --check-prefix=NO-WARN
+// RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate 2>&1 | FileCheck %s --check-prefix=NO-WARN
+
+/// No warning with -fsanitize=thread when no instrumentation is enabled.
+// RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=NO-WARN
+// RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate -fno-profile-instr-generate -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=NO-WARN
+// NO-WARN-NOT: warning: '-fprofile-update=single' is ignored
+
 // AIX specific tests
 // RUN: %clang -### %s --target=powerpc-unknown-aix -fprofile-generate -fprofile-update=atomic 2>&1 | FileCheck %s --check-prefix=AIX
 // RUN: %clang -### %s --target=powerpc-unknown-aix -fprofile-generate -fprofile-update=prefer-atomic 2>&1 | FileCheck %s --check-prefix=AIX

>From b90ab290c336d4dec1b685d8477ffb3498f254a7 Mon Sep 17 00:00:00 2001
From: Dan Blackwell <dan_blackwell at apple.com>
Date: Fri, 6 Nov 2026 14:44:08 -0800
Subject: [PATCH 09/10] Honor -fprofile-update=single under -fsanitize=thread
 and warn about counter races

---
 clang/docs/UsersManual.md                     |  6 +++---
 .../clang/Basic/DiagnosticDriverKinds.td      |  8 +++----
 clang/lib/Driver/ToolChains/Clang.cpp         | 21 +++++++++++--------
 clang/test/Driver/fprofile-update.c           | 15 +++++++------
 .../Instrumentation/InstrProfiling.cpp        |  4 ++--
 .../Instrumentation/InstrumentationOptions.td |  4 ++--
 .../InstrProfiling/tsan-compatibility.ll      | 14 +++++++++++++
 7 files changed, 46 insertions(+), 26 deletions(-)

diff --git a/clang/docs/UsersManual.md b/clang/docs/UsersManual.md
index 441ff756b3b65b..bf5011fafa7f74 100644
--- a/clang/docs/UsersManual.md
+++ b/clang/docs/UsersManual.md
@@ -3700,9 +3700,9 @@ when supported by the target, or `single` otherwise.
 
 When `-fsanitize=thread` is used with `-fprofile-instr-generate`,
 `-fprofile-generate` or `-fcs-profile-generate`, counter updates in
-functions instrumented by ThreadSanitizer are always atomic. An explicit
-`-fprofile-update=single` is ignored for those functions, and Clang emits a
-warning.
+functions instrumented by ThreadSanitizer default to `atomic`. Explicitly
+specifying `-fprofile-update=single` overrides this, and Clang emits a
+warning as non-atomic counter updates may be reported as data races.
 :::
 
 :::{option} -fprofile-continuous
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 11e517ebd0b70a..c3bb34a2e1e279 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -168,10 +168,10 @@ def warn_drv_unsupported_option_for_flang : Warning<
 def warn_drv_unsupported_diag_option_for_flang : Warning<
   "the warning option '-%0' is not supported">,
   InGroup<OptionIgnored>;
-def warn_drv_fprofile_update_single_ignored_tsan : Warning<
-  "'%0' is ignored for functions instrumented by '-fsanitize=thread'; profile "
-  "counter updates in those functions are atomic">,
-  InGroup<OptionIgnored>;
+def warn_drv_tsan_profile_update_single : Warning<
+  "'%0' with '-fsanitize=thread' may cause ThreadSanitizer to report data "
+  "races on profile counters">,
+  InGroup<DiagGroup<"tsan-profile-update">>;
 def warn_drv_unsupported_option_for_processor : Warning<
   "ignoring '%0' option as it is not currently supported for processor '%1'">,
   InGroup<OptionIgnored>;
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index bcc2ebec0a78aa..6b12b35a9237e0 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -604,17 +604,20 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
     StringRef Val = A->getValue();
     if (Val == "atomic" || Val == "prefer-atomic")
       CmdArgs.push_back("-fprofile-update=atomic");
-    else if (Val != "single")
+    else if (Val == "single") {
+      if (SanArgs.needsTsanRt() &&
+          (PGOGenerateArg || CSPGOGenerateArg || ProfileGenerateArg)) {
+        // Under TSan, instrprof defaults to atomic counter updates to avoid
+        // data race reports on profile counters; override that default and
+        // warn.
+        D.Diag(diag::warn_drv_tsan_profile_update_single)
+            << A->getAsString(Args);
+        CmdArgs.push_back("-mllvm");
+        CmdArgs.push_back("-instrprof-atomic-counter-update-all=false");
+      }
+    } else
       D.Diag(diag::err_drv_unsupported_option_argument)
           << A->getSpelling() << Val;
-    else if (SanArgs.needsTsanRt() &&
-             (PGOGenerateArg || CSPGOGenerateArg || ProfileGenerateArg)) {
-      // Non-atomic counter updates would be reported as data races, so the
-      // instrprof lowering forces atomic updates in TSan-instrumented
-      // functions.
-      D.Diag(diag::warn_drv_fprofile_update_single_ignored_tsan)
-          << A->getAsString(Args);
-    }
   }
   if (const auto *A = Args.getLastArg(options::OPT_fprofile_continuous)) {
     if (!PGOGenerateArg && !CSPGOGenerateArg && !ProfileGenerateArg)
diff --git a/clang/test/Driver/fprofile-update.c b/clang/test/Driver/fprofile-update.c
index d8423234baa813..97248c9475a764 100644
--- a/clang/test/Driver/fprofile-update.c
+++ b/clang/test/Driver/fprofile-update.c
@@ -13,27 +13,30 @@
 
 // ERROR: error: unsupported argument 'unknown' to option '-fprofile-update='
 
-/// Under -fsanitize=thread, instrprof counter updates are always atomic in
-/// TSan-instrumented functions, so an explicit -fprofile-update=single is
-/// ignored with a warning.
+/// Under -fsanitize=thread, instrprof counter updates default to atomic in
+/// TSan-instrumented functions. An explicit -fprofile-update=single overrides
+/// that default with a warning.
 // RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=TSAN-SINGLE
 // RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-generate -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=TSAN-SINGLE
 // RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fcs-profile-generate -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=TSAN-SINGLE
 // RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate -fprofile-update=atomic -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=TSAN-SINGLE
-// TSAN-SINGLE: warning: '-fprofile-update=single' is ignored for functions instrumented by '-fsanitize=thread'; profile counter updates in those functions are atomic [-Woption-ignored]
-// TSAN-SINGLE-NOT: "-fprofile-update=atomic"
+// TSAN-SINGLE: warning: '-fprofile-update=single' with '-fsanitize=thread' may cause ThreadSanitizer to report data races on profile counters [-Wtsan-profile-update]
+// TSAN-SINGLE: "-mllvm" "-instrprof-atomic-counter-update-all=false"
 
 /// No warning without -fsanitize=thread
 // RUN: %clang -### %s -c --target=x86_64-linux-gnu -fprofile-instr-generate -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=NO-WARN
 
 /// No warning with -fsanitize=thread and non-single updates
 // RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate -fprofile-update=atomic 2>&1 | FileCheck %s --check-prefix=NO-WARN
+// RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate -fprofile-update=prefer-atomic 2>&1 | FileCheck %s --check-prefix=NO-WARN
+// RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate -fprofile-update=single -fprofile-update=atomic 2>&1 | FileCheck %s --check-prefix=NO-WARN
 // RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate 2>&1 | FileCheck %s --check-prefix=NO-WARN
 
 /// No warning with -fsanitize=thread when no instrumentation is enabled.
 // RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=NO-WARN
 // RUN: %clang -### %s -c --target=x86_64-linux-gnu -fsanitize=thread -fprofile-instr-generate -fno-profile-instr-generate -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=NO-WARN
-// NO-WARN-NOT: warning: '-fprofile-update=single' is ignored
+// NO-WARN-NOT: warning: '-fprofile-update=single'
+// NO-WARN-NOT: "-instrprof-atomic-counter-update-all
 
 // AIX specific tests
 // RUN: %clang -### %s --target=powerpc-unknown-aix -fprofile-generate -fprofile-update=atomic 2>&1 | FileCheck %s --check-prefix=AIX
diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
index c87ed585e72ce0..f9acc4c1c9c171 100644
--- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -838,8 +838,8 @@ bool InstrLowerer::isCounterPromotionEnabled() const {
 bool InstrLowerer::isAtomic(const Function &F) const {
   // Non-atomic counter updates are data races, which ThreadSanitizer would
   // report. Use atomic updates in functions that TSan instruments.
-  return Options.Atomic || Opts.instrprof_atomic_counter_update_all ||
-         F.hasFnAttribute(Attribute::SanitizeThread);
+  return valueOr(Opts.instrprof_atomic_counter_update_all,
+                 Options.Atomic || F.hasFnAttribute(Attribute::SanitizeThread));
 }
 
 static void doAtomicCheck(Function *F) {
diff --git a/llvm/lib/Transforms/Instrumentation/InstrumentationOptions.td b/llvm/lib/Transforms/Instrumentation/InstrumentationOptions.td
index 44a2c46ed3ec43..8d16ca5533ec18 100644
--- a/llvm/lib/Transforms/Instrumentation/InstrumentationOptions.td
+++ b/llvm/lib/Transforms/Instrumentation/InstrumentationOptions.td
@@ -388,8 +388,8 @@ defm : OptionalBoolField<"do-counter-promotion",
   "Do counter register promotion">;
 defm : BoolField<"hash-based-counter-split", "true",
   "Rename counter variable of a comdat function based on cfg hash">;
-defm : BoolField<"instrprof-atomic-counter-update-all", "false",
-  "Make all profile counter updates atomic (for testing only)">;
+defm : OptionalBoolField<"instrprof-atomic-counter-update-all",
+  "Override whether all profile counter updates are atomic">;
 defm : BoolField<"iterative-counter-promotion", "true",
   "Allow counter promotion across the whole loop nest">;
 // A debug option.
diff --git a/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll b/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
index a97b633671d0df..d11cd2a451d321 100644
--- a/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
+++ b/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
@@ -5,6 +5,7 @@
 ; RUN: opt < %s -passes=instrprof -S | FileCheck %s --check-prefixes=CHECK,NOCOND
 ; RUN: opt < %s -passes=instrprof -conditional-counter-update -S | FileCheck %s --check-prefixes=CHECK,COND
 ; RUN: opt < %s -passes=instrprof -do-counter-promotion -S | FileCheck %s --check-prefix=PROMO
+; RUN: opt < %s -passes=instrprof -instrprof-atomic-counter-update-all=false -S | FileCheck %s --check-prefix=SINGLE
 
 target triple = "aarch64-unknown-linux-gnu"
 
@@ -19,6 +20,12 @@ define void @straight_line_tsan() sanitize_thread {
 ; CHECK-LABEL: define void @straight_line_tsan(
 ; CHECK-NEXT:    atomicrmw add ptr @__profc_straight_line_tsan, i64 1 monotonic, align 8, !nosanitize
 ; CHECK-NEXT:    ret void
+;
+; SINGLE-LABEL: define void @straight_line_tsan(
+; SINGLE-NEXT:    %[[LOAD:.+]] = load i64, ptr @__profc_straight_line_tsan, align 8, !nosanitize
+; SINGLE-NEXT:    %[[ADD:.+]] = add i64 %[[LOAD]], 1
+; SINGLE-NEXT:    store i64 %[[ADD]], ptr @__profc_straight_line_tsan, align 8, !nosanitize
+; SINGLE-NEXT:    ret void
 ;
   call void @llvm.instrprof.increment(ptr @__profn_straight_line_tsan, i64 0, i32 1, i32 0)
   ret void
@@ -125,6 +132,13 @@ define dso_local void @mcdc_tsan(i32 noundef %A) sanitize_thread {
 ; CHECK:       [[WRITE]]:
 ; CHECK-NEXT:    atomicrmw or ptr %[[ADDR]], i8 %[[SHIFTED]] monotonic, align 1, !nosanitize
 ;
+; SINGLE-LABEL: define dso_local void @mcdc_tsan(
+; SINGLE:         %[[ADDR:.+]] = getelementptr inbounds i8, ptr @__profbm_mcdc_tsan, i32 %{{.+}}
+; SINGLE:         %[[SHIFTED:.+]] = shl i8 1, %{{.+}}
+; SINGLE-NEXT:    %[[BITS:.+]] = load i8, ptr %[[ADDR]], align 1, !nosanitize
+; SINGLE-NEXT:    %[[OR:.+]] = or i8 %[[BITS]], %[[SHIFTED]]
+; SINGLE-NEXT:    store i8 %[[OR]], ptr %[[ADDR]], align 1, !nosanitize
+;
 entry:
   %A.addr = alloca i32, align 4
   %mcdc.addr = alloca i32, align 4

>From c13bdfddaa63385efdcb6690a8cc552bd5a5612a Mon Sep 17 00:00:00 2001
From: Dan Blackwell <dan_blackwell at apple.com>
Date: Mon, 9 Nov 2026 09:46:44 -0800
Subject: [PATCH 10/10] Make mcdc test use i64

---
 .../Instrumentation/InstrProfiling/tsan-compatibility.ll    | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll b/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
index d11cd2a451d321..279f4e60b331f4 100644
--- a/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
+++ b/llvm/test/Instrumentation/InstrProfiling/tsan-compatibility.ll
@@ -123,7 +123,7 @@ define dso_local void @mcdc_tsan(i32 noundef %A) sanitize_thread {
 ; COND-NEXT:     store atomic i8 0, ptr @__profc_mcdc_tsan monotonic, align 1, !nosanitize
 ; COND-NEXT:     br label %[[CONT]]
 ; COND:        [[CONT]]:
-; CHECK:         %[[ADDR:.+]] = getelementptr inbounds i8, ptr @__profbm_mcdc_tsan, i32 %{{.+}}
+; CHECK:         %[[ADDR:.+]] = getelementptr inbounds i8, ptr @__profbm_mcdc_tsan, i64 %{{.+}}
 ; CHECK:         %[[SHIFTED:.+]] = shl i8 1, %{{.+}}
 ; CHECK-NEXT:    %[[BITS:.+]] = load atomic i8, ptr %[[ADDR]] monotonic, align 1, !nosanitize
 ; CHECK-NEXT:    %[[MASKED:.+]] = and i8 %[[BITS]], %[[SHIFTED]]
@@ -133,7 +133,7 @@ define dso_local void @mcdc_tsan(i32 noundef %A) sanitize_thread {
 ; CHECK-NEXT:    atomicrmw or ptr %[[ADDR]], i8 %[[SHIFTED]] monotonic, align 1, !nosanitize
 ;
 ; SINGLE-LABEL: define dso_local void @mcdc_tsan(
-; SINGLE:         %[[ADDR:.+]] = getelementptr inbounds i8, ptr @__profbm_mcdc_tsan, i32 %{{.+}}
+; SINGLE:         %[[ADDR:.+]] = getelementptr inbounds i8, ptr @__profbm_mcdc_tsan, i64 %{{.+}}
 ; SINGLE:         %[[SHIFTED:.+]] = shl i8 1, %{{.+}}
 ; SINGLE-NEXT:    %[[BITS:.+]] = load i8, ptr %[[ADDR]], align 1, !nosanitize
 ; SINGLE-NEXT:    %[[OR:.+]] = or i8 %[[BITS]], %[[SHIFTED]]
@@ -161,7 +161,7 @@ define dso_local void @mcdc_no_tsan(i32 noundef %A) {
 ; COND-NEXT:     store i8 0, ptr @__profc_mcdc_no_tsan, align 1, !nosanitize
 ; COND-NEXT:     br label %[[CONT]]
 ; COND:        [[CONT]]:
-; CHECK:         %[[ADDR:.+]] = getelementptr inbounds i8, ptr @__profbm_mcdc_no_tsan, i32 %{{.+}}
+; CHECK:         %[[ADDR:.+]] = getelementptr inbounds i8, ptr @__profbm_mcdc_no_tsan, i64 %{{.+}}
 ; CHECK:         %[[SHIFTED:.+]] = shl i8 1, %{{.+}}
 ; CHECK-NEXT:    %[[BITS:.+]] = load i8, ptr %[[ADDR]], align 1, !nosanitize
 ; CHECK-NEXT:    %[[OR:.+]] = or i8 %[[BITS]], %[[SHIFTED]]



More information about the cfe-commits mailing list