Skip to content

startup: translate the SDWA forms of the float-result conversions again - #1730

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
triggered02:startup/sdwa-cvt
Nov 24, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
triggered02:startup/sdwa-cvt

Conversation

@triggered02

Copy link
Copy Markdown
Contributor

Follow-up to the merged #1707, fixing the review's P1 (SDWA forms of the moved conversions stopped translating). Reported by tedin7 after the merge.

Cause

Moving the float-result conversions into VEC_U_TO_F left sdwa() looking only at VEC_U:

if base not in VEC_U:
    raise Unsupported('SDWA form of %s' % base)

so v_cvt_f32_u32_sdwa, v_cvt_f32_i32_sdwa, v_cvt_f32_f16_sdwa, v_cvt_f32_ubyte0..3_sdwa were all refused on the merged tree.

Fix

sdwa() accepts both tables and wraps a float result in U(...) before the destination selection writes its bit pattern; integer-result conversions keep the uint path unchanged.

v_cvt_f32_u32_sdwa v0, v1 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0
  -> v0 = U(float(((v1 >> 0u) & 0xffffu)));
v_cvt_f32_i32_sdwa v0, v1 … src0_sel:BYTE_0
  -> v0 = U(float(int(((v1 >> 0u) & 0xffu))));
v_cvt_f32_f16_sdwa v0, v1 … src0_sel:WORD_0
  -> v0 = U(f16tof32(((v1 >> 0u) & 0xffffu)));
v_cvt_f16_f32_sdwa v0, v1 …      (uint result)
  -> v0 = f32tof16(F(((v1 >> 0u) & 0xffffu)));   (unchanged)

Destination selection still writes the result: dst_sel:BYTE_1 masks to & 0xffu) << 8u, dst_unused:UNUSED_PRESERVE reads asuint(v0). An output modifier inside an SDWA form stays refused rather than dropped.

Test

startup_gfx2hlsl_sdwa_cvt builds VOP1 SDWA encodings from the checked-in decoder's layout (SRC0 = 249 marks SDWA, control word in the next dword: src0_sel[18:16], dst_sel[10:8], dst_unused[12:11], omod[15:14]), decodes them, then translates — no game-derived bytes.

dev:     startup_gfx2hlsl_sdwa_cvt Passed 0.14 s; all 9 startup_gfx2hlsl tests pass
release: startup_gfx2hlsl_sdwa_cvt Passed 0.14 s

Fail-first: removing the VEC_U_TO_F branch from sdwa() fails the test with Unsupported: SDWA form of v_cvt_f32_u32.

Still open from that review

The P2 (claiming op_sel is refused while set_vf() silently ignores it) is not in this PR — that guard changes every float path that goes through set_vf, so it needs its own test sweep rather than riding along a regression fix. Tracked as the next slice.

What this does not establish

  • That the merged tree produces the same HLSL as before the split for other SDWA operations — only the conversion forms are covered here.
  • DXC/D3D12 draw results; the numbers above are the translator tests.

AI-assisted (Claude Code).

Regression from R0Y00#1707, found in review by tedin7 after the merge (P1): moving the float-result conversions into VEC_U_TO_F left sdwa() looking only at VEC_U, so every v_cvt_f32_*_sdwa was refused with 'SDWA form of <name>'. sdwa() now accepts both tables and wraps a float result in U(...) before the destination selection writes its bit pattern; integer-result conversions keep the uint path unchanged. Test builds VOP1 SDWA encodings (SRC0=249 marks SDWA, control word in the next dword), decodes, then translates: u32/i32/f16 conversions, DWORD and BYTE selections, dst_sel masking and UNUSED_PRESERVE, the unchanged uint path, and an omod still refused. Fail-first removing the new branch fails the test. Both presets pass, all 9 startup_gfx2hlsl tests pass.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T17:57:03.316551Z e6a0cd7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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 e6a0cd79c1cedc76482e9acf3b3c1f11342599c7 (1 commit; tools/startup/gfx2hlsl.py +10/-2, new tests/startup_gfx2hlsl_sdwa_cvt_check.py +128, CMakeLists.txt +3, docs/experiments.md +5).

Linux limits. Linux x86_64, llvm-mc-19, Python 3.14. No CMake configure and no ctest (the startup_gfx2hlsl_sdwa_cvt registration is unexercised), no Windows, no DXC, no WARP or GPU. The translated HLSL is compared as text and was never compiled or run, so the arithmetic is judged by reading the emitted expressions.

What I ran

  • python3 -B tests/startup_gfx2hlsl_sdwa_cvt_check.py: PASS. Also exit 0: startup_gfx2hlsl_sdwa_check.py, startup_gfx2hlsl_vcvt_omod_check.py, startup_translator_check.py, startup_gfx2hlsl_bit_alu_check.py.
  • git diff --check origin/main...HEAD: clean (no non-ASCII added, no lines removed from existing tests). tests/docs_links_check.py and tests/bidi_controls_check.py: pass. git merge-tree against current main: no conflicts.
  • Binary check: the test builds its bytes with encode_vop1_sdwa(). I assembled the same six instructions with llvm-mc-19 -show-encoding for both gfx1030 and gfx1013 (v_cvt_f32_u32 WORD_0, v_cvt_f32_i32 BYTE_0, v_cvt_f32_f16 WORD_0, v_cvt_f32_u32 BYTE_1 into BYTE_1 with UNUSED_PRESERVE, v_cvt_f16_f32 WORD_0, v_cvt_f32_u32 with mul:2): all twelve encodings are byte-identical to the encoder.
  • Extra probes with the same encoder: clamp (bit 13), source negate (bit 20) and abs (bit 21) are refused (SDWA modifier clamp, operand '-v1', operand '|v1|'); sext on the u32 source lowers to float(asuint(asint(v1 << 16u) >> 16u)), i.e. sign-extend the field then convert as unsigned, as the ISA describes.
  • Four mutations of sdwa(), each run against the new test and restored (git status clean):
    • drop the U(...) wrapper: FAIL (4 mismatches).
    • look only at the uint table again (the bug being fixed): FAIL (SDWA form of v_cvt_f32_u32).
    • wrap the uint-result path as a float: FAIL (uint-result conversion stays on the uint path).
    • ignore unknown SDWA modifiers: FAIL (an omod on an SDWA conversion stays refused).

Semantics (RDNA 2 SDWA)

  • Source selection, the float conversion and the dst_sel/dst_unused write are in the right order: the float result is bit-cast with U() and only then masked and shifted into the destination field, which is what an SDWA destination selection does to a 32-bit result. UNUSED_PRESERVE keeps the other bits; UNUSED_SEXT is still refused.
  • Output modifier and clamp inside an SDWA form are refused instead of being dropped, which is the safe boundary (dropping them would silently change the value).

Findings: 0 P1, 0 P2, 2 nits

  • Nit. The comment above sdwa() still says "float forms are not lowered yet"; it is now wrong for the float-result conversions.
  • Nit. The new test file has no newline at the end.
  • Not verified: DXC or FXC compilation of the emitted expressions, and any draw.

AI-assisted; commands listed were executed locally.

@R0Y00
R0Y00 merged commit a4d17cf into R0Y00:main Nov 24, 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.

3 participants