[llvm-bugs] [Bug 43847] New: narrow truncated FP math with 'reassoc' fast-math-flag FMF

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Oct 30 06:04:34 PDT 2019


https://bugs.llvm.org/show_bug.cgi?id=43847

            Bug ID: 43847
           Summary: narrow truncated FP math with 'reassoc' fast-math-flag
                    FMF
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: spatel+llvm at rotateright.com
                CC: llvm-bugs at lists.llvm.org

Forking this off from the non-FMF example in bug 43841 - it's the same source
code:

float fmaf(float x, float y, float z) {
        return (((double)x * (double)y) + (double)z);
}

But let's compile with a flag that gives us wide license to rearrange
floating-point math:

$ clang -O2 fma2.c  -S -o - -emit-llvm -funsafe-math-optimizations

define float @fmaf(float %x, float %y, float %z) {
  %conv = fpext float %x to double
  %conv1 = fpext float %y to double
  %mul = fmul reassoc nsz arcp double %conv, %conv1
  %conv2 = fpext float %z to double
  %add = fadd reassoc nsz arcp double %mul, %conv2
  %conv3 = fptrunc double %add to float
  ret float %conv3
}

------------------------------------------------------------------------------

We should be able to remove all of the cast ops (fptrunc/fpext) in this IR and
narrow the math ops (fmul/fadd) to float type. Related transforms are already
implemented in InstCombine.

Notes:
1. "-funsafe-math-optimizations" translates to "reassoc nsz arcp" in IR. We
only care about "reassoc" in this example, but we don't seem to have the clang
flag for that wired up.

2. We're moving to a fast-math-flags model where all FP values can carry FMF.
But this example shows that we can not or do not apply the flags to the cast
instructions yet.

3. Ideally, we will implement #2 to allow pattern-matching from the trailing
"fptrunc", but even without that, we could allow this fold: 
fmul reassoc (fpext X), (fpext Y) --> fpext (fmul X, Y)

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20191030/a8913239/attachment.html>


More information about the llvm-bugs mailing list