Skip to content

Shaders: a loop that branches back to the entry instruction translates - #105

Open
tedin7 wants to merge 1 commit into
Marvisak:mainfrom
tedin7:vic-loop-entry
Open

tedin7 wants to merge 1 commit into
Marvisak:mainfrom
tedin7:vic-loop-entry

Conversation

@tedin7

@tedin7 tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

What

Accept shader loops whose back edge targets byte offset zero. The IR builder now puts an empty entry block before the code when the first instruction is a jump or branch target, so the loop header has one edge for the initial register values and one for the loop-carried values.

Why

A valid shader whose first instruction is also its loop header was rejected with Unsupported { offset: 0, what: "a branch to the program entry" }, because block 0 was both the entry and a loop header and SSA had no edge to carry the initial values into its phis. Every other loop shape already works. Only the entry-block handling changes. Block indices shift by one only when the program branches to offset 0.

Tested

Linux x86_64 only, synthetic shader words only (no game files):

  • cargo test -j4 -p victory-shader ir::tests::loops_can_branch_to_offset_zero on origin/main plus only the new test: FAILED with Unsupported { offset: 0, what: "a branch to the program entry" }.
  • The same command with the fix: 1 passed. The test checks the block layout, both phi inputs (initial value and loop-carried value) and that structure::structure accepts the result.
  • cargo test -j4 -p victory-shader: 52 passed, 0 failed.
  • cargo clippy -j4 -p victory-shader --tests: no warnings in ir.rs; the 2 existing warnings elsewhere are unchanged.
  • rustfmt --check --edition 2024 crates/victory-shader/src/ir.rs: clean.

Not run: Windows runtime and any GPU execution. The emit path was not run on a real shader, only the IR and structuring steps.

@lordon1a

Copy link
Copy Markdown
Contributor

Verified 6e6d3fc02 on Windows 11 (x86_64-pc-windows-gnullvm, rustc 1.100.0, applied cleanly to main@4c0bb89): pass.

  • cargo xtask policy: 151 files ok
  • cargo test -p victory-shader -- --skip v_med3_f32_matches_rdna2_with_nan: 69 passed, 0 failed.

AI-assisted.

@DaviTostes

Copy link
Copy Markdown

Tested 6e6d3fc on Arch Linux x86_64, rustc 1.98.1: pass.

@mercuryVM mercuryVM 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.

Tested 6e6d3fc on Windows 11, RTX 5070, MSVC: the workspace passes (170 outside victory-shader; victory-shader --lib single-threaded: 58; older base). victory translate <title exe> --validate --pipelines gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines.

An empty preheader only when some flow targets offset 0 is the standard fix. It gives the header's phis a distinct entry edge, and leaves block numbering unchanged for every other program. Shifting through block_at and the partition_point mapping keeps all jump targets consistent. The new test checks the shape (entry → header, header preds [0, 1]). Removing the old refusal is safe, since block 0 can no longer have preds.

No issues found.

AI-assisted (Claude Code).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
… the entry instruction translates

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Xeltax pushed a commit to Xeltax/VIcky that referenced this pull request Nov 24, 2026
…isak#306 Marvisak#333 Marvisak#105 Marvisak#288 Marvisak#343 Marvisak#353 Marvisak#326 Marvisak#259 Marvisak#179 Marvisak#176 Marvisak#226 Marvisak#286)

Conflicts:
- emit/lower.rs: shader-opcodes and shader-tracking each keyed heap
  handles by the resource properties (a tuple vs two flattened words);
  the same key, shader-tracking's form and comment kept. Lowered's
  test-only fields, test imports and tests of both sides kept.
- emit/wave.rs: tests of both sides kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. ir::build_bounded in crates/victory-shader/src/ir.rs rejected any program that branches to offset 0 ("a branch to the program entry"); it now puts an empty entry block in front of the code, giving SSA a separate edge for the initial register values (a standard preheader). Block indices (block_at, the partition_point mapping) shift consistently, programs that never target offset 0 are unchanged, and exit in emit/lower.rs handles an empty block. The test checks the block layout, both phi inputs and that structure::structure accepts the result.

CI: no check runs on the head commit.


Generated by Claude Code

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 26, 2026
…at was marked

The first title runs of the switch (RTX 5070, 12 GB) showed three things.

The budget. Copies were admitted at 54 % of the budget, before the
world's textures loaded, and the textures then took the card to 95 %.
Admission now leaves a margin: use stays under 85 % of the budget, and
the cap (a quarter of the budget, or VICTORY_VRAM_COPIES_MB) is never
more than what the rest of the process leaves under 85 %, so it shrinks
as the card fills. Once use passes 85 %, nothing is admitted until it is
back under 75 %. Each buffer in video memory gets
D3D12_RESIDENCY_PRIORITY_LOW: past the budget the system moves these
copies to system memory before the title's textures and targets, with
the same buffer and GPU address.

The bring-back. Ranges declared written and marked came back whole after
each submission, though the CPU reads nothing of them but their marked
pages, which come back anyway. Now only unmarked ranges (main compares
them whole) and marked pages come back. The diagnostics line splits the
bytes by why: declared ranges, marked pages, computed-address pages.

Descriptor slots. A copy in video memory costs a list no more dynamic
slots than one in system memory. Its buffer and GPU address are fixed at
creation. Root-view slots are keyed by copy and kind, fresh tables by
the title's descriptor words, and page-table view pairs by copy. A test
now checks this over 8 submissions. The run's slot shortage followed
more draws and twice the host textures under =2, not the copies' views.

Tests: a copy in video memory takes the same slots as a system copy over
8 submissions, and keeps its address; marked ranges come back by their
marked pages and unmarked ones whole, once; the margin, the 75 % latch
and the shrinking cap with the run's numbers; Marvisak#693 and Belobug Marvisak#105 as
before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fuzeofficlal

Copy link
Copy Markdown
Contributor

Code review and RTX 5090 + WARP + DXIL verification at head 6e6d3fc027d0 merged cleanly onto main (63bcca43c6a0; emit/lower.rs untouched):

  • Code review (crates/victory-shader/src/ir.rs:165-198):
    • Inserting an empty preheader block at blocks[0] (offset: 0, preds: [], exit: Exit::Jump(1)) when any Flow::Jump(0) | Flow::Branch(0) targets offset 0 shifts machine block indices cleanly while keeping machine.offset == 0 intact for s_getpc_b64.
    • Checked downstream lowering (lower.rs:1302-1345): self.block[0] maps to self.s.entry (including mesh shaders updating order[0]), and the loop header at blocks[1] receives initial p.inputs, loop counter 0, and active EXEC along the 0 -> 1 edge cleanly.
  • Verified (Windows 11, NVIDIA RTX 5090 + WARP, dxil.dll 10.0.26100.0, VICTORY_D3D12_DEBUG=1, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default + --features diagnostics): 0 warnings
    • ir::tests (10/10, including loops_can_branch_to_offset_zero), structure::tests (13/13, 1 ignored), and emit::tests (17/17 on both RTX 5090 and WARP) all passed.

LGTM.

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