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

via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 12 06:30:21 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

Author: Nathan Gauër (Keenuts)

<details>
<summary>Changes</summary>

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.

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


2 Files Affected:

- (modified) llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp (+2) 
- (added) llvm/test/CodeGen/SPIRV/pointers/store-to-array-first-element.ll (+20) 


``````````diff
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
+}

``````````

</details>


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


More information about the llvm-commits mailing list