shader: make every component's value before the first store of an output #263

Merged
Ketan-Kapse merged 1 commit from upstream-pr/263 into main 2026-11-27 11:42:57 +00:00
Ketan-Kapse commented 2026-11-27 06:41:53 +00:00 (Migrated from github.com)

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-as refuses 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::export now makes every value first and then emits the storeOutput, storeVertexOutput or storePrimitiveOutput calls 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 covers storeOutput (pixel and vertex), storeVertexOutput and storePrimitiveOutput, 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 with metal-objdump, and assembled again with metal-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)

  • Before: 66 failing and 110 passing mesh and pixel pairs were dumped. All 66 failing libraries, and 33 of the 110 passing ones, had "Instruction does not dominate all uses" under metal-as.
  • After: 143 of 143 dumped libraries pass metal-as, and compiler failures per run went from between 44 and 104 to 0.
  • 122 old and new pairs, matched by DXIL size, have identical output calls and identical declared output types.

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):

  • victory-shader: 315 passed, 0 failed, 3 ignored (312 on main, plus the 3 new ones).
  • victory-gpu: 316 passed, 1 failed, 1 ignored. The failure is list::tests::unordered_access_clears_have_the_same_slots_in_both_layouts, which fails the same way on main.
  • victory-metal: 30, 12 and 14 passed, as on main.
  • Run natively as arm64 too (debug, no --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 victory builds. cargo xtask policy passes. clippy on the macOS target for victory-shader, victory-gpu, victory-metal and victory-backend reports the same findings as main (no new ones).
  • The Windows clippy gate (cargo clippy --workspace --all-targets --locked -- -D warnings, and the same with --all-features) passes for x86_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

  • The Windows-only tests (diff_tests and others that need a D3D12 device) were not run.
  • No game run on this branch.

🤖 Generated with Claude Code

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5522425963 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/263). Originally posted by [Ketan-Kapse](https://github.com/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-as` refuses 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::export` now makes every value first and then emits the `storeOutput`, `storeVertexOutput` or `storePrimitiveOutput` calls 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 covers `storeOutput` (pixel and vertex), `storeVertexOutput` and `storePrimitiveOutput`, 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 with `metal-objdump`, and assembled again with `metal-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) - Before: 66 failing and 110 passing mesh and pixel pairs were dumped. All 66 failing libraries, and 33 of the 110 passing ones, had "Instruction does not dominate all uses" under `metal-as`. - After: 143 of 143 dumped libraries pass `metal-as`, and compiler failures per run went from between 44 and 104 to 0. - 122 old and new pairs, matched by DXIL size, have identical output calls and identical declared output types. ## 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): - victory-shader: 315 passed, 0 failed, 3 ignored (312 on main, plus the 3 new ones). - victory-gpu: 316 passed, 1 failed, 1 ignored. The failure is `list::tests::unordered_access_clears_have_the_same_slots_in_both_layouts`, which fails the same way on main. - victory-metal: 30, 12 and 14 passed, as on main. - Run natively as arm64 too (debug, no `--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 victory` builds. `cargo xtask policy` passes. clippy on the macOS target for victory-shader, victory-gpu, victory-metal and victory-backend reports the same findings as main (no new ones). - The Windows clippy gate (`cargo clippy --workspace --all-targets --locked -- -D warnings`, and the same with `--all-features`) passes for `x86_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 - The Windows-only tests (`diff_tests` and others that need a D3D12 device) were not run. - No game run on this branch. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
LakshanSivakumar commented 2026-11-27 08:08:08 +00:00 (Migrated from github.com)

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:

  1. A sweep over a corpus of the title's shaders by output fingerprint, with the counts of outcomes (translated and refused before and after). The text says outcomes should not change and fingerprints will. I read the loop the same way, but the rule is that a change to the translator is measured. I am running this branch over my own corpus now and will post the table here.
  2. Please say whether 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.
  3. The converter test is skipped when the converter or the Metal tools are missing, so CI will not run it. Please say in the text how it was run and on what.

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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7213188443 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/263#issuecomment-7213188443). Originally posted by [LakshanSivakumar](https://github.com/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: 1. A sweep over a corpus of the title's shaders by output fingerprint, with the counts of outcomes (translated and refused before and after). The text says outcomes should not change and fingerprints will. I read the loop the same way, but the rule is that a change to the translator is measured. I am running this branch over my own corpus now and will post the table here. 2. Please say whether `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. 3. The converter test is skipped when the converter or the Metal tools are missing, so CI will not run it. Please say in the text how it was run and on what. 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.
Ketan-Kapse commented 2026-11-27 08:34:05 +00:00 (Migrated from github.com)

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 makes flattenedThreadIdInGroup in self.s.entry and caches the value in self.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-objdump and metal-as through xcrun), 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 with Instruction does not dominate all uses; with the new one metal-as accepts 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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7213676136 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/263#issuecomment-7213676136). Originally posted by [Ketan-Kapse](https://github.com/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 makes `flattenedThreadIdInGroup` in `self.s.entry` and caches the value in `self.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-objdump` and `metal-as` through `xcrun`), 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 with `Instruction does not dominate all uses`; with the new one `metal-as` accepts 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.
LakshanSivakumar commented 2026-11-27 08:53:07 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7214033446 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/263#issuecomment-7214033446). Originally posted by [LakshanSivakumar](https://github.com/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.
alicans commented 2026-11-27 09:18:52 +00:00 (Migrated from github.com)

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 c1b9587 with this PR's 52e4647 applied 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.

  • No Compiler encountered an internal error line in the log and no XPC_ERROR_CONNECTION_INTERRUPTED.
  • The ten pixel shaders that failed this way on macOS 15 before were each first-bound and none was refused: 615820c53854c962, 5a1c651829a060de, 59c5abdcb23974a7, 725962e1f1be4855, 5d85e2176433f3af, 80b896aafd2ec5f4, 3f04c18f62a176a0, 215b7ef6f2a22dd7, f5fa188ea38ad066, d4ad36beb4d0ead9.
  • Before this PR they all failed on macOS 15, at about 30 s each before the compile deadline landed, and were refused.
  • The only conversion refusals left are 180 of 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:

  • Prewarm of 16,384 shaders took 84 s.
  • The first world frame was at about +103 s: frame 1885, with 1,688 first binds in that frame.
  • After that the world advanced 12 frames in 156 s.
    • The log repeats work held for a wait that does not hold, with fence signals queued 2-13 s before the GPU reached them.
    • This matches the default-residency numbers Sandile-0511 posted on #79 for a 24 GB M5. I have not tried VICTORY_METAL_RESIDENCY_SETS=heap with this build yet.
  • On screen the world is black (#258); the UI layer draws.
  • The memory level fell from 80 % to 23-25 %; the process RSS reached 8 GB.
  • At +284 s the title ended with an unhandled 0xc0000005 at image+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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7214532802 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/263#issuecomment-7214532802). Originally posted by [alicans](https://github.com/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 `c1b9587` with this PR's `52e4647` applied 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.** - No `Compiler encountered an internal error` line in the log and no `XPC_ERROR_CONNECTION_INTERRUPTED`. - The ten pixel shaders that failed this way on macOS 15 before were each first-bound and none was refused: `615820c53854c962`, `5a1c651829a060de`, `59c5abdcb23974a7`, `725962e1f1be4855`, `5d85e2176433f3af`, `80b896aafd2ec5f4`, `3f04c18f62a176a0`, `215b7ef6f2a22dd7`, `f5fa188ea38ad066`, `d4ad36beb4d0ead9`. - Before this PR they all failed on macOS 15, at about 30 s each before the compile deadline landed, and were refused. - The only conversion refusals left are 180 of `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:** - Prewarm of 16,384 shaders took 84 s. - The first world frame was at about +103 s: frame 1885, with 1,688 first binds in that frame. - After that the world advanced 12 frames in 156 s. - The log repeats `work held for a wait that does not hold`, with fence signals queued 2-13 s before the GPU reached them. - This matches the default-residency numbers Sandile-0511 posted on #79 for a 24 GB M5. I have not tried `VICTORY_METAL_RESIDENCY_SETS=heap` with this build yet. - On screen the world is black (#258); the UI layer draws. - The memory level fell from 80 % to 23-25 %; the process RSS reached 8 GB. - At +284 s the title ended with an unhandled `0xc0000005` at `image+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.
LakshanSivakumar commented 2026-11-27 10:32:18 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7216003897 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/263#issuecomment-7216003897). Originally posted by [LakshanSivakumar](https://github.com/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.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
LakshanSivakumar/Native-Victory-macOS!263
No description provided.