Skip to content

startup: prove v_med3_f32 results uniform for v_readfirstlane_b32 - #2198

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-readfirstlane-med3
Nov 25, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-readfirstlane-med3

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Change

Part of #1723. Follows #1931, #2153 and #2183.

The uniformity proof behind v_readfirstlane_b32 follows a list of lane-pure VALU operations (UNIFORM_VALU). v_med3_f32 was not in it. Compiled pixel shaders clamp a scalar with a median of three and read the result back, for example:

v_med3_f32 v7, s2, 0, 0.5
v_readfirstlane_b32 s9, v7

The median is a per-lane function of its three inputs (med3() in the emitted HLSL), like the multiply and fused multiply-add already in the list. This change adds it. Source and output modifiers follow the existing float rules: negation, absolute value and clamp. Nothing else changes.

Where it applies: in branch-free code, and in the entry block of a program with branches (#2183). Past the entry block, the read still needs a further rule, which this PR does not add.

Open PR #2095 changes the v_med3_f32 lowering for a NaN source (med3_32()). The operation stays a per-lane function, so the proof is unaffected.

Verification

On fd457299 (origin/main) merged into this branch, head 5ba0b508:

  • python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py: Readfirstlane uniformity checks: 0 failed.
  • python -B tests/docs_links_check.py: passes.
  • git diff --check origin/main...HEAD: clean. The changed files are LF in the index.
  • Mutant: removing v_med3_f32 from the set fails "median of three" and "median of three scalar sources". The existing negative control, which empties the set, also covers both.

Local measurement, not reproducible from the repository: on its own, this changes none of 383 locally cached requests, and the 75 that translated keep byte-identical HLSL. In one full-screen pixel pipeline from a captured frame, three reads have this form. They sit past the entry block of a branched program, so they also need the further rule mentioned above.

Evidence and follow-up

The fixture is hand-written disassembly text. It is not taken from any title.

  • Accepted: the [0, 0.5] clamp form, and a form with negated, absolute and clamped scalar sources.
  • Refused: a median with a lane-varying source, and a median with a lane-mask source.

docs/experiments.md (uniform sources of v_readfirstlane_b32) lists the operation. Reproduce with:

ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_readfirstlane_uniform$' -V

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.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code). All commands above were run locally.

🤖 Generated with Claude Code

NiiRoZz and others added 2 commits November 25, 2026 03:54
The uniformity proof behind v_readfirstlane_b32 did not follow
v_med3_f32, so a scalar clamped with a median of three (for example
v_med3_f32 v14, s44, 0, 0.5) never reached a proven read. The median is
a per-lane function of its three inputs, like the multiply and
fused multiply-add already in the set.

The check accepts the clamp form and a form with negated, absolute
and clamped scalar sources. It refuses a median with a lane-varying
source or a lane-mask source.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…firstlane-med3

# Conflicts:
#	docs/experiments.md

@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 5ba0b508fa351ed5b271b3819564b7f28ac0dc57 (comment only).

Summary: Adds v_med3_f32 to UNIFORM_VALU so v_readfirstlane_b32 can read its result when all three sources are uniform. The change is correct and small. No P1 or P2 findings.

Checked

  • v_med3_f32 is a per-lane function of its three inputs (med3() helper, tools/startup/gfx2hlsl.py:2556). It reads no lane id or other lane, so the uniformity proof holds.
  • v_med3_f32 is in VEC_F (gfx2hlsl.py:2307), so the existing -/|x| source-modifier stripping and the clamp/mul:2/mul:4/div:2 output-modifier allowlist apply to it, like the other float ops. All three sources must be uniform (all(self.uniform_source(tok) ...)), so a varying third source is refused.
  • Tests: two accepted cases (plain, and with negation/abs/clamp) and two refusals (varying VGPR source, lane-mask source). Docs sentence in docs/experiments.md matches the code.
  • Clean-room and neutral title: OK. Mergeable. Linux Python CI check passes; Windows and fixture jobs were still pending when I looked.

Commands run locally (worktree at the head above)

  • python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py -> Readfirstlane uniformity checks: 0 failed
  • python3 tests/docs_links_check.py -> Checked 18 Markdown files and 405 links.
  • python3 tests/bidi_controls_check.py -> no bidirectional controls.
  • git diff --check origin/main...HEAD -> clean
  • Mutation: removed 'v_med3_f32' from UNIFORM_VALU -> the check reports 2 failed (both new accepted cases are caught). Reverted with git checkout -- ..

Notes

  • nit: the refusal tests do not cover a med3 chain across an exec write or past the entry block. Those paths are shared with the other listed ops, so this is not blocking.
  • No Windows run: the change is a table entry plus Linux-testable translation, with no new HLSL emitted, so I did not queue the VM.

AI-assisted; commands listed were executed locally.

…firstlane-med3

# Conflicts:
#	docs/experiments.md
@NiiRoZz

NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Merged current main into the branch (d584331d). The only conflict was the docs/experiments.md paragraph that #2203 also edits; it now carries main's text plus the median.

Since #2203 is on main, I also took the review nit and added two cases to the recompute check: a median after a join, which is accepted and recomputed (it fails without v_med3_f32 in the set), and a median chain across an exec write, which is refused. Both checks, the docs links check and git diff --check pass.

AI-assisted (Claude Code).

@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.

Re-review of head d584331d9725e8c09fc239768f121ac1cc61c8c6 (comment only). My earlier review covered 5ba0b508.

Summary: The head merges main and adds one new accepted case and one new refused case to the recompute check, plus the doc sentence. The UNIFORM_VALU entry for v_med3_f32 is unchanged in substance. No P1 or P2 findings.

Checked

  • git diff --stat origin/main...HEAD is 4 files, 17 insertions, 6 deletions: gfx2hlsl.py (one table entry and its comment), the two readfirstlane checks, and docs/experiments.md.
  • The new recompute case (median of three after a join) expects s9 = U(med3(F(s3), F(0x00000000u), 0.5));. That matches the translator's med3 lowering.
  • The new refusal (median of three across an EXEC write) closes the nit from my earlier review.
  • Mergeable. CI: Linux Python checks pass, fixture assemble/translate passes, Windows build still pending when I looked.

Commands run locally (worktree at the head above)

  • python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py -> Readfirstlane uniformity checks: 0 failed
  • python3 -B tests/startup_gfx2hlsl_readfirstlane_recompute_check.py -> Readfirstlane recompute checks: 0 failed
  • python3 tests/docs_links_check.py -> Checked 18 Markdown files and 409 links.
  • python3 tests/bidi_controls_check.py -> no bidirectional controls.
  • git diff --check origin/main...HEAD -> clean
  • Mutation: removed 'v_med3_f32' from UNIFORM_VALU. The uniform check reports 2 failed and the recompute check reports 1 failed. Reverted with git checkout -- ..

Notes

  • No Windows run from me: the change is a table entry plus Linux-testable translation, with no new HLSL form emitted.
  • Clean-room and neutral title: OK.

AI-assisted; commands listed were executed locally.

@stan495 stan495 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verified head d584331d on Windows x64 (Python 3.12).

  • python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py -> passed
  • python -B tests/startup_gfx2hlsl_readfirstlane_recompute_check.py -> passed
  • python -B tests/docs_links_check.py -> passed (18 Markdown files, 409 links)

Verdict: Approved at d584331d.

@R0Y00
R0Y00 merged commit 60e52f3 into R0Y00:main Nov 25, 2026
5 checks passed
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.

4 participants