Skip to content

SPIRVIntrinsics: tell Enzyme that the built-in vector variables are integers - #642

Merged
maleadt merged 2 commits into
mainfrom
tb/spirv-enzyme-type
Dec 3, 2026
Merged

maleadt merged 2 commits into
mainfrom
tb/spirv-enzyme-type

Conversation

@maleadt

@maleadt maleadt commented Dec 2, 2026

Copy link
Copy Markdown
Member

Reverse-mode Enzyme fails to compile KernelAbstractions kernels on oneAPI.jl (JuliaGPU/oneAPI.jl#771). It finds no derivative for the call of _Z26__spirv_BuiltInWorkgroupIdi, and compiles an EnzymeNoDerivativeError into the kernel.

The cause is Enzyme's type analysis. oneAPI.jl computes KernelInterface indices in 32 bits, so the built-in's 64-bit result only reaches integer code through a trunc, and Enzyme doesn't infer the type of a value from its truncation. Unable to rule out a pointer, Enzyme treats the call as one it has to differentiate. KA's POCL back end uses 64-bit indices, where the result feeds integer arithmetic directly, which is why the same tests pass there.

Enzyme knows that the NVVM and AMDGPU work-item intrinsics return integers. This gives the SPIR-V built-in declarations an enzyme_type return attribute that says the same. The SPIR-V back-end and the Khronos translator ignore it. The existing test of the declarations' attributes now also checks for it, and fails without the change.

Testing:

Includes the bump to SPIRVIntrinsics 1.6.2.

…ntegers

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.
@codecov

codecov Bot commented Dec 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.14%. Comparing base (ad7d380) to head (6aa75c3).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #642      +/-   ##
==========================================
- Coverage   90.32%   90.14%   -0.18%     
==========================================
  Files          18       18              
  Lines        1684     1684              
==========================================
- Hits         1521     1518       -3     
- Misses        163      166       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@maleadt
maleadt merged commit ab29229 into main Dec 3, 2026
20 checks passed
@maleadt
maleadt deleted the tb/spirv-enzyme-type branch December 3, 2026 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant