diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 58c2f00a5e..379d3384e4 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -77,6 +77,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"], {}), @@ -209,6 +210,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_cselect_mask_check.py \ tests/startup_gfx2hlsl_mask_overwrite_check.py \ diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f72a52a46..b238780f2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2541,6 +2541,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/docs/experiments.md b/docs/experiments.md index d7c9d78454..ede660c83f 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -5326,7 +5326,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. 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 new file mode 100644 index 0000000000..3a4de3ba17 --- /dev/null +++ b/tests/startup_gfx2hlsl_cvt_pk_u8_check.py @@ -0,0 +1,176 @@ +"""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. 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 +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(' 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 bd078c08b3..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,6 +616,8 @@ def clear_low16(self, n): self.low16_only_v.discard(n) def set_v(self, n, expr, *, low16_only=False): + if self.byte_float_v: + self.byte_float_v.discard(n) if hasattr(self, 'uniform_v'): self.uniform_v.discard(n) if low16_only: @@ -934,9 +937,18 @@ 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)) + dst = reg_range(ops[0])[1] low16 = base in ('v_cvt_f32_f16', 'v_cvt_f32_ubyte0', 'v_cvt_f32_ubyte1') - return self.set_vf(reg_range(ops[0])[1], - text_f.format(*[self.u(o, low16_only=low16) for o in ops[1:1 + arity]]), mods) + 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)): + # The whole register now holds float(0..255), exactly +0.0 or an integer in 1..255, until the next write. + # 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] if mods: @@ -1940,6 +1952,44 @@ def op_v_cvt_i32_f32(self, ops, mods): dst = reg_range(ops[0])[1] self.set_v(dst, 'uint(int(F(%s)))' % self.u(ops[1])) + def op_v_cvt_pk_u8_f32(self, ops, mods): + # 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 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 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: + raise Unsupported('v_cvt_pk_u8_f32 with %d operands, expected 4' % len(ops)) + dst = reg_range(ops[0]) + if not dst or dst[0] != 'v' or dst[1] != dst[2]: + raise Unsupported('v_cvt_pk_u8_f32 destination %s' % ops[0]) + if not reg_range(ops[1]): + src = self.u(ops[1]) + bits = int(src[:-1], 16) + value = struct.unpack(' (vertex parameter, component mask); None pairs by number self.vs_fetch = vs_fetch # stream_fetch(): the loads the vertex stage's entry call stands for