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

Kunqiu Chen via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 13 05:48:19 PDT 2025


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

Commit 44e875ad5b2ce26826dd53f9e7d1a71436c86212 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.

>From 7ea8daf0f5a8baf159887853c75b1da4b06c21d0 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 +
 1 file changed, 1 insertion(+)

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 =



More information about the llvm-commits mailing list