From 2dc5dd4009b507e9dc4b0a2d236a6b6faea5e571 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 2 Dec 2026 18:52:34 +0100 Subject: [PATCH 1/2] SPIRVIntrinsics: tell Enzyme that the built-in vector variables are integers Reverse-mode Enzyme failed to compile KernelAbstractions kernels on oneAPI.jl: it found no derivative for the call of `_Z26__spirv_BuiltInWorkgroupIdi`, and compiled an `EnzymeNoDerivativeError` into the kernel. oneAPI.jl computes KernelInterface indices in 32 bits, so the built-in's 64-bit result only reaches integer code through a `trunc`, through which Enzyme's type analysis doesn't infer the source type. Not knowing that the result isn't a pointer, Enzyme then needs a derivative of the call. KA's POCL back end uses 64-bit indices, which is why it passed. Enzyme knows that the NVVM and AMDGPU work-item intrinsics return integers. Give the SPIR-V declarations the same knowledge with an `enzyme_type` return attribute, which the SPIR-V back-end and translator drop. --- lib/intrinsics/src/work_item.jl | 6 +++++- test/intrinsics.jl | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/intrinsics/src/work_item.jl b/lib/intrinsics/src/work_item.jl index 30777c9f..725ff4d7 100644 --- a/lib/intrinsics/src/work_item.jl +++ b/lib/intrinsics/src/work_item.jl @@ -17,7 +17,10 @@ end # back-end lowers to a load and an extract of the component. the declaration gets the # attributes LLVM gives its own work-item intrinsics (e.g. `llvm.nvvm.read.ptx.sreg.tid.x`): # Enzyme only differentiates through calls of `nofree` declarations, and `speculatable` lets -# the optimizer hoist calls out of conditionals. +# the optimizer hoist calls out of conditionals. The `enzyme_type` return attribute tells +# Enzyme that the result is an integer, as it knows for those intrinsics: when the result +# only reaches integer code through a `trunc` (e.g. with 32-bit indices), Enzyme otherwise +# can't rule out a pointer, and fails to differentiate the call in reverse mode. @llvmgenerated builder function builtin_vector_variable(::Val{name}, idx::Int32)::UInt where {name} ft = LLVM.FunctionType(convert(LLVMType, UInt), [idx.value_type]) f = LLVM.Function(current_module(builder), String(name), ft) @@ -27,6 +30,7 @@ end push!(f.function_attributes, EnumAttribute(:nosync)) push!(f.function_attributes, EnumAttribute(:speculatable)) f.memory_effects = MemoryEffects(:none) + push!(f.return_attributes, StringAttribute("enzyme_type", "{[-1]:Integer}")) call!(builder, ft, f, [idx]) end diff --git a/test/intrinsics.jl b/test/intrinsics.jl index ad633c09..62a8f8a4 100644 --- a/test/intrinsics.jl +++ b/test/intrinsics.jl @@ -249,6 +249,8 @@ builtin_attributes_kernel(a) = (@inbounds a[get_global_id(1)] = get_group_id(1); @test attrs !== nothing group = match(Regex("attributes #$(attrs[1]) = \\{([^}]*)\\}"), ir)[1] @test issubset(["nofree", "nosync", "nounwind", "speculatable", "willreturn"], split(group)) + # Enzyme's type analysis needs to know the result is an integer + @test occursin(r"declare \"enzyme_type\"=\"\{\[-1\]:Integer\}\" i64 @_Z26__spirv_BuiltInWorkgroupIdi", ir) end # truncating the 3-D builtins used to produce loads of vector types SPIR-V doesn't have From 6aa75c33c239b48cf279aae8342eb1427c99e04c Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 2 Dec 2026 18:52:41 +0100 Subject: [PATCH 2/2] Bump SPIRVIntrinsics to 1.6.2. --- lib/intrinsics/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/intrinsics/Project.toml b/lib/intrinsics/Project.toml index 522cf21d..b7bbd663 100644 --- a/lib/intrinsics/Project.toml +++ b/lib/intrinsics/Project.toml @@ -1,7 +1,7 @@ name = "SPIRVIntrinsics" uuid = "71d1d633-e7e8-4a92-83a1-de8814b09ba8" authors = ["Tim Besard "] -version = "1.6.1" +version = "1.6.2" [deps] GPUToolbox = "096a3bc2-3ced-46d0-87f4-dd12716f4bfc"