Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -474,7 +474,7 @@ u64 publish_compute_request(const void* desc)
std::vector<unsigned char> 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<unsigned char>* order[] = {&code, &data, &root_blob, &fields, &state_a, &state_b};
for (const auto* part : order) key = fnv_part(*part, key);
Expand Down
4 changes: 2 additions & 2 deletions tests/startup_compute_request_offline_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<Q', len(part)) + part:
Expand Down
4 changes: 2 additions & 2 deletions tests/startup_gfx2hlsl_buffer_store_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ def main():
'%s %s' % (result, fail_reason(name)))

# 8. The cache revision moved, so a translation from before the store lowering is never loaded.
check('cache key uses revision 27', G.pipeline_key(b'a', b'b', ROOT) ==
G.fnv(ROOT, G.fnv(b'b', G.fnv(b'a', G.fnv(b'x2w-startup-shader-27')))))
check('cache key uses revision 28', G.pipeline_key(b'a', b'b', ROOT) ==
G.fnv(ROOT, G.fnv(b'b', G.fnv(b'a', G.fnv(b'x2w-startup-shader-28')))))

if failures:
print('FAILED: ' + ', '.join(failures))
Expand Down
2 changes: 1 addition & 1 deletion tests/startup_gfx2hlsl_const_copy_join_check.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""`s_mov_b32` copy of a plain constant SGPR in tools/startup/gfx2hlsl.py: run-time write and join.

The copy folds the constant into later reads (revision x2w-startup-shader-27), and these two cases pin what must
The copy folds the constant into later reads (revision x2w-startup-shader-28), and these two cases pin what must
stay true next to the fold:
- the copy still assigns its register at run time (`s12 = ...;` is emitted), so a reader the fold does not reach
(a branch target after a join) sees a real value;
Expand Down
54 changes: 35 additions & 19 deletions tests/startup_gfx2hlsl_fma_mix_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,63 +130,63 @@ def expect_refused(label, code, asm, low16_only_v=()):
plain = expect('f32 sources',
encode(MIX_F32, 1, (v2, v3, v4)),
'v_fma_mix_f32 v1, v2, v3, v4',
'v1 = U(F(v2) * F(v3) + F(v4));')
'v1 = U(mix32(F(v2), F(v3), F(v4)));')
low = expect('binary16 low half of src0',
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));')
'v1 = U(mix32(f16tof32(v2), F(v3), F(v4)));')
high = expect('binary16 high half of src0',
encode(MIX_F32, 1, (v2, v3, v4), op_sel=(1, 0, 0), op_sel_hi=(1, 0, 0)),
'v_fma_mix_f32 v1, v2, v3, v4 op_sel:[1,0,0] op_sel_hi:[1,0,0]',
'v1 = U(f16tof32(v2 >> 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',
Expand All @@ -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.
Expand All @@ -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]')
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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.)
Expand Down
4 changes: 2 additions & 2 deletions tests/startup_gfx2hlsl_fract_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/startup_indirect_draw_indexed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ ComPtr<IUnknown> 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;
Expand Down
2 changes: 1 addition & 1 deletion tests/startup_packed_position_provenance_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/startup_resource_retire_title.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down
6 changes: 3 additions & 3 deletions tests/startup_shader_cache_key_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading
Loading