Direct DXIL: block-local CSE in Module::optimize does not invalidate wave/quad intrinsics across dx.op.discard #1891

Closed
opened 2026-11-26 05:08:37 +00:00 by github-bot · 0 comments
Owner

@⁠microwave805 on GitHub · 2026-11-24 22:37 UTC · original

Static audit on main @ 689f2db found that block-local CSE in Module::optimize() caches wave and quad intrinsics across dx.op.discard within the same basic block.

Where

  • crates/xcl/src/gpu/dxil/module/opt.rs:82-106 (wave_call, cse_kind)
  • crates/xcl/src/gpu/dxil/module/opt.rs:448-475 (block-local local CSE table)

Root cause

cse_kind() classifies all dx.op.wave* and dx.op.quad* calls (except dx.op.waveIsFirstLane) as Cse::Block, storing them in a per-block local map (opt.rs:448).

In DXIL pixel shaders, dx.op.discard(cond) (opcode 82) is a normal mid-block call — not a block terminator — that demotes lanes with cond == true to helper lanes, changing the wave's active lane set for all subsequent wave/quad operations in the same block. Because opt.rs:449-475 never clears local when visiting a dx.op.discard call, any wave/quad intrinsic after dx.op.discard with identical operands (such as dx.op.waveActiveBallot(true) or dx.op.waveReadLaneFirst(x)) is replaced with the pre-discard value computed before lanes were demoted.

Trigger

Any DXIL pixel shader block containing a wave or quad op (e.g. waveActiveBallot(true)), followed by dx.op.discard(cond), followed by the same wave or quad op in the same basic block. Module::optimize() eliminates the second call and reuses the pre-discard result.

Fix direction

In opt.rs:449-475, when op is Op::Call(f, _) with self.funcs[*f as usize].name == "dx.op.discard", call local.clear(), and add a unit test in module/opt.rs verifying that identical waveActiveBallot calls on opposite sides of dx.op.discard are not merged.

AI use

  • Antigravity (Gemini 4 Neon): static code audit on main @ 689f2db and issue drafting.
**@⁠microwave805** on GitHub · 2026-11-24 22:37 UTC · [original](https://github.com/xcl-dev/xcl/issues/1891) Static audit on `main @ 689f2db` found that block-local CSE in `Module::optimize()` caches wave and quad intrinsics across `dx.op.discard` within the same basic block. ### Where - `crates/xcl/src/gpu/dxil/module/opt.rs:82-106` (`wave_call`, `cse_kind`) - `crates/xcl/src/gpu/dxil/module/opt.rs:448-475` (block-local `local` CSE table) ### Root cause `cse_kind()` classifies all `dx.op.wave*` and `dx.op.quad*` calls (except `dx.op.waveIsFirstLane`) as `Cse::Block`, storing them in a per-block `local` map (`opt.rs:448`). In DXIL pixel shaders, `dx.op.discard(cond)` (opcode 82) is a normal mid-block call — not a block terminator — that demotes lanes with `cond == true` to helper lanes, changing the wave's active lane set for all subsequent wave/quad operations in the same block. Because `opt.rs:449-475` never clears `local` when visiting a `dx.op.discard` call, any wave/quad intrinsic after `dx.op.discard` with identical operands (such as `dx.op.waveActiveBallot(true)` or `dx.op.waveReadLaneFirst(x)`) is replaced with the pre-discard value computed before lanes were demoted. ### Trigger Any DXIL pixel shader block containing a wave or quad op (e.g. `waveActiveBallot(true)`), followed by `dx.op.discard(cond)`, followed by the same wave or quad op in the same basic block. `Module::optimize()` eliminates the second call and reuses the pre-discard result. ### Fix direction In `opt.rs:449-475`, when `op` is `Op::Call(f, _)` with `self.funcs[*f as usize].name == "dx.op.discard"`, call `local.clear()`, and add a unit test in `module/opt.rs` verifying that identical `waveActiveBallot` calls on opposite sides of `dx.op.discard` are not merged. ### AI use - Antigravity (Gemini 4 Neon): static code audit on `main @ 689f2db` and issue drafting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:08:37 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.