Skip to content

shader: clear the lane-mask mark on scalar writes of descriptor data in gfx2hlsl (Part of #1723) - #2795

Open
Miffedrandir wants to merge 3 commits into
R0Y00:mainfrom
Miffedrandir:startup/descriptor-alu-clears-lane-mask
Open

Miffedrandir wants to merge 3 commits into
R0Y00:mainfrom
Miffedrandir:startup/descriptor-alu-clears-lane-mask

Conversation

@Miffedrandir

Copy link
Copy Markdown

Change

Part of #1723.

Scalar ALU on descriptor data in tools/startup/gfx2hlsl.py records only a symbol for its destination and never calls set_s. That covers an s_mov of a descriptor word, a field extract or mask of one (the structured-to-raw conversion of #2546), and an s_add_u32 to a descriptor pointer through pointer_add. Since #2122, set_s is what clears a register's lane-mask mark, so these writes kept a mark left by an earlier v_cmp/saveexec into the same SGPR.

Since #2702, a scalar instruction that reads a lane mask as a number is refused. So the stride clamp of a structured-to-raw conversion in a register that once held a mask was refused, even though the register holds a descriptor field:

s_bfe_u32 s23, s45, 0xe0010      ; DW1[29:16] stride of a buffer descriptor
s_max_u32 s23, s23, 1            ; refused: s_max_u32 reads lane mask s23 as numeric data

The descriptor-data path now clears the mark (and the wave64 high-word mark) on its destination SGPRs before writing them. lower() classifies the sources before _lower() runs, so an instruction that itself reads a mask as a number is still refused. Every later branch of the path either writes the destination or refuses the program. The change is 8 lines in one place. It closes the gap the existing docs section listed as not covered ("the other symbol-only scalar writes ... still keep a mark they find").

Verification

Base: 700522ef (upstream main). Windows 11 10.0.26200, Python 3.11 venv from requirements-dev.txt. All checks below are pure Python; no GPU involved.

  • python -B tests/startup_gfx2hlsl_mask_overwrite_check.py: all checks ok. New cases, each after v_cmp_eq_u32_e64 s6, v0, v1, in a pixel and a vertex stage:
    • s_mov_b32 s6, s17, s_bfe_u32 s6, s17, 0xe0010, s_and_b32 s6, s19, 0xcfffffff and s_add_u32 s6, s2, 16 on descriptor or pointer symbols clear the mark on s6.
    • s_bfe_u32 s6, s17, 0xe0010 then s_max_u32 s6, s6, 1 is not refused.
  • Control: s_bfe_u32 s8, s17, 0xe0010 keeps the mark on s6 (only the destination is cleared).
  • Negative control: the same test run against the unpatched tools/startup/gfx2hlsl.py at 700522ef fails 6 checks: the s_bfe_u32 and s_add_u32 clearing checks and the stride clamp (s_max_u32 reads lane mask s6 as numeric data), each in both stages. The s_mov_b32 and s_and_b32 cases already pass there.
  • ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_mask_overwrite_controls$': 1/1 passed.
  • The Linux CI Python list from windows.yml (203 checks) via tools/run_checks_parallel.py -j 12:
    • 200 ok.
    • 2 capability skips (startup_run_sh_check, startup_build_rev_check).
    • 1 failure, dxc_cache_check (OSError: [WinError 193]). It fails the same way on unpatched main on this machine (a local DXC setup issue), unrelated to this change.
  • docs_links_check and bidi_controls_check: ok.

How it was found: local integration of open PRs

I found this while checking how much a combination of the open translator PRs would help locally captured shader requests (local data, not included; about 3,800 requests, 585 graphics pipelines). The integration branch was b457f7ff plus a --no-ff merge of each of these PR heads, re-keyed with tools/startup/shader_cache_rekey.py and translated with gfx2hlsl.py --pending:

PR Result in the local integration
#2736 sample_b / get_lod past a divergent EXEC (now merged) merged cleanly
#2741 binary16 VOPC compares merged cleanly
#2760 image_gather4_lz with wrap addressing merged cleanly
#2763 bind the resource-heap image record of a pixel draw merged cleanly (contains #2761)
#2761 pixel-stage resource-heap image record already contained in #2763
#2754 loop around v_interp_p1ll_f16 / p2_f16 (now merged) merged cleanly
#2747 s_wqm exec, exec on a narrowed EXEC as a quad vote conflicts only in tests (startup_image_get_lod_flow_check.py, startup_image_sample_b_flow_check.py); PR side taken
#2758 per-lane sample_b bias past a divergent EXEC same two test files; PR side taken
#2702 refuse a lane mask read as numeric data (now merged) not merged: conflicts in gfx2hlsl.py and runtime sources
#2745 v_readfirstlane_b32 of a per-lane VGPR dropped: merged with its test side, but 3 checks on main then failed (startup_gfx2hlsl_readfirstlane_uniform_check, startup_compute_readfirstlane_check, startup_gfx2hlsl_compute_check), because main's newer tests expect refusals the PR accepts

In that integration, 116 graphics pipelines stopped first at s_max_u32 reads lane mask sN as numeric data. With this patch none do. They then stop at their next refusal, mostly DPP forms (v_min_u32_dpp, v_subrev_f32_dpp) in pixel programs with branches, so the count of translated graphics pipelines does not change (89 of 585). This PR is based on and verified against main only. It does not depend on any of the PRs above.

Evidence and follow-up

  • No new fixture files. The cases are synthetic instruction text and register states in the existing check.
  • Not covered: a draw through a translated pipeline.
  • Follow-up another contributor could pick up: DPP quad reads (v_min_u32_dpp and similar) in pixel programs with non-uniform branches are the most common first refusal for the remaining graphics pipelines in the local capture (about 180 of 311 missing).

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed (docs/experiments.md, "Startup translator: lane-mask marks after a scalar overwrite").
  • I did not include game files, dumps, distribution links or secrets.

This change was AI-assisted (Claude Code).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Sir6MraM8AjP6AcLDr3paU

Miffedrandir and others added 2 commits November 26, 2026 09:57
…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 R0Y00#1723

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sir6MraM8AjP6AcLDr3paU

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head eb86524 (comment only, no approval).

What is good

  • The fix is small and in the right place: tools/startup/gfx2hlsl.py:5313 clears masks and mask_high on the destination SGPRs of the descriptor-data path. lower() classifies the sources first, so an instruction that really reads a mask as a number is still refused.
  • The new checks in tests/startup_gfx2hlsl_mask_overwrite_check.py cover ps and vs, the four scalar forms (s_mov, s_bfe_u32, s_and_b32, s_add_u32 pointer add), the s_bfe_u32 + s_max_u32 stride clamp, and a control (write into s8 keeps the mark on s6).
  • Mutation: replacing the masks discard with pass makes the check fail (6 failures). The control and the refusal tests from #2122/#2702 still pass.

What is missing

  • No real D3D12 WARP readback vs CPU reference. Before this change the stride clamp program was refused; now it translates, so a new HLSL path is emitted for it (s_bfe_u32 then s_max_u32 on a register that earlier held a v_cmp result). The current tests only check Python mask bookkeeping. Please add one case to an existing WARP-based gfx2hlsl check (for example next to tests/startup_gfx2hlsl_desc_vcc_check.py): compare, then s_bfe_u32 of a descriptor word, then s_max_u32 with 1, store the result, read it back from WARP and compare it with a CPU value. A control where the clamp reads a real mask must still be refused.
  • Overlap to settle: #2784 (a 32-bit integer scalar result ends the lane-mask tag of its register) touches the same mark-clearing area. Different slice (numeric results vs descriptor data) but please confirm with @R0Y00 / the #2784 author that the two do not collide in set_s and the descriptor path.

Checks run locally on the worktree at this head (0 commits behind main):

  • python3 tests/startup_gfx2hlsl_mask_overwrite_check.py -> ok
  • python3 tests/docs_links_check.py -> ok
  • python3 tests/bidi_controls_check.py -> ok
  • git diff --check origin/main...HEAD -> clean
  • one code mutation of the new line -> new check fails as expected

CI was still pending at review time and no Windows run was done, so this is not a merge decision. Merge stays after the WARP readback case is added.

AI-assisted; commands listed were executed locally.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sir6MraM8AjP6AcLDr3paU
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants