Skip to content

shader: lower s_bfm_b64 in gfx2hlsl (Part of #1723) - #2003

Closed
tedin7 wants to merge 2 commits into
R0Y00:mainfrom
tedin7:x2w-pipe-s-bfm-b64
Closed

tedin7 wants to merge 2 commits into
R0Y00:mainfrom
tedin7:x2w-pipe-s-bfm-b64

Conversation

@tedin7

@tedin7 tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Part of #1723

What this unblocks: startup shaders that build 64-bit bitfield masks with s_bfm_b64 now translate instead of being refused.

Adds op_s_bfm_b64 to tools/startup/gfx2hlsl.py, the 64-bit twin of op_s_bfm_b32: D = ((1 << S0[5:0]) - 1) << S1[5:0].

  • Constant operands fold to two exact 32-bit words (width 32 and 33..63 included).
  • Run-time operands build the mask words without a 32-bit shift count of 32 or more, then reuse shl64. Both sources are read before either destination register is written, so overlapping registers are safe.
  • SCC is untouched. m0, vcc, exec, odd destinations above s105 and modifiers are refused.
  • New tests/startup_gfx2hlsl_bfm_b64_check.py (13 hand-written constant cases, run-time block shape, refusals), registered in CMake as startup_gfx2hlsl_bfm_b64_controls.

Verification:

  • python3 -B tests/startup_gfx2hlsl_bfm_b64_check.py -> startup s_bfm_b64 controls passed (with the fix).
  • Same test with the fix reverted -> Unsupported: s_bfm_b64 (fails before, passes after).
  • python3 -B tests/startup_gfx2hlsl_scalar_m0_check.py, ..._shift64_check.py, ..._bit_alu_check.py -> all pass.
  • Not run: Win2025 GPU draw (this change is pure-Python controls only; no GPU readback of the dynamic path yet).

AI-assisted; commands listed were executed locally.

Part of R0Y00#1723. Constant operands fold to two exact words; run-time operands
build the mask without a 32-bit shift count of 32 or more and reuse shl64.
Refuses m0, vcc, exec and modifiers.
@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-24T23:05:07.344042Z ce590a3 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ce590a3a50

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/startup/gfx2hlsl.py
Comment on lines +923 to +924
def op_s_bfm_b64(self, ops, mods):
"""64-bit bitfield mask: D = ((1 << S0[5:0]) - 1) << S1[5:0]; both sources are 32-bit, SCC is untouched."""

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Document the new startup translation capability

This adds a supported startup-translator instruction without updating README or docs/experiments.md; the existing s_bfm_b64 experiment documents the separate pixel_aot translator. AGENTS.md requires updating the public capability record when support changes. Record the supported/refused operand scope and python3 -B tests/startup_gfx2hlsl_bfm_b64_check.py reproduction command, explicitly distinguishing the Python controls from unverified GPU execution.

AGENTS.md reference: AGENTS.md:L207-L208

Useful? React with 👍 / 👎.

Comment thread tools/startup/gfx2hlsl.py
Comment on lines +943 to +944
self.set_s(d, '0x%08xu' % (value & 0xffffffff), ('const', value & 0xffffffff))
self.set_s(d + 1, '0x%08xu' % (value >> 32), ('const', value >> 32))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Clear destination lane-mask tags when writing the bitfield

If a destination SGPR previously held a lane mask, this handler overwrites its numeric value without removing its entry from self.masks. For example, the decoded sequence s_mov_b64 s[20:21], exec; s_bfm_b64 s[20:21], 4, 4; s_or_b32 s22, s20, 0 emits the final assignment as (0x000000f0u | 0x00000000u) & 1, producing zero instead of 0xf0 and consequently setting SCC incorrectly. Clear both destination mask tags in the constant and runtime paths so subsequent scalar operations do not truncate the numeric result to one bit.

Useful? React with 👍 / 👎.

Comment thread tools/startup/gfx2hlsl.py
return None
# The mask words are built so no 32-bit shift sees a count of 32 or more, then shl64 moves them;
# both sources are read before either destination register is written (D may overlap S0/S1).
width, offset = self.u(ops[1]), self.u(ops[2])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reject lane-mask sources used as integer counts

When a source is exec_lo, vcc_lo, or a mask-tagged SGPR, self.u() supplies the per-lane 0/1 projection rather than the wave's 32-bit mask. For example, on an all-active wave, the accepted instruction s_bfm_b64 s[20:21], exec_lo, 0 emits bw = exec & 63u and produces 1, whereas the actual exec_lo is 0xffffffff, giving width 63 and result 0x7fffffffffffffff. Refuse these sources, as the scalar complement handlers do for lane masks, unless their full-wave numeric values can be represented.

Useful? React with 👍 / 👎.

@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Two open PRs lower s_bfm_b64 — #2003 and #2007 — and they conflict on three files

Not a problem with your patch; a coordination note so the effort is not spent twice. Checked against main 7f149ac9:

#2003  contains op_s_bfm_b64: True   merge-base 36a9a06f, 22 commits behind
#2007  contains op_s_bfm_b64: True   merge-base 1ebfe511, 27 commits behind

git checkout -B bfm-both origin/main
git merge pr-2003 && git merge pr-2007
    UU AGENTS.md
    AA tests/startup_gfx2hlsl_bfm_b64_check.py
    UU tools/startup/gfx2hlsl.py

Both add a fixture with the same filename, so whichever lands second has to be rewritten rather than rebased.

Where the two implementations differ

The semantics agree (D.u64 = ((1ULL << S0[5:0]) - 1) << S1[5:0], SCC untouched, both words written). The differences are in what is refused:

#2003 #2007
modifiers and operand count one combined message two messages
destination alignment dst[2] != dst[1] + 1 and dst[2] > 105 adds dst[1] % 2
source register form accepts anything reg_range maps to s refuses exec/vcc and any range r[1] != r[2]

Two of those matter:

What I would suggest

Keep #2007's dst[1] % 2 and its explicit source refusal, and take #2003's split modifier/operand messages if you prefer them — then close the other, since neither check can be reused as is.

I have already reviewed #2007 at the behaviour level (five mutations, all caught, plus an exhaustive check of the emitted expression against the ISA formula over all 4,096 (width, offset) pairs). I have not reviewed #2003.

The same duplication exists for v_swap_b32 (#1995 and #2012), so this looks like a pattern worth a one-line note in CONTRIBUTING.md — "check open PRs for the mnemonic before starting" — rather than something either author did wrong.

Notes

  • AI-assisted coordination note.

@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Ran it, broke it four ways: two caught, two survivors proven equivalent — and one real gap in the refusals

Windows 11 build 26300.9974, device-free:

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_bfm_b64_check.py
startup s_bfm_b64 controls passed      exit 0
mutation result
constant fold loses its & 63 masks caught
the run-time path stops clearing sym for the destination pair caught
high-word condition bw > 32u → bw >= 32u survived
high-word shift (bw - 32u) & 31u → bw & 31u survived

The two survivors are equivalent mutants, not gaps — checked, not assumed

I evaluated the emitted expression, both mutants, and the ISA formula over all 4,096 (width, offset) pairs:

as written (bw>32, bw-32)   mismatches over 4096 pairs: 0
mutant 1   (bw>=32, bw-32)  mismatches over 4096 pairs: 0
mutant 2   (bw>32,  bw&31)  mismatches over 4096 pairs: 0

widths where (bw-32)&31 != bw&31 : []
widths where the high word is active : 33..63 (31 values)

The high word is only active for bw in 33..63, and on exactly that range (bw-32)&31 == bw&31, while at bw == 32 the shift is 0 so >= and >
agree. So no fixture can distinguish them, and asking for one would be asking for a test that cannot fail. Your expression is correct as written.

The real gap: an odd-aligned 64-bit destination is accepted

dst = reg_range(ops[0])
if not dst or dst[0] != 's' or dst[2] != dst[1] + 1 or dst[2] > 105:
    raise Unsupported('s_bfm_b64 destination %s' % ops[0])

There is no alignment term, so every one of these passes the condition:

s[2:3]      reg_range=('s', 2, 3)      accepted
s[3:4]      reg_range=('s', 3, 4)      accepted      <- odd
s[104:105]  reg_range=('s', 104, 105)  accepted

A 64-bit scalar operand must be even-aligned in the register file, so s_bfm_b64 s[3:4], … should be refused. One clause closes it:

if not dst or dst[0] != 's' or dst[2] != dst[1] + 1 or dst[1] % 2 or dst[2] > 105:

The other PR for this mnemonic (#2007) has that term, which is why I raised the duplicate on both — see my earlier note. If you keep #2003 instead, this
is the one line I would take from the other one.

Where this version is stronger than #2007

bw - 32u instead of bw & 31u in the high word reads as the intent (they are equal, as shown above), and the folding path writes two exact 32-bit
words with ('const', …) symbols, which the other version also does. Nothing to change.

What is not covered

No shader compiled or run; SCC untouched is asserted by your fixture. The ISA quotation in the docstring is taken at face value.

Notes

  • AI-assisted review.

@JB2WIN

JB2WIN commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

@tedin7, #2007 lowers s_bfm_b64 too. I claimed it in #1723 at 22:20 UTC and hadn't seen your PR when I did. R0Y00 asked on #2007 to keep a single implementation there, and I'm now adding his requested mask-alias refusal and the uniformity check on top of #1931. If your branch has controls #2007 lacks, tell me and I'll bring them over with attribution. Sorry for the overlap.

AI-assisted (Claude Code).

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Closing in favor of #2007 (@JB2WIN had already claimed s_bfm_b64 in #1723) so there is a single clean implementation as requested by @R0Y00. Thanks @ChazY11 for the thorough review!

@tedin7 tedin7 closed this Nov 25, 2026
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