Skip to content

shader: lower v_cvt_pk_u8_f32 in the direct C++ compute path - #2998

Merged
MasterDomino merged 1 commit into
R0Y00:cpp-migrationfrom
legacy-code-archaeologist:shader/direct-cvt-pk-u8
Nov 27, 2026
Merged

MasterDomino merged 1 commit into
R0Y00:cpp-migrationfrom
legacy-code-archaeologist:shader/direct-cvt-pk-u8

Conversation

@legacy-code-archaeologist

@legacy-code-archaeologist legacy-code-archaeologist commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Change

The direct compute frontend now lowers v_cvt_pk_u8_f32. Before, it refused it with direct DXIL lowering for v_cvt_pk_u8_f32. Refs #2997, part of the f32-ALU coverage in #2935.

The lowering computes D = (S2 & ~(0xff << 8 * S1[1:0])) | (flt32_to_uint8(S0) << 8 * S1[1:0]). The conversion is the one gfx2hlsl.py (op_v_cvt_pk_u8_f32) uses, measured on RDNA 2 (docs/experiments.md, tools/rdna_cvt_pk_u8.py): round to nearest even, saturated to [0, 255]. NaN, both zeros and all negative values give 0; +inf gives 255.

It follows the same expression:

  • an integer positive test, (bits - 1) < 0x7f800000, which fast-math cannot fold;
  • DXIL FMin (opcode 36) with 255.0, then Round_ne (opcode 26), then fptoui;
  • the merge of the resulting byte into S2.

Source and output modifiers stay refused.

Verification

Commit e56abe74 on cpp-migration e0d83759 (rebased after #2990 and #2994 merged; the controls were rerun on it), Windows 10.0.26100.9711, dev preset, NVIDIA Quadro M1000M, driver 32.0.15.7371, debug layer unavailable.

  • ctest --preset dev --no-tests=error -R '^native_cvt_pk_u8_|^native_direct_container$' -V: 2 passed, 1 skipped.
    • native_cvt_pk_u8_controls passed. It compiles the program and checks the module with dxc -dumpbin: one FMin and one Round_ne per case, and no FMax. It also refuses a negated S0 and checks that the half-up, truncating and negative-passing references differ. The pre-commit review found that a first version used opcode 35 (FMax), and the module check was added for that.
    • native_cvt_pk_u8_hardware skipped (77). It needs shader model 6.6 with wave32, so the readback is not measured here.
  • The CPU reference agrees with model_rne_sat of tools/rdna_cvt_pk_u8.py for all 16 inputs. 14 of them are rows of tests/data/rdna_cvt_pk_u8_measured.tsv; 1e30 and 127.4999 rely on the model only.
  • python -B tests/docs_links_check.py: ok.

Evidence and follow-up

The fixture has 16 cases, all over S2 = 0xa5a5a5a5:

  • ties 0.5, 1.5, 2.5 and 254.5;
  • 255.5, 1e30 and +inf;
  • -0.0, -1.0 and -inf;
  • a quiet NaN;
  • the smallest subnormal;
  • 127.4999, 0.0 and 255.0;
  • all four byte selects, plus select 6.

The VOP3 word was assembled with LLVM 15.0.7. The other words were checked with tools/disasm_rdna2.py --hex. The note is docs/experiments/native-direct-cvt-pk-u8.md.

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.

🤖 Generated with Claude Code

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Integration check for e56abe74: both this PR and #2996 (e08d9510) target cpp-migration, but a merge trial of the two heads conflicts in src/gfx1013_direct_compute.inl. Please land them sequentially. If #2996 lands first, rebase this PR on the resulting cpp-migration and rerun both opcode fixtures before merging.

The direct compute frontend refused v_cvt_pk_u8_f32. It now computes
(S2 & ~(0xff << 8 * S1[1:0])) | (flt32_to_uint8(S0) << 8 * S1[1:0])
with the conversion gfx2hlsl.py measured on RDNA 2: an integer test
(bits - 1) < 0x7f800000 for positive values, DXIL FMin with 255.0,
Round_ne and fptoui, so NaN, zeros and negatives give 0 and +inf 255.

tests/native_cvt_pk_u8_check.py dispatches ties, the saturation bound,
infinities, a NaN, a subnormal and all byte selects against a CPU
reference that agrees with tools/rdna_cvt_pk_u8.py's measured model.
Its controls check the module for one FMin and one Round_ne per case
and that half-up, truncating and negative-passing references differ.

Refs R0Y00#2997. AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

Thanks. All three of my open integer-ALU PRs inserted their branch in front of v_bfi_b32 in src/gfx1013_direct_compute.inl, so any two of them conflicted. I moved the insertion points so they no longer touch the same lines:

Merge trials on cpp-migration e0d83759: each pair of heads merges cleanly, and so does an octopus merge of all three. On the octopus merge, compile_dxil builds, and native_align_bfm_controls, native_cvt_pk16_controls, native_cvt_pk_u8_controls, native_sad_controls, native_signed_forms_controls, native_shift64_controls and native_direct_container pass (7/7). The code of each lowering is unchanged; only its position moved. They can land in any order now.

AI-assisted.

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

Solid lowering — correct round-to-nearest-even semantics, proper saturation, and the DXIL opcode checks in the test fixture are a great defense against regression (catching the earlier opcode 35 → 36 mix-up).

Findings

✅ What works well

  • Conversion path is correct: (bits - 1) < 0x7f800000 positive test → FMin(x, 255.0) → Round_ne → fptoui → merge into S2. This faithfully replicates the RDNA2-measured gfx2hlsl.py behavior.
  • Magic number clarity: The constant 2139095040 is 0x7f800000, the smallest IEEE 754 float with exponent 255 (i.e. NaN/inf boundary). The (bits - 1) < 0x7f800000 form is correct for unsigned comparison: it's true for all positive finite floats and false for NaN/inf/-0.0/+inf. A comment with the hex form would help readers.
  • Byte merge: xor(shl(255, amount), 0xFFFFFFFF) = (0xFF << amount) ^ 0xFFFFFFFF = ~(0xFF << amount) — correct mask. Then and(S2, mask) | (byte << amount) does the right byte-place update.
  • Test fixture coverage: 16 cases including ties (0.5, 1.5, 2.5, 254.5), overflow (255.5, 1e30, +inf), negatives (-0, -1, -inf), NaN, subnormal, boundary (127.4999, 0, 255), and byte-select edge case (select = 6 → low 2 bits = 2). The mutant references (half-up, truncating, negative-passing) are independent.
  • DXIL opcode assertion: Checking that -dumpbin contains exactly len(CASES) occurrences of opcode 36 (FMin) and opcode 26 (Round_ne), but zero of opcode 35 (FMax), is an excellent compile-time verification. This catches the earlier mistake of using the wrong opcode.

⚠️ Minor observations

  1. 2139095040 magic number: Consider // 0x7f800000 = UINT32 min for positive finite floats (NaN/inf boundary) as a comment. The decimal form is harder to parse at a glance.

  2. float("bitcast i32 " + a + " to float"): This bitcasts the raw 32-bit value from the VGPR to a float register for the FP comparison. Correct, but if the VGPR value is a denormal or subnormal, the bitcast is still valid (DXIL handles subnormals). No issue, just noting that edge cases are covered by the (bits - 1) < 0x7f800000 test being false only for NaN/inf/-0/+inf, not for subnormals (which are positive finite and pass through).

  3. native_cvt_pk_u8_hardware skip rationale: The note about needing shader model 6.6 with wave32 is clear. The test uses SKIP_RETURN_CODE 77 so it won't fail CI. This is well-documented.

Recommendation

Clean and well-tested. Ready to land with or without the decimal→hex comment.

🏷 One thought: if this family (v_cvt_pk_*) gets more members (e.g. v_cvt_pk_u16_f32), consider whether a shared helper function in gfx1013_direct_compute.inl for the byte/word-place merge pattern would reduce duplication. Not necessary for this PR.

@MasterDomino MasterDomino 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 c7d668be. CI on the head is green. This is the wave32 SM 6.6 readback you asked for. Approving and merging.

Code read:

  • Positive test: (bits - 1) <u 0x7f800000 holds from the smallest subnormal to +inf. NaN, both zeros and negatives give 0.
  • Conversion: FMin(x, 255.0) (opcode 36), then Round_ne (26), then fptoui.
  • Byte merge: the byte goes into S2 at 8 * S1[1:0] under ~(0xff << amount).

Run on AMD: Windows 11 26300.9974, AMD Radeon RX 9070 XT (driver 32.0.32029.2010), plus WARP, release. This was tested together with #2986, #2996, #3003 and #2975 merged onto cpp-migration e0d83759; all five merge cleanly with each other. ctest --preset release --no-tests=error -R '^(native_.*|startup_compute_dispatch(_.*)?_(warp|hardware)|startup_slot67_indirect_dispatch_warp|startup_compute_translate_e2e_warp|startup_stream_draw.*|startup_native_graphics.*)$' gives 94 tests, 90 passed and 4 skipped (77, WARP has no 32-lane waves). native_cvt_pk_u8_hardware and native_cvt_pk_u8_controls passed. This also covers R0Y00's request to rerun both opcode fixtures together with #2996: both pass on the combined tree.

Mutation here: using Round_z (29) instead of Round_ne fails native_cvt_pk_u8_controls on the opcode count and native_cvt_pk_u8_hardware on the ties. With it restored, both pass.

I will close #2997 by hand.

AI-assisted; the commands above were run locally.

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