[llvm] [SPIR-V] Fix infinite loop on GEP handling for an empty struct (PR #202587)

Arseniy Obolenskiy via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 9 05:03:30 PDT 2026


https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/202587

None

>From 0c50fd3ebc341d9e1695dac821307979a9811b28 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Tue, 9 Jun 2026 14:01:19 +0200
Subject: [PATCH] [SPIR-V] Fix infinite loop on GEP handling for an empty
 struct

---
 llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp  |  9 ++++-----
 .../pointers/getelementptr-empty-struct.ll     | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)
 create mode 100644 llvm/test/CodeGen/SPIRV/pointers/getelementptr-empty-struct.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index 180084a65d5b0..f5c462bdc629b 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -767,11 +767,10 @@ bool SPIRVEmitIntrinsics::walkLogicalAccessChainDynamic(
   // of the struct if the current type is a struct.
   // Try to find the first array type that is at offset 0 in the struct.
   while (auto *ST = dyn_cast<StructType>(CurType)) {
-    if (ST->getNumElements() > 0) {
-      CurType = ST->getElementType(0);
-      OnLiteralIndexing(CurType, 0);
-      continue;
-    }
+    if (ST->getNumElements() == 0)
+      break;
+    CurType = ST->getElementType(0);
+    OnLiteralIndexing(CurType, 0);
   }
 
   assert(CurType);
diff --git a/llvm/test/CodeGen/SPIRV/pointers/getelementptr-empty-struct.ll b/llvm/test/CodeGen/SPIRV/pointers/getelementptr-empty-struct.ll
new file mode 100644
index 0000000000000..ec8fa7209a9f4
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/pointers/getelementptr-empty-struct.ll
@@ -0,0 +1,18 @@
+; RUN: llc -O0 -mtriple=spirv1.6-unknown-vulkan1.3-compute %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv1.6-unknown-vulkan1.3-compute %s -o - -filetype=obj | spirv-val --target-env vulkan1.3 %}
+
+; CHECK: OpFunction
+
+%empty = type {}
+
+ at global = internal addrspace(10) global %empty zeroinitializer
+ at in_idx = internal addrspace(10) global i32 zeroinitializer
+
+define void @main() #0 {
+entry:
+  %idx = load i32, ptr addrspace(10) @in_idx
+  %gep = getelementptr inbounds i8, ptr addrspace(10) @global, i32 %idx
+  ret void
+}
+
+attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }



More information about the llvm-commits mailing list