| Issue |
238835
|
| Summary |
[mlir][SPIR-V] `convert-spirv-to-llvm` `spirv.BitFieldSExtract` performs a bit-width shift (LLVM poison) when `count = 0`; the SPIR-V spec defines the result as `0`
|
| Labels |
|
| Assignees |
|
| Reporter |
lin334141-cell
|
# [mlir][SPIR-V] `convert-spirv-to-llvm` `spirv.BitFieldSExtract` performs a bit-width shift (LLVM poison) when `count = 0`; the SPIR-V spec defines the result as `0`
## Versions
- Reproduced (**executed end-to-end**, outputs below) on LLVM `24.0.0git` at `67005b724c917e1bdca50c82ecaa1b21a69244ed` (same-source rebuild, Release+assertions, macOS arm64) and on `main` at `61817b48e757f9a07f2a84e36326542052c12c22` (2026-10-29, local build). Both produce identical outputs.
- Source-level check: `BitFieldSExtractPattern` in `mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp` is **byte-identical** between `67005b72` (lines 488–534) and today's `main` (lines 490–542). The pattern is unchanged at the checked `main` snapshot; that snapshot was not built or executed, and no newer revision was checked.
- Upstream dedup (checked 2026-11-04): no existing report found for this pattern. Two sibling issues covering the same conversion pass are being filed separately — the **`BitFieldInsert`** missing-insert-mask defect and the `BitFieldUExtract` `count = W` mask-construction defect; the three share the pass but neither mechanism nor fix (cross-reference numbers to be added).
## Description
`BitFieldSExtractPattern::matchAndRewrite` lowers
```mlir
%r = "spirv.BitFieldSExtract"(%base, %offset, %count)
```
to (type width `W`, with `offset`/`count` broadcast to `W`):
```llvm
%amountToShiftLeft = sub %W, (add %count, %offset) ; W - (offset + count)
%baseShiftedLeft = shl %base, %amountToShiftLeft
%amountToShiftRight = add %offset, %amountToShiftLeft ; = W - count
%r = ashr %baseShiftedLeft, %amountToShiftRight
```
When `count = 0`, `amountToShiftRight` equals `W` — the shift amount is the **bit width** of the type. Per the LLVM Language Reference, a `shl`/`lshr`/`ashr` with a shift amount greater than or equal to the bit width produces **poison**. The SPIR-V specification instead defines this case: `OpBitFieldSExtract` with `Count = 0` returns `0`. (The related `count = 0` case of `OpBitFieldUExtract` is computed by a mask, not a width shift, and is correct; `offset + count = W` with `count > 0` does **not** reach a width shift here — `amountToShiftRight = W - count < W` — and is mathematically correct, so it is not part of this defect.)
Because the result is poison, any concrete value observed at execution is target- and optimization-dependent by definition; the values below are what the two bound builds happened to produce, and are not claimed to be the necessary output on other backends.
## Minimal reproducer (`min.mlir`)
```mlir
module{func.func @sext(%base:i8,%off:i32,%cnt:i32)->i8{
%r="spirv.BitFieldSExtract"(%base,%off,%cnt):(i8,i32,i32)->i8
func.return %r:i8
}
func.func @sext32(%base:i32,%off:i32,%cnt:i32)->i32{
%r="spirv.BitFieldSExtract"(%base,%off,%cnt):(i32,i32,i32)->i32
func.return %r:i32
}}
```
## Commands (verified as shown, clean directory)
```bash
mlir-opt min.mlir --mlir-disable-threading \
'--pass-pipeline=builtin.module(convert-spirv-to-llvm,convert-arith-to-llvm,convert-func-to-llvm,reconcile-unrealized-casts)' \
-o lowered.mlir
mlir-translate --mlir-to-llvmir lowered.mlir -o min.ll
llvm-as driver.ll -o driver.bc
llvm-link min.ll driver.bc -o p.bc
llc -O2 -filetype=obj p.bc -o p.o
clang p.o -o run && ./run
```
Driver (`driver.ll`):
```llvm
target triple = "arm64-apple-macos"
@pf = private unnamed_addr constant [32 x i8] c"sext: i8=%02x i32=%x ref3=%02x\0A\00"
declare i32 @printf(ptr, ...)
declare i8 @sext(i8, i32, i32)
declare i32 @sext32(i32, i32, i32)
define i32 @main() {
entry:
%a = call i8 @sext(i8 -7, i32 1, i32 0)
%az = zext i8 %a to i32
%b = call i32 @sext32(i32 7, i32 1, i32 0)
%c = call i8 @sext(i8 -7, i32 1, i32 3)
%cz = zext i8 %c to i32
call i32 (ptr, ...) @printf(ptr @pf, i32 %az, i32 %b, i32 %cz)
ret i32 0
}
```
`offset = 1, count = 0` is in the SPIR-V-defined domain (`offset <= W`; `count = 0` explicitly defined ⇒ result `0`). The third call is a `count = 3` control that exercises the well-defined path (`bits[1,4)` of `0xF9` = `0b100`, sign-extended to `i8` = `0xFC`).
## Expected behavior (SPIR-V 1.x spec)
`i8 = 00` (count = 0 ⇒ 0), `i32 = 0` (count = 0 ⇒ 0), `ref3 = fc` (well-defined control).
```
sext: i8=00 i32=0 ref3=fc
```
## Actual behavior (executed, identical on both revisions)
```
sext: i8=ff i32=80000000 ref3=fc
```
- The i8 result is `0xff` instead of `0`: at the IR level the i8 `ashr` with shift amount 8 (`W - count = 8`) is poison per the LLVM Language Reference (shift amount >= bit width of `i8`); the backend widened the shift to `i32` where 8 is legal, and the materialized value is the widened formula's garbage (`(base << 7) >> 8` truncated to `i8`) — one instance of that poison, with no cross-backend or cross-optimization-level guarantee.
- The i32 result is `0x80000000` instead of `0`: `ashr i32 %x, 32` is poison per the LLVM Language Reference (shift amount >= bit width); the concrete value above is the observed instance on this target and is not guaranteed across backends, bitcode optimizer runs, or optimization levels.
- The `count = 3` control returns `0xfc`, correct — the defect is specific to `count = 0`.
## Broader evidence (not required for reproduction)
In an 81-frame four-width replay with an independent SPIR-V-spec reference, exactly **10 SExtract output units** were wrong — **all with `count = 0`**, distributed **i8: 3, i16: 1, i32: 3, i64: 3** (these 10 units span 10 distinct frozen frames; the full replay also contains 32 wrong `BitFieldInsert` units from the separate `BitFieldInsert` insert-mask defect filed alongside this report). The `BitFieldUExtract` outputs were correct in all frames.
## Suggested fix direction
Special-case `count = 0` (result `0`, matching the spec) — e.g. emit `select (icmp eq %count, 0), zero, <current formula>` — which prevents the poison from propagating into the result. Note this guards the value; it does not necessarily remove the width-shift instruction from the IR, and we make no claim that it does. (For completeness, the `count = W` mask-construction shift in this pattern family is tracked in sibling issues covering the `BitFieldInsert`/`BitFieldUExtract` conversions; cross-reference numbers to be added.) Happy to test a patch.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs