Shaders: f16 results flush output denormals when MODE says so #3365
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3365"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-26T20:36:24Z.
What was wrong
With MODE bit 7 clear (bit 3 of
s_denorm_mode's immediate, ors_setregof the bit), RDNA2 flushes f16 output denormals. An f16 result that is below the smallest normal half, once rounded to a half's precision as if the exponent had no bottom, becomes a zero of its sign. The translator ignoress_denorm_modeand always keeps the subnormal.Measured on gfx1036 with
s_denorm_mode 4(16-bit input denormals kept, output denormals flushed).s_denorm_mode 0gives the same results for these f32-source cases.v_cvt_f16_f320x33800000 (2^-24), any round modev_cvt_f16_f320xb3800000v_cvt_f16_f320x387fe000 (2^-14 - 2^-25), nearest evenv_cvt_f16_f320x387ff000 (2^-14 - 2^-26), nearest evenv_cvt_f16_f320x387ff000, toward zerov_mul_f160x3c01 × 0x8001v_max_f160x0001, 0x8003The third and fourth rows show the rule. 0x387fe000 has 11 significant bits below 2^-14, so it flushes, even though rounding it onto the subnormal grid would give the smallest normal. 0x387ff000 rounds up to 2^-14 at a half's precision, so it is not flushed.
The same holds on gfx1036 for:
v_add_f16,v_sub_f16,v_mul_f16,v_fma_f16,v_fmac_f16,v_fract_f16andv_rcp_f16;v_pk_add_f16,v_pk_mul_f16andv_pk_fma_f16, per half;v_fma_mixlo_f16andv_cvt_pkrtz_f16_f32. The latter always rounds toward zero, so its limit is 2^-14.An exact model matches every measured result of the ten operations whose result is one rounding of an exact value:
v_cvt_f16_f32,v_fma_mixlo_f16of x × 1 + 0,v_add/sub/mul/fma/fmac_f16and the packed add/mul/fma. That is 44,808 of 44,808 results, in all 12 MODE settings below. The model:v_max_f16,v_min_f16,v_med3_f16andv_pk_max_f16do not flush their result: a subnormal operand comes out as it went in.s_setreg_imm32_b32 hwreg(HW_REG_MODE, 7, 1)sets the same bit. Writing 0 afters_denorm_mode 12flushes, and writing 1 afters_denorm_mode 0keeps denormals.What changed
round16does for the round mode becomesmode_at_blocks. It is the same function #3166 adds, with the same text.denorm16uses it to follow MODE bit 7. The bit starts as kept, the console's float mode.s_denorm_modeands_setregwith a constant write it. Ans_setregfrom an SGPR, or paths that disagree at a join, make it unknown, and an unknown bit keeps denormals as before.flush16(h, f, mode)turns a resulth, narrowed fromf, into a zero of its sign when the bit is clear and|f|is below the limit for the round mode: 0x387ff000 for nearest even (or an unknown mode), 0x38800000 toward zero, and 0x387fe001 for the direction that rounds the magnitude up. It runs at these places:valu16except min/max/med3;v_fma_mix{lo,hi}_f16;v_cvt_pkrtz_f16_f32, in toward-zero mode.v_fma_f16path decides on the exact sum, the f32 sum and its TwoSum error.narrow16itself is unchanged.emit/lower/denorm16.rs, becauselower.rswould otherwise pass the asset policy's 512 KiB file limit.lower.rsis now 521,855 bytes.Tests
valu_tests::f16_results_flush_output_denormals_as_the_mode_says, with rows measured on gfx1036:v_cvt_f16_f32withs_denorm_mode12, 4 and 0, in nearest-even and toward-zero mode, 12 inputs;v_add_f16,v_mul_f16,v_fma_f16,v_fract_f16,v_pk_mul_f16,v_fma_mixlo_f16andv_cvt_pkrtz_f16_f32withs_denorm_mode 4;v_max_f16andv_min_f16as controls that keep subnormals;s_setregMODE[7] write both ways.s_denorm_mode 4, round 0, 0x33800000).run_cases.cargo test -p victory-shader --lib, lib tests (cargo test -p victory-shader --lib -- --test-threads=1 --skip heap_direct_texture_reads_mark_their_heap_index; that test crashes the process on main too): 297 passed, 5 failed, 3 ignored. main63bcca4on this machine: 296 passed and the same 5 failed (rect::tests::rectangles_get_their_fourth_corner,stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride,stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,synthetic_tests::gathers_at_a_level_match_the_hardware,tests::elements_sharing_a_row_keep_their_order).cargo clippy -p victory-shader --lib --tests -- -D warnings: clean.cargo xtask policy: ok (192 files). The new code is inlower/denorm16.rs, because adding it tolower.rstakes that file past the policy's 524,288-byte limit.Hardware comparison
A probe runs the same instruction words on the gfx1036 hw oracle and through the translator (a local copy of the
build_valu_csharness, run on the Radeon). It covers 22 f16-producing operations, 96 to 110 rows of 3 results each, in 12 MODE settings:s_denorm_mode12 and 0 in all four round modes, and 4 and 8 in nearest-even and toward-zero mode. The table counts results that differ from the hardware. NaNs with another payload count as equal.s_denorm_mode 4(outputs flushed, inputs kept), nearest even, the 13 flushing ops: 3,870s_denorm_mode 4, toward zero, the same ops: 3,870s_denorm_mode 0, f32-source ops (v_cvt_f16_f32,v_fma_mixlo_f16,v_cvt_pkrtz_f16_f32), nearest even and toward zero: 1,980s_denorm_mode 4,v_max/min/med3_f16,v_pk_max_f16: 2,304s_denorm_mode 12), all ops and round modes: 25,848All differences left in the
s_denorm_mode 4rows are also there with denormals kept, so none of them is a flush. They are:v_fma_mixlo_f16zeros of f32-denormal or −0 sources, where main gives −0 and gfx1036 gives +0, in every mode;v_rcp_f1690,v_fma_f16/v_fmac_f1634,v_pk_fma_f1633,v_sub_f165,v_add_f162,v_pk_add_f164.The RTX 5080 (610.88) ran the same probe for
v_cvt_f16_f32,v_add_f16,v_mul_f16,v_fma_f16,v_pk_mul_f16,v_fma_mixlo_f16,v_cvt_pkrtz_f16_f32and thes_setregcases. Every case gives the same count as on the Radeon, on main and with this PR.What this does not change
s_denorm_mode0, 8, 3), gfx1036 also reads subnormal f16 sources as zeros. This affects the arithmetic, min/max/med3,v_floor/ceil_f16,v_sqrt/rcp_f16andv_cvt_f32_f16. For example,v_cvt_f32_f16of 0x0001 gives 0 andv_floor_f16of 0x8001 gives -0. This PR does not model that. Unders_denorm_mode 0, results with subnormal inputs still differ: 4,947 of 25,848 here, down from 7,722. #3166 does it forv_cvt_norm_*16_f16, and a general input flush could follow the same analysis.v_cvt_f16_f32is #3159. With #3159 merged onto this PR,v_cvt_f16_f32matches gfx1036 on all 3,960 results in the 12 MODE settings; main has 936 differences there. That works becauseflush16uses the limit of the round mode, so it agrees with #3159's directed narrowing.Merging with related PRs
float_result. The resolution isnarrow16_in_modeforv_cvt_f16_f32, thenflush16. With that resolution the combined result is the one above.Lowerfields and initialiser only.mode_at_blocksis identical.round16/ the run loop and in the packed fallback.Not tested
v_interp_p2_f16and the 16-bit input narrowing ininterp: not measured, and left unchanged.v_exp_f16,v_log_f16,v_sin/cos_f16,v_max3/min3_f16: no subnormal results in the probe. They follow the arithmetic and the min/max rule respectively, which is an assumption.AI use
Claude Code (Claude Opus 5.5) wrote the probes, the model, the fix and the test, and ran them on the GPUs above. The numbers come from those runs. No game data was used.
🤖 Generated with Claude Code
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T20:48:23Z.
Review: APPROVED
LGTM —
denorm16_after,flush16, andflush16_exactcleanly model RDNA2MODE[7](s_denorm_modebit 3 andHW_REG_MODEbit 7) output denormal flushing while preserving sign bits, NaN/Inf, and thev_min/v_max/v_med3/v_pk_min/v_pk_maxexception, and moving the helpers intoemit/lower/denorm16.rskeepslower.rswithin the size limit.Verified
crates/victory-shader/src/emit/lower.rssize: 521,855 bytes (under the 524,288-byte limit).cargo run --locked -p xtask -- policy:192 files ok.cargo clippy --locked -p victory-shader --all-targets -- -D warnings(default &--all-features): clean.rustfmt --edition 2024: clean on all PR-touched hunks andemit/lower/denorm16.rs.emit::valu_tests::f16_results_flush_output_denormals_as_the_mode_says(RTX 5090 + WARP): passed; related f16 regression tests (v_fma_f16,f16_max_and_min,v_cvt_f16,packed_fmac,v_med3_f16): 5 passed, 0 failed;emit::lower: 33 passed, 0 failed.Non-blocking scope notes (matching the PR's stated follow-ups)
flush16_exact(denorm16.rs:91) assumes RNE (mode16 != Some(3)), and directed rounding modes (1 | 2), intermediate f32 double rounding inv_fmac/v_pk_fma/v_fma_mix*/ RTZv_fma_f16,v_interp_p2_f16, whole-wave section writes, and SGPR/divergentMODEjoins (Nonedefaulting to keep) remain follow-ups.Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T20:50:32Z.
Review: APPROVED
LGTM — verified on RTX 5090 and
VICTORY_WARP=1(Windows / D3D12) againstorigin/main(63bcca4).MODEsetting (cleanly factored intoemit/lower/denorm16.rs) matches RDNA2 hardware behavior on both discrete NVIDIA hardware and WARP.cargo test -p victory-shader --lib emit::valu_tests::f16_results_flush_output_denormals_as_the_mode_says -- --test-threads=1: passed on RTX 5090 andVICTORY_WARP=1.cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 192 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by evan-kolberg on 2026-11-26T20:58:23Z.
Independent M4 Pro pixel-stage result for Lyrnc’s #3365 lowering. On CrossOver 26.3 and D3DMetal 4.0 beta 3, Mac main
bf99e728differed from the gfx1036 output-mode oracle at 64/256 pixels. The same Mac base with2c7372e’s production code matched 256/256. Exact #3365 change · Exact Mac baseThe generated fragment shader used
v_cvt_f16_f32with nearest-even rounding and MODE immediates 12 and 4. In mode 4 the patch changed0x8001 → 0x8000for negative minimum subnormal and0x0400 → 0x0000for FP320x387fefff. One FP32 ULP higher (0x387ff000) stayed0x0400. RGBA32F readback recorded the raw half word as an exact FP32 integer and echoed the original runtime FP32 input. The fixture adapted Capt-Speed’s pixel-shader builder and VastyViz’s fullscreen triangle. Capt-Speed’s builder · VastyViz’s trianglePrepared and tested with Codex.
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T21:18:35Z.
I combined
2c7372ewith the published #2623/#3192/#3364 resolution in94e8860. The newv_fmac_f16fixture checks signed subnormal results, two exact products at the rounding boundary and a product close enough to round up before the flush decision. It checks the destination's upper half as well.The control passes all five cases with
s_denorm_mode 12. Withs_denorm_mode 4, it differs in four cases. The combined commit passes all ten cases on app-local WARP in three runs. The affected VALU, semantics, lowering and IR suite had 123 passed and one ignored. Strict Clippy in both configurations and the asset policy check passed. The rounding analysis and your output-denormal handling sharefp16_mode.rs, bringinglower.rsto 522,429 bytes.@Lyrnc, if you rerun the combined fixture on gfx1036, the new test is
v_fmac_f16_flushes_the_exact_result_in_output_denorm_mode. If you take the integration or test into another commit, please keep the Git author or useCo-authored-by: andrewpozdnakov7-jpg <[email protected]>.Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T21:32:53Z.
The input MODE change from #3166 and this output change also work in the same shader. Combined commit
a6ba20eshares the MODE propagation while keeping the input analysis lazy. The test reads a subnormal in a normalized conversion and creates a signed subnormal withv_fmac_f16using normal inputs. Both output words match for both signs under all four MODE combinations on WARP. Test details in #3166.If you fold the shared MODE resolution or the test into another commit, please keep the Git author or use
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T13:31:59Z.
I combined your exact
2c7372ewith #37386a4a505in6d7a038. Git merged the files cleanly and kept both commits as direct parents. The comparison change removes f32 subnormal checks on widened f16 inputs. Your output-mode logic still governs which f16 results are flushed.Against your head, the combined branch reproduced all 851,968 output words bit for bit on Microsoft WARP 1.0.22 with Agility 1.620.2. The inputs cover every half bit pattern, both destination halves, clamp and source modifiers. Your f16 output-MODE regression, the signed-zero min/max test and the f16 CPU-model test all passed. A single min3 or max3 shader went from 132 to 108 written DXIL instructions. Strict all-feature shader Clippy and the asset policy passed.
@Lyrnc, the integration tree is ready for merge coordination. Please retain Git authorship when cherry-picking or rebasing. If folding #3738 into another commit, please add
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.AI-assisted with OpenAI Codex.
Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-28T15:30:36Z.
NVIDIA row for this PR: it passes on Ampere as well as on the RTX 5080, and the new test fails on this card without the fix.
RTX 3070 (desktop), driver 616.56, Windows 11 Pro 10.0.26100, debug. Head
2c7372eon main63bcca4(one commit, clean).valu_tests::f16_results_flush_output_denormals_as_the_mode_says: 1 passed, 0 failed, in 3 of 3 runs. It ran on both the hardware adapter and WARP: no "skipped" line. One extra run withVICTORY_WARP=1, where every device is WARP, also passed.Negative control: the same test with main's
crates/victory-shader/src/emit/lower.rs(git diff --stat HEAD: 1 file, 5 insertions, 38 deletions;lower/denorm16.rsis then no longer compiled) fails in 3 of 3 runs, on the hardware adapter, at the row the description names:hardware: s_denorm_mode 4, round 0, 0x33800000gave0xabcd0001, want0xabcd0000. On this card main keeps the subnormal0x0001where gfx1036 flushes it to0x0000, the same as the description's table. TheVICTORY_WARP=1control fails at the same row with the same value. Its message still says "hardware", but that run is WARP.cargo test --locked -p victory-shader --lib -- --skip heap_direct_texture_reads_mark_their_heap_index(default threads):63bcca4: 298 passed, 3 failed, 3 ignored.The 3 failures are the same on both:
stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride,stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zerosandsynthetic_tests::gathers_at_a_level_match_the_hardware. The description's other two failures,rect::tests::rectangles_get_their_fourth_cornerandtests::elements_sharing_a_row_keep_their_order, pass here on both. The skipped test crashes the binary on this card (open issue #2571).Nothing NVIDIA-specific: on Ampere the test passes with the fix and fails without it, as on the RTX 5080.
Not tested: a title run; the hardware probe over the 12 MODE settings; the combinations with #3159, #3166 or #3738.
AI-assisted (Claude Code, Claude Opus 5.5).
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T23:14:36Z.
I resolved the overlap between
2c7372eand the directedv_mul_f16rounding in #3804. Combinedab1bb23keeps both exact commits as parents. The multiply narrows with the active rounding mode, then uses this PR's output flush in the same mode.A regression checks 12 signed operand pairs in each of the four rounding modes with output denormals kept and flushed. With just
2c7372e, a kept negative product rounded toward +∞ gives0x8400instead of0x83ff. With just #3804, a product below the normal range remains0x03ffin flush mode instead of0x0000. The combined build matched all 192 results across two app-local Microsoft WARP invocations, including a product between the nearest-even and directed flush thresholds. The existing denormal and multiply tests, 33 lowerer tests, both strict Clippy configurations and the asset policy passed. The combinedlower.rsis 523,009 bytes.The next step is maintainer review and confirmation on a native Radeon. Please retain the Git author on cherry-pick or rebase, or add
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>if folding the change into another commit.AI-assisted with OpenAI Codex.
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T23:40:57Z.
ColorMeBoom confirmed zero mismatches for all 2,116 multiply inputs on an RX 5700 XT and WARP at #3804's exact
386cc0fhead. The full shader suite had 303 passed and three ignored. Native reportThe remaining native check here is
emit::valu_tests::f16_multiply_combines_rounding_and_output_denorm_modeon the combinedab1bb23branch. It uses generated operands at the normal/subnormal boundary and exercises both denormal modes. The combined check passed locally on WARP.AI-assisted with OpenAI Codex.
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T23:57:16Z.
I resolved the packed multiply overlap between the earlier scalar integration and #3811. Combined
62dfd02keeps both exact histories. Each packed half is narrowed using the active round mode before this PR applies the corresponding output flush threshold.The new test puts positive and negative products in the same packed word at six points around the smallest normal half. It covers all four round modes with output denormals both kept and flushed. The earlier scalar integration returns
0x84000400instead of0x83ff0400in the kept mode 1 case. The combined build matched all 96 packed readbacks on two software invocations. The scalar boundary regression, original denormal test, packed rounding checks, 33 lowerer tests, both strict Clippy configurations and the asset policy passed.lower.rsis 523,138 bytes.The next step is maintainer review and a native Radeon repeat of
packed_f16_multiply_combines_rounding_and_output_denorm_mode. Please retain the Git author on cherry-pick or rebase, or addCo-authored-by: andrewpozdnakov7-jpg <[email protected]>if folding the regression into another commit.AI-assisted with OpenAI Codex.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.