From 6030f310d5ad836acfb410dd28d36a8965def0e6 Mon Sep 17 00:00:00 2001 From: AlastairHatfield Date: Thu, 26 Nov 2026 09:57:45 +0000 Subject: [PATCH 1/4] shader: clear the lane-mask mark on scalar writes of descriptor data in gfx2hlsl Scalar ALU on descriptor data (an s_mov of a descriptor word, a field extract or mask of one, an s_add_u32 to a descriptor pointer) records only a symbol for its destination and skipped set_s, so the destination kept a lane-mask mark from an earlier write. The stride clamp of a structured-to-raw conversion (s_bfe_u32 sN, desc, 0xe0010 then s_max_u32 sN, sN, 1) then read as a lane-mask read. The descriptor-data path now clears the mark on its destination SGPRs before writing them; lower() has already classified the sources, so a mask read by the same instruction is treated as before. Part of #1723 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Sir6MraM8AjP6AcLDr3paU --- docs/experiments.md | 4 ++- .../startup_gfx2hlsl_mask_overwrite_check.py | 36 +++++++++++++++++++ tools/startup/gfx2hlsl.py | 8 +++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/docs/experiments.md b/docs/experiments.md index 9a97ab9d25..2925bc1a69 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -14872,7 +14872,9 @@ ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cselect_mask_controls$ `tools/startup/gfx2hlsl.py` records the SGPRs that hold a lane mask (a `v_cmp` destination, a saveexec copy) in `Stage.masks`, and lowers a scalar logic op with a mask source as a mask op whose result is the lane's bit. The generic scalar path cleared the mark when it wrote ordinary data into such a register, but the other scalar writers did not. After `v_cmp_eq_u32_e64 s6, v0, v1` and `s_buffer_load_dword s6, s[16:19], 0x0`, s6 holds a buffer word, yet `s_or_b32 s8, s6, s7` was lowered as `s8 = (s6 | s7) & 1;`. Every write through `set_s` now clears the mark, the writers of a mask mark the register again after the write, and `s_load` clears the mark on its destination registers, also in the forms that only record a descriptor symbol. -`tests/startup_gfx2hlsl_mask_overwrite_check.py` writes a mask with a compare, overwrites it with an `s_buffer_load`, an `s_load` through a root pointer, an `s_load` of a descriptor or an `s_cselect_b32`, and evaluates the following `s_or_b32` on a buffer word with the restricted evaluator of `startup_gfx2hlsl_bit_alu_check.py`. Controls: without the overwrite the OR stays a mask OR, and a compare or a mask logic op after a load marks the register again. On the translator before this change seven checks fail (the mark stays and the OR is cut to bit 0). Test: `startup_gfx2hlsl_mask_overwrite_controls`. Not covered: a draw through a translated pipeline; the other symbol-only scalar writes (for example an `s_mov` of a descriptor) still keep a mark they find. +`tests/startup_gfx2hlsl_mask_overwrite_check.py` writes a mask with a compare, overwrites it with an `s_buffer_load`, an `s_load` through a root pointer, an `s_load` of a descriptor or an `s_cselect_b32`, and evaluates the following `s_or_b32` on a buffer word with the restricted evaluator of `startup_gfx2hlsl_bit_alu_check.py`. Controls: without the overwrite the OR stays a mask OR, and a compare or a mask logic op after a load marks the register again. On the translator before this change seven checks fail (the mark stays and the OR is cut to bit 0). Test: `startup_gfx2hlsl_mask_overwrite_controls`. Not covered: a draw through a translated pipeline. + +Scalar ALU on descriptor data (an `s_mov` of a descriptor word, a field extract or mask of one, an `s_add_u32` to a descriptor pointer) records only a symbol for its destination and did not clear the mark. A register that had held a mask and then a descriptor field kept the mark, so in a captured vertex program the stride clamp of a structured-to-raw conversion (`s_bfe_u32 s23, s45, 0xe0010`, then `s_max_u32 s23, s23, 1`) read as a lane-mask read. The descriptor-data path now clears the mark on its destination SGPRs before it writes them. The sources are classified before that, so a mask read by the same instruction is treated as before. The check runs these four writes after a compare in a pixel and a vertex stage and the clamp after the extract; a descriptor write into another SGPR keeps the mark on s6 (control). On the translator before this change the eight clearing checks fail. ```powershell ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_mask_overwrite_controls$' -V diff --git a/tests/startup_gfx2hlsl_mask_overwrite_check.py b/tests/startup_gfx2hlsl_mask_overwrite_check.py index 6dc037cc62..b6840bdbed 100644 --- a/tests/startup_gfx2hlsl_mask_overwrite_check.py +++ b/tests/startup_gfx2hlsl_mask_overwrite_check.py @@ -14,6 +14,11 @@ buffer word with the restricted evaluator of startup_gfx2hlsl_bit_alu_check: an s_buffer_load, an s_load through a root pointer, an s_load of a descriptor, and an s_cselect_b32 into the former mask register. Controls: without the overwrite the OR stays a mask OR, and a compare or a mask logic op written after a load marks the register again. + +Scalar ALU on descriptor data (an s_mov of a descriptor word, the s_bfe_u32 and s_and_b32 of a structured-to-raw +conversion, an s_add_u32 to a descriptor pointer) records only a symbol for its destination. It now clears the mark +as well, in a pixel and a vertex stage, so the s_max_u32 that clamps an extracted stride is not taken as a mask read. +Control: such a write into another SGPR keeps the mark on s6. """ import struct import sys @@ -39,6 +44,9 @@ class Pipe: sampler_heap = False root = {'params': [{'type': 2}], 'flags': 0} + def __init__(self): + self.modified = set() # descriptors written by scalar ALU (the structured-to-raw conversion marks them) + def resource(self, key, kind): return 'r0' @@ -104,6 +112,34 @@ def main(): run(s, 'v_cmp_eq_u32_e64 s5, v0, v1', 's_load_dwordx4 s[4:7], s[12:13], 0x20') check('s_load_dwordx4 of a descriptor clears the mark on s5', 5 not in s.masks, s.masks) + # Scalar ALU on descriptor data writes the destination's symbol without set_s. The register then holds + # descriptor data, so a later numeric read of it is not a lane-mask read (the stride clamp of a + # structured-to-raw conversion: s_bfe_u32 of DW1[29:16], then s_max_u32 with 1). + for kind in ('ps', 'vs'): + for text in ('s_mov_b32 s6, s17', 's_bfe_u32 s6, s17, 0xe0010', 's_and_b32 s6, s19, 0xcfffffff', + 's_add_u32 s6, s2, 16'): + s = stage() + s.kind = kind + try: + run(s, COMPARE, text) + except G.Unsupported as error: + check('%s %s after a compare translates' % (kind, text), False, error) + continue + check('%s %s (descriptor data) clears the mark on s6' % (kind, text), 6 not in s.masks, s.masks) + s = stage() + s.kind = kind + try: + run(s, COMPARE, 's_bfe_u32 s6, s17, 0xe0010', 's_max_u32 s6, s6, 1') + check('%s s_max_u32 of the extracted stride is not refused as a lane-mask read' % kind, True) + except G.Unsupported as error: + check('%s s_max_u32 of the extracted stride is not refused as a lane-mask read' % kind, False, error) + # Control: only the destination loses its mark; a descriptor write to another SGPR leaves the mask s6. + s = stage() + s.kind = kind + run(s, COMPARE, 's_bfe_u32 s8, s17, 0xe0010') + check('control: %s s_bfe_u32 into s8 keeps the mask mark on s6' % kind, 6 in s.masks and 8 not in s.masks, + s.masks) + # A mask written after a load marks the register again. s = stage() run(s, 's_buffer_load_dword s6, s[16:19], 0x0', COMPARE) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 44c3b40ccc..d5d3e5a1c3 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -5310,6 +5310,14 @@ def src_sym(t): raise Unsupported('scc from descriptor data') if word_pack: raise Unsupported('%s on descriptor data' % base) + # Every path below writes the destination's symbol directly, not through set_s, or refuses. The + # written word is descriptor data, so a lane-mask mark the SGPR held from an earlier write ends here as + # set_s ends it. lower() has already classified the sources, so a mask read by this instruction is + # still refused. + if dst and dst[0] == 's': + for reg in range(dst[1], dst[2] + 1): + getattr(self, 'masks', set()).discard(reg) + getattr(self, 'mask_high', set()).discard(reg) if self.pointer_add(base, dname, srcs, syms): return None if dname == 'm0': From eb8652480e8df977d3016751a0acbf61fe2066e2 Mon Sep 17 00:00:00 2001 From: AlastairHatfield Date: Thu, 26 Nov 2026 09:59:10 +0000 Subject: [PATCH 2/4] docs: state the control result on the current translator Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Sir6MraM8AjP6AcLDr3paU --- docs/experiments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/experiments.md b/docs/experiments.md index 2925bc1a69..2511a535c1 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -14874,7 +14874,7 @@ ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cselect_mask_controls$ `tests/startup_gfx2hlsl_mask_overwrite_check.py` writes a mask with a compare, overwrites it with an `s_buffer_load`, an `s_load` through a root pointer, an `s_load` of a descriptor or an `s_cselect_b32`, and evaluates the following `s_or_b32` on a buffer word with the restricted evaluator of `startup_gfx2hlsl_bit_alu_check.py`. Controls: without the overwrite the OR stays a mask OR, and a compare or a mask logic op after a load marks the register again. On the translator before this change seven checks fail (the mark stays and the OR is cut to bit 0). Test: `startup_gfx2hlsl_mask_overwrite_controls`. Not covered: a draw through a translated pipeline. -Scalar ALU on descriptor data (an `s_mov` of a descriptor word, a field extract or mask of one, an `s_add_u32` to a descriptor pointer) records only a symbol for its destination and did not clear the mark. A register that had held a mask and then a descriptor field kept the mark, so in a captured vertex program the stride clamp of a structured-to-raw conversion (`s_bfe_u32 s23, s45, 0xe0010`, then `s_max_u32 s23, s23, 1`) read as a lane-mask read. The descriptor-data path now clears the mark on its destination SGPRs before it writes them. The sources are classified before that, so a mask read by the same instruction is treated as before. The check runs these four writes after a compare in a pixel and a vertex stage and the clamp after the extract; a descriptor write into another SGPR keeps the mark on s6 (control). On the translator before this change the eight clearing checks fail. +Scalar ALU on descriptor data (an `s_mov` of a descriptor word, a field extract or mask of one, an `s_add_u32` to a descriptor pointer) records only a symbol for its destination and did not clear the mark. A register that had held a mask and then a descriptor field kept the mark, so in a captured vertex program the stride clamp of a structured-to-raw conversion (`s_bfe_u32 s23, s45, 0xe0010`, then `s_max_u32 s23, s23, 1`) read as a lane-mask read. The descriptor-data path now clears the mark on its destination SGPRs before it writes them. The sources are classified before that, so a mask read by the same instruction is treated as before. The check runs these four writes after a compare in a pixel and a vertex stage and the clamp after the extract; a descriptor write into another SGPR keeps the mark on s6 (control). On the translator before this change (700522ef) six checks fail: the `s_bfe_u32` and `s_add_u32` clearing checks and the stride clamp, which is refused as `s_max_u32 reads lane mask s6 as numeric data`, each in both stages. ```powershell ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_mask_overwrite_controls$' -V From 802dc26f5ba1929b706f6999d5362e39a39ea0f3 Mon Sep 17 00:00:00 2001 From: AlastairHatfield Date: Thu, 26 Nov 2026 10:15:27 +0000 Subject: [PATCH 3/4] docs: the captured stride clamp is in a pixel program Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Sir6MraM8AjP6AcLDr3paU --- docs/experiments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/experiments.md b/docs/experiments.md index 2511a535c1..f10e7536d1 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -14874,7 +14874,7 @@ ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cselect_mask_controls$ `tests/startup_gfx2hlsl_mask_overwrite_check.py` writes a mask with a compare, overwrites it with an `s_buffer_load`, an `s_load` through a root pointer, an `s_load` of a descriptor or an `s_cselect_b32`, and evaluates the following `s_or_b32` on a buffer word with the restricted evaluator of `startup_gfx2hlsl_bit_alu_check.py`. Controls: without the overwrite the OR stays a mask OR, and a compare or a mask logic op after a load marks the register again. On the translator before this change seven checks fail (the mark stays and the OR is cut to bit 0). Test: `startup_gfx2hlsl_mask_overwrite_controls`. Not covered: a draw through a translated pipeline. -Scalar ALU on descriptor data (an `s_mov` of a descriptor word, a field extract or mask of one, an `s_add_u32` to a descriptor pointer) records only a symbol for its destination and did not clear the mark. A register that had held a mask and then a descriptor field kept the mark, so in a captured vertex program the stride clamp of a structured-to-raw conversion (`s_bfe_u32 s23, s45, 0xe0010`, then `s_max_u32 s23, s23, 1`) read as a lane-mask read. The descriptor-data path now clears the mark on its destination SGPRs before it writes them. The sources are classified before that, so a mask read by the same instruction is treated as before. The check runs these four writes after a compare in a pixel and a vertex stage and the clamp after the extract; a descriptor write into another SGPR keeps the mark on s6 (control). On the translator before this change (700522ef) six checks fail: the `s_bfe_u32` and `s_add_u32` clearing checks and the stride clamp, which is refused as `s_max_u32 reads lane mask s6 as numeric data`, each in both stages. +Scalar ALU on descriptor data (an `s_mov` of a descriptor word, a field extract or mask of one, an `s_add_u32` to a descriptor pointer) records only a symbol for its destination and did not clear the mark. A register that had held a mask and then a descriptor field kept the mark, so in a captured pixel program the stride clamp of a structured-to-raw conversion (`s_bfe_u32 s23, s45, 0xe0010`, then `s_max_u32 s23, s23, 1`) read as a lane-mask read. The descriptor-data path now clears the mark on its destination SGPRs before it writes them. The sources are classified before that, so a mask read by the same instruction is treated as before. The check runs these four writes after a compare in a pixel and a vertex stage and the clamp after the extract; a descriptor write into another SGPR keeps the mark on s6 (control). On the translator before this change (700522ef) six checks fail: the `s_bfe_u32` and `s_add_u32` clearing checks and the stride clamp, which is refused as `s_max_u32 reads lane mask s6 as numeric data`, each in both stages. ```powershell ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_mask_overwrite_controls$' -V From 6b08636e23efe6964decf71bf88a6e9b3f02b995 Mon Sep 17 00:00:00 2001 From: AlastairHatfield Date: Thu, 26 Nov 2026 11:58:58 +0000 Subject: [PATCH 4/4] tests: dispatch the clamped structured-to-raw store after a lane mask in its register Case I-raw-mask of startup_gfx2hlsl_compute_check writes a lane mask into s21 with v_cmp_gt_u32_e64, then extracts and clamps the store descriptor's stride into s21 and stores as case A. The WARP and hardware dispatches compare all 320 words with the CPU reference. Control: s_max_u32 of the mask itself stays refused. On the translator without the descriptor-data mark clear, the case is refused. Co-Authored-By: Claude Opus 5.5 --- docs/experiments.md | 3 +++ tests/startup_gfx2hlsl_compute_check.py | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/experiments.md b/docs/experiments.md index f10e7536d1..83c41a6035 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -14876,8 +14876,11 @@ ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cselect_mask_controls$ Scalar ALU on descriptor data (an `s_mov` of a descriptor word, a field extract or mask of one, an `s_add_u32` to a descriptor pointer) records only a symbol for its destination and did not clear the mark. A register that had held a mask and then a descriptor field kept the mark, so in a captured pixel program the stride clamp of a structured-to-raw conversion (`s_bfe_u32 s23, s45, 0xe0010`, then `s_max_u32 s23, s23, 1`) read as a lane-mask read. The descriptor-data path now clears the mark on its destination SGPRs before it writes them. The sources are classified before that, so a mask read by the same instruction is treated as before. The check runs these four writes after a compare in a pixel and a vertex stage and the clamp after the extract; a descriptor write into another SGPR keeps the mark on s6 (control). On the translator before this change (700522ef) six checks fail: the `s_bfe_u32` and `s_add_u32` clearing checks and the stride clamp, which is refused as `s_max_u32 reads lane mask s6 as numeric data`, each in both stages. +The extract and the clamp emit no HLSL: the clamped stride is a symbol, and a numeric read of it is refused as before (`numeric use of s6 holding descnum`), with or without the earlier mask. It changes output only where it feeds the structured-to-raw conversion of a buffer store, which is recognized in compute and vertex programs. Case I-raw-mask of `tests/startup_gfx2hlsl_compute_check.py` covers that on the GPU: `v_cmp_gt_u32_e64 s21, 3, v0` in a 24-lane group, then the clamped conversion into s21 and the store of case A. The dispatch compares all 320 words with the CPU reference. The translation differs from case I-raw only by the dead compare. Control: `s_max_u32 s21, s21, 1` straight after the compare is still refused. On the translator before this change the case is refused (`s_max_u32 reads lane mask s21 as numeric data`), so the controls and the dispatch fail. + ```powershell ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_mask_overwrite_controls$' -V +ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_compute_(controls|warp|hardware)$' -V ``` ## Startup translator: lane masks used as vector data diff --git a/tests/startup_gfx2hlsl_compute_check.py b/tests/startup_gfx2hlsl_compute_check.py index 06e1173b0e..bf95e4b06b 100644 --- a/tests/startup_gfx2hlsl_compute_check.py +++ b/tests/startup_gfx2hlsl_compute_check.py @@ -723,6 +723,7 @@ def signed(x): 's_bfe_u32 s21, s29, 0xe0010': '1dff959310000e00', 's_max_u32 s21, s21, 1': '15819584', 's_max_u32 s21, s21, 2': '15829584', + 'v_cmp_gt_u32_e64 s21, 3, v0': '1500c4d483000200', 's_bfe_u32 s21, s17, 0xe0011': '11ff959311000e00', 's_mul_i32 s18, s18, s21': '12151293', 's_mul_i32 s18, s18, 4': '12841293', @@ -769,6 +770,15 @@ def replace_store(*lines): CASES['I-raw'] = (SOURCE_A.replace('buffer_store_dword', '\n'.join((*RAW_CONVERSION, 'buffer_store_dword'))), splice(*RAW_CONVERSION), *CASES['A'][2:]) +# Case I-raw-mask: s21 first holds a lane mask (a compare in a 24-lane group), then the clamped stride of the same +# conversion. The descriptor-data write ends the mark, so the clamp is not a lane-mask read and the store lands as +# in case A. The translator before that change refused it: s_max_u32 reads lane mask s21 as numeric data. +MASK_S21 = 'v_cmp_gt_u32_e64 s21, 3, v0' +CLAMPED_CONVERSION = (STRIDE_EXTRACT, 's_max_u32 s21, s21, 1', *RAW_CONVERSION[1:]) +CASES['I-raw-mask'] = (SOURCE_A.replace('buffer_store_dword', + '\n'.join((MASK_S21, *CLAMPED_CONVERSION, 'buffer_store_dword'))), + splice(MASK_S21, *CLAMPED_CONVERSION), *CASES['A'][2:]) + # Replace the nonzero stride constant in s5 with the first active lane of a # fresh 24-thread group. Ignoring FF1 leaves a different word at every i > 0. CASES['A-ff1'] = ('s_ff1_i32_b32 s5, exec_lo\n' + SOURCE_A, @@ -873,7 +883,7 @@ def formatted_input(): def expected(name): - if name in ('I', 'I-raw'): + if name in ('I', 'I-raw', 'I-raw-mask'): return expected('A') out = [SENTINEL] * WORDS if name == 'join-load': @@ -1711,6 +1721,13 @@ def check_controls(): # Captured programs clamp the extracted stride to at least 1 before scaling NUM_RECORDS. clamped = (STRIDE_EXTRACT, 's_max_u32 s21, s21, 1', *RAW_CONVERSION[1:]) assert translate('A', code=splice(*clamped)).hlsl_cs() == raw.hlsl_cs() + # After a lane mask in s21 the clamped conversion still matches case I-raw apart from the compare itself; the + # dispatch of case I-raw-mask stores through it. Control: a clamp that reads the mask itself stays refused. + masked = translate('I-raw-mask').hlsl_cs().splitlines() + assert [line for line in masked if line not in raw.hlsl_cs().splitlines()] == [ + ' s21 = ((0x00000003u > v0) ? 1u : 0u);'], masked + refused('s_max_u32 reads lane mask s21 as numeric data', lambda: translate( + 'A', code=splice(MASK_S21, 's_max_u32 s21, s21, 1', *RAW_CONVERSION[1:])).hlsl_cs()) refused('buffer store through a modified descriptor', lambda: translate( 'A', code=splice(STRIDE_EXTRACT, 's_max_u32 s21, s21, 2', *RAW_CONVERSION[1:])).hlsl_cs()) reload = ('s_load_dwordx8 s[24:31], s[10:11], 0x0', 's_waitcnt lgkmcnt(0)')