Skip to content

startup: lower s_nand_b32, s_nor_b32 and s_xnor_b32 in gfx2hlsl - #1925

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
MasterDomino:startup/gfx2hlsl-sop2-complement-b32
Nov 24, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
MasterDomino:startup/gfx2hlsl-sop2-complement-b32

Conversation

@MasterDomino

@MasterDomino MasterDomino commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

What / Why

Fixes #1919. Part of #870.

tools/startup/gfx2hlsl.py lowered the 64-bit scalar complement logic ops (s_nand_b64, s_nor_b64, s_xnor_b64) and the 32-bit s_andn2_b32/s_orn2_b32, but not s_nand_b32, s_nor_b32 or s_xnor_b32. pixel_aot already lowers them (#561) and the decoder prints them (#1078). With #1915 applied, 12 requests in one local shader cache stop at unsupported: s_nor_b32.

Change

One line in the SCA table, next to the other 32-bit logic ops:

's_nand_b32': ('~({0} & {1})', '$ != 0'), 's_nor_b32': ('~({0} | {1})', '$ != 0'), 's_xnor_b32': ('~({0} ^ {1})', '$ != 0'),

The expression text is the same as pixel_aot's SCALAR entries. SCC follows the existing logic-op rule (result != 0). The results stay 32-bit uint. Lane-mask forms are refused (review follow-up, 9b620015): a vcc_lo/exec_lo operand or destination, or an SGPR holding a compare mask, is the whole wave's mask, and guest SCC is set when any bit of the complemented 32-bit result is, which the per-lane projection cannot show. These now fail with <op> on a lane mask.

New tests/startup_gfx2hlsl_sop2_complement_check.py, registered as startup_gfx2hlsl_sop2_complement. It is also added to the platform-independent Python job, to the LLVM 15 job (binary and decode) and to the AGENTS.md list. docs/experiments.md has a short paragraph on it.

Tests

Base: upstream/main 27cf223. Windows 11 build 26300, Python 3.13.1, LLVM 15.0.7 (llvm-mc and LLVM-C.dll).

  • ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_sop2_complement$' -V: passed.
  • ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_': 28/28 passed, with no skips.
  • python -B tests/startup_gfx2hlsl_sop2_complement_check.py: PASS. It covers:
    • The encoder matches the LLVM 15 encodings recorded in rdna2_tools_check.py.
    • The table text and SCC rule equal pixel_aot's.
    • 11 value forms × 2,184 operand pairs match an integer reference for both the result and SCC, for each op. The forms are SGPR, in place on either source, one register on both sides, inline 0/1/-1, a literal on either side and an m0 destination. The values are 0, all ones, 0xaaaaaaaa/0x55555555, every single bit and its complement, and mixed words. SCC starts at 2, so a missing SCC write fails. The value set produces both SCC = 0 and SCC = 1 for every op.
    • The 11 numeric forms emit the s_and_b32/s_or_b32/s_xor_b32 lines with the operation wrapped in ~( ), with the same bookkeeping; the 4 vcc_lo/exec_lo source and destination forms, and an SGPR mask source, are refused with <op> on a lane mask.
    • In-check mutants must fail.
    • All 19 SOP2 ops the decoder names that still have no lowering are refused by name.
  • python -B tests/startup_gfx2hlsl_sop2_complement_check.py binary <llvm-mc 15.0.7>: llvm-mc assembles 90 decoded SOP2 forms to the same words. PASS.
  • python -B tests/startup_gfx2hlsl_sop2_complement_check.py decode <LLVM-C.dll 15.0.7>: LLVM disassembles all 90 forms as tools/disasm_rdna2.py does. PASS.
  • Pure-Python checks: startup_gfx2hlsl_operand_count, _mad, _capture, _smem_offset, _scalar_m0, _bit_alu, _shift64 and _op_refusals, plus startup_translator, startup_flat_param, docs_links and bidi_controls. All exit 0.

Recount (aggregate only). The req_* files of one local shader cache were copied into a scratch directory and translated offline: 188 requests, 116 distinct native programs. s_nor_b32 appears 11 times in 4 programs, s_nand_b32 2 times in 1 program and s_xnor_b32 not at all.

Translator Translated s_nor_b32 refusals Next refusal for those requests
main 90 0 (the 12 stop earlier at v_cmpx_le_i32_sdwa) -
this branch 90 0 (same as main) -
#1915 head (800a8e3) 90 12 -
#1915 head + this change 90 0 v_cmp_class_f32 (6 -> 18)

#1915 has since merged. With the lane-mask gate, the 12 loading requests now stop at s_nor_b32 on a lane mask: in this cache 7 of the 11 s_nor_b32 instances read vcc_lo (6 as the first source, 1 as the second) and 4 are numeric, so the numeric slice alone does not move them. Translated counts and all other refusal counts are unchanged (90 translated). The branch is rebased onto 7ca344dc and startup_gfx2hlsl_* passes 31/31.

Negative controls

In-check mutants replace the table entry in memory. Each one fails:

s_nor_b32 mutation without the complement fails: ('s_nor_b32 s3, s1, s2', '0x0', '0x0', (0, 0), (4294967295, 1)): ok
s_nor_b32 mutation without the SCC write fails: ('s_nor_b32 s3, s1, s2', '0x0', '0x0', (4294967295, 2), (4294967295, 1)): ok

The same results hold for s_nand_b32 and s_xnor_b32.

Manual source mutants edit tools/startup/gfx2hlsl.py, run the check and restore the file. Each exits 1:

== nor without complement: exit 1
  s_nor_b32: 11 forms x 2184 pairs equal the reference (result and SCC): MISMATCH
    ('s_nor_b32 s3, s1, s2', '0x0', '0x0', (0, 0), (4294967295, 1))
== nand without complement: exit 1
== xnor without complement: exit 1
== xnor without SCC: exit 1
  s_xnor_b32: 11 forms x 2184 pairs equal the reference (result and SCC): MISMATCH
    ('s_xnor_b32 s3, s1, s2', '0x0', '0x0', (4294967295, 2), (4294967295, 1))

Run against main's gfx2hlsl.py, the check exits 1. Each form reports refused: s_nor_b32 (or s_nand_b32/s_xnor_b32).

Not covered

  • No D3D12 draw. The check evaluates the emitted HLSL on the CPU. The HLSL is the same text pixel_aot emits. The scalar draw in tests/pixel_aot_check.py covers that text with s_nand_b32/s_nor_b32/s_xnor_b32 mutants. I did not rerun that draw for this change. No newly translated pair exists to compile, because the affected requests stop at the next refusal.
  • Lane-mask forms stay refused. Lowering them needs the whole-wave mask and SCC from the full 32-bit result (for example a wave-level reduction), shown on a mixed-mask consumer such as s_cselect/s_cbranch_scc for both outcomes. That would be its own slice, and it is what the 12 loading requests need.
  • A wrong operand count on any SCA op still raises IndexError instead of Unsupported. This predates the change and applies to every table entry.
  • The recount does not show that any request now translates end to end. The 12 affected requests stop at the next refusal.

🤖 Generated with Claude Code

The scalar table had the 64-bit complement logic ops and the 32-bit
s_andn2_b32/s_orn2_b32, but not the 32-bit s_nand_b32, s_nor_b32 and
s_xnor_b32. Add them with pixel_aot's lowering text and the logic-op SCC
rule (SCC = result != 0).

tests/startup_gfx2hlsl_sop2_complement_check.py decodes SOP2 words built
here, translates them and evaluates the emitted lines against an integer
reference over edge values (result and SCC); checks the mask and
vcc_lo/exec_lo forms against the non-complement sibling; runs mutants
without the complement and without the SCC write; keeps every SOP2 op
with no lowering refused by name; and has binary (llvm-mc) and decode
(LLVM C API) round-trip modes.

Fixes R0Y00#1919

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

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The numeric forms, complements and SCC controls pass on this exact head. Please narrow the new lowering to numeric operands and destinations unless the whole-mask effect can be proven. The mask controls currently pin the sibling HLSL: it reduces the complemented result to a single lane before computing SCC. Guest SCC is true when any bit of the whole 32-bit result is set, so on a mixed VCC mask it can be 1 even for a pixel whose projected result is 0. A following s_cselect or s_cbranch_scc can observe the difference. Please refuse the lane-mask forms for this slice, or show correct behavior for both outcomes on a mixed-mask consumer. Check whether the 12 reported requests use the numeric slice after gating it.

Review follow-up: a vcc_lo/exec_lo operand or destination, or an SGPR
holding a compare mask, is the whole wave's mask, and guest SCC is set when
any bit of the complemented 32-bit result is; the per-lane projection
cannot show that. s_nand_b32, s_nor_b32 and s_xnor_b32 now lower only
numeric operands and destinations; the check refuses all mask forms
(including an SGPR mask source) and keeps the numeric reference, sibling
and mutation controls.

Refs R0Y00#1919

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

Copy link
Copy Markdown
Collaborator Author

Narrowed to numeric operands and destinations in 9b620015:

  • Refused: s_nand_b32, s_nor_b32 and s_xnor_b32 with a vcc_lo/exec_lo operand or destination, or an SGPR holding a compare mask, now fail with <op> on a lane mask.
  • Check updated: all 5 mask forms per op are refused, including the SGPR-mask source. The numeric forms keep the integer reference (11 forms × 2,184 pairs, result and SCC), the sibling comparison and the mutation controls.
  • Other checks: startup_gfx2hlsl_* 31/31; binary with llvm-mc 15.0.7 PASS.

The 12 requests after gating: they now stop at s_nor_b32 on a lane mask. In this cache, 7 of the 11 s_nor_b32 instances read vcc_lo (6 as the first source, 1 as the second) and only 4 are numeric, so the numeric slice does not move them. Translated and other refusal counts are unchanged (90 translated). The description is updated.

What would move them: a lane-mask slice that computes the whole-wave result and SCC from the full 32-bit mask, shown for both outcomes on a mixed-mask s_cselect/s_cbranch_scc consumer. I'd keep that as its own issue.

For awareness: ChazY11's #1928 adds the same three table entries without a mask gate, so the same point applies there.

AI-assisted.

@R0Y00

R0Y00 commented Nov 24, 2026

Copy link
Copy Markdown
Owner

The updated head correctly refuses lane masks. I also checked a tracked descriptor source: the shared symbolic path silently left the old SCC for all three new ops. The added control fails on 9b620015 and passes once that input is refused, alongside the numeric, mask and scalar m0 controls.

@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Cross-check with the encode_sop2 route, as you asked. Two independent paths, and they agree.

1. Your check, on your branch

git fetch origin pull/1925/head:review-1925
python -B tests/startup_gfx2hlsl_sop2_complement_check.py
exit 0

2. My route, on the same branch

The startup_gfx2hlsl_scalar_m0_check.py encode_sop2 path builds the SOP2 word, decodes it with disasm_rdna2 and runs the
translator, so it never touches your table or your fixture:

s_nand_b32   s_nand_b32 s28, s5, s6       -> ['s28 = ~(s5 & s6);', 'scc = s28 != 0;']
s_nor_b32    s_nor_b32 s28, s5, s6        -> ['s28 = ~(s5 | s6);', 'scc = s28 != 0;']
s_xnor_b32   s_xnor_b32 s28, s5, s6       -> ['s28 = ~(s5 ^ s6);', 'scc = s28 != 0;']
s_and_b32    s_and_b32 s28, s5, s6        -> ['s28 = s5 & s6;',    'scc = s28 != 0;']

Same text your table produces, and the neighbour is unchanged. Encoding on my side is 0x80000000 | op << 23 | sdst << 16 | ssrc1 << 8 | ssrc0 with opcodes 0x18, 0x1a, 0x1c, 0x0e from the decoder table, so this is not a copy of your
expectations.

3. Mutations I ran against your branch

Because an agreeing second path is weak evidence if neither one fails when the code is wrong, I broke yours three ways and ran
your fixture each time:

mutation result
table: 's_nand_b32': ('~({0} & {1})', '$ != 0') → drop the ~ caught
code path: word_complement = base in ('s_nand_b32', 's_nor_b32', 's_xnor_b32') → False caught
table: 's_nand_b32': (…, '$ != 0') → (…, None) (SCC dropped) caught

The second one is the useful one: your implementation has both a table entry and a word_complement code path, so a fixture that
only compared emitted text against a table-derived expectation could pass while the code path was dead. Yours does not.

What this does not cover

My route only speaks the four ops above at sdst=28, ssrc0=5, ssrc1=6; it says nothing about your 11 operand forms, the
2,184 value pairs, the lane-mask forms, the refusals or the LLVM round trip. Those rest on your fixture alone.

Note

I had opened #1928 for the same issue before reading the claim line in the description; it is closed. Your branch is the one to
merge — the cross-check above is the part I could still contribute.

  • AI-assisted.

@MasterDomino

Copy link
Copy Markdown
Collaborator Author

Thanks for the descriptor-source control and the fix. I pulled 34105de4 locally: all 17 tests/startup_gfx2hlsl_*_check.py controls pass on it, including startup_gfx2hlsl_sop2_complement_check.py with the new symbolic-input refusal. I have nothing further to change on this slice. The whole-wave SCC lowering for the lane-mask forms stays a separate follow-up.

AI-assisted.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Rechecked the exact numeric-only candidate with mask and descriptor refusal controls. The independent result/SCC matrix and both mutation families pass.

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.

startup: gfx2hlsl lacks the 32-bit scalar complement logic ops (s_nor_b32, s_nand_b32, s_xnor_b32)

3 participants