xshader: optimize HLSL codegen without crashing DXC new inlining #523

Merged
glovepost merged 1 commit from upstream-pr/523 into main 2026-12-01 15:10:09 +00:00
glovepost commented 2026-11-29 19:50:29 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-29T19:50:29Z.


Rebased onto origin/main (f360038d), split per review, and fixed the DXC 1.9 (1.9.2609.5) -fnew-inlining-behavior SIGSEGV.

Split PRs

  • 3-bank page_waves ballots & double-buffered reads -> #555
  • Uniform scc_from_mask scalar arithmetic/bitcount elision -> #558

DXC 1.9 (1.9.2609.5) -fnew-inlining-behavior Segfault Root Cause

Using the exact runtime flags (-disable-lifetime-markers -fnew-inlining-behavior, plus -O1 for cs_6_6) across 258 recorded ProjectScarlett requests:

  • origin/main: 213 compiled, 0 crashes (45 xshader unsupported).
  • Original #523 (3fdea412): 143/213 crashed with SIGSEGV (exit -11) (45 cs_6_6, 20 ms_6_6, 62 ps_6_6, 3 ps_6_1, 2 ps_6_0, 9 vs_6_6, 2 vs_6_0).
  • Bisect proof:
    • Reverting compare.rs alone turned 143/143 crashing shaders into 143/143 passing.
    • Keeping compare.rs's 64-bit integer XOR/and form and trivial pred == 0 / pred == 15 shortcuts while removing tb_f32_relation / tb_f32_relation_ftz turned 30/30 crashes into 30/30 passes.
    • Applying compare.rs alone onto origin/main reproduced 30/30 DXC exit -11 segfaults.
    • On CS_InterpolateStrands_94d04887_Wrapped, even rewriting tb_f32_relation_ftz from select(...) to ternary ?: or early if (...) return ...; still segfaulted DXC under -fnew-inlining-behavior, so tb_f32_relation / tb_f32_relation_ftz has been dropped from this PR (backed up on glovepost/TuxBox:backup/pr523-full).

Remaining HLSL Codegen Summary

  • Dead high-half exec_hi / vcc_hi zero initialization elision (setup.rs, output.rs) and Opnd::Null wv/ws write elision (state.rs).
  • Structured buffer stride specialization (tb_sbw_* and power-of-2 / single-word shifts in output.rs, scalar_memory.rs, buffer.rs).
  • Constant folding / dyadic literal specialization (v_mul_legacy_f32 via tb_mullc, shifts, v_bfe_i32, v_cvt_pk_u8_f32), branchless 64-bit bit ops/shifts/readfirstlane/exec_group helpers (helper_code.rs, vector.rs, vector_wide.rs, scalar_bits.rs, operands.rs), redundant (uint) cast elision in exec_group::predicate, and 64-bit integer XOR/and compares + trivial pred 0/15 float shortcuts (compare.rs).

Verification

  • python3 tuxbox.py check --no-gpu passed 9/9 on final commit HEAD:
    • Python tests: 740 run, 0 failed
    • Shader compiler tests: 654 + 3 + 2 passed (host_profiles.txt updated for changed emission)
    • DXBC tests: 120 passed
    • XG layout tests: 24 passed, 1 ignored; XG entry point tests: 7 passed
    • Storage codec tests: 22 passed
    • ACP HAL tests: 9 passed
    • D3D12 Windows typecheck: passed
  • DXC 1.9 (1.9.2609.5) -disable-lifetime-markers -fnew-inlining-behavior check across 258 recorded ProjectScarlett requests on final commit HEAD: 213/213 compiled, 0 DXC segfaults (45 xshader unsupported, identical to origin/main).
  • tools/stage_baseline.py cpu on the 58 locally recorded tools/reference/stage-baseline/sample.json requests (89 translations; measured on the compare.rs-reverted pre-final build vs origin/main):
    • 89/89 compiled (0 regressions), 87/89 changed DXIL.
    • HLSL bytes: 108,204,252 -> 104,244,946 (-3.659%)
    • DXIL bytes: 35,797,836 -> 35,489,752 (-0.861%)
    • DXC wall time: sum 359.68s -> 354.13s (-1.54%), median 4.199s -> 3.971s (-5.43%, -7.45% across changed DXIL)
    • DXC CPU time (cpu_seconds): sum 357.51s -> 351.66s (-1.64%), median 4.188s -> 3.916s (-6.49%)
  • GPU tests (tuxbox.py check) and ProjectScarlett were not run against this rebased commit.

AI-assisted: bisected, split, rebased, and tested with LLM tooling.

<!-- greyfinch-sync:brcly/TuxBox:issue:5554463852 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/523). Originally posted by [glovepost](https://github.com/glovepost) on 2026-11-29T19:50:29Z. --- Rebased onto `origin/main` (`f360038d`), split per review, and fixed the DXC 1.9 (`1.9.2609.5`) `-fnew-inlining-behavior` SIGSEGV. ### Split PRs - 3-bank `page_waves` ballots & double-buffered reads -> **#555** - Uniform `scc_from_mask` scalar arithmetic/bitcount elision -> **#558** ### DXC 1.9 (`1.9.2609.5`) `-fnew-inlining-behavior` Segfault Root Cause Using the exact runtime flags (`-disable-lifetime-markers -fnew-inlining-behavior`, plus `-O1` for `cs_6_6`) across 258 recorded ProjectScarlett requests: - `origin/main`: `213` compiled, `0` crashes (`45` `xshader` unsupported). - Original `#523` (`3fdea412`): **`143/213` crashed with SIGSEGV (`exit -11`)** (`45 cs_6_6`, `20 ms_6_6`, `62 ps_6_6`, `3 ps_6_1`, `2 ps_6_0`, `9 vs_6_6`, `2 vs_6_0`). - Bisect proof: - Reverting `compare.rs` alone turned `143/143` crashing shaders into `143/143` passing. - Keeping `compare.rs`'s 64-bit integer XOR/`and` form and trivial `pred == 0` / `pred == 15` shortcuts while removing `tb_f32_relation` / `tb_f32_relation_ftz` turned `30/30` crashes into `30/30` passes. - Applying `compare.rs` alone onto `origin/main` reproduced `30/30` DXC `exit -11` segfaults. - On `CS_InterpolateStrands_94d04887_Wrapped`, even rewriting `tb_f32_relation_ftz` from `select(...)` to ternary `?:` or early `if (...) return ...;` still segfaulted DXC under `-fnew-inlining-behavior`, so `tb_f32_relation` / `tb_f32_relation_ftz` has been dropped from this PR (backed up on `glovepost/TuxBox:backup/pr523-full`). ### Remaining HLSL Codegen Summary - Dead high-half `exec_hi` / `vcc_hi` zero initialization elision (`setup.rs`, `output.rs`) and `Opnd::Null` `wv`/`ws` write elision (`state.rs`). - Structured buffer stride specialization (`tb_sbw_*` and power-of-2 / single-word shifts in `output.rs`, `scalar_memory.rs`, `buffer.rs`). - Constant folding / dyadic literal specialization (`v_mul_legacy_f32` via `tb_mullc`, shifts, `v_bfe_i32`, `v_cvt_pk_u8_f32`), branchless 64-bit bit ops/shifts/`readfirstlane`/`exec_group` helpers (`helper_code.rs`, `vector.rs`, `vector_wide.rs`, `scalar_bits.rs`, `operands.rs`), redundant `(uint)` cast elision in `exec_group::predicate`, and 64-bit integer XOR/`and` compares + trivial `pred 0/15` float shortcuts (`compare.rs`). ### Verification - `python3 tuxbox.py check --no-gpu` passed **9/9** on final commit HEAD: - Python tests: 740 run, 0 failed - Shader compiler tests: 654 + 3 + 2 passed (`host_profiles.txt` updated for changed emission) - DXBC tests: 120 passed - XG layout tests: 24 passed, 1 ignored; XG entry point tests: 7 passed - Storage codec tests: 22 passed - ACP HAL tests: 9 passed - D3D12 Windows typecheck: passed - DXC 1.9 (`1.9.2609.5`) `-disable-lifetime-markers -fnew-inlining-behavior` check across 258 recorded ProjectScarlett requests on final commit HEAD: **`213/213` compiled, `0` DXC segfaults** (`45` `xshader` unsupported, identical to `origin/main`). - `tools/stage_baseline.py cpu` on the 58 locally recorded `tools/reference/stage-baseline/sample.json` requests (`89` translations; measured on the `compare.rs`-reverted pre-final build vs `origin/main`): - `89/89` compiled (`0` regressions), `87/89` changed DXIL. - HLSL bytes: `108,204,252 -> 104,244,946` (**`-3.659%`**) - DXIL bytes: `35,797,836 -> 35,489,752` (**`-0.861%`**) - DXC wall time: sum `359.68s -> 354.13s` (`-1.54%`), median `4.199s -> 3.971s` (**`-5.43%`**, `-7.45%` across changed DXIL) - DXC CPU time (`cpu_seconds`): sum `357.51s -> 351.66s` (`-1.64%`), median `4.188s -> 3.916s` (**`-6.49%`**) - GPU tests (`tuxbox.py check`) and ProjectScarlett were not run against this rebased commit. AI-assisted: bisected, split, rebased, and tested with LLM tooling.
brcly commented 2026-11-29 21:41:54 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-29T21:41:54Z.


We measured this against main on the recorded ProjectScarlett requests before merging, and found a blocker.

  • It changes the HLSL of 33,961 of 34,402 translated programs (none gained or lost). That means a full recompile for every player, so it has to be clearly faster or smaller.
  • DXC 1.9 (1.9.2609.5) crashes (exit 139, segfault) on its output for 101 of 180 sampled programs when compiled with -fnew-inlining-behavior, which the runtime passes for game shaders (INLINING_FLAG in runtime_shaders.py). The same programs from main compile. Without the flag, or with the default flags only, they compile, so the translator GPU checks, which use their own flags, don't see it. In the game, every one of those compiles would crash and be retried without the flag, which is the slower path.
  • On the 79 that do compile with the flag: 2.6 s → 2.5 s of DXC (−5%), DXIL the same size.

To reproduce: translate any recorded request with this branch and run dxc -T ps_6_6 -E main -HV 2021 -disable-lifetime-markers -fnew-inlining-behavior on the HLSL. The new helper forms (the branchless select(and(...)) helpers and the specialised relation helpers) are where I'd look first, but I haven't bisected it.

It also bundles three changes (codegen, 3-bank page_waves, mesh SCC elision). If you split them, the parts that don't trigger the crash can be measured and land on their own.

<!-- greyfinch-sync:brcly/TuxBox:comment:7263079230 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/523#issuecomment-7263079230). Originally posted by [brcly](https://github.com/brcly) on 2026-11-29T21:41:54Z. --- We measured this against `main` on the recorded ProjectScarlett requests before merging, and found a blocker. - **It changes the HLSL of 33,961 of 34,402 translated programs** (none gained or lost). That means a full recompile for every player, so it has to be clearly faster or smaller. - **DXC 1.9 (1.9.2609.5) crashes (exit 139, segfault) on its output for 101 of 180 sampled programs** when compiled with `-fnew-inlining-behavior`, which the runtime passes for game shaders (`INLINING_FLAG` in `runtime_shaders.py`). The same programs from `main` compile. Without the flag, or with the default flags only, they compile, so the translator GPU checks, which use their own flags, don't see it. In the game, every one of those compiles would crash and be retried without the flag, which is the slower path. - On the 79 that do compile with the flag: 2.6 s → 2.5 s of DXC (−5%), DXIL the same size. To reproduce: translate any recorded request with this branch and run `dxc -T ps_6_6 -E main -HV 2021 -disable-lifetime-markers -fnew-inlining-behavior` on the HLSL. The new helper forms (the branchless `select(and(...))` helpers and the specialised relation helpers) are where I'd look first, but I haven't bisected it. It also bundles three changes (codegen, 3-bank `page_waves`, mesh SCC elision). If you split them, the parts that don't trigger the crash can be measured and land on their own.
glovepost commented 2026-11-29 23:25:41 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-29T23:25:41Z.


Thanks for catching that DXC crash! I reproduced and bisected it locally using dxc 1.9.2609.5 with the runtime flags (-disable-lifetime-markers -fnew-inlining-behavior, plus -O1 for cs_6_6) across 258 recorded ProjectScarlett requests:

Bisect result

  • Trigger: tb_f32_relation / tb_f32_relation_ftz in tools/shader-compiler/src/hlsl/compare.rs is the sole necessary and sufficient cause of all 143/213 DXC exit -11 (SIGSEGV) crashes on 3fdea412 (45 cs_6_6, 20 ms_6_6, 62 ps_6_6, 3 ps_6_1, 2 ps_6_0, 9 vs_6_6, 2 vs_6_0):
    • Reverting compare.rs alone turned 143/143 crashes into 143/143 passes (select(and(...)) in helper_code.rs and predicate.rs were innocent).
    • Keeping the 64-bit integer XOR/and compare cleanup and trivial pred == 0/15 shortcuts while dropping tb_f32_relation / tb_f32_relation_ftz still passed 30/30 (and 213/213 on final HEAD).
    • Applying compare.rs alone onto origin/main crashed 30/30.
    • Note: on CS_InterpolateStrands_94d04887_Wrapped, even rewriting tb_f32_relation_ftz from select(...) to ?: or early if (...) return ...; still crashed DXC under -fnew-inlining-behavior.

Splits & cleanup

  • #555: 3-bank page_waves ballots & double-buffered reads (9/9 host checks, 213/213 DXC -fnew-inlining-behavior ok).
  • #558: Uniform scc_from_mask scalar arithmetic/bitcount elision (9/9 host checks, 213/213 DXC -fnew-inlining-behavior ok).
  • #523 (this PR): Rebased onto origin/main (f360038d) with tb_f32_relation*, page_waves, and abi.rs removed/split out (full 3fdea412 backed up on glovepost/TuxBox:backup/pr523-full):
    • tuxbox.py check --no-gpu: 9/9 passed (740 Python tests, 654+3+2 xshader).
    • Runtime DXC -fnew-inlining-behavior check across 258 requests: 213/213 ok, 0 crashes.
    • stage_baseline.py cpu (58 sample.json requests / 89 translations, measured on the compare.rs-reverted pre-final build): -3.659% HLSL bytes, -0.861% DXIL bytes, -6.49% median DXC cpu_seconds (0 regressions).

GPU checks (tuxbox.py check) and ProjectScarlett were not run against this rebased commit.

<!-- greyfinch-sync:brcly/TuxBox:comment:7264452088 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/523#issuecomment-7264452088). Originally posted by [glovepost](https://github.com/glovepost) on 2026-11-29T23:25:41Z. --- Thanks for catching that DXC crash! I reproduced and bisected it locally using `dxc 1.9.2609.5` with the runtime flags (`-disable-lifetime-markers -fnew-inlining-behavior`, plus `-O1` for `cs_6_6`) across 258 recorded ProjectScarlett requests: ### Bisect result - **Trigger:** `tb_f32_relation` / `tb_f32_relation_ftz` in `tools/shader-compiler/src/hlsl/compare.rs` is the sole necessary and sufficient cause of all `143/213` DXC `exit -11` (SIGSEGV) crashes on `3fdea412` (`45 cs_6_6`, `20 ms_6_6`, `62 ps_6_6`, `3 ps_6_1`, `2 ps_6_0`, `9 vs_6_6`, `2 vs_6_0`): - Reverting `compare.rs` alone turned `143/143` crashes into `143/143` passes (`select(and(...))` in `helper_code.rs` and `predicate.rs` were innocent). - Keeping the 64-bit integer XOR/`and` compare cleanup and trivial `pred == 0`/`15` shortcuts while dropping `tb_f32_relation` / `tb_f32_relation_ftz` still passed `30/30` (and `213/213` on final HEAD). - Applying `compare.rs` alone onto `origin/main` crashed `30/30`. - Note: on `CS_InterpolateStrands_94d04887_Wrapped`, even rewriting `tb_f32_relation_ftz` from `select(...)` to `?:` or early `if (...) return ...;` still crashed DXC under `-fnew-inlining-behavior`. ### Splits & cleanup - **#555:** 3-bank `page_waves` ballots & double-buffered reads (`9/9` host checks, `213/213` DXC `-fnew-inlining-behavior` ok). - **#558:** Uniform `scc_from_mask` scalar arithmetic/bitcount elision (`9/9` host checks, `213/213` DXC `-fnew-inlining-behavior` ok). - **#523 (this PR):** Rebased onto `origin/main` (`f360038d`) with `tb_f32_relation*`, `page_waves`, and `abi.rs` removed/split out (full `3fdea412` backed up on `glovepost/TuxBox:backup/pr523-full`): - `tuxbox.py check --no-gpu`: **9/9** passed (`740` Python tests, `654+3+2` xshader). - Runtime DXC `-fnew-inlining-behavior` check across 258 requests: **`213/213` ok, `0` crashes**. - `stage_baseline.py cpu` (58 `sample.json` requests / 89 translations, measured on the `compare.rs`-reverted pre-final build): **`-3.659%` HLSL bytes, `-0.861%` DXIL bytes, `-6.49%` median DXC `cpu_seconds`** (`0` regressions). GPU checks (`tuxbox.py check`) and ProjectScarlett were not run against this rebased commit.
glovepost commented 2026-11-30 00:35:38 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-30T00:35:38Z.


Rebased onto main (f482572a, commit f1194f8b461c) with no functional changes from 2bb24112, and ran the changed GPU checks on NVIDIA:

  • Command: TUXBOX_CHECK_JOBS=4 DXC=<dxc> python3 tuxbox.py check --changed --adapter 10de:1e84 --proton <proton> --prefix <prefix> --windows-dxc <windows-dxc>
  • GPU / driver / backend: NVIDIA GeForce RTX 2070 SUPER (10de:1e84), driver 620.73.04, GE-Proton11-14
  • Results: Lanes and fixtures on the GPU (lanes-bindless-compute, lanes-compute-root, lanes-compute-root-recovery, lanes-raster-tables, lanes-shader-cache) and Shader translator on the GPU all passed (13 passed, 0 failed, 0 not run).
  • Still missing: AMD GPU checks. ProjectScarlett was not run on this commit.

Evidence: TuxBox check of f1194f8b461c, no uncommitted changes, GPU 10de:1e84: 13 passed, 0 failed, 0 not run

<!-- greyfinch-sync:brcly/TuxBox:comment:7265407067 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/523#issuecomment-7265407067). Originally posted by [glovepost](https://github.com/glovepost) on 2026-11-30T00:35:38Z. --- Rebased onto `main` (`f482572a`, commit `f1194f8b461c`) with no functional changes from `2bb24112`, and ran the changed GPU checks on NVIDIA: - **Command:** `TUXBOX_CHECK_JOBS=4 DXC=<dxc> python3 tuxbox.py check --changed --adapter 10de:1e84 --proton <proton> --prefix <prefix> --windows-dxc <windows-dxc>` - **GPU / driver / backend:** NVIDIA GeForce RTX 2070 SUPER (`10de:1e84`), driver `620.73.04`, `GE-Proton11-14` - **Results:** `Lanes and fixtures on the GPU` (`lanes-bindless-compute`, `lanes-compute-root`, `lanes-compute-root-recovery`, `lanes-raster-tables`, `lanes-shader-cache`) and `Shader translator on the GPU` all passed (**13 passed, 0 failed, 0 not run**). - **Still missing:** AMD GPU checks. ProjectScarlett was not run on this commit. `Evidence: TuxBox check of f1194f8b461c, no uncommitted changes, GPU 10de:1e84: 13 passed, 0 failed, 0 not run`
NickStallman commented 2026-11-30 01:20:31 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-30T01:20:31Z.


The AMD check you were missing, on f1194f8b: AMD iGPU (1002:13c0, RADV, Mesa 26.0.8, GE-Proton11-14, Linux), full python tuxbox.py check: 199 pass, 0 fail. A game was running on the other card during part of it. ProjectScarlett was not run on this commit.

Evidence: TuxBox check of f1194f8b46, no uncommitted changes, GPU 1002:13c0: 14 passed, 0 failed, 0 not run

<!-- greyfinch-sync:brcly/TuxBox:comment:7266054809 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/523#issuecomment-7266054809). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-11-30T01:20:31Z. --- The AMD check you were missing, on f1194f8b: AMD iGPU (`1002:13c0`, RADV, Mesa 26.0.8, GE-Proton11-14, Linux), full `python tuxbox.py check`: 199 pass, 0 fail. A game was running on the other card during part of it. ProjectScarlett was not run on this commit. Evidence: TuxBox check of f1194f8b461c, no uncommitted changes, GPU 1002:13c0: 14 passed, 0 failed, 0 not run
brcly commented 2026-11-30 04:12:21 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-30T04:12:21Z.


Heads-up: main just landed a large cleanup (up to e5d2e913), so this PR needs a rebase.

  • Formatting: the whole workspace is now formatted with rustfmt (see the new rustfmt.toml).
  • Lints: every crate denies unwrap(), lossy as casts and undocumented unsafe outside tests.
  • CI: it now also runs cargo fmt --check, rustdoc with warnings denied, and Miri on the providers' unit tests.

The least painful way is to format your branch first, so the rebase only shows real changes:

git fetch origin
git checkout origin/main -- rustfmt.toml
cargo fmt --all && git commit -am "rustfmt"
git rebase origin/main

Then run python tuxbox.py check --no-gpu, which now includes the format step.

If clippy flags something new:

  • A cast: use From for widening, TryFrom for narrowing, and a mask for bit fields.
  • An unwrap(): use ?, or expect with the reason it can't fail.
  • An unsafe block: it needs a // SAFETY: comment.

CONTRIBUTING's "Code checks" section lists the commands. Thanks!

<!-- greyfinch-sync:brcly/TuxBox:comment:7268562826 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/523#issuecomment-7268562826). Originally posted by [brcly](https://github.com/brcly) on 2026-11-30T04:12:21Z. --- Heads-up: main just landed a large cleanup (up to e5d2e913), so this PR needs a rebase. - **Formatting:** the whole workspace is now formatted with rustfmt (see the new `rustfmt.toml`). - **Lints:** every crate denies `unwrap()`, lossy `as` casts and undocumented `unsafe` outside tests. - **CI:** it now also runs `cargo fmt --check`, rustdoc with warnings denied, and Miri on the providers' unit tests. The least painful way is to format your branch first, so the rebase only shows real changes: ```sh git fetch origin git checkout origin/main -- rustfmt.toml cargo fmt --all && git commit -am "rustfmt" git rebase origin/main ``` Then run `python tuxbox.py check --no-gpu`, which now includes the format step. If clippy flags something new: - **A cast:** use `From` for widening, `TryFrom` for narrowing, and a mask for bit fields. - **An `unwrap()`:** use `?`, or `expect` with the reason it can't fail. - **An `unsafe` block:** it needs a `// SAFETY:` comment. CONTRIBUTING's "Code checks" section lists the commands. Thanks!
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
brcly/TuxBox!523
No description provided.