Skip to content

fix(shader): make the first-lane reads #522 refused exactly, and drop #548's per-host-wave fallback - #552

Merged
pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
hackgods:fix/548-first-lane-exact
Nov 27, 2026
Merged

pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
hackgods:fix/548-first-lane-exact

Conversation

@hackgods

Copy link
Copy Markdown
Contributor

#548 is not exact

#548 handles a program that halves refuse at a first-lane read (FIRST_ACROSS) by lowering it again without #522's halves trigger. Each 32-lane host wave then reads its own first lane. On the console, every lane of a wave64 reads the wave's first active lane. The upper host wave's first lane is console lane 32 or later. Wherever the value differs between the halves, the upper half reads something the console never reads.

Counterexample: #548's own test program. in_a_loop(&[v_readfirstlane_b32 s12, v0], 12, Count::FirstLane) without its v_mbcnt, one group of 64 threads:

v_readfirstlane_b32 s21, v0      ; the loop count
loop: v_readfirstlane_b32 s12, v0
      s_add_u32 s20, s20, 1; s_cmp_lt_u32 s20, s21; s_cbranch_scc1 loop
v_mov_b32 v1, s12                ; stored by every lane
  • On the console, thread 0 is the wave's first lane, so every lane stores 0.
  • On main (b407fa0), D3DMetal on an M5 Pro stores 32 in lanes 32–63. The upper host wave's first lane is thread 32: its loop runs 32 times, and it reads s12 = 32.
  • In a group of 128, lanes 96–127 store 96 where the console stores 64:
AMD Compatibility Mode (halves): 32 lanes of 64 differ from the console
AMD Compatibility Mode (halves): lane 32: [32], the console [0]
AMD Compatibility Mode (halves): 64 lanes of 128 differ from the console
AMD Compatibility Mode (halves): lane 96: [96], the console [64]

a_first_lane_read_in_a_loop_matches_the_console_on_every_gpu (new) runs this program on every adapter wherever it translates, and compares each lane with the console. It fails on b407fa0 and passes on this branch.

In the game. The programs #548 brings back that we inspected read values made from scalars:

  • 23d507cbb6a22e86 and b0095eb50222e808: a cascade loop counter that steps on constant-buffer data.
  • eeed82b877b44b5f: a phi of s_bfe results across an s_cselect branch.
  • ef154ebc9e03d1b1: a function of s43 under a v_cmpx.
  • 8bb272e763f76148: a reciprocal of a constant-buffer divisor.

By inspection, every lane that computes one of these values computes it alike. So we found no wrong in-game output from #548. Two cases were not checked:

  • a half with no active lane, whose reads take a stale lane wherever the result is used after the region;
  • 7b2c3c1db2298f44, 9991e29feaa26e3e and the other five, which were not inspected.

Nothing in #548 checks any of this. The fallback applies to every program refused at FIRST_ACROSS, including ones like the test above.

The fix

  1. Refuse again a first-lane read the halves cannot make (reverts fix(shader): fall back to the host waves' first lanes where halves refuse a first-lane read #548's lower_limited retry and first_lane_halves flag). fix(shader): fall back to the host waves' first lanes where halves refuse a first-lane read #548's test now asserts the refusal, with and without v_mbcnt.
  2. Make each half read its own first active lane only where that is provably exact (first_lane::read_alone). This brings back 8bb272e763f76148 and its twin 91caf91cfd11d512 exactly. Since fix(shader): fix v_readfirstlane_b32 in branches and empty EXEC on split wave64 (#512) #522, main refused them at 0x1d8:
GFX10 code at 0x1d8: a first-lane read of a value that may differ between 32-lane halves (wave64 on a 32-lane host) is not supported

The program is a wave64 compute program with 1024 threads and 12 KiB of LDS. It divides by a constant-buffer value s21 the way the console compiler does, inside a v_cmpx region that also holds a loop, so the halves cannot meet there:

v_cvt_f32_u32 v2, s21
v_rcp_iflag_f32 v2, v2
v_mul_legacy_f32 v2, 0x4f7ffffe, v2
v_cvt_u32_f32 v2, v2
v_readfirstlane_b32 s35, v2

lane_uniform counts a guarded VALU write as alike only where no lane can be inactive, so v2 is "maybe different". It is different only in lanes the read never takes. read_alone proves three things for a v_readfirstlane_b32:

  1. The value is the same in every lane the read can take. It comes from constants and scalars, by plain VALU operations under the read's own EXEC value. The scalars are root parameters and what s_load* / s_buffer_load* and scalar ALU operations make of them, never lane masks, SCC or lane reads. Each half computes them for itself, as every halves translation already assumes for every scalar.

  2. Only lanes active at the read see the result. Every reader of it, or of a scalar made from it, is one of two things:

    • a scalar ALU operation, held to the same rule;
    • a vector instruction writing no scalar register, under an EXEC dword no wider than the read's. That is the greatest set closed under compares into EXEC, s_and_saveexec, s_and_b64 / s_andn2_b64 of such a mask, s_mov_b64 copies and phis.

    No branch, scalar memory access, or EXEC / VCC / M0 write takes the result.

  3. The read is in no cycle, so its EXEC value is one mask.

So a half with an active lane reads the console's value. A half without one reads a stale lane that none of its lanes ever sees. Such reads no longer put a program in halves. In halves, they read the half's own first active lane (first_lane_in_wave with the guard), with no exchange. Every other read is unchanged.

The 10 other programs #548 brings back are refused again on this branch, as on #522 (listed below). Bringing them back exactly needs a uniformity proof lane_uniform does not make yet: a loop-carried counter (its fixed point starts from nothing), and scalars from s_buffer_load (it trusts only root parameters).

Before/after

Over all 2,734 compute programs the title passed in our runs:

main b407fa0 this branch
refused 58 68
8bb272e763f76148, 91caf91cfd11d512 translated by the fallback the same DXIL (8,160 B each, what d8f884e made), now proven
b538852f58c99307 halves: 2,693 group barriers, 728 KB per-lane: 0 barriers, 165 KB (what d8f884e made)
translated differently 144 programs, none newly in halves

The 10 programs refused again are the other ones #548's fallback brought back in our shader pack. #548 lists 7, but in ours it brings back 12. The 10 are:

23d507cbb6a22e86, 26efd2ba098464bd, 7b2c3c1db2298f44, 9991e29feaa26e3e, b0095eb50222e808, b05fc3d9cc6f248e, b33d65393cc79e79, e1b8e701f0a02e50, eeed82b877b44b5f, ef154ebc9e03d1b1

#522 refused all of them. The 144 programs that translate differently make first-lane reads #522 passed between the halves, or put the program in halves for, in each half instead. Their DXIL shrinks, b538852f58c99307 by 77%. That program is the main part of #522's GPU-time regression (see the issue draft).

Tests

  • first_lane_reads_halves_cannot_pass_are_refused (fix(shader): fall back to the host waves' first lanes where halves refuse a first-lane read #548's test, now asserting the refusal).

  • a_first_lane_read_in_a_loop_matches_the_console_on_every_gpu: fails on b407fa0 (32 of 64 and 64 of 128 lanes differ), passes here (the program is refused).

  • readfirstlane_of_a_scalar_in_branch: s_buffer_load_dword s14, then v4 = f32(s14) + 1.0 and v_readfirstlane_b32 s12, v4 in an s_and_saveexec region with an s_cbranch_execz, then v2 = s12 inside the region (or, in the second variant, after EXEC is restored).

  • a_first_lane_read_only_active_lanes_see_matches_the_console_on_every_gpu covers four cases, with and without halves:

    • the active lanes in the lower half;
    • the upper half only (the lower host wave has no active lane);
    • both halves;
    • none.

    Every lane gives what the console gives: 42.0 where active, else 0.

  • a_first_lane_read_only_active_lanes_see_is_made_in_each_half: read inside, the program takes no exchange. Read after EXEC is restored, it still passes the halves' first lanes and ballots between them.

  • Under Wine on an M5 Pro (D3DMetal), the whole victory-shader suite passes on this branch: 477 passed, 0 failed, 5 ignored. Native tests, clippy (x86_64-pc-windows-gnullvm, --all-targets, -D warnings) and cargo xtask policy pass.

🤖 Generated with Claude Code

hackgods and others added 2 commits November 27, 2026 16:51
…st wave's own

pragyaangaur#548 lowers a program that halves refuse at a first-lane read
(FIRST_ACROSS) once more without pragyaangaur#522's halves trigger, so each 32-lane
host wave reads its own first lane. Nothing proves that lane holds the
console's value: on the console every lane of a wave64 reads the wave's
first active lane, and where the value differs between the halves the
upper host wave reads another one.

pragyaangaur#548's own test program shows it: v_readfirstlane_b32 s21, v0 counts a
loop of v_readfirstlane_b32 s12, v0, and every lane stores s12. The
console stores 0 in all 64 lanes of a group of 64 (thread 0 is the
wave's first lane). With the fallback, D3DMetal on an M5 Pro stores 32
in lanes 32-63: the upper host wave's first lane is thread 32.

So such a read is refused again, as pragyaangaur#522 did. A new GPU test runs the
program on every adapter wherever it translates and compares each lane
with the console, and pragyaangaur#548's test now asserts the refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lanes see

v_readfirstlane_b32 reads a value not proven the same in every lane of
the console wave, and refuses such a read where the halves cannot pass
values (FIRST_ACROSS). The proof (lane_uniform) counts a guarded VALU
write as alike only where no lane can be inactive, since inactive lanes
keep the old value. So a read of a value computed under the read's own
EXEC is refused in divergent code, though every lane the read can take
holds it.

In GTA VI that refused 8bb272e763f76148 and 91caf91cfd11d512 (1024
threads, LDS 12 KiB): an integer division by a constant-buffer value,
whose reciprocal the console compiler computes in VALU from the scalar
divisor and reads from the first lane, inside a v_cmpx region holding a
loop, where the halves cannot meet. It translated before pragyaangaur#522 (d8f884e,
d2b4889); since 05db3d0 it is refused and its dispatches are dropped.

A read is now made by each host half by itself (first_lane::read_alone)
where:

1. every instruction that made the value ran under the read's own EXEC
   value and computed it by plain VALU operations from constants and
   scalars: root parameters and what scalar loads and scalar ALU
   operations make of them, never a lane mask or a lane's value; so every
   lane active at the read holds the same function of the same scalars,
   which each half computes for itself in any halves translation;
2. only lanes active at the read see the result: every reader of it (or
   of a scalar made from it) is a scalar ALU operation, held to the same,
   or a vector instruction writing no scalar register under an EXEC no
   wider than the read's (compares into EXEC, s_and_saveexec, ANDs,
   s_andn2 and copies of such masks, and their phis); no branch, scalar
   memory access, or EXEC, VCC or M0 write takes it;
3. the read is in no cycle.

Then a half with an active lane reads the console's value, and a half
without one reads a value none of the readers in its lanes runs to see.
Such reads no longer put a program in halves, and in halves they read
the half's own first active lane with no exchange.

Of the 2,734 compute programs in the title's shader pack, two now
translate again (8bb272e763f76148's translation is byte for byte what
d2b4889 made) and none is newly refused. 144 others translate
differently: first-lane reads that pragyaangaur#522 passed between the halves, or put
the program in halves for, are now made by each half. b538852f58c99307,
in halves since pragyaangaur#522 for one such read (2,693 group barriers), is again
what d8f884e made (none); group barriers drop by 4,119 in all.

Tests: the read of a scalar-made value in an s_and_saveexec region gives
the console's lanes on hardware with the active lanes in the lower half,
the upper half (the lower host wave has none), both, or none, with and
without halves; read after EXEC is restored it still passes the halves'
first lanes between them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pragyaangaur
pragyaangaur merged commit 738e3d0 into pragyaangaur:main Nov 27, 2026
1 check passed
@4TheDoctor

Copy link
Copy Markdown
Contributor

A heads-up from an M3 Max (D3DMetal 4.0b3, CrossOver 26.1, Series X). With #548's fallback dropped, six world programs are refused again on main a31fcca1, and the cell interior goes dark again.

Same recipe for both runs (diagnostics build, no per-list timing, captures at 0 to +400 world frames):

program c8e4f326 (before #522) a31fcca1
avgLuminanceCompute:ColorDownsampleCS (b0095eb50222e808) translated refused: "a first-lane read of a value that may differ between 32-lane halves"
avgLuminanceCompute:ColorDownsampleInitCS (23d507cbb6a22e86) translated refused, same
cascadeshadows_classify:CS_ShadowTileBalanceRTCost (7b2c3c1db2298f44) translated refused, same
cascadeshadows_classify:CS_ShadowTileClassify (9991e29feaa26e3e) translated refused, same
tiled_lighting:CS_PrescalarizeLightLoop (11eeb27c2d15980d) translated refused, same
vfx_proj_decal_hit_detection:CS_ComputeHitDetectionSkinnedResults (eeed82b877b44b5f) translated refused: "a descriptor's address or format read as data"
CS_DefragHeap_ProcessFreeRequests (8bb272e763f76148) translated translated (exact now, thanks)

In game on a31fcca1, the player is a black silhouette and the cell interior is unlit at +300 and +400 world frames. Only the yard through the window is lit. On c8e4f326 she and the cell were lit at the same points. It looks like what I measured with a refuse-only fix on #512, and what #548 measured: missing passes cost more than an inexact first lane. No hangs or ended command buffers in 720 s; world frame 604 ms (cold shader cache).

So on a 32-lane host these reads still don't reach the exact path. Could the fallback stay until they do? I can test any variant in game on the M3 Max.

@hackgods

Copy link
Copy Markdown
Contributor Author

Thanks, that is a clear report, and the dark cell matters more than the speed of any of this.

The five programs refused at the first-lane read are the ones #548's fallback translated by reading each 32-lane half's own first lane. In these programs the read value looks alike in every lane, because it is built from scalars: the luminance downsample's loop counter steps on constant-buffer data, and the classifiers' values come from s_buffer_load constants. That would also be why the fallback gave a lit picture here. But lane_uniform does not prove it today, and the fallback also applies to reads where the halves do differ (#548's own test program gives 32 of 64 lanes a value the console never reads).

So the fix we are working on makes lane_uniform prove these reads exactly, not refuse them:

  • bound its operand check to the operand's own registers (it currently trips on unrelated higher registers);
  • count full-dword float and conversion ops as full overwrites;
  • read a scalar load's result once for both halves at the load, which is what the console's single load per wave does, even with a racing store.

We are checking offline which of the five that recovers, then will test them in game on an M5 Pro and open a PR. eeed82b877b44b5f is a different refusal (a descriptor's address or format read as data) and needs its own look.

If you'd rather have the lit picture back on main until that lands, restoring #548's fallback is a single revert of that part of #552. That's your call; we can prepare it either way. Testing variants on your M3 Max once the PR is up would help a lot.

Claude Code (Claude Opus 5.5).

@hackgods

Copy link
Copy Markdown
Contributor Author

Status after checking the six programs offline (translated with main a31fcca1):

  • 11eeb27c2d15980d (tiled_lighting prescalarize): recoverable exactly. Its first-lane reads are register-index waterfall loops (v_readfirstlane → v_cmp_eq → s_and_saveexec → s_mov m0 → v_movrels/movreld → s_xor exec → loop). Each half visits each of its own lanes once, and the body only moves per-lane registers. A rule that recognises that shape (with no scalar stores, atomics, barriers or cross-lane ops in the body) translates it. That comes first. Note: here it is this one that is refused as "a descriptor's address or format read as data" (0x484), and eeed82b877b44b5f at the first-lane read (0x3dc).
  • eeed82b877b44b5f (decal hit detection): recoverable exactly once scalar loads are read once for both halves and the proof covers phis and s_bfe_u32 of them. Medium work.
  • 23d507cbb6a22e86, b0095eb50222e808 (ColorDownsample*), 7b2c3c1db2298f44, 9991e29feaa26e3e (shadow tiles): no static proof covers them, even an unsound maximal one. Their remaining first-lane reads take typed-buffer loads (buffer_load_format_x) that come after image stores in the same program (e.g. 0x8ac before 0xb64 in 23d5). A store from one half can land between the two halves' loads, so nothing proves the loads alike. The exact route we are building is a per-dispatch check: a variant translated on the assumption that those loads agree is used only when nothing the dispatch writes overlaps the ranges it relies on, and otherwise the dispatch runs today's translation. Given the auto-exposure downsample is among them, these four are likely the dark cell.

Claude Code (Claude Opus 5.5).

@earpjonathan

Copy link
Copy Markdown
Contributor

The same on an M5 Pro (48 GB, macOS 27.0.1, CrossOver 26.3 Wine with the repo's patches, D3DMetal 4.0 beta 3, Series X), on main a31fcca1 against d424152a (#548), with the same scripted opening for both. Each build ran twice, and I judged the second (warm) run:

When the per-dispatch PR is up, I'll run it the same way: whether these six translate, the late cell brightness, and the frame time.

Prepared with Claude Code.

@earpjonathan

earpjonathan commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

More on the dark cell: on an M5 Pro it comes mostly from 11eeb27c2d15980d (tiled lighting), not from the six programs refused at the first-lane read.

I ran two local builds of d1609d9d, each with the same scripted opening, twice, and judged the second (warm) run. Neither build is meant for main:

A B
Mean brightness (out of 255) of the last six captured world frames, in the cell 17, 22, 22, 14, 12, 12 49, 57, 46, 45, 44, 45
Median frame 385 ms 375 ms

With B the cell interior (the characters, the bunks, the walls) is lit as it was before #522, in the cold run as well as the warm one. So the waterfall rule for 11eeb27c2d15980d that you put first should bring most of the light back by itself. When it's up I'll run it the same way, with and without the per-dispatch work for the other four.

Prepared with Claude Code.

@hackgods

Copy link
Copy Markdown
Contributor Author

Thanks, that is a very useful split. Then 11eeb27c2d15980d is the one to land first. Its fix is the register-index waterfall rule described above: each half's loop visits each of its own lanes once with m0 = that lane, and the body only moves per-lane registers. It is exact, translates the program byte for byte as before #522, and changes no other of the 2,734 compute programs. It is finishing its full Wine suite now; we will open it as a PR right after.

The other part is ready too: while-shaped divergent loops in lockstep in halves, which recovers the two luminance downsample programs and four more refused ones exactly, with fewer barriers overall. On our M5 Pro both luminance programs translated in game with it. Results with both on the cell will follow.

Claude Code (Claude Opus 5.5).

hackgods added a commit to hackgods/VIctory-macOS that referenced this pull request Nov 27, 2026
…er with halves-once-slots

The exchange the group runs once leaves out its leading barrier
(VICTORY_HALVES_OWN_WORDS, on by default), so pragyaangaur#552's test expects three
more barriers than the read alone with it on and four with it off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@earpjonathan

Copy link
Copy Markdown
Contributor

More data for the lockstep-loops part: the six programs still matter for how the cell looks, even with 11eeb27c2d15980d exact.

Two local builds of d1609d9d, both lit, on the same scripted opening, judged on the warm run:

C B
Mean brightness of the last six captured world frames 49, 51, 55, 45, 45, 46 49, 57, 46, 45, 44, 45
Median frame 374 ms 375 ms

The brightness and speed match, but the pictures don't. In the cell at the same moment of both runs, C draws the round table's top almost black, and its walls and characters come out cooler and a little dimmer. B looks as builds from before #522 did. I can't tell from frames which of the six makes the difference. The auto-exposure downsample and the shadow tiles would fit, though. I'll run your lockstep-loops PR the same way and compare these frames.

Prepared with Claude Code.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants