Skip to content

gfx2hlsl: lower s_nand_b32, s_nor_b32 and s_xnor_b32 on lane masks - #2459

Merged
tedin7 merged 2 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-complement-lane-mask
Nov 25, 2026
Merged

tedin7 merged 2 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-complement-lane-mask

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

What / Why

gfx2hlsl.py refuses the three scalar complements (s_nand_b32, s_nor_b32, s_xnor_b32) whenever an operand is a
lane mask (<op> on a lane mask). The guard was added because the per-lane model keeps one bit of a mask, while the
guest 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 every
source 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_b32 with an SGPR destination (s0..s105) when every source is
    vcc_lo, exec_lo or an SGPR holding a lane mask. Emits sD = (~(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 carries
    the 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 mark
    are unchanged.

  • Still refused (<op> on a lane mask): a mask next to a plain word or a constant, a vcc_lo, exec_lo or m0
    destination, 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):

  • 5 all-mask forms per operation: exact emitted line, destination marked, no SCC line; the line evaluated on all 32
    lanes of 196 edge-word pairs against (~(a op b) >> lane) & 1, SCC left at its sentinel.
  • Negative controls: 8 mixed, constant and mask-destination forms per operation stay refused, nothing emitted; the
    existing lane-mask refusal controls (5 per operation) pass unchanged.
  • Whole programs: compare, compare, complement, s_and_saveexec_b32 accepted; SCC write then read accepted; an SCC
    read refused in 4 shapes per operation (direct s_cselect_b32, s_cbranch_scc1, a read behind a jump that skips
    the SCC write, a read at a loop head reached over the back edge).
  • Join rule: a mask whose write a branch skips, and a mask the loop body overwrites, are refused; a mask written
    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.
  • Mutations: join rule disabled accepts both refused programs; SCC read scan disabled accepts the read.

Commands and results (Windows, Python 3.11+ venv):

python -B tests/startup_gfx2hlsl_sop2_complement_check.py      -> PASS (80 checks ok)
python -B tests/startup_translator_check.py                    -> exit 0
python -B tests/startup_gfx2hlsl_*_check.py (each, no arguments) -> 63 of 63 files exit 0 (with the two above and startup_triage_run_check.py)
python -B tests/docs_links_check.py                            -> Checked 19 Markdown files and 465 links
git diff --check                                               -> clean

Not run: the binary and decode modes (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):

first refusal before after
s_nand_b32 on a lane mask 62 0
SDWA form of v_cndmask_b32 30 60
descriptor operand holds no symbol (constant sampler after a join) 28 58
v_readfirstlane_b32 requires a proven uniform source 4 4
image_gather4_lz requires a constant sampler 0 2

All 62 first refusals are one shape: two compare masks, the complement, then s_and_saveexec_b32, which
overwrites 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

  • No device evidence: translation and CPU evaluation of the emitted lines only.
  • A complement mixing a lane mask with a word whose bits are all equal (s_cselect_b32 sN, -1, 0) is still refused.
  • The join rule applies only to the complement. Other operations that read the program-order mask mark are unchanged.
  • Not changed, noted for review: s_not_b32 and s_orn2_b32 on lane masks are accepted and emit a
    per-lane SCC, although their guest SCC has the same whole-word property; and a 32-bit and/or/xor of a lane mask
    with a plain word emits (mask op word) & 1, which reads bit 0 of the word for every lane.

🤖 Generated with Claude Code

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>
@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Five mutations: three caught, two survive — and I could not establish that either survivor is a gap

Baseline: startup_gfx2hlsl_sop2_complement_check.py PASS, whole startup_gfx2hlsl_* family green.

mutation result
the scc=None rule dropped (scc=None if lane_complement else scc → always scc) caught
the joined-mask refusal disabled caught
is_lane_mask sources no longer force the general path caught
the SGPR destination bound loosened (dst[1] <= 105 → <= 106) survived
word_complement dropped from lane_complement (any op with a mask dst and mask sources takes the complement path) survived

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

dst[1] <= 105 → 106. There is no fixture register at that boundary, so nothing distinguishes them today. Whether 106 is actually wrong depends on how the SGPR index maps to the emitted declaration; I did not establish a difference, so I am not calling it a gap — only unasserted.

word_complement dropped. I tried to show a behaviour change with s_and_b32 (non-complemented, mask destination, mask sources), comparing the emitted lines with and without the mutation. My probe did not reach the branch: the stage's masks set is empty unless a mask register is first introduced, so is_lane_mask was false and both versions emitted an empty line. That is my harness limitation, not evidence of equivalence — and not evidence of a gap either.

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 s_mov_b64 exec, -1 so exec is in masks, then s_and_b32 s[10:11], vcc, exec, asserting the emitted expression is the plain & and not the complemented form. That is the shape the word_complement conjunct exists to exclude, and it currently has no fixture.

One note on the width bound

dst[1] <= 105 is the same constant the shift-add path uses at line 2428 (dst[1] > 105), so the two are consistent — worth a named constant or a comment saying why 105, because a future SGPR-count change would need both.

AI-assisted; the five mutations and the probe were run locally on Windows 11 build 26300.9974.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 and descriptor_scc_reads now 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_b32 on lane masks and and/or/xor of 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 -> PASS
  • python3 tests/docs_links_check.py -> 19 files, 465 links OK
  • python3 tests/bidi_controls_check.py -> OK
  • git diff --check origin/main...HEAD -> clean
  • Mutation 1: disable the join_masks refusal (line 2425) -> test FAILS ("a mask that another path into the branch target changes is refused").
  • Mutation 2: accept any mask source instead of all (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.

legacy-code-archaeologist added a commit to legacy-code-archaeologist/Xbox2Windows that referenced this pull request Nov 25, 2026
's nand/nor rules)

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

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head 5239256 (comment only).

Checked against origin/main:

  • Stage.unjoined_masks plus the new join_masks track 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_reads now yields the effect kind and refuses any SCC read reachable before the next SCC write, also through branches and back edges. The descriptor message 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 -> PASS
  • python3 tests/docs_links_check.py -> pass
  • python3 tests/bidi_controls_check.py -> pass
  • git diff --check origin/main...HEAD -> clean
  • Mutation: scc=None if lane_complement else scc changed to scc=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.

@tedin7

tedin7 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

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 523925679a6a527afae1231f79e0abb695249021:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target d3d12_xs_device_obj d3d12_xs_shim -- -j 2): 0.45s (configure 0.00s)
  • Dev ctest: 0/0 passed (no downstream ctest targets for this change; builds + Python checks only).
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target d3d12_xs_device_obj d3d12_xs_shim -- -j 2): 1.01s (configure 0.00s)
  • Windows Python checks: tests/mesh_heap_sampler_check.py ok (0.5s), tests/startup_buffer_address_check.py ok (0.3s), tests/startup_compute_request_offline_check.py ok (0.3s), tests/startup_compute_translate_check.py ok (0.2s), tests/startup_flat_param_check.py ok (0.1s), tests/startup_gfx2hlsl_add_sub_co_check.py ok (0.1s), tests/startup_gfx2hlsl_add_sub_co_ci_check.py ok (0.1s), tests/startup_gfx2hlsl_bit_alu_check.py ok (0.5s), tests/startup_gfx2hlsl_capture_check.py ok (0.7s), tests/startup_gfx2hlsl_cmp16_check.py ok (0.2s), tests/startup_gfx2hlsl_compare_dst_check.py ok (0.1s), tests/startup_gfx2hlsl_compute_check.py ok (0.2s), tests/startup_gfx2hlsl_cselect_mask_check.py ok (0.1s), tests/startup_gfx2hlsl_cvt_alu_check.py ok (0.1s), tests/startup_gfx2hlsl_f32_lg_check.py ok (1.0s), tests/startup_gfx2hlsl_f32_nan_check.py ok (3.9s), tests/startup_gfx2hlsl_handler_arity_check.py ok (0.1s), tests/startup_gfx2hlsl_image_arity_check.py ok (0.1s), tests/startup_gfx2hlsl_image_get_lod_check.py ok (0.1s), tests/startup_gfx2hlsl_int_med3_check.py ok (0.1s), tests/startup_gfx2hlsl_join_symbols_check.py ok (0.1s), tests/startup_gfx2hlsl_loop_constants_check.py ok (0.1s), tests/startup_gfx2hlsl_mask_overwrite_check.py ok (0.1s), tests/startup_gfx2hlsl_operand_count_check.py ok (0.1s), tests/startup_gfx2hlsl_readfirstlane_recompute_check.py ok (0.1s), tests/startup_gfx2hlsl_readfirstlane_uniform_check.py ok (0.1s), tests/startup_gfx2hlsl_refusals_check.py ok (0.3s), tests/startup_gfx2hlsl_root_check.py ok (0.5s), tests/startup_gfx2hlsl_saveexec_same_reg_check.py ok (0.1s), tests/startup_gfx2hlsl_scalar_brev_fold_check.py ok (0.1s), tests/startup_gfx2hlsl_scalar_m0_check.py ok (0.1s), tests/startup_gfx2hlsl_sdwa_cvt_check.py ok (0.2s), tests/startup_gfx2hlsl_sgpr_spill_check.py ok (0.1s), tests/startup_gfx2hlsl_shift64_check.py ok (0.1s), tests/startup_gfx2hlsl_smem_offset_check.py ok (0.1s), tests/startup_gfx2hlsl_sop2_complement_check.py ok (4.7s), tests/startup_gfx2hlsl_sopk_accum_check.py ok (0.6s), tests/startup_gfx2hlsl_sopk_signext_check.py ok (0.1s), tests/startup_gfx2hlsl_swap_check.py ok (0.2s), tests/startup_gfx2hlsl_vopc_sdwa_check.py ok (3.5s), tests/startup_gfx2hlsl_waitcnt_depctr_check.py ok (0.1s), tests/startup_gfx2hlsl_watch_check.py ok (2.3s), tests/startup_image_2d_array_check.py ok (0.1s), tests/startup_image_3d_check.py ok (0.1s), tests/startup_image_gather_lz_check.py ok (0.4s), tests/startup_image_load_d16_check.py ok (0.7s), tests/startup_image_sample_a16_check.py ok (0.1s), tests/startup_image_sample_b_check.py ok (0.1s), tests/startup_image_sample_c_check.py ok (0.2s), tests/startup_image_sample_l_check.py ok (0.1s), tests/startup_image_sample_lod_check.py ok (0.1s), tests/startup_inline_sampler_check.py ok (0.1s), tests/startup_run_ps1_check.py ok (7.9s), tests/startup_run_sh_check.py FAIL (0.1s), tests/startup_sampler_heap_check.py ok (0.1s), tests/startup_smem_address_check.py ok (0.1s), tests/startup_stream_fetch_check.py ok (0.2s), tests/startup_stream_interface_check.py ok (0.2s), tests/startup_translator_check.py ok (0.3s)
  • Release ctest: 0/0 passed (same scope).
  • sccache: 0/2 hits; wall 37.2s

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.

@tedin7
tedin7 merged commit 2cc59ae into R0Y00:main Nov 25, 2026
5 checks passed
legacy-code-archaeologist added a commit to legacy-code-archaeologist/Xbox2Windows that referenced this pull request Nov 25, 2026
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>
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