Skip to content

Translator: --f32-denorm-flush models the game MODE for f32 denormals (SHADER-377) - #2140

Draft
xxkliff wants to merge 1 commit into
ItsNotRexed:mainfrom
xxkliff:shader-377-f32-denorm-flush
Draft

xxkliff wants to merge 1 commit into
ItsNotRexed:mainfrom
xxkliff:shader-377-f32-denorm-flush

Conversation

@xxkliff

@xxkliff xxkliff commented Nov 22, 2026

Copy link
Copy Markdown

SHADER-377 (claimed by @xxkliff in #2078; finding by @Vonsek).

What

--f32-denorm-flush models the game's MODE (denorm_mode_32 = 0, f32 denormals flushed) in the translator. With it, a denormal f32 operand reads as a zero of its sign and a denormal v_mul_f32/v_fma_f32 result is flushed to a zero of its sign, for v_cmp_*_f32, v_max/min/med3/max3/min3_f32, v_mul_f32, v_fma_f32, v_cvt_flr_i32_f32 and v_sqrt_f32. It reuses Gen.flush_f32 (SHADER-118) through two small helpers, Gen.src_fm and Gen.res_fm.

Off by default: the corpus translation does not change. For the 31 fixtures in tests/semantic/fixtures/disasm that translate without a header, the SPIR-V is byte-identical before and after. make_game.py does not pass the option.

Tests

  • tests/semantic/test_f32_denorm_flush.py: 12 tests, each op run with the option off and on.
  • python -m unittest discover -s tests/semantic: 1683 tests, OK (2 skipped).
  • python -m compileall -q make_game.py tools tests: OK. test_docs_repo_paths, test_docs_list_items, test_docs_links pass.
  • test_docs_design_rows already fails on main (repeated rows, being fixed under PROC-75/PROC-97); the new row has a unique topic.

What this is not

  • No GPU run. Not tested on any GPU. The expected words are derived by hand from the rule in the SHADER-377 finding (gfx1036, tools/hw_oracle), not taken from a hardware run.
  • Assumption: v_fmac_f32, v_fmaak_f32 and v_fmamk_f32 follow v_fma_f32 (same FMA unit; not in the measured list). Written in the docs/design.md row.
  • Not covered (not measured): v_add_f32, v_sub_f32, v_subrev_f32, v_cvt_rpi_i32_f32, v_floor/ceil/fract_f32 (TEST-328), f16 and f64 ops. A test pins that v_add_f32 keeps its denormal.
  • No corpus count (no game files used). Whether to turn the flush on by default on 32-wide GPUs needs a GPU run and the instruction cost over the corpus.

AI-assisted (Claude). The contract row is in docs/design.md.

…ls (SHADER-377)

With the game's MODE (denorm_mode_32 = 0) RDNA2 reads a denormal f32 operand as a
zero of its sign and flushes a denormal v_mul_f32/v_fma_f32 result, while the
translator left denormals to the driver (NVIDIA keeps them). The new option applies
the rule, through the existing Gen.flush_f32, to v_cmp_*_f32, v_max/min/med3/max3/
min3_f32, v_mul_f32, v_fma_f32 (and v_fmac/fmaak/fmamk), v_cvt_flr_i32_f32 and
v_sqrt_f32. Off by default: the translation of the fixtures is byte-identical.

tests/semantic/test_f32_denorm_flush.py runs each case with the option off and on;
expected words are derived from the rule by hand, not from a GPU run. A contract
row in docs/design.md records the scope, the assumptions and what is not covered.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DeAwAMB8x9HUxUYPTxsWtt

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both the multiply and FMA paths flush the result before applying the output modifier. This loses subnormal values that the modifier should raise back into the normal range.

Comment thread tools/rdna2spirv.py
self.emit(f"{r} = {F2[op]} %float {x} {y}")
self.dst(a[0], self.tou(self.clamp_mods(r, mods)))
if op == "v_mul_f32":
r = self.res_fm(r)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Apply OMOD before flushing the multiply result. With --f32-denorm-flush, FLT_MIN * 0.5 is subnormal; an output modifier such as mul:2 should raise it back to FLT_MIN before output flushing. This preflush turns the value into zero first, so the modifier cannot recover it. RDNA2 specifies OMOD before output denormal flushing.

Comment thread tools/rdna2spirv.py
r = self.tmp()
self.emit_fma(r, x, y, z)
self.dst(a[0], self.tou(self.clamp_mods(r, mods)))
self.dst(a[0], self.tou(self.res_fm(self.clamp_mods(self.res_fm(r), mods))))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Apply OMOD before flushing the FMA result too. self.res_fm(r) flushes the FMA output before clamp_mods applies OMOD, so a subnormal FMA result scaled back into the normal range incorrectly becomes zero. Keep the output flush after the modifier, as required by the RDNA2 result order.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The order (flush before omod/clamp) follows Vonsek's gfx1036 measurement in #2938 ("a denormal intermediate the hardware flushes before the omod/clamp"), but I can't confirm it covers mul:2 on a denormal product. Not measured for v_mul/v_fma, so I'll mark it as an assumption in the design.md row. @Vonsek, could you run v_mul_f32_e64 v4, 0x00800000, 0.5 mul:2 on gfx1036? 0x00800000 means Flyander is right, 0 means the current order is.

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.

3 participants