Skip to content

startup: lower the src_scc scalar operand as the SCC bit - #1896

Closed
ChazY11 wants to merge 1 commit into
R0Y00:mainfrom
ChazY11:startup/src-scc-operand
Closed

ChazY11 wants to merge 1 commit into
R0Y00:mainfrom
ChazY11:startup/src-scc-operand

Conversation

@ChazY11

@ChazY11 ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Implements #1820.

Change

Stage.u() learns the scalar source code 253:

if tok == 'src_scc':
    # Source code 253 reads the SCC bit as the integer value 0 or 1: s_mul_i32 s28, 1.0,
    # src_scc multiplies the raw bits of the inline float. The scalar code keeps SCC in scc.
    return '(scc ? 1u : 0u)'

The generated scalar code already carries SCC in scc (initialised and updated by the instructions that set it), so
nothing else moves.

Tested

ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_' -v
100% tests passed, 0 tests failed out of 21      (startup_gfx2hlsl_src_scc Passed 0.34 s)

The new tests/startup_gfx2hlsl_src_scc_check.py builds SOP2 words from the layout tools/disasm_rdna2.py decodes
([31:30] == 0b10, opcode w0[29:23], sdst w0[22:16], src1 w0[15:8], src0 w0[7:0]; inline 1.0 is source code
242, src_scc is 253), decodes them, and translates:

form emitted
s_mul_i32 s28, 1.0, src_scc (the census form) s28 = 0x3f800000u * (scc ? 1u : 0u);
s_mul_i32 s29, src_scc, 1.0 s29 = (scc ? 1u : 0u) * 0x3f800000u;
s_and_b32 s28, src_scc, s5 s28 = (scc ? 1u : 0u) & s5; then scc = s28 != 0;
the two above in sequence both statements, in order

The census form keeps the raw bits of the inline float (0x3f800000u), which is what a scalar integer multiply does —
an implementation that emitted 1u would produce a different instruction.

Fail-first

Both controls, each reverted:

src_scc branch -> raise Unsupported   startup_gfx2hlsl_src_scc FAILED (exit 1)
the operand      -> '1u'              startup_gfx2hlsl_src_scc FAILED (exit 1)
reverted                              passed

The second one is the reason the first case exists: it catches an implementation that accepts the operand but drops the
multiply's operand bits.

Neighbours

startup_gfx2hlsl_operand_count, startup_gfx2hlsl_mad and startup_translator all pass unchanged. The last case in the new
check also asserts that ttmp0, vcc_hi and exec_hi are still refused by u(), so this branch did not widen the scalar
operand set.

Scope

One production branch, one new device-free check, one CMakeLists.txt registration with TIMEOUT 60. No draw mode, no
hardware dependency.

Notes

  • AI-assisted.

Source code 253 reads the SCC bit as the integer 0 or 1, so s_mul_i32 s28,
1.0, src_scc multiplies the raw bits of the inline float by it. The translator
refused the operand as unknown; the generated scalar code keeps SCC in scc, so
lower it to (scc ? 1u : 0u).

Refs R0Y00#1820.

@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 exact head 6eb338bf770cebe3cdd765c3609ccbffd94eed5a (verified equal to gh pr view 1896 --json headRefOid at review time). Diff: tools/startup/gfx2hlsl.py (+4), tests/startup_gfx2hlsl_src_scc_check.py (+84, new), CMakeLists.txt (+4). No Windows code is touched, so no Windows run was needed.

Verdict: no P1, no P2. Approach is correct; one P3 nit. (Comment only, not an approval.)

Clean-room and title

  • Title is neutral and describes the change. Commit message cites #1820, matching the PR body.
  • The only facts used are the public source-code table in tools/disasm_rdna2.py (253 = src_scc) and the generated code's existing scc variable. No third-party code or text was found.
  • Author/committer is the contributor's own noreply identity.

Semantics

  • Stage.u() now returns (scc ? 1u : 0u) for src_scc (tools/startup/gfx2hlsl.py:214-217). scc is declared as bool scc = false in the prologue (gfx2hlsl.py:1168), so the expression is well-typed and the initial value is defined.
  • The ternary gives a 32-bit 0/1 integer with no sign or overflow issue, and the raw bits of the inline float are kept (0x3f800000u).
  • The SCC definition order is preserved because every SCC writer already emits scc = ...; inline in program order.
  • The branch is checked before reg_range(tok), so it cannot shadow a real register name, and it only matches the exact token.

I also probed neighbouring scalar forms that now reach the new branch, using the PR's own stage() helper (output of the probe):

s_mov_b32 s4, src_scc        -> s4 = (scc ? 1u : 0u);
s_mov_b64 s[4:5], src_scc    -> s4 = ((scc ? 1u : 0u)) & 1;  s5 = 0u;
s_and_b64 s[4:5], src_scc, s[6:7] -> s4 = ((scc ? 1u : 0u) & s6) & 1;  scc = s4 != 0;  s5 = 0u;
s_cselect_b32 s4, src_scc, s5 -> s4 = scc ? (scc ? 1u : 0u) : s5;

All are consistent with the existing handling of 32-bit scalar operands and the 64-bit mask path (a 64-bit read of SCC is zero-extended, high word 0). No crash and no wrong-width result.

Commands run locally (venv python, in the clean worktree at the SHA above)

git diff --check origin/main...HEAD                      exit 0
python -B tests/startup_gfx2hlsl_src_scc_check.py        "src_scc operand: 5 cases passed"  rc=0
python -B tests/startup_gfx2hlsl_operand_count_check.py  "operand count checks: 0 failed"    rc=0
python -B tests/startup_gfx2hlsl_mad_check.py            passed  rc=0
python -B tests/startup_gfx2hlsl_op_refusals_check.py    "3 cases passed"  rc=0
python -B tests/startup_gfx2hlsl_bit_alu_check.py        passed  rc=0
python -B tests/startup_translator_check.py              "startup translator checks: 0 failed"
python -B tests/docs_links_check.py                      "Self-test passed. Checked 18 Markdown files and 376 links."  rc=0
python -B tests/bidi_controls_check.py                   "no bidirectional controls" (1331 files)  rc=0

Mutations (each applied to gfx2hlsl.py, test run, then git checkout -- .)

M1 return -> raise Unsupported('operand src_scc')   FAIL rc=1  (Unsupported: operand src_scc)
M2 return '1u'                                       FAIL rc=1  (['s28 = 0x3f800000u * 1u;'])
M3 inverted (scc ? 0u : 1u)                          FAIL rc=1
M4 bare '(scc)'                                      FAIL rc=1
M5 widen branch to also accept 'ttmp0'               FAIL rc=1  (ttmp0 was accepted by u())

The test fails for every mutation, including the "silently widened operand set" case. The worktree was clean afterwards.

Findings

  • P1: none.
  • P2: none.
  • P3 (nit), CMakeLists.txt near line 1872: the new startup_gfx2hlsl_src_scc block was inserted between the comment "Two integer operations whose refusal was never reached: v_cvt_i32_f32 ... v_readfirstlane_b32 ..." and the startup_gfx2hlsl_op_refusals test it describes. That comment is now separated from its test. Moving the new block above the comment would fix it.
  • P3 (observation, not blocking): the test checks exact emitted strings, so a harmless rewrite such as bool(scc) ? 1u : 0u would need the test updated. That is acceptable for a translator whose output is the contract.

AI-assisted; commands listed were executed locally.

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

This overlaps #1821 and has the same stale SCC blocker. For example, a compare first sets SCC to 0, s_add_u32 then adds 0xffffffff and 1, and the next src_scc read must return the carry (1). Stage.scalar currently emits the sum without updating SCC, so this change reads the old 0. The existing checks exercise only modeled writers and the default HLSL bool.

Please coordinate with #1821 and keep one implementation. Accept a src_scc read only when the reaching SCC value is modeled, including across branches, or explicitly refuse it. Retain the raw-bit operand case and add the distinguishing carry or stale-writer refusal control, together with a valid defined comparison.

@thepatrickworld

thepatrickworld commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Adding the scope to the blocker above, because it changes the size of the fix: it is not only s_add_u32.

Eight SCA entries set SCC on RDNA 2 and carry None:

's_add_i32': ('{0} + {1}', None), 's_add_u32': ('{0} + {1}', None), 's_sub_i32': ('{0} - {1}', None), 's_sub_u32': ('{0} - {1}', None),
's_min_u32': ('min({0}, {1})', None), 's_max_u32': ('max({0}, {1})', None),
's_min_i32': ('uint(min(int({0}), int({1})))', None), 's_max_i32': ('uint(max(int({0}), int({1})))', None),

S_MUL_I32, S_BFM_B32 and S_BREV_B32 also carry None and are correct there — they leave SCC unchanged (RDNA 2 ISA, Table 11, column "Sets SCC?"). The repository already states the rule for its other scalar path: docs/experiments.md, describing the scalar ALU table in tools/pixel_aot.py, records SCC as "carry or borrow for the unsigned add … the comparison for min, max and s_cmp_* …", with s_mul_i32 leaving it unchanged.

So a guard scoped to s_add_u32 alone would still accept the read after the other seven. At head 6eb338bf merged with main at 3ad0069d:

s_and_b32 s4, s5, s6         ->  s4 = s5 & s6;  scc = s4 != 0;
s_add_u32 s0, s1, s2         ->  s0 = s1 + s2;                       <- no statement for the carry out
s_mul_i32 s28, 1.0, src_scc  ->  s28 = 0x3f800000u * (scc ? 1u : 0u);

Control: substituting s_and_b32 for the middle instruction, a modelled writer, adds scc = s0 != 0; — which is what makes the missing line evidence rather than a probe artefact. Case 3 of the new test uses that modelled writer, so it cannot reach the untracked path.

Code reading and translation output only — no hardware run is claimed here.

AI-assisted.

@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Closing this as a duplicate: #1821 by @triggered02 landed the same change first.

For the record, so nothing of the verification is lost — #1821 is the same three lines in Stage.u()
(return '(scc ? 1u : 0u)' for source code 253), and it also updates docs/experiments.md, which mine did not. It was opened
at 2026-11-24T19:14:13Z, 73 minutes before this one, and I did not see it when I claimed #1820 — my mistake, not theirs.

Two things from my run that are worth keeping if you want them in #1821 (I am not pushing them):

  • The census form must keep the raw bits of the inline float: s_mul_i32 s28, 1.0, src_scc →
    s28 = 0x3f800000u * (scc ? 1u : 0u);. Rewriting the operand as 1u produces a different instruction, and it is the one
    mutation that a test on this branch should catch. My check asserted that form explicitly.
  • Built here with startup_gfx2hlsl_src_scc passing, its neighbours (startup_gfx2hlsl_operand_count,
    startup_gfx2hlsl_mad, startup_translator) unchanged, and two fail-first controls: removing the branch fails, and
    returning 1u instead of (scc ? 1u : 0u) fails.

Both branches use the same test filename, so whichever merges will decide the other's history; #1821 is first and has the doc
row, so take that one.

  • AI-assisted.

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.

4 participants