[llvm] [SPIR-V] Deduce pointee type for all global variables, not only initialized ones (PR #202047)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 6 08:07:25 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-spir-v

Author: Julian Klappenbach (jklappenbach)

<details>
<summary>Changes</summary>

SPIRVEmitIntrinsics::processGlobalValue only recorded a global variable's
element type (deduceElementTypeHelper) when hasInitializer() was true. For an
undef-initialized, non-constant aggregate global — e.g. a Workgroup `[N x T]`
shared tile (`@<!-- -->g = internal addrspace(3) global [N x T] undef`) — hasInitializer
returns false, so the variable's type was never recorded from its declared value
type. Its pointee type was then inferred lazily from a flat element-typed GEP use
(`getelementptr T, ptr @<!-- -->g, %i`) as the scalar `T`.

With the variable typed as a scalar pointer, the array-to-pointer-decay rewrite
in visitGetElementPtrInst (which prepends a 0 index so a Logical-SPIR-V access
chain can index the array) is skipped, because its DeducedPointeeTy is not an
ArrayType. The GEP stays a byte-offset pointer-arithmetic form Logical SPIR-V
cannot express, and the dynamic index is dropped: every invocation accesses
element 0. A Workgroup array indexed by a loop variable thus collapsed to a
scalar variable, silently corrupting workgroup-shared reductions/staging on
Vulkan (the emitted module passes spirv-val but computes wrong results).

A global variable's pointee type is concrete and authoritative, so record it for
every global in processGlobalValue (which runs before the use-based forward
pass). The deduction result is still ignored at the call site — TypedPointerType
isn't expressible in general LLVM IR; it is stored in the Global Registry.

(cherry picked from commit 2849c532820328544bee3ea3d8acd25289d3f457)

---
*Verified against current `llvm/main`: applies cleanly, the SPIR-V target builds, and the included lit test(s) pass (`llc … | FileCheck`, + `spirv-val` where applicable).*

---
Full diff: https://github.com/llvm/llvm-project/pull/202047.diff


2 Files Affected:

- (modified) llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp (+14-4) 
- (added) llvm/test/CodeGen/SPIRV/pointers/type-deduce-global-array-undef.ll (+37) 


``````````diff
diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index 97fa49d8836fb..a9f13f89cc5c1 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -2553,12 +2553,22 @@ void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV,
   if (!shouldEmitIntrinsicsForGlobalValue(GVUsers, GV, CurrF))
     return;
 
+  // Deduce and record the variable's pointee type from its declared value type,
+  // for EVERY global — not only hasInitializer() ones. A global variable's type
+  // is concrete and authoritative; recording it here (this runs before the
+  // use-based forward pass) keeps an undef-initialized, non-constant aggregate —
+  // e.g. a Workgroup `[N x T]` shared tile — from being collapsed to its element
+  // type by flat element-typed GEP accesses. Without this, hasInitializer()
+  // excludes such globals (undef + non-constant), the variable's type is inferred
+  // from a `getelementptr T, ...` use as scalar `T`, the array-to-pointer-decay
+  // GEP rewrite in visitGetElementPtrInst is skipped (its ArrayType check fails),
+  // and under Logical SPIR-V the dynamic index is dropped — every invocation then
+  // accesses element 0. (Result ignored: TypedPointerType isn't expressible in
+  // general LLVM IR; it is stored in the Global Registry.)
+  deduceElementTypeHelper(&GV, false);
+
   Constant *Init = nullptr;
   if (hasInitializer(&GV)) {
-    // Deduce element type and store results in Global Registry.
-    // Result is ignored, because TypedPointerType is not supported
-    // by llvm IR general logic.
-    deduceElementTypeHelper(&GV, false);
     Init = GV.getInitializer();
     Value *InitOp = Init;
     if (isa<UndefValue>(Init) && Init->getType()->isAggregateType()) {
diff --git a/llvm/test/CodeGen/SPIRV/pointers/type-deduce-global-array-undef.ll b/llvm/test/CodeGen/SPIRV/pointers/type-deduce-global-array-undef.ll
new file mode 100644
index 0000000000000..8aa546a4e2a51
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/pointers/type-deduce-global-array-undef.ll
@@ -0,0 +1,37 @@
+; A global variable's pointee type is concrete (its value type) and must be used
+; even for an undef-initialized, NON-constant aggregate — e.g. a Workgroup
+; `[N x T]` shared tile. Otherwise the variable's type is inferred from a flat
+; element-typed GEP use as the scalar element, the array-to-pointer-decay rewrite
+; is skipped, and under Logical SPIR-V the dynamic index is dropped (every
+; invocation accesses element 0). The variable must keep its array type and be
+; indexed with an OpAccessChain carrying the dynamic index.
+
+; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-vulkan1.3-compute %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-vulkan1.3-compute %s -o - -filetype=obj | spirv-val %}
+
+ at tile = internal addrspace(3) global [64 x i32] undef, align 16
+
+; CHECK-DAG: %[[#U32:]] = OpTypeInt 32 0
+; CHECK-DAG: %[[#Arr:]] = OpTypeArray %[[#U32]] %[[#]]
+; CHECK-DAG: %[[#ArrPtr:]] = OpTypePointer Workgroup %[[#Arr]]
+; CHECK-DAG: %[[#EltPtr:]] = OpTypePointer Workgroup %[[#U32]]
+; CHECK-DAG: %[[#Tile:]] = OpVariable %[[#ArrPtr]] Workgroup
+
+; The (dynamic) index survives as an OpAccessChain into the array tile; it is not
+; dropped, and the variable is not collapsed to a scalar `OpTypePointer Workgroup
+; %u32` with no index.
+; CHECK: %[[#Id:]] = OpCompositeExtract %[[#U32]] %[[#]] 0
+; CHECK: %[[#Ptr:]] = OpAccessChain %[[#EltPtr]] %[[#Tile]] %[[#Id]]
+; CHECK: OpStore %[[#Ptr]] %[[#]]
+
+define void @store_dynamic_index() #0 {
+entry:
+  %id = call i32 @llvm.spv.thread.id.in.group.i32(i32 0)
+  %p = getelementptr i32, ptr addrspace(3) @tile, i32 %id
+  store i32 42, ptr addrspace(3) %p, align 4
+  ret void
+}
+
+declare i32 @llvm.spv.thread.id.in.group.i32(i32)
+
+attributes #0 = { "hlsl.shader"="compute" "hlsl.numthreads"="64,1,1" }

``````````

</details>


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


More information about the llvm-commits mailing list