llvm / llvm/llvm-project

[InstCombine] Incorrect clamp pattern optimization with partial `nnan ninf` flags

Open
#190,913 1 comment 0 reactions 1 assignee Claimed by @Nathiyaa-Sengodan View on GitHub
llvm:instcombine miscompilation
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

InstCombine incorrectly transforms a clamp pattern when fast-math flags produce more poison, which was propagated to the `ret float %r`.

for code:
```llvm
define float @clamp_float_unordered_nonstrict_maxmin2(float %x) {
%cmp2 = fcmp nnan ninf ult float %x, 2.550000e+02
%min = select i1 %cmp2, float %x, float 2.550000e+02
%cmp1 = fcmp ule float %x, 1.000000e+00
%r = select nnan ninf i1 %cmp1, float 1.000000e+00, float %min
ret float %r
}
```

It was optimized by `opt` with `-O3` as follows:
```llvm
define float @clamp_float_unordered_nonstrict_maxmin2(float %x) local_unnamed_addr #0 {
%cmp2.inv = fcmp nnan ninf oge float %x, 2.550000e+02
%min = select nnan ninf i1 %cmp2.inv, float 2.550000e+02, float %x
%.inv = fcmp oge float %min, 1.000000e+00
%r1 = select i1 %.inv, float %min, float 1.000000e+00
ret float %r1
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) }
```

For the input `%x = SNaN`, it may propagate poison farther then befor.

Alive: https://alive2.llvm.org/ce/z/SEFSUe

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.