Skip to content

rdna2spirv: gate the binary64 conversion, frexp and div_fixup output modifiers by MODE - #6314

Open
fiestorra wants to merge 1 commit into
ItsNotRexed:mainfrom
fiestorra:f64-omod-conversions
Open

fiestorra wants to merge 1 commit into
ItsNotRexed:mainfrom
fiestorra:f64-omod-conversions

Conversation

@fiestorra

Copy link
Copy Markdown
Contributor

What this fixes

docs/design.md already encodes the RDNA2 rule for output modifiers in two places (the f32 rounding row and the row "Output modifiers are gated by RDNA2 MODEs", which says the omod applies only when MODE.ieee == 0 and the output denormals of the destination precision are flushed). Three rows still state the opposite for the binary64 ops, and two of them say the behaviour was never measured on hardware:

  • "32-bit integers to binary64" asserts "Output scaling precedes clamping", confirmed only by SPIRV-Tools constant folding.
  • "v_div_fixup_f16, v_div_fixup_f64" says the output modifiers "apply" and is marked "Not measured on RDNA2 hardware".
  • "v_frexp_mant_f64, v_frexp_exp_i32_f64" says the modifiers "apply … via clamp_mods_f64" while its own text notes denorm_mode_16_64=3 "ignoring omod", and is marked "Not measured on hardware".

tools/rdna2spirv.py follows the unmeasured wording: it feeds mul:2/mul:4/div:2 to dst_f64 / clamp_mods_f64 for these ops, which emits OpCapability Float64 + OpFMul %double + FClamp for a scaling that the game's MODE discards.

What I measured

tools/f64_omod_probe.py (new, stdlib only, tools/hw_oracle) on my gfx1036 iGPU (AMD Radeon(TM) Graphics, the 2-CU part of a 7800X3D, Windows driver 31.0.24002.92): 35 cases per op x 5 MODEs x 5 modifier forms, run at wave32 and wave64.

hardware oracle: device=AMD Radeon(TM) Graphics arch=gfx1036
175 cases across 5 mode(s) x 5 modifier forms x 2 wave(s): scaling_gated_on=129, scaling_gated_off=396,
scaling_changed_value=99, clamp_changed_value=90, mismatches=0, wave_diffs=0

Every expectation is derived from the plain (no-modifier) result measured in the same MODE on the same device, so the probe compares the effect of the modifier rather than re-deriving the operation. Results, all with wave32 and wave64 identical word for word:

op (destination) mul:2/mul:4/div:2 clamp
v_cvt_f64_i32, v_cvt_f64_u32 (f64) applied only in IEEE=0, denorm_mode_16_64=0; ignored in denorm_mode_16_64=3 and in both IEEE=1 MODEs applied in every MODE
v_cvt_f32_f64 (f32) applied in IEEE=0 with denorm32=0 — including denorm_mode_16_64=3 — and ignored only with denorm32=3 or IEEE=1 applied in every MODE
v_frexp_mant_f64 (f64) as the first row applied in every MODE (it changes the value only for the negative mantissa, v_frexp_mant_f64(-1.75) = -0.875 -> +0.0)
v_div_fixup_f64, v_div_fixup_f16 as the first row (for f16, only bits [15:0] of the destination are written on either wave) applied in every MODE

So the gate is per result precision, exactly as the f32 row and MODEs row already describe: fp32_denorm for an f32 destination, the 16/64-bit denormal mode for f16 and f64 destinations. v_cvt_f32_f64 is the case that proves it: its operands are binary64, but its destination is f32 and it scales under denorm_mode_16_64 = 3.

Code change

Four sites, all inside the branches of those ops: v_cvt_f64_i32/v_cvt_f64_u32 pass only clamp to dst_f64; the v_div_fixup_* branch keys its Float64/FClamp path on clamp alone (has_clamp); the two clamp_mods_f64 calls for v_div_fixup_f64 and v_frexp_mant_f64 filter the scaling out; the uses_f64 gate for v_frexp_mant_f64 is raised by clamp only. v_cvt_f32_f64 keeps its full modifier handling (correct, since its destination is f32) and the v_frexp_exp_i32_f64 rejection is untouched. The existing clamp_f16 helper already passed allow_omod=False, so the f16 path needed no change.

Tests

  • tests/semantic/test_int32_to_f64.py: the clamp case now asserts FClamp on the converted value with no OpFMul %double; the pure-scaling case asserts that neither OpFMul %double nor FClamp is emitted.
  • tests/semantic/test_alu_frexp_f64.py: mul:2/mul:4/div:2 are kept as accepted ISA forms that emit no OpFMul %double, and their values are checked against the hardware result (v_frexp_mant_f64(3.0) = 0.75, -1.75 -> -0.875); only clamp forms enable Float64/FClamp.
  • tests/semantic/test_alu_div_fixup.py: new case for both f64 and f16 with scaling only: no OpCapability Float64, no OpFMul %double, no FClamp, no UnpackHalf2x16, and the destination keeps the unmodified fixup value.
  • tests/test_f64_omod_probe.py: gate model, a simulated oracle that follows the gate (exit 0), a device that scales regardless of MODE (exit 1, with the MODE named), a wave32/wave64 difference (exit 1), 77 for an unavailable or non-GFX10 oracle, and the CLI argument check.

Run here: docs checks 33 OK; test_*frexp* 246 OK; test_*cvt* 173 OK; test_*omod* 9 OK; test_alu_div_fixup 52 OK; test_f64_omod_probe 8 OK; tools/f64_omod_probe.py --self-test OK and the hardware run above exits 0.

Corpus

Counted over the whole corpus (E:\leonida-work\shaders\dxbc\disasm, 26,503 listings): v_cvt_f64_i32 0, v_cvt_f64_u32 0, v_frexp_mant_f64 0, v_frexp_exp_i32_f64 0, v_div_fixup_f16 0, v_div_fixup_f64 0; v_cvt_f32_f64 36 occurrences in 5 listings, all _e32 and none with an output modifier (v_add_f64: 216 in 5 listings, also none). No listing uses any op whose path this PR changes, so no corpus output changes; that is also why no GPU fixture is added here and the claim is carried by the probe instead.

Related

Claimed in #5256 (my claim line for this item); a status: update with these numbers follows there. Overlaps #5268 in tools/rdna2spirv.py only in the sense that it touches the v_rcp_f64/v_rsq_f64/v_sqrt_f64 branches, which I leave alone; if #5268 lands first I will rebase. Part of the same finding also shows up in #4371, where I grouped v_cvt_f32_f64 with the binary64 destinations and filtered its modifiers to clamp — that grouping is wrong for exactly the reason this PR measures, and I am correcting it on that branch.

…modifiers by MODE

Measured on a gfx1036 iGPU (driver 31.0.24002.92) through tools/hw_oracle with the new
tools/f64_omod_probe.py: 35 cases per op x 5 MODEs x 5 modifier forms at wave32 and wave64,
175 cases, 0 mismatches, 0 wave differences. mul:2/mul:4/div:2 scale the f16 and binary64
results only with IEEE=0 and denorm_mode_16_64=0; under denorm_mode_16_64=3 they are ignored
while clamp still applies in every MODE. v_cvt_f32_f64 writes an f32 result and follows the
f32 gate (denorm32), not the 16/64-bit one. The translator now emits Float64/FMul/FClamp for
these ops only when clamp is requested, and the three docs rows that claimed the scaling
applied and was unmeasured state the gate with the measurement that confirmed it.
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