| Issue |
238836
|
| Summary |
[mlir][SPIR-V] `convert-spirv-to-llvm` `spirv.BitFieldUExtract` returns a poison-derived value for `offset = 0, count = W`; the SPIR-V spec result is `base` itself
|
| Labels |
|
| Assignees |
|
| Reporter |
lin334141-cell
|
# [mlir][SPIR-V] `convert-spirv-to-llvm` `spirv.BitFieldUExtract` returns a poison-derived value for `offset = 0, count = W`; the SPIR-V spec result is `base` itself
## 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: `BitFieldUExtractPattern` in `mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp` at the checked `main` snapshot still emits the same `shl -1, %count` mask construction; 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 are being filed separately — the `BitFieldInsert` missing-insert-mask defect and the `BitFieldSExtract` count=0 width shift; the three share the conversion pass but each has its own mechanism, and this one has its own lowering site (the UExtract mask construction; cross-reference numbers to be added).
## Description
`BitFieldUExtractPattern::matchAndRewrite` lowers
```mlir
%r = "spirv.BitFieldUExtract"(%base, %offset, %count)
```
to:
```llvm
%mask = xor (shl iW -1, %count), -1 ; (1 << count) - 1
%shifted = lshr %base, %offset
%r = and %shifted, %mask
```
With `offset = 0, count = W` the first shift amount is the **bit width** of the type. Per the LLVM Language Reference, a `shl` with a shift amount greater than or equal to the bit width produces **poison**; the mask, and therefore the whole result, is poison-derived. The SPIR-V specification defines this input: bits `[0, W)` of `base` zero-extended to width `W` — i.e. the result is `base` itself. (First-error location: the `maskShiftedByCount` emission in `BitFieldUExtractPattern::matchAndRewrite`; main lines ~543–560.)
Because the result is poison-derived, any concrete value observed at execution is target- and optimization-dependent by definition; the value below is what the two bound builds happened to produce and is not claimed to be the necessary output on other backends.
## Minimal reproducer (`min.mlir`)
```mlir
module{func.func @uext(%base:i32,%off:i32,%cnt:i32)->i32{
%r="spirv.BitFieldUExtract"(%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 [22 x i8] c"uext: got=%x want=%x\0A\00"
@want = private unnamed_addr constant [1 x i32] [i32 -1431655766]
declare i32 @printf(ptr, ...)
declare i32 @uext(i32, i32, i32)
define i32 @main() {
entry:
%r = call i32 @uext(i32 -1431655766, i32 0, i32 32)
%w = load i32, ptr @want
call i32 (ptr, ...) @printf(ptr @pf, i32 %r, i32 %w)
ret i32 0
}
```
`base = 0xAAAAAAAA` (arbitrary fully-defined bit pattern), `offset = 0`, `count = 32 = W` — inside the SPIR-V-defined domain (`offset + count <= W`).
## Expected behavior (SPIR-V 1.x spec)
`OpBitFieldUExtract` extracts bits `[0, 32)` of `base`, zero-extended to width 32 — i.e. the result is `base` itself: `0xaaaaaaaa`.
```
uext: got=aaaaaaaa want=aaaaaaaa
```
## Actual behavior (executed, identical on both revisions)
```
uext: got=0 want=aaaaaaaa
```
The poison in the mask was materialized as `0` on this target (the observed concrete value is not guaranteed across backends, bitcode optimizer runs, or optimization levels); the spec-mandated result is `0xaaaaaaaa`.
## Relation to the sibling drafts
- `BitFieldInsert` (missing insert mask): different mechanism, different fix site; the `count = W` mask-construction shift noted there is this same defect seen from the insert pattern.
- `BitFieldSExtract` (count = 0 width shift): different mechanism (its `ashr` amount is `W - count`, not a mask construction).
- The same full-width mask construction (`shl iW -1, %count` with `%count = W`) also affects the `spirv.BitFieldInsert` conversion path in this pass (its mask emission uses the identical construction; see the insert issue for that path's own missing-insert-mask defect), so a comprehensive fix should guard both bit-width shift sites together. All three live in `mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp`.
## Suggested fix direction
Guard the mask construction so no shift amount reaches the bit width — e.g. `select (icmp eq %count, 0), 0, (1 << count) - 1` composed with a `count = W` special case (`mask = all-ones`), or clamp the shift amount and fix up the two spec-defined boundary results. The sketch is untested beyond the witnesses shown. Happy to test a patch.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs