Repository navigation
feat(shader): implement basic f64 VALU ops - #446
Conversation
Progress report (b0a4a99 → fe26911)📈 GPU shader instructions: 75.13% (+1.63%, +19 implemented instructions) ✅ 19 implemented
|
The Vulkan precision table defines GLSL.std.450 Fma as "inherited from OpFMul followed by OpFAdd", so a driver may round twice. RADV fuses it, the lavapipe in CI (Mesa 25.2.8, LLVM 20) does not, and F64Arithmetic failed there on the fma rounding-error row. v_fma_f64 is now emulated with operations Vulkan requires to be correctly rounded: the operands are scaled to exponent 0, the product is split exactly (Dekker), added to the addend with TwoSum, the low parts are summed with round-to-odd and one final round-to-nearest gives the fused result (Boldo and Melquiond). The result is scaled back by two exact powers of two. Inf/NaN, zero factors and subnormal inputs take the mul + add path, an addend 2^60 larger than the product is returned as is. Matches the hardware on all 13253 fma results captured for boykopovar#446 and an exact model on 300000 random inputs incl. ties. NaN results of add, mul and fract are also chosen on the bits (first NaN input quieted, otherwise the default NaN) since Vulkan does not specify which NaN an operation returns. Co-Authored-By: Claude <noreply@anthropic.com>
c2c3f74 to
e64270e
Compare
Decode and translate the remaining unary f64 transcendentals and v_trig_preop_f64. On RDNA2 hardware v_rcp_f64, v_rsq_f64 and v_sqrt_f64 are approximations (relative error up to ~2^-24.3 over 8209 inputs; sqrt(1.0) is not even exact), so they are emitted as exact double operations and the test compares against the hardware value with a 2^-23 tolerance. Special cases are exact and selected on bits: NaN inputs are quieted (RADV keeps signaling NaN on FDiv), negative inputs give the default NaN 0xfff8000000000000, rsq(-0) = -inf, sqrt(-0) = -0. v_trig_preop_f64 matches the hardware bit for bit on 24627 results plus a sweep of the table tail: the 2/PI table is 1184 bits (bits below b17 read as zero), shift = 53 * S1[4:0] + max(exp - 1077, 0), +128 scale for exp >= 1968, omod ignored. The 53-bit window is read from a private u32 table and scaled in two exact steps so only the final multiply rounds. Stacked on the f64 infrastructure of boykopovar#446. Co-Authored-By: Claude <noreply@anthropic.com>
e64270e to
fae9051
Compare
Decode and translate the remaining unary f64 transcendentals and v_trig_preop_f64. On RDNA2 hardware v_rcp_f64, v_rsq_f64 and v_sqrt_f64 are approximations (relative error up to ~2^-24.3 over 8209 inputs; sqrt(1.0) is not even exact), so they are emitted as exact double operations and the test compares against the hardware value with a 2^-23 tolerance. Special cases are exact and selected on bits: NaN inputs are quieted (RADV keeps signaling NaN on FDiv), negative inputs give the default NaN 0xfff8000000000000, rsq(-0) = -inf, sqrt(-0) = -0. v_trig_preop_f64 matches the hardware bit for bit on 24627 results plus a sweep of the table tail: the 2/PI table is 1184 bits (bits below b17 read as zero), shift = 53 * S1[4:0] + max(exp - 1077, 0), +128 scale for exp >= 1968, omod ignored. The 53-bit window is read from a private u32 table and scaled in two exact steps so only the final multiply rounds. Stacked on the f64 infrastructure of boykopovar#446. Co-Authored-By: Claude <noreply@anthropic.com>
|
Thanks, this is a big and careful piece of work, and the hardware vectors make it easy to trust. Two small things. Main already has TranslationContext::readF64Bits (from #365) with exactly the rules readF64 implements here (literal as the high dword, the exact 1/(2π) double, abs and neg on bit 63); could readF64 just build the U64 from readF64Bits, so the rules live in one place? #474 already uses both. And v_fma_f64 falls back to a separate multiply and add whenever an input is subnormal, which rounds twice; since f64 denormals follow the driver that may be fine for now, but I think it should be written down in docs/dev/TechnicalDebt.md so the gap is recorded. |
The Vulkan precision table defines GLSL.std.450 Fma as "inherited from OpFMul followed by OpFAdd", so a driver may round twice. RADV fuses it, the lavapipe in CI (Mesa 25.2.8, LLVM 20) does not, and F64Arithmetic failed there on the fma rounding-error row. v_fma_f64 is now emulated with operations Vulkan requires to be correctly rounded: the operands are scaled to exponent 0, the product is split exactly (Dekker), added to the addend with TwoSum, the low parts are summed with round-to-odd and one final round-to-nearest gives the fused result (Boldo and Melquiond). The result is scaled back by two exact powers of two. Inf/NaN, zero factors and subnormal inputs take the mul + add path, an addend 2^60 larger than the product is returned as is. Matches the hardware on all 13253 fma results captured for boykopovar#446 and an exact model on 300000 random inputs incl. ties. NaN results of add, mul and fract are also chosen on the bits (first NaN input quieted, otherwise the default NaN) since Vulkan does not specify which NaN an operation returns. Co-Authored-By: Claude <noreply@anthropic.com>
fae9051 to
22b9fb0
Compare
|
Thanks! Both done, rebased onto main:
|
|
Thanks for the quick update, that covers it. I'll test it here and merge once CI is green. |
|
I ran the tests here and |
Decode and translate v_add/mul/fma/min/max/ldexp_f64, v_trunc/ceil/floor/ rndne/fract_f64, v_frexp_mant/exp_i32_f64 and the f64 <-> f32/i32/u32 conversions. f64 values stay as their raw bits (U64) in the IR and are bitcast to double in SPIR-V, which needs the Float64 capability: the device enables shaderFloat64 when it also preserves signed zero, inf and NaN for 64-bit floats, and the shader declares SignedZeroInfNanPreserve 64. Semantics measured on RDNA2 hardware (8209 inputs per op): literals are the high dword, the 1/(2*pi) inline constant is the exact double, omod is ignored, clamp maps NaN to +0, min/max quiet a signaling NaN and drop a quiet one, NaN results are quiet, and the integer conversions saturate with NaN -> 0. Truncation, rounding and ldexp are done on the bits so they do not depend on the driver: lavapipe returned wrong results for f64 RoundEven and Trunc, and GLSL Ldexp is undefined past +-1024. Fma is NoContraction so RADV keeps it fused. Co-Authored-By: Claude <noreply@anthropic.com>
The Vulkan precision table defines GLSL.std.450 Fma as "inherited from OpFMul followed by OpFAdd", so a driver may round twice. RADV fuses it, the lavapipe in CI (Mesa 25.2.8, LLVM 20) does not, and F64Arithmetic failed there on the fma rounding-error row. v_fma_f64 is now emulated with operations Vulkan requires to be correctly rounded: the operands are scaled to exponent 0, the product is split exactly (Dekker), added to the addend with TwoSum, the low parts are summed with round-to-odd and one final round-to-nearest gives the fused result (Boldo and Melquiond). The result is scaled back by two exact powers of two. Inf/NaN, zero factors and subnormal inputs take the mul + add path, an addend 2^60 larger than the product is returned as is. Matches the hardware on all 13253 fma results captured for boykopovar#446 and an exact model on 300000 random inputs incl. ties. NaN results of add, mul and fract are also chosen on the bits (first NaN input quieted, otherwise the default NaN) since Vulkan does not specify which NaN an operation returns. Co-Authored-By: Claude <noreply@anthropic.com>
…e v_fma_f64 subnormal gap Co-Authored-By: Claude <noreply@anthropic.com>
OpFConvert leaves NaN payloads to the driver; NVIDIA returns its own. v_cvt_f64_f32 now shifts the f32 payload left by 29 and v_cvt_f32_f64 keeps the top 23 payload bits, both quieted, as RDNA does (60/60 NaN conversions measured on RDNA2). F64NanConversions covers both with the hardware rows. Co-Authored-By: Claude <noreply@anthropic.com>
22b9fb0 to
fe26911
Compare
|
Thanks for running it on NVIDIA. Fixed and rebased onto main:
Both match the hardware on all 60 NaN conversions I measured on RDNA2 (quiet and signalling, both signs, random payloads). The other conversions here already pick their NaN results without the driver (f64 to i32/u32 select 0 for NaN; the arithmetic, rounding, min/max and fma paths go through
|
|
Thanks, that fixes it. Merged with current main, all five f64 tests including the new |
Decode v_cmp_*_f64, v_cmpx_*_f64 and v_cmp(x)_class_f64 in their VOPC and VOP3 encodings and translate them as exact IEEE-754 compares on the 64-bit pattern with the existing 64-bit integer IR, so no Float64 capability is needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit bd75a89) integ: upstream boykopovar#365 (merged on main as bd75a89, after astrobot's last main merge), taken as the prerequisite of boykopovar#446: it adds readF64Bits.
The Vulkan precision table defines GLSL.std.450 Fma as "inherited from OpFMul followed by OpFAdd", so a driver may round twice. RADV fuses it, the lavapipe in CI (Mesa 25.2.8, LLVM 20) does not, and F64Arithmetic failed there on the fma rounding-error row. v_fma_f64 is now emulated with operations Vulkan requires to be correctly rounded: the operands are scaled to exponent 0, the product is split exactly (Dekker), added to the addend with TwoSum, the low parts are summed with round-to-odd and one final round-to-nearest gives the fused result (Boldo and Melquiond). The result is scaled back by two exact powers of two. Inf/NaN, zero factors and subnormal inputs take the mul + add path, an addend 2^60 larger than the product is returned as is. Matches the hardware on all 13253 fma results captured for boykopovar#446 and an exact model on 300000 random inputs incl. ties. NaN results of add, mul and fract are also chosen on the bits (first NaN input quieted, otherwise the default NaN) since Vulkan does not specify which NaN an operation returns. Co-Authored-By: Claude <noreply@anthropic.com> (cherry picked from commit 6f20ed3)
Decode and translate the remaining unary f64 transcendentals and v_trig_preop_f64. On RDNA2 hardware v_rcp_f64, v_rsq_f64 and v_sqrt_f64 are approximations (relative error up to ~2^-24.3 over 8209 inputs; sqrt(1.0) is not even exact), so they are emitted as exact double operations and the test compares against the hardware value with a 2^-23 tolerance. Special cases are exact and selected on bits: NaN inputs are quieted (RADV keeps signaling NaN on FDiv), negative inputs give the default NaN 0xfff8000000000000, rsq(-0) = -inf, sqrt(-0) = -0. v_trig_preop_f64 matches the hardware bit for bit on 24627 results plus a sweep of the table tail: the 2/PI table is 1184 bits (bits below b17 read as zero), shift = 53 * S1[4:0] + max(exp - 1077, 0), +128 scale for exp >= 1968, omod ignored. The 53-bit window is read from a private u32 table and scaled in two exact steps so only the final multiply rounds. Stacked on the f64 infrastructure of boykopovar#446. Co-Authored-By: Claude <noreply@anthropic.com>
Decode and translate the remaining unary f64 transcendentals and v_trig_preop_f64. On RDNA2 hardware v_rcp_f64, v_rsq_f64 and v_sqrt_f64 are approximations (relative error up to ~2^-24.3 over 8209 inputs; sqrt(1.0) is not even exact), so they are emitted as exact double operations and the test compares against the hardware value with a 2^-23 tolerance. Special cases are exact and selected on bits: NaN inputs are quieted (RADV keeps signaling NaN on FDiv), negative inputs give the default NaN 0xfff8000000000000, rsq(-0) = -inf, sqrt(-0) = -0. v_trig_preop_f64 matches the hardware bit for bit on 24627 results plus a sweep of the table tail: the 2/PI table is 1184 bits (bits below b17 read as zero), shift = 53 * S1[4:0] + max(exp - 1077, 0), +128 scale for exp >= 1968, omod ignored. The 53-bit window is read from a private u32 table and scaled in two exact steps so only the final multiply rounds. Stacked on the f64 infrastructure of boykopovar#446. Co-Authored-By: Claude <noreply@anthropic.com>
Decode and translate the remaining unary f64 transcendentals and v_trig_preop_f64. On RDNA2 hardware v_rcp_f64, v_rsq_f64 and v_sqrt_f64 are approximations (relative error up to ~2^-24.3 over 8209 inputs; sqrt(1.0) is not even exact), so they are emitted as exact double operations and the test compares against the hardware value with a 2^-23 tolerance. Special cases are exact and selected on bits: NaN inputs are quieted (RADV keeps signaling NaN on FDiv), negative inputs give the default NaN 0xfff8000000000000, rsq(-0) = -inf, sqrt(-0) = -0. v_trig_preop_f64 matches the hardware bit for bit on 24627 results plus a sweep of the table tail: the 2/PI table is 1184 bits (bits below b17 read as zero), shift = 53 * S1[4:0] + max(exp - 1077, 0), +128 scale for exp >= 1968, omod ignored. The 53-bit window is read from a private u32 table and scaled in two exact steps so only the final multiply rounds. Stacked on the f64 infrastructure of boykopovar#446. Co-Authored-By: Claude <noreply@anthropic.com>
Takes the 13 gate-batch PRs that merged upstream (boykopovar#319 boykopovar#329 boykopovar#330 boykopovar#331 boykopovar#325 boykopovar#435 boykopovar#471 boykopovar#294 boykopovar#376 boykopovar#404 boykopovar#394 boykopovar#401 boykopovar#446) in place of astrobot's local versions, as integ/gate-batch cab0eab had them: boykopovar#294 and boykopovar#376 in astrobot's depth model (DepthTarget, state.depth), boykopovar#394 with the thick mip tails, boykopovar#401 over astrobot's slab pool, boykopovar#471 keeping the settled single-lane path. Also takes the rest of main since a818bee (boykopovar#532 boykopovar#340 boykopovar#486 boykopovar#484 boykopovar#316 boykopovar#243 boykopovar#334 boykopovar#434 boykopovar#437 boykopovar#470 boykopovar#373 boykopovar#452 boykopovar#455 and our upstreamed PRs, where astrobot keeps its later revisions and env switches). Resolution notes: relinker converter/matcher/tests are upstream's (boykopovar#432 decoder covers VEX RIP-relative followers); RecompileResult gains boykopovar#316's hostSubgroupSize (set from request.target.subgroupSize); shader disk cache format 9; boykopovar#452's scissor message; upstream's later live-stack and memory-copy decode tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mgp3oZvembQziVNE2xARQK
What
The basic f64 VALU ops threw "opcode not implemented". Found by the ISA coverage sweep:
v_fma_f64,v_add_f64,v_mul_f64,v_min_f64,v_max_f64,v_ldexp_f64.v_cvt_{i32,u32,f32}_f64,v_cvt_f64_{i32,u32,f32},v_{trunc,ceil,floor,rndne,fract}_f64,v_frexp_mant_f64,v_frexp_exp_i32_f64.There was no f64 path in the backend, so this adds one:
U64, auvec2) in the IR. New IR opcodes (FPAdd64,ConvertF64F32, ...) bitcast todoublein SPIR-V.shaderFloat64when it is available together withshaderSignedZeroInfNanPreserveFloat64, andpublishes
Float64. Shaders that use f64 declareFloat64andSignedZeroInfNanPreserve 64. On a device without itthey fail to compile with a clear error. Graphics validation now accepts
SignedZeroInfNanPreserve32 and 64.0x3fc45f306dc9c882, and abs/neg act on bit 63.Behaviour measured on RDNA2 hardware (Radeon 660M, 8209 inputs per op, matched 100% by a Python model before the C++
was written):
cvt_f32_f64too). Clamp works: NaN → +0, -0 → +0.returns (RADV keeps a signalling one), so the NaN is chosen on the bits.
v_fma_f64is fused. GLSLFmais not enough: Vulkan defines its precision as "inherited from OpFMul followed byOpFAdd", and the lavapipe in CI (Mesa 25.2.8) rounds twice. So fma is emulated with correctly rounded mul/add only:
exact product (Dekker split) + TwoSum + round-to-odd, one final rounding (Boldo-Melquiond), on operands scaled to
exponent 0. It matches the hardware on all 13253 captured fma results and an exact model on 300k random inputs.
fract:x - floor(x)clamped to0x3fefffffffffffff. inf → NaN.frexp: inf/NaN give the input (NaN quieted) and exponent 0.cvt_i32/u32_f64saturate, NaN → 0.cvtbetween f32 and f64 quiets NaN and keeps the payload.trunc/ceil/floor/rndneandldexpare done on the bits instead of GLSL.std.450. lavapipe returned wrong f64RoundEven(1.5)(1.0) andTrunc(fraction bits kept). GLSLLdexpis undefined past ±1024, and lavapipe gave NaNfor
ldexp(-inf, huge)in one sweep run.ldexpnormalises the mantissa, then scales in one exact step and onerounding step.
Overlap: #365 (VOPC f64 compares, DotDebian) adds a similar
readF64Bitshelper and does not touch these opcodes.Whichever merges second can switch to the other's helper. It merges cleanly with this one.
Tested
result classes and forced edge rows (fract of -2^-60, ldexp with INT_MIN/INT_MAX/-2100 exponents, ±inf):
F64Arithmetic: add, mul, fma, fma rounding error, min, max, ldexp, abs/neg.F64Rounding: trunc, ceil, floor, rndne, fract, frexp, cvt_f32 with omod, cvt_i32/u32.F64Conversions: cvt from f32/i32/u32, clamp, omod, literal, VOP3 VOP1 form with neg, 1/(2π) inline constant.both, outside the PR.
shaderDenormPreserveFloat64), so the testvectors exclude denormal inputs and results below 2^-1021. On RADV, 6 more batches that include denormals all match
the hardware.
Fma(CI lavapipe), add without quieting (RADV), min/max without sNaN quieting, min/max ±0, GLSL
TruncandRoundEven(lavapipe), no fract clamp, f32→f64 without quieting (RADV), no i32 saturation, no clamp, 1/(2π) as thewidened f32.
ctest: 146/147. The failure isagc_driver_pixel_helper_lanes(wave64 case) on thismachine's RADV, and it fails the same way on
mainwithout this change.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)