[bug] v_readfirstlane_b32 ignores EXEC on a split wave: each half reads its own first host lane #254
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T05:31:38Z.
What is wrong
On wave64 programs that we run as two 32-lane host waves, the plain
v_readfirstlane_b32arm returns the value of the first host lane of each half. It ignores EXEC. The console returns the value of the first lane that is active in the 64-lane EXEC, and every lane sees that same value.The translator accepts the shape, so this is a wrong result, not a refusal.
Repro (synthetic program, our own assembly, no title code)
v1 = lane indexs_mov_b64 exec, <lanes 8 and 9>v_readfirstlane_b32 s12, v1Expected: every lane reads 8. On Metal: lanes 0-31 read 0, lanes 32-63 read 32. It also reads 0 when only lane 8 writes
v1 = 77inside a branch.Where
crates/victory-shader/src/emit/lower.rs, the plain"v_readfirstlane_b32"arm (around line 9098 on main2a3b03d1). It lowers to "first running host thread". The lockstep arm (lockstep_first_lane) does it right, and the doc comment inhalves.rsalready admits the limit.Why it matters
It is in the tail of all six culling compute shaders that fill the indirect mesh records (see the black-world issue). There, every wave reads an old count of 0, so waves write their visible-instance entries over each other. It does not explain the zero counts, but it will corrupt the lists as soon as counts are non-zero.
What a fix needs
v_readlane, other users of the first-lane helper).Work on this has started on our side; a second pair of eyes on the design, or a PR, is welcome.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by evan-kolberg on 2026-11-27T05:44:39Z.
Thanks for the generated repro. While reviewing #254 with Codex, I found an empty-EXEC corner case in the lockstep selector on
2a3b03d1. It selects lane 31 becauseFirstbitLo(0)gives0xffffffff, and adding 32 wraps that to 31. RDNA 2 specifies reading the VGPR value from lane 0 when EXEC is empty. Stock selector · RDNA 2 ISA, VOP1 opcode 2 · FirstbitLo behaviorA generated CPU test evaluated the actual emitted index. Lower-only, upper-only and both-halves masks selected 8, 40 and 31 with and without the change. Checking the high word before selecting the upper result made empty EXEC select 0 instead of 31. This covers lockstep index selection. The ordinary split-wave path and hardware broadcast remain separate, as does #258’s zero-count investigation.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by marcus-4 on 2026-11-27T06:06:41Z.
A fix for this, in case it helps or saves you time:
marcus-4:fix/readfirstlane-split-wave, two commits on main2a3b03d1. Happy to open a PR, or to leave it as a reference if your branch is further along.What it does (
Lower::split_first_lane,lower/halves.rs;v_readfirstlane_b32of a wave64 program split into two host waves):A value proven the same in every lane of the console wave keeps the per-half read, which is then exact. The proof is
Lower::wave_uniform, conservative:Without this, every first lane read of a uniform value in divergent code would be refused.
Otherwise, where both halves arrive (
exchange_site): the lowest bit of EXEC, read across the halves withwide_read_lane. An EXEC narrowed by a scalar move leaves every thread running, so your repro takes this route.Otherwise it is refused ("a first lane read across the two host waves of a console wave, in divergent code"), so a one-wave group retries in lockstep.
A first lane read of a VGPR now turns on
console_lanesand the halves. Waterfall reads and reads of a VGPR just moved from a scalar or constant are left out, which keepsmask_tests::a_wave64_mask_joined_with_data_at_a_uniform_loop_is_its_numbertranslating.The second commit fixes the empty-EXEC case @evan-kolberg found: the selector uses the high word only when it is not 0, so an empty EXEC reads lane 0.
Tests (
split_wave_tests, Metal, groups of two console waves, which lockstep does not take):wave32_tests::a_branch_on_a_first_lane_read_is_not_uniform_across_the_halvesnow expects the read's own exchange (4 barriers).split_sccis unchanged: it still counts an SCC made from a first lane read as split, which mesh programs' per-half read needs. Behind such a branch nothing exchanges, which is now only conservative for compute.M1 Pro, macOS 27.2.1, Xcode 27.2, MSC 4.0.3:
--target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory: 381 passed, 0 failed-D warnings) andxtask policy: cleanNot done: the corpus comparison by output fingerprint (no shader corpus here), so how many programs change or become refused per stage is unmeasured; and no game run.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 4TheDoctor on 2026-11-27T06:25:10Z.
The same bug is in pragyaangaur/VIctory-macOS's copy of the translator, which shares this code's history. I checked it on main
c8e4f326, on an M3 Max under D3DMetal (the Wine route), with a GPU test in itshalves_tests. The program is synthetic and hand-encoded, a group of 64, and has the branch shape the console compiler emits:v_cmp_eq_u32 vcc, 20, v3): 40 expected, 0 in all 64.s_mov_b32 exec_lo/exec_hi) is refused there ("a lane mask holding lanes elsewhere read as data"), so only the branch form gives a wrong answer.v_readfirstlane_b32arm goes throughhalves::first_lane_across, which exchanges the halves. With no active mask passed it takes the lower half's lane 0, so the result is 0 everywhere rather than your 0 / 32 split. Same root cause: EXEC isn't consulted.Reported there as pragyaangaur/VIctory-macOS#512, pointing at marcus-4's branch here. If it helps your test suite, the branch form (lanes 8, 9 and 40, 41) is a cheap case that fails before a fix and covers both halves.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T06:48:21Z.
Thank you for the empty-EXEC case and for evaluating the emitted index rather than only reading it.
marcus-4 has a branch for this issue (
fix/readfirstlane-split-wave, linked in the comment below yours). Its second commit handles empty EXEC by reading lane 0. Could you run your generated test against that commit? Then we have one answer for the selector and not two.The ordinary split-wave path and the hardware broadcast stay with that branch, and the zero counts stay in #258.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T06:48:22Z.
Thank you for the branch and the table. Yes please, open it as a pull request against main.
For the pull request:
Lower::wave_uniformis the part that can accept wrongly. Please add negatives: a value that looks uniform but differs per lane (VALU work of lane-varying input made under a narrowed EXEC and read after EXEC widens), and a first lane read of a value carried out of a divergent loop (#257).v_readfirstlaneand differs between the two host halves. Please say in the pull request how your change affects that question.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sandile-0511 on 2026-11-27T07:20:22Z.
The branch proposed above had no corpus comparison, so here is one, by output fingerprint as the issue asks.
Method. 21,608 containers (every shader the title registered in a run that reached the prologue world; 2,232 of them bound from the first world frame on), translated with
emit::translate_withand the native host's options, compared by outcome and by a fingerprint of the output. Branchfix/readfirstlane-split-waveat1d8fbb4against main2a3b03d1.The 32 newly refused:
emit). The 14 bound in the world frame:10e0fca4b7654380270113640b5b1b16394ca169444159314db97d2b4966329f67e290ca8506de9b7a4db0f35278deed7df5467e5cf1a7d594c051820454d80a9829383e53a675afb00825313cb610e3d44aa4cdbfb7d229d6a9a11ff71e32fcd8f399520433d6cfe7a20acb516d4013.3d4d03e134620340614095132867d1a06aa4ca1eaf496f5482c9d44f01942818) and 1 with "a lane mask holding lanes elsewhere read as data: a scalar operation not modelled" (10a21c93a949a4c5). None of the five was refused on main, so these look like a side effect of turning on the console lanes and the halves for a first lane read.The cullers.
5833c0c20c5bec9b,b30937e9de066fa9andff826810673a0198translate on main and on the branch, with new output. The other three are refused on both, for the cross-lane reason #253 lifts.Not checked yet: whether the 351 changed outputs differ only at first lane reads, and whether the 27 refusals are forced or could be narrowed. I am looking at both and will add what I find.
AI-assisted: sweeps and summary done with Claude Code on my own machine; I reviewed the text.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T08:07:52Z.
Thank you for running this, with the table. It is the comparison by output I asked marcus-4 for, and you have made it for them.
What it says to me: the branch does not gain a single shader, it changes 351 outputs, and it turns 32 shaders that translate today into refusals, 14 of them bound in the world frame. A refusal is not a wrong accept, but 14 world shaders dropped is not a fix for a black world. Five of the 32 are refused for reasons that have nothing to do with the first lane read, which you describe as a side effect of turning on the console lanes and halves.
Please add what you find on the 351 (only at the first lane reads, or elsewhere) and on whether the 27 own refusals can be narrowed. marcus-4, when you open the pull request, please put these numbers in the text and answer them.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by evan-kolberg on 2026-11-27T08:31:15Z.
I used Codex to rerun the same generated four-mask CPU test on Marcus’s second commit,
1d8fbb434aa6b5a94e9a346801fb25202f96e2e6, as Lakshan requested. The fixture calls the lockstep selector and evaluates the emitted index on the CPU. The only local additions were the fixture and its evaluator.All four matched on arm64 macOS with Rust 1.98.1. Thanks to Marcus for the fix and Lakshan for requesting the independent check. The result is limited to these generated lockstep indices. Hardware broadcast and the wider corpus regressions are outside this check.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by brainbudt on 2026-11-27T10:01:28Z.
/claim
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-27T10:01:39Z.
Claimed by @brainbudt. Open a draft pull request with
Refs #254in its description within 24 hours, or the claim expires.