[InstCombine] Miscompilation where folding a negation into a division by a constant turns an exact division into a reciprocal multiply
Fuzzer Generated Test
Test Commit e633c7da8ff144a11b3d875956fbaea06b969070
Description
foldFNegIntoConstant rewrites fneg arcp (fdiv X, C) to fdiv arcp X, -C, taking arcp from the fneg alone, so the exact division becomes a multiply by a rounded reciprocal (related: #191653, #228557). Alive2 approximates arcp, so the output below also includes the run with arcp removed from both sides.
Steps to reproduce
- Minimized test case,
input.ll
define float @f(float %x) {
%d = fdiv float %x, -42.0
%r = fneg arcp float %d
ret float %r
}
Output
----------------------------------------
define float @f(float %x) {
#0:
%d = fdiv float %x, -4.2e+01
%r = fneg arcp float %d
ret float %r
}
=>
define float @f(float %x) {
#0:
%r = fmul arcp float %x, 2.3809524e-02
ret float %r
}
ERROR: Couldn't prove the correctness of the transformation
Alive2 approximated the semantics of the programs and therefore we
cannot conclude whether the bug found is valid or not.
Approximations done:
- arcp
Summary:
0 correct transformations
0 incorrect transformations
1 failed-to-prove transformations
0 Alive2 errors
----------------------------------------
define float @f(float %x) {
#0:
%d = fdiv float %x, -4.2e+01
%r = fneg float %d
ret float %r
}
=>
define float @f(float %x) {
#0:
%r = fmul float %x, 2.3809524e-02
ret float %r
}
Transformation doesn't verify!
ERROR: Value mismatch
Example:
float %x = #x000000bd (2.65e-43)
Source:
float %d = #x80000004 (-6e-45)
float %r = #x00000004 (6e-45)
Target:
float %r = #x00000005 (7e-45)
Source value: #x00000004 (6e-45)
Target value: #x00000005 (7e-45)
Summary:
0 correct transformations
1 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errors
CC: @regehr
Alive2 approximates arcp, so here is a reproduction with llubi (built from LLVM e633c7da8ff1). It runs the source and the InstCombine output on x = 0x1.7ap-142 (bits 0x000000bd) and prints both results.
main.ll (run with llubi main.ll):
@fmt = private constant [15 x i8] c"src=%a tgt=%a\0A\00"
define float @src(float %x) {
%d = fdiv float %x, -42.0
%r = fneg arcp float %d
ret float %r
}
define float @tgt(float %x) {
%r = fmul arcp float %x, f0x3CC30C31
ret float %r
}
define i32 @main() {
%x = bitcast i32 189 to float
%s = call float @src(float %x)
%t = call float @tgt(float %x)
%sd = fpext float %s to double
%td = fpext float %t to double
call i32 (ptr, ...) @printf(ptr @fmt, double %sd, double %td)
%ne = fcmp une float %s, %t
%rc = zext i1 %ne to i32
ret i32 %rc
}
declare i32 @printf(ptr, ...)
Output:
src=0x1p-147 tgt=0x1.4p-147
The source returns 0x1p-147 and the target returns 0x1.4p-147.