Skip to content

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

Closed
0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:shaders/int64-flag-only-for-emitted-ops
Closed

0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:shaders/int64-flag-only-for-emitted-ops

Conversation

@0xkn0wh3r3

@0xkn0wh3r3 0xkn0wh3r3 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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

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 Marvisak#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.
@Dathinel

Copy link
Copy Markdown
Contributor

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.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

145 passed / 0 failed / 2 ignored. 64-bit integer operations declared only where they are made (+1 test) — capability declaration precision for i64 ops; relates to #1599's dword compares and the 64-bit atomics cluster.

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-shader --lib a_64_bit_operation_left_out_declares_no_64_bit_operations — passed
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean fix ensuring INT64_OPS is only declared when 64-bit integer operations are actually emitted. Looks good.

AI-assisted (Gemini 4 Neon).

@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Owner

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

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head c6bc034 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).

@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

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 0xkn0wh3r3 closed this Nov 25, 2026
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 25, 2026
Three image instructions translated into DXIL operations the validator
rejects (and a driver may refuse, or run wrongly), where refusing them
is the right outcome:

- a gather (`image_gather4_lz`, `_l`, `_lz_o`, `_l_o`) from a 1D or 3D
  image: "gather requires resource declared as texture/2D/Cube/2DArray/
  CubeArray";
- a gather with offsets (`_o`) from a cube or cube array: "out of bound
  offset must be undef";
- `image_load_mip` from a UAV, which is one level of its resource: "uav
  load don't support mipLevel/sampleIndex" (and the typed-UAV-load flag
  was left out, so "Flags must match usage" too).

They are now refused with those reasons, as the gathers DXIL cannot do
already are ("a gather with implicit level", "a gather of several
channels").

Found by validating random programs (the generator of Marvisak#1335, with a
validation pass, validator 1.6): 5 of 16,207 programs that translated
out of 200,000; with this and Marvisak#2056 every one of them validates. The new
test lowers each into LDS, so the refusal ends the translation, and the
2D gather and the plain UAV load next to them, which still lower; it
fails on `main`, and without either of the checks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants