From 9b6f6473bef2704e26357e7d971b15e59a3e7754 Mon Sep 17 00:00:00 2001 From: saii <120340942+Not-Saii@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:27:19 +0200 Subject: [PATCH] startup: lower v_fma_mix* through a precise mad so a zero product keeps its sign On an RX 5700 XT (#2729) and an RX 9070 XT (#2746) the -0 case of startup_gfx2hlsl_fma_mix_dispatch_hardware read back 00000000. Their probes lost the sign only when a binary16 operand widened by f16tof32 fed a non-precise multiply or mad. Non-precise HLSL arithmetic need not keep a zero's sign. All three forms now call mix32(a, b, c), a precise mad. Operand order, half selection and modifiers are unchanged; saturate stays outside. mad still lets a backend fuse, so the 1 ULP note stands. The dispatch mode prints a fused/unfused probe per adapter, and a control helper that clears a -0 result must fail exactly the -0 case. The translation changes, so the cache revision moves to x2w-startup-shader-28 (and 28cs) in gfx2hlsl.py, d3d12_draw.inl and the tests that spell it. Refs #2729 Refs #2746 Co-Authored-By: Claude Opus 5.5 --- docs/experiments.md | 8 +++ docs/startup-runtime.md | 3 +- src/startup_stubs/d3d12_draw.inl | 4 +- .../startup_compute_request_offline_check.py | 4 +- tests/startup_gfx2hlsl_buffer_store_check.py | 4 +- .../startup_gfx2hlsl_const_copy_join_check.py | 2 +- tests/startup_gfx2hlsl_fma_mix_check.py | 54 ++++++++++++------- tests/startup_gfx2hlsl_fract_check.py | 4 +- tests/startup_indirect_draw_indexed.cpp | 2 +- ...tartup_packed_position_provenance_check.py | 2 +- tests/startup_resource_retire_title.cpp | 2 +- tests/startup_shader_cache_key_check.py | 6 +-- tools/startup/gfx2hlsl.py | 12 +++-- 13 files changed, 68 insertions(+), 39 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 9941e98f33..a9969d7afa 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -15449,6 +15449,14 @@ Measured at `741ff02c` plus the dispatch mode, `dev` preset, Windows 11 26300.99 ctest --preset dev --no-tests=error -j1 -R '^startup_gfx2hlsl_fma_mix(_fxc|_dispatch_(warp|hardware))?$' -V ``` +### Signed zero: a `precise` `mad` (cache revision 28) + +On an AMD Radeon RX 5700 XT (#2729) and an RX 9070 XT (#2746), the `-0` case read back `00000000` instead of `80000000`; WARP kept the sign. Their probes lost it only when a binary16 operand widened by `f16tof32` fed a non-`precise` multiply or `mad`, and kept it under `precise`. Non-`precise` HLSL arithmetic does not have to keep a zero's sign. + +All three forms now call `mix32(a, b, c)`, whose body is `precise float result = mad(a, b, c); return result;`. Operand order, half selection and modifiers are unchanged, and `saturate` stays outside the call. DXC 1.9.2602.17 emits it as `FMad` with `!dx.precise` on the `FMad` and on the `legacyF16ToF32` that feeds it. `mad` still lets a backend fuse or round the product, so the 1 ULP note above still applies. The dispatch mode prints `mad(4097, 4097, -16785408)` per adapter as information: `3f800000` means fused and `00000000` unfused. A second control shader uses a helper that clears the sign of a `-0` result. It must fail only the `-0` case. + +Measured on this branch, `dev` preset, Windows 11 26300, DXC 1.9.2602.17, D3D12 debug layer on with 0 errors and 0 warnings. WARP (1414:008c, 10.0.26100.9818): 17 of 17 words, `-0` kept, unfused. NVIDIA GeForce RTX 4090 (10de:2684, 32.0.16.1088): 17 of 17, fused. Both controls behaved as described. The RTX 4090 also passed before this change, so these runs do not show the fix on RDNA. The RX 5700 XT and RX 9070 XT retests are still pending. + ## Non-fused multiply-add in the startup translator The gfx1013 `v_mad_f32`, `v_mac_f32`, `v_madmk_f32`, `v_madak_f32` and diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index eb04947379..7480e7e199 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-27` and `x2w-startup-shader-27cs`. +Graphics and compute use cache revisions `x2w-startup-shader-28` and `x2w-startup-shader-28cs`. +Revision 28 lowers `v_fma_mix*` through a `precise` `mad`, so a zero product keeps its sign. 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 diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index b30f0a8bff..6b4e009435 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-27"; + constexpr char revision[] = "x2w-startup-shader-28"; 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-27cs"; // tools/startup/gfx2hlsl.py compute_key + constexpr char compute_revision[] = "x2w-startup-shader-28cs"; // 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 d2b65817b3..89519ce701 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 = 0xb52531cd9c15da3b # framed key at revision 27cs +KEY = 0x39e7422f5da15950 # framed key at revision 28cs def framed_key(): h = 1469598103934665603 - for b in b'x2w-startup-shader-27cs': + for b in b'x2w-startup-shader-28cs': h = ((h ^ b) * 1099511628211) & 0xffffffffffffffff for part in (CODE, DATA, ROOT, DESC, STATE_A, STATE_B): for b in struct.pack('> 16) * F(v3) + F(v4));') + 'v1 = U(mix32(f16tof32(v2 >> 16), F(v3), F(v4)));') check('control: op_sel changes the selected half', low != high) check('control: op_sel_hi changes the source format', plain != low) expect('abs then neg after widening a high half, scalar source, clamp', encode(MIX_F32, 1, (v2, S5, v4), op_sel=(0, 1, 0), op_sel_hi=(0, 1, 0), abs_bits=(0, 1, 0), neg=(0, 1, 0), clamp=1), 'v_fma_mix_f32 v1, v2, -|s5|, v4 op_sel:[0,1,0] op_sel_hi:[0,1,0] clamp', - 'v1 = U(saturate(F(v2) * (-abs(f16tof32(s5 >> 16))) + F(v4)));') + 'v1 = U(saturate(mix32(F(v2), (-abs(f16tof32(s5 >> 16))), F(v4))));') expect('inline 0 as a binary16 addend', encode(MIX_F32, 1, (v2, v3, ZERO), op_sel_hi=(1, 1, 1)), 'v_fma_mix_f32 v1, v2, v3, 0 op_sel_hi:[1,1,1]', - 'v1 = U(f16tof32(v2) * f16tof32(v3) + 0.0);') + 'v1 = U(mix32(f16tof32(v2), f16tof32(v3), 0.0));') expect('negated inline 0 addend keeps a -0 product', encode(MIX_F32, 1, (v2, v3, ZERO), op_sel=(0, 1, 0), op_sel_hi=(0, 1, 0), neg=(0, 0, 1)), 'v_fma_mix_f32 v1, v2, v3, -0 op_sel:[0,1,0] op_sel_hi:[0,1,0]', - 'v1 = U(F(v2) * f16tof32(v3 >> 16) + (-0.0));') + 'v1 = U(mix32(F(v2), f16tof32(v3 >> 16), (-0.0)));') expect('inline float on a binary32 source', encode(MIX_F32, 1, (v2, ONE_F, v4), op_sel_hi=(1, 0, 0)), 'v_fma_mix_f32 v1, v2, 1.0, v4 op_sel_hi:[1,0,0]', - 'v1 = U(f16tof32(v2) * 1.0 + F(v4));') + 'v1 = U(mix32(f16tof32(v2), 1.0, F(v4)));') # neg on inline -0.5 prints as `--0.5`; `(--0.5)` would be an HLSL decrement (review on #2055). expect('negated negative inline float', encode(MIX_F32, 1, (MINUS_HALF_F, v3, v4), neg=(1, 0, 0)), 'v_fma_mix_f32 v1, --0.5, v3, v4', - 'v1 = U((-(-0.5)) * F(v3) + F(v4));') + 'v1 = U(mix32((-(-0.5)), F(v3), F(v4)));') expect('control: an unnegated literal on a binary32 source still translates', encode(MIX_F32, 1, (LITERAL, v3, v4), literal=0x3f800000), 'v_fma_mix_f32 v1, 0x3f800000, v3, v4', - 'v1 = U(F(0x3f800000u) * F(v3) + F(v4));') + 'v1 = U(mix32(F(0x3f800000u), F(v3), F(v4)));') # --- the binary16-destination forms keep their packing -------------------------------------------- expect('mixlo writes the low half', encode(MIXLO_F16, 1, (v2, v3, v4), op_sel_hi=(1, 1, 0)), 'v_fma_mixlo_f16 v1, v2, v3, v4 op_sel_hi:[1,1,0]', - 'v1 = (v1 & 0xffff0000u) | f32tof16(f16tof32(v2) * f16tof32(v3) + F(v4));') + 'v1 = (v1 & 0xffff0000u) | f32tof16(mix32(f16tof32(v2), f16tof32(v3), F(v4)));') expect('mixlo with a negated inline 0 on a binary16 source', encode(MIXLO_F16, 1, (v2, v3, ZERO), op_sel_hi=(1, 1, 1), neg=(0, 0, 1)), 'v_fma_mixlo_f16 v1, v2, v3, -0 op_sel_hi:[1,1,1]', - 'v1 = (v1 & 0xffff0000u) | f32tof16(f16tof32(v2) * f16tof32(v3) + (-0.0));') + 'v1 = (v1 & 0xffff0000u) | f32tof16(mix32(f16tof32(v2), f16tof32(v3), (-0.0)));') expect('mixhi writes the high half from a high-half source', encode(MIXHI_F16, 1, (v2, v3, v4), op_sel=(0, 0, 1), op_sel_hi=(0, 0, 1)), 'v_fma_mixhi_f16 v1, v2, v3, v4 op_sel:[0,0,1] op_sel_hi:[0,0,1]', - 'v1 = (v1 & 0xffffu) | (f32tof16(F(v2) * F(v3) + f16tof32(v4 >> 16)) << 16);') + 'v1 = (v1 & 0xffffu) | (f32tof16(mix32(F(v2), F(v3), f16tof32(v4 >> 16))) << 16);') # A register whose upper 16 bits are undefined (odd-dmask D16 image_load) can feed a low-half read. expect('low half of a register with undefined upper bits', encode(MIX_F32, 1, (v2, v3, v4), op_sel_hi=(1, 0, 0)), 'v_fma_mix_f32 v1, v2, v3, v4 op_sel_hi:[1,0,0]', - 'v1 = U(f16tof32(v2) * F(v3) + F(v4));', low16_only_v={2}) + 'v1 = U(mix32(f16tof32(v2), F(v3), F(v4)));', low16_only_v={2}) # --- refusals ------------------------------------------------------------------------------------- expect_refused('high half of a register with undefined upper bits', @@ -212,7 +212,7 @@ def expect_refused(label, code, asm, low16_only_v=()): expect('literal on a binary16 source', encode(MIX_F32, 1, (LITERAL, v3, v4), op_sel_hi=(1, 0, 0), literal=0x3c00), 'v_fma_mix_f32 v1, 0x3c00, v3, v4 op_sel_hi:[1,0,0]', - 'v1 = U(f16tof32(0x3c00u) * F(v3) + F(v4));') + 'v1 = U(mix32(f16tof32(0x3c00u), F(v3), F(v4)));') # The decoder prints neg on inline 1..64 or a literal as a negative integer. Read as an integer, the # sign-bit flip is lost (neg on inline 1 is 0x80000001, not 0xffffffff), so these stay refused. # Inline -1 prints the same text as neg on inline 1, so it is refused too. @@ -236,12 +236,12 @@ def expect_refused(label, code, asm, low16_only_v=()): suffix = (' op_sel:[1,0,0]' if high else '') + ' op_sel_hi:[1,0,0]' expect('select literal half %d' % high, raw, 'v_fma_mix_f32 v1, 0x40003c00, v3, v4' + suffix, - 'v1 = U(f16tof32(0x%04xu) * F(v3) + F(v4));' % bits) + 'v1 = U(mix32(f16tof32(0x%04xu), F(v3), F(v4)));' % bits) expect('inline half on a low binary16 source', encode(MIX_F32, 1, (240, v3, v4), op_sel_hi=(1, 0, 0)), 'v_fma_mix_f32 v1, 0.5, v3, v4 op_sel_hi:[1,0,0]', - 'v1 = U(0.5 * F(v3) + F(v4));') + 'v1 = U(mix32(0.5, F(v3), F(v4)));') expect_refused('unverified high half of inline constant', encode(MIX_F32, 1, (240, v3, v4), op_sel=(1, 0, 0), op_sel_hi=(1, 0, 0)), 'v_fma_mix_f32 v1, 0.5, v3, v4 op_sel:[1,0,0] op_sel_hi:[1,0,0]') @@ -250,7 +250,7 @@ def expect_refused(label, code, asm, low16_only_v=()): def compile_translated(fxc): """Compile every translated line in one ps_5_1 entry point; returns FXC's exit status and output.""" regs = sorted({int(n) for line in translated for n in re.findall(r'\bv(\d+)\b', line)}) - body = ['#define F asfloat', '#define U asuint', + body = ['#define F asfloat', '#define U asuint', G.MIX32, 'uint main(nointerpolation uint4 p : TEXCOORD0) : SV_Target {', ' uint s5 = p.y;'] body += [' uint v%d = p.x + %du;' % (r, r) for r in regs] @@ -370,9 +370,9 @@ def raw_fill(index): return ((index + 1) * 0x9E3779B1 & 0xffffffff) | 1 -def dispatch_source(rows, lines): +def dispatch_source(rows, lines, helper=G.MIX32): """One thread: per case, load opaque inputs, run the translated line, store v1.""" - body = ['#define F asfloat', '#define U asuint', 'RWByteAddressBuffer data : register(u0);', + body = ['#define F asfloat', '#define U asuint', helper, 'RWByteAddressBuffer data : register(u0);', '[numthreads(1, 1, 1)]', 'void main() {'] for index, ((_label, words), line) in enumerate(zip(rows, lines)): base = index * 6 @@ -408,6 +408,10 @@ def check_dispatch(dxc, runner, adapter): rows = [(label, case_inputs(label, decode_mix(code))) for label, code, _line in cases] expected = [reference(decode_mix(code), words) for (_l, code, _), (_, words) in zip(cases, rows)] lines = [line for _l, _c, line in cases] + # Information, not a check: 4097 * 4097 = 16785409 rounds to 16785408 in binary32, so a fused mad + # returns 1.0 and an unfused one 0.0. mix32 allows either (#2729). + rows.append(('cancellation', {'v2': f32_word(4097.0), 'v3': f32_word(4097.0), 'v4': f32_word(-16785408.0)})) + lines.append('v1 = U(mix32(F(v2), F(v3), F(v4)));') out_dir = os.path.join(here, '..', 'out') os.makedirs(out_dir, exist_ok=True) with tempfile.TemporaryDirectory(dir=out_dir) as directory: @@ -417,6 +421,18 @@ def check_dispatch(dxc, runner, adapter): got = words[index * 6 + 5] check('%s: %s readback' % (adapter, label), got == expected[index], 'expected %08x, got %08x' % (expected[index], got)) + fused = words[len(cases) * 6 + 5] + print('%s: mad(4097, 4097, -16785408) = %08x (%s)' % ( + adapter, fused, {0x3f800000: 'fused', 0: 'unfused'}.get(fused, 'neither fused nor unfused'))) + # Negative control: a helper that clears the sign of a zero result, as the RDNA 1 and RDNA 4 drivers + # did without precise, must fail exactly the -0 case. + lossy = G.MIX32.replace('return result;', 'return asfloat(asuint(result) == 0x80000000u ? 0u : asuint(result));') + assert lossy != G.MIX32 + lost = run_dispatch(dxc, runner, adapter, dispatch_source(rows, lines, lossy), len(rows) * 6, directory, + 'fma_mix_sign_control') + caught = [label for i, (label, _c, _l) in enumerate(cases) if lost[i * 6 + 5] != expected[i]] + check('%s: control that clears a -0 result fails only the -0 product case' % adapter, + caught == ['negated inline 0 addend keeps a -0 product'], repr(caught)) # Negative control: drop the high-half shift, so the device reads the low half. The readback must # then match a reference with op_sel cleared, and differ from the correct result somewhere. # (A zero product, as in the -0 case, hides the half it reads.) diff --git a/tests/startup_gfx2hlsl_fract_check.py b/tests/startup_gfx2hlsl_fract_check.py index 3f27815f42..8d9c2de357 100644 --- a/tests/startup_gfx2hlsl_fract_check.py +++ b/tests/startup_gfx2hlsl_fract_check.py @@ -170,8 +170,8 @@ def float_capped(word): repr((seed and seed.group(1), literal and literal.group(1)))) check('the two literals are identical', seed and literal and seed.group(1) == literal.group(1), repr((seed and seed.group(1), literal and literal.group(1)))) -check('the revision is x2w-startup-shader-27, after the SCC model of s_add, s_sub, s_min and s_max', - bool(seed and seed.group(1) == 'x2w-startup-shader-27'), repr(seed and seed.group(1))) +check('the revision is x2w-startup-shader-28, after the precise v_fma_mix lowering', + bool(seed and seed.group(1) == 'x2w-startup-shader-28'), repr(seed and seed.group(1))) def key_from_seed(word): diff --git a/tests/startup_indirect_draw_indexed.cpp b/tests/startup_indirect_draw_indexed.cpp index 2ee5022f39..1641191bd5 100644 --- a/tests/startup_indirect_draw_indexed.cpp +++ b/tests/startup_indirect_draw_indexed.cpp @@ -308,7 +308,7 @@ ComPtr translated_pipeline(ID3D12Device* guest, ID3D12RootSignature* r bool needs_start, TableSample sample = TableSample::texture, 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-27"; + constexpr char revision[] = "x2w-startup-shader-28"; static std::uint32_t vertex_ids[]{0xe0000101, 0xe0000102, 0xe0000103}; static std::uint32_t pixel_ids[]{0xe0000202, 0xe0000203, 0xe0000205}; static std::uint32_t indexed_pixel_id = 0xe0000204; diff --git a/tests/startup_packed_position_provenance_check.py b/tests/startup_packed_position_provenance_check.py index fa65464864..b68fa1c556 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-27'))))) + bare == G.fnv(root_blob(), G.fnv(ps_words(4), G.fnv(VS_WORDS, G.fnv(b'x2w-startup-shader-28'))))) 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 7805001d3e..ce21bc438f 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-27"; // d3d12_draw.inl cached_translation +constexpr char draw_revision[] = "x2w-startup-shader-28"; // 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 717ce44d6e..e4b0733b63 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-27 excludes NUL. Stored order is a, b, root, then optional input layout. + # Revision x2w-startup-shader-28 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) == 0xdbd1da85b0e910d0 - assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe") == 0x1c9404a56b034dc + assert gfx2hlsl.pipeline_key(*vector) == 0xe4791179bf7096b3 + assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe") == 0xf6cb7f56c2985cfb 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 760b6e7289..2a07c4b74a 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -8653,8 +8653,9 @@ def operand(tok, is_half, is_hi): return '(-%s)' % text if neg else text # v_fma_mix* is fused, but HLSL may round `a * b` before the add, so binary32 sources with long - # mantissas can differ from the hardware by 1 ULP (same as v_fma_f32 in VEC_F). - expr = '%s * %s + %s' % tuple(operand(*a) for a in zip(ops[1:4], half, hi)) + # mantissas can differ from the hardware by 1 ULP (same as v_fma_f32 in VEC_F). mix32 is a precise + # mad: without it an RDNA 1 and an RDNA 4 driver drop the sign of a -0 product (#2729, #2746). + expr = 'mix32(%s, %s, %s)' % tuple(operand(*a) for a in zip(ops[1:4], half, hi)) if clamp: expr = 'saturate(%s)' % expr d = reg_range(ops[0])[1] @@ -9448,6 +9449,8 @@ def declarations(self): 'v_min3_f32': (3, 'minmax_zero32({0}, minmax_zero32({1}, {2}, false), false)'), 'v_max3_f32': (3, 'minmax_zero32({0}, minmax_zero32({1}, {2}, true), true)'), } +# v_fma_mix*: precise keeps the sign of a zero result; mad still lets a backend fuse or round the product. +MIX32 = 'float mix32(float a, float b, float c) { precise float result = mad(a, b, c); return result; }' # 16-bit VALU: (sources, source kind, float or uint expression of the sources, result kind). 'h' sources are # binary16 read from the low half and computed in binary32; an 'h' result is rounded back to binary16. The # i16/u16 conversions to binary16 read the low half as an integer; the conversions to integers truncate toward @@ -9978,6 +9981,7 @@ def common(self): 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; }', + MIX32, '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)));', @@ -10566,7 +10570,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-27')))) + key = fnv(root_blob, fnv(ps_code, fnv(vs_code, fnv(b'x2w-startup-shader-28')))) key = fnv(layout, key) if layout else key return key if ps_inputs is None else fnv(b'psin1' + struct.pack('