Skip to content

Shaders: validate far-jump arithmetic and control-flow entries - #2686

Open
XeNS1 wants to merge 2 commits into
Marvisak:mainfrom
XeNS1:codex/far-jump-recognition-20261125
Open

XeNS1 wants to merge 2 commits into
Marvisak:mainfrom
XeNS1:codex/far-jump-recognition-20261125

Conversation

@XeNS1

@XeNS1 XeNS1 commented Nov 25, 2026 •

Copy link
Copy Markdown

gfx10::far_jump followed 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:

  • An isolated Cargo harness imports the actual gfx10 and ir modules and the unchanged ShaderError enum, without behavior stubs. All 36 unit tests and strict all-target Clippy pass.
  • cargo check -p victory-shader --all-features --locked --offline passes. Changed code is formatted; repository asset policy (184 files), staged policy (2 files), and diff checks pass.
  • Full shader tests remain blocked locally by six pre-existing non-Windows references to emit::validator in lower.rs. Full-library strict Clippy on Rust 1.100 also hits the unchanged needless_range_loop at lower.rs:1061. Full Windows CI on pinned Rust 1.98.1 passes both workspace Clippy configurations and workspace tests on the updated head (run).

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.

@DrDarkDK

Copy link
Copy Markdown

A branch can reach s_setpc_b64 without executing the address setup the lookback validates. For example, a conditional branch can target setpc directly while fallthrough executes getpc, 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 call control, so the test should check that they agree.

Static review only.

AI-assisted with Codex.

@kotekompotek

Copy link
Copy Markdown

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.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Independent Windows 11 verification and code review of 38b757ec (on 13f91ca):

Verification (rustc 1.98.1 MSVC)

  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings and --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

  • The high-word pair validation (s_getpc_b64 -> s_add_u32/s_sub_u32 -> matching s_addc_u32/s_subb_u32 -> s_setpc_b64), GFX10 inline constant decoding (128..=192, 193..=208, 0xFF), i128 negation safety, and strict s_nop/s_waitcnt/s_waitcnt_depctr clobber rejection all look solid.
  • Confirming & expanding the CFG entry-point issue: because far_jump walks backward by byte offset through before(at) without checking whether a branch targets the interior of the chain or s_setpc_b64 directly, there is also a decode vs ir::build_bounded mismatch:
    • Consider 0x00: s_cbranch_scc1 getpc; 0x04: s_branch setpc. When decode() follows 0x04 to setpc first, found does not yet contain getpc..addc, so far_jump returns Indirect (Flow::Stop) and never decodes target.
    • pending then decodes getpc..addc into found.
    • Later, ir::build_bounded runs gfx10::control over the completed list, now sees getpc..addc immediately before setpc, classifies it as Flow::Jump(target), and fails block_at(target) (jump into a block) because target was never decoded.
    • Ensuring interior instructions / s_setpc_b64 are not branch targets (or handling the chain consistently across decode and ir::build_bounded) avoids both false-positive static jumps and this decode/IR divergence.

@XeNS1 XeNS1 changed the title Shaders: validate both halves of a far-jump address chain Shaders: validate far-jump arithmetic and control-flow entries Nov 25, 2026
@XeNS1

XeNS1 commented Nov 25, 2026

Copy link
Copy Markdown
Author

Addressed the CFG-entry and traversal-order findings in 6dbac14. 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 setpc ordering 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.

@pablogonz12

Copy link
Copy Markdown
Contributor

AMD/RADV offline check of the head commit (no game run).

  • Hardware / stack: RX 9070 XT, Mesa 26.3.1 RADV, GE-Proton11-14; base = origin/main 13f91ca.
  • Method: the 751-program world-shader dump, translated by each tree's own victory-shader --example retranslate, then dxil-spirv (master), spirv-val --target-env vulkan1.3, and a real vkCreateCompute/GraphicsPipelines on RADV (vkpipe, one process per program, so a driver crash or hang is recorded per program rather than stopping the batch).
  • Offline only: pipelines are created, never bound or drawn, so wrong pixels and ISA-level miscompiles are out of reach here.
    Result, base 13f91ca vs head 6dbac14: 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.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Re-verified 6dbac14 on Windows 11 (rustc 1.98.1 MSVC, 13f91ca):

Verification

  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings and --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, structure 11 passed (1 ignored), emit::flow_tests 1 passed.

Review Confirmation

  • Both the interior/s_setpc_b64 entry-point issue and the decode vs ir::build_bounded traversal-order divergence are cleanly resolved: decode_with_flows's monotonic entries: BTreeSet<usize> retry loop (allowing entries to start while rejecting (start + 1..=end)), deferred speculative errors, and having ir::build_bounded consume the resolved flows directly close the loop completely. Looks ready to merge!

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate (recommend a look). gfx10::far_jump now accepts a getpc / s_add_u32|s_sub_u32 / matching s_addc_u32|s_subb_u32 / setpc chain only when the registers and carry match and nothing but s_nop/s_waitcnt/s_waitcnt_depctr sits between the steps. It builds the offset from both words (i128, so -i64::MIN is safe). decode_with_flows leaves the setpc indirect when any branch enters after getpc, and ir::build_bounded now 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 removed control was ir.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: check passing 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