[pr-performance][shaders][AMD] New switch DARNER_LEGACY_MIN (off): legacy multiply tests min(|a|,|b|) == 0 for every factor pair #10

Open
Jesper-A wants to merge 2 commits from Jesper-A/Darner:pr5-legacy-mul into main
Contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

Re-filed from CodeFloe #342 (the CodeFloe repo is gone; nothing was migrated). Same branch and commit.

Base: the branch (2 commits) is on f2bccf656, the main commit it was cut from. main is now dd2f061d5. Not rebased (we rebase only if you ask). A text-only trial merge (git merge-tree --write-tree trustno1/main pr5-legacy-mul) conflicts in docs/findings.md only; emit/lower.rs and README.md merge cleanly.

New switch DARNER_LEGACY_MIN, off by default (1 = on). Off, the output is what main emits today.

On AMD RDNA hosts the legacy multiply (a zero times anything is zero, as in D3D9) tests one factor for zero when that factor is known finite: a == 0 ? 0 : a * b. With the switch on, it tests min(|a|, |b|) == 0 ? 0 : a * b for every pair (DXIL FAbs 6 and FMin 36, both already emitted there). Per the author of the port, Mesa's NIR folds only the min form into one instruction (v_mul_dx9_zero_f32 on RDNA); the finite-factor test stayed a compare and a select after the multiply. We did not check that ourselves.

Where: Lower::legacy_mul in crates/darner-shader/src/emit/lower.rs (the RDNA branch only; hosts that are not RDNA, or not forced with DARNER_HOST_RDNA=1, never reach it). The choice is the small function legacy_zero_test in emit/cost.rs, which has its own tests; the switch is Rewrite::LegacyMin in the same file's SWITCHES and OFF_BY_DEFAULT. It is documented in the README switch table, the cost.rs module doc and docs/findings.md.

Side effect with the switch on: the zero is +0 in every case, as on the console. Off, the product of a finite non-zero factor and a zero can be -0. A new test, legacy_min_form_is_the_legacy_product in emit/cost.rs, checks the min formula in Rust (0, -0, inf, NaN, denormals) against the D3D9 rule. It does not check emitted DXIL.

Shader cache: this base has no TRANSLATOR_VERSION. build.rs collects every "DARNER_*" name in the translator source into DARNER_SHADER_SWITCHES, and translation_fingerprint (emit/mod.rs) hashes each switch's value, so turning the switch on or off re-translates every shader. The source hash is part of the same fingerprint, so the first start after this lands also translates again once, with the switch off and the same output.

Credit: port of lobomfz/VIctory-xmem PR #257 (author Jojonobody2), commit 47f5223c3. That is a PR in the xmem fork, not a Darner number. The commit keeps Co-authored-by: Jojonobody2. The switch and the off-by-default form are ours.

The author's numbers, theirs and not measured here (RX 7900 XTX, RADV shader stats, instructions without s_delay_alu): 13,918 to 13,626; 35,684 to 35,217; 3,845 to 3,760, in three programs. No speed claim is made. The gain on AMD's Windows driver is unverified.

Not a bug fix, so no Fixes/Refs.

Spec dependence

May depend on the driver and the GPU. The instruction saving was reported by the author on Mesa/RADV only; whether the Windows AMD driver fuses the same form is unknown. NVIDIA: the RDNA branch is not taken on NVIDIA unless DARNER_HOST_RDNA=1 forces it, so the default run on the RTX 2070 SUPER (Turing) is untouched. If forced, the min form is two FAbs and one FMin where the finite-factor form is one compare; whether NVIDIA's compiler turns that into a cost or a gain is untested. Off by default for that reason. Untested on any real GPU.

How it was tested

  • Tested revision: 91eefe7a5 plus the switch change in the working tree; 01c3f3569 differs from that only by a doc-comment row in emit/cost.rs (per the porter), and tests and clippy were not re-run on it. cargo xtask policy passed on 01c3f3569. Base f2bccf656 (not rebased on the current main, dd2f061d5).
  • Commands and results:
    • cargo test -p darner-shader --lib --locked -- --test-threads 1 --skip in_the_pair_form_a_half_that_left --skip legacy_mul_forms_are_exact: 547 passed, 4 failed, 15 ignored. The two legacy-min tests (legacy_min_off_keeps_the_one_factor_test, legacy_min_form_is_the_legacy_product) pass. The skipped in_the_pair_form_a_half_that_left hangs on main too, as reported by the porter; legacy_mul_forms_are_exact is a 2^32-float loop, too slow in debug.
    • The 4 failures are reported to fail on trustno1/main as well (reported to me, not re-run by me on main): emit::barycentric_tests::first_half_values_render_on_hardware_and_warp, emit::rect::tests::rectangles_are_drawn_whole, emit::rect::tests::the_rectangle_stage_is_a_geometry_shader, emit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders.
    • cargo clippy -p darner-shader --all-targets --all-features -- -D warnings -A clippy::unnecessary_lazy_evaluations: clean, run before the doc-only edit (that one lint is allowed; it is not from this change).
    • cargo xtask policy: passed on 01c3f3569 (531 files ok).
  • The three gates: cargo clippy --workspace --all-targets --locked -- -D warnings: pending. The same with --all-features: pending (only the crate-level run above was done). cargo test --workspace --locked: pending. There is no CI upstream, and nothing here claims one.
  • Gameplay, when relevant: none, no GPU run. GPU, profile, resolution, DLSS, frame generation, ray tracing: none.
  • Before and after, when relevant: none, no GPU run.
  • Shader wave sizes tested, when relevant: none, no GPU run; the change is in the RDNA branch of the shared lowering and is off by default.
  • Coverage gaps: legacy_mul_forms_are_exact (the exhaustive oracle) was skipped, so it did not run on this commit; no run on a real driver; no measurement of the instruction count or of speed with the switch on; shader-cache cold start not timed; the DXIL emitted with the switch on is not checked by a test, only the choice function and the formula.

Checklist

  • This PR contains one change based on main (base f2bccf656).
  • The diff follows docs/asset-policy.md (cargo xtask policy passed on 01c3f3569).
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none found for DARNER_LEGACY_MIN or the legacy multiply. On 2026-12-03 this host had 3 issues and 5 pull requests in total (#3 by another contributor; #5 to #8 are ours, re-filed today); none is about shaders. The one open pull request (#3, ray tracing mode in F1) changes host/menu.rs, host/panel.rs and hwrt.rs: none of this change's files. Earlier, on CodeFloe (not migrated), two pull requests touched emit/lower.rs: CodeFloe #295 (assumed_finite, DARNER_LEGACY_FINITE; it changed the fa/fb line just above this hunk, so more factors would count as finite and this switch would apply to more multiplies; the hunks were a few lines apart and might conflict; never tried merging both) and CodeFloe #183 (wave64 ballots; no overlap seen in the legacy multiply). Neither is on this host, and DARNER_LEGACY_FINITE is not on main (dd2f061d5).
  • Our own re-filed PRs that touch the same files: #8 (texture LOD bias) and #6 (radial wheel hold), both README.md and docs/findings.md, and #7 (long frames: docs/findings.md). Different changes; whichever lands first, the others take the new rows when they are rebased.

This change was made with AI assistance (Claude Code).

## Contributor role (optional) - Fluxer ID: ## What it changes Re-filed from CodeFloe #342 (the CodeFloe repo is gone; nothing was migrated). Same branch and commit. **Base:** the branch (2 commits) is on `f2bccf656`, the `main` commit it was cut from. `main` is now `dd2f061d5`. Not rebased (we rebase only if you ask). A text-only trial merge (`git merge-tree --write-tree trustno1/main pr5-legacy-mul`) conflicts in `docs/findings.md` only; `emit/lower.rs` and `README.md` merge cleanly. New switch `DARNER_LEGACY_MIN`, **off by default** (`1` = on). Off, the output is what `main` emits today. On AMD RDNA hosts the legacy multiply (a zero times anything is zero, as in D3D9) tests one factor for zero when that factor is known finite: `a == 0 ? 0 : a * b`. With the switch on, it tests `min(|a|, |b|) == 0 ? 0 : a * b` for every pair (DXIL `FAbs` 6 and `FMin` 36, both already emitted there). Per the author of the port, Mesa's NIR folds only the min form into one instruction (`v_mul_dx9_zero_f32` on RDNA); the finite-factor test stayed a compare and a select after the multiply. We did not check that ourselves. Where: `Lower::legacy_mul` in `crates/darner-shader/src/emit/lower.rs` (the RDNA branch only; hosts that are not RDNA, or not forced with `DARNER_HOST_RDNA=1`, never reach it). The choice is the small function `legacy_zero_test` in `emit/cost.rs`, which has its own tests; the switch is `Rewrite::LegacyMin` in the same file's `SWITCHES` and `OFF_BY_DEFAULT`. It is documented in the README switch table, the `cost.rs` module doc and `docs/findings.md`. Side effect with the switch on: the zero is +0 in every case, as on the console. Off, the product of a finite non-zero factor and a zero can be -0. A new test, `legacy_min_form_is_the_legacy_product` in `emit/cost.rs`, checks the min formula in Rust (0, -0, inf, NaN, denormals) against the D3D9 rule. It does not check emitted DXIL. Shader cache: this base has no `TRANSLATOR_VERSION`. `build.rs` collects every `"DARNER_*"` name in the translator source into `DARNER_SHADER_SWITCHES`, and `translation_fingerprint` (`emit/mod.rs`) hashes each switch's value, so turning the switch on or off re-translates every shader. The source hash is part of the same fingerprint, so the first start after this lands also translates again once, with the switch off and the same output. Credit: port of lobomfz/VIctory-xmem PR #257 (author Jojonobody2), commit 47f5223c3. That is a PR in the xmem fork, not a Darner number. The commit keeps `Co-authored-by: Jojonobody2`. The switch and the off-by-default form are ours. The author's numbers, theirs and not measured here (RX 7900 XTX, RADV shader stats, instructions without `s_delay_alu`): 13,918 to 13,626; 35,684 to 35,217; 3,845 to 3,760, in three programs. **No speed claim is made.** The gain on AMD's Windows driver is unverified. Not a bug fix, so no `Fixes`/`Refs`. ### Spec dependence May depend on the driver and the GPU. The instruction saving was reported by the author on Mesa/RADV only; whether the Windows AMD driver fuses the same form is unknown. NVIDIA: the RDNA branch is not taken on NVIDIA unless `DARNER_HOST_RDNA=1` forces it, so the default run on the RTX 2070 SUPER (Turing) is untouched. If forced, the min form is two `FAbs` and one `FMin` where the finite-factor form is one compare; whether NVIDIA's compiler turns that into a cost or a gain is untested. Off by default for that reason. Untested on any real GPU. ## How it was tested - Tested revision: 91eefe7a5 plus the switch change in the working tree; `01c3f3569` differs from that only by a doc-comment row in `emit/cost.rs` (per the porter), and tests and clippy were not re-run on it. `cargo xtask policy` passed on 01c3f3569. Base f2bccf656 (not rebased on the current `main`, `dd2f061d5`). - Commands and results: - `cargo test -p darner-shader --lib --locked -- --test-threads 1 --skip in_the_pair_form_a_half_that_left --skip legacy_mul_forms_are_exact`: 547 passed, 4 failed, 15 ignored. The two legacy-min tests (`legacy_min_off_keeps_the_one_factor_test`, `legacy_min_form_is_the_legacy_product`) pass. The skipped `in_the_pair_form_a_half_that_left` hangs on `main` too, as reported by the porter; `legacy_mul_forms_are_exact` is a 2^32-float loop, too slow in debug. - The 4 failures are reported to fail on `trustno1/main` as well (reported to me, not re-run by me on `main`): `emit::barycentric_tests::first_half_values_render_on_hardware_and_warp`, `emit::rect::tests::rectangles_are_drawn_whole`, `emit::rect::tests::the_rectangle_stage_is_a_geometry_shader`, `emit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders`. - `cargo clippy -p darner-shader --all-targets --all-features -- -D warnings -A clippy::unnecessary_lazy_evaluations`: clean, run before the doc-only edit (that one lint is allowed; it is not from this change). - `cargo xtask policy`: passed on 01c3f3569 (531 files ok). - The three gates: `cargo clippy --workspace --all-targets --locked -- -D warnings`: pending. The same with `--all-features`: pending (only the crate-level run above was done). `cargo test --workspace --locked`: pending. There is no CI upstream, and nothing here claims one. - Gameplay, when relevant: none, no GPU run. GPU, profile, resolution, DLSS, frame generation, ray tracing: none. - Before and after, when relevant: none, no GPU run. - Shader wave sizes tested, when relevant: none, no GPU run; the change is in the RDNA branch of the shared lowering and is off by default. - Coverage gaps: `legacy_mul_forms_are_exact` (the exhaustive oracle) was skipped, so it did not run on this commit; no run on a real driver; no measurement of the instruction count or of speed with the switch on; shader-cache cold start not timed; the DXIL emitted with the switch on is not checked by a test, only the choice function and the formula. ## Checklist - [x] This PR contains one change based on `main` (base f2bccf656). - [x] The diff follows `docs/asset-policy.md` (`cargo xtask policy` passed on 01c3f3569). - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none found for `DARNER_LEGACY_MIN` or the legacy multiply. On 2026-12-03 this host had 3 issues and 5 pull requests in total (#3 by another contributor; #5 to #8 are ours, re-filed today); none is about shaders. The one open pull request (#3, ray tracing mode in F1) changes `host/menu.rs`, `host/panel.rs` and `hwrt.rs`: none of this change's files. Earlier, on CodeFloe (not migrated), two pull requests touched `emit/lower.rs`: CodeFloe #295 (`assumed_finite`, `DARNER_LEGACY_FINITE`; it changed the `fa`/`fb` line just above this hunk, so more factors would count as finite and this switch would apply to more multiplies; the hunks were a few lines apart and might conflict; never tried merging both) and CodeFloe #183 (wave64 ballots; no overlap seen in the legacy multiply). Neither is on this host, and `DARNER_LEGACY_FINITE` is not on `main` (`dd2f061d5`). - Our own re-filed PRs that touch the same files: #8 (texture LOD bias) and #6 (radial wheel hold), both `README.md` and `docs/findings.md`, and #7 (long frames: `docs/findings.md`). Different changes; whichever lands first, the others take the new rows when they are rebased. This change was made with AI assistance (Claude Code).
Where one factor was known finite, the zero test was of that factor alone
(`a == 0 ? 0 : a * b`). Mesa's NIR folds only `min(|a|,|b|) == 0 ? 0 : a * b`
into its fmulz (RDNA's `v_mul_dx9_zero_f32`), so the other form stayed a
compare and a select after the multiply. The lowering now always uses the
min form (dx.op FAbs 6 and FMin 36, both already emitted here). The zero is
+0 as the console's in every case; before, the product of a zero and a
finite value could be -0. New test legacy_min_form_is_the_legacy_product
covers 0, -0, inf, NaN and denormals against the D3D9 rule.

Port of lobomfz/VIctory-xmem PR #257 (author Jojonobody2), commit 47f5223c3.
The author's numbers (RX 7900 XTX, RADV shader stats, instructions without
s_delay_alu), theirs, not measured here: 13,918 -> 13,626; 35,684 -> 35,217;
3,845 -> 3,760 in three programs. The gain on Windows AMD-proprietary and
NVIDIA drivers is unverified.

Shader cache: this base has no TRANSLATOR_VERSION; the source hash is part
of translation_fingerprint, so every cached shader translates again.

Tests: darner-shader --lib, skipping the hanging wave64 pair test and the
2^32-float oracle64::legacy_mul_forms_are_exact (debug-slow, not touched):
546 passed, 4 failed (the 4 known on trustno1/main), 15 ignored.

Co-authored-by: Jojonobody2 <[email protected]>

This change was made with AI assistance (Claude Code).
The previous commit made the legacy multiply test min(|a|, |b|) == 0 for
every factor pair (from lobomfz/VIctory-xmem PR #257, by Jojonobody2). It
now does so only with DARNER_LEGACY_MIN=1; off, the translation is what
trustno1/main emits (a known finite factor tested alone, same zero sign).
The code is only reached on AMD RDNA hosts (or DARNER_HOST_RDNA=1).

The switch is a Rewrite of emit::cost, so build.rs lists it in
DARNER_SHADER_SWITCHES and its value joins the translation fingerprint and
the shader cache key. Documented in cost.rs, README and docs/findings.md.
New test legacy_min_off_keeps_the_one_factor_test covers the choice
(legacy_zero_test), not the emitted module.

This change was made with AI assistance (Claude Code).
Jesper-A changed title from WIP: [pr-performance][shaders][AMD] New switch DARNER_LEGACY_MIN (off): legacy multiply tests min(|a|,|b|) == 0 for every factor pair to [pr-performance][shaders][AMD] New switch DARNER_LEGACY_MIN (off): legacy multiply tests min(|a|,|b|) == 0 for every factor pair 2026-12-03 18:29:05 +00:00
This pull request has changes conflicting with the target branch.
  • docs/findings.md
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'pr5-legacy-mul:Jesper-A-pr5-legacy-mul'
git switch 'Jesper-A-pr5-legacy-mul'

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.

git switch 'main'
git merge --no-ff 'Jesper-A-pr5-legacy-mul'
git switch 'Jesper-A-pr5-legacy-mul'
git rebase 'main'
git switch 'main'
git merge --ff-only 'Jesper-A-pr5-legacy-mul'
git switch 'Jesper-A-pr5-legacy-mul'
git rebase 'main'
git switch 'main'
git merge --no-ff 'Jesper-A-pr5-legacy-mul'
git switch 'main'
git merge --squash 'Jesper-A-pr5-legacy-mul'
git switch 'main'
git merge --ff-only 'Jesper-A-pr5-legacy-mul'
git switch 'main'
git merge 'Jesper-A-pr5-legacy-mul'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!10
No description provided.