Shaders: validate far-jump arithmetic and control-flow entries #2684
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2686"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by XeNS1 on 2026-11-25T18:12:46Z.
gfx10::far_jumpfollowed a target derived from the low-word literal even when the high-word carry was missing, mismatched, or clobbered. A physical getpc / arithmetic / setpc sequence also does not prove a constant target when another branch enters after getpc: the bypassing path can carry a different register pair. Decode and IR previously recomputed this decision from different predecessor sets, which could produce an IR jump to code that was never decoded.Validate the complete local getpc / low arithmetic / matching high carry / setpc chain, including both registers and both halves of the signed offset. Wide host arithmetic handles subtraction of i64::MIN. Within the existing four-instruction lookback, only NOPs, s_waitcnt and s_waitcnt_depctr may separate the operations.
Track discovered branch and jump destinations and leave the setpc indirect if any entry bypasses part of its setup. A later path can invalidate a chain already followed: in that case decode restarts with the additional entries retained, removing speculative code and its errors. Errors are deferred until pending paths have been explored, so an invalid speculative target cannot hide the path that invalidates it. The entry set only grows, and every retry blocks at least one newly invalidated chain. IR consumes the final decoded flow decisions rather than resolving the jump a second time. Entering at getpc remains valid; unreachable data is not scanned for apparent branches. This is conservative local recognition, not general constant propagation.
Eleven new synthetic tests cover arithmetic/carry validation, register and SCC clobbers, signed 64-bit offsets, scheduling controls, entries at low/high/setpc, both traversal orders, late ordinary and resolved-far entries, valid/invalid/out-of-range speculative targets, and flow decisions whose invalidating branch disappears on retry. The CFG negative cases check the indirect IR exit in both wave sizes. The original arithmetic regressions failed 3 tests against main; the CFG regressions failed 5 tests against the first PR revision. All 36 decoder/SP3/IR tests now pass.
Validation on Linux x86-64 / Rust 1.100.0:
cargo check -p victory-shader --all-features --locked --offlinepasses. Changed code is formatted; repository asset policy (184 files), staged policy (2 files), and diff checks pass.Semantics checked against AMD RDNA2 ISA, sections 12.1/12.3 and the LLVM 21.1 far-branch sequence. The decoder/control API changes here overlap #2012's lookup work and will need integration if both land. No GPU or game files used; all instruction fixtures are handwritten from the public ISA. No measured in-game impact claimed. AI-assisted: OpenAI Codex.
Coordination claim.
Imported from Marvisak/VIctory. Originally posted by DrDarkDK on 2026-11-25T18:59:51Z.
Review: APPROVED
No description provided.
Imported from Marvisak/VIctory. Originally posted by DrDarkDK on 2026-11-25T18:15:56Z.
A branch can reach
s_setpc_b64without executing the address setup the lookback validates. For example, a conditional branch can targetsetpcdirectly while fallthrough executesgetpc, the low-word operation, and the carry. The lookback resolves one fixed target for both paths, even though the taken branch can arrive with a different address pair.Could we add that control-flow case and leave the jump indirect when a branch can enter after
getpc? Both the decoder and IR builder callcontrol, so the test should check that they agree.Static review only.
AI-assisted with Codex.
Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T18:38:13Z.
The recognizer is now honest: requiring the matching carry op (addc for add, subb for sub), checking both registers of the pair, and whitelisting only nop/waitcnt as separators closes both holes - a stale getpc+literal pairing and the +/-4GiB-vs-+20 high-word ambiguity. Combining the halves before negating (i128) also fixes the i64::MIN subtraction edge. Test matrix covers every failure mode I can think of, including SCC clobbers and register clobbers at each position in the chain.
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T18:40:29Z.
Independent Windows 11 verification and code review of
38b757ec(on13f91ca):Verification (
rustc 1.98.1MSVC)cargo clippy --locked -p victory-shader --all-targets -- -D warningsand--features host: 0 warnings.gfx10: 21 passed, 0 failed (all 5 new far-jump tests pass).ir::tests(9 passed),structure::tests(11 passed, 1 ignored),emit::flow_tests(1 passed).Code Review Notes
s_getpc_b64->s_add_u32/s_sub_u32-> matchings_addc_u32/s_subb_u32->s_setpc_b64), GFX10 inline constant decoding (128..=192,193..=208,0xFF),i128negation safety, and stricts_nop/s_waitcnt/s_waitcnt_depctrclobber rejection all look solid.far_jumpwalks backward by byte offset throughbefore(at)without checking whether a branch targets the interior of the chain ors_setpc_b64directly, there is also adecodevsir::build_boundedmismatch:0x00: s_cbranch_scc1 getpc; 0x04: s_branch setpc. Whendecode()follows0x04tosetpcfirst,founddoes not yet containgetpc..addc, sofar_jumpreturnsIndirect(Flow::Stop) and never decodestarget.pendingthen decodesgetpc..addcintofound.ir::build_boundedrunsgfx10::controlover the completedlist, now seesgetpc..addcimmediately beforesetpc, classifies it asFlow::Jump(target), and failsblock_at(target)(jump into a block) becausetargetwas never decoded.s_setpc_b64are not branch targets (or handling the chain consistently acrossdecodeandir::build_bounded) avoids both false-positive static jumps and this decode/IR divergence.Imported from Marvisak/VIctory. Originally posted by XeNS1 on 2026-11-25T18:57:33Z.
Addressed the CFG-entry and traversal-order findings in
6dbac145d9. Thanks @DrDarkDK and @fuzeofficlal for the concrete cases and the Windows verification of the first revision.The decoder now records branch/jump destinations and rejects a setup with an entry in
(getpc, setpc]. This includes destinations discovered later on another path, including resolved far jumps. If such an entry invalidates a chain already followed, decode restarts and discards speculative instructions and errors. Entries are retained across retries so removing the invalidating path cannot make the same chain oscillate between direct and indirect. Errors from speculative targets are deferred until pending paths have been visited.IR uses the final decoded flow decisions instead of repeating the physical lookback on the final sorted instruction list. This covers the
cbranch getpc; branch setpcordering and the case where the branch invalidating a chain is itself removed during retry.Six additional synthetic tests cover low/high/setpc entries, both visitation orders, late direct/far entries, valid/invalid/out-of-range speculative targets, and retained indirect decisions. A positive control still resolves a branch to getpc and ignores an unreachable data word resembling a branch. The CFG negative cases also check indirect IR exits for wave32 and wave64. With these tests, the previous PR revision had 31 pass / 5 fail; the updated code has 36 pass / 0 fail. Strict harness Clippy, all-features shader library check, repository/staged asset policy and diff checks pass locally. Fixtures are handwritten public-ISA instructions; no game data or game execution was used.
Windows CI on this exact head: passed both workspace Clippy configurations and the workspace tests (run).
AI-assisted with OpenAI Codex.
Imported from Marvisak/VIctory. Originally posted by pablogonz12 on 2026-11-25T19:41:16Z.
AMD/RADV offline check of the head commit (no game run).
origin/main13f91caee.victory-shader --example retranslate, then dxil-spirv (master),spirv-val --target-env vulkan1.3, and a realvkCreateCompute/GraphicsPipelineson RADV (vkpipe, one process per program, so a driver crash or hang is recorded per program rather than stopping the batch).Result, base
13f91caeevs head6dbac145d: both translate 749 / 751 (the same 2 refused in both:a lane mask read as data is not supported). 0 of the 751 programs' DXIL bytes change, no pipeline result or time differs.None of these programs takes a far jump, so the validation this PR adds is not reached by the dump: the new tests are the evidence, and
victory-shader's test target does not build on Linux here (Windows validator module). No objection from this side.Not tested in a game run; not tested on NVIDIA.
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T19:51:06Z.
Re-verified
6dbac14on Windows 11 (rustc 1.98.1MSVC,13f91ca):Verification
cargo clippy --locked -p victory-shader --all-targets -- -D warningsand--features host: 0 warnings.cargo xtask policy: 184 files ok.gfx10: 27 passed, 0 failed (all 6 new CFG entry / traversal-order / far-jump recheck tests pass),ir::9 passed,structure11 passed (1 ignored),emit::flow_tests1 passed.Review Confirmation
s_setpc_b64entry-point issue and thedecodevsir::build_boundedtraversal-order divergence are cleanly resolved:decode_with_flows's monotonicentries: BTreeSet<usize>retry loop (allowing entries tostartwhile rejecting(start + 1..=end)), deferred speculative errors, and havingir::build_boundedconsume the resolvedflowsdirectly close the loop completely. Looks ready to merge!Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:19:10Z.
Automated triage for the maintainers.
Merge candidate (recommend a look).
gfx10::far_jumpnow accepts a getpc /s_add_u32|s_sub_u32/ matchings_addc_u32|s_subb_u32/ setpc chain only when the registers and carry match and nothing buts_nop/s_waitcnt/s_waitcnt_depctrsits between the steps. It builds the offset from both words (i128, so-i64::MINis safe).decode_with_flowsleaves the setpc indirect when any branch enters after getpc, andir::build_boundednow uses the decoder's final flow decisions instead of resolving the jump a second time. I read the retry loop: entries only grow, and a chain blocked by a known entry is never accepted again, so it terminates. Errors are held until the walk is final. The only other caller of the removedcontrolwasir.rs. The change is more conservative than main (more jumps stay indirect), and the semantics are cited to the public RDNA2 ISA and LLVM's far-branch sequence. The two decode/IR divergence cases raised in review are covered by tests.Heads-up: it conflicts with #2012 (decode by dword table) in
crates/victory-shader/src/gfx10/mod.rs, so whichever lands second needs a rebase.CI:
checkpassing on this head.Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T02:26:39Z.
Review: CHANGES_REQUESTED
Reviewed
6dbac145d97a88ac3315353625b070d5a2b04f59.The
i12864-bit carry/borrow arithmetic (s_add_u32/s_addc_u32ands_sub_u32/s_subb_u32), clobber rejection, anddecode_with_flowsentry-rechecking convergence loop look solid (cargo xtask policy,clippy --all-targets/--all-features,gfx10::tests27/27,ir::tests9/9, andrustfmtall pass cleanly).One validation gap to address before merging:
far_jumptargets (crates/victory-shader/src/gfx10/mod.rs:272-275):flow()preserves 4-byte alignment via4 * i16, whereasfar_jump()computes an arbitrary bytedeltaand only filters.filter(|&t| t < len)without.is_multiple_of(4).target = 22pastsetpc + 4 = 20in a 28-byte buffer),decode_with_flowsdecodes unaligned bytes across dword boundaries, and the overlap checkw[0].offset + 4 * w[0].words.len() > w[1].offset(20 > 22is false) does not catch it before handing the unaligned leader toir::build_bounded. Conversely, an unaligned target insidestart + 1..=endentersentriesand turns a malformed jump intoFlow::Stopinstead of an ISA error..filter(|&t| t < len && t.is_multiple_of(4))(plus a unit test covering an unaligned far-jump offset) closes this cleanly.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.