Repository navigation
shader: lower s_bfm_b64 in gfx2hlsl (Part of #1723) #2003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """s_bfm_b64 in tools/startup/gfx2hlsl.py (Part of #1723), the 64-bit twin of s_bfm_b32. | ||
|
|
||
| usage: startup_gfx2hlsl_bfm_b64_check.py | ||
|
|
||
| Pure Python, no GPU. Constant operands fold to two exact 32-bit words; run-time operands emit one block | ||
| that reads both sources before writing either destination register; unsupported shapes are refused. | ||
| """ | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| HERE = Path(__file__).resolve().parent | ||
| sys.path.insert(0, str(HERE)) | ||
| import startup_gfx2hlsl_scalar_m0_check as M # noqa: E402 | ||
|
|
||
| G = M.G | ||
| SOP2_BFM_B64 = 0x25 | ||
| DST = 20 # s[20:21] | ||
| CONST_W, CONST_O = 6, 7 | ||
|
|
||
|
|
||
| def run(width_sym, offset_sym, *, dst=DST, w=CONST_W, o=CONST_O): | ||
| stage = M.ProbeStage() | ||
| stage.sym[CONST_W], stage.sym[CONST_O] = ('const', width_sym), ('const', offset_sym) | ||
| code = M.encode_sop2(SOP2_BFM_B64, dst, w, o) | ||
| _, name, asm, _ = M.D.decode_instruction(code, 0) | ||
| assert name == 's_bfm_b64', name | ||
| stage.one(name, asm) | ||
| return stage | ||
|
|
||
|
|
||
| def words(stage): | ||
| lo, hi = stage.sym[DST], stage.sym[DST + 1] | ||
| assert lo[0] == hi[0] == 'const', (lo, hi) | ||
| assert stage.lines == ['s%d = 0x%08xu;' % (DST, lo[1]), 's%d = 0x%08xu;' % (DST + 1, hi[1])], stage.lines | ||
| return lo[1], hi[1] | ||
|
|
||
|
|
||
| def controls(): | ||
| # (width, offset) -> (low word, high word), written out by hand from the 64-bit definition. | ||
| cases = ( | ||
| (0, 0, 0x00000000, 0x00000000), | ||
| (1, 0, 0x00000001, 0x00000000), | ||
| (16, 0, 0x0000ffff, 0x00000000), | ||
| (32, 0, 0xffffffff, 0x00000000), # the width the 32-bit op cannot express | ||
| (33, 0, 0xffffffff, 0x00000001), | ||
| (63, 0, 0xffffffff, 0x7fffffff), | ||
| (64, 0, 0x00000000, 0x00000000), # only bits [5:0] count: width 64 is width 0 | ||
| (1, 63, 0x00000000, 0x80000000), | ||
| (4, 30, 0xc0000000, 0x00000003), # the mask crosses the word boundary | ||
| (8, 32, 0x00000000, 0x000000ff), | ||
| (32, 16, 0xffff0000, 0x0000ffff), | ||
| (16, 100, 0x00000000, 0x000ffff0), # offset 100 -> 36: bits 36..51 | ||
| (1, 64, 0x00000001, 0x00000000), # only bits [5:0] of the offset count: offset 64 is offset 0 | ||
| ) | ||
| for width, offset, lo, hi in cases: | ||
| got = words(run(width, offset)) | ||
| assert got == (lo, hi), (width, offset, '%08x %08x' % got) | ||
|
|
||
| # A run-time source keeps the mask in run-time code: both sources are read inside one block, so a | ||
| # destination that overlaps a source (s[6:7] = bfm(s6, s7)) cannot feed a half-written value back. | ||
| stage = M.ProbeStage() | ||
| _, name, asm, _ = M.D.decode_instruction(M.encode_sop2(SOP2_BFM_B64, 8, 8, 128 + 2), 0) | ||
| stage.one(name, asm) | ||
| assert stage.sym.get(8) is None and stage.sym.get(9) is None, stage.sym | ||
| block = [line for line in stage.lines if 'shl64' in line] | ||
| assert len(block) == 1 and block[0].count('s8 = ') == 1 and block[0].count('s9 = ') == 1, stage.lines | ||
| assert block[0].index('ud') < block[0].index('s8 = '), block[0] | ||
| assert {8, 9} <= stage.used_s, stage.used_s | ||
|
|
||
| # SCC is not touched. | ||
| assert not any('scc' in line for line in stage.lines), stage.lines | ||
|
|
||
| for dst in (M.M0, 106): | ||
| try: | ||
| run(1, 1, dst=dst) | ||
| except G.Unsupported: | ||
| pass | ||
| else: | ||
| raise AssertionError('destination %d should be refused' % dst) | ||
|
|
||
|
|
||
| def main(): | ||
| controls() | ||
| print('startup s_bfm_b64 controls passed') | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| sys.exit(main()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -920,6 +920,39 @@ def op_s_bfm_b32(self, ops, mods): | |
| self.set_s(d, '(((1u << (%s & 31)) - 1) << (%s & 31))' % (self.u(ops[1]), self.u(ops[2]))) | ||
| return None | ||
|
|
||
| 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.""" | ||
| if mods or len(ops) != 3: | ||
| raise Unsupported('s_bfm_b64 with %d operands and modifiers %s' % (len(ops), mods)) | ||
| 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]) | ||
| vals = [] | ||
| for o in ops[1:3]: | ||
| r = reg_range(o) | ||
| if r and r[0] == 's': | ||
| vals.append(self.is_const(r[1])) | ||
| else: | ||
| try: | ||
| vals.append(int(o, 0)) | ||
| except ValueError: | ||
| vals.append(None) | ||
| d = dst[1] | ||
| if None not in vals: | ||
| value = (((1 << (vals[0] & 63)) - 1) << (vals[1] & 63)) & 0xffffffffffffffff | ||
| self.set_s(d, '0x%08xu' % (value & 0xffffffff), ('const', value & 0xffffffff)) | ||
| self.set_s(d + 1, '0x%08xu' % (value >> 32), ('const', value >> 32)) | ||
|
Comment on lines
+943
to
+944
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If a destination SGPR previously held a lane mask, this handler overwrites its numeric value without removing its entry from Useful? React with 👍 / 👎. |
||
| 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]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a source is Useful? React with 👍 / 👎. |
||
| self.emit('{ uint bw = (%s) & 63u; uint2 t = shl64(bw >= 32u ? 0xffffffffu : ((1u << (bw & 31u)) - 1u),' | ||
| ' bw > 32u ? ((1u << ((bw - 32u) & 31u)) - 1u) : 0u, %s); s%d = t.x; s%d = t.y; }' % (width, offset, d, d + 1)) | ||
| for r in (d, d + 1): | ||
| self.sym.pop(r, None) | ||
| self.used_s.add(r) | ||
| return None | ||
|
|
||
| def op_s_load_dwordx8(self, ops, mods): | ||
| self.op_s_load_dwordx4(ops, mods, 8) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds a supported startup-translator instruction without updating README or
docs/experiments.md; the existings_bfm_b64experiment documents the separatepixel_aottranslator.AGENTS.mdrequires updating the public capability record when support changes. Record the supported/refused operand scope andpython3 -B tests/startup_gfx2hlsl_bfm_b64_check.pyreproduction command, explicitly distinguishing the Python controls from unverified GPU execution.AGENTS.md reference: AGENTS.md:L207-L208
Useful? React with 👍 / 👎.