diff --git a/CMakeLists.txt b/CMakeLists.txt index c97af0a060..e4c867ac86 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4263,7 +4263,7 @@ if(EXISTS "${INTAKE_PYTHON}") set_tests_properties(startup_gfx2hlsl_compute_mbcnt_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) foreach(control IN ITEMS swapped-axes dropped-store b-offset c-unpredicated f-peer f-broadcast f-second-fill g-swapped-offsets g-overlap j-scalar-dropped h-inner-unpredicated k-block-entered m-else-unswitched n-unmasked o-mask-kept - n-dropped-broadcast) + n-dropped-broadcast p-dropped-andn2 p-dropped-or p-block-entered q-block-entered q-not-all-ones) add_test(NAME startup_gfx2hlsl_compute_${control}_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_compute_check.py" control ${control} "${DXC_EXECUTABLE}" "$" ${adapter}) diff --git a/docs/experiments.md b/docs/experiments.md index 2e93287b17..15f8059024 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -15508,13 +15508,24 @@ be the lane select of `v_cndmask`, the carry-in of `v_addc`, `v_subb`, `v_add_co `s_and_saveexec_b32` / `s_and_b32` with exec. `s_cbranch_vccz`, `s_cbranch_vccnz`, `s_cmp`, a copy of M and M as a data operand see the other lanes' bits, which the one-lane model does not have, and keep the refusal. +The same lane mask is accepted with three more block writes, each exact only under a condition. All of them need +every mask the write uses to hold lane bits (bit 0 is the lane's own bit: a `v_cmp`, an `s_and` with exec, a copy +of exec, or 0 / -1), not data such as a group id or `s_mov_b32 M, 16`, whose lane bit sits at bit `lane` in the wave: +- preserving: `s_andn2_b32 M, M, exec_lo` and `s_or_b32 M, M, T`, with T written in the block by a `v_cmp` or an + `s_and` of exec and a lane mask. A skipped lane keeps its bit, so M needs no zeroing, but M's last write before the + branch must itself hold lane bits (no branch or branch target after it); +- compare: a `v_cmp` (not `v_cmpx`) into M, only when `s_mov_b32 M, 0` (or `s_movk_i32 M, 0`) is the last write before the branch; +- all ones: `s_orn2_b32 M, X, exec_lo`, X a lane mask, only when `s_mov_b32 M, -1` is the last write before the branch. +Zeroing and all-ones writes do not mix, and any other write of M that no later zeroing or all-ones write overwrites +keeps the refusal. + The block is still refused in these cases: - a definition that is live at the join, unless the absorbed-read or narrowed-mask rule proves every read of it or it is the lane mask above; a lane mask with a failed condition stays refused (zeroing with a value other than 0 or missing, a branch or a branch target between the zeroing and the branch, a write of M that is not - from exec, a `vcc_hi` or 64-bit write, a whole-mask read after the join); + one of the forms above, a `vcc_hi` or 64-bit write, a whole-mask read after the join); - any other exec write: exec set from a compare alone, restored from a mask saved before the block or on only - some of the paths, written by a form the pass does not follow (`s_orn2`, `s_nand`, `s_nor`, `s_xnor`, `s_not`, + some of the paths, written by a form the pass does not follow (`s_orn2` into exec, `s_nand`, `s_nor`, `s_xnor`, `s_not`, the other saveexec and wrexec forms), written in a block that a branch from outside enters (a proven calculated `s_setpc_b64` jump counts), or restored from a copy of the other exec half; - an effect outside scalar registers, such as a scalar store or atomic, a barrier, `s_endpgm` or `v_writelane`; diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index f38340b825..ece68590a3 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -208,9 +208,9 @@ The translation is refused when: - either header has another size; - the code reads an attribute the table does not list. -The shader cache revision is `x2w-startup-shader-20`. Revision 20 decodes MTBUF/MUBUF format 50 (`R10G10B10A2_UNORM`) in the run-time FORMAT switch, so cached shaders from revision 19 are not reused. Revision 19 folds `s_mov_b32` from a scalar register that +The shader cache revision is `x2w-startup-shader-21`. Revision 21 refuses a compute lane-mask form that revision 20 translated (`s_and_b32 M, X, exec_lo` with X data, see docs/experiments.md), so its cached compute shaders are not reused. Revision 20 decodes MTBUF/MUBUF format 50 (`R10G10B10A2_UNORM`) in the run-time FORMAT switch, so cached shaders from revision 19 are not reused. Revision 19 folds `s_mov_b32` from a scalar register that holds a plain constant, so later reads of the copy use the constant word. The matching compute -(`x2w-startup-shader-20cs`) key advances because that stage uses the shared lowering. The fixed slot 57 mesh +(`x2w-startup-shader-21cs`) key advances because that stage uses the shared lowering. The fixed slot 57 mesh (`x2w-startup-mesh-57-3`) key stays, because that shader is not translated from guest code. Revisions before 19 read a register copied from a constant as a run-time value. Revision 18 preserves the sign of zero in floating-point min, max, min3, max3 and med3. diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 9d2c4aed19..a59f9888aa 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-20"; + constexpr char revision[] = "x2w-startup-shader-21"; 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-20cs"; // tools/startup/gfx2hlsl.py compute_key + constexpr char compute_revision[] = "x2w-startup-shader-21cs"; // 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 3b6c7fec31..46042f18ff 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 = 0x88e751913fcbf7a8 # framed key at revision 20cs +KEY = 0x90041d02ef75b4e9 # framed key at revision 21cs def framed_key(): h = 1469598103934665603 - for b in b'x2w-startup-shader-20cs': + for b in b'x2w-startup-shader-21cs': h = ((h ^ b) * 1099511628211) & 0xffffffffffffffff for part in (CODE, DATA, ROOT, DESC, STATE_A, STATE_B): for b in struct.pack('> lane) & 1 for lane in range(4) if not (e >> lane) & 1): + bad.add(m) + want = {'cmp': set(range(1, 16)), 'mov_exec': set(range(1, 16)), 'orn2': set(range(15))}.get(name, set()) + assert bad == want, (name, sorted(bad)) + + def check_controls(): carry = translate('address-carry').hlsl_cs() assert 's21 = (scc ? 1u : 0u);' in carry @@ -1223,6 +1292,7 @@ def check_controls(): raise AssertionError('graphics store accepted') check_hand_words() check_off_stores() + check_execz_mask_exact() for name in ('O-dword', 'O-shift'): hlsl = translate(name).hlsl_cs().splitlines() assert any('u0.Store' in line for line in hlsl), (name, hlsl) @@ -1622,16 +1692,22 @@ def phi(zero, write, read, between=(), inside=()): return splice(*enter, *zero, *between, 's_cbranch_execz %d' % (len(write) + len(inside)), *write, *inside, *leave, *read) phi_text = lambda code: translate('A', code=code).hlsl_cs() + SPLICE.update({'s_and_b32 s20, exec_lo, vcc_lo': '7e6a1487'}) cnd_vcc, cnd_s = ['v_cndmask_b32 v5, v5, v6, vcc_lo'], ['v_cndmask_b32 v5, v5, v6, s20'] for zero, write, read in ((['s_mov_b32 vcc_lo, 0'], ['s_mov_b32 vcc_lo, exec_lo'], cnd_vcc), (['s_mov_b32 s20, 0'], ['s_mov_b32 s20, exec_lo'], cnd_s), - (['s_mov_b32 vcc_lo, 0'], ['s_and_b32 vcc_lo, s7, exec_lo'], cnd_vcc), - (['s_mov_b32 s20, 0'], ['s_and_b32 s20, s7, exec_lo'], cnd_s), - (['s_mov_b32 s20, 0'], ['s_and_b32 s20, exec_lo, s7'], cnd_s)): + (['s_mov_b32 vcc_lo, 0'], ['v_cmp_gt_u32 vcc_lo, 3, v0', 's_and_b32 vcc_lo, vcc_lo, exec_lo'], cnd_vcc), + (['s_mov_b32 s20, 0'], ['v_cmp_gt_u32 vcc_lo, 3, v0', 's_and_b32 s20, vcc_lo, exec_lo'], cnd_s), + (['s_mov_b32 s20, 0'], ['v_cmp_gt_u32 vcc_lo, 3, v0', 's_and_b32 s20, exec_lo, vcc_lo'], cnd_s)): text = phi_text(phi(zero, write, read)) # the lane select reads the mask's lane bit, which the block set from exec (the model runs the block for # every lane it models, and skips it only where exec is clear, leaving the zeroing) assert '& 1) ? ' in text and ('vcc = (exec) & 1;' in text or 's20 = ' in text), text + # an s_and of exec with data (a group id) is not a lane mask: its lane bit is at bit `lane` in the wave + for write in (['s_and_b32 vcc_lo, s7, exec_lo'], ['s_and_b32 s20, s7, exec_lo']): + refused('definition of ' + ('vcc' if 'vcc_lo, s7' in write[0] else 's20') + ', live where the block joins', + lambda write=write: phi_text(phi(['s_mov_b32 %s, 0' % ('vcc_lo' if 'vcc_lo, s7' in write[0] else 's20')], + write, cnd_vcc if 'vcc_lo, s7' in write[0] else cnd_s))) # s_and with exec reads the lane bit only (the result is a lane mask): accepted phi_text(phi(['s_mov_b32 vcc_lo, 0'], ['s_mov_b32 vcc_lo, exec_lo'], ['s_and_b32 s20, vcc_lo, exec_lo'] + cnd_vcc)) # so does s_and_saveexec with the mask as its source (exec &= the lane bit); with the mask as its destination @@ -1670,6 +1746,58 @@ def phi(zero, write, read, between=(), inside=()): ['s_cmp_eq_u32 s20, 0'] + cnd_s))) refused(live('s20'), lambda: phi_text(phi(['s_mov_b32 s20, 0'], ['s_mov_b32 s20, exec_lo'], ['s_mov_b32 s7, s20'] + cnd_s))) + # A block that leaves the mask bit of a lane it skips unchanged is exact whatever M held before the branch: + # `s_andn2 M, M, exec_lo` and `s_or M, M, T` with T exec-masked in the block (the compiler's i1 phi with a + # per-lane old value). A compare into M, or an s_orn2 with exec, changes a skipped lane's bit: accepted only + # when M was zeroed (compare) or set to all ones (orn2) before the branch. + SPLICE.update({'s_andn2_b32 s20, s20, exec_lo': '147e148a', 's_andn2_b32 vcc_lo, vcc_lo, exec_lo': '6a7e6a8a', + 's_and_b32 s22, s7, exec_lo': '077e1687', 's_and_b32 s22, s7, s7': '07071687', + 's_or_b32 s20, s20, s22': '14161488', 's_or_b32 vcc_lo, vcc_lo, s22': '6a166a88', + 's_or_b32 s20, s20, s7': '14071488', 's_andn2_b32 s20, s7, exec_lo': '077e148a', + 's_orn2_b32 s20, s7, exec_lo': '077e148b', 'v_cmp_gt_u32 vcc_lo, 3, v0': '8300887d'}) + # T and M hold lane bits (bit 0 = the lane's bit, the model's encoding): T from a compare, M zeroed before the branch. + # Group-id data (s7) as T, or `s_mov_b32 M, 16` as M, has the wave's lane bit at bit `lane` and is refused. + cmp_t = ['v_cmp_gt_u32 vcc_lo, 3, v0', 's_and_b32 s22, vcc_lo, exec_lo'] + SPLICE.update({'s_and_b32 s22, vcc_lo, exec_lo': '6a7e1687', 's_mov_b32 s20, 16': '9003 94be'.replace(' ', ''), + 's_orn2_b32 s20, vcc_lo, exec_lo': '6a7e148b'}) + blend = ['s_andn2_b32 s20, s20, exec_lo', *cmp_t, 's_or_b32 s20, s20, s22'] + z20, zv = ['s_mov_b32 s20, 0'], ['s_mov_b32 vcc_lo, 0'] + for zero, write, read in ((z20, blend, cnd_s), (z20, blend[:1], cnd_s), + (zv, ['s_andn2_b32 vcc_lo, vcc_lo, exec_lo', 's_and_b32 s22, vcc_lo, exec_lo', + 's_or_b32 vcc_lo, vcc_lo, s22'], cnd_vcc)): + phi_text(phi(zero, write, read)) + # ANDing a numeric word with EXEC does not turn it into this lane's projected bit. + refused(live('s20'), lambda: phi_text(phi(['s_and_b32 s20, s7, exec_lo'], blend[:1], cnd_s))) + phi_text(phi(['s_and_b32 s20, vcc_lo, exec_lo'], blend[:1], cnd_s)) + phi_text(phi(['s_mov_b32 vcc_lo, 0'], ['v_cmp_gt_u32 vcc_lo, 3, v0'], cnd_vcc)) # compare into a zeroed M + phi_text(phi(['s_mov_b32 s20, -1'], ['v_cmp_gt_u32 vcc_lo, 3, v0', 's_orn2_b32 s20, vcc_lo, exec_lo'], cnd_s)) # orn2 + # controls: the same writes without their set-up, an unmasked term, M not the first source, mixed classes, + # M or an operand that is not a lane mask, and a whole-mask read after a preserving block + refused(live('vcc'), lambda: phi_text(phi([], ['v_cmp_gt_u32 vcc_lo, 3, v0'], cnd_vcc))) + refused(live('vcc'), lambda: phi_text(phi(['s_mov_b32 vcc_lo, -1'], ['v_cmp_gt_u32 vcc_lo, 3, v0'], cnd_vcc))) + for zero in ([], z20): + refused(live('s20'), lambda zero=zero: phi_text(phi(zero, ['s_orn2_b32 s20, s7, exec_lo'], cnd_s))) + refused(live('s20'), lambda: phi_text(phi(['s_mov_b32 s20, -1'], ['s_orn2_b32 s20, s7, exec_lo'], cnd_s))) + refused(live('s20'), lambda: phi_text(phi(['s_mov_b32 s20, -1'], ['s_mov_b32 s20, exec_lo'], cnd_s))) + refused(live('s20'), lambda: phi_text(phi(z20, ['s_mov_b32 s20, exec_lo', 's_orn2_b32 s20, s7, exec_lo'], cnd_s))) + refused(live('s20'), lambda: phi_text(phi(z20, [blend[0], *cmp_t, 's_or_b32 s20, s20, s7'], cnd_s))) + refused(live('s20'), lambda: phi_text(phi(z20, [blend[0], 's_and_b32 s22, s7, exec_lo', blend[3]], cnd_s))) + refused(live('s20'), lambda: phi_text(phi(z20, [blend[0], 's_and_b32 s22, s7, s7', blend[3]], cnd_s))) + refused(live('s20'), lambda: phi_text(phi(z20, ['s_andn2_b32 s20, s7, exec_lo'], cnd_s))) + # A compare-derived operand does not make the classes mixable: a zero write (s_mov M, exec_lo) and a ones write + # (s_orn2 with exec) in one block are refused after either setting. `s_andn2 M, M, X` keeps M's skipped bits only for + # X == exec_lo; with a compare mask it clears bits of lanes the model skips. + SPLICE.update({'s_andn2_b32 s20, s20, vcc_lo': '146a148a'}) + mixed_cmp = ['v_cmp_gt_u32 vcc_lo, 3, v0', 's_mov_b32 s20, exec_lo', 's_orn2_b32 s20, vcc_lo, exec_lo'] + for setting in (z20, ['s_mov_b32 s20, -1']): + refused(live('s20'), lambda setting=setting: phi_text(phi(setting, mixed_cmp, cnd_s))) + for setting in (z20, [], ['s_mov_b32 s20, -1']): + refused(live('s20'), lambda setting=setting: phi_text( + phi(setting, ['v_cmp_gt_u32 vcc_lo, 3, v0', 's_andn2_b32 s20, s20, vcc_lo'], cnd_s))) + refused(live('s20'), lambda: phi_text(phi([], blend, cnd_s))) # M unknown before the block + refused(live('s20'), lambda: phi_text(phi(['s_mov_b32 s20, 16'], blend, cnd_s))) # M holds data, not lane bits + for read in (['s_cmp_eq_u32 s20, 0'] + cnd_s, ['s_mov_b32 s7, s20'] + cnd_s): + refused(live('s20'), lambda read=read: phi_text(phi(z20, blend, read))) # An implicit-LOD sample has no derivatives in a compute lane; the level-zero form translates. load = ('s_mov_b64 s[20:21], 0', 's_mov_b64 s[22:23], 0', 's_load_dwordx8 s[24:31], s[10:11], 0x40') sample = 'image_sample v[12:15], v[0:1], s[24:31], s[20:23] dmask:0xf dim:SQ_RSRC_IMG_2D' @@ -1727,6 +1855,11 @@ def check_binary(llvm_mc): 'n-unmasked': ('NM', 's23 = (vcc & exec) & 1;', 's23 = (vcc) & 1;'), # lanes 6 and 7 store with their stale vcc 'o-mask-kept': ('O', 's0 = (s0 & ~exec) & 1;', 's0 = (s0) & 1;'), # lanes 0-2 keep their bit and store 'k-block-entered': ('K', 'pc = ((exec & 1) == 0) ? 2 : 1;', 'pc = 1; exec = 1;'), # skipped waves run it + 'p-dropped-andn2': ('P', 's23 = (s23 & ~exec) & 1;', 's23 = (s23) & 1;'), # an entering lane keeps its old bit + 'p-dropped-or': ('P', 's23 = (s23 | s22) & 1;', 's23 = (s23) & 1;'), # an entering lane loses its compare bit + 'p-block-entered': ('P', 'pc = ((exec & 1) == 0) ? 2 : 1;', 'pc = 1; exec = 1;'), # skipping lanes run the block + 'q-block-entered': ('Q', 'pc = ((exec & 1) == 0) ? 2 : 1;', 'pc = 1; exec = 1;'), + 'q-not-all-ones': ('Q', 's23 = 0xffffffffu;', 's23 = 0u;'), # M not set before the branch: skipped lanes read 0 'g-swapped-offsets': ('G', 'v6 = lds[(a + 20u) >> 2];\n v7 = lds[(a + 144u) >> 2];', 'v6 = lds[(a + 144u) >> 2];\n v7 = lds[(a + 20u) >> 2];'), 'g-overlap': ('G', 'v7 = lds[(a + 144u)', 'v7 = lds[(v6 + 144u)'), diff --git a/tests/startup_gfx2hlsl_const_copy_join_check.py b/tests/startup_gfx2hlsl_const_copy_join_check.py index b1756e1102..c771b7ca27 100644 --- a/tests/startup_gfx2hlsl_const_copy_join_check.py +++ b/tests/startup_gfx2hlsl_const_copy_join_check.py @@ -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-20), and these two cases pin what must +The copy folds the constant into later reads (revision x2w-startup-shader-21), 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; diff --git a/tests/startup_gfx2hlsl_fract_check.py b/tests/startup_gfx2hlsl_fract_check.py index 246f1f6610..8f5a393b38 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-20, after format 50 (R10G10B10A2_UNORM) joined the FORMAT switch', - bool(seed and seed.group(1) == 'x2w-startup-shader-20'), repr(seed and seed.group(1))) +check('the revision is x2w-startup-shader-21, after the lane-mask-preserving execz forms joined the compute lowering', + bool(seed and seed.group(1) == 'x2w-startup-shader-21'), 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 1169681257..514f9d566d 100644 --- a/tests/startup_indirect_draw_indexed.cpp +++ b/tests/startup_indirect_draw_indexed.cpp @@ -295,7 +295,7 @@ ComPtr 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-20"; + constexpr char revision[] = "x2w-startup-shader-21"; 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 8620af940c..c701eb64ec 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-20'))))) + bare == G.fnv(root_blob(), G.fnv(ps_words(4), G.fnv(VS_WORDS, G.fnv(b'x2w-startup-shader-21'))))) 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 979ba9a44f..f750b74376 100644 --- a/tests/startup_resource_retire_title.cpp +++ b/tests/startup_resource_retire_title.cpp @@ -403,7 +403,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-20"; // d3d12_draw.inl cached_translation +constexpr char draw_revision[] = "x2w-startup-shader-21"; // 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 fd44bba115..8c6fccb3b9 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-20 excludes NUL. Stored order is a, b, root, then optional input layout. + # Revision x2w-startup-shader-21 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) == 0x4564b4bb15aa0b9b - assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe") == 0x8a4952a88d16d6e3 + assert gfx2hlsl.pipeline_key(*vector) == 0x93a74e652d95daf6 + assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe") == 0x1ff6b039237188d6 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 178ce07d04..0d1f71559c 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -2785,7 +2785,12 @@ def execz_refusals(self): whose bit is 0 for any lane whose exec bit is clear there: the clear-mask proof keeps exec clear for that lane, and a copy of exec, or X & exec, has that bit clear. M is 0 at the branch on every path (nothing branches or lands between the zeroing and the branch). So for a lane the model skips, the wave's M has that lane's bit 0 whether the wave ran the block or not, which is the - model's M (0); a lane that runs the block runs it in the model too. This holds only while M is read per + model's M (0); a lane that runs the block runs it in the model too. Writes that leave a skipped lane's bit + as it was are accepted for any M: `s_andn2_b32 M, M, exec_lo` and `s_or_b32 M, M, T` where T is an exec-masked + value computed in the block (`s_and_b32 T, X, exec_lo` or a v_cmp result), the blend the compiler emits for an + i1 phi whose other input is a per-lane value. A v_cmp into M (not v_cmpx) needs M zeroed before the branch + like the moves above, and `s_orn2_b32 M, X, exec_lo` needs M set to all ones (`s_mov_b32 M, -1`). Zeroing + and all-ones writes do not mix. This holds only while M is read per lane. A whole-mask read (s_cbranch_vccz/vccnz, s_cmp, a plain copy, a data operand) sees the other lanes' bits, which the one-lane model does not have, so any such read of M reachable after the join, before M is rewritten, keeps the refusal. The reads accepted are the lane select of v_cndmask, the carry-in of v_addc / @@ -3013,34 +3018,61 @@ def waterfall_refusal(self, head, close, effects, live_in, jumps): return None def phi_mask_ok(self, key, b, t, effects, succ, targets, index): - """True when lane mask `key` ('vcc' for vcc_lo, or 'sN'), live at the join t of the s_cbranch_execz at b, is - the zeroed-then-set-from-exec shape execz_refusals accepts (see its docstring): every write of it in the - block is a 32-bit move of exec_lo or an s_and with exec_lo into it alone, the last write before the branch - is `s_mov_b32 M, 0` with no branch and no branch target after it up to the branch, and after the join it is - only read per lane. The caller has checked that the block is straight-line.""" + """True when lane mask `key` ('vcc' for vcc_lo, or 'sN'), live at the join t of the s_cbranch_execz at b, leaves the + bit of every lane the model skips as the wave has it (see execz_refusals), and after the join it is only read per + lane. A lane the model skips has its exec bit clear for the whole block (the exec proof), and the wave runs the block + iff some lane is active, so the model is exact iff the block never changes that lane's bit of M. Every write of M + in the block is one of (mask_write_class), a 32-bit M written alone. Every mask it uses must hold lane bits + (lane_bit_keys, or a block temp): the model keeps a lane's bit at bit 0 where the wave has it at bit `lane`: + 'keep' s_andn2 M, M, exec_lo; s_or M, M, T with T written in the block by s_and with exec_lo or a v_cmp + (bit 0 for a lane whose exec bit is clear): the lane's bit is unchanged, whatever M held before. + 'zero' s_mov M, exec_lo; s_and M, X, exec_lo; a v_cmp into M (VOPC writes 0 for an inactive lane): bit 0, + exact only when M was zero before the branch: the last write before it is `s_mov_b32 M, 0`. + 'ones' s_orn2 M, X, exec_lo: bit 1, exact only when M was all ones before it: `s_mov_b32 M, -1`. + Zeroing or setting M before the branch needs no branch and no branch target after it up to the branch; 'zero' and + 'ones' writes do not mix. Any other write (M reused as an offset) is accepted only if a later 'zero' or 'ones' + write overwrites it, since a 'keep' write would carry its bits for a skipped lane. The caller has checked that the block is straight-line.""" name = 'vcc_lo' if key == 'vcc' else key + classes, temps, stale = set(), set(), False + enc = self.lane_bit_keys(b, effects, targets, index) for i in range(b + 1, t): - if key not in effects[i][2]: - continue + wrote = effects[i][2] _, ops, _ = parse(self.ins[i][3]) base = re.sub(r'_e(32|64)$', '', self.ins[i][2]) - if not ops or ops[0] != name: - return False - if base == 's_mov_b32' and ops[1:] == ['exec_lo']: - continue - if base == 's_and_b32' and len(ops) == 3 and 'exec_lo' in ops[1:]: - continue - return False - # The zeroing: the last instruction before the branch that may write M. - z = next((k for k in range(b - 1, -1, -1) if key in effects[k][2]), None) - if z is None: - return False - _, ops, _ = parse(self.ins[z][3]) - if self.ins[z][2] != 's_mov_b32' or ops != [name, '0']: - return False - if any(self.ins[k][2].startswith(('s_cbranch', 's_branch', 's_setpc')) for k in range(z + 1, b)) or \ - any(index.get(x) in range(z + 1, b + 1) for x in targets): + if key in wrote: + if not ops or ops[0] != name: + return False + kind = self.mask_write_class(base, ops, name, temps, enc) + if kind is None: + stale = True # data in M (an s_movk used as an offset): only a later full overwrite makes it exact + else: + classes.add(kind) + stale = stale and kind == 'keep' + for w in wrote - {key} - EXEC_KEYS: + # T must also hold lane bits: an s_and of exec with a lane mask (not with a group id or other data) + lane_src = base != 's_and_b32' or any( + ('vcc' if x == 'vcc_lo' else x) in temps | enc for x in ops[1:] if x != 'exec_lo') + if self.exec_masked_def(base, ops, w) and lane_src: + temps.add(w) + else: + temps.discard(w) + enc.discard(w) + if stale or ('zero' in classes and 'ones' in classes): return False + if 'keep' in classes and key not in self.lane_bit_keys(b, effects, targets, index): + return False # M's bits before the block must already be lane bits (bit 0 = this lane), not data + need = {'zero': '0', 'ones': '-1'}.get(next(iter(classes & {'zero', 'ones'}), None)) + if need is not None: + # The setting: the last instruction before the branch that may write M. + z = next((k for k in range(b - 1, -1, -1) if key in effects[k][2]), None) + if z is None: + return False + _, ops, _ = parse(self.ins[z][3]) + if self.ins[z][2] not in ('s_mov_b32', 's_movk_i32') or ops != [name, need]: + return False + if any(self.ins[k][2].startswith(('s_cbranch', 's_branch', 's_setpc')) for k in range(z + 1, b)) or \ + any(index.get(x) in range(z + 1, b + 1) for x in targets): + return False # After the join, until M is rewritten, every read must be per lane. seen, todo = set(), [t] while todo: @@ -3055,6 +3087,67 @@ def phi_mask_ok(self, key, b, t, effects, succ, targets, index): todo.extend(succ[i]) return True + def lane_bit_keys(self, b, effects, targets, index): + """Keys ('vcc' or 'sN') whose value at the s_cbranch_execz at b holds the lane's own bit as bit 0, the encoding the + one-lane model gives a lane mask: the last write before b is a v_cmp, an s_and with exec_lo, a copy of exec_lo, or + 0 / -1 (every bit alike), with no branch or branch target between it and b. A value from anywhere else (a group id, + an offset, `s_mov_b32 M, 16`) has the wave's lane bit at bit `lane`, which the model reads from bit 0.""" + entries = {index.get(target) for target in targets} + start = next((k + 1 for k in range(b - 1, -1, -1) + if self.ins[k][2].startswith(('s_cbranch', 's_branch', 's_setpc')) + or k + 1 in entries), 0) + found = set() + for k in range(start, b): + _, ops, _ = parse(self.ins[k][3]) + base = re.sub(r'_e(32|64)$', '', self.ins[k][2]) + writes = effects[k][2] - EXEC_KEYS + proven = set() + for w in writes: + name = 'vcc_lo' if w == 'vcc' else w + masked = self.exec_masked_def(base, ops, w) + if masked and base == 's_and_b32': + other = ops[2] if ops[1] == 'exec_lo' else ops[1] + key = 'vcc' if other == 'vcc_lo' else other + masked = key in found or other in ('0', '-1', 'exec_lo') + if masked or (base in ('s_mov_b32', 's_movk_i32') and len(ops) == 2 + and ops[0] == name and ops[1] in ('0', '-1', 'exec_lo')): + proven.add(w) + found.difference_update(writes) + found.update(proven) + return found + + @staticmethod + def exec_masked_def(base, ops, key): + """True when this instruction writes `key` ('sN' or 'vcc') with a value whose bit is 0 for a lane whose exec bit is + clear: s_and with exec_lo into it, or a v_cmp (not v_cmpx) into it.""" + name = 'vcc_lo' if key == 'vcc' else key + if not ops or ops[0] != name: + return False + if base == 's_and_b32': + return len(ops) == 3 and 'exec_lo' in ops[1:] + return base.startswith('v_cmp') and not base.startswith('v_cmpx') + + @staticmethod + def mask_write_class(base, ops, name, temps, enc=frozenset()): + """'keep', 'zero' or 'ones' for a write of mask `name` inside an execz block (see phi_mask_ok), else None.""" + key = 'vcc' if name == 'vcc_lo' else name + if base == 's_mov_b32' and ops[1:] == ['exec_lo']: + return 'zero' + lane = lambda x: ('vcc' if x == 'vcc_lo' else x) in temps | enc # a lane mask: bit 0 is this lane's bit + if base == 's_and_b32' and len(ops) == 3 and 'exec_lo' in ops[1:] and lane(ops[2] if ops[1] == 'exec_lo' else ops[1]): + return 'zero' + if base.startswith('v_cmp') and not base.startswith('v_cmpx'): + return 'zero' + if base == 's_orn2_b32' and len(ops) == 3 and ops[2] == 'exec_lo' and lane(ops[1]): + return 'ones' + if base == 's_andn2_b32' and ops == [name, name, 'exec_lo']: + return 'keep' + if base == 's_or_b32' and len(ops) == 3 and name in ops[1:]: + other = ops[2] if ops[1] == name else ops[1] + if other != name and ('vcc' if other == 'vcc_lo' else other) in temps: + return 'keep' + return None + def per_lane_read(self, i, key): """True when instruction i reads mask `key` only as a lane select or carry, or into exec (s_and_saveexec, s_and with exec): the lane's own bit is all it uses.""" @@ -9862,7 +9955,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-20')))) + key = fnv(root_blob, fnv(ps_code, fnv(vs_code, fnv(b'x2w-startup-shader-21')))) key = fnv(layout, key) if layout else key return key if ps_inputs is None else fnv(b'psin1' + struct.pack('