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

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 2 01:45:56 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

Author: Arseniy Obolenskiy (aobolensk)

<details>
<summary>Changes</summary>

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

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


2 Files Affected:

- (modified) llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp (+1-1) 
- (added) llvm/test/CodeGen/SPIRV/pointers/array-stride-subbyte.ll (+15) 


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

``````````

</details>


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


More information about the llvm-commits mailing list