[llvm] [SPIR-V] Fix store to first element array (PR #175546)

Nathan Gauër via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 12 06:29:47 PST 2026


https://github.com/Keenuts created https://github.com/llvm/llvm-project/pull/175546

The IR can store to the first element of an array the same way it stores to the first element of a struct by specifying the base pointer. This commit fixes the pointercast legalization pass to support this.

>From 25a267d50391f2cfd482e6226ae95687b1fe5aa8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Mon, 12 Jan 2026 14:21:45 +0100
Subject: [PATCH] [SPIR-V] Fix store to first element array

The IR can store to the first element of an array the same
way it stores to the first element of a struct by specifying the
base pointer. This commit fixes the pointercast legalization pass
to support this.
---
 .../Target/SPIRV/SPIRVLegalizePointerCast.cpp |  2 ++
 .../pointers/store-to-array-first-element.ll  | 20 +++++++++++++++++++
 2 files changed, 22 insertions(+)
 create mode 100644 llvm/test/CodeGen/SPIRV/pointers/store-to-array-first-element.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp b/llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp
index a794f3e9c5363..8c6a770d3a08e 100644
--- a/llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp
@@ -370,6 +370,8 @@ class SPIRVLegalizePointerCast : public FunctionPass {
       storeToFirstValueAggregate(B, Src, Dst, D_VT, Alignment);
     else if (D_AT && S_VT && S_VT->getElementType() == D_AT->getElementType())
       storeArrayFromVector(B, Src, Dst, D_AT, Alignment);
+    else if (D_AT && D_AT->getElementType() == FromTy)
+      storeToFirstValueAggregate(B, Src, Dst, D_AT, Alignment);
     else
       llvm_unreachable("Unsupported ptrcast use in store. Please fix.");
 
diff --git a/llvm/test/CodeGen/SPIRV/pointers/store-to-array-first-element.ll b/llvm/test/CodeGen/SPIRV/pointers/store-to-array-first-element.ll
new file mode 100644
index 0000000000000..2369c51799993
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/pointers/store-to-array-first-element.ll
@@ -0,0 +1,20 @@
+; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-pc-vulkan1.3-compute %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-pc-vulkan1.3-compute %s -o - -filetype=obj | spirv-val %}
+
+; CHECK-DAG: %[[#Int:]] = OpTypeInt 32 0
+; CHECK-DAG: %[[#IntPtr:]] = OpTypePointer Function %[[#Int]]
+; CHECK-DAG: %[[#Array:]] = OpTypeArray %[[#Int]] %[[#]]
+; CHECK-DAG: %[[#ArrayPtr:]] = OpTypePointer Function %[[#Array]]
+; CHECK-DAG: %[[#Const:]] = OpConstant %[[#Int]] 123
+; CHECK-DAG: %[[#Zero:]] = OpConstant %[[#Int]] 0
+
+; CHECK: %[[#Var:]] = OpVariable %[[#ArrayPtr]] Function
+; CHECK: %[[#GEP:]] = OpInBoundsAccessChain %[[#IntPtr]] %[[#Var]] %[[#Zero]]
+; CHECK: OpStore %[[#GEP]] %[[#Const]]
+
+define spir_func void @test_array_store() {
+entry:
+  %var = alloca [4 x i32]
+  store i32 123, ptr %var
+  ret void
+}



More information about the llvm-commits mailing list