Issue 244527
Summary [fuzzer-found][DAGCombiner] zext/aext of `and(bitcast(extract_subvector(vNi1, 0)), C)` treats the bitcast as a truncate, which is wrong on big-endian
Labels backend:MIPS, miscompilation, llvm:SelectionDAG, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

IR-only reproducer: InstCombine rewrites the pattern as `extractelement(bitcast <32 x i1> to <2 x i16>), 0`, and none of 108 C variants reached it. `@f` and `@ref` compute the same value; `@ref` hides the AND behind a `freeze` so the fold does not apply. `main` prints `k*1000000 + f*1000 + ref` for a single true lane k = 0..31; f and ref must agree.

```llvm
; llc -O2 -mtriple=mips repro.ll   (also -O0; plain MIPS32 BE, or +msa).  Executed on the unicorn MIPS BE emulator:
;   emurun.py llc mips "" repro.ll O2   -> each line is k*1000000 + f*1000 + ref
; f = zext(and(bitcast(extract_subvector(icmp <32 x i8>, 0) to i16), 255)); ref = same with the AND hidden behind freeze.
; Expected (BE, LangRef): lane k in 8..15 sets bit 15-k, so f(k=8) = 128.  Got: f = 0 for k = 8..15, and f = 128 >> (k-24) for k = 24..31.
@fmt = private constant [4 x i8] c"%d\0A\00"
declare i32 @printf(ptr, ...)
define i32 @f(ptr %pa, ptr %pb) noinline {
  %a = load <32 x i8>, ptr %pa
  %b = load <32 x i8>, ptr %pb
  %c = icmp eq <32 x i8> %a, %b
  %s = shufflevector <32 x i1> %c, <32 x i1> poison, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
  %bc = bitcast <16 x i1> %s to i16
  %an = and i16 %bc, 255
  %z = zext i16 %an to i32
  ret i32 %z
}
define i32 @ref(ptr %pa, ptr %pb) noinline {
  %a = load <32 x i8>, ptr %pa
  %b = load <32 x i8>, ptr %pb
  %c = icmp eq <32 x i8> %a, %b
  %s = shufflevector <32 x i1> %c, <32 x i1> poison, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
  %bc = bitcast <16 x i1> %s to i16
  %fr = freeze i16 %bc
  %an = and i16 %fr, 255
  %z = zext i16 %an to i32
  ret i32 %z
}
define i32 @main() {
  %pa = alloca [32 x i8], align 32
  %pb = alloca [32 x i8], align 32
  br label %loop
loop:
  %k = phi i32 [0, %0], [%k1, %next]
  br label %fill
fill:
  %i = phi i32 [0, %loop], [%i1, %fill]
  %ia = getelementptr i8, ptr %pa, i32 %i
  %ib = getelementptr i8, ptr %pb, i32 %i
  store i8 0, ptr %ia
  %eq = icmp eq i32 %i, %k
  %v = select i1 %eq, i8 0, i8 1
  store i8 %v, ptr %ib
  %i1 = add i32 %i, 1
  %d = icmp eq i32 %i1, 32
  br i1 %d, label %call, label %fill
call:
  %r = call i32 @f(ptr %pa, ptr %pb)
  %r2 = call i32 @ref(ptr %pa, ptr %pb)
  %ne = icmp ne i32 %r, %r2
  br label %pr
pr:
  %m1 = mul i32 %k, 1000000
  %m2 = mul i32 %r, 1000
  %m3 = add i32 %m1, %m2
  %m4 = add i32 %m3, %r2
  call i32 (ptr, ...) @printf(ptr @fmt, i32 %m4)
  br label %next
next:
  %k1 = add i32 %k, 1
  %dd = icmp eq i32 %k1, 32
  br i1 %dd, label %end, label %loop
end:
  ret i32 0
}
```

Resulted in this output (`llc -O2 -mtriple=mips`, run on a MIPS32 big-endian emulator; -O0 is the same):
```
...
8000128      <- k=8:  f = 0,   ref = 128
...
15000001     <- k=15: f = 0,   ref = 1
...
24128000     <- k=24: f = 128, ref = 0   (lane 24 is not even in the extracted <16 x i1>)
...
31001000     <- k=31: f = 1,   ref = 0
```

| true lane k | expected f (= ref) | actual f |
|---|---|---|
| 0..7, 16..31 | 0 | 0 for 0..7; **128 >> (k-24)** for 24..31 |
| 8..15 | 128 >> (k-8) | **0** |

The same wrong code is produced for aarch64_be and s390x (the AGT check fires on all three).

Trunk: https://godbolt.org/z/Ks7EYcnoe

Root cause: `visitZERO_EXTEND` and `visitANY_EXTEND` (DAGCombiner.cpp) fold `(zext/aext (and (bitcast (extract_subvector X:vNi1, 0) to iK), C))` into `(and (anyext (bitcast X to iN)), C)`. This treats `bitcast(extract_subvector(X, 0))` as `trunc(bitcast X)`. That is only true on little-endian. On big-endian, lane 0 of a vNi1→iN bitcast is the most significant bit, so the low K bits of `bitcast X` are the last K lanes of X, not the first K. There is no endianness check.

Suggested fix: only take the bitcast/extract_subvector branch when `DAG.getDataLayout().isLittleEndian()`, in both `visitZERO_EXTEND` and `visitANY_EXTEND`. On big-endian the equivalent would be a shift right by N-K. Validated locally: with the guard, f == ref for all 32 lanes in the emulator, the AGT check no longer fires on mips/aarch64_be/s390x, and the big-endian tests that bitcast `i1` vectors give identical output.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to