[llvm] [AMDGPU] Fix a potential use-after-erase in `AMDGPUPromoteAlloca` pass (PR #174529)

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 7 20:58:29 PST 2026


================
@@ -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
----------------
shiltian wrote:

So this depends on the order of the two `store`s. In my `opt` run, it was:

```
  store i32 0, ptr addrspace(5) %0, align 4 // 1
  store i32 undef, ptr addrspace(5) %0, align 4 // 2
```

Then in a run in the pipeline, it was:

```
  store i32 undef, ptr addrspace(5) %1, align 4 // 2
  store i32 0, ptr addrspace(5) %1, align 4 // 1
```

Basically, it depends on the order of the iteration of all uses of the `alloca`.

FWIW, the following is what the crash looks like:

```
(gdb) r
Starting program: /home/nfs/build/llvm/debug/bin/clang-22 -cc1 -triple amdgcn-amd-amdhsa -aux-triple x86_64-unknown-linux-gnu -O2 -Werror=atomic-alignment -S -save-temps=cwd -disable-free -clear-ast-before-backend -main-file-name rocrefact_csrrf_splitlu.cpp -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fno-rounding-math -mconstructor-aliases -aux-target-cpu x86-64 -fcuda-is-device -fno-threadsafe-statics -mllvm -amdgpu-internalize-symbols --gpu-max-threads-per-block=1024 -fcuda-allow-variadic-functions -mlink-builtin-bitcode /opt/rocm/amdgcn/bitcode/ocml.bc -mlink-builtin-bitcode /opt/rocm/amdgcn/bitcode/ockl.bc -mlink-builtin-bitcode /opt/rocm/amdgcn/bitcode/oclc_daz_opt_off.bc -mlink-builtin-bitcode /opt/rocm/amdgcn/bitcode/oclc_unsafe_math_off.bc -mlink-builtin-bitcode /opt/rocm/amdgcn/bitcode/oclc_finite_only_off.bc -mlink-builtin-bitcode /opt/rocm/amdgcn/bitcode/oclc_correctly_rounded_sqrt_on.bc -mlink-builtin-bitcode /opt/rocm/amdgcn/bitcode/oclc_wavefrontsize64_off.bc -mlink-builtin-bitcode /opt/rocm/amdgcn/bitcode/oclc_isa_version_1250.bc -mlink-builtin-bitcode /opt/rocm/amdgcn/bitcode/oclc_abi_version_600.bc -target-cpu gfx1250 -debug-info-kind=constructor -dwarf-version=5 -debugger-tuning=gdb --compress-debug-sections=zlib -fdebug-compilation-dir=/home/shiltian/Documents/tmp/rocsolver-failure/build -fcoverage-compilation-dir=/home/shiltian/Documents/tmp/rocsolver-failure/build -resource-dir /home/nfs/build/llvm/debug/lib/clang/22 -Wno-pass-failed -std=c++17 -fno-autolink -ferror-limit 19 -fvisibility=hidden -fvisibility-inlines-hidden -fhip-new-launch-api -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -vectorize-loops -vectorize-slp -mllvm -amdgpu-early-inline-all=true -mllvm -amdgpu-function-calls=false -cuid=2fb59a26a12b0ef7 -fcuda-allow-variadic-functions -faddrsig -fdwarf2-cfi-asm -o rocrefact_csrrf_splitlu-hip-amdgcn-amd-amdhsa-gfx1250.s -x ir reduced.ll
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
warning: '--gpu-max-threads-per-block=1024' is ignored since it is only supported for HIP
warning: overriding the module target triple with amdgcn-amd-amdhsa [-Woverride-module]
  store i32 undef, ptr addrspace(5) %1, align 4
  store i32 0, ptr addrspace(5) %1, align 4
clang-22: /home/shiltian/Documents/vscode/llvm-project/llvm/lib/Analysis/LazyValueInfo.cpp:726: std::optional<ValueLatticeElement> llvm::LazyValueInfoImpl::solveBlockValueNonLocal(Value *, BasicBlock *): Assertion `isa<Argument>(Val) && "Unknown live-in to the entry block"' failed.

Program received signal SIGABRT, Aborted.
0x00007fffe909eb2c in pthread_kill () from /lib/x86_64-linux-gnu/libc.so.6
(gdb)
```

https://github.com/llvm/llvm-project/pull/174529


More information about the llvm-commits mailing list