llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[fuzzer-found] [SLP] Miscompile at -O2 on AArch64: buildvector of a trimmed node reuses lanes of a node demoted to i1

Open ubmaxxer opened this issue 1 week ago • 0 comments

On main, the SLP vectorizer demotes a trunc node to i1, because its vectorized user is and x, 1. Two of its scalars are also in a second trunc node, the operand of a sub, which needs all 16 bits. Tree trimming then turns the second node into a buildvector, and the buildvector takes those two lanes from the demoted node through sext <4 x i1> ... to <4 x i16>. The sub gets 0 or -1 instead of the value. LLVM 23.1.6 and 22.1.8 do not vectorize this function.

Reproducer

godbolt (opt trunk and opt 23.1.0): https://godbolt.org/z/az66q756M

target triple = "aarch64-unknown-linux-gnu"

define void @test(ptr %a, ptr %b) {
entry:
  %l0 = load i32, ptr %a, align 4
  %shl0 = shl i32 %l0, 16
  %ashr0 = ashr exact i32 %shl0, 16
  %m0 = load i32, ptr %a, align 4
  %tm0 = trunc i32 %m0 to i16
  %max0 = call i16 @llvm.umax.i16(i16 %tm0, i16 1)
  %t0 = trunc i32 %l0 to i16
  %sub0 = sub i16 %max0, %t0
  %ext0 = sext i16 %sub0 to i32
  %cmp0 = icmp slt i32 %ashr0, %ext0
  %and0 = and i16 %t0, 1
  %sel0 = select i1 %cmp0, i16 %and0, i16 0
  %a1 = getelementptr inbounds nuw i8, ptr %a, i64 4
  %l1 = load i32, ptr %a1, align 4
  %t1 = trunc i32 %l1 to i16
  %shl1 = shl i32 %l1, 16
  %ashr1 = ashr exact i32 %shl1, 16
  %max1 = call i16 @llvm.umax.i16(i16 %t1, i16 1)
  %m1 = load i32, ptr %a1, align 4
  %tm1 = trunc i32 %m1 to i16
  %sub1 = sub i16 %max1, %tm1
  %ext1 = sext i16 %sub1 to i32
  %cmp1 = icmp slt i32 %ashr1, %ext1
  %and1 = and i16 %t1, 1
  %sel1 = select i1 %cmp1, i16 %and1, i16 0
  %b1 = getelementptr inbounds nuw i8, ptr %b, i64 2
  %a2 = getelementptr inbounds nuw i8, ptr %a, i64 8
  %l2 = load i32, ptr %a2, align 4
  %shl2 = shl i32 %l2, 16
  %ashr2 = ashr exact i32 %shl2, 16
  %t2 = trunc i32 %l2 to i16
  %sub2 = sub i16 %max0, %t2
  %ext2 = sext i16 %sub2 to i32
  %cmp2 = icmp slt i32 %ashr2, %ext2
  %and2 = and i16 %t2, 1
  %sel2 = select i1 %cmp2, i16 %and2, i16 0
  %b2 = getelementptr inbounds nuw i8, ptr %b, i64 4
  %a3 = getelementptr inbounds nuw i8, ptr %a, i64 12
  %l3 = load i32, ptr %a3, align 4
  %t3 = trunc i32 %l3 to i16
  %shl3 = shl i32 %l3, 16
  %ashr3 = ashr exact i32 %shl3, 16
  %max3 = call i16 @llvm.umax.i16(i16 %t3, i16 1)
  %sub3 = sub i16 %max3, %tm0
  %ext3 = sext i16 %sub3 to i32
  %cmp3 = icmp slt i32 %ashr3, %ext3
  %and3 = and i16 %t3, 1
  %sel3 = select i1 %cmp3, i16 %and3, i16 0
  store i16 %sel0, ptr %b, align 2
  store i16 %sel1, ptr %b1, align 2
  store i16 %sel2, ptr %b2, align 2
  store i16 %sel3, ptr %b, align 2
  ret void
}

declare i16 @llvm.umax.i16(i16, i16)

@fmt = private constant [13 x i8] c"%d %d %d %d\0A\00"
declare i32 @printf(ptr, ...)

; a = {3, 3, 3, 3}, b = {0, 0, 0, 0}
define i32 @main() {
  %a = alloca [4 x i32], align 4
  %b = alloca [4 x i16], align 2
  store <4 x i32> <i32 3, i32 3, i32 3, i32 3>, ptr %a, align 4
  store <4 x i16> zeroinitializer, ptr %b, align 2
  call void @test(ptr %a, ptr %b)
  %v = load <4 x i16>, ptr %b, align 2
  %e = zext <4 x i16> %v to <4 x i32>
  %e0 = extractelement <4 x i32> %e, i64 0
  %e1 = extractelement <4 x i32> %e, i64 1
  %e2 = extractelement <4 x i32> %e, i64 2
  %e3 = extractelement <4 x i32> %e, i64 3
  %r = call i32 (ptr, ...) @printf(ptr @fmt, i32 %e0, i32 %e1, i32 %e2, i32 %e3)
  ret i32 0
}
$ opt -S -passes=slp-vectorizer repro.ll -o out.ll
$ llubi repro.ll
0 0 0 0
$ llubi out.ll
0 0 1 0

alive-tv on @test alone (before and after the pass) reports Transformation doesn't verify! ERROR: Mismatch in memory. It needs -smt-to=1200000; with the default timeout it gives up.

What goes wrong

@test in out.ll, abridged:

  %1 = load <4 x i32>, ptr %a, align 4
  ...
  %5 = trunc <4 x i32> %4 to <4 x i16>
  %6 = trunc <4 x i32> %1 to <4 x i1>                ; (1)
  ...
  %9 = call <4 x i16> @llvm.umax.v4i16(<4 x i16> %5, <4 x i16> splat (i16 1))
  %10 = sext <4 x i1> %6 to <4 x i16>                ; (2)
  %11 = shufflevector <4 x i16> %10, <4 x i16> %5, <4 x i32> <i32 0, i32 poison, i32 2, i32 4>   ; (3)
  ...
  %15 = sub <4 x i16> %9, %14                        ; (4)
  %16 = sext <4 x i16> %15 to <4 x i32>
  %17 = icmp slt <4 x i32> %8, %16
  %18 = select <4 x i1> %17, <4 x i1> %6, <4 x i1> zeroinitializer
  1. %6 holds %t0, %t1, %t2, %t3 for the and ..., 1 node. Only bit 0 is used there, so the node is demoted to i1.
  2. The demoted vector is sign-extended back to i16, which gives 0 or -1 per lane.
  3. Lanes 0 and 2 of the subtrahend (%t0 and %t2) are taken from that vector.
  4. The sub needs all 16 bits of %t0 and %t2.

For lane 2 with the value 3: the scalar code computes umax(3, 1) - 3 = 0, 3 < 0 is false, and 0 is stored. The vector code computes 3 - (-1) = 4, 3 < 4 is true, and bit 0 of 3 is stored, which is 1. Lane 0 is wrong in the same way, but its store to b[0] is overwritten by the fourth store.

Where it comes from

From the debug output and the code, as far as I can tell:

  1. computeMinimumValueSizes() runs before calculateTreeCostAndTrimNonProfitable().
  2. %t0 and %t2 are in two trunc nodes: the operand of the and node and the operand of the sub node. The first one is demoted to i1. At that point this is fine, because the sub node has its own trunc node at i16.
  3. Trimming then replaces the second node by a buildvector (SLP: Trimming unprofitable subtree at node 13 with cost 9 and gather cost 8.). MinBWs is not looked at again.
  4. When the buildvector is emitted, %t0 and %t2 are found in the demoted node, and ShuffleInstructionBuilder casts that node's <4 x i1> value to <4 x i16>.

Notes

  • Wrong on main at fa2302dee791 (2026-11-26). opt from the 23.1.6 and 22.1.8 releases leaves @test scalar.
  • With the triple changed to x86_64-unknown-linux-gnu, main vectorizes @test in a different shape and the result is correct.
  • The IR comes from this C loop, compiled with clang -O2 --target=aarch64-linux-gnu. For a = {3, 3, 3, 3} and n = 4 it gives b = {0, 0, 1, 0} instead of {0, 0, 0, 0}:
typedef unsigned short u16;
static inline u16 mx(u16 a, u16 b) { return a > b ? a : b; }

void f(int *a, u16 *b, long n) {
  int *q = a;
  u16 *p = b;
  for (long i = 0; i < n; i += 4, q += 4, p += 4) {
    p[0] = (u16)q[0] & (u16)((short)q[0] < (short)(u16)(mx((u16)a[i], 1) - (u16)q[0]));
    p[1] = (u16)q[1] & (u16)((short)q[1] < (short)(u16)(mx((u16)q[1], 1) - (u16)a[i + 1]));
    p[2] = (u16)q[2] & (u16)((short)q[2] < (short)(u16)(mx(1, (u16)a[i]) - (u16)q[2]));
    b[0] = (u16)q[3] & (u16)((short)q[3] < (short)(u16)(mx((u16)q[3], 1) - (u16)a[i]));
  }
}

This problem was found with a fuzzer; the reproducer above was reduced from its test case. The report was written with LLM assistance.

ubmaxxer avatar Nov 26 '26 12:11 ubmaxxer