Skip to content

fix(shader): let each half read a first lane a multiply-accumulate made - #606

Merged
pragyaangaur merged 1 commit into
pragyaangaur:mainfrom
hackgods:fix/read-alone-mac
Nov 28, 2026
Merged

pragyaangaur merged 1 commit into
pragyaangaur:mainfrom
hackgods:fix/read-alone-mac

Conversation

@hackgods

Copy link
Copy Markdown
Contributor

What was refused

ef154ebc9e03d1b1 has been refused since #522:

GFX10 code at 0xe98: a first-lane read of a value that may differ between 32-lane halves (wave64 on a 32-lane host) is not supported

Its one first-lane read is of a function of a constant-buffer value (s43, from s_buffer_load_dwordx4 s[40:43]), made in a region lanes are picked into by v_cmpx, and read back only by lanes of that region:

0x0890  v_mov_b32 v30, s43
0x08b8  v_mac_f32 v30, lit(0xc3960000), v29     ; v29: trunc of a multiple of s43
0x0968  v_fma_f32 v37, v30, s0, lit(0xbec00000)
0x0984  v_readfirstlane_b32 s15, v37
...
0x15fc  v_fmac_f32 v35, s15, v35                 ; deeper in the region

This is the read first_lane::read_alone (#552) exists for: the same in every lane of the read's EXEC, seen only by lanes of that EXEC, so each 32-lane half reads its own first active lane and nothing passes between the halves. The rule refused it at v_mac_f32: it left out every multiply-accumulate, because one adds to its old destination, an operand the rule did not look at.

The change

A multiply-accumulate (v_mac_f32, v_fmac_f32, v_mac_legacy_f32) counts where its old destination is held to the same as its operands: made under the read's own EXEC value from constants and scalars. Then every lane of that EXEC adds the same product to the same value. The 16-bit forms stay out, as before.

Effect

Over the 2,734 compute programs our runs passed:

main d1609d9 this branch
refused 68 66
ef154ebc9e03d1b1 refused 55,424 B, no group barrier
e1b8e701f0a02e50 refused 56,072 B, no group barrier
newly refused none
others translated differently none

Both were among the ten programs reported on #552 as refused again.

Tests

  • mac_read_in_a_region: v4 = f32(s14), v_mac_f32 v4, v7, v8 with v7 = 2.0, read from the first lane in an s_and_saveexec_b64 region and used there. v8 is 2.0, or the lane's thread id.
  • a_first_lane_read_of_a_multiply_accumulate_is_made_in_each_half: with v8 = 2.0 the program takes no exchange (on main: 5 group barriers, in halves); with the thread id it still runs in halves and passes the first lane between them.
  • a_first_lane_read_of_a_multiply_accumulate_matches_the_console_on_every_gpu: both forms, regions in the lower half, the upper half (groups of 64) and both (groups of 64 and 128): the region's lanes store 41.0 + 2.0 * 2.0, or 41.0 + 2.0 * the thread id of the console wave's first lane in the region; the others 0. It passes on main too (there through the exchange).
  • the_mac_read_programs_are_what_their_comments_say.
  • Under Wine on an M5 Pro (D3DMetal) the whole victory-shader suite passed: 485 passed, 0 failed, 5 ignored. That run was before a last change that reformatted two lines and reshaped one test; after it the three tests above were run again and pass. Native tests, clippy (x86_64-pc-windows-gnullvm, -D warnings, lib, tests and benches) and cargo xtask policy pass.

🤖 Generated with Claude Code

first_lane::read_alone lets each 32-lane half of a console wave make a
v_readfirstlane_b32 by itself where the value is the same in every lane
of the read's EXEC: made from constants and scalars by plain VALU
operations under that EXEC. It left out every multiply-accumulate,
because one adds to its old destination, an operand the rule did not
look at. ef154ebc9e03d1b1 reads a function of a constant-buffer value so
made, in a region its lanes are picked into:

  v_mov_b32 v30, s43
  v_mac_f32 v30, lit(0xc3960000), v29
  v_fma_f32 v37, v30, s0, lit(0xbec00000)
  v_readfirstlane_b32 s15, v37

So the read sent the program to halves, which refused it: its
dispatches have been dropped since pragyaangaur#522.

A multiply-accumulate (v_mac_f32, v_fmac_f32, v_mac_legacy_f32) now
counts where its old destination is held to the same as its operands:
then every lane of the read's EXEC adds the same product to the same
value.

ef154ebc9e03d1b1 and e1b8e701f0a02e50 translate again, with no group
barrier. Over the 2,734 compute programs our runs passed, no other
translates differently and none is newly refused: 68 refused, now 66.

Tests: a first-lane read of v4 += 2.0 * v8 in a region, its result used
there, takes no exchange where v8 is the same in the region's lanes
(before: 5 group barriers, in halves), and still passes between the
halves where v8 is the lane's thread id; both give the console's lanes
on hardware, in groups of 64 and 128.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@earpjonathan

Copy link
Copy Markdown
Contributor

@hackgods A question before we go further, since first-lane reads are your area.

On the play build (main + #589 #593 #605 #606 #608 #592 #595), nine wave64 compute programs on a 32-lane host are still refused as "a lane mask read as data": fa0bd10a35bfef3c, d7db57731ada919b, ce79e9eba7ae0348, be6ce5d849d51127, b4b0427232707598, abd7a22106898c66, 922e1d0ee1e19a85, 56f3bf2d9388e5a7, 466ec41d3f9d24c0. No open PR changes them, including #591 and #610. They're all one shape, in groups of 128 or 256 threads:

  • A vector compare writes a mask (not EXEC).
  • s_bcnt1_i32_b64 counts the mask.
  • A branch on SCC skips a block chain when no lane of the console wave has the bit.

That needs both halves' ballots, and the #511 meet is the way to get them in divergent code. They're refused because meet_regions wants the whole in-step region acyclic, and here loops come after the ballots.

We have a local meet.rs-only change:

  • A cyclic region is accepted, with sites only in its blocks that are in no cycle.
  • It's refused if a divergent block in it has s_barrier. meet_leave already resolves a site on the edge into a block that can't reach it, so a half leaves before such a loop.

With that change, the nine get past every mask site and then stop at v_readfirstlane_b32 of per-lane values in divergent blocks, outside loops. There are one to five such reads per program, feeding scalar compares or bit sets. Two of the mask sites read such a value. The other 30 programs we checked give byte-identical output.

What seems missing is a first-lane site in the same rendezvous. Each half publishes its first active lane's value and whether it has one, and the lower half's value wins when it has a lane.

Are you already working on something like that? If you are, we'll send you the meet.rs rule as a small separate PR or leave it with you, whichever you prefer. If you're not, would you be OK with us doing both pieces?

Separately, e4a9f58e3acfcf7b (#620 removes its descriptor refusal) now stops at a first-lane read inside an outer loop: eight reads of values made in the same pass, under the read's EXEC, from scalars. Its result is only read under a narrower EXEC in the same pass. Judging read_alone per pass, instead of requiring no cycle, would cover it. I'm mentioning it in case it fits your 9991 branch.

Prepared with Claude Code.

@hackgods

Copy link
Copy Markdown
Contributor Author

We're not working on a first-lane site in the meet rendezvous, so please go ahead with both pieces. The meet.rs rule and the site together make sense as one PR, or as two if the maintainer prefers.

Notes from our side that may save time:

  • The exact rule. It is what you describe: each half publishes its first active lane's value and whether it has an active lane, and the console's first active lane is the lower half's if that half has one, otherwise the upper half's. The case that a single-half read gets wrong is a half with no active lane in the region, which still has to take the other half's value. Our measurements and test forms for an alike value in a predicated block are in fix(shader): read an alike value in a predicated block from a lane that made it #605. Its tests cover regions in the lower half, the upper half and both, used inside and after the region, in groups of 64 and 128. They may be reusable for the new site.
  • Barriers. On main without a pair exchange, a separate exchange for value plus has-lane costs two dwords. Riding on the meet's existing rendezvous is the cheap way to do it.
  • e4a9f58e3acfcf7b. Judging read_alone per pass, rather than requiring no cycle, sounds right if the value is made, read and used within one pass under the read's EXEC. Our 9991 branch adds a counted-loop break shape but doesn't change read_alone's cycle rule, so it won't cover this one. Feel free to take it.

Claude Code (Claude Opus 5.5).

@earpjonathan

Copy link
Copy Markdown
Contributor

@hackgods A correction on e4a9f58e3acfcf7b. I suggested above that judging read_alone per pass would cover it, and that's wrong.

We tried it. The value read at 0x1ee8 is made under the read's EXEC from a s_buffer_load_dwordx4 whose offset comes from a loop-carried scalar: a header phi that one back edge leaves alone and another advances. In a divergent loop the halves can take different back edges, so they can hold different counters in the same pass and load different data. A per-pass rule therefore isn't exact for it. With that rule our dumps showed no change at all, so we dropped it. Getting e4a9f58e through would need a proof that the loop's back edges are taken uniformly across the group, which we aren't pursuing for now.

The first-lane site in the meet rendezvous is a separate matter, and it's being tested now. In our dumps it translates all nine "lane mask read as data" programs and five of the first-lane ones, and the other translations come out byte-identical. I'll post once it has run on the GPU and in game.

Prepared with Claude Code.

@hackgods

Copy link
Copy Markdown
Contributor Author

Thanks for checking, and agreed. If the halves can take different back edges in one pass, they hold different counters, so a per-pass read_alone can't be exact for e4a9f58e3acfcf7b without a proof that the back edges are taken uniformly across the group. Good to hear the meet first-lane site covers the nine "lane mask read as data" programs; I'll look at #648 too, since it should cover eeed82b877b44b5f, which declares all 32 KiB of LDS.

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.

3 participants