Skip to content
Merged
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
54 changes: 54 additions & 0 deletions docs/experiments/native-mesh-vector-alu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Mesh frontend vector bit-select and binary16 narrowing

The direct C++ mesh frontend behind `gfx1013-raw-ms` now lowers two more forms
that the forced slot 86 mesh path refused. After the scalar bit operations, the census of the
2 873 title requests ordered the next refusals `v_cvt_f16_f32` (357) and
`v_and_or_b32` (331), followed by the control-flow, heap-provenance and pixel
blocks.

## Lowering

Both names join the vector list in
[gfx1013_direct_mesh.inl](../../src/gfx1013_direct_mesh.inl) that already handles
`v_or3_b32`, `v_add3_u32` and the other three-source forms, so they share
`arithmetic()` with the compute frontend.

- `v_and_or_b32` (VOP3 `0x371`) takes its arity of three from the
`alu_rules` entry and lowers to `(S0 & S1) | S2`. The shared operand gate
still refuses `abs`, `neg`, `clamp` and `omod`.
- `v_cvt_f16_f32` (VOP1 `0x0A`) narrows S0 with the same integer
round-to-nearest-even conversion the compute frontend uses, so desktop
conversion and denormal handling cannot change the encoding: the sign comes
from bit 31, binary32 subnormals become signed zeros, a binary16 subnormal is
kept, the 65520 tie and everything above it give `0x7c00`, and a NaN keeps
its quiet bit (`0x7e00`). The low half of the destination receives the
encoding and the high half keeps the reaching destination value, as the
compute frontend and the RDNA 2 high-half-preservation rule do. Because the
merged word reads the destination, an unproven destination still refuses
(`unproven mesh entry register`); the conversion is not treated as a
uniform result for `v_readfirstlane`.

## Observed IR

With `s20 = 0x0000ffff` and `s21 = 0x00ff0000`, the source-built
`v_and_or_b32 v1, v0, s20, s21` emits `and i32 %v0, 65535` then
`or i32 %and, 16711680`, and POS0.x exports that word.

`v_cvt_f16_f32 v1, v3` with v3 holding 1.0f, 0.0f or -2.0f emits
`lshr i32 <bits>, 16` with `and ... 32768` for the sign,
`and i32 <bits>, 2147483647`, `lshr ... 23` and `and ... 8388607` for the
magnitude, exponent and fraction, the nearest-even narrowing with
`icmp ugt i32 %exp, 142` giving `31744`, `icmp ugt i32 %mag, 2139095040`
selecting `32256` plus the quiet bits, then `or i32 %narrow, %sign`,
`and i32 %destination, -65536` and the final `or`. POS0.x exports the whole
word.

## Controls

An unbound third source keeps `unbound numeric SGPR s24`. A conversion from an
unproven VGPR 24 or into an unproven VGPR 5 keeps `unproven mesh entry
register`.

**Reproduce** with
`ctest --preset release --no-tests=error -R '^native_direct'`.

6 changes: 5 additions & 1 deletion src/gfx1013_direct_mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2108,8 +2108,10 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
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" ||
i.name=="v_mad_u32_u24" || i.name=="v_add3_u32" || i.name=="v_bfe_u32" || i.name=="v_mov_b32" || i.name=="v_add_nc_u32" || i.name=="v_sub_nc_u32" || i.name=="v_subrev_nc_u32" || i.name=="v_add_co_ci_u32" || i.name=="v_lshlrev_b32" ||
i.name=="v_lshrrev_b32" || i.name=="v_or_b32" || i.name=="v_or3_b32" || i.name=="v_and_b32" ||
i.name=="v_lshrrev_b32" || i.name=="v_or_b32" || i.name=="v_or3_b32" || i.name=="v_and_or_b32" ||
i.name=="v_and_b32" ||
i.name=="v_xor_b32" || i.name=="v_lshl_add_u32" || i.name=="v_cvt_f32_u32" || i.name=="v_cvt_f32_i32" ||
i.name=="v_cvt_f16_f32" ||
i.name=="v_mul_f32" || i.name=="v_add_f32" || i.name=="v_fma_f32" || i.name=="v_bfm_b32" ||
i.name=="v_bcnt_u32_b32")) {
const bool flat=i.name=="v_lshl_add_u32" && alu.operand32(i,i.src[0])==wave_number &&
Expand All @@ -2124,6 +2126,8 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
pair || i.name=="v_mul_legacy_f32" || i.name=="v_bfm_b32" || i.name=="v_bcnt_u32_b32" ? 2 :
i.name=="v_trunc_f32" || i.name=="v_fract_f32" || i.name=="v_cos_f32" ||
i.name.substr(0,15)=="v_cvt_f32_ubyte" ? 1 : 0;
// v_cvt_f16_f32 keeps the destination's high half, so its single
// source cannot prove the merged word uniform on its own.
const auto uniform_source=[&](unsigned r) {
if (r==106 || r==107 || r==126 || r==127 || r==251 || r==252) return false;
if (constant32(i,r)) return true;
Expand Down
103 changes: 103 additions & 0 deletions tests/native_direct_mesh_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ def assert_export_column(text, value, column):
rf"float {re.escape(back['back'])},", text), text


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 _sop1_literal(op, dst, value):
return [_sop1(op, dst, 255), value & 0xFFFFFFFF]


def _inline_u32(value):
if 0 <= value <= 64:
return 128 + value
Expand Down Expand Up @@ -104,6 +116,16 @@ 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 _vop3(op, dst, src0, src1, src2):
return [0xD4000000 | ((op & 0x3FF) << 16) | (dst & 0xFF),
(src0 & 0x1FF) | ((src1 & 0x1FF) << 9) | ((src2 & 0x1FF) << 18)]


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

Expand Down Expand Up @@ -142,6 +164,59 @@ 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 assert_half_narrowing(text, source):
"""The shared nearest-even binary16 narrowing and the destination merge."""
value = r'%[\w.]+'
head = re.search(
rf'(?P<shift>{value}) = lshr i32 {source}, 16\n'
rf'\s*(?P<sign>{value}) = and i32 (?P=shift), 32768\n'
rf'\s*(?P<mag>{value}) = and i32 {source}, 2147483647\n'
rf'\s*(?P<exp>{value}) = lshr i32 (?P=mag), 23\n'
rf'\s*(?P<frac>{value}) = and i32 (?P=mag), 8388607\n', text)
assert head, text
tail = re.search(
rf'(?P<over>{value}) = icmp ugt i32 {re.escape(head["exp"])}, 142\n'
rf'\s*(?P<inf>{value}) = select i1 (?P=over), i32 31744, i32 {value}\n'
rf'\s*(?P<nan>{value}) = icmp ugt i32 {re.escape(head["mag"])}, 2139095040\n'
rf'\s*(?P<quiet_shift>{value}) = lshr i32 {re.escape(head["frac"])}, 13\n'
rf'\s*(?P<quiet>{value}) = or i32 (?P=quiet_shift), 32256\n'
rf'\s*(?P<narrow>{value}) = select i1 (?P=nan), i32 (?P=quiet), i32 (?P=inf)\n'
rf'\s*(?P<half>{value}) = or i32 (?P=narrow), {re.escape(head["sign"])}\n'
rf'\s*(?P<keep>{value}) = and i32 {value}, -65536\n'
rf'\s*(?P<result>{value}) = or i32 (?P=half), (?P=keep)\n', text)
assert tail, text
assert_export_column(text, tail['result'], 0)


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 @@ -566,6 +641,34 @@ 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)
# VOP3 v_and_or_b32 v1, v0, s20, s21 is (S0 & S1) | S2 through the
# shared three-source rule; s20 is 0x0000ffff and s21 0x00ff0000.
and_or = program[:5] + [
*_sop1_literal(3, 20, 0x0000FFFF), *_sop1_literal(3, 21, 0x00FF0000),
*_vop3(0x371, 1, 256, 20, 21)] + program[6:]
text = run('v-and-or-b32', and_or)
masked = re.search(r'(?P<low>%[\w.]+) = and i32 (?P<value>%[\w.]+), 65535\n'
r'\s*(?P<result>%[\w.]+) = or i32 (?P=low), 16711680\n', text)
assert masked, text
assert_export_column(text, masked['result'], 0)
run('v-and-or-b32-unproven', program[:5] + [
*_sop1_literal(3, 20, 0x0000FFFF), *_vop3(0x371, 1, 256, 20, 24)] + program[6:],
'+0x1c: unbound numeric SGPR s24')
# v_cvt_f16_f32 narrows with the shared nearest-even integer conversion
# and keeps the destination's high half; POS0.x exports the whole word.
for name, source, bits in (
('v-cvt-f16-f32-normal', 242, '1065353216'), # 1.0f
('v-cvt-f16-f32-zero', 128, '0'), # 0.0f
('v-cvt-f16-f32-negative', 245, '-1073741824'), # -2.0f
):
words = program[:6] + [_vop1(1, 3, source), _vop1(0x0A, 1, 259)] + program[6:]
assert_half_narrowing(run(name, words), bits)
run('v-cvt-f16-f32-unproven-source',
program[:6] + [_vop1(1, 3, 242), _vop1(0x0A, 1, 280)] + program[6:],
'+0x1c: unproven mesh entry register 280')
run('v-cvt-f16-f32-unproven-destination',
program[:6] + [_vop1(1, 3, 242), _vop1(0x0A, 5, 259)] + program[6:],
'+0x1c: unproven mesh entry register 261')
# 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