From 0e5aed6f65779622834877aa50755be4ccecfd08 Mon Sep 17 00:00:00 2001 From: triggered02 <153171514+triggered02@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:38:21 +0530 Subject: [PATCH 1/2] shader: lower v_cvt_off_f32_i4 in startup gfx2hlsl --- .github/workflows/windows.yml | 1 + AGENTS.md | 1 + CMakeLists.txt | 10 + CMakePresets.json | 2 +- docs/experiments.md | 25 + .../startup_gfx2hlsl_cvt_off_f32_i4_check.py | 719 ++++++++++++++++++ tools/startup/gfx2hlsl.py | 17 +- 7 files changed, 771 insertions(+), 4 deletions(-) create mode 100644 tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index d62cf5f7a..c950fda60 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -227,6 +227,7 @@ jobs: tests/startup_live_telemetry_check.py \ tests/startup_gfx2hlsl_cvt_alu_check.py \ tests/startup_gfx2hlsl_cvt_pk_u8_check.py \ + tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py \ tests/startup_gfx2hlsl_int_med3_check.py \ tests/startup_gfx2hlsl_cselect_mask_check.py \ tests/startup_gfx2hlsl_mask_overwrite_check.py \ diff --git a/AGENTS.md b/AGENTS.md index 138a3ee0d..a8c9d8093 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -228,6 +228,7 @@ python3 -m venv out/pe-env ./out/pe-env/bin/python -B tests/startup_image_gather_lz_check.py ./out/pe-env/bin/python -B tests/startup_image_sample_c_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_cvt_alu_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py ./out/pe-env/bin/python -B tests/startup_triage_run_check.py ./out/pe-env/bin/python -B tests/startup_run_ps1_check.py ./out/pe-env/bin/python -B tests/startup_run_sh_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index c1c11e495..c978afd22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3214,6 +3214,16 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter} $<$:${STARTUP_FXC}>) set_tests_properties(startup_gfx2hlsl_cvt_pk_u8_${adapter} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77) endforeach() + # gfx2hlsl v_cvt_off_f32_i4 (VOP1 0x0e and VOP3A 0x18e): signed 4-bit nibble conversion with output modifiers, + # low16_only provenance, uniformity tracking, refusals, and WARP/hardware draws with mutated controls. + add_test(NAME startup_gfx2hlsl_cvt_off_f32_i4_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_gfx2hlsl_cvt_off_f32_i4_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py" draw "${DXC_EXECUTABLE}" + "$" ${adapter} $<$:${STARTUP_FXC}>) + set_tests_properties(startup_gfx2hlsl_cvt_off_f32_i4_${adapter} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77) + endforeach() # gfx2hlsl 16-bit integer VOPC compares (v_cmp/v_cmpx _i16/_u16, #1723): translation controls, then draws of a # census-shaped v_cmpx kill and of every condition folded into mask words, with mutated lowerings. add_test(NAME startup_gfx2hlsl_cmp16_controls COMMAND "${INTAKE_PYTHON}" -B diff --git a/CMakePresets.json b/CMakePresets.json index ffb951925..398176d5b 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -42,7 +42,7 @@ "displayName": "Windows pull request smoke tests", "filter": { "include": { - "name": "^(desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|startup_launch|startup_tools_lint|startup_indirect_(dispatch|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?)$" + "name": "^(desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|startup_launch|startup_tools_lint|startup_gfx2hlsl_cvt_off_f32_i4_controls|startup_indirect_(dispatch|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?)$" } } }, diff --git a/docs/experiments.md b/docs/experiments.md index 467948493..bcdecb858 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -15121,6 +15121,31 @@ data, because a refusal is observable at the `Stage` entry point. ```powershell ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_sad_(controls|warp|hardware)$' -V +## Startup translator: signed 4-bit offset conversion (`v_cvt_off_f32_i4`) + +`tools/startup/gfx2hlsl.py` lowers `v_cvt_off_f32_i4` in VOP1 `_e32` (`0x0e`) and VOP3A `_e64` (`0x18e`) via +`VEC_U_TO_F` as `(float(int({0} << 28) >> 28) * 0.0625)`, matching RDNA 2 ISA semantics: the offset is inherent +in the signed 4-bit two's-complement nibble (`[3:0]` in `[-8, +7]`, with no separate `- 8` bias term subtracted), +scaled by `1/16` to produce exact binary32 steps in `[-0.5, +0.4375]`. As a `VEC_U_TO_F` +conversion, it accepts output modifiers (`clamp`, `mul:2`, `mul:4`, `div:2`, scaled before `clamp`), marks its source +`low16_only=True` in `Stage.lower` because only bits `[3:0]` are read, and participates in `UNIFORM_VALU` tracking for +`v_readfirstlane_b32`. VOP3A source `abs`/`neg` bits, `op_sel`, wrong operand counts, and SDWA forms (`_sdwa`) are +refused. + +[The check fixture](../tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py) constructs synthetic gfx1013 VOP1 `_e32` and +VOP3A `_e64` words decoded via `gfx2hlsl.decode()` (no game data), verifies all 16 nibble values `0..15` with upper-bit +garbage (`0x12345678`, `0xfffffff8`), output modifiers, `low16_only` provenance (odd-dmask `d16` `image_load` followed +by `v_cvt_off_f32_i4`, in-place overwrite clearing, and whole-word read refusal), `UNIFORM_VALU` propagation and refusals, +`abs`/`neg`/`op_sel`/arity/SDWA refusals, and an unlowered control. On WARP (`Microsoft Basic Render Driver`, driver +`10.0.26100.9818`, debug layer unavailable) with FXC `ps_5_1` and DXC `ps_6_0`, all 14 draws matched (7 per compiler, +covering all 16 nibbles, upper-bit garbage, and output modifiers) and all 6 mutated draws (`unsigned_nibble`, +`5bit_signext`, and `early_clamp`) failed against the reference with their mutated output words. Not covered: hardware +GPU draw (`startup_gfx2hlsl_cvt_off_f32_i4_hardware` not run here) and SDWA/DPP forms (refused). + +```powershell +python -B tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py +python -B tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py draw warp +ctest --preset dev --no-tests=error -R "^startup_gfx2hlsl_cvt_off_f32_i4_(controls|warp|hardware)$" -V ``` ## Opt-in release of retired slot-133 title-memory textures (Slice 2) diff --git a/tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py b/tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py new file mode 100644 index 000000000..61c7a648a --- /dev/null +++ b/tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py @@ -0,0 +1,719 @@ +"""`v_cvt_off_f32_i4` (VOP1 0x0e and VOP3A 0x18e) in tools/startup/gfx2hlsl.py. + + (no arguments) translation, decoder, evaluator, provenance, uniformity, refusal, and + unlowered-control checks, pure Python + draw [fxc] + compile the translated pairs with FXC ps_5_1 (when given) and DXC ps_6_0, + draw all 16 nibble values, upper-bit garbage, and e64 output modifiers on the + adapter, and require mutated lowerings to fail with their mutated words + +RDNA 2 ISA semantics: + `v_cvt_off_f32_i4` reads bits `[3:0]` of SRC0 as a signed 4-bit two's-complement integer in `[-8, +7]` and returns + `float(i4) * 0.0625` (exact steps of `1/16`, `[-0.5, +0.4375]`). As a float-result conversion in `VEC_U_TO_F`, it + accepts output modifiers (`clamp`, `mul:2`, `mul:4`, `div:2`, applied with scaling before `clamp`), reads only bits + `[3:0]` (`low16_only=True`), and preserves uniformity (`UNIFORM_VALU`). Source `abs`/`neg`, `op_sel`, wrong operand + counts, and SDWA forms stay refused. + +Every instruction tested here is constructed as synthetic gfx1013 machine words from the layouts in +`tools/disasm_rdna2.py` and decoded via `gfx2hlsl.decode()` (no game data). +""" +import re +import struct +import sys +import tempfile +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE.parent / 'tools')) +sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) +sys.path.insert(0, str(HERE)) +import gfx2hlsl as G # noqa: E402 +from startup_flat_param_check import compile_hlsl, draw, root_blob # noqa: E402 +from startup_gfx2hlsl_bit_alu_check import POST_WORDS, PRE_WORDS, vs_words, words_text # noqa: E402 +from startup_image_3d_check import VS_WORDS as FULLSCREEN_VS_WORDS, root_blob as table_root_blob # noqa: E402 + +MASK = 0xffffffff +VOP1_CVT_OFF_F32_I4 = 0x0e +VOP1_CVT_F32_UBYTE2 = 0x13 +VOP1_E64_BASE = 0x180 +VOP2_ADD_NC_U32 = 0x25 +VOP1_READFIRSTLANE_B32 = 0x02 +VOPC_CMPX_EQ_U32 = 0xd2 + +OMOD = {None: 0, '': 0, 'mul:2': 1, 'mul:4': 2, 'div:2': 3} +OMOD_SCALE = {None: 1.0, '': 1.0, 'mul:2': 2.0, 'mul:4': 4.0, 'div:2': 0.5} +INLINE_INT = {n: 128 + n for n in range(65)} +INLINE_INT.update({-n: 192 + n for n in range(1, 17)}) +LITERAL = 255 +S_LOAD_DESC_X8 = bytes.fromhex('04040cf4000000fa') # s_load_dwordx8 s[16:23], s[8:9], 0x0 +S_WAITCNT_LGKMCNT0 = bytes.fromhex('7fc08cbf') # s_waitcnt lgkmcnt(0) +S_WAITCNT_VMCNT0 = bytes.fromhex('703f8cbf') # s_waitcnt vmcnt(0) +S_ENDPGM = 0xbf810000 +EXP_MRT0_V12_V15 = bytes.fromhex('0f1800f80c0d0e0f') + + +def V(n): + return 256 + n + + +def S(n): + return n + + +# ---- Synthetic gfx1013 encoders ------------------------------------------------------------------------------------- + +def encode_vop1_e32(op, vdst, src0, *, literal=None): + w0 = 0x7e000000 | ((vdst & 0xff) << 17) | ((op & 0xff) << 9) | (src0 & 0x1ff) + return struct.pack('> 2) & 0x1f) << 16) | (1 << 31) + return struct.pack('> 1) & 0x3f) + w1 = offset_imm & 0x1fffff + return struct.pack('= 8 else nibble + val = i4 * 0.0625 + if omod: + val *= OMOD_SCALE[omod] + if clamp: + if val <= 0.0: + val = 0.0 + elif val >= 1.0: + val = 1.0 + return f32_bits(val) + + +def wrong_unsigned_nibble(word, *, omod='', clamp=False): + """Mutated reference: treats bits[3:0] as unsigned 0..15 instead of signed -8..+7.""" + val = (word & 0xf) * 0.0625 + if omod: + val *= OMOD_SCALE[omod] + if clamp: + val = min(max(val, 0.0), 1.0) + return f32_bits(val) + + +def wrong_5bit_nibble(word, *, omod='', clamp=False): + """Mutated reference: sign-extends 5 bits (<< 27 >> 27) instead of 4 bits (<< 28 >> 28).""" + low5 = word & 0x1f + i5 = low5 - 32 if low5 >= 16 else low5 + val = i5 * 0.0625 + if omod: + val *= OMOD_SCALE[omod] + if clamp: + val = min(max(val, 0.0), 1.0) + return f32_bits(val) + + +class U32(int): + def __new__(cls, value): + return super().__new__(cls, int(value) & MASK) + + def __lshift__(self, n): + return U32((int(self) << (int(n) & 31)) & MASK) + + def __rshift__(self, n): + return U32((int(self) & MASK) >> (int(n) & 31)) + + def __and__(self, other): + return U32(int(self) & int(other)) + + def __rand__(self, other): + return U32(int(other) & int(self)) + + +class I32(int): + def __new__(cls, value): + u = int(value) & MASK + return super().__new__(cls, u - 0x100000000 if u & 0x80000000 else u) + + def __rshift__(self, n): + return I32(int(self) >> (int(n) & 31)) + + +class F32: + def __init__(self, bits): + self.bits = int(bits) & MASK + + @classmethod + def from_float(cls, value): + return cls(f32_bits(value)) + + def to_float(self): + return bits_to_f32(self.bits) + + def __mul__(self, factor): + return F32.from_float(self.to_float() * float(factor)) + + +def hlsl_saturate(val): + f = val.to_float() + if f <= 0.0: + return F32(0) + if f >= 1.0: + return F32(0x3f800000) + return val + + +HLSL_ENV = { + 'U32': U32, + 'uint': U32, + 'int': I32, + 'float': lambda x: F32.from_float(int(x)), + 'saturate': hlsl_saturate, + 'U': lambda f: U32(f.bits), +} + + +def eval_hlsl_expr(expr, regs): + py_expr = re.sub(r'\b(0x[0-9a-fA-F]+|\d+)u\b', r'U32(\1)', expr) + return int(eval(py_expr, {'__builtins__': {}}, {**HLSL_ENV, **{k: U32(v) for k, v in regs.items()}})) + + +# ---- Test harness helpers ------------------------------------------------------------------------------------------- + +failures = [] + + +def check(label, condition, detail=''): + print(f"{label}: {'ok' if condition else 'MISMATCH'}", flush=True) + if not condition: + failures.append(label) + if detail: + print(' ' + detail, flush=True) + + +def make_pipeline(ps_body_bytes, *, use_table_root=False): + """Build a full G.Pipeline from synthetic gfx1013 bytes (calls G.decode() and G.Stage).""" + if use_table_root: + return G.Pipeline(FULLSCREEN_VS_WORDS, ps_body_bytes, table_root_blob()) + return G.Pipeline(vs_words([(0, 0, 0)] * 4), ps_body_bytes, root_blob()) + + +def decode_and_one(code, *, sym=None, low16_v=()): + """Decode bytes with G.decode() and execute them on a straight-line Stage.""" + ins = G.decode(code) + stage = G.Stage.__new__(G.Stage) + stage.kind = 'ps' + stage.lines = [] + stage.sym = dict(sym or {}) + stage.used_s, stage.used_v, stage.uniform_v = set(), set(), set() + stage.varying_s = set() + stage.scc_uniform = True + stage.uniform_expr = {} + stage.past_entry = False + stage.has_branches = False + stage.exec_known = True + stage.masks = set() + stage.exports = {} + stage.low16_only_v = set(low16_v) + stage.pinned_low16_v = set() + stage.whole_read_v = set() + stage.byte_float_v = set() + stage.bad_sgprs = set() + stage.defined_uniform_s = None + stage.since_join = None + pipe = type('Pipe', (), {'resources': {}, 'notes': set(), 'modified': set(), 'sampler_heap': False})() + stage.pipe = pipe + for _off, _sz, name, asm in ins: + stage.one(name, asm) + return ins, stage + + +def is_refused_bytes(code, expected_substr='', *, via_pipeline=False): + try: + if via_pipeline: + G.Pipeline(vs_words([(0, 0, 0)] * 4), code, root_blob()) + else: + decode_and_one(code) + except G.Unsupported as err: + return expected_substr in str(err) + return False + + +def is_refused_asm(asm, expected_substr=''): + stage = G.Stage.__new__(G.Stage) + stage.lines, stage.sym = [], {} + stage.used_s, stage.used_v, stage.uniform_v = set(), set(), set() + stage.exec_known = True + try: + stage.one(asm.split(' ', 1)[0], asm) + except G.Unsupported as err: + return expected_substr in str(err) + return False + + +# ---- Pure-Python controls ------------------------------------------------------------------------------------------- + +def check_decode_and_lowering(): + # 1. VOP1 _e32 and VOP3A _e64 decoding via G.decode() + decode_vectors = ( + (encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 4, V(2)), 'v_cvt_off_f32_i4_e32 v4, v2'), + (encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 4, S(2)), 'v_cvt_off_f32_i4_e32 v4, s2'), + (encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 4, INLINE_INT[3]), 'v_cvt_off_f32_i4_e32 v4, 3'), + (encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 4, LITERAL, literal=0x12345678), + 'v_cvt_off_f32_i4_e32 v4, 0x12345678'), + (encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2)), 'v_cvt_off_f32_i4_e64 v4, v2'), + (encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), clamp=1), 'v_cvt_off_f32_i4_e64 v4, v2 clamp'), + (encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), omod='mul:2'), 'v_cvt_off_f32_i4_e64 v4, v2 mul:2'), + (encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), omod='mul:4'), 'v_cvt_off_f32_i4_e64 v4, v2 mul:4'), + (encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), omod='div:2'), 'v_cvt_off_f32_i4_e64 v4, v2 div:2'), + (encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), clamp=1, omod='mul:4'), + 'v_cvt_off_f32_i4_e64 v4, v2 clamp mul:4'), + (encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, S(2), omod='div:2'), 'v_cvt_off_f32_i4_e64 v4, s2 div:2'), + (encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, LITERAL, literal=0xfffffff8), + 'v_cvt_off_f32_i4_e64 v4, 0xfffffff8'), + ) + for code, expected_asm in decode_vectors: + ins, st = decode_and_one(code) + check(f'decode+lower {expected_asm}', + len(ins) == 1 and ins[0][3] == expected_asm and len(st.lines) == 1, + f'got ins={ins!r} lines={st.lines!r}') + + # 2. All 16 nibble values 0..15 + upper-bit garbage across e32 and e64 * (omod x clamp) + garbage_prefixes = (0x00000000, 0x12345670, 0xfffffff0, 0x80000000, 0xdeadbee0) + samples = [prefix | nib for prefix in garbage_prefixes for nib in range(16)] + samples.extend((0x12345678, 0xfffffff8)) + + eval_count = 0 + eval_mismatches = [] + for enc_name, code in ( + ('e32', encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 4, V(2))), + ('e64', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2))), + ): + _, st = decode_and_one(code) + expr = st.lines[0][len('v4 = '):-1] + for word in samples: + eval_count += 1 + got = eval_hlsl_expr(expr, {'v2': word}) + want = isa_cvt_off_f32_i4(word) + if got != want and len(eval_mismatches) < 3: + eval_mismatches.append((enc_name, hex(word), hex(got), hex(want))) + + for omod in ('', 'mul:2', 'mul:4', 'div:2'): + for clamp in (0, 1): + code = encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), omod=omod, clamp=clamp) + _, st = decode_and_one(code) + expr = st.lines[0][len('v4 = '):-1] + for word in samples: + eval_count += 1 + got = eval_hlsl_expr(expr, {'v2': word}) + want = isa_cvt_off_f32_i4(word, omod=omod, clamp=bool(clamp)) + if got != want and len(eval_mismatches) < 3: + eval_mismatches.append((omod, clamp, hex(word), hex(got), hex(want))) + + check(f'all {eval_count} nibble/garbage/modifier evaluations match RDNA2 ISA', + not eval_mismatches, repr(eval_mismatches)) + + # 3. Verify hardware order: omod before clamp (5 -> 0.3125 * 4 = 1.25 -> saturate = 1.0, not 1.25; + # 8 -> -0.5 * 2 = -1.0 -> saturate = 0.0) + _, st_cm4 = decode_and_one(encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), omod='mul:4', clamp=1)) + expr_cm4 = st_cm4.lines[0][len('v4 = '):-1] + check('clamp applies after mul:4 on nibble 5 (0.3125 * 4 -> 1.0)', + eval_hlsl_expr(expr_cm4, {'v2': 0x12345675}) == f32_bits(1.0), expr_cm4) + check('clamp clamps negative nibble 8 (-0.5 * 4 -> 0.0)', + eval_hlsl_expr(expr_cm4, {'v2': 0xfffffff8}) == f32_bits(0.0), expr_cm4) + + # 4. Literal and inline constant sources via G.decode() + for desc, code, want_val in ( + ('e32 literal 0x12345678', encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 4, LITERAL, literal=0x12345678), -0.5), + ('e32 literal 0xfffffff8', encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 4, LITERAL, literal=0xfffffff8), -0.5), + ('e64 literal 0x12345678', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, LITERAL, literal=0x12345678), -0.5), + ('e64 literal 0xfffffff8', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, LITERAL, literal=0xfffffff8), -0.5), + ('e32 inline 7', encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 4, INLINE_INT[7]), 0.4375), + ('e32 inline -1', encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 4, INLINE_INT[-1]), -0.0625), + ('e64 inline -8', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, INLINE_INT[-8]), -0.5), + ): + _, st = decode_and_one(code) + expr = st.lines[0][len('v4 = '):-1] + got = eval_hlsl_expr(expr, {}) + check(f'constant source {desc}', got == f32_bits(want_val), f'{expr} -> {hex(got)}') + + +def check_low16_provenance(): + # Build full synthetic pixel shaders starting with an odd-dmask d16 image_load into v4, + # making v4 low16_only_v via real decoded MIMG words through G.Pipeline / G.decode(). + load_prefix = (S_LOAD_DESC_X8 + S_WAITCNT_LGKMCNT0 + + encode_vop1_e32(0x01, 0, INLINE_INT[0]) + + encode_vop1_e32(0x01, 1, INLINE_INT[0]) + + encode_mimg_load_d16(vdata=4, vaddr=0, srsrc=16, dmask=0x1) + + S_WAITCNT_VMCNT0) + exp_v5 = encode_exp_mrt0(5, 5, 5, 5) + encode_endpgm() + + # Accepted: v_cvt_off_f32_i4_e32 and _e64 reading low16_only v4 into v5 + for tag, cvt in ( + ('e32', encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 5, V(4))), + ('e64', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 5, V(4), omod='mul:2', clamp=1)), + ): + ps_bytes = load_prefix + cvt + exp_v5 + try: + pipe = make_pipeline(ps_bytes, use_table_root=True) + hlsl = pipe.hlsl_ps() + ok = 4 in pipe.ps.low16_only_v and 5 not in pipe.ps.low16_only_v and '<< 28' in hlsl + detail = '' + except G.Unsupported as err: + ok = False + detail = str(err) + check(f'low16_only accepted by v_cvt_off_f32_i4_{tag}', ok, detail) + + # Accepted: in-place overwrite v4 <- v_cvt_off_f32_i4(v4) clears low16_only on v4 so whole-word read succeeds + ps_inplace = (load_prefix + encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 4, V(4)) + + encode_vop2_e32(VOP2_ADD_NC_U32, 6, INLINE_INT[1], V(4)) + + encode_exp_mrt0(6, 6, 6, 6) + encode_endpgm()) + try: + pipe = make_pipeline(ps_inplace, use_table_root=True) + ok = 4 not in pipe.ps.low16_only_v + detail = '' + except G.Unsupported as err: + ok = False + detail = str(err) + check('in-place v_cvt_off_f32_i4 v4, v4 clears low16_only for subsequent whole-word read', ok, detail) + + # Negative control: reading v4 whole-word AFTER v_cvt_off_f32_i4 v5, v4 must still fail + ps_leak = (load_prefix + encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 5, V(4)) + + encode_vop2_e32(VOP2_ADD_NC_U32, 6, INLINE_INT[1], V(4)) + + encode_exp_mrt0(6, 6, 6, 6) + encode_endpgm()) + try: + make_pipeline(ps_leak, use_table_root=True) + except G.Unsupported as err: + ok = 'whole-word read of v4' in str(err) + detail = str(err) + else: + ok = False + detail = 'unexpectedly accepted' + check('low16_only still catches whole-word read of v4 after v_cvt_off_f32_i4 v5, v4', ok, detail) + + # Negative control: ubyte2 reads bits[23:16] of v4 and must still be refused on odd d16 image_load + ps_ubyte2 = load_prefix + encode_vop1_e32(VOP1_CVT_F32_UBYTE2, 5, V(4)) + exp_v5 + try: + make_pipeline(ps_ubyte2, use_table_root=True) + except G.Unsupported as err: + ok = 'whole-word read of v4' in str(err) + detail = str(err) + else: + ok = False + detail = 'unexpectedly accepted' + check('low16_only negative control: v_cvt_f32_ubyte2 v5, v4 refused', ok, detail) + + +def check_uniform_valu(): + # table_root_blob gives s9 = root constant 1 (uniform user-data scalar). + # Translate real pixel programs via G.Pipeline -> G.decode() -> G.Stage with v_readfirstlane_b32. + rfl = encode_vop1_e32(VOP1_READFIRSTLANE_B32, 10, V(7)) + copy_to_v5 = encode_vop1_e32(0x01, 5, S(10)) + tail = copy_to_v5 + encode_exp_mrt0(5, 5, 5, 5) + encode_endpgm() + + for tag, body in ( + ('e32 uniform s9', encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 7, S(9)) + rfl), + ('e64 uniform s9 mul:2 clamp', + encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 7, S(9), omod='mul:2', clamp=1) + rfl), + ('chained uniform v7 -> v8 -> readfirstlane', + encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 7, S(9)) + + encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 8, V(7), omod='div:2') + + encode_vop1_e32(VOP1_READFIRSTLANE_B32, 10, V(8))), + ): + try: + pipe = make_pipeline(body + tail, use_table_root=True) + lines = [line for _, blk, _ in pipe.ps.blocks for line in blk] + ok = any(line.startswith('s10 = v') for line in lines) + detail = repr(lines) + except G.Unsupported as err: + ok = False + detail = str(err) + check(f'UNIFORM_VALU accepted ({tag})', ok, detail) + + # Negative control 1: varying VGPR v0 -> v7 -> v_readfirstlane_b32 s10, v7 must be refused + ps_varying = encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 7, V(0)) + rfl + tail + try: + make_pipeline(ps_varying, use_table_root=True) + except G.Unsupported as err: + ok = 'v_readfirstlane_b32 requires a proven uniform source' in str(err) + detail = str(err) + else: + ok = False + detail = 'unexpectedly accepted' + check('UNIFORM_VALU refuses varying VGPR source in v_readfirstlane_b32', ok, detail) + + # Negative control 2: unknown exec after v_cmpx_eq_u32 -> v_cvt_off_f32_i4 v7, s9 -> v_readfirstlane_b32 + ps_exec_unknown = (encode_vopc_e32(VOPC_CMPX_EQ_U32, INLINE_INT[0], 0) + + encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 7, S(9)) + rfl + tail) + try: + make_pipeline(ps_exec_unknown, use_table_root=True) + except G.Unsupported as err: + ok = 'v_readfirstlane_b32 requires a proven uniform source' in str(err) + detail = str(err) + else: + ok = False + detail = 'unexpectedly accepted' + check('UNIFORM_VALU refuses v_readfirstlane_b32 when exec is unknown', ok, detail) + + +def check_refusals_and_unlowered(): + # Refusals: abs, neg, op_sel, SDWA (all decoded from synthetic gfx1013 bytes via G.decode()) + byte_refusals = ( + ('e64 abs on v2', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), abs0=1), ''), + ('e64 neg on v2', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), neg0=1), ''), + ('e64 neg on s2', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, S(2), neg0=1), ''), + ('e64 neg on inline 3', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, INLINE_INT[3], neg0=1), 'neg/abs'), + ('e64 op_sel src0', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), opsel=(1, 0, 0, 0)), 'op_sel'), + ('e64 op_sel src1', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), opsel=(0, 1, 0, 0)), 'op_sel'), + ('e64 op_sel dst', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2), opsel=(0, 0, 0, 1)), 'op_sel'), + ('SDWA BYTE_1', encode_vop1_sdwa(VOP1_CVT_OFF_F32_I4, 4, 2, src0_sel=1), 'SDWA form of v_cvt_off_f32_i4'), + ('SDWA DWORD clamp', encode_vop1_sdwa(VOP1_CVT_OFF_F32_I4, 4, 2, src0_sel=6, clamp=1), + 'SDWA form of v_cvt_off_f32_i4'), + ('SDWA neg', encode_vop1_sdwa(VOP1_CVT_OFF_F32_I4, 4, 2, src0_sel=1, neg=1), ''), + ('SDWA abs', encode_vop1_sdwa(VOP1_CVT_OFF_F32_I4, 4, 2, src0_sel=1, abs_=1), ''), + ) + for label, code, msg in byte_refusals: + check(f'refused: {label}', is_refused_bytes(code, msg)) + + # Arity refusals + for asm, expected_err in ( + ('v_cvt_off_f32_i4 v4', 'with 1 operands, expected 2'), + ('v_cvt_off_f32_i4 v4, v2, v3', 'with 3 operands, expected 2'), + ('v_cvt_off_f32_i4_e64 v4', 'with 1 operands, expected 2'), + ('v_cvt_off_f32_i4_e64 v4, v2, 0', 'with 3 operands, expected 2'), + ): + check(f'refused arity: {asm}', is_refused_asm(asm, expected_err)) + + # Unlowered control: temporarily remove 'v_cvt_off_f32_i4' from G.VEC_U_TO_F and assert both e32 and e64 refuse + saved = G.VEC_U_TO_F.pop('v_cvt_off_f32_i4') + try: + for tag, code in ( + ('e32', encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 4, V(2))), + ('e64', encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 4, V(2))), + ): + check(f'unlowered control ({tag})', is_refused_bytes(code, 'v_cvt_off_f32_i4')) + finally: + G.VEC_U_TO_F['v_cvt_off_f32_i4'] = saved + + +def controls(): + check_decode_and_lowering() + check_low16_provenance() + check_uniform_valu() + check_refusals_and_unlowered() + if failures: + print('FAILED:', ', '.join(failures)) + sys.exit(1) + print('startup_gfx2hlsl_cvt_off_f32_i4_check: all controls passed') + + +# ---- Draw fixtures and mutated controls ----------------------------------------------------------------------------- + +# Base pixel body: 4 lanes alternating VOP1 _e32 (lanes 0, 2) and VOP3A _e64 (lanes 1, 3), reading v0..v3 -> v12..v15. +PLAIN_BODY = ( + encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 12, V(0)) + + encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 13, V(1)) + + encode_vop1_e32(VOP1_CVT_OFF_F32_I4, 14, V(2)) + + encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 15, V(3)) +) + +# Output-modifier pixel body: 4 lanes in VOP3A _e64 carrying clamp, mul:2, mul:4, and clamp + div:2. +MOD_SPEC = ( + ('', True), # lane 0: clamp + ('mul:2', False), # lane 1: mul:2 + ('mul:4', True), # lane 2: clamp mul:4 (tests scaling before clamp) + ('div:2', False), # lane 3: div:2 +) +MOD_BODY = ( + encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 12, V(0), clamp=1) + + encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 13, V(1), omod='mul:2') + + encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 14, V(2), omod='mul:4', clamp=1) + + encode_vop1_e64(VOP1_CVT_OFF_F32_I4, 15, V(3), omod='div:2') +) + +# 20 inputs (5 draws x 4 lanes): all 16 nibble values 0..15 plus upper-bit garbage including 0x12345678 and 0xfffffff8. +PLAIN_DRAWS = [ + # Draw 0: exercises sign bit (0x8), upper-bit garbage (0x12345678, 0xfffffff8), and positive + negative nibbles + [(0x12345678, 0, 0), (0xfffffff8, 0, 0), (0x80000007, 0, 0), (0xdeadbeef, 0, 0)], + # Nibbles 0..3 with distinct upper-bit patterns + [(0x00000000, 0, 0), (0x11111111, 0, 0), (0x22222222, 0, 0), (0x33333333, 0, 0)], + # Nibbles 4..7 with distinct upper-bit patterns + [(0x44444444, 0, 0), (0x55555555, 0, 0), (0x66666666, 0, 0), (0x77777777, 0, 0)], + # Nibbles 8..11 with distinct upper-bit patterns + [(0x88888888, 0, 0), (0x99999999, 0, 0), (0xaaaaaaa0 | 0xa, 0, 0), (0xbbbbbbb0 | 0xb, 0, 0)], + # Nibbles 12..15 with distinct upper-bit patterns + [(0xccccccc0 | 0xc, 0, 0), (0xddddddd0 | 0xd, 0, 0), (0xeeeeeee0 | 0xe, 0, 0), (0xffffffff, 0, 0)], +] + +MOD_DRAWS = [ + # Lane 0 (clamp): -0.5 -> 0.0; Lane 1 (mul:2): -0.5 -> -1.0; + # Lane 2 (clamp mul:4): 5 (0.3125 * 4 = 1.25 -> 1.0); Lane 3 (div:2): 7 -> 0.21875 + [(0xfffffff8, 0, 0), (0x12345678, 0, 0), (0x12345675, 0, 0), (0x80000007, 0, 0)], + # Lane 0 (clamp): +0.4375 -> 0.4375; Lane 1 (mul:2): +0.4375 -> 0.875; + # Lane 2 (clamp mul:4): -1 -> 0.0; Lane 3 (div:2): -8 -> -0.25 + [(0x00000007, 0, 0), (0xabcdef07, 0, 0), (0xffffffff, 0, 0), (0x00000008, 0, 0)], +] + + +def mutate_plain_unsigned(text): + mutated, count = re.subn(r'int\((v\d+) << 28\) >> 28', r'(\1 & 15u)', text) + assert count == 4 and mutated != text, count + return mutated + + +def mutate_plain_5bit(text): + mutated, count = re.subn(r'int\((v\d+) << 28\) >> 28', r'int(\1 << 27) >> 27', text) + assert count == 4 and mutated != text, count + return mutated + + +def mutate_mod_early_clamp(text): + # Swap saturate((...) * 4.0) into (saturate(...)) * 4.0 on lane 2 (v14) + pattern = r'saturate\(\(\(float\(int\(v2 << 28\) >> 28\) \* 0\.0625\)\) \* 4\.0\)' + replacement = r'(saturate((float(int(v2 << 28) >> 28) * 0.0625))) * 4.0' + mutated, count = re.subn(pattern, replacement, text) + assert count == 1 and mutated != text, (count, text) + return mutated + + +def check_draw(dxc, renderer, adapter, fxc=None): + controls() + assert {x[0] & 0xf for chunk in PLAIN_DRAWS for x in chunk} == set(range(16)) + + plain_ps_words = PRE_WORDS + PLAIN_BODY + POST_WORDS + mod_ps_words = PRE_WORDS + MOD_BODY + POST_WORDS + plain_ps_hlsl = G.Pipeline(vs_words([(0, 0, 0)] * 4), plain_ps_words, root_blob()).hlsl_ps() + mod_ps_hlsl = G.Pipeline(vs_words([(0, 0, 0)] * 4), mod_ps_words, root_blob()).hlsl_ps() + + compilers = ([('fxc', fxc, 'vs_5_1', 'ps_5_1')] if fxc else []) + [('dxc', dxc, 'vs_6_0', 'ps_6_0')] + draws = mutations = 0 + with tempfile.TemporaryDirectory() as tmp: + directory = Path(tmp) + for kind, compiler, vs_profile, ps_profile in compilers: + plain_cso = compile_hlsl(compiler, kind, plain_ps_hlsl, f'ps_plain_{kind}', ps_profile, directory) + for idx, chunk in enumerate(PLAIN_DRAWS): + vs_hlsl = G.Pipeline(vs_words(chunk), plain_ps_words, root_blob()).hlsl_vs() + vs_cso = compile_hlsl(compiler, kind, vs_hlsl, f'vs_plain_{idx}_{kind}', vs_profile, directory) + want = words_text([isa_cvt_off_f32_i4(x[0]) for x in chunk]) + run = draw(renderer, adapter, vs_cso, plain_cso, want) + if run.returncode: + raise AssertionError(f'{kind} plain draw {idx} {chunk}\n{run.stdout}\n{run.stderr}') + draws += 1 + print(f'{kind} plain[{idx}] {[hex(x[0]) for x in chunk]}: {want}', flush=True) + + if idx == 0: + for mname, mut_fn, ref_fn in ( + ('unsigned_nibble', mutate_plain_unsigned, wrong_unsigned_nibble), + ('5bit_signext', mutate_plain_5bit, wrong_5bit_nibble), + ): + wrong = words_text([ref_fn(x[0]) for x in chunk]) + assert wrong != want + bad_cso = compile_hlsl(compiler, kind, mut_fn(plain_ps_hlsl), + f'ps_mut_{mname}_{kind}', ps_profile, directory) + mrun = draw(renderer, adapter, vs_cso, bad_cso, want) + if mrun.returncode == 0 or f'actual={wrong}' not in mrun.stderr: + raise AssertionError( + f'{kind} mutation {mname} did not read {wrong}\n{mrun.stdout}\n{mrun.stderr}') + mutations += 1 + print(f'{kind} mutation {mname}: fails as expected, reads {wrong}', flush=True) + + mod_cso = compile_hlsl(compiler, kind, mod_ps_hlsl, f'ps_mod_{kind}', ps_profile, directory) + for idx, chunk in enumerate(MOD_DRAWS): + vs_hlsl = G.Pipeline(vs_words(chunk), mod_ps_words, root_blob()).hlsl_vs() + vs_cso = compile_hlsl(compiler, kind, vs_hlsl, f'vs_mod_{idx}_{kind}', vs_profile, directory) + want = words_text([ + isa_cvt_off_f32_i4(x[0], omod=omod, clamp=clamp) + for x, (omod, clamp) in zip(chunk, MOD_SPEC) + ]) + run = draw(renderer, adapter, vs_cso, mod_cso, want) + if run.returncode: + raise AssertionError(f'{kind} mod draw {idx} {chunk}\n{run.stdout}\n{run.stderr}') + draws += 1 + print(f'{kind} mod[{idx}] {[hex(x[0]) for x in chunk]}: {want}', flush=True) + + if idx == 0: + # Early clamp on lane 2: saturate(0.3125) * 4.0 = 1.25 (0x3fa00000) instead of 1.0 (0x3f800000) + wrong_words = [ + isa_cvt_off_f32_i4(x[0], omod=omod, clamp=clamp) + for x, (omod, clamp) in zip(chunk, MOD_SPEC) + ] + wrong_words[2] = f32_bits(bits_to_f32(isa_cvt_off_f32_i4(chunk[2][0], clamp=True)) * 4.0) + wrong = words_text(wrong_words) + assert wrong != want + bad_cso = compile_hlsl(compiler, kind, mutate_mod_early_clamp(mod_ps_hlsl), + f'ps_mut_early_clamp_{kind}', ps_profile, directory) + mrun = draw(renderer, adapter, vs_cso, bad_cso, want) + if mrun.returncode == 0 or f'actual={wrong}' not in mrun.stderr: + raise AssertionError( + f'{kind} mutation early_clamp did not read {wrong}\n{mrun.stdout}\n{mrun.stderr}') + mutations += 1 + print(f'{kind} mutation early_clamp: fails as expected, reads {wrong}', flush=True) + + print(f'{adapter}: {draws} draws matched, {mutations} mutated draws read their mutated words') + + +def main(): + if len(sys.argv) == 1: + controls() + return + if sys.argv[1] == 'draw' and len(sys.argv) in (5, 6): + check_draw(*sys.argv[2:]) + return + raise SystemExit(__doc__) + + +if __name__ == '__main__': + main() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 34f5a14a3..06e5f36d0 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -373,6 +373,14 @@ def check_encoding(code, off, n, name, asm): word0, word1 = struct.unpack_from('<2I', code, off) if word0 & 0x700 or word1 & 0xe0000000: raise Unsupported('%s with neg/abs' % name) + if name == 'v_cvt_off_f32_i4_e64': + # Integer-nibble conversion: the instruction has one source, so only src0's VOP3A ABS (w0[8]) and NEG + # (w1[29]) can carry a modifier. Masking all three bit positions of each field (w0[10:8], w1[31:29]) + # also refuses the unused src1/src2 bits, which keeps an inline constant from masquerading as a + # negative integer and keeps a hidden modifier out of the printed operand. + word0, word1 = struct.unpack_from('<2I', code, off) + if word0 & 0x700 or word1 & 0xe0000000: + raise Unsupported('%s with neg/abs: not a 4-bit integer source modifier' % name) if name == 'v_cndmask_b32_e64': # The printed -1 is ambiguous: it can be plain inline -1 or inline 1 with NEG. word0, word1 = struct.unpack_from('<2I', code, off) @@ -678,8 +686,8 @@ def smem_mods(mods): # Multiply, fused multiply-add (including the literal form), reciprocal and median of three use # only local arithmetic. Move copies its input bits. None reads a lane ID or another lane. UNIFORM_VALU = frozenset(( - 'v_cvt_u32_f32', 'v_cvt_i32_f32', 'v_cvt_f32_u32', 'v_cvt_f32_ubyte0', 'v_mul_f32', - 'v_fma_f32', 'v_fmaak_f32', 'v_rcp_f32', 'v_med3_f32', 'v_mov_b32', + 'v_cvt_u32_f32', 'v_cvt_i32_f32', 'v_cvt_f32_u32', 'v_cvt_f32_ubyte0', 'v_cvt_off_f32_i4', + 'v_mul_f32', 'v_fma_f32', 'v_fmaak_f32', 'v_rcp_f32', 'v_med3_f32', 'v_mov_b32', )) @@ -2567,7 +2575,7 @@ def lower(self, base, asm, ops, mods): if any(m not in ('clamp', 'mul:2', 'mul:4', 'div:2') for m in mods): raise Unsupported('%s with %s' % (base, mods)) dst = reg_range(ops[0])[1] - low16 = base in ('v_cvt_f32_f16', 'v_cvt_f32_ubyte0', 'v_cvt_f32_ubyte1') + low16 = base in ('v_cvt_f32_f16', 'v_cvt_f32_ubyte0', 'v_cvt_f32_ubyte1', 'v_cvt_off_f32_i4') self.set_vf(dst, text_f.format(*[self.u(o, low16_only=low16) for o in ops[1:1 + arity]]), mods) if (base.startswith('v_cvt_f32_ubyte') and not mods and self.exec_known and not getattr(self, 'has_branches', True)): @@ -2722,6 +2730,8 @@ def dpp(self, base, ops, mods): # VEC_U_TO_F conversions apply output modifiers before dst_sel, VEC_F forms stay refused, and # dst_unused UNUSED_SEXT has no lowering. def sdwa(self, base, ops, mods): + if base == 'v_cvt_off_f32_i4': + raise Unsupported('SDWA form of %s' % base) fields = {} out_mods = [] float_result = base in VEC_U_TO_F @@ -5409,6 +5419,7 @@ def declarations(self): 'v_cvt_f32_ubyte0': (1, 'float({0} & 255)'), 'v_cvt_f32_ubyte1': (1, 'float(({0} >> 8) & 255)'), 'v_cvt_f32_ubyte2': (1, 'float(({0} >> 16) & 255)'), 'v_cvt_f32_ubyte3': (1, 'float({0} >> 24)'), 'v_cvt_f32_f16': (1, 'f16tof32({0})'), + 'v_cvt_off_f32_i4': (1, '(float(int({0} << 28) >> 28) * 0.0625)'), } # operands are uints; the result is a uint expression VEC_U = { From f6b86579bd44d26093ef46a0dccbf0af74b14a2d Mon Sep 17 00:00:00 2001 From: triggered02 <153171514+triggered02@users.noreply.github.com> Date: Thu, 26 Nov 2026 01:09:31 +0530 Subject: [PATCH 2/2] tests: pin the DPP16 quad-broadcast acceptance for v_cvt_off_f32_i4 The generic Stage.dpp_broadcast path is op-agnostic: it proves whole-quad activity, reads the lane quad_perm names with QuadReadLaneAt, and then lowers the base operation on that value. v_cvt_off_f32_i4 is now in VEC_U_TO_F, so it takes that path like any other unary VOP1; the merge base refused every form of the op, which is why the DPP form was refused there too. Pin the acceptance (lane 2 and lane 3, the shader-model bump, and the nibble sign-extend on the broadcast value) plus the refusals that keep it sound: a non-broadcast and a mixed quad_perm, a partial row_mask, bound_ctrl:0, an entry mask that is not proven whole-quad, and a vertex stage. Refs #2452 --- .../startup_gfx2hlsl_cvt_off_f32_i4_check.py | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py b/tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py index 61c7a648a..e8a8a2907 100644 --- a/tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py +++ b/tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py @@ -509,6 +509,90 @@ def check_uniform_valu(): check('UNIFORM_VALU refuses v_readfirstlane_b32 when exec is unknown', ok, detail) +def check_dpp_broadcast(): + """The generic DPP16 `quad_perm:[k,k,k,k]` broadcast path accepts this VOP1 op, and only where it is sound. + + The merge base refused every `v_cvt_off_f32_i4` form because the op was not lowered at all, so it also + refused the DPP form. The op is now in `VEC_U_TO_F`, and `Stage.dpp_broadcast` is op-agnostic: it proves + whole-quad activity, reads the named lane of the quad with `QuadReadLaneAt`, and then lowers the base + operation on that value (for a unary op the broadcast happens before the conversion, which is the ISA + order). Accepting the broadcast is therefore intended rather than incidental, and the rows below pin the + acceptance, the lane it reads, the shader-model bump, and the four refusals that keep the rest out. + """ + def probe(**state): + stage = G.Stage.__new__(G.Stage) + stage.kind = 'ps' + stage.lines, stage.sym = [], {} + stage.used_s, stage.used_v, stage.uniform_v = set(), set(), set() + stage.varying_s = set() + stage.scc_uniform = True + stage.uniform_expr = {} + stage.past_entry = False + stage.has_branches = False + stage.exec_known = True + stage.masks = set() + stage.exports = {} + stage.low16_only_v = set() + stage.pinned_low16_v = set() + stage.whole_read_v = set() + stage.byte_float_v = set() + stage.bad_sgprs = set() + stage.defined_uniform_s = None + stage.since_join = None + stage.wqm_exec = True + stage.needs_sm6 = False + stage.pipe = type('Pipe', (), {'resources': {}, 'notes': set(), 'modified': set(), + 'sampler_heap': False})() + for key, value in state.items(): + setattr(stage, key, value) + return stage + + def translate(asm, **state): + stage = probe(**state) + try: + stage.one(asm.split(' ', 1)[0], asm) + except G.Unsupported as error: + return None, str(error) + return stage, None + + accepted = 'v_cvt_off_f32_i4_dpp v2, v1 quad_perm:[2,2,2,2] row_mask:0xf bank_mask:0xf' + stage, reason = translate(accepted) + check('dpp_broadcast accepts quad_perm:[2,2,2,2] for v_cvt_off_f32_i4', + stage is not None, str(reason)) + check('dpp_broadcast reads lane 2 of the quad with QuadReadLaneAt', + stage is not None and any('QuadReadLaneAt(v1, 2u)' in line for line in stage.lines), + str(stage and stage.lines)) + check('dpp_broadcast lowers the conversion on the broadcast value (nibble sign-extend x 1/16)', + stage is not None and any('int(v256 << 28) >> 28' in line and '0.0625' in line + for line in stage.lines), + str(stage and stage.lines)) + check('dpp_broadcast requires shader model 6 for QuadReadLaneAt', + stage is not None and stage.needs_sm6 is True) + # The same broadcast at a different lane, to prove the lane is not hard-coded. + lane3, reason = translate('v_cvt_off_f32_i4_dpp v2, v1 quad_perm:[3,3,3,3] row_mask:0xf bank_mask:0xf') + check('dpp_broadcast reads the lane quad_perm names, not a fixed one', + lane3 is not None and any('QuadReadLaneAt(v1, 3u)' in line for line in lane3.lines), + str(reason) or str(lane3 and lane3.lines)) + for label, asm, state, message in ( + ('a non-broadcast quad_perm', 'v_cvt_off_f32_i4_dpp v2, v1 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf', + {}, ''), + ('a mixed quad_perm', 'v_cvt_off_f32_i4_dpp v2, v1 quad_perm:[2,2,2,3] row_mask:0xf bank_mask:0xf', + {}, 'only the swaps'), + ('a partial row_mask', 'v_cvt_off_f32_i4_dpp v2, v1 quad_perm:[2,2,2,2] row_mask:0x7 bank_mask:0xf', + {}, 'partial row_mask'), + ('bound_ctrl:0', 'v_cvt_off_f32_i4_dpp v2, v1 quad_perm:[2,2,2,2] row_mask:0xf bank_mask:0xf bound_ctrl:0', + {}, 'bound_ctrl'), + ('an entry mask not proven whole-quad', + 'v_cvt_off_f32_i4_dpp v2, v1 quad_perm:[2,2,2,2] row_mask:0xf bank_mask:0xf', {'wqm_exec': False}, + 'whole-quad'), + ('a vertex stage', 'v_cvt_off_f32_i4_dpp v2, v1 quad_perm:[2,2,2,2] row_mask:0xf bank_mask:0xf', + {'kind': 'vs'}, 'straight-line pixel stage'), + ): + refused, detail = translate(asm, **state) + check('dpp_broadcast refuses %s' % label, + refused is None and message in (detail or ''), str(detail)) + + def check_refusals_and_unlowered(): # Refusals: abs, neg, op_sel, SDWA (all decoded from synthetic gfx1013 bytes via G.decode()) byte_refusals = ( @@ -553,6 +637,7 @@ def controls(): check_decode_and_lowering() check_low16_provenance() check_uniform_valu() + check_dpp_broadcast() check_refusals_and_unlowered() if failures: print('FAILED:', ', '.join(failures))