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

[InstCombine] Miscompilation where a select pulled out of a float operation takes the operation's fast-math flags

Open sairam2661 opened this issue 1 month ago • 2 comments

Fuzzer Generated Test

Test Commit e633c7da8ff144a11b3d875956fbaea06b969070

Description SimplifySelectsFeedingBinaryOp puts the binop's nsz on the new select: for equal nonzero %x and %y, the target may return -0.0 where the source returns +0.0.

This appears to be a real issue based on discussion in #180906, #202453.

Steps to reproduce

  • Minimized test case (src), input.ll
define half @f(i1 %c, half %x, half %y) {
  %a = select i1 %c, half %x, half 0xH7C00
  %b = select i1 %c, half %y, half 0xH7C00
  %r = fsub nsz half %a, %b
  ret half %r
}
  • InstCombine output (tgt), output.ll
define half @f(i1 %c, half %x, half %y) {
  %1 = fsub nsz half %x, %y
  %r = select nsz i1 %c, half %1, half +qnan
  ret half %r
}

Output

define half @f(i1 %c, half %x, half %y) {
#0:
  %a = select i1 %c, half %x, half 0x7c00
  %b = select i1 %c, half %y, half 0x7c00
  %r = fsub nsz half %a, %b
  ret half %r
}
=>
define half @f(i1 %c, half %x, half %y) {
#0:
  %#1 = fsub nsz half %x, %y
  %r = select nsz i1 %c, half %#1, half 0x7e00
  ret half %r
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
i1 %c = #x1 (1)
half %x = #x0002 (0.000000119209?)
half %y = #x0002 (0.000000119209?)

Source:
half %a = #x0002 (0.000000119209?)
half %b = #x0002 (0.000000119209?)
half %r = #x0000 (+0.0)

Target:
half %#1 = #x0000 (+0.0)
half %r = #x8000 (-0.0)
Source value: #x0000 (+0.0)
Target value: #x8000 (-0.0)

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

CC: @regehr

sairam2661 avatar Oct 21 '26 22:10 sairam2661

@sairam2661: Output is not LLVM IR.

EugeneZelenko avatar Oct 21 '26 22:10 EugeneZelenko

Oh, my bad. I will keep this in mind, thanks for catching this!

sairam2661 avatar Oct 21 '26 22:10 sairam2661