Shaders: a folded waterfall over a descriptor every lane copied from one scalar load is exact #526

Open
ntsthay wants to merge 1 commit from ntsthay/folded-waterfall into Main AGit
Contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

On a 32-lane host, vfx_proj_decal_vbuffer:CS_ProjDecalResolve_Scalarized (e4a9f58e3acfcf7b in 1.0.2.1) was refused by the folded form and ended in local waves (APPROXIMATE, any mask). The refusal was "a descriptor chosen by several conditions" at 0x5288.

The program has four of LLVM's descriptor waterfalls over an 8-dword image descriptor (loop heads 0x5148, 0x5304, 0x56c4 and 0x5864). Every lane holds the same descriptor. v57..v64 are v_mov_b32 copies of s[8:15], from one s_load_dwordx8 reached by two paths, so each loop runs once. The fold turns each console v_readfirstlane_b32 into a read under each half of EXEC plus its own s_cmp_lg_u32 E.lo, 0 and s_cselect_b32 (fold.rs). That gives eight compares for one descriptor, and tracking refused them as eight conditions.

Four rules in resource.rs and structure.rs. Each acts only where tracking refused before:

  • One condition. Two SCC values are one condition when the SOPC instructions that write them have the same encoding and operands of the same origin (same_condition). An SOPC is a pure function of its operands.
  • No choice. When both outcomes of a Select are the same source and that source is uniform (Source::is_uniform: no Fetched, Lanes or LanePhi anywhere in it), the Select is dropped. A per-lane descriptor keeps the Select. There the high half would get the low half's copy.
  • Each outcome under its own lanes. When an outcome cannot be followed under the access's lanes, it is tried again under the lanes of its own first-lane reads (arm, read_known). The result is taken only if it is uniform.
  • A loop mask stays within its guard. Structured::meet_bounded and stays_within prove by induction that a lane mask joined at a loop head stays within a bound. The proof checks every incoming edge, and on back edges it takes the join as within the bound. The bound may only test values defined before the loop. runs uses this, so the copy before a waterfall counts for the lanes the loop narrows down to.
  • Own compares. A v_cmp_eq_u32/u64 of a first-lane read against the lane's own VGPRs is true in every lane when those VGPRs follow their copies to one scalar dword, and the read follows its own lanes' copies to the same dword (own_compares). It joins Waterfall::compares, and the loop is lowered as written.

How it was tested

  • Tested revision: this commit on main c8258f9.
  • New tests:
    • resource.rs: a folded waterfall over a descriptor every lane copied from scalars is exact, with a Phi of two loads and all 8 compares proven. Two refusals are also checked: a waterfall over descriptors that differ between the halves proves no compare, and one whose retry widens the lanes (s_mov_b64 exec, -1) is refused.
    • structure.rs: a_search_assuming_facts_answers_as_every_assignment_does checks meet_assuming against all 256 assignments of 8 variables, 800 random cases.
    • emit/waterfall_tests.rs, on the card: lanes_sample_the_texture_a_folded_waterfall_over_scalar_copies_names is bit-exact for 64 console lanes. a_folded_waterfall_over_descriptors_that_differ_between_halves_is_refused_or_exact checks that the result is never approximate.
  • cargo clippy --workspace --all-targets --locked -- -D warnings, and the same with --all-features: clean.
  • cargo test -p darner-shader --lib on an RTX 4080 under Proton (proton-cachyos, driver 620.73): 574 passed, 0 failed, 16 ignored.
  • darner translate --list of the whole shaders.rpf, DARNER_HOST_WAVE=32, before and after, on our integration branch: 25,693 shaders emitted by each, and the module hash differs for exactly one of them, CS_ProjDecalResolve_Scalarized. With DARNER_WAVE_WHY=1 it now comes out folded (before wave-wide); before, it was local waves (APPROXIMATE).
  • Changes a default: yes, for that one program on 32-lane hosts. Every other shader comes out byte for byte the same.
  • Not checked in play: I have not looked at the decals in game with this alone.

Checklist

  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • None. #501 and #504 change other folded programs and other code.

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #312 (DARNER_FIRSTLANE_UNIFORM) also deals with first-lane reads of values every lane holds. It excludes loaded values, so it does not cover this program, whose descriptor comes from an s_load.
## Contributor role (optional) - Fluxer ID: ## What it changes On a 32-lane host, `vfx_proj_decal_vbuffer:CS_ProjDecalResolve_Scalarized` (`e4a9f58e3acfcf7b` in 1.0.2.1) was refused by the folded form and ended in local waves (APPROXIMATE, any mask). The refusal was "a descriptor chosen by several conditions" at 0x5288. The program has four of LLVM's descriptor waterfalls over an 8-dword image descriptor (loop heads 0x5148, 0x5304, 0x56c4 and 0x5864). Every lane holds the same descriptor. v57..v64 are `v_mov_b32` copies of `s[8:15]`, from one `s_load_dwordx8` reached by two paths, so each loop runs once. The fold turns each console `v_readfirstlane_b32` into a read under each half of EXEC plus its own `s_cmp_lg_u32 E.lo, 0` and `s_cselect_b32` (`fold.rs`). That gives eight compares for one descriptor, and tracking refused them as eight conditions. Four rules in `resource.rs` and `structure.rs`. Each acts only where tracking refused before: - **One condition.** Two SCC values are one condition when the SOPC instructions that write them have the same encoding and operands of the same origin (`same_condition`). An SOPC is a pure function of its operands. - **No choice.** When both outcomes of a `Select` are the same source and that source is uniform (`Source::is_uniform`: no `Fetched`, `Lanes` or `LanePhi` anywhere in it), the `Select` is dropped. A per-lane descriptor keeps the `Select`. There the high half would get the low half's copy. - **Each outcome under its own lanes.** When an outcome cannot be followed under the access's lanes, it is tried again under the lanes of its own first-lane reads (`arm`, `read_known`). The result is taken only if it is uniform. - **A loop mask stays within its guard.** `Structured::meet_bounded` and `stays_within` prove by induction that a lane mask joined at a loop head stays within a bound. The proof checks every incoming edge, and on back edges it takes the join as within the bound. The bound may only test values defined before the loop. `runs` uses this, so the copy before a waterfall counts for the lanes the loop narrows down to. - **Own compares.** A `v_cmp_eq_u32`/`u64` of a first-lane read against the lane's own VGPRs is true in every lane when those VGPRs follow their copies to one scalar dword, and the read follows its own lanes' copies to the same dword (`own_compares`). It joins `Waterfall::compares`, and the loop is lowered as written. ## How it was tested - Tested revision: this commit on main `c8258f9`. - New tests: - `resource.rs`: a folded waterfall over a descriptor every lane copied from scalars is exact, with a Phi of two loads and all 8 compares proven. Two refusals are also checked: a waterfall over descriptors that differ between the halves proves no compare, and one whose retry widens the lanes (`s_mov_b64 exec, -1`) is refused. - `structure.rs`: `a_search_assuming_facts_answers_as_every_assignment_does` checks `meet_assuming` against all 256 assignments of 8 variables, 800 random cases. - `emit/waterfall_tests.rs`, on the card: `lanes_sample_the_texture_a_folded_waterfall_over_scalar_copies_names` is bit-exact for 64 console lanes. `a_folded_waterfall_over_descriptors_that_differ_between_halves_is_refused_or_exact` checks that the result is never approximate. - `cargo clippy --workspace --all-targets --locked -- -D warnings`, and the same with `--all-features`: clean. - `cargo test -p darner-shader --lib` on an RTX 4080 under Proton (proton-cachyos, driver 620.73): 574 passed, 0 failed, 16 ignored. - `darner translate --list` of the whole `shaders.rpf`, `DARNER_HOST_WAVE=32`, before and after, on our integration branch: 25,693 shaders emitted by each, and the module hash differs for exactly one of them, `CS_ProjDecalResolve_Scalarized`. With `DARNER_WAVE_WHY=1` it now comes out `folded (before wave-wide)`; before, it was local waves (APPROXIMATE). - Changes a default: yes, for that one program on 32-lane hosts. Every other shader comes out byte for byte the same. - Not checked in play: I have not looked at the decals in game with this alone. ## Checklist - [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. - None. #501 and #504 change other folded programs and other code. ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #312 (`DARNER_FIRSTLANE_UNIFORM`) also deals with first-lane reads of values every lane holds. It excludes loaded values, so it does not cover this program, whose descriptor comes from an `s_load`.
ntsthay changed title from Shaders: a folded waterfall over a descriptor every lane copied from one scalar load is exact to [pr-fix][shaders] Shaders: a folded waterfall over a descriptor every lane copied from one scalar load is exact 2026-12-05 07:04:22 +00:00
TrustNo1 changed title from [pr-fix][shaders] Shaders: a folded waterfall over a descriptor every lane copied from one scalar load is exact to Shaders: a folded waterfall over a descriptor every lane copied from one scalar load is exact 2026-12-06 03:27:42 +00:00
Owner

Test needed — commit 31582530; what to check:

On a 32-lane host, vfx_proj_decal_vbuffer:CS_ProjDecalResolve_Scalarized (e4a9f58e3acfcf7b in 1.0.2.1) was refused by the folded form and ended in local waves (APPROXIMATE, any mask). The refusal was "a descriptor chosen by several conditions" at 0x5288.

The program has four of LLVM's descriptor waterfalls over an 8-dword image descriptor (loop heads 0x5148, 0x5304, 0x56c4 and 0x5864). Every lane holds the same descriptor. v57..v64 are v_mov_b32 copies of s[8:15], from one s_load_dwordx8 reached by two paths, so each loop runs once. The fold turns each console v_readfirstlane_b32 into a read under each half of EXEC plus its own s_cmp_lg_u32 E.lo, 0 and s_cselect_b32 (fold.rs). That gives eight compares for one descriptor, and tracking refused them as eight conditions.

Four rules in resource.rs and structure.rs. Each acts only where tracking refused before:

  • One condition. Two SCC values are one condition when the SOPC instructions that write them have the same encoding and operands of the same origin (same_condition). An SOPC is a pure function of its operands.
  • No choice. When both outcomes of a Select are the same source and that source is u

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `31582530`; what to check: On a 32-lane host, `vfx_proj_decal_vbuffer:CS_ProjDecalResolve_Scalarized` (`e4a9f58e3acfcf7b` in 1.0.2.1) was refused by the folded form and ended in local waves (APPROXIMATE, any mask). The refusal was "a descriptor chosen by several conditions" at 0x5288. The program has four of LLVM's descriptor waterfalls over an 8-dword image descriptor (loop heads 0x5148, 0x5304, 0x56c4 and 0x5864). Every lane holds the same descriptor. v57..v64 are `v_mov_b32` copies of `s[8:15]`, from one `s_load_dwordx8` reached by two paths, so each loop runs once. The fold turns each console `v_readfirstlane_b32` into a read under each half of EXEC plus its own `s_cmp_lg_u32 E.lo, 0` and `s_cselect_b32` (`fold.rs`). That gives eight compares for one descriptor, and tracking refused them as eight conditions. Four rules in `resource.rs` and `structure.rs`. Each acts only where tracking refused before: - **One condition.** Two SCC values are one condition when the SOPC instructions that write them have the same encoding and operands of the same origin (`same_condition`). An SOPC is a pure function of its operands. - **No choice.** When both outcomes of a `Select` are the same source and that source is u Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
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/526/head:ntsthay/folded-waterfall'
git switch 'ntsthay/folded-waterfall'

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 'ntsthay/folded-waterfall'
git switch 'ntsthay/folded-waterfall'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'ntsthay/folded-waterfall'
git switch 'ntsthay/folded-waterfall'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'ntsthay/folded-waterfall'
git switch 'Main'
git merge --squash 'ntsthay/folded-waterfall'
git switch 'Main'
git merge --ff-only 'ntsthay/folded-waterfall'
git switch 'Main'
git merge 'ntsthay/folded-waterfall'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 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!526
No description provided.