Skip to content

startup: flip the sign bit for a negated source on the vector select - #1710

Merged
R0Y00 merged 6 commits into
R0Y00:mainfrom
triggered02:startup/cndmask-neg
Nov 24, 2026
Merged

R0Y00 merged 6 commits into
R0Y00:mainfrom
triggered02:startup/cndmask-neg

Conversation

@triggered02

@triggered02 triggered02 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1709. Fourth group from the loading-screen refusal census (#870): 2 of 29 shaders.

Note on authorship: the branch now carries R0Y00's dfc73a1b ("Preserve unmodified negative select immediates"), which replaces my first attempt with a better design, plus his merge of main (ab586947). The description below reflects the final tree.

Change

In an integer instruction the - source modifier is that source's sign bit flipped — the bit pattern a float negation produces. The vector select passed the printed operand straight to the uint helper, so v_cndmask_b32_e64 v31, v30, -v30 did not translate:

v_cndmask_b32_e64 v31, v30, v30      -> v31 = (s0 & 1) ? v30 : v30;
v_cndmask_b32_e64 v31, v30, -v30     -> v31 = (s0 & 1) ? (v30 ^ 0x80000000u) : v30;

The helper is used only where the modifier is modelled. Another integer instruction carrying the same - operand still refuses rather than silently flipping a bit, so this does not quietly widen the accepted set.

Immediate operands (R0Y00's refinement)

The printed -1 is ambiguous: it can be the plain inline constant -1 (0xffffffff) or inline 1 with NEG set (0x80000001). Rather than guess from the text, the ambiguity is resolved in the decoder: decode() inspects the encoded source field and refuses NEG on immediate and literal data operands, while plain immediates keep translating:

v_cndmask_b32_e64 v31, -1, v30      -> v31 = (s0 & 1) ? v30 : 0xffffffffu;      (plain inline -1)
v_cndmask_b32_e64 v31, -1.0, v30    -> v31 = (s0 & 1) ? v30 : 0xbf800000u;      (plain inline -1.0)
NEG on an inline constant           -> REFUSED (decode)
NEG on a literal dword              -> REFUSED (decode)

Test

startup_gfx2hlsl_cndmask_neg builds VOP2-as-VOP3 encodings from the checked-in decoder's layout (0b110101 prefix, opcode 0x100 + n, neg bits w1[31:29]), decodes them, then translates — no game-derived bytes. Eleven checks: negation on either source and on both, the unchanged no-negation form, another integer op still refusing, plain -1 / -1.0 immediates translating, and NEG on an inline integer, an inline float and a literal all refused.

dev: startup_gfx2hlsl_cndmask_neg Passed 0.14 s; startup_translator Passed 0.18 s

Verified at the merged head ab586947.

Census A/B (local, aggregates only)

With the change, refusals grouped as operand '-v…' go 2 → 0, and those shaders progress to the next form (v_cvt_pknorm_u16_f32, v_cmp_class_f32).

What this does not establish

  • That the loading screen gets further — needs a run that visibly reaches it; counts are local aggregates.
  • |vN| (absolute) on an integer source and negations on other integer instructions stay refused.

AI-assisted (Claude Code).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T17:37:54.986419Z efd7a2f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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 efd7a2f86f93af91c0a11f749aaf9dac39788fb7 (Linux only; no Windows or RDNA2 hardware here).

Verdict: 0 P1, 1 P2, 1 P3. The change itself is correct for register and SGPR sources; the P2 is a neighbouring gap the new helper makes visible.

Semantics

  • RDNA2 v_cndmask_b32 (VOP3) accepts the NEG source modifier, and it flips bit 31 of that source. u_src emits (x ^ 0x80000000u), which is that behaviour. The select order is unchanged (mask ? src1 : src0).
  • The handler is the only caller, so v_and_b32_e64 ... -v30 still raises Unsupported. The widening claim in the PR text holds.
  • |v| and -|v| still reach u() and are refused.

P2: negated integer inline constant is read as a negative constant

The VOP3 decoder prints NEG on an inline constant as a plain -. llvm-mc prints neg(1) for that encoding and -1 for the different encoding src 193.

  • Encoding v_cndmask_b32_e64 v31, <src 129 (=1)>, v30, s0 with neg0 decodes to -1.
  • It translates to 0xffffffffu. Hardware gives 0x80000001.
  • The same happened before this PR (u('-1')), so it is not a regression. But u_src now claims to model the modifier and its re.match(r'-[\d.]', tok) guard cannot tell the two cases apart.
  • Suggestion: make the decoder print neg(c) for a constant (as _format_sdwa_float_modifiers already does), or refuse a negated integer constant, and add a control.

P3: the numeric guard is untested

Dropping not re.match(r'-[\d.]', tok) makes u_src('-1') return (0x00000001u ^ 0x80000000u), and the test still passes. A control with a plain -1 or -4.0 source would pin it.

Commands executed locally

  • python3 -B tests/startup_gfx2hlsl_cndmask_neg_check.py: 6 ok, PASS.
  • llvm-mc-19 -triple=amdgcn -mcpu=gfx1030 -show-encoding on the four select spellings (no neg, neg on src1, neg on src0, both). The bytes equal the ones the test builds (..., 0x00/0x40/0x20/0x60 in the last byte).
  • Local mutations of tools/startup/gfx2hlsl.py, each run against the test and then reverted:
    • sign flip removed: FAIL
    • source order swapped: FAIL
    • 0x8000000u (one zero short): FAIL
    • | instead of ^: FAIL
    • negation applied to both operands: FAIL
    • numeric guard removed: PASS (the P3 gap)
  • Probe: src 129 and 193 with neg bits, and sgpr, vcc and float-constant sources. This found the P2.

Limits: CMake registration and the ctest presets were not run; the Windows build and the loading-screen census claim (2 to 0) were not reproduced.

AI-assisted review.

Fixes R0Y00#1709. In an integer instruction the '-' source modifier is that source's sign bit flipped; the select passed the printed operand to the uint helper, so v_cndmask_b32_e64 v31, v30, -v30 did not translate - 2 of the 29 refused shaders in the loading-screen cache (R0Y00#870). The select now reads both sources through a helper that flips the sign bit for a '-' operand and is identical otherwise, and the helper is used only where the modifier is modelled: another integer instruction carrying it still refuses. Test builds VOP2-as-VOP3 encodings from the decoder layout, decodes, then translates; both presets pass; fail-first reverts the call and the test fails. Census A/B: 'operand -v' 2 -> 0.
Review on R0Y00#1710 (P2/P3): the disassembler prints both 'src 193' (literal -1, 0xffffffff) and 'src 129' with NEG (inline 1 with its sign bit flipped, 0x80000001) as '-1', so the printed operand does not identify the value. u_src now refuses that spelling instead of picking one reading, and the test pins it with both encodings plus a check that they decode identically; a double negation '--1' (src 193 with NEG) stays unambiguous and translates. Dropping the guard fails the test.
@triggered02

Copy link
Copy Markdown
Contributor Author

Both addressed in b019275d (rebased onto current main).

P2 — the ambiguous spelling is now refused, not guessed. You are right that the printed operand does not identify the value: src 193 is the literal -1 (0xffffffff) and src 129 with NEG is inline 1 with its sign bit flipped (0x80000001), and both print as -1. u_src now raises for that exact spelling instead of picking one reading, so the select no longer translates to 0xffffffff for a case that may be 0x80000001:

v_cndmask_b32_e64 v31, -1, v30      -> REFUSED: negated integer constant '-1' is ambiguous
v_cndmask_b32_e64 v31, --1, v30     -> v31 = (s0 & 1) ? v30 : (0xffffffffu ^ 0x80000000u);   (src 193 with NEG, unambiguous)

I did not change the decoder to print neg(1): that is a disassembly-wide output change and belongs in its own slice, not inside a select fix. Refusing keeps this PR's claim honest — it models the modifier only where the printed operand identifies the source.

P3 — the guard is pinned. Two controls, one per encoding, plus a check that both decode to the same text (which is why refusal is the right answer):

a negated inline constant is refused as ambiguous: ok
a negative literal source is refused as ambiguous too: ok
both spellings decode identically: ok
a double negation still translates: ok

Your mutation now fails: dropping the guard gives fail-first exit: 1 | a negative literal source is refused as ambiguous too.

Verified after rebase on 107a0466: test passes in dev (0.16 s) and release (0.14 s). Re-running the CMake configure per preset first, since your review correctly noted the presets were not exercised.

Thanks for probing the constant sources — that is precisely the class of thing a register-only fixture hides.

AI-assisted (Claude Code).

@R0Y00

R0Y00 commented Nov 24, 2026

Copy link
Copy Markdown
Owner

I pushed dfc73a1 to fix the Linux regression. Plain negative inline constants keep their existing value in both encodings. Encoded NEG on an immediate is still refused, even when it disassembles with the same spelling as a plain constant. The new select fixture and both failing Linux cases (startup_flat_param_check.py and startup_gfx2hlsl_bit_alu_check.py) passed locally. Waiting on the checks for this new head.

@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 ab586947d920895a98d5b6601d11dd6daa9396a2 (4 commits: the original select change, b019275d and dfc73a1b which refuse NEG on an immediate and keep plain negative immediates, and a merge of main; tools/startup/gfx2hlsl.py +21/-1, new tests/startup_gfx2hlsl_cndmask_neg_check.py +126, CMakeLists.txt +3, docs/experiments.md +10). I reviewed the whole diff again; my earlier review was of efd7a2f8.

Linux limits. Linux x86_64, llvm-mc-19, Python 3.14. No CMake configure and no ctest (the startup_gfx2hlsl_cndmask_neg registration is unexercised), no Windows, no DXC, no WARP or GPU. The emitted HLSL is compared as text and was never compiled or run.

What I ran

  • python3 -B tests/startup_gfx2hlsl_cndmask_neg_check.py: PASS. Also exit 0: startup_gfx2hlsl_sdwa_check.py, startup_gfx2hlsl_vcvt_omod_check.py, startup_translator_check.py, startup_gfx2hlsl_bit_alu_check.py, startup_gfx2hlsl_smem_offset_check.py.
  • git diff --check origin/main...HEAD: clean; no non-ASCII added; no existing test line removed. tests/docs_links_check.py and tests/bidi_controls_check.py: pass. git merge-tree against current main: no conflicts.
  • Binary check: I assembled the instructions the test builds with encode_vop2_e64() using llvm-mc-19 -show-encoding for gfx1030 and gfx1013: v30, -v30, -v30, v30, -v30, -v30, plain -1 (src 193), plain -1.0 (src 243) and neg(1) (src 129 with NEG) are byte-identical to the encoder on both. The NEG bits (29 for src0, 30 for src1) and the 9-bit source fields in decode() therefore match the assembler.
  • Probes with the same encoder: |v30|, -|v30| and -s0 on the mask are refused (operand '|v30|', operand '-s0'), so abs and a negated mask do not slip through.
  • Nine mutations of the production code, each run against the new test and restored (git status clean):
    • no sign flip, and flipping bit 30 instead of 31: FAIL.
    • negation applied to the wrong source, and the two sources swapped: FAIL.
    • NEG on an immediate not refused: FAIL (NEG on literal remains refused).
    • NEG bit read from position 28+i: FAIL.
    • literal source (255) no longer refused: FAIL.
    • plain negative constants also flipped: FAIL (plain inline -1.0 still translates).
    • lower edge of the float inline range moved from 240 to 241: PASSED, not caught (see nit 1).

Semantics

  • A negated VOP3 source on v_cndmask_b32 flips bit 31 exactly: (v ^ 0x80000000u) keeps NaN payloads and turns -0.0 into +0.0, which is what a sign-bit flip does, without any canonicalisation. It applies to the source it is printed on, and the select order is unchanged ((mask & 1) ? src1 : src0).
  • The ambiguity of the printed -1 (plain inline -1, or inline 1 with NEG) is resolved from the encoded fields: NEG on 128..208, 240..248 and 255 is refused, plain negative immediates keep their bit pattern. I found no case where NEG is silently dropped.

Findings: 0 P1, 0 P2, 3 nits

  1. Nit, coverage. The refusal range edges (128, 208, 240, 248) are not each exercised: the mutation above moving 240 to 241 survives, because the tests only use 129, 242 and 255 with NEG.
  2. Nit. op_v_cndmask_b32 ignores mods, so clamp and mul:2 on v_cndmask_b32_e64 translate to the same text with the modifier dropped (I checked with the encoder). This is not introduced by the PR (the handler ignored them before), and I did not verify whether the hardware applies them to this instruction, so I only flag it.
  3. Nit. startup_gfx2hlsl_cndmask_neg_check.py uses encode_vop2_e64(AND_B32, ... neg1=1) for "another integer op stays refused"; llvm-mc-19 rejects v_and_b32_e64 ..., -v30, so that bytes pattern is not one an assembler produces. It still checks the intended property (the decoder's text is refused).
  • Not verified: the "2 of 29" census (local cache), DXC or FXC compilation, any draw.

AI-assisted; commands listed were executed locally.

…mmediate range edges

Review nits on R0Y00#1710. (1) op_v_cndmask_b32 now refuses clamp/mul:2 instead of dropping them from the emitted text: the select takes no output modifier, so a modifier that reaches the handler was silently changing nothing. (2) The test exercises every edge of the refused immediate ranges (128, 208, 240, 248) and a plain 0.5 that must still translate, so moving a range boundary by one no longer survives mutation. (3) The synthetic v_and_b32_e64 negation encoding is annotated as not assembler-producible, with the property it actually checks. Fail-first: removing the modifier refusal fails 'a mul:2 on the select is refused'; moving the 240 edge to 241 fails 'NEG on the immediate at 240 is refused'. Python-level ctest subset green; the dev tree has no built binaries here, so the executable-backed tests were not run.
@triggered02

Copy link
Copy Markdown
Contributor Author

All three nits addressed in 9bed7ffc (on top of your ab586947).

1 — range edges. The test now tries NEG on each boundary the decoder refuses (128, 208, 240, 248) and a plain 0.5 that must keep translating, so your surviving mutation no longer passes:

NEG on the immediate at 128/208/240/248: refused
plain 0.5 still translates -> v31 = (s0 & 1) ? v30 : 0x3f000000u;
fail-first: 240 -> 241  =>  "NEG on the immediate at 240 is refused" FAIL

2 — modifiers silently dropped. You are right that this predates the PR, and the same reasoning as the PR itself applies: a modifier the text drops changes the value. op_v_cndmask_b32 now refuses any modifier instead of ignoring it, and both cases are pinned:

a clamp on the select is refused
a mul:2 on the select is refused
fail-first: removing the refusal  =>  "a mul:2 on the select is refused" FAIL

I did not try to establish what the hardware does with those bits on this instruction — same limit you noted, so refusal is the safe side.

3 — non-assembler encoding. Kept, with a comment stating that llvm-mc rejects -v30 on v_and_b32_e64 and that the property under test is only that a handler which does not model the modifier refuses the decoder's text.

Verification: the Python-level tests that reach gfx2hlsl pass in dev (startup_image_sample_l, startup_image_sample_c, the image control tests, startup_translator) and startup_gfx2hlsl_cndmask_neg passes in release. The dev tree here has no built executables (5 files in out/build/dev), so the whole startup_* family returned 142 failures for missing binaries — not a signal about this change. Flagging that plainly rather than quoting a green family I did not get.

Thanks for the byte-level assembler cross-check against both gfx1030 and gfx1013; that is the part of the encoder I could not verify locally.

AI-assisted (Claude Code).

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

Re-review of head f9e815d (comment only, not an approval). My earlier review was at ab58694.

Verdict: 0 P1 / 0 P2, three nits. Update: while I was reviewing, the head moved from 9bed7ff to f9e815d, which is a merge of main. The PR's own changes are identical (the added and removed lines of gfx2hlsl.py, the test, docs/experiments.md and CMakeLists.txt compare equal to the 9bed7ff diff against main). At the new head the cndmask test (PASS), the sdwa and vcvt_omod checks, docs_links_check (17 files, 366 links), bidi_controls_check (1303 files) and git diff --check origin/main...HEAD pass again. The mutation results below were run at 9bed7ff and not repeated.

Earlier points, all fixed: the NEG range edges are now tried on 128, 208, 240 and 248 plus a plain 0.5 (240 -> 241 and 208 -> 207 both fail); clamp and mul:2 are no longer dropped silently (op_v_cndmask_b32 raises on any mods, two tests pin it); the v_and_b32_e64 fixture is kept with a comment. The only commit since is 9bed7ff (+3 lines in gfx2hlsl.py and the test).

Semantics

  • (x ^ 0x80000000u) is an exact sign-bit flip: NaN payloads are kept and -0.0 becomes +0.0, with no canonicalisation. It applies to the source it is printed on and the select order is unchanged.
  • The mask is never negated; v_cndmask_b32_e64 ..., -s0 does not assemble in llvm-mc. |v30| and -|v30| (assembled for gfx1030) are refused with operand '|v30|'. The e32 form has no modifiers; the SDWA form with -v30 is refused as a whole.
  • A constant source such as -0x3f800000 assembles to inline -4.0 and translates to 0xc0800000u; NEG on a constant source is refused by encoded field in decode() (128..208, 240..248, 255).
  • No assertion is weakened (the test diff is additive) and no test searches source text. Doc claims match the tests; no title content.

Run

  • python3 -B tests/startup_gfx2hlsl_cndmask_neg_check.py: PASS. sdwa, vcvt_omod, translator, bit_alu, smem_offset and flat_param checks: pass. docs_links_check (17 files, 365 links), bidi_controls_check (1296 files), git diff --check origin/main...HEAD: clean. llvm-mc-19 -mcpu=gfx1030 was used on the probe forms and its bytes fed through G.decode and Stage.one.
  • 13 production mutations in tools/startup/gfx2hlsl.py (each restored with git checkout --). Caught: bit 30 instead of 31; swapped sources; dropped mods refusal; neg check only on src0; literal 255 no longer refused; NEG bit read from position 28+i; range 208 -> 207; range 240 -> 241.
  • NOT caught: the mask routed through u_src (no test pins "a negated mask is refused"; no assembler can emit it); stripping | in u_src (self.u(tok[1:].strip('|'))), which would turn -|v30| into a plain negation, although production refuses both today; and 248 -> 247, which is equivalent because source 248 is not a decodable inline constant.

Nits

  1. Add a decoder-produced -s0 mask control, or document why it cannot be encoded.
  2. Add |v30| and -|v30| refusal controls; the PR text says abs stays refused but nothing pins it.
  3. A decoded v_cndmask_b32_e64 ... vcc_lo emits (vcc & 1); pre-existing and out of scope.

Overlap: no cndmask or u_src hits in #1755, #1747, #1703, #1706, #1712, #1708, #1745 and #1741 (only those eight were grepped). Main has moved; I did not check whether a rebase conflicts.

Not run: CMake/ctest (so the startup_gfx2hlsl_cndmask_neg add_test is unexercised), DXC or WARP (the emitted HLSL is compared as text, never compiled), the census claim (2 of 29 shaders, 2 -> 0), mingw (no C++ in the diff).

Linux limits: no Windows, WARP, DXC or GPU here; LLVM 19 only (CI pins LLVM 15). Anything that needs them was not run.

AI-assisted; commands listed were executed locally.

@R0Y00
R0Y00 merged commit 787f284 into R0Y00:main Nov 24, 2026
5 checks passed
@triggered02

Copy link
Copy Markdown
Contributor Author

Both coverage nits are closed in #+ (($pr -split '/')[-1].Trim()) +.

  • Negated mask: it can be decoded — setting w1[31] makes the decoder print -s0 (LLVM just refuses to assemble it, which is why no fixture existed). The test now asserts REFUSED: operand '-s0' with a comment saying the word is decoder-produced only. So the answer to your nit 1 is "encodable from the word, only refused before by accident" rather than "cannot be encoded".
  • Absolute values: the encoder gained the abs bits; |v30| on the first source, the second source and both are pinned as refused (operand '|v30|').

Fail-first: routing the mask through u_src fails a negated mask is refused; stripping the abs bars in u_src fails absolute values on both sources are refused. 22 checks; dev startup_gfx2hlsl* 10/10 and the release test pass; docs/bidi checks pass.

Your nit 3 (vcc_lo emitting (vcc & 1)) stays out of scope, as you flagged.

AI-assisted (Claude Code).

tedin7 pushed a commit that referenced this pull request Nov 24, 2026
Review nits on the merged #1710: the PR text says a source absolute value stays refused and a negated mask cannot be assembled, but nothing pinned either. The encoder gains the abs bits and the mask negate bit, and the test now asserts that |v30| on either source (and both), and a decoder-produced -s0 mask, are refused. All four spellings are words the decoder produces but llvm-mc rejects, so the property under test is the refusal of the printed operand. Fail-first: routing the mask through u_src fails 'a negated mask is refused'; stripping the abs bars fails 'absolute values on both sources are refused'. 22 checks; startup_gfx2hlsl* dev family and the release test 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: the vector select refuses a negated source operand (2 of 29 loading-screen shader refusals)

3 participants