victory-shader: fold constants and eliminate dead pure ops in bitcode #1716

Open
github-bot wants to merge 1 commit from github-bot/gh-1716 into main AGit
Owner

@⁠bartikele on GitHub · 2026-11-24 21:11 UTC · original
bartikele:shader-bitcode-dce-fold → main · +507 −2 · 3 files · 1 commits · open

Summary

  • Add Module::optimize in crates/victory-shader/src/emit/bitcode.rs and invoke it right before Module::write in crates/victory-shader/src/emit/shader.rs.
  • Iteratively perform:
    • Integer constant folding (Bin, Cmp, Select, Cast zero/sign extension & truncation) and algebraic identities (x + 0, x - 0, x - x, x * 0, x * 1, x & 0, x & -1, x & x, x | 0, x | -1, x | x, x ^ 0, x ^ x, shifts by 0, cmp eq/ne/ult/slt on identical operands).
    • Constant CondBr and same-target CondBr(c, t, t) folding to Term::Br, pruning unreachable blocks and dead Phi incoming edges from predecessors that no longer branch to the block, and collapsing trivial Phis.
    • Pure dead-code elimination (Bin, Cast, Cmp, Select, Extract, Gep, Phi, and Attr::ReadNone calls) seeded from side-effecting ops, live terminators, and metadata value roots.
    • Pruning unreferenced external function declarations so DXIL validation does not reject unused externals.
  • Keep Module::live and Module::write unchanged so existing bitcode writer tests stay byte-identical.

Verification

  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean
  • cargo test -p victory-shader --locked -- --test-threads=1: 123 passed, 0 failed, 1 ignored (including 3 new itcode::tests::optimize_* unit tests and dxil.dll validation tests)
  • cargo xtask policy: 154 files ok
  • Full corpus ictory translate --validate: 0 panics, 0 Module bitcode is invalid errors (21,714 / 21,886 emitted DXIL programs validated)
**@⁠bartikele** on GitHub · 2026-11-24 21:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716) `bartikele:shader-bitcode-dce-fold` → `main` · +507 −2 · 3 files · 1 commits · open ## Summary - Add Module::optimize in crates/victory-shader/src/emit/bitcode.rs and invoke it right before Module::write in crates/victory-shader/src/emit/shader.rs. - Iteratively perform: - Integer constant folding (Bin, Cmp, Select, Cast zero/sign extension & truncation) and algebraic identities (x + 0, x - 0, x - x, x * 0, x * 1, x & 0, x & -1, x & x, x | 0, x | -1, x | x, x ^ 0, x ^ x, shifts by 0, cmp eq/ne/ult/slt on identical operands). - Constant CondBr and same-target CondBr(c, t, t) folding to Term::Br, pruning unreachable blocks and dead Phi incoming edges from predecessors that no longer branch to the block, and collapsing trivial Phis. - Pure dead-code elimination (Bin, Cast, Cmp, Select, Extract, Gep, Phi, and Attr::ReadNone calls) seeded from side-effecting ops, live terminators, and metadata value roots. - Pruning unreferenced external function declarations so DXIL validation does not reject unused externals. - Keep Module::live and Module::write unchanged so existing bitcode writer tests stay byte-identical. ## Verification - cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean - cargo test -p victory-shader --locked -- --test-threads=1: 123 passed, 0 failed, 1 ignored (including 3 new itcode::tests::optimize_* unit tests and dxil.dll validation tests) - cargo xtask policy: 154 files ok - Full corpus ictory translate --validate: 0 panics, 0 Module bitcode is invalid errors (21,714 / 21,886 emitted DXIL programs validated)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:42:27 +00:00
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 22:55 UTC · original · edited 2026-11-24 23:37 UTC

A regression on WARP: RTX 3070 (driver 616.56) plus WARP, Windows 11, debug, pr1716 (ffe310e-based) merged cleanly onto main 70564ae. cargo test -p victory-shader --locked: 201 passed, 11 failed, 2 ignored, the same in 2 of 2 runs. main alone fails only #1744's 3 (a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, a_heap_buffer_read_by_element_takes_the_views_stride, gathers_at_a_level_match_the_hardware). The 8 new ones:

  • bvh_tests::intersections_on_the_gpu_equal_the_reference, …_with_64_bit_pointers_on_the_gpu_…, …_on_nodes_in_title_memory_…
  • valu_tests::image_addresses_of_16_bits_and_clamps
  • waterfall_tests::lanes_sample_the_entry_their_offset_names_in_the_console_waterfall, …_texture_their_arm_loaded_or_picked, …_texture_their_if_else_arm_chose, …_their_own_texture_in_a_waterfall_narrowed_by_v_cmpx

Each fails alone too (--exact), so it isn't one test taking the shared device down for the others. In each, the hardware adapter's half passes (e.g. "hardware: equal (2093 boxes …)"), then the WARP half fails with warp: dispatch failed: The GPU device instance has been suspended, with nothing logged before it. So the folded DXIL of these programs validates and runs on the NVIDIA driver but removes the WARP device. They share BVH node, 16-bit address and waterfall descriptor arithmetic, which is plausibly where folding changes the IR the most. Update, narrowed to the pure DCE. I put local switches around the parts of optimize() and ran three of the eight (intersections_on_the_gpu_equal_the_reference, lanes_sample_the_texture_their_if_else_arm_chose, image_addresses_of_16_bits_and_clamps) alone each time:

optimize() variant result
as is 3 fail
skipped entirely 3 pass
no simplify_inst and no CondBr folding (loop changes nothing) 3 fail
only the DCE disabled (every instruction kept live) 3 pass
only the unused-declaration drop disabled 3 fail
DCE on, but every Call treated as impure 3 fail
DCE on, CondBr conditions of unreachable blocks also marked live 3 fail

Cause found. dxc -dumpbin of image_addresses_of_16_bits_and_clamps's program with and without the DCE: everything the DCE removes is dead (add x, 0, constant arithmetic, unused extractvalues). But it removes the extractvalue users of ReadOnly calls and keeps the calls, because is_pure treats only ReadNone calls as removable. The optimised module ends with two dx.op.getDimensions and four dx.op.rawBufferLoad.f32 calls whose results have no uses; without the DCE each has one. The NVIDIA driver accepts that, and WARP's JIT takes the device down on it.

Fix: let the DCE also drop unused calls whose function is Attr::ReadOnly, since they have no side effects:

Op::Call(f, _) => matches!(self.funcs[*f as usize].attr, Some(Attr::ReadNone | Attr::ReadOnly)),

With exactly that change, the three tests above pass, and the full cargo test -p victory-shader --locked is back to 209 passed, 3 failed, 2 ignored on the RTX 3070 plus WARP. Those 3 are #1744's NVIDIA heap-cap failures on main, so all 8 regressions are gone.

AI-assisted (Claude Code). Synthetic tests only.

**@⁠upipibi** on GitHub · 2026-11-24 22:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#issuecomment-7146810806) · edited 2026-11-24 23:37 UTC A regression on WARP: RTX 3070 (driver 616.56) plus WARP, Windows 11, debug, `pr1716` (`ffe310e`-based) merged cleanly onto `main` `70564ae`. `cargo test -p victory-shader --locked`: 201 passed, 11 failed, 2 ignored, the same in 2 of 2 runs. `main` alone fails only #1744's 3 (`a_heap_buffer_read_where_no_buffer_view_is_reads_zeros`, `a_heap_buffer_read_by_element_takes_the_views_stride`, `gathers_at_a_level_match_the_hardware`). The 8 new ones: - `bvh_tests::intersections_on_the_gpu_equal_the_reference`, `…_with_64_bit_pointers_on_the_gpu_…`, `…_on_nodes_in_title_memory_…` - `valu_tests::image_addresses_of_16_bits_and_clamps` - `waterfall_tests::lanes_sample_the_entry_their_offset_names_in_the_console_waterfall`, `…_texture_their_arm_loaded_or_picked`, `…_texture_their_if_else_arm_chose`, `…_their_own_texture_in_a_waterfall_narrowed_by_v_cmpx` Each fails alone too (`--exact`), so it isn't one test taking the shared device down for the others. In each, the hardware adapter's half passes (e.g. "hardware: equal (2093 boxes …)"), then the WARP half fails with `warp: dispatch failed: The GPU device instance has been suspended`, with nothing logged before it. So the folded DXIL of these programs validates and runs on the NVIDIA driver but removes the WARP device. They share BVH node, 16-bit address and waterfall descriptor arithmetic, which is plausibly where folding changes the IR the most. **Update, narrowed to the pure DCE.** I put local switches around the parts of `optimize()` and ran three of the eight (`intersections_on_the_gpu_equal_the_reference`, `lanes_sample_the_texture_their_if_else_arm_chose`, `image_addresses_of_16_bits_and_clamps`) alone each time: | `optimize()` variant | result | |---|---| | as is | 3 fail | | skipped entirely | 3 pass | | no `simplify_inst` and no `CondBr` folding (loop changes nothing) | 3 fail | | only the DCE disabled (every instruction kept live) | 3 pass | | only the unused-declaration drop disabled | 3 fail | | DCE on, but every `Call` treated as impure | 3 fail | | DCE on, `CondBr` conditions of unreachable blocks also marked live | 3 fail | **Cause found.** `dxc -dumpbin` of `image_addresses_of_16_bits_and_clamps`'s program with and without the DCE: everything the DCE removes is dead (`add x, 0`, constant arithmetic, unused `extractvalue`s). But it removes the `extractvalue` users of `ReadOnly` calls and keeps the calls, because `is_pure` treats only `ReadNone` calls as removable. The optimised module ends with two `dx.op.getDimensions` and four `dx.op.rawBufferLoad.f32` calls whose results have **no uses**; without the DCE each has one. The NVIDIA driver accepts that, and WARP's JIT takes the device down on it. **Fix:** let the DCE also drop unused calls whose function is `Attr::ReadOnly`, since they have no side effects: ```rust Op::Call(f, _) => matches!(self.funcs[*f as usize].attr, Some(Attr::ReadNone | Attr::ReadOnly)), ``` With exactly that change, the three tests above pass, and the full `cargo test -p victory-shader --locked` is back to 209 passed, 3 failed, 2 ignored on the RTX 3070 plus WARP. Those 3 are #1744's NVIDIA heap-cap failures on `main`, so all 8 regressions are gone. AI-assisted (Claude Code). Synthetic tests only.
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 23:38 UTC · original

Found the cause of the WARP regression, and a one-line fix (details in my comment above): the DCE leaves ReadOnly dx.op calls (rawBufferLoad, getDimensions) with no uses after removing their extractvalues, and WARP fails on those. Treating Attr::ReadOnly calls as removable in is_pure brings the full suite back to main's result on an RTX 3070 plus WARP.

**@⁠upipibi** on GitHub · 2026-11-24 23:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#issuecomment-7147622460) Found the cause of the WARP regression, and a one-line fix (details in my comment above): the DCE leaves `ReadOnly` `dx.op` calls (`rawBufferLoad`, `getDimensions`) with no uses after removing their `extractvalue`s, and WARP fails on those. Treating `Attr::ReadOnly` calls as removable in `is_pure` brings the full suite back to `main`'s result on an RTX 3070 plus WARP.
Author
Owner

@⁠akimfomin on GitHub · 2026-11-25 00:46 UTC · original

Independent follow-up on this PR's exact head 758bf7fb. PortBridge generated a live circular-copy fixture from the public compute fixture by alessiodeia and tested a narrow extension to this optimizer for an entry-seeded i32 PHI ring.

The one-seed 64-register case went from 66 to 2 PHIs and from 4,168 to 3,724 bytes. Microsoft DXV accepted all 12 A/B outputs. Each two-seed case stayed byte-identical. On RTX 4070 Ti SUPER, all eight before/after 2- and 16-register cases matched all 72 checked output words. Prevalence, compile-time impact, and 64-register GPU execution remain unmeasured.

Patch, opt-in fixture, reproduction and results. This builds on #1716's optimizer; the fixture can also be used on its own. AI-assisted with Codex.

**@⁠akimfomin** on GitHub · 2026-11-25 00:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#issuecomment-7148860570) Independent follow-up on this PR's exact head `758bf7fb`. PortBridge generated a live circular-copy fixture from the public compute fixture by alessiodeia and tested a narrow extension to this optimizer for an entry-seeded i32 PHI ring. The one-seed 64-register case went from 66 to 2 PHIs and from 4,168 to 3,724 bytes. Microsoft DXV accepted all 12 A/B outputs. Each two-seed case stayed byte-identical. On RTX 4070 Ti SUPER, all eight before/after 2- and 16-register cases matched all 72 checked output words. Prevalence, compile-time impact, and 64-register GPU execution remain unmeasured. [Patch, opt-in fixture, reproduction and results](https://github.com/akimfomin/PortBridge/blob/3c9684f0c22f06d35c3b2c2b59a407d3fa5f4830/research/rendering-night/victory-live-phi/README.md). This builds on #1716's optimizer; the fixture can also be used on its own. AI-assisted with Codex.
Author
Owner

@⁠zed6000 on GitHub · 2026-11-25 01:57 UTC · original

Measured offline on the title's shaders (as in #1247): merged onto main 70564ae and compared program by program (victory translate --validate --list, shaders.rpf 1.0.2.1, dxil.dll 1.8.2502). This PR gains nothing there and loses 6 programs that validate on main (4 pixel, 2 compute). With the PR, they fail validation with:

  • validator: error: Flags must match usage. (4)
  • validator: error: Internal declaration 'code' is unused. (2)

This is separate from upipibi's WARP finding above. The likely cause is that dead-code elimination removes the last use of an operation or a declaration, but the module still declares the matching shader flags, or keeps the now-unused internal global. The validator checks both. Offline counts only; no program names or data.

**@⁠zed6000** on GitHub · 2026-11-25 01:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#issuecomment-7150152560) Measured offline on the title's shaders (as in #1247): merged onto main `70564ae` and compared program by program (`victory translate --validate --list`, `shaders.rpf` 1.0.2.1, dxil.dll 1.8.2502). This PR gains nothing there and **loses 6 programs** that validate on main (4 pixel, 2 compute). With the PR, they fail validation with: - `validator: error: Flags must match usage.` (4) - `validator: error: Internal declaration 'code' is unused.` (2) This is separate from upipibi's WARP finding above. The likely cause is that dead-code elimination removes the last use of an operation or a declaration, but the module still declares the matching shader flags, or keeps the now-unused internal global. The validator checks both. Offline counts only; no program names or data.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:35 UTC · original · edited 2026-11-25 18:22 UTC

Automated triage for the maintainers.

Thanks for the rebase. On current main, much of what Module::optimize (crates/victory-shader/src/emit/bitcode.rs) does is already done when the module is written: Module::live drops values nothing reads (including unused read-only dx.op calls), Module::term/Module::plan fold constant branches and leave out unreachable blocks, and Module::kept drops unused declarations and constant globals (commit 286a00e). So the new part is mainly the integer constant folding, the algebraic identities and the trivial-phi collapse. The final live() + retain pass in optimize repeats what the writer already does.

What would change the verdict:

  • Measurements on current main. The 21 % instruction reduction and the 6 programs that failed validation were both measured on 940d32f, which is before 286a00e. The benefit, and whether any program still fails validation, need to be measured again against today's main.
  • The weakened test. In emit::tests (crates/victory-shader/src/emit/mod.rs), the low-half check now also accepts 65535, so it no longer shows that the destination's high half is kept. Please give the test a destination that cannot be folded, so it still checks the -65536 mask, rather than accepting either result.
  • Optionally, drop the DCE step that the writer already does, so optimize only folds.

I read the folding rules (simplify_inst: masks per bit width, signed division overflow guarded, float compares left alone) and found no wrong rewrite. The trivial-phi replacement is sound under SSA dominance.

CI: check passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#issuecomment-7152056560) · edited 2026-11-25 18:22 UTC <!-- victory-triage v1 head=436a091947b85c6c880a78a47aa1335bf18bb36f verdict=needs-work at=2026-11-25T18:22:42Z --> Automated triage for the maintainers. Thanks for the rebase. On current `main`, much of what `Module::optimize` (`crates/victory-shader/src/emit/bitcode.rs`) does is already done when the module is written: `Module::live` drops values nothing reads (including unused read-only `dx.op` calls), `Module::term`/`Module::plan` fold constant branches and leave out unreachable blocks, and `Module::kept` drops unused declarations and constant globals (commit 286a00e). So the new part is mainly the integer constant folding, the algebraic identities and the trivial-phi collapse. The final `live()` + `retain` pass in `optimize` repeats what the writer already does. What would change the verdict: - **Measurements on current `main`.** The 21 % instruction reduction and the 6 programs that failed validation were both measured on `940d32f`, which is before 286a00e. The benefit, and whether any program still fails validation, need to be measured again against today's `main`. - **The weakened test.** In `emit::tests` (`crates/victory-shader/src/emit/mod.rs`), the low-half check now also accepts `65535`, so it no longer shows that the destination's high half is kept. Please give the test a destination that cannot be folded, so it still checks the `-65536` mask, rather than accepting either result. - Optionally, drop the DCE step that the writer already does, so `optimize` only folds. I read the folding rules (`simplify_inst`: masks per bit width, signed division overflow guarded, float compares left alone) and found no wrong rewrite. The trivial-phi replacement is sound under SSA dominance. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠r4tedx on GitHub · 2026-11-25 05:28 UTC · original

Measured on current main (940d32f): 21.4 % fewer instructions, and the same 6 programs lost

Triage asked for a benefit this can show. I merged head 758bf7fb onto main 940d32f (clean merge) together with #2115, which counts the instructions of every emitted shader, and compared the two listings program by program (translate --validate --list --count over the title's shaders.rpf, dxil.dll 10.0.26100.0).

main main + this PR
instructions in emitted programs 190,954,569 150,017,576 −21.44 %
basic blocks 3,213,914 3,153,576 −1.88 %
programs validated 23,918 23,912 −6
  • 25,562 of the 25,591 emitted programs get smaller, 29 stay the same, none grows.
  • Where it comes from: bitcast −22,401,564 (52.4 M → 30.0 M), add −4,778,091, and −3,046,252, extractvalue −2,365,625, icmp −2,074,280, sub −1,465,088, shl −1,342,395. So most of what goes is dead register reads and dead address arithmetic, not folded constants.
  • The 6 lost programs validate on main and fail here with Flags must match usage. (4) and Internal declaration 'code' is unused. (2). These are the two reasons @⁠zed6000 found on 70564ae; they are still there on 940d32f. No program is gained.

What this does not show: GPU time. A driver's compiler removes dead code itself, so the likelier gain is pipeline compile time and cache size. I did not measure either.

Local title data, offline, CPU only; counts and validator reasons only. The title was not run.

AI-assisted

**@⁠r4tedx** on GitHub · 2026-11-25 05:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#issuecomment-7153805547) **Measured on current `main` (`940d32f`): 21.4 % fewer instructions, and the same 6 programs lost** Triage asked for a benefit this can show. I merged head `758bf7fb` onto `main` `940d32f` (clean merge) together with #2115, which counts the instructions of every emitted shader, and compared the two listings program by program (`translate --validate --list --count` over the title's `shaders.rpf`, dxil.dll 10.0.26100.0). | | `main` | `main` + this PR | | | --- | ---: | ---: | ---: | | instructions in emitted programs | 190,954,569 | 150,017,576 | −21.44 % | | basic blocks | 3,213,914 | 3,153,576 | −1.88 % | | programs validated | 23,918 | 23,912 | −6 | - 25,562 of the 25,591 emitted programs get smaller, 29 stay the same, none grows. - Where it comes from: `bitcast` −22,401,564 (52.4 M → 30.0 M), `add` −4,778,091, `and` −3,046,252, `extractvalue` −2,365,625, `icmp` −2,074,280, `sub` −1,465,088, `shl` −1,342,395. So most of what goes is dead register reads and dead address arithmetic, not folded constants. - The 6 lost programs validate on `main` and fail here with `Flags must match usage.` (4) and `Internal declaration 'code' is unused.` (2). These are the two reasons @⁠zed6000 found on `70564ae`; they are still there on `940d32f`. No program is gained. What this does not show: GPU time. A driver's compiler removes dead code itself, so the likelier gain is pipeline compile time and cache size. I did not measure either. Local title data, offline, CPU only; counts and validator reasons only. The title was not run. AI-assisted
Author
Owner

@⁠bartikele on GitHub · 2026-11-25 11:50 UTC · original

Rebased onto current main (940d32f) and addressed all three issues from the reviews (commit ad2fb4):

  1. WARP regression (8 tests, @⁠upipibi): Updated is_pure to treat both Attr::ReadNone and Attr::ReadOnly calls as droppable when unused, so dead
    awBufferLoad / getDimensions calls whose extractvalues were eliminated are also removed. All 8 WARP tests (vh_tests, alu_tests::image_addresses_of_16_bits_and_clamps, waterfall_tests) now pass.
  2. Unused internal globals (Internal declaration 'code' is unused, @⁠zed6000 / @⁠r4tedx): Module::optimize now also prunes unreferenced internal globals (g.init.is_some()) and remaps V::Global operands.
  3. Shader flags vs usage (Flags must match usage, @⁠zed6000 / @⁠r4tedx / triage): Moved self.m.optimize() before dx.entryPoints property construction in shader.rs and reconciled WAVE_OPS, INT64, ATOMIC64_*, TYPED_UAV_LOADS, RESOURCE_HEAP, and SAMPLER_HEAP against the optimized module (keeping RAW_BUFFERS whenever raw/structured buffers remain in dx.resources).
  4. Tests: Added itcode::tests::optimize_drops_unused_readonly_call_and_internal_global and updated emit::tests::interpolated_f16_writes_the_op_sel_half_with_modifiers where
**@⁠bartikele** on GitHub · 2026-11-25 11:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#issuecomment-7161568587) Rebased onto current main (940d32f) and addressed all three issues from the reviews (commit ad2fb4): 1. **WARP regression (8 tests, @⁠upipibi)**: Updated is_pure to treat both Attr::ReadNone and Attr::ReadOnly calls as droppable when unused, so dead awBufferLoad / getDimensions calls whose extractvalues were eliminated are also removed. All 8 WARP tests (vh_tests, alu_tests::image_addresses_of_16_bits_and_clamps, waterfall_tests) now pass. 2. **Unused internal globals (Internal declaration 'code' is unused, @⁠zed6000 / @⁠r4tedx)**: Module::optimize now also prunes unreferenced internal globals (g.init.is_some()) and remaps V::Global operands. 3. **Shader flags vs usage (Flags must match usage, @⁠zed6000 / @⁠r4tedx / triage)**: Moved self.m.optimize() before dx.entryPoints property construction in shader.rs and reconciled WAVE_OPS, INT64, ATOMIC64_*, TYPED_UAV_LOADS, RESOURCE_HEAP, and SAMPLER_HEAP against the optimized module (keeping RAW_BUFFERS whenever raw/structured buffers remain in dx.resources). 4. **Tests**: Added itcode::tests::optimize_drops_unused_readonly_call_and_internal_global and updated emit::tests::interpolated_f16_writes_the_op_sel_half_with_modifiers where
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 13:32 UTC · original

@⁠bartikele Re-run at bad2fb4 (as-is, on main 940d32f), RTX 3070 (616.56) plus WARP, Windows 11, debug: the 8 tests from my WARP report all pass now, 2 runs of cargo test -p victory-shader --locked.

  • bvh_tests: intersections_on_the_gpu_equal_the_reference, the …_with_64_bit_pointers_on_the_gpu_… and …_on_nodes_in_title_memory_… variants.
  • valu_tests::image_addresses_of_16_bits_and_clamps.
  • The four waterfall_tests.

The suite gives 248 passed, 3 failed, 3 ignored in both runs. The 3 are #1744's NVIDIA failures that main has too (a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, a_heap_buffer_read_by_element_takes_the_views_stride, gathers_at_a_level_match_the_hardware).

AI-assisted (Claude Code). Synthetic tests only.

**@⁠upipibi** on GitHub · 2026-11-25 13:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#issuecomment-7163937499) @⁠bartikele Re-run at `bad2fb4` (as-is, on `main` `940d32f`), RTX 3070 (616.56) plus WARP, Windows 11, debug: the 8 tests from my WARP report all pass now, 2 runs of `cargo test -p victory-shader --locked`. - `bvh_tests`: `intersections_on_the_gpu_equal_the_reference`, the `…_with_64_bit_pointers_on_the_gpu_…` and `…_on_nodes_in_title_memory_…` variants. - `valu_tests::image_addresses_of_16_bits_and_clamps`. - The four `waterfall_tests`. The suite gives 248 passed, 3 failed, 3 ignored in both runs. The 3 are #1744's NVIDIA failures that `main` has too (`a_heap_buffer_read_where_no_buffer_view_is_reads_zeros`, `a_heap_buffer_read_by_element_takes_the_views_stride`, `gathers_at_a_level_match_the_hardware`). AI-assisted (Claude Code). Synthetic tests only.
Author
Owner

@⁠bartikele on GitHub · 2026-11-25 16:45 UTC · original

Rebased onto current main (67e8ae1, commit 436a091), integrating with 286a00e:

  • Module::optimize() now runs right before self.m.uses() in shader.rs and delegates unused external/internal declaration pruning and shader flag reconciliation to main's plan()/uses()/write(), keeping Module::optimize() focused on constant folding, algebraic identities, constant CondBr folding, unreachable Phi edge pruning, trivial Phi collapse, and dead pure instruction removal.

Verified on 67e8ae1:

  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean
  • cargo test -p victory-shader --locked emit::bitcode::tests: 16 passed, 0 failed, 1 ignored
  • cargo test -p victory-shader --locked emit::tests: 17 passed, 0 failed
  • cargo xtask policy: 185 files ok
**@⁠bartikele** on GitHub · 2026-11-25 16:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#issuecomment-7168479183) Rebased onto current main (67e8ae1, commit 436a091), integrating with 286a00e: - Module::optimize() now runs right before self.m.uses() in shader.rs and delegates unused external/internal declaration pruning and shader flag reconciliation to main's plan()/uses()/write(), keeping Module::optimize() focused on constant folding, algebraic identities, constant CondBr folding, unreachable Phi edge pruning, trivial Phi collapse, and dead pure instruction removal. Verified on 67e8ae1: - cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean - cargo test -p victory-shader --locked emit::bitcode::tests: 16 passed, 0 failed, 1 ignored - cargo test -p victory-shader --locked emit::tests: 17 passed, 0 failed - cargo xtask policy: 185 files ok
Author
Owner

@⁠zed6000 on GitHub · 2026-11-25 18:26 UTC · original

Re-measured after the rebase. Head 436a091, merged onto main 13f91ca (a clean merge), program by program (shaders.rpf 1.0.2.1, dxil.dll 1.8.2502): 0 gained, 0 lost. The 6 programs I reported earlier as failing validation ("Flags must match usage" and "Internal declaration 'code' is unused") now validate. cargo test -p victory-shader --lib gives 274 passed and 0 failed on an RX 7900 XTX. Offline counts only; no program names or data.

**@⁠zed6000** on GitHub · 2026-11-25 18:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#issuecomment-7170903311) Re-measured after the rebase. Head `436a091`, merged onto main `13f91ca` (a clean merge), program by program (`shaders.rpf` 1.0.2.1, dxil.dll 1.8.2502): **0 gained, 0 lost.** The 6 programs I reported earlier as failing validation ("Flags must match usage" and "Internal declaration 'code' is unused") now validate. `cargo test -p victory-shader --lib` gives 274 passed and 0 failed on an RX 7900 XTX. Offline counts only; no program names or data.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-24 21:40 UTC · original · approved these changes

Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 99 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows; macOS covers lowering/structure unit tests.

New bitcode optimize() pass: bounded (4 iterations) constant folding + trivial-phi compression with a step budget, unreachable-block phi pruning, branch folding on constant conditions, then pure DCE. is_pure is conservative (Load/Store/AtomicRmw/CmpXchg and non-ReadNone calls are roots; metadata values and CondBr conditions are roots too), and unused external declarations are dropped with index remapping. +3 macOS tests (99 total).

**@⁠catminou4** on GitHub · 2026-11-24 21:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#pullrequestreview-6047847553) · **approved these changes** Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 99 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows; macOS covers lowering/structure unit tests. New bitcode optimize() pass: bounded (4 iterations) constant folding + trivial-phi compression with a step budget, unreachable-block phi pruning, branch folding on constant conditions, then pure DCE. is_pure is conservative (Load/Store/AtomicRmw/CmpXchg and non-ReadNone calls are roots; metadata values and CondBr conditions are roots too), and unused external declarations are dropped with index remapping. +3 macOS tests (99 total).
Author
Owner

@⁠liandraraiane on GitHub · 2026-11-26 12:49 UTC · original

Measured on NVIDIA: what the folding changes in the DXIL, and on the card.

Setup: RTX 3060 12 GB (Ampere), driver 610.88, Windows 11 26200, release. This PR merged onto main 63bcca4 (clean), plus two local measurement hooks: one that counts the instructions Module::write writes per program, and one that repeats a test's compute dispatch 500 times between GPU timestamps; and an env switch to skip optimize() so both sides run from one binary.

Correctness: victory-shader suite with this PR: 302 passed, 3 failed; main alone: 298 passed, the same 3 failed (the capped-heap failures of #1835 on NVIDIA). No new failure; the 4 new tests pass.

DXIL size, over the 1,330 programs the suite translates: instructions written 51,089 → 45,057 (−11.8 %); integer arithmetic −23 %, casts −14 %, compares −6 %, selects −5 %, calls (loads, samples, wave ops) −0.2 %; bitcode bytes −1 %. main already drops unread values and constant branches when it writes (Module::live, Module::term), so the change here is the folding and identities.

GPU time: the heaviest programs of the suite (ray/box intersection ~1,090 instructions, title memory at computed addresses ~880, waterfall loops ~370, f16 and gather tests), median of 5 alternated runs, µs per dispatch:

Test lowered as is optimized Δ
bvh_tests::intersections_on_the_gpu_equal_the_reference 6.029 6.025 −0.1 %
bvh_tests::intersections_on_nodes_in_title_memory_… 6.304 6.308 +0.1 %
memory_tests::title_memory_is_read_and_written_at_computed_addresses 2.824 2.820 −0.1 %
synthetic_tests::relative_moves_in_waterfall_loops_… 5.282 5.284 0.0 %
valu_tests::v_fma_f16_rounds_once_and_preserves_other_half 1.249 1.249 0.0 %

Those programs do shrink (1,086 → 1,066, 876 → 838, 371 → 334 instructions), but the card's time does not move: NVIDIA's compiler already folds these. So on this card the gain would be smaller modules (and perhaps shorter pipeline compiles, not measured), not GPU time. One block-local count for the next passes: within blocks, 2.1 % of instructions repeat an earlier pure value, 6.5 % of reads repeat an earlier read with no store between, and pure calls almost never repeat.

Not tested: a title; AMD; pipeline creation time.

AI use: Claude Code ran the builds and measurements on this machine and drafted this comment.

**@⁠liandraraiane** on GitHub · 2026-11-26 12:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/1716#issuecomment-7192504699) Measured on NVIDIA: what the folding changes in the DXIL, and on the card. **Setup:** RTX 3060 12 GB (Ampere), driver 610.88, Windows 11 26200, release. This PR merged onto `main` `63bcca4` (clean), plus two local measurement hooks: one that counts the instructions `Module::write` writes per program, and one that repeats a test's compute dispatch 500 times between GPU timestamps; and an env switch to skip `optimize()` so both sides run from one binary. **Correctness:** `victory-shader` suite with this PR: 302 passed, 3 failed; `main` alone: 298 passed, the same 3 failed (the capped-heap failures of #1835 on NVIDIA). No new failure; the 4 new tests pass. **DXIL size**, over the 1,330 programs the suite translates: instructions written 51,089 → 45,057 (−11.8 %); integer arithmetic −23 %, casts −14 %, compares −6 %, selects −5 %, calls (loads, samples, wave ops) −0.2 %; bitcode bytes −1 %. `main` already drops unread values and constant branches when it writes (`Module::live`, `Module::term`), so the change here is the folding and identities. **GPU time:** the heaviest programs of the suite (ray/box intersection ~1,090 instructions, title memory at computed addresses ~880, waterfall loops ~370, f16 and gather tests), median of 5 alternated runs, µs per dispatch: | Test | lowered as is | optimized | Δ | |---|---:|---:|---:| | `bvh_tests::intersections_on_the_gpu_equal_the_reference` | 6.029 | 6.025 | −0.1 % | | `bvh_tests::intersections_on_nodes_in_title_memory_…` | 6.304 | 6.308 | +0.1 % | | `memory_tests::title_memory_is_read_and_written_at_computed_addresses` | 2.824 | 2.820 | −0.1 % | | `synthetic_tests::relative_moves_in_waterfall_loops_…` | 5.282 | 5.284 | 0.0 % | | `valu_tests::v_fma_f16_rounds_once_and_preserves_other_half` | 1.249 | 1.249 | 0.0 % | Those programs do shrink (1,086 → 1,066, 876 → 838, 371 → 334 instructions), but the card's time does not move: NVIDIA's compiler already folds these. So on this card the gain would be smaller modules (and perhaps shorter pipeline compiles, not measured), not GPU time. One block-local count for the next passes: within blocks, 2.1 % of instructions repeat an earlier pure value, 6.5 % of reads repeat an earlier read with no store between, and pure calls almost never repeat. Not tested: a title; AMD; pipeline creation time. AI use: Claude Code ran the builds and measurements on this machine and drafted this comment.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1716/head:github-bot/gh-1716'
git switch 'github-bot/gh-1716'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1716'
git switch 'github-bot/gh-1716'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1716'
git switch 'github-bot/gh-1716'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1716'
git switch 'main'
git merge --squash 'github-bot/gh-1716'
git switch 'main'
git merge --ff-only 'github-bot/gh-1716'
git switch 'main'
git merge 'github-bot/gh-1716'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1716
No description provided.