From 2d67fe41930379043235b98b2b6e0799ebf05f27 Mon Sep 17 00:00:00 2001 From: Ybucaille <83926195+Ybucaille@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:15:33 +0100 Subject: [PATCH 1/4] Lower v_cvt_pk_u8_f32 in the startup gfx2hlsl translator VOP3A opcode 350 of the RDNA 1.0 and RDNA 2 ISA guides: D = (S2 & ~(0xff << 8 * S1[1:0])) | ((flt32_to_uint8(S0) & 0xff) << 8 * S1[1:0]) The guides leave flt32_to_uint8 undefined (rounding, bounds, NaN and negative results are not stated), so the lowering holds only where every reading agrees: S0 is +0.0 or an integer in [1, 255]. A constant S0 outside that set is refused, as are modifiers, a wrong operand count and a non-VGPR destination. A register S0 cannot be checked statically; the HLSL clamp only keeps the conversion defined. This is the set that tools/compute_store_aot.py already lowers (#1546). S1 may be a register or a constant; only its bits [1:0] are used. The fixture is source-built and draws all four byte selectors with S1 bits above [1:0] set, S2 words of four distinct bytes, and S0 from 0.0 to 255.0. Its negative controls (a lowering that loses S2, a wrong byte position) are detected, and the check fails when the shift in the translator is mutated. The D3D12 draw was not run here (Linux): the controls and the llvm-mc reassembly were. Refs #2023 (item K) AI-assisted. --- .github/workflows/windows.yml | 2 + CMakeLists.txt | 10 ++ tests/startup_gfx2hlsl_cvt_pk_u8_check.py | 110 ++++++++++++++++++++++ tools/startup/gfx2hlsl.py | 25 +++++ 4 files changed, 147 insertions(+) create mode 100644 tests/startup_gfx2hlsl_cvt_pk_u8_check.py diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 15d72931ed..e9c1d1841b 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -75,6 +75,7 @@ jobs: (["tests/startup_sampler_heap_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_scalar_m0_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_gfx2hlsl_cvt_alu_check.py", "binary", "llvm-mc-15"], {}), + (["tests/startup_gfx2hlsl_cvt_pk_u8_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_image_sample_l_check.py", "binary", "llvm-mc-15"], {}), (["tests/startup_image_gather_lz_check.py", "--llvm-mc", "llvm-mc-15"], {}), (["tests/startup_image_sample_c_check.py", "binary", "llvm-mc-15"], {}), @@ -202,6 +203,7 @@ jobs: tests/startup_host_cpu_check.py \ tests/startup_live_telemetry_check.py \ tests/startup_gfx2hlsl_cvt_alu_check.py \ + tests/startup_gfx2hlsl_cvt_pk_u8_check.py \ tests/startup_gfx2hlsl_int_med3_check.py \ tests/startup_gfx2hlsl_capture_check.py \ tests/startup_translator_check.py \ diff --git a/CMakeLists.txt b/CMakeLists.txt index 6758bca27a..93bab2f4e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2241,6 +2241,16 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter} $<$:${STARTUP_FXC}>) set_tests_properties(startup_gfx2hlsl_cvt_alu_${adapter} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77) endforeach() + # gfx2hlsl v_cvt_pk_u8_f32 (#2023 item K): translation controls, then a draw of the translated pairs with + # reference words and a mutated lowering that loses S2. + add_test(NAME startup_gfx2hlsl_cvt_pk_u8_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_cvt_pk_u8_check.py") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_gfx2hlsl_cvt_pk_u8_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_cvt_pk_u8_check.py" draw "${DXC_EXECUTABLE}" + "$" ${adapter} $<$:${STARTUP_FXC}>) + set_tests_properties(startup_gfx2hlsl_cvt_pk_u8_${adapter} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77) + endforeach() add_test(NAME startup_sampler_heap_controls COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_sampler_heap_check.py") add_test(NAME startup_vertex_fetch_formats_controls COMMAND "${INTAKE_PYTHON}" -B diff --git a/tests/startup_gfx2hlsl_cvt_pk_u8_check.py b/tests/startup_gfx2hlsl_cvt_pk_u8_check.py new file mode 100644 index 0000000000..001f9133e4 --- /dev/null +++ b/tests/startup_gfx2hlsl_cvt_pk_u8_check.py @@ -0,0 +1,110 @@ +"""v_cvt_pk_u8_f32 of tools/startup/gfx2hlsl.py (item K of #2023). + + (no arguments) translation controls, pure Python + binary reassemble the pixel bodies and compare them with the recorded words + draw [fxc] + draw one pixel per four input sets and compare the four result words + +The RDNA 1.0 and RDNA 2 ISA guides give (VOP3A opcode 350) + D = (S2 & ~(0xff << 8 * S1[1:0])) | ((flt32_to_uint8(S0) & 0xff) << 8 * S1[1:0]) +and leave flt32_to_uint8 undefined: rounding, bounds, NaN and negative results are not stated. The lowering and this +fixture therefore cover only an S0 that is +0.0 or an integer in [1, 255], where truncation, any rounding mode and any +clamp give the same byte (the form tools/compute_store_aot.py already lowers, #1546). Fractional, negative, above 255 +and NaN values are not exercised, and a constant S0 outside that set is refused. S1 keeps its bits above [1:0] in every +draw set, and every S2 holds four distinct bytes, so a wrong byte or an overwritten S2 changes the word. +""" +import re +import struct +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from startup_gfx2hlsl_lane_alu import G, MASK, Probe, check, check_draw, main, refused, words # noqa: E402 + + +def f32(value): + return struct.unpack(' Date: Wed, 25 Nov 2026 02:05:57 +0100 Subject: [PATCH 2/4] Require proven or contracted S0 for v_cvt_pk_u8_f32 Review of #2101: the generic path accepted any register S0 and clamped it, so a computed fraction, NaN or negative value could silently select an invented byte, since the ISA leaves flt32_to_uint8 undefined there. A register S0 is now accepted only when proven +0.0 or an integer in 1..255: written by v_cvt_f32_ubyte0..3 (no modifier, straight-line code, entry exec mask) with no write since. Any other register is refused, unless the caller selects the Pipeline keyword pk_u8_s0_contract, which the fixture draws do; use of the contract is recorded in the notes. --- docs/experiments.md | 2 +- tests/startup_gfx2hlsl_cvt_pk_u8_check.py | 82 ++++++++++++++++++++--- tests/startup_gfx2hlsl_lane_alu.py | 7 +- tools/startup/gfx2hlsl.py | 32 +++++++-- 4 files changed, 107 insertions(+), 16 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 40fce45519..b262fbc79b 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -5147,7 +5147,7 @@ The source-built gfx1013 fixture `VADD_NC_I32` in `tests/compute_store_check.py` #### Guarded float-to-byte insert -`v_cvt_pk_u8_f32 vD, s0, s1, s2` (VOP3A opcode 350, #1546) lowers to an inline expression on known words. The RDNA 1.0 and RDNA 2 ISA guides give `D = (S2 & ~(0xff << 8 * S1[1:0])) | ((flt32_to_uint8(S0) & 0xff) << 8 * S1[1:0])`. The byte select and the merge are fully specified. `flt32_to_uint8` is not defined: the guides give no rounding mode, no bounds and no result for NaN or negative inputs, and AMD's machine-readable ISA description only says that the float becomes an unsigned 8-bit integer. The translation is claimed only where every reading agrees: an S0 that is +0.0 or an integer in [1, 255]. Truncation, every rounding mode and any clamp give the same byte there. The HLSL is `(S2 & ~(0xffu << ((S1 & 3u) << 3u))) | ((uint(asfloat(S0)) & 0xffu) << ((S1 & 3u) << 3u))`. +`v_cvt_pk_u8_f32 vD, s0, s1, s2` (VOP3A opcode 350, #1546) lowers to an inline expression on known words. The RDNA 1.0 and RDNA 2 ISA guides give `D = (S2 & ~(0xff << 8 * S1[1:0])) | ((flt32_to_uint8(S0) & 0xff) << 8 * S1[1:0])`. The byte select and the merge are fully specified. `flt32_to_uint8` is not defined: the guides give no rounding mode, no bounds and no result for NaN or negative inputs, and AMD's machine-readable ISA description only says that the float becomes an unsigned 8-bit integer. The translation is claimed only where every reading agrees: an S0 that is +0.0 or an integer in [1, 255]. Truncation, every rounding mode and any clamp give the same byte there. A constant S0 outside that set is refused. A register S0 is accepted by ordinary translation only when it is proven in the set: written by `v_cvt_f32_ubyte0..3` without modifier, in straight-line code under the entry exec mask, with no write since. Any other register is refused (`source vN is not proven to be +0.0 or an integer in 1..255`) unless the caller selects the `pk_u8_s0_contract` keyword of `Pipeline`, which states that every such S0 holds +0.0 or an integer in 1..255 and is recorded in the pipeline notes. The fixture draws select it, because their S0 words come from flat parameters; the request path never does. The HLSL is `(S2 & ~(0xffu << ((S1 & 3u) << 3u))) | ((uint(asfloat(S0)) & 0xffu) << ((S1 & 3u) << 3u))`. S0 is a VGPR, an SGPR that holds a known word, a literal, the integer zero or an inline float constant. S1 and S2 are any known word. A constant S0 outside the agreed set is refused with `Unverified ALU operand`: `0.5`, `1/(2*pi)`, 1.5, negative values, -0.0, 256.0, infinity, NaN, a nonzero inline integer (a denormal bit pattern) and a word wider than 32 bits. A word read from memory is not known statically, so a caller that binds one must keep it inside the set. The input modifiers `-`, `|...|` and `-|...|`, `clamp`, `mul:2`, the `_e64` spelling and a register range are not lowered; each stops at the instruction. Unknown, pending, descriptor and saved-exec sources and a destination past v255 are refused, as for the integer subset. A result is data only and is refused as an address or a lane-guard operand. diff --git a/tests/startup_gfx2hlsl_cvt_pk_u8_check.py b/tests/startup_gfx2hlsl_cvt_pk_u8_check.py index 001f9133e4..3a4de3ba17 100644 --- a/tests/startup_gfx2hlsl_cvt_pk_u8_check.py +++ b/tests/startup_gfx2hlsl_cvt_pk_u8_check.py @@ -10,7 +10,9 @@ and leave flt32_to_uint8 undefined: rounding, bounds, NaN and negative results are not stated. The lowering and this fixture therefore cover only an S0 that is +0.0 or an integer in [1, 255], where truncation, any rounding mode and any clamp give the same byte (the form tools/compute_store_aot.py already lowers, #1546). Fractional, negative, above 255 -and NaN values are not exercised, and a constant S0 outside that set is refused. S1 keeps its bits above [1:0] in every +and NaN values are not exercised, and a constant S0 outside that set is refused. A register S0 is accepted in ordinary +translation only when it was written by v_cvt_f32_ubyte0..3 with no write since; any other register is refused unless +the Pipeline selects pk_u8_s0_contract, which the fixture draws do (their S0 words come from flat parameters). S1 keeps its bits above [1:0] in every draw set, and every S2 holds four distinct bytes, so a wrong byte or an overwritten S2 changes the word. """ import re @@ -43,12 +45,12 @@ def dropped_src2(s0, s1, s2): (f32(200.0), 0xabcdef02, 0x89abcdef), (f32(16.0), 0x7ffffffb, 0xc0ffee11), ] FAMILY = { + 'pipeline': {'pk_u8_s0_contract': True}, 'ops': OPS, 'draw': {'v_cvt_pk_u8_f32': DRAW_SETS}, # A lowering that loses S2: the reference of the mutation keeps only the inserted byte. 'mutations': {'v_cvt_pk_u8_f32': (r'\((v\d+) & ~\(0xffu << \(\(v\d+ & 3u\) << 3u\)\)\)', '0u', dropped_src2)}, 'body': {'v_cvt_pk_u8_f32': words('0c005ed500092204' '0d005ed5010b2604' '0e005ed5020d2a04' '0f005ed5030f2e04')}, - 'accepted': ['v_cvt_pk_u8_f32 v1, v2, v3, v4', 'v_cvt_pk_u8_f32_e64 v1, v2, 3, s4', - 'v_cvt_pk_u8_f32 v1, 1.0, v3, v4', 'v_cvt_pk_u8_f32 v1, 0, 2, 0xa1b2c3d4', + 'accepted': ['v_cvt_pk_u8_f32 v1, 1.0, v3, v4', 'v_cvt_pk_u8_f32 v1, 0, 2, 0xa1b2c3d4', 'v_cvt_pk_u8_f32 v1, 0x437f0000, v3, v4'], 'refused': ['v_cvt_pk_u8_f32 v1, v2, v3', 'v_cvt_pk_u8_f32 v1, v2, v3, v4, v5', 'v_cvt_pk_u8_f32 v1, v2, v3, v4 clamp', 'v_cvt_pk_u8_f32 v1, v2, v3, v4 mul:2', @@ -56,14 +58,40 @@ def dropped_src2(s0, s1, s2): 'v_cvt_pk_u8_f32 v1, 0.5, v3, v4', 'v_cvt_pk_u8_f32 v1, -1.0, v3, v4', 'v_cvt_pk_u8_f32 v1, 0x43800000, v3, v4', 'v_cvt_pk_u8_f32 v1, 0x7fc00000, v3, v4', 'v_cvt_pk_u8_f32 v1, 0x3fc00000, v3, v4', 'v_cvt_pk_u8_f32 v1, 0x80000000, v3, v4', - 'v_cvt_pk_u8_f32 s1, v2, v3, v4', 'v_cvt_pk_u8_f32 v[1:2], v2, v3, v4'], + 'v_cvt_pk_u8_f32 s1, 1.0, v3, v4', 'v_cvt_pk_u8_f32 v[1:2], v2, v3, v4'], } -def emitted(asm): +class Pipe: + """The two Pipeline attributes the op reads: the selected contract and the notes.""" + + def __init__(self, contract): + self.pk_u8_s0_contract, self.notes = contract, set() + + +def make_probe(contract=False): probe = Probe() - probe.one(asm.split(' ', 1)[0], asm) - return probe.lines[-1] + probe.pipe, probe.has_branches = Pipe(contract), False + return probe + + +def run(lines, contract=False): + probe = make_probe(contract) + for asm in lines: + probe.one(asm.split(' ', 1)[0], asm) + return probe + + +def emitted(asm, contract=True): + return run([asm], contract).lines[-1] + + +def refuses(lines, contract=False): + try: + run(lines, contract) + except G.Unsupported as error: + return str(error) + return None def evaluate(line, regs): @@ -98,7 +126,45 @@ def selector_controls(): def run_controls(): selector_controls() check('constant inside the set is lowered', 'F(0x3f800000u)' in emitted('v_cvt_pk_u8_f32 v1, 1.0, v3, v4')) - check('S1 and S2 constants are lowered', not refused('v_cvt_pk_u8_f32 v1, v2, 7, 0xffffffff')) + check('S1 and S2 constants are lowered', not refused('v_cvt_pk_u8_f32 v1, 1.0, 7, 0xffffffff')) + contract_controls() + + +def contract_controls(): + pk = 'v_cvt_pk_u8_f32 v1, v2, v3, v4' + # An unproven register S0 is refused in ordinary translation, with a message naming the register. + for asm in (pk, 'v_cvt_pk_u8_f32_e64 v1, v2, 3, s4', 'v_cvt_pk_u8_f32 v1, s2, v3, v4'): + check('unproven S0 refused without the contract: ' + asm, refuses([asm]) is not None) + check('refusal message', refuses([pk]) == 'v_cvt_pk_u8_f32 source v2 is not proven to be +0.0 or an integer in 1..255') + # The same words translate when the contract is selected, and the note records it. + probe = run([pk, 'v_cvt_pk_u8_f32_e64 v1, v2, 3, s4'], True) + check('unproven S0 accepted with the contract', len(probe.lines) == 2 and 'clamp(F(v2)' in probe.lines[0]) + check('the contract is recorded in the notes', any('pk_u8_s0_contract' in n for n in probe.pipe.notes)) + check('no note without a contract use', not run(['v_cvt_pk_u8_f32 v1, 1.0, v3, v4'], True).pipe.notes) + # Proven forms, each of the four bytes, need no contract. + for n in range(4): + cvt = 'v_cvt_f32_ubyte%d v2, v5' % n + check('ubyte%d result is proven' % n, refuses([cvt, pk]) is None) + check('ubyte%d result by the e64 spelling is proven' % n, refuses(['v_cvt_f32_ubyte%d_e64 v2, v5' % n, pk]) is None) + # Anything that writes the register afterwards, or a different register, is unproven again. + for label, lines in (('a float multiply', ['v_cvt_f32_ubyte0 v2, v5', 'v_mul_f32 v2, v2, v6', pk]), + ('a computed register', ['v_mul_f32 v2, v5, v6', pk]), + ('a move', ['v_cvt_f32_ubyte0 v2, v5', 'v_mov_b32 v2, v7', pk]), + ('another register', ['v_cvt_f32_ubyte0 v8, v5', pk]), + ('a saturating conversion', ['v_cvt_f32_ubyte0 v2, v5 clamp', pk]), + ('a scaled conversion', ['v_cvt_f32_ubyte0 v2, v5 mul:2', pk]), + ('an integer conversion', ['v_cvt_f32_u32 v2, v5', pk])): + check('%s is refused without the contract' % label, refuses(lines) is not None) + check('%s is accepted with the contract' % label, refuses(lines, True) is None) + # The proof needs straight-line code and the entry exec mask. + probe = make_probe() + probe.has_branches = True + probe.one('v_cvt_f32_ubyte0', 'v_cvt_f32_ubyte0 v2, v5') + check('no proof in a program with branches', 2 not in getattr(probe, 'byte_float_v', ())) + probe = make_probe() + probe.exec_known = False + probe.one('v_cvt_f32_ubyte0', 'v_cvt_f32_ubyte0 v2, v5') + check('no proof under a partial exec mask', 2 not in getattr(probe, 'byte_float_v', ())) if __name__ == '__main__': diff --git a/tests/startup_gfx2hlsl_lane_alu.py b/tests/startup_gfx2hlsl_lane_alu.py index ce076fe93a..4ff732d480 100644 --- a/tests/startup_gfx2hlsl_lane_alu.py +++ b/tests/startup_gfx2hlsl_lane_alu.py @@ -18,6 +18,7 @@ body: mnemonic -> the recorded pixel body words (llvm-mc -mcpu=gfx1013) refused: assembly lines that must stay refused accepted: assembly lines that must translate (one instruction, no pipeline) + pipeline: optional keyword arguments of the Pipeline built for the fixture (a selected contract) Every reference is written from the ISA description with plain integer arithmetic, not from the lowering. """ import re @@ -46,7 +47,8 @@ def body_source(family, op): def translate(family, op, mutate=False): - pipe = G.Pipeline(vs_words([(0, 0, 0)] * 4), PRE_WORDS + family['body'][op] + POST_WORDS, root_blob()) + pipe = G.Pipeline(vs_words([(0, 0, 0)] * 4), PRE_WORDS + family['body'][op] + POST_WORDS, root_blob(), + **family.get('pipeline', {})) text = pipe.hlsl_ps() if mutate: pattern, replacement, _ = family['mutations'][op] @@ -121,7 +123,8 @@ def check_draw(family, dxc, renderer, adapter, fxc=None): for index in range(0, len(sets), 4): chunk = sets[index:index + 4] vs = compile_hlsl(compiler, kind, G.Pipeline(vs_words(chunk), PRE_WORDS + family['body'][op] - + POST_WORDS, root_blob()).hlsl_vs(), + + POST_WORDS, root_blob(), + **family.get('pipeline', {})).hlsl_vs(), 'vs_%s_%d_%s' % (op, index, kind), vs_profile, directory) want = words_text(expected(family, op, chunk)) run = draw(renderer, adapter, vs, good, want) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 9d36406aca..7c10797338 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -456,6 +456,7 @@ def emit(self, text): self.lines.append(text) def set_v(self, n, expr): + getattr(self, 'byte_float_v', set()).discard(n) if hasattr(self, 'uniform_v'): self.uniform_v.discard(n) self.used_v.add(n) @@ -694,8 +695,13 @@ def lower(self, base, asm, ops, mods): # accepted here, because ignoring them would silently change the operand. if any(m not in ('clamp', 'mul:2', 'mul:4', 'div:2') for m in mods): raise Unsupported('%s with %s' % (base, mods)) - return self.set_vf(reg_range(ops[0])[1], - text_f.format(*[self.u(o) for o in ops[1:1 + arity]]), mods) + dst = reg_range(ops[0])[1] + self.set_vf(dst, text_f.format(*[self.u(o) 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)): + # The whole register now holds float(0..255), exactly +0.0 or an integer in 1..255, until the next write. + self.__dict__.setdefault('byte_float_v', set()).add(dst) + return None if base in VEC_U: arity, text = VEC_U[base] if mods: @@ -1446,8 +1452,11 @@ def op_v_cvt_pk_u8_f32(self, ops, mods): # D = (S2 & ~(0xff << 8 * S1[1:0])) | ((flt32_to_uint8(S0) & 0xff) << 8 * S1[1:0]) # The guides leave flt32_to_uint8 undefined (rounding, bounds, NaN and negative results are not # stated), so S0 must be +0.0 or an integer in [1, 255], where every reading gives the same byte. - # A constant S0 outside that set is refused; a register is not known statically, so the program - # must keep it inside the set. The clamp only keeps the HLSL conversion defined. + # A constant S0 outside that set is refused. A register is accepted only when proven: it was written + # by v_cvt_f32_ubyte0..3 (no modifier, straight-line code, full exec) with no write since. Any other + # register is refused unless the caller selects the pk_u8_s0_contract of the Pipeline, which states + # that every S0 register holds +0.0 or an integer in 1..255. The clamp only keeps the HLSL + # conversion defined. if mods: raise Unsupported('v_cvt_pk_u8_f32 with modifiers') if len(ops) != 4: @@ -1461,6 +1470,16 @@ def op_v_cvt_pk_u8_f32(self, ops, mods): value = struct.unpack(' Date: Wed, 25 Nov 2026 02:45:52 +0100 Subject: [PATCH 3/4] Split the proven-register rule of v_cvt_pk_u8_f32 into its own paragraph --- docs/experiments.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/experiments.md b/docs/experiments.md index b262fbc79b..bda7789ae4 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -5147,7 +5147,9 @@ The source-built gfx1013 fixture `VADD_NC_I32` in `tests/compute_store_check.py` #### Guarded float-to-byte insert -`v_cvt_pk_u8_f32 vD, s0, s1, s2` (VOP3A opcode 350, #1546) lowers to an inline expression on known words. The RDNA 1.0 and RDNA 2 ISA guides give `D = (S2 & ~(0xff << 8 * S1[1:0])) | ((flt32_to_uint8(S0) & 0xff) << 8 * S1[1:0])`. The byte select and the merge are fully specified. `flt32_to_uint8` is not defined: the guides give no rounding mode, no bounds and no result for NaN or negative inputs, and AMD's machine-readable ISA description only says that the float becomes an unsigned 8-bit integer. The translation is claimed only where every reading agrees: an S0 that is +0.0 or an integer in [1, 255]. Truncation, every rounding mode and any clamp give the same byte there. A constant S0 outside that set is refused. A register S0 is accepted by ordinary translation only when it is proven in the set: written by `v_cvt_f32_ubyte0..3` without modifier, in straight-line code under the entry exec mask, with no write since. Any other register is refused (`source vN is not proven to be +0.0 or an integer in 1..255`) unless the caller selects the `pk_u8_s0_contract` keyword of `Pipeline`, which states that every such S0 holds +0.0 or an integer in 1..255 and is recorded in the pipeline notes. The fixture draws select it, because their S0 words come from flat parameters; the request path never does. The HLSL is `(S2 & ~(0xffu << ((S1 & 3u) << 3u))) | ((uint(asfloat(S0)) & 0xffu) << ((S1 & 3u) << 3u))`. +`v_cvt_pk_u8_f32 vD, s0, s1, s2` (VOP3A opcode 350, #1546) lowers to an inline expression on known words. The RDNA 1.0 and RDNA 2 ISA guides give `D = (S2 & ~(0xff << 8 * S1[1:0])) | ((flt32_to_uint8(S0) & 0xff) << 8 * S1[1:0])`. The byte select and the merge are fully specified. `flt32_to_uint8` is not defined: the guides give no rounding mode, no bounds and no result for NaN or negative inputs, and AMD's machine-readable ISA description only says that the float becomes an unsigned 8-bit integer. The translation is claimed only where every reading agrees: an S0 that is +0.0 or an integer in [1, 255]. Truncation, every rounding mode and any clamp give the same byte there. A constant S0 outside that set is refused. + +A register S0 is accepted by ordinary translation only when it is proven in the set: written by `v_cvt_f32_ubyte0..3` without modifier, in straight-line code under the entry exec mask, with no write since. Any other register is refused (`source vN is not proven to be +0.0 or an integer in 1..255`) unless the caller selects the `pk_u8_s0_contract` keyword of `Pipeline`, which states that every such S0 holds +0.0 or an integer in 1..255 and is recorded in the pipeline notes. The fixture draws select it, because their S0 words come from flat parameters; the request path never does. The HLSL is `(S2 & ~(0xffu << ((S1 & 3u) << 3u))) | ((uint(asfloat(S0)) & 0xffu) << ((S1 & 3u) << 3u))`. S0 is a VGPR, an SGPR that holds a known word, a literal, the integer zero or an inline float constant. S1 and S2 are any known word. A constant S0 outside the agreed set is refused with `Unverified ALU operand`: `0.5`, `1/(2*pi)`, 1.5, negative values, -0.0, 256.0, infinity, NaN, a nonzero inline integer (a denormal bit pattern) and a word wider than 32 bits. A word read from memory is not known statically, so a caller that binds one must keep it inside the set. The input modifiers `-`, `|...|` and `-|...|`, `clamp`, `mul:2`, the `_e64` spelling and a register range are not lowered; each stops at the instruction. Unknown, pending, descriptor and saved-exec sources and a destination past v255 are refused, as for the integer subset. A result is data only and is refused as an address or a lane-guard operand. From e556be5e11ac0e7bd987214914ee6ad55d6a87da Mon Sep 17 00:00:00 2001 From: Ybucaille <83926195+Ybucaille@users.noreply.github.com> Date: Wed, 25 Nov 2026 06:54:45 +0100 Subject: [PATCH 4/4] Give byte_float_v a class default like the other VGPR sets The proof set was read through getattr and created through __dict__.setdefault, which would hide a misspelt name. An immutable class default, replaced by a real set on the first proof, keeps probes built without __init__ working, as low16_only_v does. The proof site now says that every VGPR write must drop it. --- tools/startup/gfx2hlsl.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 7b58429e40..ec584401ef 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -447,6 +447,7 @@ class Stage: low16_only_v = () # VGPRs whose upper 16 bits are undefined after odd-dmask d16 image_load pinned_low16_v = () # low16_only_v live across basic-block boundaries whole_read_v = () # VGPRs read outside a proven low-16-bit consumer + byte_float_v = () # VGPRs proven to hold +0.0 or an integer in 1..255 (v_cvt_f32_ubyte0..3 result) def __init__(self, pipe, kind, code, refusals=None, decoded=None): self.pipe, self.kind = pipe, kind @@ -615,7 +616,8 @@ def clear_low16(self, n): self.low16_only_v.discard(n) def set_v(self, n, expr, *, low16_only=False): - getattr(self, 'byte_float_v', set()).discard(n) + if self.byte_float_v: + self.byte_float_v.discard(n) if hasattr(self, 'uniform_v'): self.uniform_v.discard(n) if low16_only: @@ -941,7 +943,11 @@ def lower(self, base, asm, ops, mods): if (base.startswith('v_cvt_f32_ubyte') and not mods and self.exec_known and not getattr(self, 'has_branches', True)): # The whole register now holds float(0..255), exactly +0.0 or an integer in 1..255, until the next write. - self.__dict__.setdefault('byte_float_v', set()).add(dst) + # Every VGPR write goes through set_v, which drops the proof; a path writing a VGPR without set_v + # would have to drop it too. + if not isinstance(self.byte_float_v, set): + self.byte_float_v = set() + self.byte_float_v.add(dst) return None if base in VEC_U: arity, text = VEC_U[base] @@ -1973,7 +1979,7 @@ def op_v_cvt_pk_u8_f32(self, ops, mods): src = reg_range(ops[1]) if src[0] != 'v' or src[1] != src[2]: raise Unsupported('v_cvt_pk_u8_f32 source %s' % ops[1]) - if src[1] not in getattr(self, 'byte_float_v', ()): + if src[1] not in self.byte_float_v: if not getattr(getattr(self, 'pipe', None), 'pk_u8_s0_contract', False): raise Unsupported('v_cvt_pk_u8_f32 source %s is not proven to be +0.0 or an integer in 1..255' % ops[1])