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

[InstCombine] foldBitCeil miscompiles vector `not` with poison lanes

Open sairam2661 opened this issue 1 month ago • 1 comments

Fuzzer Generated Test Reproducer https://alive2.llvm.org/ce/z/f7b5hL

Test Commit dff64a2bf54d0abfc3c2232abb04ec28c40087ed

Description Since xor x, -1 is ~x, the function is the bit_ceil(x) idiom and InstCombine (foldBitCeil) removes the guarding select, but it also accepts xor x, <poison, -1> as ~x, so in lane 0 it returns 1 << (-ctlz(poison) & 7) = poison where the source's select returned 1

Steps to reproduce

  • Minimized test case, input.ll
define <2 x i8> @f(<2 x i8> %x) {
  %dec = add <2 x i8> %x, splat (i8 -1)
  %ult = icmp ult <2 x i8> %dec, splat (i8 -2)
  %not = xor <2 x i8> %x, <i8 poison, i8 -1>
  %ctlz = call <2 x i8> @llvm.ctlz.v2i8(<2 x i8> %not, i1 false)
  %sub = sub <2 x i8> splat (i8 8), %ctlz
  %shl = shl <2 x i8> splat (i8 1), %sub
  %r = select <2 x i1> %ult, <2 x i8> %shl, <2 x i8> splat (i8 1)
  ret <2 x i8> %r
}

declare <2 x i8> @llvm.ctlz.v2i8(<2 x i8>, i1 immarg)

Output

define <2 x i8> @f(<2 x i8> %x) {
#0:
  %dec = add <2 x i8> %x, { 255, 255 }
  %ult = icmp ult <2 x i8> %dec, { 254, 254 }
  %not = xor <2 x i8> %x, { poison, 255 }
  %ctlz = ctlz <2 x i8> %not, 0
  %sub = sub <2 x i8> { 8, 8 }, %ctlz
  %shl = shl <2 x i8> { 1, 1 }, %sub
  %r = select <2 x i1> %ult, <2 x i8> %shl, <2 x i8> { 1, 1 }
  ret <2 x i8> %r
}
=>
define <2 x i8> @f(<2 x i8> %x) {
#0:
  %not = xor <2 x i8> %x, { poison, 255 }
  %ctlz = ctlz <2 x i8> %not, 0
  %#range_0_%ctlz = !range <2 x i8> %ctlz, i8 0, i8 9
  %#1 = sub nsw <2 x i8> { 0, 0 }, %#range_0_%ctlz
  %#2 = and <2 x i8> %#1, { 7, 7 }
  %r = shl nuw <2 x i8> { 1, 1 }, %#2
  ret <2 x i8> %r
}
Transformation doesn't verify!

ERROR: Target is more poisonous than source

Example:
<2 x i8> %x = < #x00 (0), #x00 (0) >

Source:
<2 x i8> %dec = < #xff (255, -1), #xff (255, -1) >
<2 x i1> %ult = < #x0 (0), #x0 (0) >
<2 x i8> %not = < poison, #xff (255, -1) >
<2 x i8> %ctlz = < poison, #x00 (0) >
<2 x i8> %sub = < poison, #x08 (8) >
<2 x i8> %shl = < poison, poison >
<2 x i8> %r = < #x01 (1), #x01 (1) >

Target:
<2 x i8> %not = < poison, #xff (255, -1) >
<2 x i8> %ctlz = < poison, #x00 (0) >
<2 x i8> %#range_0_%ctlz = < poison, #x00 (0) >
<2 x i8> %#1 = < poison, #x00 (0) >
<2 x i8> %#2 = < poison, #x00 (0) >
<2 x i8> %r = < poison, #x01 (1) >
Source value: < #x01 (1), #x01 (1) >
Target value: < poison, #x01 (1) >

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors

CC: @regehr

sairam2661 avatar Oct 14 '26 18:10 sairam2661

Can I work on this?

sairam2661 avatar Oct 26 '26 18:10 sairam2661