[llvm] [SPIR-V] Fix ArrayStride truncation to 0 for sub-byte element types (PR #207140)

Arseniy Obolenskiy via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 6 01:44:21 PDT 2026


https://github.com/aobolensk updated https://github.com/llvm/llvm-project/pull/207140

>From 71937705d9f8021948d016214d1bf9e879ecd241 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Thu, 2 Jul 2026 10:43:46 +0200
Subject: [PATCH 1/2] [SPIR-V] Fix ArrayStride truncation to 0 for sub-byte
 element types

addArrayStrideDecorations computed the stride as `getTypeSizeInBits/8` which truncates to 0 for sub-byte element types such as i1

0 strides are disallowed in spec
---
 llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp     |  2 +-
 .../SPIRV/pointers/array-stride-subbyte.ll        | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/SPIRV/pointers/array-stride-subbyte.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index 6dd965c266fcb..39b4dbac78fb7 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -2282,7 +2282,7 @@ void SPIRVGlobalRegistry::addStructOffsetDecorations(
 
 void SPIRVGlobalRegistry::addArrayStrideDecorations(
     Register Reg, Type *ElementType, MachineIRBuilder &MIRBuilder) {
-  uint32_t SizeInBytes = DL.getTypeSizeInBits(ElementType) / 8;
+  uint32_t SizeInBytes = DL.getTypeAllocSize(ElementType);
   buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::ArrayStride,
                   {SizeInBytes});
 }
diff --git a/llvm/test/CodeGen/SPIRV/pointers/array-stride-subbyte.ll b/llvm/test/CodeGen/SPIRV/pointers/array-stride-subbyte.ll
new file mode 100644
index 0000000000000..9089ba79bf7ff
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/pointers/array-stride-subbyte.ll
@@ -0,0 +1,15 @@
+; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv1.6-vulkan1.3-library %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv1.6-vulkan1.3-library %s -o - -filetype=obj | spirv-val %}
+
+; A sub-byte element (i1) must not truncate its ArrayStride to 0.
+
+ at .str = private unnamed_addr constant [2 x i8] c"B\00", align 1
+
+; CHECK: OpDecorate [[array:%[0-9]+]] ArrayStride 1
+; CHECK: [[bool:%[0-9]+]] = OpTypeBool
+; CHECK: [[array]] = OpTypeRuntimeArray [[bool]]
+
+define external void @main() {
+  %handle = tail call target("spirv.VulkanBuffer", [0 x i1], 12, 1) @llvm.spv.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, ptr nonnull @.str)
+  ret void
+}

>From 6c6161d16e1a01aec6fa95e81a03242abb90f169 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <gooddoog at student.su>
Date: Mon, 6 Jul 2026 10:44:12 +0200
Subject: [PATCH 2/2] Update
 llvm/test/CodeGen/SPIRV/pointers/array-stride-subbyte.ll
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Juan Manuel Martinez CaamaƱo <jmartinezcaamao at gmail.com>
---
 llvm/test/CodeGen/SPIRV/pointers/array-stride-subbyte.ll | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/test/CodeGen/SPIRV/pointers/array-stride-subbyte.ll b/llvm/test/CodeGen/SPIRV/pointers/array-stride-subbyte.ll
index 9089ba79bf7ff..12f1c6e5f8547 100644
--- a/llvm/test/CodeGen/SPIRV/pointers/array-stride-subbyte.ll
+++ b/llvm/test/CodeGen/SPIRV/pointers/array-stride-subbyte.ll
@@ -1,5 +1,5 @@
-; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv1.6-vulkan1.3-library %s -o - | FileCheck %s
-; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv1.6-vulkan1.3-library %s -o - -filetype=obj | spirv-val %}
+; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv1.6-vulkan1.3-library < %s | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv1.6-vulkan1.3-library -filetype=obj < %s | spirv-val %}
 
 ; A sub-byte element (i1) must not truncate its ArrayStride to 0.
 



More information about the llvm-commits mailing list