[llvm] d038850 - [SPIR-V] Fix division by zero on byte addressing GEP into a subbyte array (#202593)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 15 08:05:15 PDT 2026
Author: Arseniy Obolenskiy
Date: 2026-06-15T17:05:11+02:00
New Revision: d038850fde42c69028b84135437cb2be994d0ada
URL: https://github.com/llvm/llvm-project/commit/d038850fde42c69028b84135437cb2be994d0ada
DIFF: https://github.com/llvm/llvm-project/commit/d038850fde42c69028b84135437cb2be994d0ada.diff
LOG: [SPIR-V] Fix division by zero on byte addressing GEP into a subbyte array (#202593)
walkLogicalAccessChainConstant divided the byte offset by the element
size which is zero for sub-byte element types ([N x i1])
Added:
llvm/test/CodeGen/SPIRV/pointers/getelementptr-subbyte-array.ll
Modified:
llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index 89b6dad3d5239..10c2a478ed72f 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -799,7 +799,7 @@ bool SPIRVEmitIntrinsics::walkLogicalAccessChainConstant(
do {
if (ArrayType *AT = dyn_cast<ArrayType>(CurType)) {
- uint32_t EltTypeSize = DL.getTypeSizeInBits(AT->getElementType()) / 8;
+ uint64_t EltTypeSize = DL.getTypeAllocSize(AT->getElementType());
assert(Offset < AT->getNumElements() * EltTypeSize);
uint64_t Index = Offset / EltTypeSize;
Offset = Offset - (Index * EltTypeSize);
diff --git a/llvm/test/CodeGen/SPIRV/pointers/getelementptr-subbyte-array.ll b/llvm/test/CodeGen/SPIRV/pointers/getelementptr-subbyte-array.ll
new file mode 100644
index 0000000000000..8b188deab826f
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/pointers/getelementptr-subbyte-array.ll
@@ -0,0 +1,39 @@
+; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; CHECK-DAG: %[[#int8:]] = OpTypeInt 8 0
+; CHECK-DAG: %[[#bool:]] = OpTypeBool
+; CHECK-DAG: %[[#int32:]] = OpTypeInt 32 0
+; CHECK-DAG: %[[#size:]] = OpConstant %[[#int32]] 8
+; CHECK-DAG: %[[#bytearr:]] = OpTypeArray %[[#int8]] %[[#size]]
+; CHECK-DAG: %[[#boolarr:]] = OpTypeArray %[[#bool]] %[[#size]]
+; CHECK-DAG: %[[#boolptr:]] = OpTypePointer Function %[[#boolarr]]
+; CHECK-DAG: %[[#i8ptr:]] = OpTypePointer Function %[[#int8]]
+; CHECK-DAG: %[[#one:]] = OpConstant %[[#]] 1
+
+ at bytes = internal global [8 x i8] zeroinitializer
+ at bools = internal global [8 x i1] zeroinitializer
+
+; Byte addressing into a byte array lowers to a valid logical access chain.
+; CHECK: OpFunction
+; CHECK: %[[#var:]] = OpVariable %[[#]] Function
+; CHECK: %[[#]] = OpInBoundsAccessChain %[[#i8ptr]] %[[#var]] %[[#one]]
+define void @byte_array() {
+entry:
+ %gep = getelementptr inbounds i8, ptr @bytes, i32 1
+ store i8 0, ptr %gep
+ ret void
+}
+
+; Byte addressing into a subbyte array uses the element alloc size as the
+; stride. This previously crashed with a division by zero because the element
+; type size in bits (1 for i1) is not a whole number of bytes.
+; CHECK: OpFunction
+; CHECK: %[[#bvar:]] = OpVariable %[[#boolptr]] Function
+; CHECK: %[[#]] = OpInBoundsAccessChain %[[#]] %[[#bvar]] %[[#one]]
+define void @subbyte_array() {
+entry:
+ %gep = getelementptr inbounds i8, ptr @bools, i32 1
+ store i1 true, ptr %gep
+ ret void
+}
More information about the llvm-commits
mailing list