Skip to content

[Vulkan][SPIR-V] i8 loads emulated as i32 read past the binding range when the buffer length is not a multiple of 4 #25272

Open

Description

@jschuhmacher

On Vulkan targets without 8-bit storage (e.g. the default vulkan-spirv target, vp_android_baseline_2022, storage = b32), an i8 linalg.batch_matmul returns wrong results when an operand's byte length is not a multiple of 4.

func.func @mm(%a: tensor<2x2x3xi8>, %b: tensor<2x3x1xi8>) -> tensor<2x2x1xi32> {
  %c0 = arith.constant 0 : i32
  %e = tensor.empty() : tensor<2x2x1xi32>
  %f = linalg.fill ins(%c0 : i32) outs(%e : tensor<2x2x1xi32>) -> tensor<2x2x1xi32>
  %r = linalg.batch_matmul ins(%a, %b : tensor<2x2x3xi8>, tensor<2x3x1xi8>) outs(%f : tensor<2x2x1xi32>) -> tensor<2x2x1xi32>
  return %r : tensor<2x2x1xi32>
}
iree-compile --iree-hal-target-backends=vulkan-spirv mm.mlir -o mm.vmfb
iree-run-module --device=vulkan --module=mm.vmfb --function=mm \
  --input='2x2x3xi8=[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]' \
  --input='2x3x1xi8=[[7],[8],[9]],[[7],[8],[9]]'

Expected: 2x2x1xi32=[[50][122]][[50][122]]
Actual: 2x2x1xi32=[[50][122]][[7][28]]

Seen on lavapipe (llvmpipe, Mesa 25.2.8, LLVM 20.1.2) with release 3.14.0rc2026111.

Cause

  • i8 is emulated by loading the containing i32 word and shifting. The index arithmetic in the generated SPIR-V is correct.
  • %b is 6 bytes, and the dispatch binds it with range [0, 6]. Batch 1 reads bytes 4 and 5 through a 4-byte load of word 1 (bytes 4–7), 2 bytes past the bound range.
  • The runtime sets VkDescriptorBufferInfo.range to the exact binding length
    (runtime/src/iree/hal/drivers/vulkan/command_buffer.c). It creates theVkBuffer at the exact allocation size (allocator.c), so the range cannot simply be rounded up.
  • robustBufferAccess is off by default, so the read is undefined behaviour. lavapipe returns 0 for the whole word, which drops the k=1 and k=2 terms of batch 1 (1·7 = 7, 4·7 = 28). Drivers that return the neighbouring bytes would hide the bug.

Supporting checks:

  • The same matmul with N=4, where every buffer length is a multiple of 4 bytes, gives the correct result.
  • With --iree-vulkan-target=valhall, which has 8-bit storage, the result is correct.

Assisted-by: Claude Code

Activity

  1. added
    bug 🐞
    hal/vulkan
    codegen/spirv
    on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐞codegen/spirvhal/vulkan

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions