From 816ab82107398fa96c4cf194b07320c692780bfc Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Tue, 1 Dec 2026 13:51:23 +0000 Subject: [PATCH] shader: lower v_and_or_b32 and v_cvt_f16_f32 in the mesh frontend After the scalar bit operations, the forced slot 86 mesh path refused v_cvt_f16_f32 in 357 requests and v_and_or_b32 in 331, ahead of the control-flow, heap-provenance and pixel blocks. - v_and_or_b32 (VOP3 0x371) joins the mesh vector list that already handles v_or3_b32 and v_add3_u32, so it takes the three-source 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) joins the same list and reuses the compute frontend's integer round-to-nearest-even narrowing: the sign comes from bit 31, binary32 subnormals become signed zeros, binary16 subnormals are kept, overflow including the 65520 tie gives 0x7c00, and a NaN keeps its quiet bit. The encoding goes into the low half and the reaching destination value keeps the high half, as the compute frontend does, so an unproven destination still refuses and the merged word is not treated as uniform. tests/native_direct_mesh_check.py adds source-built programs with the run helper: v_and_or_b32 with s20 = 0x0000ffff and s21 = 0x00ff0000 exports (and v0, 65535) | 0x00ff0000, and v_cvt_f16_f32 exports the narrowed word for 1.0f, 0.0f and -2.0f. Controls reject an unbound third source and an unproven conversion source or destination. Observed IR: - %t8 = and i32 %v0, 65535 ; %t9 = or i32 %t8, 16711680 - v_cvt_f16_f32: lshr , 16 / and ... 32768 for the sign, and , 2147483647, lshr ... 23, and ... 8388607, the nearest-even narrowing with icmp ugt %exp, 142 -> 31744 and icmp ugt %mag, 2139095040 -> 32256 | quiet bits, then or %narrow, %sign, and %destination, -65536 and the final or. Verified: - cmake --build --preset release --target compile_dxil (7/7 steps, compile_dxil.exe linked) - python -B tests/native_direct_mesh_check.py : exit 0, "mesh allocation, indexed outputs, group exchange and negative controls passed" - ctest --preset release --no-tests=error -R '^native_direct' 2/2 passed: native_direct_container 15.04 s, native_direct_mesh 13.78 s - python -B tests/docs_links_check.py: 210 files, 807 links Refs #3703 --- docs/experiments/native-mesh-vector-alu.md | 54 +++++++++++ src/gfx1013_direct_mesh.inl | 6 +- tests/native_direct_mesh_check.py | 103 +++++++++++++++++++++ 3 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 docs/experiments/native-mesh-vector-alu.md diff --git a/docs/experiments/native-mesh-vector-alu.md b/docs/experiments/native-mesh-vector-alu.md new file mode 100644 index 000000000..502f632e8 --- /dev/null +++ b/docs/experiments/native-mesh-vector-alu.md @@ -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 , 16` with `and ... 32768` for the sign, +`and i32 , 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'`. + diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 575612d03..29ca2e98f 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -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 && @@ -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; diff --git a/tests/native_direct_mesh_check.py b/tests/native_direct_mesh_check.py index 7fa7767e9..4f64badc5 100644 --- a/tests/native_direct_mesh_check.py +++ b/tests/native_direct_mesh_check.py @@ -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 @@ -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) @@ -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{value_re}) = uitofp i32 {re.escape(value)} to float\n', text) + assert as_float, text + bits = re.search(rf'(?P{value_re}) = bitcast float {re.escape(as_float["float"])} to i32\n', text) + assert bits, text + back = re.search(rf'(?P{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{value}) = icmp ne i32 {re.escape(result)}, 0\n', text) + assert nonzero, text + scc = re.search(rf'(?P{value}) = zext i1 {re.escape(nonzero["nonzero"])} to i32\n', text) + assert scc, text + take = re.search(rf'(?P{value}) = icmp ne i32 {re.escape(scc["scc"])}, 0\n', text) + assert take, text + chosen = re.search(rf'(?P{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{value}) = lshr i32 {source}, 16\n' + rf'\s*(?P{value}) = and i32 (?P=shift), 32768\n' + rf'\s*(?P{value}) = and i32 {source}, 2147483647\n' + rf'\s*(?P{value}) = lshr i32 (?P=mag), 23\n' + rf'\s*(?P{value}) = and i32 (?P=mag), 8388607\n', text) + assert head, text + tail = re.search( + rf'(?P{value}) = icmp ugt i32 {re.escape(head["exp"])}, 142\n' + rf'\s*(?P{value}) = select i1 (?P=over), i32 31744, i32 {value}\n' + rf'\s*(?P{value}) = icmp ugt i32 {re.escape(head["mag"])}, 2139095040\n' + rf'\s*(?P{value}) = lshr i32 {re.escape(head["frac"])}, 13\n' + rf'\s*(?P{value}) = or i32 (?P=quiet_shift), 32256\n' + rf'\s*(?P{value}) = select i1 (?P=nan), i32 (?P=quiet), i32 (?P=inf)\n' + rf'\s*(?P{value}) = or i32 (?P=narrow), {re.escape(head["sign"])}\n' + rf'\s*(?P{value}) = and i32 {value}, -65536\n' + rf'\s*(?P{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 = { @@ -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%[\w.]+) = and i32 (?P%[\w.]+), 65535\n' + r'\s*(?P%[\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()