Skip to content

startup: draw the gfx2hlsl conversion operations - #1708

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
Asimmimx:startup/gfx2hlsl-cvt-alu-check
Nov 24, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
Asimmimx:startup/gfx2hlsl-cvt-alu-check

Conversation

@Asimmimx

Copy link
Copy Markdown
Contributor

Refs #1149.

Change

No check ran the conversion operations of the VEC_U table in tools/startup/gfx2hlsl.py: v_cvt_f32_u32, v_cvt_f32_i32, v_cvt_u32_f32, v_cvt_f32_ubyte0 to 3, v_cvt_f32_f16 and v_cvt_f16_f32.

This PR adds a check only. The translator does not change.

tests/startup_gfx2hlsl_cvt_alu_check.py draws them with the per-lane runner tests/startup_gfx2hlsl_lane_alu.py. The runner is byte-identical to the copies in #1703 and #1706, so the PRs merge in any order. In each draw:

  • one operation runs per lane on flat inputs, alternating _e32 and _e64;
  • the four result words are compared exactly;
  • the recorded llvm-mc bodies are checked by a binary mode;
  • the controls cover refused modifiers and operand counts.

References, from the AMD RDNA 2 ISA reference:

Operation Reference Inputs that matter
v_cvt_f32_u32, _i32 round to nearest even 0x80000001 → 2^31, 0xffffffff → 2^32, 0x01000001 → 2^24 (tie to even), negative integers
v_cvt_u32_f32 truncate toward zero; negative, -0 and NaN → 0 1.75, -3.5, NaN, 0x4f7fffff (largest below 2^32), 123456.9
v_cvt_f32_ubyteN byte N of S0 words that differ in every byte
v_cvt_f32_f16 S0[15:0] other binary16 values in the high half, ±inf, 65504
v_cvt_f16_f32 bits 15:0, high half clear exactly representable values, 65504, 2^-14, -0

Each operation has one mutated lowering, and the first draw with it must read the mutation's own reference words:

  • signed and unsigned exchanged;
  • rounding instead of truncation;
  • the wrong byte or a 7-bit mask;
  • the high binary16 half;
  • a negated source.

Results

Commit 9d47aec5 on origin/main. Windows 11 26300.9974, DXC 1.8.2502.11, FXC 10.0.26100, debug layer on.

Adapter Driver Draws Mutated draws
WARP 10.0.26100.9818 34/34 match 17/17 read the mutated words
AMD Radeon RX 6600 (RDNA 2) 32.0.21045.29003 34/34 match 17/17 read the mutated words

There were no skips. binary with llvm-mc 19.1.1 gives fixture words match llvm-mc.

FXC and v_cvt_f16_f32. Eight operations are drawn through both FXC ps_5_1 and DXC ps_6_0; v_cvt_f16_f32 is drawn through DXC only.

Its results are 16-bit words, so every exported word is a binary32 denormal in the fixture's float target. The FXC ps_5_1 output of that pixel stage read 00000000,00000000,00000000,00000000 on WARP and on the RX 6600, where 00003c00,0000c100,00007bff,00000400 was expected. A variant with one channel converting a constant read zeros as well. The same words with 0x3f800000 ORed in at the export read back exactly (3f803c00,3f80c100,3f807bff,3f800400), on both adapters. The conversion is therefore right, and the zeros come from how the FXC-compiled stage exports denormal bit patterns.

This may matter beyond the fixture. A translated pixel stage compiled with FXC that exports packed 16-bit data through a float target could lose it. I have not checked any real pipeline for that.

Not covered:

  • v_cvt_u32_f32 at or above 2^32 and of +inf, where the HLSL conversion of an out-of-range value is not pinned;
  • binary16 denormal inputs;
  • binary32 values that v_cvt_f16_f32 has to round.

AI-assisted (Claude Code).

The VEC_U table lowers v_cvt_f32_u32, v_cvt_f32_i32, v_cvt_u32_f32,
v_cvt_f32_ubyte0..3, v_cvt_f32_f16 and v_cvt_f16_f32, and no check ran
any of them.

tests/startup_gfx2hlsl_cvt_alu_check.py draws them with the shared
per-lane runner (byte-identical copy of tests/startup_gfx2hlsl_lane_alu.py)
against references written from the RDNA 2 ISA: integer to binary32 with
round to nearest even (2^31, 2^32 and a tie at 2^24), truncation with 0
for negatives, -0 and NaN, byte selection, S0[15:0] for the binary16 source
and a cleared high half for the binary16 result. Each operation has a
mutated lowering whose first draw must read its own words.

All 34 draws match on WARP and an RX 6600; the 17 mutated draws read
their mutated words. v_cvt_f16_f32 is drawn through DXC only: its result
words are binary32 denormals in the fixture's float target, and the FXC
ps_5_1 output of that stage reads 0 in every channel on both adapters,
while the same words with 0x3f800000 ORed in read back exactly.

@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 9d47aec5d4b1aa75ca9037de551f563327e662cb (Linux only; no D3D12, WARP or RDNA2 hardware here, so the 34 draws were not reproduced).

Verdict: 0 P1, 1 P2, 2 P3. Translator untouched; references match the RDNA2 ISA text; the fixture words are valid. The P2 is a CMake ordering problem that quietly drops the FXC draws on a fresh configure.

P2: STARTUP_FXC is read before it is defined

The new block (CMakeLists.txt ~1638-1647) uses $<$<BOOL:${STARTUP_FXC}>:${STARTUP_FXC}>, but find_program(STARTUP_FXC ...) is at line 1650, after it. ${} expands at configure time, so on a clean build dir the argument is empty and the draw runs DXC only. FXC is picked up only after the cache holds the variable (second configure). Minimal CMake reproduction run locally: first configure prints USE_BEFORE=[], second prints the path.

  • Effect: the reported FXC ps_5_1 draws (16 of the 34) can be missing from a fresh CI or developer run, silently; the run still passes.
  • Suggestion: place the new block after the find_program line, as startup_gfx2hlsl_bit_alu is.

Semantics

  • References are plain Python from the ISA text: RNE int to binary32, truncation with negative/NaN to 0, byte N, S0[15:0], low-half binary16 result. The inputs hit the cases the PR lists. Counts check out: 9 ops x 2 chunks = 18 DXC draws, 16 FXC (f16_f32 excluded), 17 mutated draws.
  • P3 (not verifiable here): the "bits 31:16 clear" reference for v_cvt_f16_f32 rests on the author's reading of the ISA; this review cannot check it against hardware.
  • P3: on Linux the controls are structural. They match the translated text (any lowering change fails the count == 4 check on the mutation pattern) but evaluate no value, unlike the bit_alu sibling, which has a CPU evaluator. Numeric correctness exists only in the Windows draws.

Commands executed locally

  • python3 -B tests/startup_gfx2hlsl_cvt_alu_check.py: 29 ok, 0 MISMATCH, exit 0.
  • ... cvt_alu_check.py binary llvm-mc-19: fixture words match llvm-mc (CI uses llvm-mc-15).
  • Local mutations, each reverted afterwards (restored clean):
    • translator: ubyte1 shift 8 to 16, cvt_u32_f32 without max, cvt_f32_i32 without int(), cvt_f16_f32 negated. All four fail the controls (pattern no longer matches).
    • test: ubyte1 mutation replacement made a no-op fails count == 4; recorded body words of ubyte2 swapped for ubyte1 fails binary; NaN guard removed from the cvt_u32_f32 reference crashes the controls.
  • A CPU emulation I wrote of the translated expressions over all 72 draw inputs agrees with the references (0 differences) and reports differences for each of the four translator mutations, so the chosen inputs separate the right and wrong lowerings. It is an independent model, not the HLSL compilers.
  • Minimal CMake script for the P2.

Limits: no ctest run, no Windows draw, no FXC denormal check (the v_cvt_f16_f32 FXC claim is the author's measurement only).

AI-assisted review.

@R0Y00

R0Y00 commented Nov 24, 2026

Copy link
Copy Markdown
Owner

#1707 is now on main at 49634a1. It enables output modifiers on float-result conversions, so the refusal in this fixture for v_cvt_f32_f16 with mul:2 is stale on the combined code. Please keep the conversion draw and replace that refusal with an integer-result case, for example v_cvt_f16_f32 with mul:2. The plain conversion and its existing output mutations should stay intact.

@KainanP

KainanP commented Nov 24, 2026

Copy link
Copy Markdown

On the "Not covered" item, v_cvt_u32_f32 at or above 2^32 and of +inf: here is data for both sides of it.

Hardware. On RDNA 2 silicon (gfx1036, desktop APU of the console's generation), @4usphs ran v_cvt_u32_f32 as inline assembly over xcl's #639 inputs, including ±inf and the integer range limits. It saturates: an out-of-range value gives 0xFFFFFFFF and negative values give 0. NaN gives 0 for v_cvt_u32_f32 / v_cvt_i32_f32: xcl-dev/xcl#639 (comment). (In the same run, v_cvt_flr/rpi_i32_f32 of a NaN gave 0x7FFFFFFF / 0x80000000 by the NaN's sign, not 0. That is not relevant to this table, but it matters if those two are added later.)

HLSL side. uint(max(F(x), 0.0)) leaves values ≥ 2^32 to an out-of-range cast, which HLSL leaves undefined. What comes out depends on whether the value gets constant-folded:

So to pin the row rather than leave it adapter-dependent, an explicit clamp is the usual approach. xcl uses select(isnan(f) || f <= 0, 0, select(f >= 4294967296.0, 0xFFFFFFFF, (uint)f)); AnyXboxX tests NaN on the bits, because dxc folds x != x to false. A row with 0x4f800000 (2^32) and +inf expecting 0xffffffff would then cover it. None of this was measured on Scarlett or through FXC.

AI-assisted (Claude Code). The figures are quoted from the linked threads.

@R0Y00

R0Y00 commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Correction to my earlier note: this candidate already refused the integer-result v_cvt_f16_f32 modifier. I kept that case and added an explicit accepted float-result modifier after integrating main. I also moved the registration after FXC discovery so a clean build runs both compilers where supported. Windows Release controls, WARP and RTX 4060 Laptop passed 3/3 with the existing per-operation negative controls. The merged candidate now needs its own CI.

@R0Y00
R0Y00 merged commit ecc6e6c 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.

4 participants