Repository navigation
gfx2hlsl: lower s_nand_b32, s_nor_b32 and s_xnor_b32 on lane masks - #2459
Conversation
s_nand_b32, s_nor_b32 and s_xnor_b32 were refused whenever an operand was a lane mask. Bit i of ~(S0 op S1) depends only on bit i of each source, so with every source a lane mask (vcc_lo, exec_lo or an SGPR holding a compare mask) and an SGPR destination the lane's bit of the result is exact. SCC is (the whole complemented word != 0), which the bits of the other lanes decide. It is not emitted: the write is recorded as an SCC effect that is not modelled, and the existing stale-SCC scan refuses a program that can read it before the next SCC write, through joins and back edges. The mask mark follows program order. At a branch target a marked SGPR stays proven only if its last write dominates the target and no block on a path from that write to the target writes it again; a complement on any other marked SGPR is refused until the register is written again. A mask mixed with a plain word or a constant, and a vcc_lo, exec_lo or m0 destination, stay refused. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Five mutations: three caught, two survive — and I could not establish that either survivor is a gapBaseline:
The three caught rows are the ones that decide correctness: SCC must stay untouched on the lane-complement path (the complement of a mask does not carry the operation's carry semantics), a mask joined at a branch target is not provably per-lane, and a mix of mask and ordinary sources must fall back to the word path. Good. The two survivors, stated as unexercised rather than as gaps
If you want that line pinned, the cheapest case is one where a non-complemented op takes a mask destination and mask sources, e.g. after One note on the width bound
AI-assisted; the five mutations and the probe were run locally on Windows 11 build 26300.9974. |
tedin7
left a comment
There was a problem hiding this comment.
Review of head d94dae5 (Linux checks only; no Windows run by me).
What I checked
- Diff scope:
tools/startup/gfx2hlsl.py,tests/startup_gfx2hlsl_sop2_complement_check.py,docs/experiments.md. Small, additive, no CI/launcher/hook files. - Logic: the lane-mask complement is only taken when every source is a lane mask and the destination is a single SGPR (
gfx2hlsl.py~2415-2426), so per-lane bit i is exact. SCC is not emitted anddescriptor_scc_readsnow refuses any program that can read it before the next SCC write.unjoined_masks(dominance + re-write on a path to the branch target) looks right to me for forward joins and back edges. - Duplicates: no other open PR covers this. #1925 and #561 (merged) cover only the non-mask 32-bit forms; #2030 (closed) was the earlier lane-mask attempt.
- Sibling tables: the author notes
s_not_b32/s_orn2_b32on lane masks andand/or/xorof a mask with a plain word still emit a per-lane SCC. Those are out of scope here, fine as a follow-up.
Commands run (all passed)
python3 -B tests/startup_gfx2hlsl_sop2_complement_check.py-> PASSpython3 tests/docs_links_check.py-> 19 files, 465 links OKpython3 tests/bidi_controls_check.py-> OKgit diff --check origin/main...HEAD-> clean- Mutation 1: disable the
join_masksrefusal (line 2425) -> test FAILS ("a mask that another path into the branch target changes is refused"). - Mutation 2: accept
anymask source instead ofall(line 2422) -> test FAILS (mixed/constant/mask-destination forms stay refused). - Worktree restored with
git checkout -- .after each.
Notes
- The PR body states there is no device evidence: the test evaluates the emitted lines on the CPU only. There is no author-posted Windows result on this head, so I make no claim about Windows. A D3D12 WARP readback against the CPU reference would be the stronger guard for a shader lowering; the maintainers can decide if that is needed before merge.
- No P1/P2 correctness issue found.
AI-assisted; commands listed were executed locally.
tedin7
left a comment
There was a problem hiding this comment.
Review of head 5239256 (comment only).
Checked against origin/main:
Stage.unjoined_masksplus the newjoin_maskstrack whether a lane-mask mark holds at a branch target (dominating last write, no rewrite on a path into the target). The refusal fires only for the complement ops, so other lowerings are unchanged.- The lane-complement path accepts only SGPR destinations <= s105 with every source a lane mask (vcc_lo, exec_lo, or a marked SGPR). Mixed mask and plain word, constants, and vcc_lo/exec_lo/m0 destinations stay refused. Bit i of ~(a op b) depends only on bit i of each source, so the per-lane lowering is exact.
- SCC is not emitted for this form (other lanes decide it).
descriptor_scc_readsnow yields the effect kind and refuses any SCC read reachable before the next SCC write, also through branches and back edges. Thedescriptormessage text is unchanged. - No other open or merged PR covers this slice (#1925 merged the plain word form, #2030 and #1928 closed).
Commands run in a worktree on this head:
python3 tests/startup_gfx2hlsl_sop2_complement_check.py-> PASSpython3 tests/docs_links_check.py-> passpython3 tests/bidi_controls_check.py-> passgit diff --check origin/main...HEAD-> clean- Mutation:
scc=None if lane_complement else sccchanged toscc=scc-> the check FAILS (16 failing lines) - Mutation: joined-mask refusal disabled -> the check FAILS
Note: like the sibling complement checks, the test runs the emitted lines against an integer reference per lane, not a D3D12 WARP readback. The lowering is a single (~(a op b)) & 1 on one-bit lane values, so I consider that enough here.
AI-assisted; commands listed were executed locally.
|
Win2025 WARP receipt for head 5239256 (dev + release, pipeline run 2026-11-25). Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit
Note: tests/startup_run_sh_check.py returned the skip code (77, no POSIX shell on the VM); it is not touched by this PR. AI-assisted; commands listed were executed locally. |
Main now carries R0Y00#2459, R0Y00#2461, R0Y00#2466, R0Y00#2483, R0Y00#2428 and others in their final form; the conflicts take main's versions. Kept from this branch: R0Y00#2460's generalized unjoined(), R0Y00#2432's scalar(mods), the s_setreg MODE check, the cube image kind, the v_cvt_f64_f32 inline check. Upstream chose R0Y00#2428 for v_{add,sub,subrev}_co_ci_u32, so the local co_ci handlers are renamed unbound_* and their two checks unregistered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
What / Why
gfx2hlsl.pyrefuses the three scalar complements (s_nand_b32,s_nor_b32,s_xnor_b32) whenever an operand is alane mask (
<op> on a lane mask). The guard was added because the per-lane model keeps one bit of a mask, while theguest SCC is (whole 32-bit complemented word != 0), which a one-lane projection cannot show.
The destination value has no such problem: bit i of
~(S0 op S1)depends only on bit i of each source. With everysource a lane mask the lane's bit of the result is exact. Only SCC is not.
Change
Accepted:
s_nand_b32/s_nor_b32/s_xnor_b32with an SGPR destination (s0..s105) when every source isvcc_lo,exec_loor an SGPR holding a lane mask. EmitssD = (~(a op b)) & 1;and marks sD as a mask.SCC is not emitted. The instruction records an SCC effect that is not modelled, and the existing stale-SCC scan
(the one used for SCC from descriptor data) refuses the program if any instruction can read that SCC before the
next SCC write, through joins and back edges:
scc from a lane-mask complement in <reader>. The scan now carriesthe kind of the stale write; the descriptor message is unchanged.
The mask mark follows program order. Because it is now the proof for accepting the form, a marked SGPR stays
proven at a branch target only if the block of its last write dominates the target and no block on a path from
that write to the target writes the register again. Otherwise the complement is refused
(
<op> on a lane mask joined at a branch target) until the register is written again. Other users of the markare unchanged.
Still refused (
<op> on a lane mask): a mask next to a plain word or a constant, avcc_lo,exec_loorm0destination,
vcc_hi/exec_hi.docs/experiments.md: the paragraph on the 32-bit scalar complements states the accepted and refused forms.Tests
tests/startup_gfx2hlsl_sop2_complement_check.py(extended, pure Python):lanes of 196 edge-word pairs against
(~(a op b) >> lane) & 1, SCC left at its sentinel.existing lane-mask refusal controls (5 per operation) pass unchanged.
s_and_saveexec_b32accepted; SCC write then read accepted; an SCCread refused in 4 shapes per operation (direct
s_cselect_b32,s_cbranch_scc1, a read behind a jump that skipsthe SCC write, a read at a loop head reached over the back edge).
before the split and on no arm is accepted across the join; a rewrite after the join proves it again; an
overwritten mask is refused as a plain word.
Commands and results (Windows, Python 3.11+ venv):
Not run: the
binaryanddecodemodes (need llvm-mc / libLLVM), and the device checks.Measured
First refusal per request over 124 vertex/pixel requests captured locally, translated with this change on top of a
local merge of open translator PRs (the requests do not reach these instructions on main alone):
s_nand_b32 on a lane maskv_cndmask_b32v_readfirstlane_b32requires a proven uniform sourceimage_gather4_lzrequires a constant samplerAll 62 first refusals are one shape: two compare masks, the complement, then
s_and_saveexec_b32, whichoverwrites SCC. None of the 62 was refused by the join rule at that complement. The SCC scan runs only on a program
that translates to the end, and all 62 still stop at a later, unrelated refusal, so it has not run on them. Static
count instead: over the 300 complement instructions in the 120 pixel programs that contain one, the same scan run
with every complement marked stale finds no SCC reader (0 programs). Later complements in these programs are not
reached yet, so the join rule is measured on the first one only.
Limits
s_cselect_b32 sN, -1, 0) is still refused.s_not_b32ands_orn2_b32on lane masks are accepted and emit aper-lane SCC, although their guest SCC has the same whole-word property; and a 32-bit
and/or/xorof a lane maskwith a plain word emits
(mask op word) & 1, which reads bit 0 of the word for every lane.🤖 Generated with Claude Code