shader: make every component's value before the first store of an output #263
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/263"
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 LakshanSivakumar/Native-Victory-macOS. Originally posted by Ketan-Kapse on 2026-11-27T06:41:53Z.
Refs #181
What and why
The export lowering made each channel's value and stored it at once. So a float cast or a 16-bit conversion of the next channel sat between two stores of the same output. Apple's Metal Shader Converter gathers the component stores of one output into a vector at the first of them. The library it wrote then used that later value before its definition.
metal-asrefuses such a module ("Instruction does not dominate all uses"), and Metal's compiler failed on mesh programs whose outputs a pixel stage reads.The loop in
Lower::exportnow makes every value first and then emits thestoreOutput,storeVertexOutputorstorePrimitiveOutputcalls back to back. A mesh program's thread index is taken once for all of them. Nothing else changes: the same values and the same stores, in the same block. No debug switch for the old order is included.Tests
emit::export_tests(victory-shader, new, three tests): hand-assembled pixel, vertex and mesh programs. For every output, the instruction that makes each stored value is written before the first store of that output. This coversstoreOutput(pixel and vertex),storeVertexOutputandstorePrimitiveOutput, with float casts and 16-bit conversions as the values. The check reads the order of the instructions as written, through a small test-only helper (Module::written_call_places).native::mesh_tests::a_converted_mesh_program_with_run_time_parameters_is_valid_ir(victory-gpu, new): a hand-assembled mesh program that exports the lane's number as a parameter is converted, listed withmetal-objdump, and assembled again withmetal-as, which verifies it. It is skipped when the converter or the Metal toolchain is absent.All four fail with the old order and pass with the new one. With the old order the last one fails with the same message as in the issue ("Instruction does not dominate all uses").
Output fingerprints
This reorders emitted IR. The DXIL of most shaders with exports differs byte for byte from before, so output fingerprints over a corpus will change for them. The disk caches follow by themselves: translations are keyed by the translator's source, and converted libraries and remembered compiler crashes by the DXIL, so each is made again once.
Count of shaders whose outcome changes: none is expected, by construction. The change does not add or remove a refusal path: every error return of the loop is still reached before anything else is emitted for the export, and a refusal ends the translation either way. I did not run a corpus on this branch, so I have no measured count for upstream main. What was measured is below, on our fork.
There are no fingerprint or golden tests in the tree that depend on the order. No existing test needed an update.
Evidence from our fork (measured there, before this branch was cut)
metal-as.metal-as, and compiler failures per run went from between 44 and 104 to 0.How it was verified here
Apple M4 Pro, macOS 27.0.1, Xcode 27.0, Metal Shader Converter 4.0 beta 3. Tests run as x86_64 under Rosetta (
--target x86_64-apple-darwin, release):list::tests::unordered_access_clears_have_the_same_slots_in_both_layouts, which fails the same way on main.--target, as AGENTS.md asks for victory-gpu): victory-gpu 317 passed, victory-shader 315 passed, victory-metal 30, 12 and 14 passed, none failed. Main has 316 in victory-gpu there, none failed.cargo build --release --target x86_64-apple-darwin -p victorybuilds.cargo xtask policypasses. clippy on the macOS target for victory-shader, victory-gpu, victory-metal and victory-backend reports the same findings as main (no new ones).cargo clippy --workspace --all-targets --locked -- -D warnings, and the same with--all-features) passes forx86_64-pc-windows-msvc, checked from this Mac with blake3's C build skipped (CARGO_FEATURE_PURE=1): a check, not a link. The gnullvm cross tools are not installed here, so the documented gnullvm command itself was not run.Not verified
diff_testsand others that need a D3D12 device) were not run.🤖 Generated with Claude Code
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T08:08:08Z.
Thank you. This is a clean change and the explanation of the converter's behaviour is clear. The three order tests are the right kind: they fail with the old order.
Before it can merge:
mesh_thread()emits an instruction or only reads one that exists. The text says the same values and the same stores; if the thread index is now made after the values, say so.Your fork measurements are useful, but they were taken with the span buffers change as well, so they do not show this change alone. I will run the vertex and pixel pairs listed in #181 before and after, as I said there.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Ketan-Kapse on 2026-11-27T08:34:05Z.
Thanks for the review. Answers to 2 and 3 now; the corpus table is yours to post, and I will add mine if I get a sweep set up.
2.
mesh_thread(). It emits at most one instruction for the whole program and it puts it in the entry block, not at the current position: the first call makesflattenedThreadIdInGroupinself.s.entryand caches the value inself.flat; every later call returns the cached value. Before this change the first call happened inside the loop (at the first mesh store), after it the first call happens between the two loops; in both cases the instruction lands in the entry block and every store uses the same value. So the thread index is not "made after the values" in the emitted code, and the only reordering is the stores moving behind the values.3. How the converter test was run. Locally on an Apple M4 Pro, macOS 27.0.1, Xcode 27.0 (its
metal-objdumpandmetal-asthroughxcrun), Metal Shader Converter 4.0 beta 3, as an x86_64 build under Rosetta and as a native arm64 build. With the old loop it fails withInstruction does not dominate all uses; with the new onemetal-asaccepts the module. I will add that to the PR text. It will not run in CI without the converter, as you say; the three order tests are the ones CI runs.On the fork numbers. A correction in the change's favour: the compiler-failure counts (44 to 104 a run before, 0 after, 143 of 143 libraries valid) were measured before the span buffers existed on our branch. That run differed from the one before it by this loop alone. The span buffers and the stride table came later the same day.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T08:53:07Z.
My own corpus run (33,625 containers, compared by output fingerprint, this branch merged onto main
2a3b03d1):Pixel: 7,549 outputs changed. Vertex: 13,274 changed. Compute: none. No shader gained, none lost, none hangs. So outcomes do not change, as you said; point 1 of my review is answered. It rewrites 20,823 outputs, so I will give it one game run before it merges; that run is in progress.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by alicans on 2026-11-27T09:18:52Z.
A game run with this PR on macOS 15, where the compiler crash in #181 first showed up: M4 Pro, 24 GB, macOS 15.8.2 (24H219), Metal Shader Converter 4.0.3 (Apple's package),
--console series-s, windowed.The build was main
c1b9587with this PR's52e4647applied on top. Two local switches were on, neither of which touches shaders: the title watchdog patch (image+0x2215c27/image+0x23cf683) and sampled textures without their two most detailed levels, needed to get to the world on 24 GB. The shader cache was cold for every exported shader, because the translator hash changed.Compiler failures: none.
Compiler encountered an internal errorline in the log and noXPC_ERROR_CONNECTION_INTERRUPTED.615820c53854c962,5a1c651829a060de,59c5abdcb23974a7,725962e1f1be4855,5d85e2176433f3af,80b896aafd2ec5f4,3f04c18f62a176a0,215b7ef6f2a22dd7,f5fa188ea38ad066,d4ad36beb4d0ead9.output of type float4 is not compatible with a MTLPixelFormatR32Uint color attachment(the class Sandile-0511 mentioned on #258).The rest of the run, for context:
work held for a wait that does not hold, with fence signals queued 2-13 s before the GPU reached them.VICTORY_METAL_RESIDENCY_SETS=heapwith this build yet.0xc0000005atimage+0x4dac5bf. I have seen this address before only on runs with the texture-level experiment on, so it may be mine and not this PR's.One run, so the frame timings are a single sample. The compiler result does not depend on timing: every previously failing pair was compiled.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T10:32:18Z.
Thank you for the answers on
mesh_thread()and on how the converter test was run. My corpus run is in the thread: no shader gained or lost, no hang, and 20,823 outputs changed (7,549 pixel, 13,274 vertex), so the outcomes do not change, as you said. alicans reports a run on macOS 15: the ten pixel shaders that crashed the compiler there now compile, and no compiler failure appears. Thank you both.Still needed: your workflow has not run (it needs my approval); please put the sentence about how the converter test was run into the description; and the branch is behind main. I have not yet played it alone: my table in #258 has five pull requests together.