Direct DXIL: TG_SIZE truncation, dead-inst double-extension scan, Alloca align=0 inalloca bit, stripped TGSM PSV0 size, and heap_raw_buffer feature flag #1894

Open
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 five small, self-contained correctness bugs across Direct DXIL's compute init, bitcode encoder, and shader-flag metadata:

1. Compute TG_SIZE[5:0] truncates total / lanes instead of rounding up

  • Where: crates/xcl/src/gpu/dxil/kernel.rs:568-578 (compute_init)
  • Bug:
    let w = lw.c(total / lanes);
    
    On RDNA2, TG_SIZE[5:0] holds the number of waves in the threadgroup (ceil(total / lanes)). For any non-multiple threadgroup size (e.g. total = 16 or 32 with wave64, or [8, 1, 1] with wave32), total / lanes evaluates to 0 instead of 1 (and e.g. total = 96 in wave64 gives 1 instead of 2).
  • Fix: total.div_ceil(lanes) (compare lw.waves = t.div_ceil(64).max(1) at kernel.rs:486).

2. Module::uses_double_extensions() scans dead self.insts instead of live self.blocks

  • Where: crates/xcl/src/gpu/dxil/module.rs:1394-1402
  • Bug: uses_double_extensions() iterates self.insts.iter() instead of self.blocks.iter().flatten() (contrast insts_use_type() directly below it at module.rs:1403-1419, which iterates self.blocks.iter().flatten()). Any dead/DCE'd/unreachable-block f64 fdiv or f64↔int cast remains in self.insts and leaves Feature::DoubleExtensions set in shader flags even though the instruction is not emitted in bitcode (DXIL validator rejects mismatched shader feature flags).
  • Fix: Iterate self.blocks.iter().flatten().map(|&i| &self.insts[i as usize]).

3. Op::Alloca bitcode encoding corrupts align == 0 into inalloca flag (bit 5)

  • Where: crates/xcl/src/gpu/dxil/module.rs:1227-1232
  • Bug:
    Op::Alloca(t, n, align) => {
        let i32t = self.value_ty(*n);
        let a = align.trailing_zeros() as u64 + 1;
        w.record(FUNC_CODE_INST_ALLOCA, &[t.0 as u64, i32t.0 as u64, abs(*n), a | 1 << 6])
    }
    
    In LLVM 3.7 bitcode, Alloca alignment is encoded in bits [4:0] as 0 (default alignment) or log2(align) + 1 (1..31), while bit 5 (1 << 5 = 32) is inalloca and bit 6 (1 << 6) is explicit type. When align == 0, 0u32.trailing_zeros() + 1 == 33 == (1 << 5) | 1, silently setting the inalloca flag bit and encoding align 1!
  • Fix: let a = if *align == 0 { 0 } else { align.trailing_zeros() as u64 + 1 }; (and assert power-of-two / align.is_power_of_two()).

4. Module::global_bytes_layout() counts stripped unused TGSM globals in PSV0

  • Where: crates/xcl/src/gpu/dxil/module.rs:1501-1503
  • Bug: global_bytes_layout() sums self.globals.iter() unfiltered, whereas the bitcode writer strips unreferenced globals (e.g. TGSM arrays whose uses were eliminated by Module::optimize()). For mesh shaders / compute metadata using global_bytes_layout(3, ...), PSV0 reports non-zero TGSM size for globals absent from the emitted bitcode.
  • Fix: Count only globals referenced by live instructions in self.blocks (matching the bitcode writer's liveness filter).

5. Shader::heap_raw_buffer() omits self.require(Feature::RawAndStructuredBuffers)

  • Where: crates/xcl/src/gpu/dxil/shader.rs:289-293
  • Bug: Shader::heap_raw_buffer() calls handle_from_heap + annotate(RAW_BUFFER) without calling self.require(Feature::RawAndStructuredBuffers) (contrast Shader::heap_raw_buffer_uav() directly below at shader.rs:297-303, which calls self.require(Feature::RawAndStructuredBuffers)). A shader whose only raw buffers come through heap_raw_buffer() will miss the required shader flag in DXIL validation.
  • Fix: Add self.require(Feature::RawAndStructuredBuffers); in Shader::heap_raw_buffer().

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/1894) Static audit on `main @ 689f2db` found five small, self-contained correctness bugs across Direct DXIL's compute init, bitcode encoder, and shader-flag metadata: ### 1. Compute `TG_SIZE[5:0]` truncates `total / lanes` instead of rounding up - **Where:** `crates/xcl/src/gpu/dxil/kernel.rs:568-578` (`compute_init`) - **Bug:** ```rust let w = lw.c(total / lanes); ``` On RDNA2, `TG_SIZE[5:0]` holds the number of waves in the threadgroup (`ceil(total / lanes)`). For any non-multiple threadgroup size (e.g. `total = 16` or `32` with `wave64`, or `[8, 1, 1]` with `wave32`), `total / lanes` evaluates to `0` instead of `1` (and e.g. `total = 96` in wave64 gives `1` instead of `2`). - **Fix:** `total.div_ceil(lanes)` (compare `lw.waves = t.div_ceil(64).max(1)` at `kernel.rs:486`). ### 2. `Module::uses_double_extensions()` scans dead `self.insts` instead of live `self.blocks` - **Where:** `crates/xcl/src/gpu/dxil/module.rs:1394-1402` - **Bug:** `uses_double_extensions()` iterates `self.insts.iter()` instead of `self.blocks.iter().flatten()` (contrast `insts_use_type()` directly below it at `module.rs:1403-1419`, which iterates `self.blocks.iter().flatten()`). Any dead/DCE'd/unreachable-block f64 `fdiv` or f64↔int cast remains in `self.insts` and leaves `Feature::DoubleExtensions` set in shader flags even though the instruction is not emitted in bitcode (DXIL validator rejects mismatched shader feature flags). - **Fix:** Iterate `self.blocks.iter().flatten().map(|&i| &self.insts[i as usize])`. ### 3. `Op::Alloca` bitcode encoding corrupts `align == 0` into `inalloca` flag (bit 5) - **Where:** `crates/xcl/src/gpu/dxil/module.rs:1227-1232` - **Bug:** ```rust Op::Alloca(t, n, align) => { let i32t = self.value_ty(*n); let a = align.trailing_zeros() as u64 + 1; w.record(FUNC_CODE_INST_ALLOCA, &[t.0 as u64, i32t.0 as u64, abs(*n), a | 1 << 6]) } ``` In LLVM 3.7 bitcode, `Alloca` alignment is encoded in bits `[4:0]` as `0` (default alignment) or `log2(align) + 1` (1..31), while bit `5` (`1 << 5 = 32`) is `inalloca` and bit `6` (`1 << 6`) is `explicit type`. When `align == 0`, `0u32.trailing_zeros() + 1 == 33 == (1 << 5) | 1`, silently setting the `inalloca` flag bit and encoding `align 1`! - **Fix:** `let a = if *align == 0 { 0 } else { align.trailing_zeros() as u64 + 1 };` (and assert power-of-two / `align.is_power_of_two()`). ### 4. `Module::global_bytes_layout()` counts stripped unused TGSM globals in `PSV0` - **Where:** `crates/xcl/src/gpu/dxil/module.rs:1501-1503` - **Bug:** `global_bytes_layout()` sums `self.globals.iter()` unfiltered, whereas the bitcode writer strips unreferenced globals (e.g. TGSM arrays whose uses were eliminated by `Module::optimize()`). For mesh shaders / compute metadata using `global_bytes_layout(3, ...)`, `PSV0` reports non-zero TGSM size for globals absent from the emitted bitcode. - **Fix:** Count only globals referenced by live instructions in `self.blocks` (matching the bitcode writer's liveness filter). ### 5. `Shader::heap_raw_buffer()` omits `self.require(Feature::RawAndStructuredBuffers)` - **Where:** `crates/xcl/src/gpu/dxil/shader.rs:289-293` - **Bug:** `Shader::heap_raw_buffer()` calls `handle_from_heap` + `annotate(RAW_BUFFER)` without calling `self.require(Feature::RawAndStructuredBuffers)` (contrast `Shader::heap_raw_buffer_uav()` directly below at `shader.rs:297-303`, which calls `self.require(Feature::RawAndStructuredBuffers)`). A shader whose only raw buffers come through `heap_raw_buffer()` will miss the required shader flag in DXIL validation. - **Fix:** Add `self.require(Feature::RawAndStructuredBuffers);` in `Shader::heap_raw_buffer()`. ### 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.