[pr-performance][shaders] Shaders: legacy products (0 times anything is 0) in the short form on every card, not only AMD #304

Open
cafe wants to merge 1 commit from cafe/legacy-products-any-card into main AGit
Contributor

Contributor role (optional)

  • Fluxer ID: Cafe#9189

What it changes

  • The problem on main: v_mul_legacy_f32 and the like are written in the short form (rdna::LEGACY: a plain product, plus one comparison min(|a|,|b|) == 0 where a factor may be an infinity or NaN) only on AMD RDNA cards or with DARNER_HOST_RDNA=1. Every other card gets the long form: a product, two comparisons, an OR and a select. host.rs already calls bit 2 host-neutral.

The short form gives the same results on any card, except that a zero result may have the other sign (as on AMD today). This change keeps DARNER_RDNA_FLOAT's host-neutral bits on every card (rdna::NEUTRAL = LEGACY); the other bits (MAX_MIN, MED3, FRACT) stay AMD-only. DARNER_RDNA_FLOAT=0 still turns it off. The bits are already in the shader cache key (host_settings), so translations made before are not reused. DARNER_LEGACY_FINITE stays off by default, as on main.

  • emit/lower.rs: on a non-RDNA host, host_rdna() keeps bits & rdna::NEUTRAL instead of 0.
  • emit/mod.rs: rdna::NEUTRAL, with why it holds on every host.
  • README: the DARNER_RDNA_FLOAT row says which bits count on which cards.

How it was tested

  • Tested revision: main f3840d73d, switched by DARNER_HOST_RDNA=1 (which turns on all four bits, not only this one); gates on f3840d73d + this commit (6f070e3f4).

  • Changes a default: yes, for every card that is not AMD RDNA (NVIDIA, Intel). The shaders are rebuilt once.

  • A/B result (RTX 2080 Ti, house save, the same 50 s after the world shows, pipelines warm):

    A (main defaults) B (DARNER_HOST_RDNA=1: all four bits)
    GPU time a frame 53.7 ms (1 run) 52.6 ms (partly cold), 52.3 ms
    fps 18.11 18.20, 18.58

    About -1.2 ms, from a single base run. An earlier main (de8d42006, with DARNER_LEGACY_FINITE on at the time), bit 2 alone on the same card, with DARNER_VIEW_START_FAST=1 on both sides: 51.8/51.4 -> 48.8/48.9 ms, 18.73/18.85 -> 19.72/19.79 fps.

  • Picture: a recorded frame of the house, translated again with DARNER_HOST_RDNA=0 and =1 (all four bits): 1,571 shaders differ, the replayed picture is the same to the pixel. The replay has no ray tracing.

  • Gates run on 6f070e3f4: cargo build --locked --release -p darner ok; cargo clippy --locked --release -p darner-shader -p darner-gpu --all-targets -- -D warnings ok; cargo test --locked --release -p darner-shader --lib -- --skip oracle64: 488 passed, 67 failed, the same 67 as on main f3840d73d on this PC (they need a Shader Model 6.6 device, which Windows 10's own D3D12 lacks, or DXIL validation). Not run this time: the darner-gpu tests (they create a D3D12 device). Not run: the whole-workspace gates.

  • Gameplay: RTX 2080 Ti, Windows 10 22H2, Series S profile, 2560x1440 output, DLSS Performance, hardware ray tracing, no frame generation.

  • Shader wave sizes tested: 32 lanes (NVIDIA).

  • Hardware dependence: untested on Intel cards by us (#38 measured this form on an Arc A770). No change on AMD RDNA cards (all bits already count there).

  • Coverage gaps: not measured with bit 2 alone on f3840d73d (the A/B above has all four bits); no check by eye in play.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • 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: #10 (AMD, DARNER_LEGACY_MIN: another change in the same lowering), #31 (Intel host detection).
  • Replaces or overlaps: #38 (draft) gives Intel cards this same form behind DARNER_INTEL_LEGACY_MUL, and changes the same lines of host_rdna(). This also covers the Intel case of draft #38. If #38 lands first, I'll rebase on it and keep its Intel switch.
## Contributor role (optional) - Fluxer ID: `Cafe#9189` ## What it changes - The problem on main: `v_mul_legacy_f32` and the like are written in the short form (`rdna::LEGACY`: a plain product, plus one comparison `min(|a|,|b|) == 0` where a factor may be an infinity or NaN) only on AMD RDNA cards or with `DARNER_HOST_RDNA=1`. Every other card gets the long form: a product, two comparisons, an OR and a select. `host.rs` already calls bit 2 host-neutral. The short form gives the same results on any card, except that a zero result may have the other sign (as on AMD today). This change keeps `DARNER_RDNA_FLOAT`'s host-neutral bits on every card (`rdna::NEUTRAL = LEGACY`); the other bits (MAX_MIN, MED3, FRACT) stay AMD-only. `DARNER_RDNA_FLOAT=0` still turns it off. The bits are already in the shader cache key (`host_settings`), so translations made before are not reused. `DARNER_LEGACY_FINITE` stays off by default, as on `main`. - `emit/lower.rs`: on a non-RDNA host, `host_rdna()` keeps `bits & rdna::NEUTRAL` instead of 0. - `emit/mod.rs`: `rdna::NEUTRAL`, with why it holds on every host. - README: the `DARNER_RDNA_FLOAT` row says which bits count on which cards. ## How it was tested - Tested revision: `main` f3840d73d, switched by `DARNER_HOST_RDNA=1` (which turns on all four bits, not only this one); gates on f3840d73d + this commit (6f070e3f4). - Changes a default: yes, for every card that is not AMD RDNA (NVIDIA, Intel). The shaders are rebuilt once. - A/B result (RTX 2080 Ti, house save, the same 50 s after the world shows, pipelines warm): | | A (`main` defaults) | B (`DARNER_HOST_RDNA=1`: all four bits) | |---|---|---| | GPU time a frame | 53.7 ms (1 run) | 52.6 ms (partly cold), 52.3 ms | | fps | 18.11 | 18.20, 18.58 | About -1.2 ms, from a single base run. An earlier `main` (de8d42006, with `DARNER_LEGACY_FINITE` on at the time), bit 2 alone on the same card, with `DARNER_VIEW_START_FAST=1` on both sides: 51.8/51.4 -> 48.8/48.9 ms, 18.73/18.85 -> 19.72/19.79 fps. - Picture: a recorded frame of the house, translated again with `DARNER_HOST_RDNA=0` and `=1` (all four bits): 1,571 shaders differ, the replayed picture is the same to the pixel. The replay has no ray tracing. - Gates run on 6f070e3f4: `cargo build --locked --release -p darner` ok; `cargo clippy --locked --release -p darner-shader -p darner-gpu --all-targets -- -D warnings` ok; `cargo test --locked --release -p darner-shader --lib -- --skip oracle64`: 488 passed, 67 failed, the same 67 as on `main` f3840d73d on this PC (they need a Shader Model 6.6 device, which Windows 10's own D3D12 lacks, or DXIL validation). Not run this time: the `darner-gpu` tests (they create a D3D12 device). Not run: the whole-workspace gates. - Gameplay: RTX 2080 Ti, Windows 10 22H2, Series S profile, 2560x1440 output, DLSS Performance, hardware ray tracing, no frame generation. - Shader wave sizes tested: 32 lanes (NVIDIA). - Hardware dependence: untested on Intel cards by us (#38 measured this form on an Arc A770). No change on AMD RDNA cards (all bits already count there). - Coverage gaps: not measured with bit 2 alone on f3840d73d (the A/B above has all four bits); no check by eye in play. ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [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: #10 (AMD, `DARNER_LEGACY_MIN`: another change in the same lowering), #31 (Intel host detection). - Replaces or overlaps: #38 (draft) gives Intel cards this same form behind `DARNER_INTEL_LEGACY_MUL`, and changes the same lines of `host_rdna()`. This also covers the Intel case of draft #38. If #38 lands first, I'll rebase on it and keep its Intel switch.
First-time contributor

The signed-zero caveat changes an observable result on NVIDIA and Intel with the new default. For v_mul_legacy_f32 with a literal 2.0 and a runtime -0.0, the current non-RDNA zero guard selects 0x00000000. The short path drops the guard because the literal is finite and nonzero, so the product can store 0x80000000. I checked the two expressions with a small CPU bit-pattern fixture. The repo's oracle64::legacy_mul expects positive zero in that case.

Could you keep bit 2 explicit opt-in on non-RDNA for now? An A/B on this exact head with DARNER_RDNA_FLOAT=0 versus =2 would isolate the speedup from the other three bits. For a default change, a small shader that stores the result as a uint would cover the difference the house-frame comparison misses.

The signed-zero caveat changes an observable result on NVIDIA and Intel with the new default. For `v_mul_legacy_f32` with a literal `2.0` and a runtime `-0.0`, the current non-RDNA zero guard selects `0x00000000`. The short path drops the guard because the literal is finite and nonzero, so the product can store `0x80000000`. I checked the two expressions with a small CPU bit-pattern fixture. The repo's `oracle64::legacy_mul` expects positive zero in that case. Could you keep bit 2 explicit opt-in on non-RDNA for now? An A/B on this exact head with `DARNER_RDNA_FLOAT=0` versus `=2` would isolate the speedup from the other three bits. For a default change, a small shader that stores the result as a uint would cover the difference the house-frame comparison misses.
Collaborator

Thanks for this. I read the change against 6f070e3f4 and main f3840d73d. We also saw andrewpozdnakov7-jpg's comment (signed zero, bit 2 opt-in off RDNA, an =0 against =2 A/B, a uint-store test); what follows adds to it.

What I checked

  1. The short form (legacy_mul, lower.rs:4680 on the head) is the product, or 0 where min(|a|,|b|) == 0. With the D3D/DXIL minimum rule as we understand it (a NaN operand returns the other operand; we could not look the sentence up in the spec text, so please correct us if it differs), the cases agree with the long form: 0NaN and 0inf give 0, NaN5 gives NaN, NaNNaN gives NaN.
  2. Three shortcuts skip the min test and return the plain product (lower.rs:4690-4691): a == b, both factors assumed finite (only with DARNER_LEGACY_FINITE), and a finite non-zero literal factor (not_zero, lower.rs:4690). The last one is what gives the signed-zero difference in the comment above: 2.0 times a runtime -0.0 returns the product, -0.0, not +0 as the long form gives (as the commenter says). The first two cannot differ in value (the value times itself, or two finite numbers); the sign of a zero can. So "only the zero's sign can differ" holds, but it is more than a rare corner: any finite non-zero literal factor takes it.
  3. host_rdna() now returns bits & NEUTRAL off RDNA (lower.rs:1098-1105), NEUTRAL = LEGACY (emit/mod.rs:544). The bits stay in the cache key (lower.rs:1078), so old translations are not reused.

The A/B does not measure the PR alone

  • Arm A is one run. Arm B is DARNER_HOST_RDNA=1, which turns on all four bits, not only bit 2.
  • The bit-2-alone figure (51.8/51.4 to 48.8/48.9 ms) is from an older main (de8d42006) with DARNER_LEGACY_FINITE on, so it is not this code.
  • When our PC is free we can run the pair the commenter asked for, on the exact head 6f070e3f4: DARNER_RDNA_FLOAT=0 against the default (which is bit 2 off RDNA), on our RTX 2070 SUPER (Turing), as launch pairs in ABBA order, and post the numbers with n and the spread here. Say if you would rather we didn't.
  • A note for others: on main f3840d73d, DARNER_HOST_RDNA=1 DARNER_RDNA_FLOAT=2 on an NVIDIA card gives the same shaders as this PR (host.rs:2495-2496 sets the flag; host_rdna() then returns rdna_float()). We grepped HOST_RDNA: the four bits and the cache key (lower.rs:1078) are its only readers on the host.

A stale comment

legacy_finite()'s comment (lower.rs:786-787) still says DARNER_LEGACY_FINITE "only acts where the legacy products are guarded (host_rdna: AMD RDNA cards, or DARNER_HOST_RDNA=1)". After this PR it acts on every card whenever it is set. The same comment records that value 31 gave dark speckled blotches on roads at sunset (PR #76 test), so someone with it set on an NVIDIA card would now get that. Please update the comment. We found no README row for DARNER_LEGACY_FINITE.

Picture check

The replay check used all four bits and had no ray tracing. That is fine for this bit; could you add one line to the PR saying so?

Gates

As on #303, the PR says the workspace gates were not run. For a default change please run cargo clippy --workspace --all-targets --locked -- -D warnings, the same with --all-features, cargo test --workspace --locked, and the darner-gpu tests. Or say so and we will run them on our PC (Windows 11, RTX 2070 SUPER) and report here on 6f070e3f4.

Overlap

Our own #10 (DARNER_LEGACY_MIN, off by default) changes the same lowering inside the RDNA branch. After #304, with DARNER_LEGACY_MIN on, #10's min form would reach every card. We will rebase or close ours if you ask. The Intel case (#38) is covered by the PR's own note.

This comment was written with AI assistance (Claude Code).

Thanks for this. I read the change against 6f070e3f4 and `main` f3840d73d. We also saw andrewpozdnakov7-jpg's comment (signed zero, bit 2 opt-in off RDNA, an `=0` against `=2` A/B, a uint-store test); what follows adds to it. **What I checked** 1. The short form (`legacy_mul`, `lower.rs:4680` on the head) is the product, or 0 where `min(|a|,|b|) == 0`. With the D3D/DXIL minimum rule as we understand it (a NaN operand returns the other operand; we could not look the sentence up in the spec text, so please correct us if it differs), the cases agree with the long form: 0*NaN and 0*inf give 0, NaN*5 gives NaN, NaN*NaN gives NaN. 2. Three shortcuts skip the min test and return the plain product (`lower.rs:4690-4691`): `a == b`, both factors assumed finite (only with `DARNER_LEGACY_FINITE`), and a finite non-zero literal factor (`not_zero`, `lower.rs:4690`). The last one is what gives the signed-zero difference in the comment above: 2.0 times a runtime -0.0 returns the product, -0.0, not +0 as the long form gives (as the commenter says). The first two cannot differ in value (the value times itself, or two finite numbers); the sign of a zero can. So "only the zero's sign can differ" holds, but it is more than a rare corner: any finite non-zero literal factor takes it. 3. `host_rdna()` now returns `bits & NEUTRAL` off RDNA (`lower.rs:1098-1105`), `NEUTRAL = LEGACY` (`emit/mod.rs:544`). The bits stay in the cache key (`lower.rs:1078`), so old translations are not reused. **The A/B does not measure the PR alone** - Arm A is one run. Arm B is `DARNER_HOST_RDNA=1`, which turns on all four bits, not only bit 2. - The bit-2-alone figure (51.8/51.4 to 48.8/48.9 ms) is from an older `main` (de8d42006) with `DARNER_LEGACY_FINITE` on, so it is not this code. - When our PC is free we can run the pair the commenter asked for, on the exact head 6f070e3f4: `DARNER_RDNA_FLOAT=0` against the default (which is bit 2 off RDNA), on our RTX 2070 SUPER (Turing), as launch pairs in ABBA order, and post the numbers with n and the spread here. Say if you would rather we didn't. - A note for others: on `main` f3840d73d, `DARNER_HOST_RDNA=1 DARNER_RDNA_FLOAT=2` on an NVIDIA card gives the same shaders as this PR (`host.rs:2495-2496` sets the flag; `host_rdna()` then returns `rdna_float()`). We grepped `HOST_RDNA`: the four bits and the cache key (`lower.rs:1078`) are its only readers on the host. **A stale comment** `legacy_finite()`'s comment (`lower.rs:786-787`) still says `DARNER_LEGACY_FINITE` "only acts where the legacy products are guarded (`host_rdna`: AMD RDNA cards, or `DARNER_HOST_RDNA=1`)". After this PR it acts on every card whenever it is set. The same comment records that value 31 gave dark speckled blotches on roads at sunset (PR #76 test), so someone with it set on an NVIDIA card would now get that. Please update the comment. We found no README row for `DARNER_LEGACY_FINITE`. **Picture check** The replay check used all four bits and had no ray tracing. That is fine for this bit; could you add one line to the PR saying so? **Gates** As on #303, the PR says the workspace gates were not run. For a default change please run `cargo clippy --workspace --all-targets --locked -- -D warnings`, the same with `--all-features`, `cargo test --workspace --locked`, and the `darner-gpu` tests. Or say so and we will run them on our PC (Windows 11, RTX 2070 SUPER) and report here on 6f070e3f4. **Overlap** Our own #10 (`DARNER_LEGACY_MIN`, off by default) changes the same lowering inside the RDNA branch. After #304, with `DARNER_LEGACY_MIN` on, #10's min form would reach every card. We will rebase or close ours if you ask. The Intel case (#38) is covered by the PR's own note. This comment was written with AI assistance (Claude Code).
First-time contributor

@Jesper-A Microsoft's DXIL spec confirms that FMin returns the other operand when exactly one operand is NaN. It also describes denormal handling separately. DXIL FMin

I put the explicit opt-in in 9fc872a on top of 6f070e3f4. This keeps the current AMD RDNA defaults and enables the shorter multiply on other cards only when its bit is explicitly selected. I also updated the legacy-finite comment to cover this path.

In a CPU fixture using the production settings parser and host selection, the non-RDNA default for the signed-zero example went from 0x80000000 to 0x00000000. Explicit =2 still produced 0x80000000, and all six AMD settings cases matched the PR head. The focused shader type check and asset policy passed. The proposed RTX 2070 SUPER comparison would still be useful for measuring the opt-in.

If you cherry-pick or rebase the contribution, please keep the Git author. If you fold it into your own commit, please add this trailer.

Co-authored-by: andrewpozdnakov7-jpg <[email protected]>

@Jesper-A Microsoft's DXIL spec confirms that FMin returns the other operand when exactly one operand is NaN. It also describes denormal handling separately. [DXIL FMin](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst#fmin) I put the explicit opt-in in [9fc872a](https://git.gtahub.st/andrewpozdnakov7-jpg/Darner/commit/9fc872a7bb58a6372386a19e47569404ee804c00) on top of 6f070e3f4. This keeps the current AMD RDNA defaults and enables the shorter multiply on other cards only when its bit is explicitly selected. I also updated the legacy-finite comment to cover this path. In a CPU fixture using the production settings parser and host selection, the non-RDNA default for the signed-zero example went from `0x80000000` to `0x00000000`. Explicit `=2` still produced `0x80000000`, and all six AMD settings cases matched the PR head. The focused shader type check and asset policy passed. The proposed RTX 2070 SUPER comparison would still be useful for measuring the opt-in. If you cherry-pick or rebase the contribution, please keep the Git author. If you fold it into your own commit, please add this trailer. `Co-authored-by: andrewpozdnakov7-jpg <296776509+[email protected]>`
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/304/head:cafe/legacy-products-any-card'
git switch 'cafe/legacy-products-any-card'

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 'cafe/legacy-products-any-card'
git switch 'cafe/legacy-products-any-card'
git rebase 'main'
git switch 'main'
git merge --ff-only 'cafe/legacy-products-any-card'
git switch 'cafe/legacy-products-any-card'
git rebase 'main'
git switch 'main'
git merge --no-ff 'cafe/legacy-products-any-card'
git switch 'main'
git merge --squash 'cafe/legacy-products-any-card'
git switch 'main'
git merge --ff-only 'cafe/legacy-products-any-card'
git switch 'main'
git merge 'cafe/legacy-products-any-card'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
3 participants
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!304
No description provided.