Repository navigation
startup: flip the sign bit for a negated source on the vector select - #1710
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
tedin7
left a comment
There was a problem hiding this comment.
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_srcemits(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 ... -v30still raises Unsupported. The widening claim in the PR text holds. |v|and-|v|still reachu()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, s0with neg0 decodes to-1. - It translates to
0xffffffffu. Hardware gives0x80000001. - The same happened before this PR (
u('-1')), so it is not a regression. Butu_srcnow claims to model the modifier and itsre.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_modifiersalready 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-encodingon the four select spellings (no neg, neg on src1, neg on src0, both). The bytes equal the ones the test builds (..., 0x00/0x40/0x20/0x60in 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.
efd7a2f to
b019275
Compare
|
Both addressed in P2 — the ambiguous spelling is now refused, not guessed. You are right that the printed operand does not identify the value: I did not change the decoder to print 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): Your mutation now fails: dropping the guard gives Verified after rebase on Thanks for probing the constant sources — that is precisely the class of thing a register-only fixture hides. AI-assisted (Claude Code). |
|
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
left a comment
There was a problem hiding this comment.
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.pyandtests/bidi_controls_check.py: pass.git merge-treeagainst current main: no conflicts.- Binary check: I assembled the instructions the test builds with
encode_vop2_e64()usingllvm-mc-19 -show-encodingforgfx1030andgfx1013:v30, -v30,-v30, v30,-v30, -v30, plain-1(src 193), plain-1.0(src 243) andneg(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 indecode()therefore match the assembler. - Probes with the same encoder:
|v30|,-|v30|and-s0on 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 statusclean):- 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_b32flips 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
- 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.
- Nit.
op_v_cndmask_b32ignoresmods, soclampandmul:2onv_cndmask_b32_e64translate 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. - Nit.
startup_gfx2hlsl_cndmask_neg_check.pyusesencode_vop2_e64(AND_B32, ... neg1=1)for "another integer op stays refused";llvm-mc-19rejectsv_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.
|
All three nits addressed in 1 — range edges. The test now tries NEG on each boundary the decoder refuses (128, 208, 240, 248) and a plain 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. 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 Verification: the Python-level tests that reach Thanks for the byte-level assembler cross-check against both AI-assisted (Claude Code). |
# Conflicts: # CMakeLists.txt
tedin7
left a comment
There was a problem hiding this comment.
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 ..., -s0does not assemble in llvm-mc.|v30|and-|v30|(assembled for gfx1030) are refused withoperand '|v30|'. The e32 form has no modifiers; the SDWA form with-v30is refused as a whole. - A constant source such as
-0x3f800000assembles to inline -4.0 and translates to0xc0800000u; NEG on a constant source is refused by encoded field indecode()(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=gfx1030was used on the probe forms and its bytes fed throughG.decodeandStage.one.- 13 production mutations in tools/startup/gfx2hlsl.py (each restored with
git checkout --). Caught: bit 30 instead of 31; swapped sources; droppedmodsrefusal; 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|inu_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
- Add a decoder-produced
-s0mask control, or document why it cannot be encoded. - Add
|v30|and-|v30|refusal controls; the PR text says abs stays refused but nothing pins it. - A decoded
v_cndmask_b32_e64 ... vcc_loemits(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.
|
Both coverage nits are closed in #
Fail-first: routing the mask through Your nit 3 ( AI-assisted (Claude Code). |
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.
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, sov_cndmask_b32_e64 v31, v30, -v30did not translate: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
-1is ambiguous: it can be the plain inline constant-1(0xffffffff) or inline1with 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:Test
startup_gfx2hlsl_cndmask_negbuilds VOP2-as-VOP3 encodings from the checked-in decoder's layout (0b110101prefix, opcode0x100 + n, neg bitsw1[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.0immediates translating, and NEG on an inline integer, an inline float and a literal all refused.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
|vN|(absolute) on an integer source and negations on other integer instructions stay refused.AI-assisted (Claude Code).