[PR #2121] rdna2spirv: mask v_mbcnt_lo/hi shift count to < 32 to avoid SPIR-V OpShiftLeftLogical UB (SHADER-MBCNT-SHIFT-IN-RANGE) #2126

Open
opened 2026-11-26 21:20:08 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2121
Author: @ShotTechOp
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/mbcnt-shift-in-range


📝 Commits (1)

  • 4d683b6 rdna2spirv: mask v_mbcnt_lo/hi shift count to < 32 to avoid SPIR-V UB

📊 Changes

2 files changed (+136 additions, -2 deletions)

View changed files

➕ tests/semantic/test_mbcnt_shift_defined.py (+131 -0)
📝 tools/rdna2spirv.py (+5 -2)

📄 Description

Claimed in #1608 as SHADER-MBCNT-SHIFT-IN-RANGE.

What and why

  • In tools/rdna2spirv.py (v_mbcnt_lo_u32_b32, v_mbcnt_hi_u32_b32), the lowering emitted:
    %inrange = OpULessThan %bool %rel %c32
    %one     = OpShiftLeftLogical %uint %c1 %rel
    %m1      = OpISub %uint %one %c1
    %big     = OpSelect %uint %inrange %m1 ...
    
  • Under SPIR-V §3.42.10 (OpShiftLeftLogical), the result is undefined whenever Shift >= 32, even when the shifted value sits on the unselected side of OpSelect (the same SPIR-V rule tools/rdna2spirv.py already guards against in v_perm_b32, v_alignbit_b32, v_alignbyte_b32, fmt_dec_component, and buffer_load_format).
    • For v_mbcnt_lo_u32_b32 in wave64, lanes 32..63 had rel = 32..63 >= 32.
    • For v_mbcnt_hi_u32_b32 in both wave32 and wave64, lanes 0..31 had rel = lane - 32 = 0xFFFFFFE0..0xFFFFFFFF >= 32.
    • On spirv_wave_interp, this produced _Poison("OpShiftLeftLogical by ... >= 32") in 32 lanes of every wave.
  • Masking rel with 31 (OpBitwiseAnd %uint %rel %c31) before OpShiftLeftLogical is an identity whenever inrange is true (0 <= rel <= 31) and keeps every OpShiftLeftLogical shift operand strictly < 32 on all lanes.

Testing

  • AI-assisted: yes.
  • Environment: Windows 11, Python 3.12 (no GPU / LLVM / SPIRV-Tools installed locally).
  • New regression suite: tests/semantic/test_mbcnt_shift_defined.py — executes translated v_mbcnt_lo_u32_b32 and v_mbcnt_hi_u32_b32 in wave32 and wave64 (plain scalar masks and lane-mask vcc, plus paired wave64 thread-rank) on spirv_wave_interp._Wave and asserts both exact per-lane outputs and zero _Poison values across all lanes.
  • RED control on origin/main: tests/semantic/test_mbcnt_shift_defined.py fails on origin/main with FAILED (failures=4) (_Poison('OpShiftLeftLogical by 32 >= 32') / _Poison('OpShiftLeftLogical by 4294967264 >= 32') across 32 lanes per wave).
  • GREEN checks on branch:
    • python -I tests/semantic/test_mbcnt_shift_defined.py -v (3/3 OK)
    • python -m unittest discover -s tests/semantic -p test_mbcnt_lo_hi.py (28/28 OK)
    • python -I tests/test_text_write_line_endings.py (3/3 OK)
    • python -m compileall -q tools/rdna2spirv.py tests/semantic/test_mbcnt_shift_defined.py
    • python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_mbcnt_shift_defined.py
    • git diff --check

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2121 **Author:** [@ShotTechOp](https://github.com/ShotTechOp) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/mbcnt-shift-in-range` --- ### 📝 Commits (1) - [`4d683b6`](https://github.com/ubiquitarianism/leonida/commit/4d683b684506d8dea0bb4410ff8c9037f3404b3a) rdna2spirv: mask v_mbcnt_lo/hi shift count to < 32 to avoid SPIR-V UB ### 📊 Changes **2 files changed** (+136 additions, -2 deletions) <details> <summary>View changed files</summary> ➕ `tests/semantic/test_mbcnt_shift_defined.py` (+131 -0) 📝 `tools/rdna2spirv.py` (+5 -2) </details> ### 📄 Description Claimed in #1608 as `SHADER-MBCNT-SHIFT-IN-RANGE`. ## What and why - In `tools/rdna2spirv.py` (`v_mbcnt_lo_u32_b32`, `v_mbcnt_hi_u32_b32`), the lowering emitted: ```spirv %inrange = OpULessThan %bool %rel %c32 %one = OpShiftLeftLogical %uint %c1 %rel %m1 = OpISub %uint %one %c1 %big = OpSelect %uint %inrange %m1 ... ``` - Under SPIR-V §3.42.10 (`OpShiftLeftLogical`), the result is undefined whenever `Shift >= 32`, even when the shifted value sits on the unselected side of `OpSelect` (the same SPIR-V rule `tools/rdna2spirv.py` already guards against in `v_perm_b32`, `v_alignbit_b32`, `v_alignbyte_b32`, `fmt_dec_component`, and `buffer_load_format`). - For `v_mbcnt_lo_u32_b32` in wave64, lanes `32..63` had `rel = 32..63 >= 32`. - For `v_mbcnt_hi_u32_b32` in both wave32 and wave64, lanes `0..31` had `rel = lane - 32 = 0xFFFFFFE0..0xFFFFFFFF >= 32`. - On `spirv_wave_interp`, this produced `_Poison("OpShiftLeftLogical by ... >= 32")` in 32 lanes of every wave. - Masking `rel` with `31` (`OpBitwiseAnd %uint %rel %c31`) before `OpShiftLeftLogical` is an identity whenever `inrange` is true (`0 <= rel <= 31`) and keeps every `OpShiftLeftLogical` shift operand strictly `< 32` on all lanes. ## Testing - **AI-assisted**: yes. - **Environment**: Windows 11, Python 3.12 (no GPU / LLVM / SPIRV-Tools installed locally). - **New regression suite**: `tests/semantic/test_mbcnt_shift_defined.py` — executes translated `v_mbcnt_lo_u32_b32` and `v_mbcnt_hi_u32_b32` in wave32 and wave64 (plain scalar masks and lane-mask `vcc`, plus paired wave64 thread-rank) on `spirv_wave_interp._Wave` and asserts both exact per-lane outputs and zero `_Poison` values across all lanes. - **RED control on `origin/main`**: `tests/semantic/test_mbcnt_shift_defined.py` fails on `origin/main` with `FAILED (failures=4)` (`_Poison('OpShiftLeftLogical by 32 >= 32')` / `_Poison('OpShiftLeftLogical by 4294967264 >= 32')` across 32 lanes per wave). - **GREEN checks on branch**: - `python -I tests/semantic/test_mbcnt_shift_defined.py -v` (3/3 OK) - `python -m unittest discover -s tests/semantic -p test_mbcnt_lo_hi.py` (28/28 OK) - `python -I tests/test_text_write_line_endings.py` (3/3 OK) - `python -m compileall -q tools/rdna2spirv.py tests/semantic/test_mbcnt_shift_defined.py` - `python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_mbcnt_shift_defined.py` - `git diff --check` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2126
No description provided.