llvm / llvm/llvm-project

Missed Optimization: Failure to fold linear floating-point expressions into a single comparison constant.

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

Description

```llvm
define i1 @src(<2 x float> %arg0) {
%v0 = extractelement <2 x float> %arg0, i64 0
%v1 = fmul float %v0, 2.000000e+00
%v2 = extractelement <2 x float> %arg0, i64 1
%v3 = fmul float %v2, 2.000000e+00
%v4 = fadd float %v1, -1.000000e+00
%v5 = fadd float %v3, -1.000000e+00
%v6 = fcmp oeq float %v4, 0.000000e+00
%v7 = fcmp oeq float %v5, 0.000000e+00
%v8 = select i1 %v6, i1 %v7, i1 false
ret i1 %v8
}

define i1 @tgt(<2 x float> %arg0) {
%v0 = extractelement <2 x float> %arg0, i64 0
%v6 = fcmp oeq float %v0, 5.000000e-01
%v2 = extractelement <2 x float> %arg0, i64 1
%v7 = fcmp oeq float %v2, 5.000000e-01
%v8 = select i1 %v6, i1 %v7, i1 false
ret i1 %v8
}
```
Alive2: available in alive2 with timeout limit = 50000:
```
----------------------------------------
define i1 @src(<2 x float> %arg0) {
#0:
%v0 = extractelement <2 x float> %arg0, i64 0
%v1 = fmul float %v0, 2.000000
%v2 = extractelement <2 x float> %arg0, i64 1
%v3 = fmul float %v2, 2.000000
%v4 = fadd float %v1, -1.000000
%v5 = fadd float %v3, -1.000000
%v6 = fcmp oeq float %v4, 0.000000
%v7 = fcmp oeq float %v5, 0.000000
%v8 = select i1 %v6, i1 %v7, i1 0
ret i1 %v8
}
=>
define i1 @tgt(<2 x float> %arg0) {
#0:
%v0 = extractelement <2 x float> %arg0, i64 0
%v6 = fcmp oeq float %v0, 0.500000
%v2 = extractelement <2 x float> %arg0, i64 1
%v7 = fcmp oeq float %v2, 0.500000
%v8 = select i1 %v6, i1 %v7, i1 0
ret i1 %v8
}
Transformation seems to be correct!
```

Godbolt: https://godbolt.org/z/dK639xcq4
Pattern found in: https://github.com/dtcxzyw/llvm-opt-benchmark/blob/main/bench/pbrt-v4/optimized/bxdfs.ll

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.