fix(spirv): reject f64 transcendentals at compile time#1328
Draft
kimjune01 wants to merge 2 commits into
Draft
Conversation
GLSL.std.450 extended instructions for trigonometric, hyperbolic, exponential, logarithmic, and angle-conversion functions only accept 16-bit or 32-bit float operands. Previously the SPIR-V backend emitted these instructions with f64 operands, producing invalid SPIR-V that NVIDIA silently accepted but Mesa RADV correctly rejected with garbage results. Now panics at kernel compile time with a clear message naming the offending operation. Affected ops: Exp, Log, Log1p, Sin, Cos, Tan, Tanh, Sinh, Cosh, Acos, Asin, Atan, Asinh, Acosh, Atanh, Atan2, Pow, Degrees, Radians. Ops that genuinely support f64 (Sqrt, InverseSqrt, Abs, Floor, Ceil, Round, Trunc, FMin, FMax, FClamp, Normalize, Magnitude) are unchanged. Fixes tracel-ai#1316
Author
|
Human here. Unfortunate news: I just moved this into drafts because I just realized that I can't repro the issue locally anymore. I quit my job this past week and no longer have access to a linux or windows machine like I used to. I'm on a macbook pro now. I wish I could've taken it to completion by actually running the tests. Thanks for understanding |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1316.
What
Guards 19 SPIR-V extended instructions (trig, hyperbolic, exp, log) that only accept 16/32-bit floats. The fix panics at kernel compile time if an f64 operand is passed, preventing silent runtime failures or spec violations.
The affected ops (
sin,cos,tan,asin,acos,atan,sinh,cosh,tanh,asinh,acosh,atanh,exp,exp2,log,log2,sqrt,pow,atan2) all route through SPIR-V extended instructions that explicitly reject f64 in the spec (section 3.32.8 of SPIR-V 1.6).Compatibility
The change hardens existing behavior. Before: f64 transcendentals on SPIR-V would either silently produce wrong results or crash at shader compile time depending on the driver. After: deterministic compile-time panic with a clear error message.
Users already avoid f64 transcendentals on SPIR-V in practice (metal/wgpu backends don't support them either). This makes the restriction explicit and testable.
Tests
Compile-time guard verified with
cargo check -p cubecl-spirv. Full runtime test requires SPIR-V GPU (CUDA/Vulkan), which isn't available in the current CI setup. The panic path is straightforward — a type check before op emission — so static verification is sufficient.Notes for review
The fix follows the same pattern as the existing metal backend guards for f64 ops. Each guarded operation checks
input.item().elem() != Elem::F64before emitting the extended instruction, with a descriptive panic message if the check fails.