[llvm] [AMDGPU] Poison invalid globals after emitting error in LowerBufferFatPointers pass (PR #184662)

Arseniy Obolenskiy via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 4 11:05:53 PST 2026


https://github.com/aobolensk updated https://github.com/llvm/llvm-project/pull/184662

>From 33c035104df6c10c152b936622cf7e42e5b5524d Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Wed, 4 Mar 2026 19:49:31 +0100
Subject: [PATCH 1/2] [AMDGPU] Poison invalid globals after emitting error in
 LowerBufferFatPointers pass

After the change from `report_fatal_error` to `Ctx.emitError` in #142014 there is a necessity to remove unsupported globals. Otherwise there is a secondary crash during ISel when processing them

Fixes SWDEV-511241
---
 .../Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp    | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp b/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
index 05e97d2fc7508..f0720147937f2 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
@@ -2467,11 +2467,13 @@ bool AMDGPULowerBufferFatPointers::run(Module &M, const TargetMachine &TM) {
 
   BufferFatPtrToStructTypeMap StructTM(DL);
   BufferFatPtrToIntTypeMap IntTM(DL);
-  for (const GlobalVariable &GV : M.globals()) {
+  SmallVector<GlobalVariable *, 4> InvalidGlobals;
+  for (GlobalVariable &GV : M.globals()) {
     if (GV.getAddressSpace() == AMDGPUAS::BUFFER_FAT_POINTER) {
       // FIXME: Use DiagnosticInfo unsupported but it requires a Function
       Ctx.emitError("global variables with a buffer fat pointer address "
                     "space (7) are not supported");
+      InvalidGlobals.push_back(&GV);
       continue;
     }
 
@@ -2481,10 +2483,17 @@ bool AMDGPULowerBufferFatPointers::run(Module &M, const TargetMachine &TM) {
       Ctx.emitError("global variables that contain buffer fat pointers "
                     "(address space 7 pointers) are unsupported. Use "
                     "buffer resource pointers (address space 8) instead");
+      InvalidGlobals.push_back(&GV);
       continue;
     }
   }
 
+  for (GlobalVariable *GV : InvalidGlobals) {
+    GV->replaceAllUsesWith(PoisonValue::get(GV->getType()));
+    GV->eraseFromParent();
+    Changed = true;
+  }
+
   {
     // Collect all constant exprs and aggregates referenced by any function.
     SmallVector<Constant *, 8> Worklist;

>From 0e25d45b1429eec52b1f3875487a96b744e45c3e Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Wed, 4 Mar 2026 20:05:21 +0100
Subject: [PATCH 2/2] Address review comments

---
 .../AMDGPU/AMDGPULowerBufferFatPointers.cpp   | 21 ++++++++++---------
 .../buffer-fat-pointer-unsupported-errors.ll  | 12 +++++++++++
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp b/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
index f0720147937f2..5fa6aaba8a99e 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
@@ -223,6 +223,7 @@
 #include "GCNSubtarget.h"
 #include "SIDefines.h"
 #include "llvm/ADT/SetOperations.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Analysis/InstSimplifyFolder.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
@@ -2467,13 +2468,19 @@ bool AMDGPULowerBufferFatPointers::run(Module &M, const TargetMachine &TM) {
 
   BufferFatPtrToStructTypeMap StructTM(DL);
   BufferFatPtrToIntTypeMap IntTM(DL);
-  SmallVector<GlobalVariable *, 4> InvalidGlobals;
-  for (GlobalVariable &GV : M.globals()) {
+
+  auto EraseInvalidGlobal = [&Changed](GlobalVariable &GV) {
+    GV.replaceAllUsesWith(PoisonValue::get(GV.getType()));
+    GV.eraseFromParent();
+    Changed = true;
+  };
+
+  for (GlobalVariable &GV : make_early_inc_range(M.globals())) {
     if (GV.getAddressSpace() == AMDGPUAS::BUFFER_FAT_POINTER) {
       // FIXME: Use DiagnosticInfo unsupported but it requires a Function
       Ctx.emitError("global variables with a buffer fat pointer address "
                     "space (7) are not supported");
-      InvalidGlobals.push_back(&GV);
+      EraseInvalidGlobal(GV);
       continue;
     }
 
@@ -2483,17 +2490,11 @@ bool AMDGPULowerBufferFatPointers::run(Module &M, const TargetMachine &TM) {
       Ctx.emitError("global variables that contain buffer fat pointers "
                     "(address space 7 pointers) are unsupported. Use "
                     "buffer resource pointers (address space 8) instead");
-      InvalidGlobals.push_back(&GV);
+      EraseInvalidGlobal(GV);
       continue;
     }
   }
 
-  for (GlobalVariable *GV : InvalidGlobals) {
-    GV->replaceAllUsesWith(PoisonValue::get(GV->getType()));
-    GV->eraseFromParent();
-    Changed = true;
-  }
-
   {
     // Collect all constant exprs and aggregates referenced by any function.
     SmallVector<Constant *, 8> Worklist;
diff --git a/llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-unsupported-errors.ll b/llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-unsupported-errors.ll
index 3fc8d73bb0536..73b0176011a3e 100644
--- a/llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-unsupported-errors.ll
+++ b/llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-unsupported-errors.ll
@@ -3,6 +3,7 @@
 ; RUN: not opt -mtriple=amdgcn-amd-amdhsa -disable-output -passes=amdgpu-lower-buffer-fat-pointers %t/contains-poison-init.ll 2>&1 | FileCheck -check-prefix=ERR1 %s
 ; RUN: not opt -mtriple=amdgcn-amd-amdhsa -disable-output -passes=amdgpu-lower-buffer-fat-pointers %t/defined-gv-type.ll 2>&1 | FileCheck -check-prefix=ERR2 %s
 ; RUN: not opt -mtriple=amdgcn-amd-amdhsa -disable-output -passes=amdgpu-lower-buffer-fat-pointers %t/declared-gv-type.ll 2>&1 | FileCheck -check-prefix=ERR3 %s
+; RUN: not opt -mtriple=amdgcn-amd-amdhsa -disable-output -passes=amdgpu-lower-buffer-fat-pointers %t/used-global-with-p7.ll 2>&1 | FileCheck -check-prefix=ERR4 %s
 
 ;--- contains-null-init.ll
 ; ERR0: error: global variables that contain buffer fat pointers (address space 7 pointers) are unsupported. Use buffer resource pointers (address space 8) instead
@@ -20,3 +21,14 @@
 ; ERR3: error: global variables with a buffer fat pointer address space (7) are not supported
 @extern_gv_is_addrspace_7 = external addrspace(7) global i32
 
+;--- used-global-with-p7.ll
+; ERR4: error: global variables that contain buffer fat pointers (address space 7 pointers) are unsupported. Use buffer resource pointers (address space 8) instead
+; ERR4-NOT: LLVM ERROR
+ at G = global ptr addrspace(7) poison
+
+define amdgpu_kernel void @use_global(ptr addrspace(7) %in) {
+  %ptr = load ptr addrspace(7), ptr @G, align 8
+  %v = load i32, ptr addrspace(7) %ptr, align 4
+  store i32 %v, ptr addrspace(7) %in, align 4
+  ret void
+}



More information about the llvm-commits mailing list