[llvm] [AMDGPU][LowerBufferFatPointers] Fix crash with `select false` (PR #166471)

Krzysztof Drewniak via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 5 10:03:55 PST 2025


https://github.com/krzysz00 updated https://github.com/llvm/llvm-project/pull/166471

>From f8334336042f68777db4f6c98df89430ad6a18c8 Mon Sep 17 00:00:00 2001
From: Krzysztof Drewniak <Krzysztof.Drewniak at amd.com>
Date: Tue, 4 Nov 2025 23:53:37 +0000
Subject: [PATCH 1/2] [AMDGPU][LowerBufferFatPointers] Fix crash with `select
 false`

If the input to LowerBufferFatPointers is such that the resource- and
offset-specific `select` instructions generated for a `select` on `ptr
addrspae(7)` fold away, the pass would crash when trying to replace an
instruction with itself. This commit resolves the issue.

Fixes https://github.com/iree-org/iree/issues/22551

While I'm here, make myself a code owner for buffer fat pointer ling.
---
 .github/CODEOWNERS                                   |  3 +++
 .../Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp   |  7 +++++--
 .../AMDGPU/lower-buffer-fat-pointers-control-flow.ll | 12 ++++++++++++
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
index 3a0a291ccb24c..3f2657a707860 100644
--- a/.github/CODEOWNERS
+++ b/.github/CODEOWNERS
@@ -166,5 +166,8 @@
 /llvm/**/dsymutil/ @JDevlieghere
 /llvm/**/llvm-dwarfutil/ @JDevlieghere
 
+# AMDGPULowerBufferFatPointers
+/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp @krzysz00
+
 # libclang/Python bindings
 /clang/bindings/python @DeinAlptraum
diff --git a/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp b/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
index 0a5913293238a..fdff21b6ef8df 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
@@ -1565,8 +1565,11 @@ void SplitPtrStructs::processConditionals() {
     } else if (isa<SelectInst>(I)) {
       if (MaybeRsrc) {
         if (auto *RsrcInst = dyn_cast<Instruction>(Rsrc)) {
-          ConditionalTemps.push_back(RsrcInst);
-          RsrcInst->replaceAllUsesWith(*MaybeRsrc);
+          // Guard against conditionals that were already folded away.
+          if (RsrcInst != *MaybeRsrc) {
+            ConditionalTemps.push_back(RsrcInst);
+            RsrcInst->replaceAllUsesWith(*MaybeRsrc);
+          }
         }
         for (Value *V : Seen)
           FoundRsrcs[V] = *MaybeRsrc;
diff --git a/llvm/test/CodeGen/AMDGPU/lower-buffer-fat-pointers-control-flow.ll b/llvm/test/CodeGen/AMDGPU/lower-buffer-fat-pointers-control-flow.ll
index 4fa7c29bfde02..71005224dd1e5 100644
--- a/llvm/test/CodeGen/AMDGPU/lower-buffer-fat-pointers-control-flow.ll
+++ b/llvm/test/CodeGen/AMDGPU/lower-buffer-fat-pointers-control-flow.ll
@@ -481,3 +481,15 @@ define void @dominance_not_in_program_order(ptr addrspace(7) inreg %arg) {
   %lsr.iv11 = phi ptr addrspace(7) [ %arg, %.loopexit ], [ %arg, %.preheader15 ]
   br label %.loopexit
 }
+
+;; iree-org/iree#22551 - crash on something that reduces to the below non-canonical select.
+define ptr addrspace(7) @noncanonical_const_cond(ptr addrspace(7) %x) {
+; CHECK-LABEL: define { ptr addrspace(8), i32 } @noncanonical_const_cond
+; CHECK-SAME: ({ ptr addrspace(8), i32 } [[RET:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[X_RSRC:%.*]] = extractvalue { ptr addrspace(8), i32 } [[RET]], 0
+; CHECK-NEXT:    [[X_OFF:%.*]] = extractvalue { ptr addrspace(8), i32 } [[RET]], 1
+; CHECK-NEXT:    ret { ptr addrspace(8), i32 } [[RET]]
+;
+  %ret = select i1 false, ptr addrspace(7) %x, ptr addrspace(7) %x
+  ret ptr addrspace(7) %ret
+}

>From e81e44deced6e60e00a9657abca9be7218dcaa23 Mon Sep 17 00:00:00 2001
From: Krzysztof Drewniak <Krzysztof.Drewniak at amd.com>
Date: Wed, 5 Nov 2025 18:03:42 +0000
Subject: [PATCH 2/2] Remove codewoners change

---
 .github/CODEOWNERS | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
index 3f2657a707860..3a0a291ccb24c 100644
--- a/.github/CODEOWNERS
+++ b/.github/CODEOWNERS
@@ -166,8 +166,5 @@
 /llvm/**/dsymutil/ @JDevlieghere
 /llvm/**/llvm-dwarfutil/ @JDevlieghere
 
-# AMDGPULowerBufferFatPointers
-/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp @krzysz00
-
 # libclang/Python bindings
 /clang/bindings/python @DeinAlptraum



More information about the llvm-commits mailing list