Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/experiments/native-mesh-scalar-bit-ops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Mesh frontend scalar bit operations and the flagged reciprocal

The direct C++ mesh frontend behind `gfx1013-raw-ms` now lowers four forms that
the forced slot 86 mesh path refused. The census of the 2 873 failing title
requests ordered the refusals `s_bfe_i32` (458), `s_not_b32` (433),
`v_rcp_iflag_f32` (145) and `s_brev_b32` (107).

## Lowering

`s_not_b32` (SOP1 `0x07`) and `s_bfe_i32` (SOP2 `0x28`) share one scalar branch
in [gfx1013_direct_mesh.inl](../../src/gfx1013_direct_mesh.inl) that calls the
same `arithmetic()` the compute frontend uses. `S_NOT_B32` is `D = ~S0`, spelled
`xor i32 S0, -1`. `S_BFE_I32` takes the offset from `S1[4:0]` and the width from
`S1[22:16]`, arithmetic-shifts the source, returns zero for a zero width and
keeps the shifted value when the width is 32 or more; the narrow path is
`ashr(shl(shifted, pad), pad)` with `pad = (32 - width) & 31`. Both write
`SCC = (D != 0)`, as the RDNA 2 ISA and the compute frontend do.

`s_brev_b32` (SOP1 `0x0B`) reverses the 32 bits through the shared `unary.i32`
intrinsic and leaves SCC unchanged. The compute switch returns before its shared
SCC write, and the SOP1 table in `tools/startup/gfx2hlsl.py` carries no SCC
expression for this form, so the mesh branch writes only the destination. A
compare seeded before the reversal still drives a select after it.

`v_rcp_iflag_f32` (VOP1 `0x2B`) joins the mesh vector list that already handles
`v_rcp_f32`, so it takes the same `1.0 / x` rule from `alu_rules`.

All three scalar forms refuse an EXEC or VCC destination, like the other mesh
scalar branches, and an unbound source keeps the shared numeric-operand refusal.

## Observed IR

The source-built programs in
[native_direct_mesh_check.py](../../tests/native_direct_mesh_check.py) export
the scalar result and its SCC through the POS0 X and Y channels. With `v0` as
the source, `v_rcp_iflag_f32 v1, v0` emits
`fdiv float 1.000000e+00, %bitcast` and the reciprocal's bits reach
`storeVertexOutput` column 0.

For `s20 = 0`, `s_not_b32 s21, s20` emits `xor i32 0, -1`, then
`icmp ne i32 %xor, 0` and `zext i1 ... to i32` for SCC; `s_cselect_b32 s22, 1, 0`
turns that into the Y channel.

For `s20 = 0xffff8000` and `s23 = 0x00080008` (offset 8, width 8),
`s_bfe_i32 s21, s20, s23` emits
`and i32 524296, 31`, `lshr i32 524296, 16`, `and ... 127`,
`ashr i32 -32768, %offset`, `sub i32 32, %width`, `and ... 31`,
`shl`/`ashr` by the pad, the `icmp uge i32 %width, 32` and `icmp eq i32 %width, 0`
selects, then the SCC `icmp ne` of the result.

`s_brev_b32 s21, s20` emits `call i32 @dx.op.unary.i32(i32 30, i32 %source)`.
The SCC `zext` from an `s_cmp_eq_u32` placed before the reversal is still the
value consumed by the following `s_cselect_b32`, for both a set and a cleared
SCC.

**Reproduce** with
`ctest --preset release --no-tests=error -R '^native_direct_mesh$'`.
19 changes: 18 additions & 1 deletion src/gfx1013_direct_mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,22 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
if (i.dst==126 || i.dst==127) refuse(pc,"mesh scalar write to EXEC");
if (i.dst==106 || i.dst==107) refuse(pc,"mesh scalar write to VCC");
alu.arithmetic(i,state);
} else if (i.name=="s_not_b32" || i.name=="s_bfe_i32") {
// SOP1/SOP2 bit operations reuse the compute lowering: S_NOT_B32 is
// D = ~S0, and S_BFE_I32 takes S1[4:0] as the offset and S1[22:16]
// as the width of a sign-extended field, where a zero width gives
// zero and a width of 32 or more keeps the arithmetic shift result.
// Both write SCC = (D != 0) per the RDNA 2 ISA.
if (i.dst==126 || i.dst==127) refuse(pc,"mesh scalar write to EXEC");
if (i.dst==106 || i.dst==107) refuse(pc,"mesh scalar write to VCC");
alu.arithmetic(i,state);
} else if (i.name=="s_brev_b32") {
// S_BREV_B32 reverses the 32 bits of S0 through the shared
// unary.i32 intrinsic and, like the compute frontend and the
// gfx2hlsl.py SOP1 table, leaves SCC unchanged.
if (i.dst==126 || i.dst==127) refuse(pc,"mesh scalar write to EXEC");
if (i.dst==106 || i.dst==107) refuse(pc,"mesh scalar write to VCC");
alu.arithmetic(i,state);
} else if (i.name=="v_readfirstlane_b32" && i.dst<106 && i.src[0]==261 && !exchanged_group.empty() && full_exec) {
alu.write(i.dst,exchanged_group);
} else if (i.name=="v_readfirstlane_b32" && i.dst<106 && (full_exec || exec_skip) &&
Expand Down Expand Up @@ -2100,7 +2116,8 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
i.name=="v_mad_f32" ||
i.name=="v_mul_lo_u32" || i.name=="v_mul_hi_u32" || i.name=="v_mac_f32" || i.name=="v_mul_legacy_f32" || i.name=="v_sub_f32" ||
i.name=="v_subrev_f32" || i.name=="v_fmac_f32" || i.name=="v_fmaak_f32" ||
i.name=="v_rsq_f32" || i.name=="v_rcp_f32" || i.name=="v_sqrt_f32" ||
i.name=="v_rsq_f32" || i.name=="v_rcp_f32" || i.name=="v_rcp_iflag_f32" ||
i.name=="v_sqrt_f32" ||
i.name=="v_fma_mixlo_f16" || i.name=="v_fma_mixhi_f16" || i.name=="v_fma_mix_f32" || i.name=="v_pack_b32_f16" ||
i.name=="v_sin_f32" || i.name=="v_cos_f32" ||
i.name.substr(0,15)=="v_cvt_f32_ubyte" || i.name=="v_min_u32" || i.name=="v_max_f32" ||
Expand Down
122 changes: 122 additions & 0 deletions tests/native_direct_mesh_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def _sop1(op, dst, src):
return 0xBE800000 | ((dst & 0x7F) << 16) | ((op & 0xFF) << 8) | (src & 0xFF)


def _sop2(op, dst, src0, src1):
return 0x80000000 | ((op & 0x7F) << 23) | ((dst & 0x7F) << 16) | ((src1 & 0xFF) << 8) | (src0 & 0xFF)


def _sopc(op, src0, src1):
return 0xBF000000 | ((op & 0x7F) << 16) | ((src1 & 0xFF) << 8) | (src0 & 0xFF)


def _inline_u32(value):
if 0 <= value <= 64:
return 128 + value
Expand Down Expand Up @@ -91,6 +99,10 @@ def _v_mov_b32(dst, src):
return (63 << 25) | ((dst & 0xFF) << 17) | (1 << 9) | (src & 0x1FF)


def _vop1(op, dst, src):
return (63 << 25) | ((dst & 0xFF) << 17) | ((op & 0xFF) << 9) | (src & 0x1FF)


def _vopc(opcode, src0, vsrc1):
return (62 << 25) | ((opcode & 0xFF) << 17) | ((vsrc1 & 0xFF) << 9) | (src0 & 0x1FF)

Expand Down Expand Up @@ -129,6 +141,34 @@ def assert_cmpx_ne(text, predicate):
assert_position_x(text, joined)


def assert_export_column(text, value, column):
"""A 32-bit SSA value reaches one POS0 column through uitofp and bitcasts."""
value_re = r'%[\w.]+'
as_float = re.search(rf'(?P<float>{value_re}) = uitofp i32 {re.escape(value)} to float\n', text)
assert as_float, text
bits = re.search(rf'(?P<bits>{value_re}) = bitcast float {re.escape(as_float["float"])} to i32\n', text)
assert bits, text
back = re.search(rf'(?P<back>{value_re}) = bitcast i32 {re.escape(bits["bits"])} to float\n', text)
assert back, text
assert re.search(rf'call void @dx\.op\.storeVertexOutput\.f32\(i32 171, i32 \d+, i32 0, i8 {column}, '
rf'float {re.escape(back["back"])},', text), text


def assert_scalar_export(text, result):
"""SCC = (D != 0) drives POS0.y while the scalar result D drives POS0.x."""
value = r'%[\w.]+'
nonzero = re.search(rf'(?P<nonzero>{value}) = icmp ne i32 {re.escape(result)}, 0\n', text)
assert nonzero, text
scc = re.search(rf'(?P<scc>{value}) = zext i1 {re.escape(nonzero["nonzero"])} to i32\n', text)
assert scc, text
take = re.search(rf'(?P<take>{value}) = icmp ne i32 {re.escape(scc["scc"])}, 0\n', text)
assert take, text
chosen = re.search(rf'(?P<chosen>{value}) = select i1 {re.escape(take["take"])}, i32 1, i32 0\n', text)
assert chosen, text
assert_export_column(text, result, 0)
assert_export_column(text, chosen['chosen'], 1)


def check_profile_writer(folder, archive, profile, blob):
# Desktop state from the one-RTV native fixture in startup_indirect_draw_indexed.cpp.
state = {
Expand Down Expand Up @@ -553,6 +593,88 @@ def run(name, words, expected=None):
('scalar-xor-vcc', 0x896ac114, 'unsupported mesh instruction or EXEC state: s_xor_b32'),
):
run(scalar_k_case, program[:5] + [scalar_k_word] + program[5:], '+0x14: ' + scalar_k_reason)
# v_rcp_iflag_f32 shares v_rcp_f32's alu_rules entry, so v1 = 1.0 / v0
# reaches the position X channel through the existing v_cvt_f32_u32.
text = run('v-rcp-iflag-f32', program[:5] + [_vop1(0x2B, 1, 256)] + program[6:])
reciprocal = re.search(r'(?P<div>%[\w.]+) = fdiv float 1\.000000e\+00, %[\w.]+\n', text)
assert reciprocal, text
assert_position_x(text, rf'bitcast float {re.escape(reciprocal["div"])} to i32')
# s_not_b32 is D = ~S0 with SCC = (D != 0): zero gives all ones and a set
# SCC, all ones gives zero and a clear SCC. POS0.x carries D, POS0.y SCC.
for name, source, complement in (
('s-not-zero', 128, r'xor i32 0, -1'),
('s-not-ones', 193, r'xor i32 -1, -1'),
):
words = program[:5] + [
_sop1(3, 20, source), _sop1(7, 21, 20), _sop2(0xA, 22, 129, 128),
_vop1(1, 1, 21), _vop1(1, 2, 22)] + program[7:]
text = run(name, words)
complement_value = re.search(rf'(?P<result>%[\w.]+) = {complement}\n', text)
assert complement_value, text
assert_scalar_export(text, complement_value['result'])
# s_bfe_i32 takes the offset from S1[4:0] and the width from S1[22:16],
# sign-extends the field and writes SCC = (D != 0). 0x8000 sign-extends
# to 0xffff8000, so bits 15:8 form 0x80 and the result is -128.
bfe = program[:5] + [
0xB0148000, _sop1(3, 23, 255), 0x00080008,
_sop2(0x28, 21, 20, 23), _sop2(0xA, 22, 129, 128),
_vop1(1, 1, 21), _vop1(1, 2, 22)] + program[7:]
text = run('s-bfe-i32', bfe)
bfe_chain = re.search(
r'(?P<offset>%[\w.]+) = and i32 524296, 31\n'
r'\s*(?P<control>%[\w.]+) = lshr i32 524296, 16\n'
r'\s*(?P<width>%[\w.]+) = and i32 (?P=control), 127\n'
r'\s*(?P<shifted>%[\w.]+) = ashr i32 -32768, (?P=offset)\n'
r'\s*(?P<space>%[\w.]+) = sub i32 32, (?P=width)\n'
r'\s*(?P<pad>%[\w.]+) = and i32 (?P=space), 31\n'
r'\s*(?P<high>%[\w.]+) = shl i32 (?P=shifted), (?P=pad)\n'
r'\s*(?P<narrow>%[\w.]+) = ashr i32 (?P=high), (?P=pad)\n'
r'\s*(?P<wide>%[\w.]+) = icmp uge i32 (?P=width), 32\n'
r'\s*(?P<kept>%[\w.]+) = select i1 (?P=wide), i32 (?P=shifted), i32 (?P=narrow)\n'
r'\s*(?P<zero>%[\w.]+) = icmp eq i32 (?P=width), 0\n'
r'\s*(?P<result>%[\w.]+) = select i1 (?P=zero), i32 0, i32 (?P=kept)\n', text)
assert bfe_chain, text
assert_scalar_export(text, bfe_chain['result'])
# s_brev_b32 reverses the bits through the shared unary.i32 intrinsic
# and leaves SCC unchanged: a compare seeded before it still drives the
# select after it, with SCC set and clear.
for name, source, compare, condition in (
('s-brev-scc-one', 128, _sopc(6, 20, 20), r'icmp eq i32 0, 0'),
('s-brev-scc-zero', 193, _sopc(6, 20, 128), r'icmp eq i32 -1, 0'),
):
words = program[:5] + [
_sop1(3, 20, source), compare, _sop1(0xB, 21, 20), _sop2(0xA, 22, 129, 128),
_vop1(1, 1, 21), _vop1(1, 2, 22)] + program[7:]
text = run(name, words)
seeded = re.search(rf'(?P<condition>%[\w.]+) = {condition}\n', text)
assert seeded, text
scc = re.search(rf'(?P<scc>%[\w.]+) = zext i1 {re.escape(seeded["condition"])} to i32\n', text)
assert scc, text
reverse = re.search(r'(?P<reverse>%[\w.]+) = call i32 @dx\.op\.unary\.i32\(i32 30, ', text)
assert reverse, text
take = re.search(rf'(?P<take>%[\w.]+) = icmp ne i32 {re.escape(scc["scc"])}, 0\n', text)
assert take, text
chosen = re.search(rf'(?P<chosen>%[\w.]+) = select i1 {re.escape(take["take"])}, i32 1, i32 0\n', text)
assert chosen, text
assert_export_column(text, reverse['reverse'], 0)
assert_export_column(text, chosen['chosen'], 1)
assert text.index(scc['scc']) < text.index(reverse['reverse']) < text.index(take['take'])
# The three forms refuse an EXEC or VCC destination like the other
# scalar branches, and an unproven source.
for name, word, destination in (
('s-not-exec', _sop1(7, 126, 20), 'EXEC'),
('s-not-vcc', _sop1(7, 106, 20), 'VCC'),
('s-brev-exec', _sop1(0xB, 126, 20), 'EXEC'),
('s-brev-vcc', _sop1(0xB, 106, 20), 'VCC'),
('s-bfe-i32-exec', _sop2(0x28, 126, 20, 23), 'EXEC'),
('s-bfe-i32-vcc', _sop2(0x28, 106, 20, 23), 'VCC'),
):
run(name, program[:5] + [0xB0148000, _sop1(3, 23, 255), 0x00080008, word] + program[5:],
'+0x20: mesh scalar write to ' + destination)
run('s-not-unproven-source', program[:5] + [_sop1(7, 21, 24)] + program[5:],
'+0x14: unbound numeric SGPR s24')
run('s-bfe-i32-unproven-control', program[:5] + [0xB0148000, _sop2(0x28, 21, 20, 24)] + program[5:],
'+0x18: unbound numeric SGPR s24')
# The three primitive slots are 9-bit indices with the edge flag in
# bit 9. 0x20280600 encodes (0,1,2) with all three edge flags set.
edge = program.copy()
Expand Down
Loading