llvm / llvm/llvm-project

Optimize fractional part is 0 pattern to trunc(x) == x

Open
#188,035 7 comments 0 reactions 1 assignee Claimed by @junyeong0619 View on GitHub
floating-point llvm:instcombine missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://alive2.llvm.org/ce/z/-Ms4-r

```c++
bool src(float x) {
float unused;
return fract(x, &unused) == 0.0f;
}

bool tgt(float x) {
return trunc(x) == x;
}
```

```llvm
define i1 @src(float %x) {
entry:
%i = tail call float @llvm.floor.f32(float %x)
%sub.i.i = fsub nsz float %x, %i
%i1 = tail call nsz float @llvm.minnum.f32(float %sub.i.i, float 0x3FEFFFFFE0000000)
%i2 = fcmp uno float %x, 0.000000e+00
%cond.i.i = select i1 %i2, float %x, float %i1
%i3 = tail call float @llvm.fabs.f32(float %x)
%i4 = fcmp oeq float %i3, 0x7FF0000000000000
%i5 = fcmp oeq float %cond.i.i, 0.000000e+00
%cmp = or i1 %i4, %i5
ret i1 %cmp
}

define i1 @tgt(float %x) {
entry:
%call = tail call float @llvm.trunc.f32(float %x)
%cmp = fcmp oeq float %call, %x
ret i1 %cmp
}

declare float @llvm.trunc.f32(float) #0
declare float @llvm.floor.f32(float) #0
declare float @llvm.minnum.f32(float, float) #0
declare float @llvm.fabs.f32(float) #0

attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
```

Rocm device libs has this pattern appear in several different functions, e.g. [tgamma](https://github.com/ROCm/llvm-project/blob/36e42eaffd2a24eaffb1d32fb80f8bd4dd825ebd/amd/device-libs/ocml/src/tgammaF.cl#L54)

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.