Skip to content

SPIR-V: declare native integer widths in the datalayout #1008

Done

Description

@maleadt

GPUCompiler's SPIR-V datalayout doesn't declare native integer widths:

GPUCompiler.jl/src/spirv.jl

Lines 142 to 144 in 94f8fbe

llvm_datalayout(::SPIRVCompilerTarget) = Int===Int64 ?
"e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1" :
"e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1"

Upstream LLVM added n8:16:32:64 to its SPIR-V datalayouts in llvm/llvm-project#110695, because without it "optimisation passes, such as InstCombine, [get] ideas and e.g. [shrink] to non byte-multiple integer types, which is not desirable and can lead to breakage further down in the toolchain".

We hit this with Julia 1.13 (LLVM 20): in KernelAbstractions' PoCL exception path, a switch on a function returning 0, 1 or 2 gets narrowed to i2:

%trunc = trunc nuw i64 %0 to i2
switch i2 %trunc, label %L21 [
  i2 1, label %L18.thread
  i2 0, label %L23
]

LLVM's SPIR-V back end happens to cope, since it rounds integer widths up to 8/16/32/64/128 bits (SPIRVGlobalRegistry::adjustOpTypeIntWidth), but:

  • other consumers may not: the Khronos translator presumably needs SPV_INTEL_arbitrary_precision_integers for such types (not verified);
  • libspirv replaces the module's datalayout with its target machine's, which does include n8:16:32:64, so we optimize under different assumptions than the back end compiles under;
  • odd-width arithmetic has to be masked again once widened.

The fix is to add n8:16:32:64 to both variants (spirv64 and spirv32), as upstream does. It only affects which integer widths the optimizer considers native, not type sizes, alignment or pointers. It should be validated with KernelAbstractions' PoCL tests and OpenCL.jl's PoCL tests, including the Khronos translator configuration.

Activity

  1. added theissue type on Oct 19, 2026
  2. added
    spirv
    on Oct 19, 2026
  3. maleadt commented on Nov 25, 2026

    @maleadt
    MemberAuthor
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

    spirv

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions