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" 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