Skip to content

Shaders: a wave32 program's mask read picks its half of a 64-lane ballot - #434

Open
NolanCotter wants to merge 3 commits into
Marvisak:mainfrom
NolanCotter:agent/host-wave-mask-halves
Open

NolanCotter wants to merge 3 commits into
Marvisak:mainfrom
NolanCotter:agent/host-wave-mask-halves

Conversation

@NolanCotter

Copy link
Copy Markdown

AI-assisted (Devin). Fixes #131.

Summary

For MaskData::Host (pixel and vertex stages — no WaveSize attribute, so the host wave size is whatever the driver picks) a host wave may have 64 lanes. Its upper half is then a second console wave whose bits land in the ballot's second dword — but mask_dword returned the first dword for every lane. Every lane mask read as a number (s_bcnt1_i32_*, s_ff1_i32_*, v_mbcnt_*, s_cmp_eq/lg_u32/u64) therefore gave the upper half's lanes the lower console wave's mask values: a ballot on a 64-lane host wave drops its high half.

  • mask_dword now selects the ballot half by the reader's host lane (lane >= 32 ? y : x) for MaskData::Host wave32 programs. Unchanged on 32-lane host waves (lane < 32 always selects the first dword).
  • v_mbcnt_lo in that mode counts bits of the lane's own half's mask below lane & 31.
  • v_mbcnt_hi in that mode is src1 alone — a wave32 mask has no high half.

Out of scope (noted in #131): the host-lane vs console-lane space for v_readlane/v_writelane computed lanes is the lane-space problem #225/#320 handle in the console flow; it is not a mask read.

Test plan

The synthetic test asserts the new lowering emits the lane index needed to pick the ballot half. To see the emitted module's declared functions in tests, emit::bitcode::Module gained a #[cfg(test)] func_names accessor and Lowered a #[cfg(test)] op_names field.

No game data: the fixture is a five-instruction hand-encoded program.

Generated with Devin

For MaskData::Host (pixel and vertex stages, whose programs declare no
WaveSize) a host wave may have 64 lanes; its upper half is a second
console wave whose bits land in the ballot's second dword. mask_dword
returned the first dword for every lane, so mask reads as data
(s_bcnt1_i32_*, s_ff1_i32_*, v_mbcnt_*, s_cmp_eq/lg_u32/u64) gave the
upper half's lanes the lower console wave's mask values.

mask_dword now selects the ballot half by the reader's host lane.
v_mbcnt_lo counts bits of the lane's own half below its lane in it, and
v_mbcnt_hi is src1 alone on wave32, whose mask has no high half. Both are
unchanged when the host wave is 32 lanes.

Fixes Marvisak#131.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@DaviTostes

Copy link
Copy Markdown

Verified 588a0e2 on Arch Linux x86_64 (rustc 1.98.1, cargo test -p victory-shader): 57 passed, 0 failed, 0 ignored.

Not covered: running the new ballot/mask paths on a Windows/Wine GPU and title behaviour (no game data here).

@marquezinii

Copy link
Copy Markdown
Contributor

Ran 588a0e2 on Windows 11 with an NVIDIA RTX 5070 (driver 621.24, 32-lane waves, the only adapter), rustc 1.98.1, debug, in its own target directory:

  • cargo test -p victory-shader: 70 passed, 0 failed, 3 runs. The validator picked up the Windows SDK's dxil.dll (10.0.26100.0), so the suite's modules were validated here, which the Linux runs skip.
  • a_wave32_mask_read_takes_its_ballot_half only checks the op list, so I also passed its lowered container through dxil.dll locally (a temporary three-line addition, not pushed): it validates.
  • On this host waves are 32 lanes, so the new half pick always takes the first dword. This run shows nothing regresses there; it does not exercise the 64-lane case the PR is about.

Not covered: a host that runs pixel shaders 64 wide, and the title (no game data here).

…names

The wave32 mask-read test now asserts the pick's shape: an unsigned
`lane >= 32` compare selecting the ballot's second dword over the first,
`lane & 31` for v_mbcnt_lo, and a bit count of a constant zero for
v_mbcnt_hi. A second test covers the paths that must not pick: wave64
programs (own dwords) and compute (the whole host wave is the console's).

emit::bitcode::Module gains a test-only inst_lines dump to support that.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@upipibi upipibi mentioned this pull request Nov 24, 2026
Under MaskData::Host a wave32 program's lane numbers stay host
lanes, so a v_mbcnt_lo rank of the upper half's console wave takes
its 32-lane base: v_mbcnt_lo(-1) still reports the lane index, and
the value composes with host-lane reads like v_readlane. The mask
itself stays per console wave, so s_bcnt1, s_ff1 and s_cmp keep
their own wave's count and the first wave's divergence never feeds
a second wave's scalars.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@NolanCotter

Copy link
Copy Markdown
Author

Update (head 48f92e2): v_mbcnt_lo now reports its rank as a host lane number.

Relationship to #145 — I missed it when I scoped this (it covers #125, and #131 is the same defect): both PRs read the upper ballot half, but pick different lane-space models for a wave32 program on a 64-lane host wave:

  • Shaders: read both ballot halves for wave32 host-wave mask ops #145 (host-whole): mask ops see the full 64-bit host ballot. s_bcnt1(exec) counts both console waves, s_cmp tests both waves' bits, and v_mbcnt_lo yields a unique index per host lane. It's GPU-verified, but its mask scalars and mbcnt ranks are polluted by the other console wave's divergence (a second wave's rank varies with the first wave's active count, and s_bcnt1(exec) reports 64 where the console program's wave expects 32).
  • This PR (per-half + host base): mask_dword selects the lane's own console wave's dword, so s_bcnt1/s_ff1/s_cmp keep their own wave's value regardless of the other wave's divergence. v_mbcnt_lo counts within its own half and then adds the wave's 32-lane base, so v_mbcnt_lo(-1) still reports the host lane index and the result stays a valid host lane for v_readlane composition — stable whether or not the other wave is fully active.

Both fix the reported defect on the common all-active case; the difference is only observable when the two console waves' tracked masks diverge. The test now asserts the emitted instruction structure (half select, lane & 31 mask, wave-base add), and v_mbcnt_lo(-1) is covered in the synthetic program.

@QemmHD

QemmHD commented Nov 24, 2026

Copy link
Copy Markdown

Tested 48f92e2 on main d2a4bae, native Windows 11, RTX 4070 SUPER (32-lane waves), driver 621.24.

No game data used.

AI-assisted (Claude Code, Claude Opus 5.5).
Discord: bigpeppa.

@catminou4 catminou4 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.

Test-merge with current main (ffe310e) conflicts — needs a rebase. Main moved recently (PR #566 waterfall loops touched lower.rs/resource.rs/valu_tests.rs), so shader PRs opened before it need rebasing. I'll re-review once it merges cleanly.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/lower.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MaskData::Host: the high half of a ballot is dropped, and nothing forces the host wave to 32

6 participants