shader: how should pixel_aot represent v_cmpx (EXEC-writing compares)? Proposed first stage on the #651 region #932
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows#932
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?
@MasterDomino on GitHub · 2026-11-23 17:44 UTC · original
Question
How should
tools/pixel_aot.pyrepresentv_cmpx_*, the compares that write their result to EXEC? What is the smallest first stage you would accept?lower()rejects everyv_cmpx_*form today. Indocs/coverage/gfx1013.tsvthey are 95 VOPC (_e32), 95 VOP3 (_e64) and 62 SDWA forms, 252 of the 1,968 forms. #338 listed them among the rejected forms. #651 then landed the stage-one masked region (s_and_saveexec_b64from a tracked comparison, optionals_cbranch_execzto the matching restore,s_or_b64 exec, exec, s[a:b], per-lane selects at the restore). I found no issue or PR since then that coversv_cmpx.I would like to propose, and then implement, the stage below. I won't start without your direction.
Proposed first stage
Accept the region opener that LLVM 15 emits for gfx10.3:
as equivalent to the existing
v_cmp_<op>_<type> vcc, …+s_and_saveexec_b64 s[a:b], vccopener. It would reuse the current region state, body restrictions and restore-time selects.v_cmp_*already lowers (f32, f16, i16/u16, i32/u32, i64/u64 andclassf16/f32), in both_e32and_e64. That is 156 forms. f64 (34 forms) and SDWA (62 forms) stay rejected.s_mov_b64save immediately before thev_cmpx, and the optionals_cbranch_execzonly at the matching restore.v_cmpxuse stays rejected. That includes av_cmpxwith no saved EXEC, and so the whole-program discard pattern.Evidence so far
The ISA definition. The AMD "RDNA 2" ISA reference guide §6.2.2 (p. 42 of 283) says: "All V_CMPX instructions write the result of their comparison (one bit per thread) the EXEC mask." §12.9 "VOPC Instructions" (p. 152) gives e.g.
V_CMPX_GT_F32: EXEC[threadId] = (S0 > S1). The same section 6.2.2 says "A thread only writes a result if the associated bit in the EXEC mask is set to 1." This is the RDNA 2 guide, not a gfx1013-specific document.gfx1013 has no separate destination on
_e64. LLVM 15.0.7llvm-mc -mcpu=gfx1013assemblesv_cmpx_gt_f32_e32 v12, v13,v_cmpx_gt_f32_e64 v12, v13,v_cmpx_gt_f32_e64 v12, s44andv_cmpx_class_f32_e64 v12, 3, and rejects an explicitexecors[0:1]destination on_e64.v_cmpx_gt_f32_sdwaassembles; the DPP form is rejected ("dpp variant of this instruction is not supported").Where LLVM 15 emits it (
llc -O2, wave64, a pixel shader with a non-flattenableif):s_mov_b64 s[0:1], exec/v_cmpx_gt_f32_e32 v0, v1/ … /s_or_b64 exec, exec, s[0:1].v_cmp_gt_f32_e32 vcc, v0, v1/s_and_saveexec_b64 s[0:1], vcc, which #651 already handles.llvm.amdgcn.kill, LLVM 15 emits nov_cmpxfor either target.So LLVM itself doesn't produce this pattern for gfx1013. I don't know whether the compiler that built the gfx1013 programs uses
v_cmpx, or how often. An aggregate count from someone with a local input, like the counts in #711, would set the priority.Inference, not measured. The proposal assumes the inactive-lane rule (a thread writes only if its EXEC bit is set), so the new mask is
EXEC_old & cond. That would make the opener equivalent tos_and_saveexec_b64from a full entry mask. LLVM's gfx10.3 rewrite ofv_cmp+s_and_saveexecintos_mov+v_cmpxdepends on the same equivalence. I haven't checked this on hardware.Questions for you
s_mov_b64save +v_cmpx" as an alternative region opener the right first stage? Or would you rather modelv_cmpxas a general EXEC write first?EXEC_old & conddefinition acceptable, given the ISA text and the LLVM equivalence, or do you want separate evidence?_e32and_e64land together, or one encoding first?Completion condition (for the first stage)
llvm-mc/llvm-objdumpfor gfx1013, using thepixel_aot_check.py binarypath.FORM_COVERAGE_RECORD, runs the cmpx-opened regions on WARP:R32G32B32A32_UINTtarget._e64, a source swap on a non-commutative comparison.v_cmpxwith no preceding save, a save that isn't immediately before it, nesting, f64, SDWA, and a body instruction outsideREGION_ALU.Required environment
The translation and LLVM 15 round trip run on Linux. The WARP draws need Windows (CI on the PR). No physical GPU and no game files.
AI-assisted.
@MasterDomino on GitHub · 2026-11-23 17:54 UTC · original
Aggregate counts from one local input, for the priority question above. It is the same population as #711: 12,101 PS records. Only counts here; no names, paths or disassembly from the input.
Method.
tools/extract_shaders.py --stage pixel --format native, then decoded withtools/disasm_rdna2.py(whattools/scan_unsupported_shaders.pyuses), ata8a88e1. Cross-check: on a random sample of 300 records, LLVM 15.0.7 (pixel_aot.disassemble, gfx1013) over the same byte ranges gives the samev_cmpxcount (3,764 in both). The Python decoder stops at the firsts_endpgm, so the numbers below are lower bounds.v_cmpx_*v_cmpx_*instructions_e32173,316,_e64885, no SDWAeq_u3253,180 ·lt_f3239,471 ·ne_u3226,800 ·gt_f3221,063 ·lg_f3211,281 ·lt_i324,499 ·gt_u324,468 ·ge_f323,256v_cmpxdirectly preceded bys_mov_b64 s[a:b], execs_mov_b32 s, exec_lo), in 6,677 recordsWhat the first EXEC write after a saved
v_cmpxis (58,198 savedv_cmpx; the wave32_b32/exec_loforms are counted with their_b64equivalents):s_mov_b64 exec, s[a:b](restore)s_andn2_b64 exec, s[a:b], exec(else branch)v_cmpx(nested or chained)s_and_saveexec_*(nested)s_mov/s_andto EXEC from another register)s_or_b64 exec, exec, s[a:b]So this compiler restores with
s_mov_b64 exec, s[a:b], not thes_or_b64 exec, exec, s[a:b]the proposal assumed. 14,450 of the restores, in 4,379 records (36%), have the shape below: no branch in the body other than at most ones_cbranch_execz. I did not check the branch target or whether the body stays withinREGION_ALU.Revised first-stage proposal. Same as above, except that the region closes with either
s_mov_b64 exec, s[a:b]ors_or_b64 exec, exec, s[a:b]naming the saved pair. With a full entry mask and theEXEC_old & conddefinition, the region mask is a subset of the saved mask, so the two restores give the same EXEC. That again rests on the inactive-lane inference, not a hardware measurement. Unchanged from above:_e32and_e64;s_andn2_b64 exec, saved, exec), chainedv_cmpx, f64 and SDWA stay rejected.The else branch and nesting are the next most common shapes, and I'd suggest them as later stages.
Side observation: the 64-bit shifts in #928 are rare in this input. Only
v_lshlrev_b64occurs: 255 records (2.1%), 3,083 instructions.AI-assisted.
@R0Y00 on GitHub · 2026-11-23 18:30 UTC · original
The structured opener fits #651. Please start with VOPC
_e32and already-supported 32-bit comparisons. Allow the immediate EXEC save and exacts_mov_b64 exec, savedrestore. The existing OR restore is also equivalent for lane selection when the inner mask is a subset of the saved mask. Keep the two restore instructions distinct for SCC: MOV preserves it and OR writes it. CMPX does not write VCC. Preserve those observable values or reject readers of any state this profile cannot track.Use the known entry mask and the existing pure per-lane body, no nesting or side effects, and verify any optional EXECZ branch targets the actual restore instruction. Under that supported entry state, intersect the compare result with the incoming EXEC. General partial-entry masks, chained CMPX, wave32 save/restore, kill/else, and _e64 can follow separately if independently validated. The 4,379 records are structural candidates until the target and body checks run.
Please include LLVM 15 gfx1013 binary round-trips and mixed/empty/full bit-exact WARP cases. Keep the reversed predicate, ignored mask, wrong branch target, overwritten save pair, and unsupported body controls. WARP establishes the translated behavior of the source-built fixture.
@MasterDomino on GitHub · 2026-11-23 18:46 UTC · original
Taking the first stage you scoped above. It's up as #957: VOPC
_e32v_cmpxon the binary32 and 32-bit integer predicates (32 forms) with the immediates_mov_b64save; thes_mov_b64ands_or_b64restores kept distinct for SCC; VCC untouched; the optionals_cbranch_execzchecked against the actual restore.Re-measured with those rules, as you asked, on the same local input (aggregates only): 3,719 records have the save +
v_cmpx+ restore shape, but only 152 regions in 112 records (0.9%) also pass the existing #651 body rule. The rest are blocked mainly by scalar loads,s_nop, conversions andv_rcpin the body. Details are in the PR.The WARP draws will run for the first time in that PR's CI.
AI-assisted.
@MasterDomino on GitHub · 2026-11-23 18:51 UTC · original
Proposal for the next stage, which I'd like to take after #957: widen the masked-region body. This is about the body, not the opener.
The #651 body rule (
REGION_ALU:v_mov_b32and binary32add/sub/subrev/mul/min/max) is what blocks almost all real regions. Same local input as above (aggregates only), counting the regions #957 accepts structurally: save + f32/i32/u32_e32v_cmpx+ restore of the same pair. That is 10,827 regions in 3,719 records.I added body instructions one at a time, greedily, and leave out any region containing a further branch (nesting stays out of scope):
REGION_ALUv_fmac_f32_e32,v_mul_legacy_f32_e32v_cvt_f32_ubyte2_e32v_rcp_f32_e32,v_rsq_f32_e32v_cvt_f32_ubyte1_e32,v_fma_f32,v_sqrt_f32_e32,v_med3_f32,v_mul_legacy_f32_e64v_mac_f32_e32,v_mad_f32,v_cvt_f32_ubyte0/3)A record also needs everything outside its regions to translate, so these count regions, not translatable shaders.
Proposed stage 2. Admit to the region body the per-lane binary32 VALU forms that
lower()already handles outside regions, each through its existing lowering and the existing restore-time select:v_fmac,v_macv_mul_legacy(e32/e64)v_cvt_f32_ubyte0..3v_rcp,v_rsq,v_sqrtv_fma,v_mad,v_med3Like the current body, they write only VGPRs, read only VGPRs, SGPRs and constants, and have no side effects or traps. A false lane's speculated value, including a NaN or infinity from
rcp/rsq/sqrtout of domain, is discarded by the select. The accumulator forms (fmac/mac) read their destination, so a destination still conditional from an earlier region would reject, as reads do today.Out of this stage:
s_mov_b32,s_waitcnt, scalar loads). They run regardless of EXEC but are skipped with the region bys_cbranch_execz;ds_swizzle;Questions for you:
I won't start until #957 has your verdict, so the two don't overlap.
AI-assisted.
@MasterDomino on GitHub · 2026-11-23 19:18 UTC · original
#957 is merged, thanks. Starting stage 2 as proposed above. The body set is exactly the listed per-lane binary32 forms, in the encodings measured:
v_fmac_f32_e32,v_mac_f32_e32,v_mul_legacy_f32_e32/_e64,v_cvt_f32_ubyte0..3_e32,v_rcp/v_rsq/v_sqrt_f32_e32,v_fma_f32,v_mad_f32andv_med3_f32. Each form gets its own in-region draw: inner and outer values differ per lane, and a wrong reference that ignores the mask must mismatch. Please redirect if you'd prefer a different set or one draw per family.AI-assisted.
@R0Y00 on GitHub · 2026-11-23 19:19 UTC · original
The scoped stage one from this thread merged in #957. The translated source-built cases passed on WARP and RTX 4060 Laptop, and LLVM 15 round-tripped the gfx1013 binary fixtures. The tests cover mixed, empty and full masks, both restore forms, genuine skip targets and wrong-predicate/wrong-mask controls. This closes the VOPC e32 32-bit structured region stage. General EXEC writes, wave32 save/restore, nesting, class forms and e64 remain separate.