Shaders: declare 64-bit integer operations only where they are made #2056

Closed
github-bot wants to merge 1 commit from gh-pr/2056/head into gh-pr/2056/base
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 04:13 UTC · original
0xkn0wh3r3:shaders/int64-flag-only-for-emitted-ops → main · +43 −1 · 1 files · 1 commits · closed without merging

The problem

Some translated programs failed validation with "Flags must match usage": the module declared 64-bit integer operations it did not contain. The validator refuses such a module (the declared flags have bit 20, which the actual ones lack).

The mechanism: Lower::src64 raised the module's 64-bit integer flag (flags::INT64) before reading its operand.

  • When the read failed (a register pair with no DXIL value, "an unread operand"), the instruction was left out. When nothing read its result, the program still translated, with the flag and no 64-bit instruction.
  • An inline or literal constant raised the flag too, and is no instruction. A 64-bit compare whose other source then failed left the same mismatch.

Found by validating random programs (the generator of #1335, run on main with a validation pass, validator 1.6):

programs translated failed with "Flags must match usage"
main 8,079 of 100,000 82, all for this flag
with the change 16,207 of 200,000 none

The fix

The flag is now raised by the instructions themselves, once they are made:

  • src64, where it builds the value from a register pair or zero-extends a 32-bit one.
  • Its callers, at their own 64-bit instruction, once every source was read: the 64-bit shifts, v_mad_u64_u32/v_mad_i64_i32 and the 64-bit compares.

A constant source alone no longer raises it.

Testing

  • New test emit::lower::tests::a_64_bit_operation_left_out_declares_no_64_bit_operations: lowers a 64-bit shift and compare of s[0:1] (user SGPRs no root parameter fills) and checks the flag stays down. It is up on main (fails with [d7000002, 00000080] left out). It also lowers them on v[0:1], where both are made, and checks the flag is up.
  • cargo test --release -p victory-shader --lib on an RTX 5070 Ti: 245 passed. The 3 failures (emit::stride_tests::* x2, emit::synthetic_tests::gathers_at_a_level_match_the_hardware) fail the same way on main on this GPU (#1835).
  • #1335's random programs, 1,000,000 with overflow checks: no panics or differences, before and after.
  • cargo clippy -p victory-shader --all-targets --features host: clean.

Independent of #1090 (constant _f/_t 64-bit compares read no operands) and #1599 (64-bit compares on their dwords). With #1599 the compare arm here goes away and the rest still applies.

**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 04:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/2056) `0xkn0wh3r3:shaders/int64-flag-only-for-emitted-ops` → `main` · +43 −1 · 1 files · 1 commits · closed without merging ## The problem Some translated programs failed validation with "Flags must match usage": the module declared 64-bit integer operations it did not contain. The validator refuses such a module (the declared flags have bit 20, which the actual ones lack). The mechanism: `Lower::src64` raised the module's 64-bit integer flag (`flags::INT64`) before reading its operand. - When the read failed (a register pair with no DXIL value, "an unread operand"), the instruction was left out. When nothing read its result, the program still translated, with the flag and no 64-bit instruction. - An inline or literal constant raised the flag too, and is no instruction. A 64-bit compare whose other source then failed left the same mismatch. Found by validating random programs (the generator of #1335, run on `main` with a validation pass, validator 1.6): | | programs translated | failed with "Flags must match usage" | |---|---|---| | `main` | 8,079 of 100,000 | 82, all for this flag | | with the change | 16,207 of 200,000 | none | ## The fix The flag is now raised by the instructions themselves, once they are made: - `src64`, where it builds the value from a register pair or zero-extends a 32-bit one. - Its callers, at their own 64-bit instruction, once every source was read: the 64-bit shifts, `v_mad_u64_u32`/`v_mad_i64_i32` and the 64-bit compares. A constant source alone no longer raises it. ## Testing - New test `emit::lower::tests::a_64_bit_operation_left_out_declares_no_64_bit_operations`: lowers a 64-bit shift and compare of `s[0:1]` (user SGPRs no root parameter fills) and checks the flag stays down. It is up on `main` (fails with `[d7000002, 00000080] left out`). It also lowers them on `v[0:1]`, where both are made, and checks the flag is up. - `cargo test --release -p victory-shader --lib` on an RTX 5070 Ti: 245 passed. The 3 failures (`emit::stride_tests::*` x2, `emit::synthetic_tests::gathers_at_a_level_match_the_hardware`) fail the same way on `main` on this GPU (#1835). - #1335's random programs, 1,000,000 with overflow checks: no panics or differences, before and after. - `cargo clippy -p victory-shader --all-targets --features host`: clean. ## Related Independent of #1090 (constant `_f`/`_t` 64-bit compares read no operands) and #1599 (64-bit compares on their dwords). With #1599 the compare arm here goes away and the rest still applies.
`Lower::src64` raised the module's 64-bit integer flag (`flags::INT64`)
before reading its operand. When the read failed (a register pair with
no DXIL value, "an unread operand") the instruction was left out, and
when nothing read its result the program still translated: with the flag
and no 64-bit instruction. The validator refuses such a module ("Flags
must match usage", the declared flags with bit 20 the actual ones lack).
An inline or literal constant raised it too, and is no instruction: a
64-bit compare whose other source then failed left the same mismatch.

The flag is now raised by the instructions themselves: `src64` where it
builds the value from a register pair or zero-extends a 32-bit one, and
its callers (the 64-bit shifts, `v_mad_u64_u32`/`v_mad_i64_i32` and the
64-bit compares) at their own 64-bit instruction, once every source was
read.

Found by validating random programs (the generator of #1335, run on
`main` with a validation pass, validator 1.6): 82 of 8,079 programs that
translated out of 100,000 failed with "Flags must match usage", all of
them for this flag; with the change, none of 16,207 out of 200,000.

The new test lowers a 64-bit shift and compare of `s[0:1]` (user SGPRs no
root parameter fills) and checks the flag stays down (it is up on
`main`), and of `v[0:1]`, where both are made, that it is up.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:53:17 +00:00
github-bot closed this pull request 2026-11-26 04:53:17 +00:00
Author
Owner

@⁠Dathinel on GitHub · 2026-11-25 04:22 UTC · original

One integration detail for whoever lands #1599 and this PR second: the expectation in the new test needs updating along with the compare arm.

I ran a_64_bit_operation_left_out_declares_no_64_bit_operations on c6bc034 alone (pass), then merged #1599 at 1f6b392 locally, retaining its dword comparison at the single source conflict. The unchanged test failed at [d4e2006a, 00020080] made: the register-sourced equality comparison now correctly leaves INT64 clear.

In the combined tree I kept the two missing-source assertions unchanged, expected INT64 for the register-sourced shift, and expected it clear for the register-sourced comparison. That passed, as did #1599's v_cmp_u64_and_i64_order_by_the_high_dword on the same tree. This is specific to the combined implementation. AI-assisted; tested on Windows.

**@⁠Dathinel** on GitHub · 2026-11-25 04:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/2056#issuecomment-7152791305) One integration detail for whoever lands #1599 and this PR second: the expectation in the new test needs updating along with the compare arm. I ran `a_64_bit_operation_left_out_declares_no_64_bit_operations` on `c6bc034` alone (pass), then merged #1599 at `1f6b392` locally, retaining its dword comparison at the single source conflict. The unchanged test failed at `[d4e2006a, 00020080] made`: the register-sourced equality comparison now correctly leaves INT64 clear. In the combined tree I kept the two missing-source assertions unchanged, expected INT64 for the register-sourced shift, and expected it clear for the register-sourced comparison. That passed, as did #1599's `v_cmp_u64_and_i64_order_by_the_high_dword` on the same tree. This is specific to the combined implementation. AI-assisted; tested on Windows.
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 06:35 UTC · original

Thanks @⁠Dathinel for running it combined with #1599 and posting the merged expectation. I've left the test as it is here, since it matches main today. Whichever of the two PRs lands second should take your combined version: the register-sourced comparison leaves INT64 clear once #1599's dword compare is in, and the register-sourced shift still sets it.

**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 06:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/2056#issuecomment-7154951198) Thanks @⁠Dathinel for running it combined with #1599 and posting the merged expectation. I've left the test as it is here, since it matches `main` today. Whichever of the two PRs lands second should take your combined version: the register-sourced comparison leaves INT64 clear once #1599's dword compare is in, and the register-sourced shift still sets it.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 09:23 UTC · original

Automated triage for the maintainers.
Merge candidate.

In crates/victory-shader/src/emit/lower.rs, Lower::src64 set flags::INT64 before reading its operand. If that read failed and the instruction was dropped, or if the source was only a constant, the module still declared 64-bit operations it did not have, and the validator rejects that ("Flags must match usage"). The flag is now set only where an i64 instruction is actually emitted: in src64's register-pair and zero-extend paths, and in its three callers (64-bit shifts, v_mad_{u,i}64_{u32,i32}, 64-bit compares), after all their sources have been read. I checked that src64 has no other callers and that no early return sits between the new flag sets and the emitted instructions. This is about DXIL validity only and does not change shader semantics.

Overlap: #1599 changes the same compare arm. The thread says the new test's expectation for the compare needs updating if #1599 lands first. #2133 (shifts without INT64) rewrites the same shift arm. Either way, whichever lands second needs a small rebase.

CI: not run (no check runs on this head). A maintainer may need to approve the workflow run.

Recommending a look, not approving.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 09:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/2056#issuecomment-7158273512) <!-- victory-triage v1 head=c6bc034b7e83c1ffdc0c412a0595daef55cf8872 verdict=merge-candidate at=2026-11-25T09:23:41Z --> Automated triage for the maintainers. **Merge candidate.** In `crates/victory-shader/src/emit/lower.rs`, `Lower::src64` set `flags::INT64` before reading its operand. If that read failed and the instruction was dropped, or if the source was only a constant, the module still declared 64-bit operations it did not have, and the validator rejects that ("Flags must match usage"). The flag is now set only where an i64 instruction is actually emitted: in `src64`'s register-pair and zero-extend paths, and in its three callers (64-bit shifts, `v_mad_{u,i}64_{u32,i32}`, 64-bit compares), after all their sources have been read. I checked that `src64` has no other callers and that no early return sits between the new flag sets and the emitted instructions. This is about DXIL validity only and does not change shader semantics. Overlap: #1599 changes the same compare arm. The thread says the new test's expectation for the compare needs updating if #1599 lands first. #2133 (shifts without INT64) rewrites the same shift arm. Either way, whichever lands second needs a small rebase. CI: not run (no check runs on this head). A maintainer may need to approve the workflow run. Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 09:29 UTC · original

Offline measurement on a retail dump

Result: no count change against main.

I merged head c6bc034b onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

**@⁠sinan-cmyk** on GitHub · 2026-11-25 09:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/2056#issuecomment-7158392380) ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head c6bc034b onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 18:51 UTC · original

Closing: upstream 286a00e covers this. Shader::finish now sets and clears the 64-bit integer flag (with the heap and wave-operation flags) from what the DXIL writer actually writes (Module::uses), so a flag can't outlive an operation that was left out.

**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 18:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/2056#issuecomment-7171489089) Closing: upstream 286a00e covers this. `Shader::finish` now sets and clears the 64-bit integer flag (with the heap and wave-operation flags) from what the DXIL writer actually writes (`Module::uses`), so a flag can't outlive an operation that was left out.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2056
No description provided.