Skip to content

[InstCombine] Wrong folding of select + fpext #157254

@dtcxzyw

Description

@dtcxzyw

Reproducer: https://alive2.llvm.org/ce/z/JPVWbm

; bin/opt -passes=instcombine test.ll -S
define double @f(float %0) {
entry:
  %a5_3 = fcmp olt float %0, 0.000000e+00
  %1 = fpext float %0 to double
  %2 = select i1 %a5_3, double 0.000000e+00, double %1
  ret double %2
}
----------------------------------------
define double @f(float %#0) {
entry:
  %a5_3 = fcmp olt float %#0, 0.000000
  %#1 = fpext float %#0 to double
  %#2 = select i1 %a5_3, double 0.000000, double %#1
  ret double %#2
}
=>
define double @f(float %#0) {
entry:
  %.inv = fcmp ole float %#0, 0.000000
  %#1 = select i1 %.inv, float 0.000000, float %#0
  %#2 = fpext float %#1 to double
  ret double %#2
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
float %#0 = #x80000000 (-0.0)

Source:
i1 %a5_3 = #x0 (0)
double %#1 = #x8000000000000000 (-0.0)
double %#2 = #x8000000000000000 (-0.0)

Target:
i1 %.inv = #x1 (1)
float %#1 = #x00000000 (+0.0)
double %#2 = #x0000000000000000 (+0.0)
Source value: #x8000000000000000 (-0.0)
Target value: #x0000000000000000 (+0.0)

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

Reported by riscv-tv (riscv-tests/test-160373129.ll). cc @regehr
It is a bit interesting that riscv-tv can also catch miscompilation when optimizing lifted LLVM IR :)

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions