Skip to content

Shaders: declare wave operations from the wave intrinsics made - #2105

Open
0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:shaders/wave-ops-flag-from-the-ops-made
Open

0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:shaders/wave-ops-flag-from-the-ops-made

Conversation

@0xkn0wh3r3

@0xkn0wh3r3 0xkn0wh3r3 commented Nov 25, 2026 •

Copy link
Copy Markdown

The problem

Some translated programs are refused by the DXIL validator: the module declares wave operations (bit 19, flags::WAVE_OPS) but contains none. The validator reports "Flags must match usage".

The flag was raised by the lowerings that make cross-lane operations, at their start, before the operation was made:

  • cross_lane, the gate that ds_swizzle, v_readlane, v_mbcnt, DPP and the like pass before reading their sources, raised it;
  • so did the host-lane v_readlane and v_mbcnt branches.

When a source then had no DXIL value, the instruction was left out. When nothing read its result, the program translated with the flag up and no wave operation in it.

How it was found

By validating random programs (#1335's generator with a validation pass, validator 1.6): 2 of 164,934 translated programs out of 2,000,000 had the flag without a wave operation.

The fix

  • Shader::op now raises the flag as it makes a wave or quad intrinsic: DXIL operations 110 to 123 (WaveIsFirstLane .. QuadOp) and 165 to 167 (WaveMatch .. WaveMultiPrefixBitCount).
  • The ten places that raised it beforehand no longer do. cross_lane is now only the check.

The flag now follows the operations actually in the module. With this, none of 123,669 translated programs out of 1,500,000 has a flag mismatch either way (with #2056 for the 64-bit flag).

Testing

  • New test emit::lower::tests::a_cross_lane_operation_left_out_declares_no_wave_operations: swizzles a register holding s0 (user SGPRs no root parameter fills, so no value). The swizzle is left out, and the test checks the flag stays down; it is up on main, so the test fails there. It then swizzles the thread id and checks the flag is up.
  • The existing tests that check Lowered::wave_ops (the waterfall loops, ballots, lane reads) pass unchanged.
  • 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 (NVIDIA D3D12 (70564ae): stride_tests & synthetic_tests fail due to HEAP_VIEWS (2M > 1M NVIDIA limit) and Stride::Structured with rawBufferLoad #1835).
  • cargo clippy -p victory-shader --all-targets --features host: clean.

Related

#2056 fixes the 64-bit flag; the "none of 123,669" figure above is with both.

The module's wave operation flag (`flags::WAVE_OPS`) was raised by the
lowerings that make cross-lane operations, at their start: `cross_lane`,
the gate `ds_swizzle`, `v_readlane`, `v_mbcnt`, DPP and the like pass
before reading their sources, raised it, and so did the host-lane
`v_readlane` and `v_mbcnt` branches. When a source then had no DXIL
value the instruction was left out, and when nothing read its result the
program translated with the flag and no wave operation. The validator
refuses such a module ("Flags must match usage", bit 19 declared and not
used).

`Shader::op` now raises the flag as it makes a wave or quad intrinsic
(DXIL operations 110 to 123, `WaveIsFirstLane` .. `QuadOp`, and 165 to
167, `WaveMatch` .. `WaveMultiPrefixBitCount`), and the ten places that
raised it beforehand no longer do; `cross_lane` is only the check.

Found by validating random programs (Marvisak#1335's generator with a validation
pass, validator 1.6): 2 of 164,934 translated programs out of 2,000,000
had the flag without a wave operation. With this, none of 123,669 out of
1,500,000 has a flag mismatch either way (with Marvisak#2056 for the 64-bit
flag).

The new test swizzles a register holding `s0` (user SGPRs no root
parameter fills: no value), left out, and checks the flag stays down (it
is up on `main`); of the thread id, that it is up.

@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_cross_lane_operation_left_out_declares_no_wave_operations — passed
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean fix declaring WAVE_OPS only when wave intrinsics are actually emitted. Looks good.

AI-assisted (Gemini 4 Neon).

@fiestorra

Copy link
Copy Markdown
Contributor

Reviewed with the census: the change is right, it clears three programs on its own, and with #2016 the class goes to zero

The direction is the one that has cost this tree the most this week — declare what the module actually contains, not what the lowering intended — so I measured it rather than argued about it, on a local 1.0.2.1 archive (26,503 containers), validator SDK 10.0.26100 dxil.dll 1.9.

Alone, over the whole corpus:

build Flags must match usage validated
main 940d32f 871 23,918
this branch 868 23,918

So it clears three programs here. That is not a criticism — it is the size of the problem it names — but it is worth putting in the record, because the remaining 868 are all VS_…VfxVBuffer… vertex programs whose mismatch is a different bit (the UAV flag, #2016). Anyone reading "871 flags failures" and this pull request should know it removes three of them.

Together with #2016, which merges cleanly onto it (I merged, built and ran):

build Flags must match usage validated
#2105 + #2016 0 24,785

and cargo test -p victory-shader --locked --lib 249 passed, 3 ignored. So the two are complementary: mine is the UAV bit, yours is wave ops, and the class closes only with both.

Three things I checked in the diff and can vouch for:

  • the opcode ranges are the right ones: 110–123 covers WaveIsFirstLane … QuadOp, and 165–167 covers WaveMatch … WaveMultiPrefixBitCount. Quads belong in the same flag — DXIL.rst lists QuadReadLaneAt, QuadOp and QuadVote in bit 19's criterion — so raising it for 122/123 is correct, not overreach;
  • nothing reads the flag mid-emission in a way this breaks: the only remaining read is Lowered.wave_ops at lower.rs:1329, and that field is consulted by tests only (I searched the crates: no production reader), so raising later cannot make a decision stale;
  • the ten eager sites are gone on the branch, so there is no path left that declares the flag without emitting one.

One residual risk, which I would write down rather than "fix": the flag is now raised when the op is emitted, so an op that is emitted and then discarded by a later pass would bring the original error back. Does anything drop emitted ops today? If the answer is "structurisation can prune a block", then the correct place is the same place the module's flags are serialised — which is what my #2016 turned into feature_info, and the pair would then be immune. That is a question, not a finding: I did not find a pruning pass in the time I gave this.

No game data is involved: counts, opcode numbers and error text.

Written with an AI assistant.

@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. Wave operations declared from wave intrinsics masks (+1 test) — capability declaration from actual intrinsic usage.

@0xkn0wh3r3

Copy link
Copy Markdown
Author

Thanks for the census numbers, and for running it with #2016. Good to have on record that this PR clears 3 and the other 868 are the UAV bit.

On the question: I checked the writer (emit/bitcode.rs, Module::write). It leaves out two things:

  • Phis nothing reads (live()). A wave op can't be one of those. Shader::op emits a call through Module::call, which pushes it into its block's body, and every body instruction is written.
  • Blocks not reachable from the entry. Layout is a DFS from block 0, so a block nothing branches to is never written, and neither are its instructions. Lowering makes no DXIL block for an unreachable IR block (Lower::block: "unreachable: none"). The helper blocks it adds (waterfalls, guarded reads) are branched to when they're made. A CondBr keeps both successors even when its condition is a constant.

So today nothing drops an op after it's emitted, and I didn't find a pruning pass either. Still, block reachability in the writer is the one place it could happen: a future change that emits into a block and then never branches to it would bring the error back. I agree the robust place is wherever the flags are serialised. If #2016's feature_info lands first, I'll rebase this onto it and derive the wave bit there from the instructions that get written.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

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

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

flags::WAVE_OPS is now raised in Shader::op (emit/shader.rs) for DXIL opcodes 110-123 and 165-167. The ten places in emit/lower.rs that raised it early are removed (cross_lane, lane_bit, quad_pick, v_readlane/v_readfirstlane, v_mbcnt, ballots, reductions). I found no wave intrinsic emitted outside Shader::op, and the opcodes it leaves out (135/136, 222) are not used. The new test fails on main and checks both directions. A commenter's census shows it clears 3 "Flags must match usage" programs, and with #2016 the class goes to zero. CI has not run on this head.


Generated by Claude Code

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