diff --git a/docs/experiments.md b/docs/experiments.md index 7495165aa3..f1304d3101 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -15394,6 +15394,15 @@ On WARP 10.0.26100.9818 and on an AMD Radeon RX 6600 (driver 32.0.21045.29003), `tests/startup_gfx2hlsl_swap_check.py` (`startup_gfx2hlsl_swap`) decodes the documented VOP1 words, evaluates the translated text of seven programs on the CPU against a plain exchange of two list entries, and checks the refusals. Two mutations (write before read, only one destination written) are loaded from the translator source and must fail the fixtures. `python -B tests/startup_gfx2hlsl_swap_check.py binary llvm-mc-15` also reassembles the source lines. This is translation only; no draw was run. +## Signed zero in startup binary16 multiplication + +The `startup_gfx2hlsl_f16_alu_draw` fixture draws plain, packed and SDWA binary16 products +with independently computed raw-word expectations. In the SDWA high-word preserve case, +the lower 16 bits start at `abcd`. Multiplying negative zero by positive zero and applying +the fixture's export tag must read back `e000abcd`. Losing the sign gives `6000abcd`. +The HLSL multiply uses a `precise` binary32 product before the binary16 rounding step. +Run `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_f16_alu_draw_(warp|hardware)$' -V`. + ## Startup translator: v_fma_mix_f32 `tools/startup/gfx2hlsl.py` now lowers `v_fma_mix_f32` (VOP3P, opcode 0x20), which it used to refuse. It shares one handler with `v_fma_mixlo_f16` and `v_fma_mixhi_f16`. Each source is read as LLVM 15 selects it (`SelectVOP3PMadMixModsImpl`): `op_sel_hi` set means a binary16 source, and `op_sel` then picks its high half. abs and then neg apply after widening. The binary32 result is written to the whole VGPR, with optional `clamp`. diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 147a15f600..d0bbfd7399 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -208,7 +208,8 @@ The translation is refused when: - either header has another size; - the code reads an attribute the table does not list. -Graphics and compute use cache revisions `x2w-startup-shader-24` and `x2w-startup-shader-24cs`. +Graphics and compute use cache revisions `x2w-startup-shader-25` and `x2w-startup-shader-25cs`. +Revision 25 preserves the sign of zero in binary16 multiply, including the packed and SDWA forms. Revision 24 invalidates translations that revision 23 wrongly accepted: `vcc_hi_scalar_mask_read` now also treats a whole-word lane-read writer (`v_readfirstlane`/`v_readlane`) of vcc_hi, in branch order too, as making a later VOP3 lane-mask read of vcc_hi refuse instead of lowering to bit 0. The two cache keys advance together. diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 162b0a0bc1..a6e4c40da4 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -353,7 +353,7 @@ Translation* cached_translation(const void* a, std::size_t a_bytes, const void* { // Keep pre-fix desktop bytecode and manifests out of this ABI's cache. // tools/startup/gfx2hlsl.py hashes the same bytes, without a trailing NUL. - constexpr char revision[] = "x2w-startup-shader-24"; + constexpr char revision[] = "x2w-startup-shader-25"; const u64 seed = fnv_bytes(revision, sizeof(revision) - 1); u64 key = fnv_bytes(root_blob.data(), root_blob.size(), fnv_bytes(b, b_bytes, fnv_bytes(a, a_bytes, seed))); // A stream's input layout (stream_input_layout) feeds the vertex fetch, so it is part of the key. @@ -474,7 +474,7 @@ u64 publish_compute_request(const void* desc) std::vector fields(sizeof(words)); words[0] = words[1] = words[3] = words[5] = words[7] = words[9] = words[10] = 0; std::memcpy(fields.data(), words, sizeof(words)); - constexpr char compute_revision[] = "x2w-startup-shader-24cs"; // tools/startup/gfx2hlsl.py compute_key + constexpr char compute_revision[] = "x2w-startup-shader-25cs"; // tools/startup/gfx2hlsl.py compute_key u64 key = fnv_bytes(compute_revision, sizeof(compute_revision) - 1); const std::vector* order[] = {&code, &data, &root_blob, &fields, &state_a, &state_b}; for (const auto* part : order) key = fnv_part(*part, key); diff --git a/tests/startup_compute_request_offline_check.py b/tests/startup_compute_request_offline_check.py index f0158aaac8..406bf5c21e 100644 --- a/tests/startup_compute_request_offline_check.py +++ b/tests/startup_compute_request_offline_check.py @@ -25,12 +25,12 @@ DESC = struct.pack('<12Q', 0, 0, 24, 0, 20, 0, len(CODE), 0, len(DATA), 0, 0, 11) STATE_A = struct.pack('<3Q', 1, 0x2c, 0xfff0000000000003) STATE_B = struct.pack('<5I', 0x1234abcd, 0xffff0000, 0, 0, 0) -KEY = 0xb3810947d0d2334c # framed key at revision 24cs +KEY = 0xc654194497506ecd # framed key at revision 25cs def framed_key(): h = 1469598103934665603 - for b in b'x2w-startup-shader-24cs': + for b in b'x2w-startup-shader-25cs': h = ((h ^ b) * 1099511628211) & 0xffffffffffffffff for part in (CODE, DATA, ROOT, DESC, STATE_A, STATE_B): for b in struct.pack(' translated_pipeline(ID3D12Device* guest, ID3D12RootSignature* r bool needs_start, bool table_buffer = false, bool indexed = false, bool rect = false) { // The opaque guest programs identify synthetic cache entries. The actual bound shaders are source-built DXBC. - constexpr char revision[] = "x2w-startup-shader-24"; + constexpr char revision[] = "x2w-startup-shader-25"; static std::uint32_t vertex_ids[]{0xe0000101, 0xe0000102, 0xe0000103}; static std::uint32_t pixel_ids[]{0xe0000202, 0xe0000203, 0xe0000204}; auto& vertex_id = vertex_ids[indexed ? 2 : needs_start]; diff --git a/tests/startup_packed_position_provenance_check.py b/tests/startup_packed_position_provenance_check.py index 145ee74b70..43355bde76 100644 --- a/tests/startup_packed_position_provenance_check.py +++ b/tests/startup_packed_position_provenance_check.py @@ -206,7 +206,7 @@ def refused(label, cache, key, reason): cache = Path(tmp) bare = G.pipeline_key(VS_WORDS, ps_words(4), root_blob()) check('without a description the key is unchanged', - bare == G.fnv(root_blob(), G.fnv(ps_words(4), G.fnv(VS_WORDS, G.fnv(b'x2w-startup-shader-24'))))) + bare == G.fnv(root_blob(), G.fnv(ps_words(4), G.fnv(VS_WORDS, G.fnv(b'x2w-startup-shader-25'))))) keys = [put_request(cache, ps_words(4), masks) for masks in (TARGET, SAME_V4, DECOY)] check('same stages, root and layout with three mask pairs: three request identities', len(set(keys)) == 3 and bare not in keys diff --git a/tests/startup_resource_retire_title.cpp b/tests/startup_resource_retire_title.cpp index aec17aa355..0138466def 100644 --- a/tests/startup_resource_retire_title.cpp +++ b/tests/startup_resource_retire_title.cpp @@ -407,7 +407,7 @@ constexpr UINT draw_side = 4; // 4x4 R8G8B8A8_UNORM target constexpr UINT draw_covered = 3; // scissor leaves the last row and column untouched as the sentinel constexpr int draw_seed = 77; constexpr UINT draw_clear_pixel = 0xffff00ffu; // ClearRenderTargetView({1, 0, 1, 1}) in R8G8B8A8_UNORM -constexpr char draw_revision[] = "x2w-startup-shader-24"; // d3d12_draw.inl cached_translation +constexpr char draw_revision[] = "x2w-startup-shader-25"; // d3d12_draw.inl cached_translation constexpr char draw_vertex_shader[] = R"( struct Varying { float4 position : SV_Position; }; diff --git a/tests/startup_shader_cache_key_check.py b/tests/startup_shader_cache_key_check.py index c33f11f658..529fb631bb 100644 --- a/tests/startup_shader_cache_key_check.py +++ b/tests/startup_shader_cache_key_check.py @@ -49,10 +49,10 @@ def check_request(blobs, key, diagnostic=None, rtv_request=None, layout=None): def main(): # Independently executed C++ runtime fnv_bytes/cached_translation known-answer vector. - # Revision x2w-startup-shader-24 excludes NUL. Stored order is a, b, root, then optional input layout. + # Revision x2w-startup-shader-25 excludes NUL. Stored order is a, b, root, then optional input layout. vector = (b"\x00VS\xff", b"PS\x00\x80", b"RTS0\x00") - assert gfx2hlsl.pipeline_key(*vector) == 0x5c116f02a69b7aef - assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe") == 0x7cccad648fafa30f + assert gfx2hlsl.pipeline_key(*vector) == 0xdf78b28cb80d4c5a + assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe") == 0x466483d084260802 blobs = (VERTEX, PIXEL, ROOT) canonical = gfx2hlsl.pipeline_key(*blobs) check_request(blobs, canonical) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 2bdefd5b10..c3745d8118 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -8353,7 +8353,7 @@ def packed_half(self, base, arity, text, ops, mods): self.set_v(d, '%s | (%s << 16)' % tuple(lanes)) def op_v_pk_mul_f16(self, ops, mods): - self.packed_half('v_pk_mul_f16', 2, '{0} * {1}', ops, mods) + self.packed_half('v_pk_mul_f16', 2, 'mul16({0}, {1})', ops, mods) def op_v_pk_add_f16(self, ops, mods): self.packed_half('v_pk_add_f16', 2, '{0} + {1}', ops, mods) @@ -9094,7 +9094,7 @@ def declarations(self): # zero, saturate to the 16-bit range and read NaN as 0. VEC_H = { 'v_add_f16': (2, 'h', '{0} + {1}', 'h'), 'v_sub_f16': (2, 'h', '{0} - {1}', 'h'), - 'v_subrev_f16': (2, 'h', '{1} - {0}', 'h'), 'v_mul_f16': (2, 'h', '{0} * {1}', 'h'), + 'v_subrev_f16': (2, 'h', '{1} - {0}', 'h'), 'v_mul_f16': (2, 'h', 'mul16({0}, {1})', 'h'), 'v_min_f16': (2, 'h', 'min({0}, {1})', 'h'), 'v_max_f16': (2, 'h', 'max({0}, {1})', 'h'), 'v_fmac_f16': (2, 'h', 'fma16o({0}, {1}, {2})', 'h'), 'v_fmamk_f16': (3, 'h', 'fma16o({0}, {1}, {2})', 'h'), 'v_fmaak_f16': (3, 'h', 'fma16o({0}, {1}, {2})', 'h'), @@ -9600,6 +9600,8 @@ def common(self): # zero despite `precise`, so ties rounded the wrong way. fma16_rne works on the encodings in 32-bit integer # arithmetic, so no driver floating-point rewrite can reach it. Infinities and NaNs keep float semantics. pixel_aot.SALU_HELPERS['fma16_rne'], + # A binary16 product is exact in binary32. Keep its sign when the result is zero (#2746). + 'float mul16(float a, float b) { precise float product = a * b; return product; }', 'float fma16o(float a, float b, float c) {', ' if (!isfinite(a) || !isfinite(b) || !isfinite(c)) return a * b + c;', ' return f16tof32(fma16_rne(f32tof16(a), f32tof16(b), f32tof16(c)));', @@ -10187,7 +10189,7 @@ def pipeline_key(vs_code, ps_code, root_blob, layout=b'', ps_inputs=None): SPI_PS_INPUT_ENA and SPI_PS_INPUT_ADDR as little-endian u32. Two pipelines with the same stages, root signature and input layout but different masks are then two requests with their own results. Without a description the key is unchanged.""" - key = fnv(root_blob, fnv(ps_code, fnv(vs_code, fnv(b'x2w-startup-shader-24')))) + key = fnv(root_blob, fnv(ps_code, fnv(vs_code, fnv(b'x2w-startup-shader-25')))) key = fnv(layout, key) if layout else key return key if ps_inputs is None else fnv(b'psin1' + struct.pack('