Skip to content

Lower v_cvt_pk_u8_f32 in the startup gfx2hlsl translator - #2101

Merged
R0Y00 merged 6 commits into
R0Y00:mainfrom
Ybucaille:startup/gfx2hlsl-cvt-pk-u8
Nov 25, 2026
Merged

R0Y00 merged 6 commits into
R0Y00:mainfrom
Ybucaille:startup/gfx2hlsl-cvt-pk-u8

Conversation

@Ybucaille

Copy link
Copy Markdown
Contributor

Lower v_cvt_pk_u8_f32 in tools/startup/gfx2hlsl.py (item K of #2023).

What it does

v_cvt_pk_u8_f32 D, S0, S1, S2 (VOP3A opcode 350) inserts a byte into S2:

D = (S2 & ~(0xff << 8 * S1[1:0])) | ((flt32_to_uint8(S0) & 0xff) << 8 * S1[1:0])

The RDNA 1.0 and RDNA 2 ISA guides do not define flt32_to_uint8: rounding, bounds, NaN and negative results are not stated. I did not find a source for them that I could cite, so this PR does what tools/compute_store_aot.py already does for the same opcode (#1546, #1592): it holds only where every reading agrees, an S0 that is +0.0 or an integer in [1, 255].

  • A constant S0 outside that set (0.5, -1.0, 256.0, NaN, -0.0) is refused.
  • A register S0 cannot be checked statically, so the program must keep it inside the set. The HLSL clamp only keeps the conversion defined; it is not a claim about the hardware.
  • Modifiers (clamp, omod, neg, abs), a wrong operand count and a non-VGPR destination are refused.
  • S1 and S2 may be registers or constants; only S1[1:0] is used.

I did not add rounding at x.5 or clamping below 0 and above 255 to the fixture, because the reference would be a guess. If a maintainer can point to the hardware behavior, that is a follow-up.

Test

tests/startup_gfx2hlsl_cvt_pk_u8_check.py, source-built (no retail bytes), on the shared startup_gfx2hlsl_lane_alu.py runner:

  • all four byte selectors, with S1 bits above [1:0] set, S2 words of four distinct bytes, S0 from 0.0 to 255.0;
  • negative controls: a lowering that loses S2 (also drawn), and a wrong byte position, both detected;
  • the check fails when the shift in the translator is mutated;
  • refusals listed above; llvm-mc reassembly of the pixel bodies.

Wired in CMakeLists.txt (controls, warp, hardware) and .github/workflows/windows.yml.

Verified here (Linux)

The new check (no arguments and binary with llvm-mc), every tests/startup_gfx2hlsl_*_check.py, tests/startup_translator_check.py, tests/docs_links_check.py and git diff --check. The D3D12/WARP draw does not run on Linux, so the draw mode is untested; the CTest entries need a Windows run.

Refs #2023 (item K)

AI-assisted.

VOP3A opcode 350 of the RDNA 1.0 and RDNA 2 ISA guides:
  D = (S2 & ~(0xff << 8 * S1[1:0]))
    | ((flt32_to_uint8(S0) & 0xff) << 8 * S1[1:0])
The guides leave flt32_to_uint8 undefined (rounding, bounds, NaN and
negative results are not stated), so the lowering holds only where
every reading agrees: S0 is +0.0 or an integer in [1, 255]. A constant
S0 outside that set is refused, as are modifiers, a wrong operand count
and a non-VGPR destination. A register S0 cannot be checked statically;
the HLSL clamp only keeps the conversion defined. This is the set that
tools/compute_store_aot.py already lowers (R0Y00#1546). S1 may be a register
or a constant; only its bits [1:0] are used.

The fixture is source-built and draws all four byte selectors with
S1 bits above [1:0] set, S2 words of four distinct bytes, and S0 from
0.0 to 255.0. Its negative controls (a lowering that loses S2, a wrong
byte position) are detected, and the check fails when the shift in the
translator is mutated. The D3D12 draw was not run here (Linux): the
controls and the llvm-mc reassembly were.

Refs R0Y00#2023 (item K)

AI-assisted.
@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Six mutations: four caught, two survivors — and one of them I expect to change the drawn word, so I would like your read before I claim anything

Baseline on this branch, both paths run here (Windows 11 build 26300.9974, DXC 10.0.26100.0):

python -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py                          exit 0
python -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py draw <dxc> <offscreen_check> warp   exit 0
warp: 2 draws matched, 1 mutated draws read their mutated words
mutation in op_v_cvt_pk_u8_f32 controls draw (warp)
shift pinned to 0u (ignore S1) caught —
rest high bits no longer preserved ((rest & ~(0xff<<s)) | → (rest |) caught —
the constant-range refusal removed caught —
len(ops) != 4 → != 3 caught —
clamp(F(x), 0.0, 255.0) → 0.0, 256.0 survived —
the insert mask & 0xffu → & 0x1ffu survived survived

The clamp survivor is fine, and here is why I am not calling it a gap

Under this lowering's stated contract the source is +0.0 or an integer in 1..255, and outside that the ISA's flt32_to_uint8 is undefined. Clamping
at 256 instead of 255 can only differ for inputs in (255, 256], which the contract excludes. So it is an equivalent mutant relative to the
precondition
— but the constant is a documented choice, and a one-line text assertion ('0.0, 255.0' in text) would pin it for free. Your call.

The mask survivor is the one I would like explained

((rest & ~(0xffu << s)) | ((x & 0xffu) << s)) → ... ((x & 0x1ffu) << s). For your own first draw vector:

s0=0x437f0000 (255.0)  s1=0  rest=0x55aa33cc   expected 0x55aa33ff

correct: clear the low byte of rest → 0x55aa3300, insert 0xff → 0x55aa33ff
mutant: clear nine bits → 0x55aa3200, insert 0xff → 0x55aa32ff

Different word, and bit 8 of rest (0x33cc → LSB of 0x33 is 1) is exactly the bit the wider mask eats. Yet the draw stayed green when I applied
it. I checked that the mutant reaches the emitted text — the assignment becomes

v5 = ((0x55aa33ccu & ~(0xffu << ((0x00000000u & 3u) << 3u))) | ((uint(clamp(F(0x437f0000u), 0.0, 255.0)) & 0x1ffu) << ((0x00000000u & 3u) << 3u)));

so one of three things is true, and I could not settle which in the time I had:

  1. my hand computation above is wrong (then the survivor is equivalent and I have learned something);
  2. check_draw compiles the good shader from a translation that is not the live one after the mutation — translate() builds hlsl_ps() at call time,
    so I would expect it to be live, which is why this is a question rather than a claim;
  3. the drawn comparison tolerates it for a reason I have not seen.

If it is (2), then the draw half is not a control against lowering mutations at all, which would matter beyond this PR. If it is (1), ignore me.

The thing this PR gets right that I want to write down

Refusing a constant S0 outside {+0.0} ∪ [1,255] is the correct shape of answer for an instruction whose own guide leaves the conversion undefined:
the translator states the precondition it needs instead of guessing a rounding rule. That is the same call I made in #2092 for class16, and it is
much easier to defend in review than a plausible-looking round().

AI-assisted; the six mutations, both check modes and the HLSL dump above were run locally.

@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Narrowing my own question: the mutant does reach the compiled pixel shader

I said I could not tell whether the draw half compiles the live lowering. That part is now settled, and it makes the survivor more interesting rather than
less:

unmutated  lane 12: ((v8 & ~(0xffu << ((v4 & 3u) << 3u))) | ((uint(clamp(F(v0), 0.0, 255.0)) & 0xffu) << ((v4 & 3u) << 3u)))
mutated    lane 12: ((v8 & ~(0xffu << ((v4 & 3u) << 3u))) | ((uint(clamp(F(v0), 0.0, 255.0)) & 0x1ffu) << ((v4 & 3u) << 3u)))
contains 0x1ffu: False -> True

So translate() is live, the mutant lands in the good shader's text, and the draw still reported 2 draws matched. Whatever is responsible is therefore
downstream of the translation — in compile_hlsl, in the renderer's comparison, or in my hand computation of the expected word.

For your convenience, the case I computed by hand from your own first draw set:

s0=0x437f0000 (255.0)   s1=0   rest=0x55aa33cc
correct: rest & ~0xff | (255 << 0)              = 0x55aa3300 | 0xff = 0x55aa33ff
mutant:  rest & ~0x1ff | (255 & 0x1ff) << 0     = 0x55aa3200 | 0xff = 0x55aa32ff

If that arithmetic is right, the mutated shader cannot produce the word want demands, and the draw should have failed — so one of the two runs I did
(controls, then draw, same mutant, same session) did something I have not accounted for. If it is wrong, then the mask mutant is equivalent and my
survivor report is noise; I would rather you tell me which than have me guess in public.

I am not claiming your draw half is unfaithful — I am saying I could not explain the result with the evidence I have, and the next reviewer should not
have to rediscover that.

AI-assisted.

@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Correction: the mask survivor is an equivalent mutant, and my hand computation was wrong

I have to withdraw the question in my previous comment. I mutated only the insert side:

((v8 & ~(0xffu << ((v4 & 3u) << 3u))) | ((uint(clamp(F(v0), 0.0, 255.0)) & 0x1ffu) << ((v4 & 3u) << 3u)))
                                                    ^^^^^^ this changed
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ clear side untouched

The clear mask is still ~(0xffu << s), so bit 8 of rest is preserved either way, and the inserted value comes from
uint(clamp(F(x), 0.0, 255.0)), which is at most 255 — so & 0x1ffu and & 0xffu select exactly the same bits. The word is identical:

rest & ~0xff            = 0x55aa3300
(255 & 0x1ff) << 0      = 0xff
result                  = 0x55aa33ff   (same as the correct lowering)

In my previous comment I wrote rest & ~0x1ff for "mutant" — that is not what the mutant does. The mutant never touches the clear side. So the draw
matching was correct, your draw half is faithful, and the survivor is equivalent: nothing to fix, and no question for you to answer.

Three things I got out of this, for the record rather than for this PR:

  1. an "equivalent mutant survivor" is a claim about the code, not about the test, and mine rested on an arithmetic step I did not re-read against the
    text I had just printed in the same comment;
  2. the mask/clear pair is a place where a one-character mutation looks like it changes the field width and does not — worth knowing if you ever want a
    real gap there, the mutation has to widen the clear mask (~(0x1ffu << s)), which would then be caught by the draw;
  3. I should have run that mutation to completion (mutating the clear side as well) before posting the question, instead of posting a question I could have
    answered with one more edit.

Sorry for the noise. The rest of the review stands: four of six mutations caught, and the clamp constant is the only thing I would still pin (with a text
assertion, since it is equivalent under the stated precondition).

AI-assisted; the mutant text above is from the run I posted, re-read.

Verified the follow-through before posting the correction

I said the mutation that would be a real gap is widening the clear mask, so I ran it:

((rest & ~(0x1ffu << s)) | ((x & 0xffu) << s))     clear side widened

controls: AssertionError: translated text reads 00000000 00000000 11223344   exit 1
draw:     exit 1

Both halves catch it, which is what a faithful pair of controls should do — and it is further evidence that my earlier survivor was equivalent rather
than missed. The clear-side mutation is the one to use if anyone wants a maintained control here.

@Its3rr0rsWRLD

Copy link
Copy Markdown
Contributor

Windows run of the requested draws for #2101, tested at head 2d67fe41930379043235b98b2b6e0799ebf05f27.

Environment

  • Windows 11 26H2, build 26300.9974
  • Hardware adapter: NVIDIA GeForce RTX 4070 Ti, driver 32.0.16.1088
  • WARP: Microsoft Basic Render Driver, d3d10warp.dll 10.0.26100.9818
  • dev preset, MSVC; built only offscreen_check and the tests' dependencies

Command

ctest --preset dev --no-tests=error -R "^startup_(gfx2hlsl_interp_f16|gfx2hlsl_bit_alu|vertex_fetch_formats|gfx2hlsl_cvt_pk_u8)_(controls|warp|hardware)$" --output-on-failure

Results: 9/9 passed (the interp_f16 family is not in this branch)

Test Result
startup_gfx2hlsl_cvt_pk_u8_controls pass
startup_gfx2hlsl_cvt_pk_u8_warp pass
startup_gfx2hlsl_cvt_pk_u8_hardware pass
startup_vertex_fetch_formats_controls pass
startup_vertex_fetch_formats_warp pass
startup_vertex_fetch_formats_hardware pass
startup_gfx2hlsl_bit_alu_controls pass
startup_gfx2hlsl_bit_alu_warp pass
startup_gfx2hlsl_bit_alu_hardware pass

Run by Claude (AI-assisted).

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

The four in-domain draws and both mutations passed on WARP and RTX 4060 Laptop. The generic startup path still accepts an arbitrary register for S0 and clamps it, with no proof or explicit profile contract that it contains only +0.0 or integers 1..255. A computed fraction, NaN or negative value can now silently select an invented byte. Please restrict the default path to proven in-domain words, or add an explicitly selected fixture/profile contract with a rejection for unproven operands in ordinary translation. Keep the four selector checks and the dropped-S2 control.

Review of R0Y00#2101: the generic path accepted any register S0 and clamped
it, so a computed fraction, NaN or negative value could silently select
an invented byte, since the ISA leaves flt32_to_uint8 undefined there.

A register S0 is now accepted only when proven +0.0 or an integer in
1..255: written by v_cvt_f32_ubyte0..3 (no modifier, straight-line code,
entry exec mask) with no write since. Any other register is refused,
unless the caller selects the Pipeline keyword pk_u8_s0_contract, which
the fixture draws do; use of the contract is recorded in the notes.
@Ybucaille

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed in 1afe5a6.

Default path: proven S0 only. In ordinary translation, a register S0 is now accepted only when its value is proven to be +0.0 or an integer in 1..255:

  • a constant in that set (unchanged);
  • a VGPR written by v_cvt_f32_ubyte0..3 (e32 or e64, without modifiers) in straight-line code under the entry EXEC, and not written since. The proof is dropped on any later write of the register.

Anything else is refused with v_cvt_pk_u8_f32 source vN is not proven to be +0.0 or an integer in 1..255. That covers a computed value (v_mul_f32 after the conversion, for example), a plain v_mov_b32 copy, v_cvt_f32_u32, a conversion with clamp or mul:2, an SGPR, and any of these after a branch or under a partial EXEC.

Explicit contract for the fixture. Pipeline(..., pk_u8_s0_contract=True) states that unproven S0 registers hold only those values. Only then are they accepted, still with the clamp, and the pipeline records the assumption in its notes, so it is written to the manifest. Ordinary translation (load_request, request_refusals) never sets it. The draws select it through the fixture family, so their HLSL is unchanged.

Kept: the four selector checks and the dropped-S2 control are unchanged. In the accepted list, the register S0 forms became constants, because they now need the contract. Each of them is covered again in the new contract controls.

New controls: an unproven S0 is refused without the contract (three spellings, exact message), and the same words are accepted with it, with the note. ubyte0..3 are accepted without the contract, in e32 and e64. Each refused form above has its own control. Each new control fails on the previous code, on a version where the contract is always on, on a version where the proof survives a later write, and on a version where the proof accepts modifiers.

Linux: the check (with binary /usr/bin/llvm-mc; the words are unchanged), all startup_gfx2hlsl_*_check.py, startup_translator_check.py and docs_links_check.py pass. git diff --check is clean. The draws were not rerun: the fixture HLSL is the same, only the way the contract is selected changed. A Windows rerun of startup_gfx2hlsl_cvt_pk_u8_(controls|warp|hardware) would confirm it.

AI-assisted.

@denn0x

denn0x commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Thanks. One concern about the register path.

In a private executable's pixel programs (decode only, aggregate counts):

  • 212 instructions are v_cvt_pk_u8_f32;
  • in 208 of them, S0 comes straight from a v_mul_f32 right before it, with no rounding instruction in between. That looks like the usual x * 255.0 pack to unorm8.

So the precondition in the comment ("the program must keep it inside the set") does not hold for these programs: S0 is generally not an integer.

  • If the hardware rounds to nearest, truncating with uint(clamp(...)) gives a byte one lower for about half the inputs.
  • If the hardware truncates, the lowering is right, but then that needs a citation. The guides leave flt32_to_uint8 undefined, as you note.

Two options:

  1. Refuse a register S0 unless it is proven integral (for example, produced by v_rndne_f32 / v_floor_f32 / v_cvt_f32_u32). That keeps the PR exact for what it can prove.
  2. Find the rounding: a hardware readback on any RDNA 2 adapter (the op is the same), or how Mesa/LLVM use the instruction for pack_unorm_4x8. If the compiler relies on it without an explicit round, that is strong evidence that the hardware rounds.

@Ybucaille

Copy link
Copy Markdown
Contributor Author

Thanks, @denn0x. Our comments crossed: 1afe5a6 (pushed a few seconds before yours) is your option 1, slightly stricter. A register S0 is accepted without the explicit fixture contract only when it comes straight from v_cvt_f32_ubyte0..3. v_rndne_f32 / v_floor_f32 / v_cvt_f32_u32 prove an integer but not the range: a negative value or one above 255 would still reach the clamp, and the clamp is exactly the undefined part. So the 208 v_mul_f32 → v_cvt_pk_u8_f32 sequences from your scan are refused in ordinary translation.

On option 2, the only measurement I found is second-hand and not on RDNA. shadPS4 #5194 (shadps4-emu/shadPS4#5194, merged 2026-10-01) reports a hardware test on PS4 (GCN). Its findings:

  • round half to even: 1.5 → 2, 2.5 → 2, 127.5 → 128, 254.5 → 254;
  • saturation to 0..255: 256 and +inf → 255, negative values and -inf → 0;
  • NaN → 0;
  • byte select by S1 & 3.

The PR does not describe the test protocol. Nothing else I checked defines the conversion. LLVM has no semantics comment, no constant folding and no pattern for int_amdgcn_cvt_pk_u8_f32. Mesa ACO only emits it to write exact 8-bit constants on GFX10+, never for pack_unorm_4x8, so it says nothing about rounding.

If that holds on RDNA, the truncating uint(clamp(...)) would indeed be one too low for about half of the x * 255.0 inputs, as you say. So I'd keep this PR to the proven domain and lower the general form (round half to even, saturate, NaN → 0) in a follow-up, once there is an RDNA readback of the cases above. Anyone with an RDNA 2 adapter who can run a raw gfx10 compute shader would settle it.

AI-assisted.

@rasptum

rasptum commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Independent Windows rerun at 1afe5a69

Measured on a physical machine, not CI. The PR head 1afe5a69e748b408b7f38c84b0879abe85cd2d41 was merged into origin/main 4c1b27dd first, so the tree under test is merge commit c6583bb0 (merge was clean).

Command

ctest --preset dev --no-tests=error -R "^startup_(gfx2hlsl_cvt_pk_u8|gfx2hlsl_interp_f16)_(controls|warp|hardware)$" --output-on-failure

Result: 3/3 passed, 0 failed, no skips

Test Result Time
startup_gfx2hlsl_cvt_pk_u8_controls Passed 0.20 s
startup_gfx2hlsl_cvt_pk_u8_warp Passed 0.85 s
startup_gfx2hlsl_cvt_pk_u8_hardware Passed 2.70 s

Environment, copied from the draw runs themselves:

WARP:

backend=warp debug_layer=enabled
adapter=Microsoft Basic Render Driver vendor=1414 device=008c
driver=10.0.19041.5794

Hardware:

backend=hardware debug_layer=enabled
adapter=NVIDIA GeForce RTX 3060 vendor=10de device=2503
driver=32.0.15.9636

Host: Windows 10 Pro 22H2, build 19045.6466; MSVC x64 Debug (dev preset); DXC 1.7.2308.16 (Windows SDK 10.0.26100.0).

Notes

  • The debug layer was available and enabled on both adapters, and the test reported no debug-layer errors. No capability skip: every leg returned 0, so this is a pass and not a skip.
  • --no-tests=error matched exactly the three expected names. The interp_f16 half of the filter belongs to a different branch and is not registered here, so it selected nothing extra.
  • Not covered by this run: the /O2 (release) configuration. The workflow skips build-and-test-release on pull requests (if: github.event_name != 'pull_request'), so nothing has exercised this change at /O2 yet.
  • What this establishes: the new v_cvt_pk_u8_f32 lowering and its drawn pixel output agree with the fixture's expected words on WARP and on the RTX 3060 at this head. It says nothing about any other form or about title startup.

@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 1afe5a69e748b408b7f38c84b0879abe85cd2d41. No blocking findings.

Summary: v_cvt_pk_u8_f32 is lowered only where S0 is +0.0 or an integer in 1..255. Constants are checked at translation. Registers must be proven by an unmodified v_cvt_f32_ubyte0..3 in straight-line, full-exec code, or the caller must select pk_u8_s0_contract. The merge expression matches the ISA text. Title and body are neutral and the CI jobs pass.

Checked

  • Every VGPR write goes through set_v (rg for other emitters found none), so discarding the proof there covers all later writes, including loads and multi-register results.
  • v_cvt_f32_ubyte* yields exactly float(0..255), so the proof is sound. It is skipped with modifiers, with branches, or when exec is not the entry mask.
  • The contract is off by default and is recorded in the pipeline notes when used.
  • The pipeline kwargs in startup_gfx2hlsl_lane_alu.py are optional, so other families are unchanged.
  • The CMake and windows.yml entries sit next to the sibling cvt_alu entries.

Commands run locally on Linux (output summarised)

  • python3 -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py -> all ok, rc=0 (translation controls only; no GPU draw was run locally).
  • python3 -B tests/startup_gfx2hlsl_cvt_alu_check.py -> all ok, rc=0 (shared runner is unaffected).
  • python3 tests/docs_links_check.py -> 18 files, 393 links OK.
  • python3 tests/bidi_controls_check.py -> OK.
  • git diff --check origin/main...HEAD -> clean.
  • Mutation 1: removed the byte_float_v discard in set_v -> the "float multiply is refused" control fails.
  • Mutation 2: widened the constant bound from 255 to 256 -> "refused forms stay refused" fails.
  • Mutation 3: dropped the has_branches guard -> "no proof in a program with branches" fails.
  • All three mutations were reverted with git checkout -- ..

P1: none.

P2: none.

Nits

  • docs/experiments.md is a long single paragraph. Splitting out the "proven register" rule would make it easier to read.
  • getattr(self, 'byte_float_v', set()).discard(n) runs on every set_v. Initialising the set in __init__ would be simpler. This is not blocking.

Not run: Windows WARP/hardware draw (x2w_pipe.py win2025). This is a re-run of the CI Windows job, which passed on this head. A maintainer who wants a local GPU readback should run startup_gfx2hlsl_cvt_pk_u8_warp.

AI-assisted; commands listed were executed locally.

@Ybucaille

Copy link
Copy Markdown
Contributor Author

Thanks @rasptum for the RTX 3060 / WARP run, and @tedin7 for the review and the mutations.

Nits:

  • The docs one is done in 7376983: the proven-register rule now has its own paragraph.
  • I kept getattr(self, 'byte_float_v', ...) as is. Several checks build bare Stage objects (Stage.__new__, or the Probe helpers in startup_gfx2hlsl_*_check.py) that skip both __init__ and translate(). They then call set_v or the op handlers directly. Initialising the set in __init__ or translate() and reading it directly broke 14 of those checks with AttributeError. The guard follows the existing uniform_v pattern for the same reason.

That commit only changes docs/experiments.md, so the draws above still apply.

AI-assisted.

@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 73769831d758cf260248bfee694549dbe54480e0 (comment only, no approval).

Verdict: logic looks sound; one blocker, the branch conflicts with main.

Findings

  • P1: GitHub reports mergeable: CONFLICTING. git merge-tree --write-tree origin/main HEAD reports a content conflict in tools/startup/gfx2hlsl.py (windows.yml, CMakeLists.txt and docs/experiments.md auto-merge). Please rebase and re-run the checks below.
  • P2: none.
  • Nit: getattr(self, 'byte_float_v', set()).discard(n) in set_v allocates a throwaway set on every register write when the attribute is absent. Initialising byte_float_v in the Stage constructor would remove the getattr/setdefault indirection in both places.

What I checked

  • The constant S0 gate (+0.0 or an integer in 1..255) matches what tools/compute_store_aot.py accepts for the same opcode, and the PR text states the undefined flt32_to_uint8 behaviour honestly.
  • A register S0 is accepted only when it was written by v_cvt_f32_ubyte0..3 with no modifier, with straight-line code and full exec, and with no write since. Every write goes through set_v, which clears the proof, including the fma_mix path. Otherwise it is refused unless the fixture-only pk_u8_s0_contract is selected, and that choice is recorded in the pipeline notes.
  • The byte insert formula matches the ISA text: mask 0xff << 8*S1[1:0], merge into S2.
  • CI on this head: Linux checks, gfx1013 fixtures and Windows build all pass. The /O2 job and the coverage chart job were skipped. I did not run the Windows WARP job (win2025), because the PR's own Windows CI job passed and the draw test is in that job.

Commands run locally

  • python3 tests/startup_gfx2hlsl_cvt_pk_u8_check.py -> v_cvt_pk_u8_f32: 8 draw sets: ok, mutation changes draw set 0: ok
  • python3 tests/startup_gfx2hlsl_lane_alu.py -> accepted forms translate: ok, refused forms stay refused: ok, Self-test passed.
  • python3 tests/docs_links_check.py -> Checked 18 Markdown files and 393 links.
  • python3 tests/bidi_controls_check.py -> no bidirectional controls.
  • git diff --check origin/main...HEAD -> clean
  • Mutation 1: forced the proof check to always fail (src[1] not in byte_float_v replaced by False). The tests failed with unproven S0 refused without the contract, so the mutation was killed.
  • Mutation 2: removed the proof-clearing discard in set_v. The tests failed with a float multiply is refused without the contract, so the mutation was killed.
  • Both mutations were reverted with git checkout -- ..

AI-assisted; commands listed were executed locally.

@Ybucaille

Copy link
Copy Markdown
Contributor Author

Merged main (4b9490ac) into the branch; new head 2487e8f8, mergeable again. One conflict in tools/startup/gfx2hlsl.py: Pipeline.__init__ now takes both ps_attr_map (from main) and pk_u8_s0_contract. The three PR commits are unchanged.

Re-run locally on the new head: startup_gfx2hlsl_cvt_pk_u8_check.py, startup_gfx2hlsl_lane_alu.py, startup_translator_check.py, docs_links_check.py, bidi_controls_check.py, plus all 41 tests/startup_gfx2hlsl_*_check.py (including the ones main just added): all exit 0. git diff --check origin/main...HEAD is clean.

On the byte_float_v nit: kept the getattr, since the bare Stage objects in the checks skip __init__ (see my earlier comment).

@tedin7, since you offered: could you run the Windows draws on 2487e8f8 when your server is free?

ctest --preset dev --no-tests=error -R "^startup_gfx2hlsl_cvt_pk_u8_(controls|warp|hardware)$" --output-on-failure

The /O2 (release) configuration is still untested for this change.

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

Re-review of head 2487e8f8888c5f78acf8c06c183856b32aa85bf9 (comment only, no approval).

Verdict: the earlier P1 (merge conflict with main) is resolved. No blocking findings.

Earlier P1 check

  • GitHub now reports mergeable: MERGEABLE.
  • git merge-tree --write-tree origin/main HEAD returns a clean tree, with no conflict in tools/startup/gfx2hlsl.py.
  • The head is a merge of main into the branch. The diff against main is 6 files, +246/-6, the same scope as before.

CI on this head: Linux checks, gfx1013 fixtures and Windows build pass. The /O2 job and the coverage chart job were skipped.

Commands run locally on Linux (output summarised)

  • python3 -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py -> all ok, rc=0
  • python3 -B tests/startup_gfx2hlsl_cvt_alu_check.py -> all ok, rc=0
  • python3 tests/docs_links_check.py -> 18 files, 405 links OK
  • python3 tests/bidi_controls_check.py -> OK
  • git diff --check origin/main...HEAD -> clean
  • Mutation: widened the constant bound from 255 to 256 in tools/startup/gfx2hlsl.py. The new check fails. The change was reverted with git checkout -- ..

P1: none.

P2: none.

Nits (unchanged, not blocking)

  • getattr(self, 'byte_float_v', set()).discard(n) in set_v allocates a set on every write. Initialising byte_float_v in the constructor would remove the getattr/setdefault indirection.

Not run: a Windows WARP/hardware draw. The CI Windows job passed on this head. A maintainer who wants a local GPU readback should run startup_gfx2hlsl_cvt_pk_u8_warp.

AI-assisted; commands listed were executed locally.

…pk-u8

# Conflicts:
#	tools/startup/gfx2hlsl.py
@Ybucaille

Copy link
Copy Markdown
Contributor Author

Merged main again (13 new commits, conflicts in set_v, the v_cvt_f32_* path with the new low16_only operands, and Pipeline.__init__ with srv_formats; both sides kept). New head 77b36ddd, mergeable; all tests/startup_gfx2hlsl_*_check.py and startup_translator_check.py pass locally. @tedin7, please use 77b36ddd rather than 2487e8f8 for the Windows draws.

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

Re-review of head 77b36dddbf6b69b67eaba4816311fe9ec840fa00 (a merge of main only; the PR's own diff is unchanged in substance since the previous reviewed head).

Scope: v_cvt_pk_u8_f32 lowering in tools/startup/gfx2hlsl.py, new tests/startup_gfx2hlsl_cvt_pk_u8_check.py, lane_alu fixture hook (pipeline kwargs), CMake and workflow wiring.

Commands executed locally in a clean worktree of this head:

  • git diff --stat origin/main...HEAD: 6 files, +246/-6.
  • python3 -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py: four lanes translated ok; 8 draw sets ok; mutation changes draw set 0 ok.
  • python3 -B tests/startup_gfx2hlsl_lane_alu.py: accepted forms translate ok; refused forms stay refused ok.
  • python3 tests/docs_links_check.py: 18 files, 407 links, ok.
  • python3 tests/bidi_controls_check.py: 1489 files, no bidi controls.
  • git diff --check origin/main...HEAD: clean.
  • Mutation 1: disabled the constant-source domain refusal (if False). The refused-forms check fails, so it is killed.
  • Mutation 2: changed the byte-select mask & 3u to & 7u. The translated-text check fails, so it is killed.
  • Worktree is clean after git checkout -- ..
  • gh pr checks: Linux checks, gfx1013 fixtures and Windows build/test are green. The /O2 job and the chart publish were skipped. The PR is mergeable.
  • I did not run the Windows VM myself. The draw tests (WARP and hardware) are covered by the PR's own Windows CI job.

Findings:

  • P1: none.
  • P2: none.
  • Nit: byte_float_v is read through getattr(self, ..., ()) and created through __dict__.setdefault. A normal attribute set in __init__ would be simpler and would not hide a typo. Optional.
  • Nit: set_v discards the proof, but any future path that writes self.v[...] without set_v would keep a stale proof. Today I found no such path. A short comment at the proof site would help.

Semantics: the formula D = (S2 & ~(0xff << 8*S1[1:0])) | ((cvt(S0) & 0xff) << 8*S1[1:0]) matches the ISA text. Leaving the undefined flt32_to_uint8 cases out by requiring S0 to be +0.0 or an integer in 1..255 is conservative, and the proof/contract split is clear. Titles and text are neutral and clean-room.

Verdict: no blocking issues from this review.

AI-assisted; commands listed were executed locally.

The proof set was read through getattr and created through
__dict__.setdefault, which would hide a misspelt name. An immutable
class default, replaced by a real set on the first proof, keeps probes
built without __init__ working, as low16_only_v does. The proof site
now says that every VGPR write must drop it.
@Ybucaille

Copy link
Copy Markdown
Contributor Author

Thanks @tedin7. Both nits done in e556be5: byte_float_v now has an immutable class default () replaced by a set on the first proof (same pattern as low16_only_v), so the getattr / __dict__.setdefault indirection is gone and probes without __init__ still work. The proof site now says that every VGPR write must go through set_v, which drops the proof. Mutation tried: skipping the drop in set_v makes startup_gfx2hlsl_cvt_pk_u8_check.py fail. All startup_gfx2hlsl_* checks pass locally.

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

Re-review of head e556be5 (changes since 77b36dd: byte_float_v becomes a class default, set_v and the proof/lookup sites no longer use getattr/__dict__.setdefault).

Verdict: no P1, no P2.

What I checked

  • git diff --stat origin/main...HEAD: 6 files, gfx2hlsl.py + one new check + lane_alu hook + CMake/CI/docs. Title is neutral, PR is MERGEABLE.
  • CI on this head: Linux Python checks, gfx1013 fixtures and Windows build/test all pass (the /O2 and chart jobs are skipped).
  • The only VGPR write path in the emitter is set_v (single emit('v%d = ...') at gfx2hlsl.py:630), and set_v is where the proof is dropped. The new comment at the proof site states this contract. Good.
  • The class default () is falsy, so set_v skips the discard until the first proof creates a per-instance set. No shared mutable state between stages.
  • The proof is only granted with no modifiers, known exec and no branches. A constant S0 outside +0.0 / 1..255 is refused. Any other register needs the explicit pk_u8_s0_contract, and that choice is recorded in pipe.notes.
  • The formula matches the ISA text quoted in the comment: D = (S2 & ~(0xff << 8*S1[1:0])) | ((cvt(S0) & 0xff) << 8*S1[1:0]).

Commands run locally on the head

  • python3 -B tests/startup_gfx2hlsl_cvt_pk_u8_check.py: all steps ok (four lanes translated, 8 draw sets, mutation changes draw set 0, accepted/refused forms).
  • python3 -B tests/startup_gfx2hlsl_lane_alu.py: Self-test passed.
  • python3 tests/docs_links_check.py: 18 files, 407 links ok.
  • python3 tests/bidi_controls_check.py: no bidirectional controls.
  • git diff --check origin/main...HEAD: clean.

Mutations (each reverted with git checkout -- .)

  1. set_v no longer discards the proof: killed (AssertionError: a float multiply is refused without the contract).
  2. Select mask & 3u changed to & 1u: killed (translated text reads 42fe0000 12345676 01fe7f80).

Windows: I did not run the Win2025 pipeline. The WARP/hardware draw tests are registered in CMake and the Windows CI job passed on this head.

Nits (non-blocking)

  • nit: byte_float_v is typed as a tuple at class level but becomes a set on first use. A one-word comment would make the switch explicit.

AI-assisted; commands listed were executed locally.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Ran head e556be5e on three adapters. All pass. Windows 11 build 26100, debug layer unavailable, FXC and DXC from SDK 10.0.26100.0.

Test Adapter Driver Result
startup_gfx2hlsl_cvt_pk_u8_controls n/a n/a pass
startup_gfx2hlsl_cvt_pk_u8_warp Microsoft Basic Render Driver 10.0.26100.8972 pass
startup_gfx2hlsl_cvt_pk_u8_hardware (XBOX2WINDOWS_ADAPTER=Quadro) NVIDIA Quadro M1000M (Maxwell) 32.0.15.7371 pass
startup_gfx2hlsl_cvt_pk_u8_hardware (XBOX2WINDOWS_ADAPTER=Intel) Intel HD Graphics 530 (Gen9) 31.0.101.2111 pass

Every run printed the same 5 ok lines, and the FXC and DXC mutations each failed as expected, for example fxc v_cvt_pk_u8_f32 mutation: fails as expected, reads 00000000,00000100,007f0000,ff000000.

AI-assisted.

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

The original S0 finding is resolved. Ordinary translation refuses an unproven value, and an explicitly selected profile records its input contract. All four byte selectors and the lost-S2 mutation verified independently on WARP and RTX 4060 Laptop.

@R0Y00
R0Y00 merged commit 2017e1b into R0Y00:main Nov 25, 2026
5 checks passed
@Ybucaille
Ybucaille deleted the startup/gfx2hlsl-cvt-pk-u8 branch November 25, 2026 06:17
legacy-code-archaeologist pushed a commit to legacy-code-archaeologist/Xbox2Windows that referenced this pull request Nov 25, 2026
flt32_to_uint8 is undefined in the RDNA ISA guides, so the startup
translator only accepted +0.0 or an integer in 1..255. It was measured on
two RDNA 2 parts (RX 6900 XT gfx1030 and a gfx1036 iGPU, Windows, ROCm
7.2): round to nearest even, saturate to 0..255, NaN and negatives to 0,
with no difference under s_denorm_mode 0. Round toward zero in MODE makes
it truncate, so s_setreg_imm32_b32 that puts a non-zero rounding mode in
MODE[1:0] is now refused instead of ignored.

The byte_float_v proof and the pk_u8_s0_contract fixture switch only
existed for the restricted domain and are removed.

Refs R0Y00#2023 (item K), follows R0Y00#2101
MasterDomino pushed a commit that referenced this pull request Nov 25, 2026
)

* shader: lower v_cvt_pk_u8_f32 for any binary32 source in gfx2hlsl

flt32_to_uint8 is undefined in the RDNA ISA guides, so the startup
translator only accepted +0.0 or an integer in 1..255. It was measured on
two RDNA 2 parts (RX 6900 XT gfx1030 and a gfx1036 iGPU, Windows, ROCm
7.2): round to nearest even, saturate to 0..255, NaN and negatives to 0,
with no difference under s_denorm_mode 0. Round toward zero in MODE makes
it truncate, so s_setreg_imm32_b32 that puts a non-zero rounding mode in
MODE[1:0] is now refused instead of ignored.

The byte_float_v proof and the pk_u8_s0_contract fixture switch only
existed for the restricted domain and are removed.

Refs #2023 (item K), follows #2101

* tests: pin the MODE write offset, width and hwreg refusal for cvt_pk_u8

Three mutations of the s_setreg_imm32_b32 MODE handling reported on
#2467 survived: dropping the offset shift, defaulting the width to 1
and skipping the non-hwreg refusal. Add accepted and refused forms
that kill each one, and note that the offset < 2 bound is a shortcut.

* Advance the startup shader cache revision to 15

The v_cvt_pk_u8_f32 lowering changes the HLSL emitted for forms that main
already accepted (constant and proven-register S0, MODE writes), so a
cache built under revision 14 would keep the old clamp lowering and the
earlier refusals. Draw and compute revisions move together.

* Guard v_cvt_pk_u8_f32 with an integer compare on the raw bits

DXC compiles the float guard to fcmp fast ogt, so the NaN result relied
on the driver honouring the ordered compare. (bits - 1u) < 0x7f800000u is
true exactly from the smallest positive subnormal to +inf, so fast-math
cannot fold it. The test evaluator now fails with a clear message when
the lowering text no longer matches. The docs name both devices that
matched (gfx1030, gfx1036) and the two open assumptions: IEEE mode and
the initial rounding mode from PGM_RSRC1.FLOAT_MODE.

* Decode float-printed immediates in s_setreg_imm32_b32 MODE writes

disasm prints some inline constants as floats (1.0, 0.5). They were
refused as writing MODE[1:0] even when bits 1:0 are clear. They now read
as their binary32 bits, and an immediate that cannot be decoded is
refused with its own reason. Refusal stays the safe direction.

* Add the v_cvt_pk_u8_f32 hardware probe and its stored output

The measurement behind the lowering could not be rerun: the probe lived
outside the repository and the raw words were not attached. Add the
probe as tools/rdna_cvt_pk_u8.py (builds its kernels with clang, runs
through HIP, writes a compact TSV and compares it with the stored one)
and store the output in tests/data/rdna_cvt_pk_u8_measured.tsv. The
gfx1030 and gfx1036 runs gave identical rows, so one set is kept.

The check now validates every stored row without a GPU: nearest-even
for the default word, truncation for the rtz word, with negative
controls from the other models. The docstring points at the in-repo
probe.
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.

8 participants