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
9 changes: 9 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
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-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.
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-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.
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-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<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 = 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('<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 22', 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-24')))))
check('cache key uses revision 25', 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-25')))))

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-24), and these two cases pin what must
The copy folds the constant into later reads (revision x2w-startup-shader-25), 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
1 change: 1 addition & 0 deletions tests/startup_gfx2hlsl_f16_alu_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def run(asm, regs):
dst, expr = re.fullmatch(r'(v\d+) = (.*);', s.lines[0]).groups()
env = {f'v{i}': v for i, v in enumerate(regs)}
env.update(
mul16=lambda a, b: a * b,
f16tof32=lambda x: h2f(x), fma16o=fma16o, f16rn=lambda x: 0x7e00 if math.isnan(x) else (f2h(x) if abs(x) < 65520.0 else (0xfc00 if x < 0 else 0x7c00)), asuint=lambda x: x, saturate=lambda x: min(max(x, 0.0), 1.0),
abs=abs, min=min, max=max, clamp=lambda x, lo, hi: min(max(x, lo), hi),
trunc=lambda x: float(int(x)), isnan=math.isnan,
Expand Down
8 changes: 8 additions & 0 deletions tests/startup_gfx2hlsl_f16_alu_draw_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,14 @@ def check(label, ok):
all(pk_lane_ok(pk_parse(key)[1], x[:pk_parse(key)[2]], *pk_parse(key)[4][i % 4], pk_parse(key)[3])
for i, x in enumerate(DRAW[key])))
if BODY_WORDS:
# Non-precise multiplication can lose -0 on RDNA 4 while giving the expected word elsewhere.
# Check the qualifier and its use even when this host's driver preserves both forms.
products = (('plain', translate('v_mul_f16'), 4),
('packed', translate('v_pk_mul_f16:lo:0'), 8),
('SDWA', sdwa_draw_text(next(cfg for cfg in SDWA_DRAW if cfg[0] == 'v_mul_f16')), 4))
for form, text, count in products:
check('%s binary16 multiply preserves its zero sign' % form,
'precise float product = a * b; return product;' in text and text.count('f16rn(mul16(') == count)
for op in (*OPS, *PK_KEYS):
try:
text = translate(op)
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-24, after the vcc_hi lane-read writer guard',
bool(seed and seed.group(1) == 'x2w-startup-shader-24'), repr(seed and seed.group(1)))
check('the revision is x2w-startup-shader-25, after the vcc_hi lane-read writer guard',
bool(seed and seed.group(1) == 'x2w-startup-shader-25'), 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 @@ -295,7 +295,7 @@ ComPtr<IUnknown> 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];
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-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
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-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; };
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-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)
Expand Down
10 changes: 6 additions & 4 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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)));',
Expand Down Expand Up @@ -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('<II', *ps_inputs), key)

Expand All @@ -10197,7 +10199,7 @@ def compute_key(cs_code, data, root_blob, desc, state_a=b'', state_b=b''):
graphics revision followed by "cs", then each part preceded by its length as a little-endian u64: the code,
the words 7-8 blob, the root signature, the 12 description words with their pointers zeroed and the two
state blocks."""
key = fnv(b'x2w-startup-shader-24cs')
key = fnv(b'x2w-startup-shader-25cs')
for part in (cs_code, data, root_blob, desc, state_a, state_b):
key = fnv(part, fnv(struct.pack('<Q', len(part)), key))
return key
Expand Down
Loading