Repository navigation
shader: lower v_cvt_pk_u8_f32 in the direct C++ compute path - #2998
MasterDomino merged 1 commit into
Conversation
fdeca7f to
e56abe7
Compare
R0Y00
left a comment
There was a problem hiding this comment.
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>
|
Thanks. All three of my open integer-ALU PRs inserted their branch in front of
Merge trials on AI-assisted. |
e56abe7 to
c7d668b
Compare
KelboLive
left a comment
There was a problem hiding this comment.
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) < 0x7f800000positive test →FMin(x, 255.0)→Round_ne→fptoui→ merge into S2. This faithfully replicates the RDNA2-measuredgfx2hlsl.pybehavior. - Magic number clarity: The constant
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. - Byte merge:
xor(shl(255, amount), 0xFFFFFFFF)=(0xFF << amount) ^ 0xFFFFFFFF=~(0xFF << amount)— correct mask. Thenand(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
-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 in gfx1013_direct_compute.inl for the byte/word-place merge pattern would reduce duplication. Not necessary for this PR.
MasterDomino
left a comment
There was a problem hiding this comment.
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 0x7f800000holds from the smallest subnormal to +inf. NaN, both zeros and negatives give 0. - Conversion:
FMin(x, 255.0)(opcode 36), thenRound_ne(26), thenfptoui. - 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.
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