Shaders: f16 results flush output denormals when MODE says so #3365
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3365
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@Lyrnc on GitHub · 2026-11-26 20:36 UTC · original
Lyrnc:fix/f16-output-denorm-flush→main· +338 −5 · 3 files · 1 commits · openWhat 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
@fuzeofficlal on GitHub · 2026-11-26 20:48 UTC · original · approved these changes
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.@Dukestar69 on GitHub · 2026-11-26 20:50 UTC · original · approved these changes
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).
@evan-kolberg on GitHub · 2026-11-26 20:58 UTC · original
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.
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.