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

Closed
opened 2026-11-24 22:37:50 +00:00 by microwave805 · 0 comments
microwave805 commented 2026-11-24 22:37:50 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by microwave805 on 2026-11-24T22:37:50Z.


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.
<!-- greyfinch-sync:ivall/xcl:issue:6592813057 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/1891). Originally posted by [microwave805](https://github.com/microwave805) on 2026-11-24T22:37:50Z. --- 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.
Sign in to join this conversation.
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
xcl-dev/xcl#1881
No description provided.