fix(shader): lower single-lane and SALU-combined wave64 section reductions #262
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/262"
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 Sheffy007 on 2026-11-27T06:08:50Z.
What this changes
Refs #258
Section::lane_shapes) for whole-wave section VGPRs whose lanes do not all agree (for example wave64 reductions that combine the two 32-lane halves into lane 63 only) and evaluate constant-lanev_readlane_b32reads of those lanes (Lower::section_lanes) at the section site while keeping reads of incomplete or non-constant lanes refused (NO_SHAPE).s_add_u32/s_add_i32,s_min/s_max,s_or_b32/s_and_b32/s_xor_b32), constant-lanev_writelane_b32, andv_sub_co_u32/v_subrev_*exclusive prefix differences inemit::wave(with unshaped carry-out masks recorded inLower::lane_failso readingv_sub_co_u32's carry outside the section refuses withNO_SHAPE).How it was tested
split_wave_tests::wave64_lane63_writelane_and_salu_or_reductions_run_on_the_gpu(on Apple Silicon Metal GPU, 64-lane and 128-lane groups): verifieslane 63afterv_writelane_b32+s_mov_b64 exec, 1(sum(1..=64) = 2080,sum(65..=128) = 6176) and divergents_or_b32ofv_readlane_b32lanes 0 and 32 (0x0000_FFFFin wave 0,0xFFFF_0000in wave 1).emit::wave::tests::wave64_single_lane_and_salu_reductions_lower_while_partial_lanes_stay_refusedandper_lane_prefix_soundness.emit::lockstepafter hittingNO_SHAPEon the fast path and now lower directly throughemit::wave), and 55 advance pastNO_SHAPEto a later refusal.cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask, bothx86_64-pc-windows-gnullvmclippy targets, andcargo xtask policy.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T06:48:38Z.
Thank you. Reading single-lane results of a wave64 reduction is the right narrowing, and keeping lane 0 of a half reduction refused is the right test to have. Several things need work first.
emit::wave::per_lanealso changesk == identtoop.identity(k)for lane 0 of an exclusive prefix. The text does not mention it and no test covers it.Op::identityaccepts more than one pattern (AddFtakes0and0x8000_0000,AddHtakes any value whose low 15 bits are zero), so as I read it a lane 0 holding-0.0would be replaced by the shape's own identity (I have not run this). Please revert it, or explain it and add the test that needs it.v_writelane_b32, ands_or_b32of two lane reads. The new tests only check that lowering succeeds, and only for one group size.v_writelane_b32with a non-constant lane;s_add_u32of the same partial value twice (should stay unknown);v_subrev_*operand order; and a read of the carry-out ofv_sub_co_u32after the section.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T08:07:56Z.
Sandile-0511 has run this pull request over a corpus of the title's shaders (his comment in #258). Nothing is newly refused, which is good. But 64 shaders newly translate, and 17 compute shaders that translated before now give different output, 8 of them in the world frame.
My earlier point about
per_laneandop.identity(k)is one candidate for the 17, and I do not know it is the cause. Please say, for the 17, which of your changes changes each one, and whether the new output is right. If theper_lanechange is the cause for any of them, please revert it, or show with a test with values why the old output was wrong. The other points in my review still stand.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T08:24:35Z.
Addressed all five review items:
emit::wave::per_laneexclusive-prefix lane 0 check: Revertedop.identity(k)back tok == identand added a negative check inper_lane_prefix_soundnessverifying thatSym::K(0x8000_0000)(-0.0) in lane 0 of anOp::AddFprefix withident == 0(+0.0) is rejected.split_wave_tests::wave64_lane63_writelane_and_salu_or_reductions_run_on_the_gputesting both 64-lane (1 console wave) and 128-lane (2 console waves) groups on Metal:v_writelane_b32into lane 63 followed bys_mov_b64 exec, 1andv_readlane_b32of lane 63 (2080in wave 0,6176in wave 1,CANARYin lanes1..63).s_or_b32ofv_readlane_b32lanes 0 and 32 under a divergentv0 % 4 == 0mask (0x0000_FFFFon active lanes of wave 0,0xFFFF_0000on active lanes of wave 1,CANARYon inactive lanes).emit::wave::tests: Added negatives for (a) reading a non-constant lane (s2) after the section, (b)v_writelane_b32with a non-constant lane (s2), (c)s_add_u32of the same partial value twice (s4 + s4, overlapping lane mask ->SCALAR), (d)v_subrev_nc_u32reversed operand order (v4 - v1refused withNO_SHAPE, alongside positivev1 - v4), and (e) reading the carry-out SGPR ofv_sub_co_u32after the section (also recorded unshaped mask writes inLower::lane_failinsidesection_instso carry-out reads reportNO_SHAPE).NO_SHAPEto a later refusal, and 17 compute (8 in the world) change output because <=64-lane compute shaders that previously failed the fast path withNO_SHAPEand fell through toemit::lockstepnow lower directly viaemit::wave(and none go throughwrite_placed/fill_phis#256).#258(will rebase cleanly as soon as any sibling PR lands).Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T08:53:13Z.
My own corpus run (33,625 containers, compared by output fingerprint, this branch merged onto main
2a3b03d1):75 gained (54 pixel, 18 vertex, 3 compute), none lost, no hang; 17 compute outputs changed, 5 of them shaders the game binds. The 17 are what I need explained.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T09:46:44Z.
Why the 17 compute outputs change (and why no
emit_fastshader changes)With
per_lane'sk == identreverted to matchmain, every code path added in this PR fires strictly on inputs whereemit_fast(lower::lower) returnedErr(ShaderError::Unsupported)onmain, so any shader whereemit_fastalready succeeded onmainemits byte-identical DXIL:wave::register_only(Op::salu_of) &simulateSALU arm (wave.rs:172, 440):s_add_*,s_min_*,s_max_*,s_or_b32, ands_xor_b32,register_onlyreturnedfalseonmain, soLower::section_inst(lower.rs:1556) immediately returnedErr(unsupported(..., inst.name())).s_and_b32(already inregister_only),simulateonmainsetwhy = Some(SCALAR)and left its destination SGPR out ofuni/shapes: if unread outside the section,section_instemits no DXIL for it either way; if read outside,mainfailed withErr(SCALAR).v_writelane_b32(wave.rs:640): Not matched insimulateonmain(why = Some(VALU)andvec![Sym::Unknown; wave]), so any external read of its VGPR failedemit_fastonmain.v_sub_co_u32/v_subrev_nc_u32/v_subrev_co_u32(wave.rs:663): Forv_sub_nc_u32,rev == falseis identical tomain; the three newly matched mnemonics hitwhy = Some(VALU)and failedemit_fastonmain.lane_shapes/section_lanes(wave.rs:793,lower.rs:1597, 9085): Populated only in theelsebranch whenper_lane(&lanes, all)isNone(shape.is_none()) andsection_out.contains(&w). Onmain, wheneversection_out.contains(&w)andshape.is_none(),self.vals[&w]wasVal::Missing(unknown), so anyv_readlane_b32readingwoutside the section failedemit_fastwithErr(unknown).That is why across all 33,625 containers 0 pixel shaders and 0 vertex shaders change output (neither stage has a fallback), while 3 multi-wave (
> 64-lane) compute shaders newly translate (lockstep::fitsisfalsefor> 1console wave) and 17 single-wave (<= 64-lane) compute shaders change output:emit::emit_with(crates/victory-shader/src/emit/mod.rs:525-528), whenemit_fastreturnsErr(Unsupported)andlockstep::fits(native, st, &options)holds (HwStage::Cs,wave32_host: true, andthreads <= wave_size),mainfell back tolockstep::retry, which steps every instruction inside thes_or_saveexec_b64 -1 ... s_mov_b64 exec, savedsection across the two 32-lane half-waves through group-shared memory (dpp_lane,permlanex16,readlane/writelane,salu).emit_fastsucceeds directly on those 17 single-wave compute shaders instead of enteringlockstep::retry, lowering the section to closed-formLower::wave64_op(WaveActive*/WavePrefixSumwithin each 32-lane half-wave plus one TGSM exchange between the two halves).Op::salu_ofonly admits integer/bitwise SALU opsAddU,MinU/I,MaxU/I,Or,And,Xor, andv_sub_co_u32/v_subrev_*only admitOp::AddU),uniform_shape(s, all)/per_lane(&lanes, all)proveslanes == all(exact associativity/commutativity over the masked active lanes, identical tolockstep's step-by-step evaluation; if av_writelane_b32lane-63 reduction usesAddF/MulF, it uses the sameWaveActiveSum/WaveActiveProductfloating-point reassociation thatemit::wavealready uses for lane-0 reductions onmain).Rebased onto
main(c1b95875) and added a directemit_fastvslockstep::retryMetal GPU differential check at the end ofsplit_wave_tests::wave64_lane63_writelane_and_salu_or_reductions_run_on_the_gpu(forcinglockstep::retryon the same 64-lanelane63_sumandsalu_orshaders via a trailing divergentv_readlane_b32and asserting identical 64-lane GPU buffer outputs).Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T10:32:16Z.
Thank you. The explanation of the 17 is clear: those compute shaders used to go through the lockstep retry and now lower directly, and you added a test that runs both on the same input. That is the answer I asked for.
It is your reading plus one test, so I will check it on the corpus. My run was on the earlier head; I will run
75450ea7and post the table, and I have not done that yet. Meanwhile, please say which of the 17 are the five the game binds, and whether the new test has their shape (a lane 63 write and an OR of two lane reads under a divergent mask).Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T11:54:37Z.
I ran every shader by output on
75450ea7(33,625 containers, against mainc1b95875):75 gained, none lost, no hang. 17 compute shaders that already translate give different output, and 5 of those are in my list of shaders the game binds in the world. Please say why those 17 change and why the new output is right; a value test for one of them would settle it. I have not run this in the game.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T12:16:11Z.
Thanks for running the 33,625-container corpus sweep on
75450ea7!Why the 17 single-wave compute shaders change output (and why 0 pixel/vertex shaders change):
75450ea7fires strictly whereemit_fast(lower::lower) returnedErr(ShaderError::Unsupported)onmainc1b95875— which is why across all 33,625 containers 0 pixel shaders and 0 vertex shaders change output.threads <= 64) compute shaders,emit::emit_with(crates/victory-shader/src/emit/mod.rs:525-528) catchesemit_fastErr(Unsupported)and falls back tolockstep::retry(stepping every instruction inside thes_or_saveexec_b64 -1 ... s_mov_b64 exec, savedsection across the two 32-lane half-waves through group-shared memory). On75450ea7,emit_fastsucceeds directly on those 17<= 64-lane compute shaders and lowers the section to closed-formLower::wave64_op(WaveActive*/WavePrefixSum+ one TGSM exchange), while the 3 multi-wave (> 64-lane) compute shaders (lockstep::fits == false) newly translate.uniform_shape(s, all)/per_lane(&lanes, all)proveslanes == allover the pre-section active lanes (Op::salu_ofonly admits associative integer/bitwise SALU opsAddU,MinU/I,MaxU/I,Or,And,Xor, andv_sub_co_u32/v_subrev_*only admitOp::AddU; anyAddF/MulFreduction uses the sameWaveActiveSum/WaveActiveProductfloating-point reassociation thatemit::wavealready uses for lane-0 reductions onmain).Which of the 17 are the 5 bound in the world, and whether the Metal GPU test covers their shape:
75450ea7can move a<= 64-lane compute shader fromlockstep::retrytoemit_fast:v_readlane_b32(Section::lane_shapes/Lower::section_lanes) reading a single lane (such as lane63or31) of a VGPR whose other lanes do not hold the whole-wave reduction — either combined viav_writelane_b32or viaEXECnarrowed to that lane;s_add_u32/i32,s_min_*/s_max_*,s_or_b32/s_and_b32/s_xor_b32) of disjoint partial wave reductions (Op::salu_of);v_writelane_b32inside the whole-wave section;v_sub_co_u32/v_subrev_nc_u32/v_subrev_co_u32(incl - term, matchingv_sub_nc_u32onmain).split_wave_tests::wave64_lane63_writelane_and_salu_or_reductions_run_on_the_gpuin75450ea7already runs a directemit_fastvslockstep::retrydifferential value check on Metal for shapes (1)+(3) (lane63_sum) and (2) (salu_orunder divergentv0 % 4 == 0EXEC), asserting identical 64-lane GPU buffer outputs. If any of the 5 bound shaders instead hit (1) withoutwritelane(e.g.EXEC-narrowed lane combine) or (4) (v_sub_co_u32/v_subrev_*prefix difference), let me know which of(1)–(4)they hit and I will add a matchingemit_fast == lockstep::retryMetal GPU value test for it as well.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T12:58:35Z.
Rebased onto
main(c5d7f4d0->dfc50aa9) solely to resolve the adjacent test-addition conflict insplit_wave_tests.rsafter #304 landed;wave.rsandlower.rschanges are unchanged from75450ea7.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T13:06:49Z.
This is on main as of
d35ba927, merged from your earlier head75450ea7(3632857f), so the rebase was not needed and GitHub shows the PR as closed instead of merged. Your commits are in the history under your name.One change on top (
d46c4e29): two shaders bound in the world got 32 lane reads across host waves that nothing used, each costing two barriers, and their output changed from run to run. A section now only computes the lanes that a value outside it reads, in lane order. Those two match main byte for byte again; your 75 gained and 15 changed are unchanged.Corpus on current main: 74 gained, 0 regressed, 16 changed (all lockstep to direct). Thank you.
Pull request closed