| Issue |
244753
|
| Summary |
Wrong code at -O1 and above: `lshr (xor V, splat(vector.reduce.umax A)), 63` on a non-power-of-2 vector (`<9 x i64>`, also 3/5/7 lanes)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
haoxintu
|
## Description
```c
typedef unsigned long long e __attribute__((ext_vector_type(9)));
__attribute__((noinline)) e
f (e a, e v, e s)
{
return (__builtin_reduce_max (a) ^ (v >> (s & 3))) >> 63;
}
int main (void)
{
e a = 0, s = 0, v = 0;
v[8] = -1; /* lane 8: (0 ^ (~0 >> 0)) >> 63 == 1 */
e r = f (a, v, s);
if (r[8] != 1)
__builtin_abort ();
return 0;
}
```
```
$ clang -O1 repro.c && ./a.out # Aborted (also -O2, -O3, -Os); -O0 is fine
```
The IR is correct; the back end miscompiles it. `llc -O2` on this IR gives `o[8] = 0` instead of 1 (driver: `a = 0`, `v[8] = ~0`):
```llvm
define void @f(ptr %pa, ptr %pv, ptr %ps, ptr %po) {
%a = load <9 x i64>, ptr %pa, align 8
%v = load <9 x i64>, ptr %pv, align 8
%m = call i64 @llvm.vector.reduce.umax.v9i64(<9 x i64> %a)
%i = insertelement <9 x i64> poison, i64 %m, i64 0
%sp = shufflevector <9 x i64> %i, <9 x i64> poison, <9 x i32> zeroinitializer
%x = xor <9 x i64> %v, %sp
%r = lshr <9 x i64> %x, splat (i64 63)
store <9 x i64> %r, ptr %po, align 8
ret void
}
```
- It fails with 3, 5, 7 and 9 lanes, and is correct with 6, 8 and 16 lanes.
- Without the final `lshr ..., 63`, the `xor` result is correct.
- `lshr (vector.reduce.umax A), 63` on its own is correct.
Only the sign bit of the `umax` reduction is demanded. That looks like it becomes an "any lane has its sign bit set" test over the widened vector, where the padding lanes are not neutral.
Sanitizers (ASan, UBSan) are clean.
Reproduced in Godbolt (x86-64 clang trunk): https://godbolt.org/z/nhe4adfMv
## Checked Versions
- Wrong at -O1/-O2/-O3/-Os with clang trunk 2012344e5da8 (2026-11-19).
- `llc` from LLVM 21.1.8 compiles the IR correctly (`o[8] = 1`), so this seems a regression.
Thanks.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs