[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:19:21 PDT 2026
https://github.com/aobolensk updated https://github.com/llvm/llvm-project/pull/202587
>From a7bca365377436e5feeb1b303f823e51b00d73f7 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 ++++-----
.../SPIRV/pointers/getelementptr-empty-struct.ll | 16 ++++++++++++++++
2 files changed, 20 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..d9d01762315de
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/pointers/getelementptr-empty-struct.ll
@@ -0,0 +1,16 @@
+; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; CHECK: OpFunction
+
+%empty = type {}
+
+ at global = internal global %empty zeroinitializer
+ at in_idx = internal global i32 zeroinitializer
+
+define void @foo() {
+entry:
+ %idx = load i32, ptr @in_idx
+ %gep = getelementptr inbounds i8, ptr @global, i32 %idx
+ ret void
+}
More information about the llvm-commits
mailing list