[llvm] [TSan] Fix missing inst cleanup (PR #144067)

Kunqiu Chen via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 13 08:00:52 PDT 2025


https://github.com/Camsyn updated https://github.com/llvm/llvm-project/pull/144067

>From 6d8a0390559ab41373d1269efad927c84153a275 Mon Sep 17 00:00:00 2001
From: Camsyn <camsyn at foxmail.com>
Date: Fri, 13 Jun 2025 19:01:43 +0800
Subject: [PATCH] [TSan] Fix missing Inst cleanup

Commit 44e875a introduced a change that replaces `ReplaceInstWithInst`
with `Instruction::replaceAllUsesWith`, without subsequent instruction
cleanup.

This results in TSan leaving behind useless `load atomic` instructions
after 'replacing' them.

This commit adds cleanup back, consistent with the context.
---
 llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp        | 1 +
 .../test/Instrumentation/ThreadSanitizer/atomic-non-integer.ll | 3 ---
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
index ec9f78edfeb1c..195a157ee20af 100644
--- a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
@@ -729,6 +729,7 @@ bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) {
     Value *C = IRB.CreateCall(TsanAtomicLoad[Idx], Args);
     Value *Cast = IRB.CreateBitOrPointerCast(C, OrigTy);
     I->replaceAllUsesWith(Cast);
+    I->eraseFromParent();
   } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
     Value *Addr = SI->getPointerOperand();
     int Idx =
diff --git a/llvm/test/Instrumentation/ThreadSanitizer/atomic-non-integer.ll b/llvm/test/Instrumentation/ThreadSanitizer/atomic-non-integer.ll
index 8bcabaecf0fdc..015ee2fe711e1 100644
--- a/llvm/test/Instrumentation/ThreadSanitizer/atomic-non-integer.ll
+++ b/llvm/test/Instrumentation/ThreadSanitizer/atomic-non-integer.ll
@@ -10,7 +10,6 @@ define float @load_float(ptr %fptr) {
 ; CHECK-NEXT:    call void @__tsan_func_entry(ptr [[TMP1]])
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @__tsan_atomic32_load(ptr [[FPTR]], i32 0)
 ; CHECK-NEXT:    [[TMP3:%.*]] = bitcast i32 [[TMP2]] to float
-; CHECK-NEXT:    [[V:%.*]] = load atomic float, ptr [[FPTR]] unordered, align 4
 ; CHECK-NEXT:    call void @__tsan_func_exit()
 ; CHECK-NEXT:    ret float [[TMP3]]
 ;
@@ -25,7 +24,6 @@ define double @load_double(ptr %fptr) {
 ; CHECK-NEXT:    call void @__tsan_func_entry(ptr [[TMP1]])
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i64 @__tsan_atomic64_load(ptr [[FPTR]], i32 0)
 ; CHECK-NEXT:    [[TMP3:%.*]] = bitcast i64 [[TMP2]] to double
-; CHECK-NEXT:    [[V:%.*]] = load atomic double, ptr [[FPTR]] unordered, align 8
 ; CHECK-NEXT:    call void @__tsan_func_exit()
 ; CHECK-NEXT:    ret double [[TMP3]]
 ;
@@ -40,7 +38,6 @@ define fp128 @load_fp128(ptr %fptr) {
 ; CHECK-NEXT:    call void @__tsan_func_entry(ptr [[TMP1]])
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i128 @__tsan_atomic128_load(ptr [[FPTR]], i32 0)
 ; CHECK-NEXT:    [[TMP3:%.*]] = bitcast i128 [[TMP2]] to fp128
-; CHECK-NEXT:    [[V:%.*]] = load atomic fp128, ptr [[FPTR]] unordered, align 16
 ; CHECK-NEXT:    call void @__tsan_func_exit()
 ; CHECK-NEXT:    ret fp128 [[TMP3]]
 ;



More information about the llvm-commits mailing list