This is an archive of the discontinued LLVM Phabricator instance.

Convert clamp into fmaxnum/fminnum pairs.
Needs ReviewPublic

Authored by sebpop on Aug 30 2016, 8:49 AM.

Details

Summary

This patch converts clamp at DAG level in DAGCombiner.cpp. For example, the following code:

float clamp(float a)                                                                                                                                                                       
{                                                                                                                                                                                             
  const float b = 255.0;                                                                                                                                                                      
  const float c = 0.0;                                                                                                                                                                        
                                                                                                                                                                                              
  if(b < a)                                                                                                                                                                                   
    return b;                                                                                                                                                                                 
  if(a < c)                                                                                                                                                                                   
    return c;                                                                                                                                                                                 
                                                                                                                                                                                              
  return a;                                                                                                                                                                                   
}

could be compiled:

clamp:                                  // @clamp
// BB#0:                                // %entry
        adrp    x8, .LCPI0_0
        ldr     s1, [x8, :lo12:.LCPI0_0]
        fmov    s2, wzr
        fmaxnm  s0, s0, s2
        fminnm  s0, s0, s1
        ret

I work with @hiraditya on this patch.

Diff Detail

Event Timeline

hxy9243 updated this revision to Diff 69695.Aug 30 2016, 8:49 AM
hxy9243 retitled this revision from to Convert clamp into fmaxnum/fminnum pairs..
hxy9243 updated this object.
hxy9243 added reviewers: sebpop, majnemer, wmi.
hxy9243 set the repository for this revision to rL LLVM.
hxy9243 added subscribers: hiraditya, llvm-commits.

Ping. Are there any updates or thoughts on this patch? Thanks.

arsenm added a subscriber: arsenm.Sep 9 2016, 1:56 PM
arsenm added inline comments.
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
6558–6559

The || should go at the end of the line

6558–6559

Why are you checking the number of operands? You can just check that it's SETCC

6561

Don't need the get node (and actually might be broken)

6565

You don't need the get node

6567

Return on separate line

6572–6575

You don't need this, the DAG is broken if this ever happens

6586–6587

Ditto

6591

unchecked dynast, used cast<>

6875–6876

This should probably only return if combineSelectFP succeeded

llvm/test/CodeGen/AArch64/aarch64-DAGCombine-fminmax.ll
2

Don't need -O=3

2

This needs to be run without unsafe math to make sure it isn't happening without it

3

Comment is wrong

6

This is redundant with the run line and should be removed

54

Missing a testcase where there are the minnum/maxnum intrinsic calls instead of ones formed from unsafe cmp + select

55

Extra string attributes should be removed

57–59

Should be removed

hxy9243 updated this revision to Diff 71044.Sep 12 2016, 12:46 PM
hxy9243 removed rL LLVM as the repository for this revision.

Address comments from @arsenm.

hxy9243 marked 14 inline comments as done.Sep 12 2016, 12:49 PM
hxy9243 added inline comments.
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
6572–6575

These are separate instructions, and could have different semantics. The comparison will be broken if we don't check semantics.

6586–6587

Ditto.

Ping. Are there any updates on this? Thanks very much.

Ping agian. Thanks in advance for the reviews.

sebpop commandeered this revision.May 23 2018, 9:30 AM
sebpop edited reviewers, added: hxy9243; removed: sebpop.
sebpop updated this revision to Diff 148233.May 23 2018, 9:32 AM

Updated patch. I will post perf numbers on some benchmarks with this patch.

In the following experiment a positive number is an increase in performance,
the best score was taken out of 3 runs on firefly aarch64 A-72:

spec2000-164.gzip0.02%
spec2000-175.vpr-0.55%
spec2000-177.mesa-0.18%
spec2000-179.art-0.38%
spec2000-181.mcf-0.88%
spec2000-183.equake-0.06%
spec2000-186.crafty-0.13%
spec2000-188.ammp-1.06%
spec2000-197.parser-0.54%
spec2000-252.eon-0.49%
spec2000-253.perlbmk0.17%
spec2000-254.gap-0.04%
spec2000-255.vortex0.26%
spec2000-256.bzip2-0.50%
spec2000-300.twolf0.38%
Herald added a project: Restricted Project. · View Herald TranscriptOct 3 2020, 7:22 PM

Is this still relevant?

Herald added a project: Restricted Project. · View Herald TranscriptSep 30 2022, 7:03 PM