[llvm] [AMDGPU] Fix a potential use-after-erase in `AMDGPUPromoteAlloca` pass (PR #174529)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 8 06:50:05 PST 2026
https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/174529
>From 5b1a0326b500c7f97b9362f4b2206cca21e132cb Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Tue, 6 Jan 2026 01:18:24 -0500
Subject: [PATCH 1/3] [AMDGPU] Fix a potential use-after-erase in
`AMDGPUPromoteAlloca` pass
In some cases, the placeholder itself can be used as the value for its corresponding block in `SSAUpdater`, and later used as an incoming value in another block in `GetValueInMiddleOfBlock`. If we erase it too early, this can lead to a use-after-erase. The tricky part is that it may not trigger any error right away, but can cause weird and completely unrelated issues later in the pipeline.
---
.../lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp | 13 +++++--
.../AMDGPU/promote-alloca-use-after-erase.ll | 34 +++++++++++++++++++
2 files changed, 45 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp b/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
index d0bc36c2f8501..bf2485e23ae46 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
@@ -1166,9 +1166,18 @@ void AMDGPUPromoteAllocaImpl::promoteAllocaToVector(AllocaAnalysis &AA) {
});
// Now fixup the placeholders.
+ DenseMap<Value *, Value *> PlaceholderMap;
for (Instruction *Placeholder : Placeholders) {
- Placeholder->replaceAllUsesWith(
- Updater.GetValueInMiddleOfBlock(Placeholder->getParent()));
+ Value *NewVal = Updater.GetValueInMiddleOfBlock(Placeholder->getParent());
+ Placeholder->replaceAllUsesWith(NewVal);
+ PlaceholderMap[Placeholder] = NewVal;
+ }
+ // Note: we cannot merge this loop with the previous one because it is
+ // possible that the placeholder itself can be used in the SSAUpdater. The
+ // replaceAllUsesWith doesn't replace those uses.
+ for (Instruction *Placeholder : Placeholders) {
+ if (!Placeholder->use_empty())
+ Placeholder->replaceAllUsesWith(PlaceholderMap[Placeholder]);
Placeholder->eraseFromParent();
}
diff --git a/llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll b/llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll
new file mode 100644
index 0000000000000..0de8daa56027c
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll
@@ -0,0 +1,34 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes=amdgpu-promote-alloca %s | FileCheck %s
+
+define amdgpu_kernel void @kernel(ptr readonly captures(none) %p) local_unnamed_addr {
+; CHECK-LABEL: define amdgpu_kernel void @kernel(
+; CHECK-SAME: ptr readonly captures(none) [[P:%.*]]) local_unnamed_addr {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: [[ASCAST:%.*]] = addrspacecast ptr [[P]] to ptr addrspace(1)
+; CHECK-NEXT: [[ALLOCA:%.*]] = freeze <14 x i32> poison
+; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr addrspace(1) [[ASCAST]], align 4
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[LOAD]], 0
+; CHECK-NEXT: br i1 [[CMP]], label %[[BB_1:.*]], label %[[BB_2:.*]]
+; CHECK: [[BB_2]]:
+; CHECK-NEXT: br label %[[BB_1]]
+; CHECK: [[BB_1]]:
+; CHECK-NEXT: [[PROMOTEALLOCA:%.*]] = phi <14 x i32> [ [[ALLOCA]], %[[BB_2]] ], [ [[ALLOCA]], %[[ENTRY]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = insertelement <14 x i32> [[PROMOTEALLOCA]], i32 0, i32 0
+; CHECK-NEXT: ret void
+;
+entry:
+ %ascast = addrspacecast ptr %p to ptr addrspace(1)
+ %alloca = alloca [14 x i32], addrspace(5)
+ %load = load i32, ptr addrspace(1) %ascast
+ %cmp = icmp eq i32 %load, 0
+ br i1 %cmp, label %bb.1, label %bb.2
+
+bb.2:
+ store i32 poison, ptr addrspace(5) %alloca
+ br label %bb.1
+
+bb.1:
+ store i32 0, ptr addrspace(5) %alloca
+ ret void
+}
>From 1677b3e4a97fc320cbbaf62180cf38a23d2d6f2f Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Tue, 6 Jan 2026 09:20:03 -0500
Subject: [PATCH 2/3] fix comments
---
llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp | 10 +++++-----
.../CodeGen/AMDGPU/promote-alloca-use-after-erase.ll | 4 ++--
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp b/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
index bf2485e23ae46..e01d6f9e87194 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
@@ -1166,18 +1166,18 @@ void AMDGPUPromoteAllocaImpl::promoteAllocaToVector(AllocaAnalysis &AA) {
});
// Now fixup the placeholders.
- DenseMap<Value *, Value *> PlaceholderMap;
- for (Instruction *Placeholder : Placeholders) {
+ SmallVector<WeakVH> PlaceholderToNewVal(Placeholders.size());
+ for (auto [Index, Placeholder] : enumerate(Placeholders)) {
Value *NewVal = Updater.GetValueInMiddleOfBlock(Placeholder->getParent());
+ PlaceholderToNewVal[Index] = NewVal;
Placeholder->replaceAllUsesWith(NewVal);
- PlaceholderMap[Placeholder] = NewVal;
}
// Note: we cannot merge this loop with the previous one because it is
// possible that the placeholder itself can be used in the SSAUpdater. The
// replaceAllUsesWith doesn't replace those uses.
- for (Instruction *Placeholder : Placeholders) {
+ for (auto [Index, Placeholder] : enumerate(Placeholders)) {
if (!Placeholder->use_empty())
- Placeholder->replaceAllUsesWith(PlaceholderMap[Placeholder]);
+ Placeholder->replaceAllUsesWith(PlaceholderToNewVal[Index]);
Placeholder->eraseFromParent();
}
diff --git a/llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll b/llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll
index 0de8daa56027c..257e037f48042 100644
--- a/llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll
+++ b/llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll
@@ -1,9 +1,9 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes=amdgpu-promote-alloca %s | FileCheck %s
-define amdgpu_kernel void @kernel(ptr readonly captures(none) %p) local_unnamed_addr {
+define amdgpu_kernel void @kernel(ptr %p) {
; CHECK-LABEL: define amdgpu_kernel void @kernel(
-; CHECK-SAME: ptr readonly captures(none) [[P:%.*]]) local_unnamed_addr {
+; CHECK-SAME: ptr [[P:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*]]:
; CHECK-NEXT: [[ASCAST:%.*]] = addrspacecast ptr [[P]] to ptr addrspace(1)
; CHECK-NEXT: [[ALLOCA:%.*]] = freeze <14 x i32> poison
>From 5c324b50a4298aad2534ed1b30d5ca1c12ffb0db Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Tue, 6 Jan 2026 14:07:24 -0500
Subject: [PATCH 3/3] use `Value *` instead of useless `WeakVH`
---
llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp | 2 +-
llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll | 2 ++
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp b/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
index e01d6f9e87194..fc72b915faf54 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
@@ -1166,7 +1166,7 @@ void AMDGPUPromoteAllocaImpl::promoteAllocaToVector(AllocaAnalysis &AA) {
});
// Now fixup the placeholders.
- SmallVector<WeakVH> PlaceholderToNewVal(Placeholders.size());
+ SmallVector<Value *> PlaceholderToNewVal(Placeholders.size());
for (auto [Index, Placeholder] : enumerate(Placeholders)) {
Value *NewVal = Updater.GetValueInMiddleOfBlock(Placeholder->getParent());
PlaceholderToNewVal[Index] = NewVal;
diff --git a/llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll b/llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll
index 257e037f48042..8d6cee48b416c 100644
--- a/llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll
+++ b/llvm/test/CodeGen/AMDGPU/promote-alloca-use-after-erase.ll
@@ -31,4 +31,6 @@ bb.2:
bb.1:
store i32 0, ptr addrspace(5) %alloca
ret void
+
+uselistorder ptr addrspace(5) %alloca, { 1, 0 }
}
More information about the llvm-commits
mailing list