Repository navigation
Conversation
The lowering writes code as the console instructions name it: a register read as a float is cast again at each read, a descriptor's handle is made and its index clamped again in each block, an address is shifted and masked again for each access. `bitcode::reuse` runs on the finished module: an instruction that computes what an earlier one computed, where every path to it has run that earlier one, is left out and its readers take the earlier result. It takes integer arithmetic and compares, integer selects, width and bit casts, extractvalue, getelementptr, handle creation and annotation, the integer `readnone` operations, `getDimensions`, and raw loads from the host's own tables (page table, view addresses, view marks, view strides). Float arithmetic, float compares, conversions, every other load, samples, atomics and wave operations stay as written. What goes is always a copy of an instruction that stays, so no operation and no declaration loses its last use: shader flags and declarations stay valid without being recomputed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
DenRakEiw
left a comment
There was a problem hiding this comment.
Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):
cargo test -p victory-shader --lib emit::bitcode::— 11 passed, 0 failed (1 ignored)cargo clippy -p victory-shader --all-targets— 0 warningscargo xtask policy— ok (184 files ok)
Clean dominator-tree GVN / value reuse pass (bitcode/reuse.rs) eliminating redundant emitted DXIL instructions. Looks great.
AI-assisted (Gemini 4 Neon).
…sak#2115 in; Marvisak#2150 left out for now Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Native NVIDIA run as requested: Windows, RTX 3050 Laptop, driver 32.0.16.1062, pinned Rust MSVC, adapter overrides unset. I ran
All five new reuse tests passed. The three failures had identical diagnostics on both runs: by-element stride read, missing buffer view, and gather (the three in #1835). All 467 printed arithmetic-model comparison lines also matched between runs. Six compiler-dependent checks skipped internally; the wave-32 permute test skipped its WARP path. I also checked that the pass actually rewrote a shader exercised by the suite. In a separate run, logging the return value of |
catminou4
left a comment
There was a problem hiding this comment.
Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.
149 passed / 0 failed / 2 ignored. One instruction for one value (13.5% fewer emitted instructions, +5 tests) — emit-size optimization with measured improvement; nice perf win.
Offline measurement on a retail dumpResult: no count change against I merged head 91d75b8 onto 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). |
|
Automated triage for the maintainers. Merge candidate. Re-reviewed at I checked what it admits:
The lowering declares wave and lane ops Evidence: on the title's archive, 10.5% fewer instructions on On your reply: the corrected note in Generated by Claude Code |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
RomainKH
left a comment
There was a problem hiding this comment.
I read reuse.rs against current main (13f91ca) and ran it merged there on macOS arm64 (CPU only). The pass looks correct to me. One sentence in the module notes states something the host does not guarantee.
What I checked
- Lane-dependent calls stay out.
integeradmits anyreadnonecall whose name ends in.i32, so I listed what the emitter declaresreadnone. On this head and onmain:unaryBits.i32,unary.i32,flattenedThreadIdInGroup.i32,dot4AddPacked.i32andloadInput.*, all functions of their operands. Every wave and quad op (waveReadLaneFirst,waveReadLaneAt,waveActiveOp,wavePrefixOp,waveActiveBallot,waveActiveBit,quadReadLaneAt) is declaredNoUnwind, andwaveGetLaneIndexisReadOnly, so none of them is merged. - The four tables are read-only views. They are
t0(page table),t1(view addresses),t2(view marks) and theVIEW_STRIDESroot SRV. The bitmaps shaders write areuviews and are not in the list:memory_dirty, andu1VIEW_WRITES, whichmain's new read marks (since this PR's base) also write. - Every terminator that reads a value is rewritten (only
CondBrdoes). Blocks the entry does not reach are not visited, butmain's writer now leaves them out (286a00e). - Merge with
main: the only conflict is inemit/mod.rs, two notes added at the same place; keeping both resolves it. Merged there, together with #2558 and #2342:cargo test -p victory-shader --locked --libgives 167 passed, 0 failed, 2 ignored. The hardware and WARP checks did not run here.
The module note
So the host does not change a table under a list that runs and reads it.
The host does change them. HostHeap writes the stride, mark and address entries into mapped upload buffers (write_stride, set_mark, note_address) whenever a slot is made, and descriptor/host.rs:531 says "Title threads rewrite slots while the GPU runs a submission".
The pass does not need that guarantee, though. Within one invocation, two reads of an entry the host is rewriting could each see either value, with or without the pass. With it, they agree. For example, buffer_guard's "no buffer view here" check and view_stride's stride then come from the same read of VIEW_STRIDES. I'd reword the note along those lines rather than state a host property.
One thing worth re-measuring
The 13.5 % was counted on 940d32f. Since then main makes one handle per descriptor at a fixed address in the entry block (850b66f), and the writer drops dead code and branches on constants (286a00e). Both overlap part of what this pass removes: createHandleFromHeap and annotateHandle were −2.4 M of the −25.7 M. A --count on the rebased head would show what is left.
AI-assisted (Claude Code); the results are from runs on my machine.
The host rewrites table entries while the GPU runs (the heap's write_stride, set_mark and note_address), so the notes no longer say it does not. The pass needs no such promise: inside one invocation the reads of one entry it merges give the first read's value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for checking the pass. Both points are done at The note. You are right that the host rewrites the tables while the GPU runs ( One correction to the example: The figure. Recounted on current
So −13.5 % was stale; it is now −10.5 %. The PR text is updated. AI-assisted. |
…k#2150) Port of upstream Marvisak#2150: new bitcode/reuse.rs implements a dominator-tree-scoped GVN over pure integer ops/selects/casts, readnone integer dx.ops, handle/DIMENSION calls and raw loads of the host's own tables (page table, view addresses/marks/strides). Shader::finish() runs it over the tables a shader reads, so the reads of one entry inside an invocation agree on the first read's value even where the host rewrites the entry mid-list (module docs; the semantics are unchanged inside an invocation, where no store or other thread can come between). Floats, non-table memory, samples and wave ops are excluded, so no contraction/reordering-sensitive computation changes meaning; anything left out is a copy of an instruction that stays, so no declaration loses its last use. Correctness: full shader suite green (300 passed), including the validator (rewritten bitcode validates), byte-sensitive diff tests and the hardware differential tests on a D3D12 device. Microbench (release, this machine): dominator GVN over a 600-inst synthetic module with 200x repeated pure ops folds 398 instructions (601 -> 203 insts, 5540 -> 2364 bytes, -57%) at 194 us pass cost. Upstream reports -13.5% emitted instructions on their corpus; translate-time and GPU effects follow DXIL size.
…ewer emitted instructions, no program lost)
Summary: a pass over the finished DXIL module keeps one instruction for one value. On the title's shaders it takes out 10.5 % of the emitted instructions (on main 13f91ca) and loses no program. It makes a synthetic store-heavy program 19–27 % faster on WARP and changes nothing measurable on an AMD GPU, whose compiler already does this. So the gain shown is smaller modules, not GPU time.
What it does
The lowering writes code as the console instructions name it: a register read as a float is cast again at each read, a descriptor's handle is made and its index clamped again in each block, an address is shifted and masked again for each access.
bitcode::reuse(new fileemit/bitcode/reuse.rs, called fromShader::finishbefore the module is written) walks the dominator tree. An instruction that computes what an earlier one computed, where every path to it has run that earlier one, is left out, and its readers take the earlier result.It takes part only where the result is a function of the operands:
extractvalue,getelementptrcreateHandleFromBinding,createHandleFromHeap,annotateHandle, and the integerdx.ops declaredreadnonegetDimensions: a resource's dimensions do not change while a shader runsIt leaves these exactly as the lowering wrote them:
dx.ops: a driver may contract or reorder them by how they are usedWhy it cannot break the flags
#1716 and #1646 each lose programs that validate on
main("Flags must match usage", "Internal declaration 'code' is unused"): removing the last use of an operation leaves the declared flags or a declaration stale. This pass removes only a copy of an instruction that stays, with the same operands. So no operation and no declaration loses its last use, no value loses its last reader, and nothing has to be recomputed. It does not do dead-code elimination or folding; those are #697, #1716 and #1646, and it composes with them.Measured
Offline, the title's
shaders.rpf(local title data, CPU only,translate --validate --list --countfrom #2115b0f0b2e, dxil.dll 10.0.26100.0; counts only). Counted onmain13f91ca, which already has 850b66f (one handle per descriptor in the entry block) and 286a00e (dead code left out, constant branches folded):main13f91caEvery program has the same outcome as on
main, compared program by program. What goes:bitcast−10,910,357,and−1,926,671,icmp−948,319,add−775,608,select−705,144,lshr−437,927,shl−406,785,or−390,609,annotateHandle−205,819,createHandleFromHeap−108,011,createHandleFromBinding−98,758,rawBufferLoad−84,623 (the table loads),getDimensions−925. Blocks are unchanged. (On940d32f, before 850b66f and 286a00e, it was 190,954,569 -> 165,215,092, −13.48 %.)GPU timestamps on a synthetic program (measured at
91d75b8on940d32f) (a compute program of 32 lanes that stores to an unordered buffer view 8 or 32 times, with write marks; least of 7 runs; the benchmark is not in this PR):mainWARP times do not predict a graphics card, and the Radeon rows show it: its compiler removes the copies itself. I did not measure pipeline compile time, and nothing was measured on NVIDIA or in the title.
Checks
cargo test -p victory-shader --libatdcdb50ewithVICTORY_WARP=1, one thread: 272 passed, 0 failed, 3 ignored, 3 skipped. Skipped:synthetic_tests::gathers_at_a_level_match_the_hardware,synthetic_tests::permutes_read_the_lane_named_and_zero_from_lanes_left_out,synthetic_tests::a_mask_joined_with_data_at_a_uniform_loop_is_its_number. They come from main, and their first leg asks for the hardware adapter, whichVICTORY_WARP=1turns into WARP (wave of 32 refused, WARP's gather rounding), so they fail there by design.-p victory-shader --all-targets -D warningsatdcdb50e: clean.cargo clippy -p victory-shader -p victory --all-targets: clean.rustfmt --checkon the new file: clean.cargo xtask policy: ok.Credit
The fork
fxceless/victory-forkhas a clean-up pass with a "reuse" step; the idea of doing this on the finished module comes from reading it. No code is taken from it. This pass differs in what it admits (an explicit list and the declaredreadnoneattribute, not name prefixes, and no floats).AI-assisted.
🤖 Generated with Claude Code