[all-commits] [llvm/llvm-project] 625538: [KnownBits] Fix add() SelfAdd assertion for bitwid...
Paweł Bylica via All-commits
all-commits at lists.llvm.org
Thu Jun 11 05:08:04 PDT 2026
Branch: refs/heads/main
Home: https://github.com/llvm/llvm-project
Commit: 625538f44601b906a3db03b2b110f50915ca3ecd
https://github.com/llvm/llvm-project/commit/625538f44601b906a3db03b2b110f50915ca3ecd
Author: Paweł Bylica <pawel at hepcolgum.band>
Date: 2026-06-11 (Thu, 11 Jun 2026)
Changed paths:
M llvm/lib/Support/KnownBits.cpp
M llvm/unittests/Support/KnownBitsTest.cpp
Log Message:
-----------
[KnownBits] Fix add() SelfAdd assertion for bitwidths >= 512 (#202769)
`KnownBits::add()` with `SelfAdd=true` lowers `X+X` to `shl(X, 1)` using
a fixed 8-bit shift amount:
```cpp
KnownBits Amt = KnownBits::makeConstant(APInt(8, 1));
return KnownBits::shl(LHS, Amt, NUW, NSW, /*ShAmtNonZero=*/true);
```
The comment there claims the shift-amount bitwidth is independent of the
source bitwidth, but that is not true: `shl()`'s `getMaxShiftAmount()`
extracts `Log2_32(BitWidth)` bits from the shift amount's max value when
`BitWidth` is a power of two:
```cpp
static unsigned getMaxShiftAmount(const APInt &MaxValue, unsigned BitWidth) {
if (isPowerOf2_32(BitWidth))
return MaxValue.extractBitsAsZExtValue(Log2_32(BitWidth), 0);
...
}
```
For source widths `>= 512`, `Log2_32(BitWidth) >= 9`, so extracting that
many bits from the 8-bit shift amount trips the assertion in
`APInt::extractBitsAsZExtValue`:
```
Assertion `bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth && "Illegal bit extraction"' failed.
#9 llvm::APInt::extractBitsAsZExtValue(unsigned int, unsigned int) const
#10 llvm::KnownBits::shl(...)
#11 llvm::KnownBits::add(...)
```
(256 is the boundary that still works: `Log2_32(256) == 8`, extracting
exactly 8 bits from the 8-bit amount.)
Fix: make the shift-amount bitwidth match the source bitwidth so the
extraction is always in bounds. The `SelfAdd` path was introduced in
#188078.
Found via fuzzing. Adds a `SelfAddWide` regression test covering widths
256/512/1024.
To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications
More information about the All-commits
mailing list