shader: lower v_cvt_pk_u8_f32 in the direct C++ compute path #2998
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2998"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T03:52:26Z.
Change
The direct compute frontend now lowers
v_cvt_pk_u8_f32. Before, it refused it withdirect 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:
FMin(opcode 36) with 255.0, thenRound_ne(opcode 26), thenfptoui;Source and output modifiers stay refused.
Verification
Commit
e56abe74oncpp-migratione0d83759(rebased after #2990 and #2994 merged; the controls were rerun on it), Windows 10.0.26100.9711,devpreset, 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_controlspassed. It compiles the program and checks the module withdxc -dumpbin: oneFMinand oneRound_neper case, and noFMax. 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_hardwareskipped (77). It needs shader model 6.6 with wave32, so the readback is not measured here.model_rne_satoftools/rdna_cvt_pk_u8.pyfor all 16 inputs. 14 of them are rows oftests/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:
The VOP3 word was assembled with LLVM 15.0.7. The other words were checked with
tools/disasm_rdna2.py --hex. The note isdocs/experiments/native-direct-cvt-pk-u8.md.Checklist
AI-assisted.
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-27T03:59:07Z.
Review: COMMENTED
Integration check for
e56abe74: both this PR and #2996 (e08d9510) targetcpp-migration, but a merge trial of the two heads conflicts insrc/gfx1013_direct_compute.inl. Please land them sequentially. If #2996 lands first, rebase this PR on the resultingcpp-migrationand rerun both opcode fixtures before merging.Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T04:01:41Z.
Thanks. All three of my open integer-ALU PRs inserted their branch in front of
v_bfi_b32insrc/gfx1013_direct_compute.inl, so any two of them conflicted. I moved the insertion points so they no longer touch the same lines:v_bfi_b32(a21837d1);v_bfe_u32(78ef2da5);s_orn2_b32(c7d668be).Merge trials on
cpp-migratione0d83759: each pair of heads merges cleanly, and so does an octopus merge of all three. On the octopus merge,compile_dxilbuilds, andnative_align_bfm_controls,native_cvt_pk16_controls,native_cvt_pk_u8_controls,native_sad_controls,native_signed_forms_controls,native_shift64_controlsandnative_direct_containerpass (7/7). The code of each lowering is unchanged; only its position moved. They can land in any order now.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T04:14:44Z.
Review: COMMENTED
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
(bits - 1) < 0x7f800000positive test →FMin(x, 255.0)→Round_ne→fptoui→ merge into S2. This faithfully replicates the RDNA2-measuredgfx2hlsl.pybehavior.2139095040is0x7f800000, the smallest IEEE 754 float with exponent 255 (i.e. NaN/inf boundary). The(bits - 1) < 0x7f800000form 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.xor(shl(255, amount), 0xFFFFFFFF)=(0xFF << amount) ^ 0xFFFFFFFF=~(0xFF << amount)— correct mask. Thenand(S2, mask) | (byte << amount)does the right byte-place update.-dumpbincontains exactlylen(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
2139095040magic 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.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) < 0x7f800000test being false only for NaN/inf/-0/+inf, not for subnormals (which are positive finite and pass through).native_cvt_pk_u8_hardwareskip rationale: The note about needing shader model 6.6 with wave32 is clear. The test usesSKIP_RETURN_CODE 77so 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 ingfx1013_direct_compute.inlfor the byte/word-place merge pattern would reduce duplication. Not necessary for this PR.Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-27T04:36:01Z.
Review: APPROVED
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:
(bits - 1) <u 0x7f800000holds from the smallest subnormal to +inf. NaN, both zeros and negatives give 0.FMin(x, 255.0)(opcode 36), thenRound_ne(26), thenfptoui.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-migratione0d83759; 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_hardwareandnative_cvt_pk_u8_controlspassed. 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 ofRound_nefailsnative_cvt_pk_u8_controlson the opcode count andnative_cvt_pk_u8_hardwareon the ties. With it restored, both pass.I will close #2997 by hand.
AI-assisted; the commands above were run locally.