Issue 238713
Summary [SystemZ] lowerSIGN_EXTEND_VECTOR_INREG folds a shuffle whose sign-extended lanes come from the second operand into UNPACKs of the first operand: `__builtin_convertvector(__builtin_shufflevector(a, b, 22, 23, ...), long16)` extends a[14], a[15]
Labels backend:SystemZ, miscompilation, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```c
// clang --target=s390x-linux-gnu -march=z13 -O1 repro.c && ./a.out     (z13 and later; -O0 is correct)
// Expected: fffffffffffffff9 fffffffffffffff8 (b[6], b[7]). Actual at -O1/-O2/-O3: e f (a[14], a[15]).
#include <stdio.h>
typedef signed char v16i8 __attribute__((vector_size(16)));
typedef long v16i64 __attribute__((vector_size(128)));

__attribute__((noinline)) void f(v16i8 a, v16i8 b, v16i64 *o) {
  v16i8 s = __builtin_shufflevector(a, b, 22, 23, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
  *o = __builtin_convertvector(s, v16i64);   // lanes 0, 1 are sext(b[6]), sext(b[7])
}

int main(void) {
  v16i8 a, b;
  v16i64 o;
  for (int i = 0; i < 16; i++) { a[i] = i; b[i] = -1 - i; }
  f(a, b, &o);
  printf("%llx\n%llx\n", (unsigned long long)o[0], (unsigned long long)o[1]);
  return !(o[0] == -7 && o[1] == -8);
}
```

Resulted in this output:
```
$ clang --target=s390x-linux-gnu -march=z13 -O0 repro.c && qemu-s390x ./a.out
fffffffffffffff9
fffffffffffffff8
$ clang --target=s390x-linux-gnu -march=z13 -O1 repro.c && qemu-s390x ./a.out      (same at -O2/-O3, z14/z16/z17)
e
f
```
For lanes 0 and 1 the code unpacks `a` (`vuplb %v0, %v24`), not `b`. repro-main.ll is an IR version, `<16 x i8>` shuffle → `sext <4 x i8> to <4 x i32>`, offset 20. That one also miscompiles at -O0 and prints sext(a[12..15]) instead of sext(b[4..7]).

Trunk: https://godbolt.org/z/1TzK3roxc (clang trunk `--target=s390x-linux-gnu -march=z13 -O2`: `vuplb %v0, %v24` reads `a`)

Root cause: `SystemZTargetLowering::lowerSIGN_EXTEND_VECTOR_INREG` (llvm/lib/Target/SystemZ/SystemZISelLowering.cpp) folds a VECTOR_SHUFFLE operand into the start offset of its UNPACK_HIGH/UNPACK_LOW chain when the extended lanes are consecutive mask elements (`ShuffleMask[Elt] % OutNumElts == Elt`). The candidate offset is a mask value, so it indexes the concatenation of both shuffle operands. It can be ≥ the first operand's lane count, here 22 - 0 = 22 or 20. The code still takes `PackedOp.getOperand(0)`. The UNPACK loop then subtracts half-vector sizes (22 → 14 → 6 → 2 for i8 → i64) and drops what is left. The result is lanes of the first operand at an unrelated position.

Suggested fix: Pick the operand the offset points into.
```diff
     if (StartOffsetCandidate != -1) {
-      StartOffset = StartOffsetCandidate;
-      PackedOp = PackedOp.getOperand(0);
+      // The mask indexes the concatenation of both shuffle operands: an offset
+      // past the first operand's lanes selects lanes of the second.
+      int InNumElts = InVT.getVectorNumElements();
+      unsigned OpNo = StartOffsetCandidate >= InNumElts ? 1 : 0;
+      StartOffset = StartOffsetCandidate - OpNo * InNumElts;
+      PackedOp = PackedOp.getOperand(OpNo);
     }
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to