Issue 242864
Summary [SLPVectorizer] Incorrect nsw preservation with demoted operand
Labels miscompilation, llvm:SLPVectorizer, generated by fuzzer
Assignees
Reporter nikic
    ```llvm
; RUN: opt -S -passes=slp-vectorizer < %s
define <2 x i8> @src(<2 x i16> %a, <2 x i32> %y) {
  %a0 = extractelement <2 x i16> %a, i64 0
  %a1 = extractelement <2 x i16> %a, i64 1
  %y0 = extractelement <2 x i32> %y, i64 0
  %y1 = extractelement <2 x i32> %y, i64 1
  %sa0 = sext i16 %a0 to i32
  %sa1 = sext i16 %a1 to i32
  %t0 = trunc i32 %sa0 to i8
  %t1 = trunc i32 %sa1 to i8
  %u0 = add nsw i32 %sa0, %y0
  %u1 = add nsw i32 %sa1, %y1
  %v0 = trunc i32 %u0 to i8
  %v1 = trunc i32 %u1 to i8
  %s0 = add i8 %t0, %v0
 %s1 = add i8 %t1, %v1
  %r0 = insertelement <2 x i8> poison, i8 %s0, i64 0
 %r1 = insertelement <2 x i8> %r0, i8 %s1, i64 1
  ret <2 x i8> %r1
}

define <2 x i8> @tgt(<2 x i16> %a, <2 x i32> %y) {
  %1 = trunc <2 x i16> %a to <2 x i8>
  %2 = sext <2 x i8> %1 to <2 x i32>
  %3 = add nsw <2 x i32> %2, %y
  %4 = trunc <2 x i32> %3 to <2 x i8>
  %5 = add <2 x i8> %1, %4
  ret <2 x i8> %5
}
```

The original IR had the add nsw on sext of i16. The vectorized IR has add nsw on sext of trunc to i8.

AI analysis:

> An operand node X is demoted (MinBWs) by the analysis of one tree root using only the
demanded bits of X's scalars (X is signed, so `IsPotentiallyTruncated` accepts a
demanded-bits width even though X's value does not fit). A second vectorized user U of the
same scalars belongs to another analysis root that fails, so U is emitted in the original
type. U then consumes `sext(narrow X)`, whose upper bits differ from X's, and keeps the
scalar `nsw` flag that was only justified for the real X. The vector `add nsw` overflows
for inputs where the scalar code does not, producing poison.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to