| Issue |
240704
|
| Summary |
[X86] Miscompile in `extractelement(bitcast(<X x i1>))` fold uses the result width to index the mask
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
Firebear518
|
**Suggested labels:** `backend:X86`, `miscompilation`
## Summary
The X86 DAG combine in `combineExtractVectorElt` has a special-case fold for extracting an element from a bitcast view of an `i1` vector. It replaces the extraction with an `EXTRACT_SUBVECTOR` from the underlying mask vector.
The fold currently uses `NumEltBits = VT.getScalarSizeInBits()`—the width of the **extracted result**—to determine both the subvector length and its starting index. Those quantities must instead be based on the scalar element width of the input vector, `VecEltBitWidth = SrcVT.getScalarSizeInBits()`.
A SelectionDAG `EXTRACT_VECTOR_ELT` result can be wider than the source vector element. In that case, the result's upper bits are unspecified (equivalent to any-extension semantics). Using the result width to index the underlying `i1` mask therefore selects the wrong range and can also construct an out-of-bounds `EXTRACT_SUBVECTOR`.
- If the incorrectly computed subvector range still fits within the source mask, the fold can silently read the wrong mask bits. This is a miscompile and does not trigger an assertion, including in an assertions-enabled build.
- If the incorrectly computed range extends beyond the source mask, an assertions-enabled build fails with `Extract subvector overflow!`. With assertions disabled, the malformed node can lead to an out-of-bounds operand read; in the reproducer below, the release build crashes and ASan reports a `use-after-poison` read in `SelectionDAG::createOperands`, reached from `DAGCombiner::visitEXTRACT_SUBVECTOR`.
The fold was introduced by commit `4e8f847676fd` (October 2022), so the affected code is present in release branches containing that change, including LLVM 16 and later, unless independently fixed. The issue reproduces on the upstream revision tested for this report, `59074ad1fe32`.
## Reproducer 1: silent wrong code
```llvm
; Compile: llc -mtriple=x86_64-unknown-unknown -mattr=+avx512f < repro1.ll
define <17 x i1> @f(<17 x i1> %a, ptr %p) {
%i = bitcast <17 x i1> %a to i17
%t = trunc i17 %i to i16
%z = zext i16 %t to i32
%v = bitcast i32 %z to <4 x i8>
%e = extractelement <4 x i8> %v, i64 0
store i8 %e, ptr %p
%m = bitcast i16 %t to <16 x i1>
%s = shufflevector <16 x i1> %m, <16 x i1> zeroinitializer, <17 x i32> <i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 1, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16>
ret <17 x i1> %s
}
```
Lane 10 of the returned vector must equal bit 1 of `%a`. The generated code instead reads bit 8. In the generated assembly, the incorrect path reads the byte corresponding to argument lane 8 (`48(%rsp)`) rather than the byte for lane 1 (`%edx`).
Results from running the generated code under Intel SDE (`-future`):
| Input `%a` (i17) | Expected lane 10 | Buggy build |
|---|---:|---:|
| `0x00002` | 1 | **0** |
| `0x00100` | 0 | **1** |
| `0x00102` | 1 | 1 |
## Reproducer 2: out-of-bounds `EXTRACT_SUBVECTOR`
```llvm
; Compile: llc -mtriple=x86_64-unknown-unknown -mattr=+avx512vl < repro2.ll
define <17 x i1> @f(<17 x i1> %a, ptr %p) {
%1 = bitcast <17 x i1> %a to i17
%2 = trunc i17 %1 to i8
%3 = zext i8 %2 to i64
%4 = bitcast i64 %3 to <4 x bfloat>
%5 = extractelement <4 x bfloat> %4, i64 0
%6 = bitcast bfloat %5 to i16
%7 = trunc i16 %6 to i1
store i1 %7, ptr %p, align 1
%8 = trunc i17 %1 to i16
%9 = bitcast i16 %8 to <16 x i1>
%10 = shufflevector <16 x i1> %9, <16 x i1> zeroinitializer, <17 x i32> <i32 3, i32 1, i32 15, i32 0, i32 4, i32 7, i32 14, i32 4, i32 14, i32 6, i32 0, i32 2, i32 15, i32 6, i32 12, i32 13, i32 2>
ret <17 x i1> %10
}
```
Observed behavior:
- **Assertions enabled:** `SelectionDAG::getNode` fails with the assertion `Extract subvector overflow!`.
- **Assertions disabled (release build):** the reproducer crashes with `SIGSEGV` in `SelectionDAG::createOperands`, called from `DAGCombiner::visitEXTRACT_SUBVECTOR`.
- **ASan, assertions disabled:** reports a `use-after-poison` read of size 16 in `SelectionDAG::createOperands`.
## Root cause
In `llvm/lib/Target/X86/X86ISelLowering.cpp`, `combineExtractVectorElt` uses the result width (`NumEltBits`) for the byte-width guard, the `EXTRACT_SUBVECTOR` length, and the starting index:
```cpp
if (InputVector.getOpcode() == ISD::BITCAST && (NumEltBits % 8) == 0) {
...
MVT SubVT = MVT::getVectorVT(MVT::i1, NumEltBits);
SDValue Sub = DAG.getNode(
ISD::EXTRACT_SUBVECTOR, dl, SubVT, Src,
DAG.getVectorIdxConstant(CIdx->getZExtValue() * NumEltBits, dl));
return DAG.getBitcast(VT, Sub);
}
```
Here, `NumEltBits` is `VT.getScalarSizeInBits()`, the width of the extracted result. The input vector's element width is `VecEltBitWidth` (`SrcVT.getScalarSizeInBits()`). The underlying `Src` is an `i1` vector, so extracting one element from the bitcast view requires extracting `VecEltBitWidth` `i1` elements, starting at `C * VecEltBitWidth`—not `NumEltBits` elements starting at `C * NumEltBits`.
The `<17 x i1>` argument/return in these reproducers is legalized into byte-sized values, creating extraction nodes whose result width can differ from the element width of the bitcast view. The same combine also handles `X86ISD::PEXTRB` and `X86ISD::PEXTRW`; those nodes produce an `i32` result even though they extract an 8-bit or 16-bit element.
## Proposed fix
Use `VecEltBitWidth` for the byte-width guard, the `EXTRACT_SUBVECTOR` length, and its starting index. Then bitcast the extracted `i1` subvector back to `SrcVT.getScalarType()` before converting it to `VT`:
- Use any-extension/truncation for `ISD::EXTRACT_VECTOR_ELT`, whose widened result has any-extension semantics.
- Use zero-extension/truncation for `X86ISD::PEXTRB` and `X86ISD::PEXTRW`, whose architectural semantics zero-extend the extracted value to `i32`.
I have a local patch and regression test available and can submit a PR after the issue is discussed.
## Validation
All checks below were run against upstream revision `59074ad1fe32`:
- The new lit regression test (`llvm/test/CodeGen/X86/extractelt-bitcast-vxi1-wide-result.ll`) passes with the patch; both RUN configurations fail or assert with the pre-patch compiler.
- 441 existing X86 CodeGen tests covering `i1` masks and AVX-512 pass with the patch.
- Assembly output is unchanged for 2,366 other patterns in the comparison set.
- All 609 SDE executions produce the expected result with the patch. The pre-patch build produced 17 miscompiles and 74 crashes in the same set.
## How it was found
The bug was found by a structured backend fuzzer (`llvm-isel-fuzzer` with a custom mutator). A differential execution oracle compared the fold-enabled build with the fold-disabled build and a scalar non-AVX-512 build as a third reference. The generated code was executed under Intel SDE.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs