<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: arial,helvetica,sans-serif; font-size: 10pt; color: #000000'><br><hr id="zwchr"><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><b>From: </b>"Warren Ristow" <warren.ristow@sony.com><br><b>To: </b>"Sanjay Patel" <spatel@rotateright.com>, "cfe-dev" <cfe-dev@lists.llvm.org>, "llvm-dev" <llvm-dev@lists.llvm.org><br><b>Cc: </b>"Nicolai Hähnle" <nhaehnle@gmail.com>, "Hal Finkel" <hfinkel@anl.gov>, "Mehdi Amini" <mehdi.amini@apple.com>, "andrew kaylor" <andrew.kaylor@intel.com><br><b>Sent: </b>Thursday, November 17, 2016 5:58:58 PM<br><b>Subject: </b>RE: what does -ffp-contract=fast allow?<br><br>


<style><!--

@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}

p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
span.gmail-transaction-comment
        {mso-style-name:gmail-transaction-comment;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>

<div class="WordSection1">
<p class="MsoNormal">> Is this a bug? We transformed the original expression into:<br>
> x * y + x<br>
<br>
<span style="font-size: 11pt; font-family: "Calibri",sans-serif; color: rgb(31, 73, 125);"></span></p>
<p class="MsoNormal"><span style="font-size: 11pt; font-family: "Calibri",sans-serif; color: rgb(31, 73, 125);">I’d say yes, it’s a bug.</span></p>
<p class="MsoNormal"><span style="font-size: 11pt; font-family: "Calibri",sans-serif; color: rgb(31, 73, 125);"> </span></p>
<p class="MsoNormal"><span id="DWT2241" style="font-size: 11pt; font-family: "Calibri",sans-serif; color: rgb(31, 73, 125);">Unless ‑ffast‑math is used (or some appropriate subset that gives us leeway, like ‑fno‑honor‑infinities or ‑fno‑honor‑nans, or somesuch), the re-association isn’t
 allowed, and that blocks the madd contraction.</span></p></div></blockquote>I agree. FP contraction alone only allows us to do x*y+z -> fma(x,y,z).<br><br> -Hal<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><div class="WordSection1"><p class="MsoNormal"><span style="font-size: 11pt; font-family: "Calibri",sans-serif; color: rgb(31, 73, 125);"></span></p>
<p class="MsoNormal"><span style="font-size: 11pt; font-family: "Calibri",sans-serif; color: rgb(31, 73, 125);"> </span></p>
<p class="MsoNormal"><span style="font-size: 11pt; font-family: "Calibri",sans-serif; color: rgb(31, 73, 125);">-Warren</span></p>
<p class="MsoNormal"><span style="font-size: 11pt; font-family: "Calibri",sans-serif; color: rgb(31, 73, 125);"> </span></p>
<p class="MsoNormal"><b><span style="font-size: 11pt; font-family: "Calibri",sans-serif;">From:</span></b><span style="font-size: 11pt; font-family: "Calibri",sans-serif;"> Sanjay Patel [mailto:spatel@rotateright.com]
<br>
<b>Sent:</b> Thursday, November 17, 2016 3:22 PM<br>
<b>To:</b> cfe-dev <cfe-dev@lists.llvm.org>; llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Cc:</b> Nicolai Hähnle <nhaehnle@gmail.com>; Hal Finkel <hfinkel@anl.gov>; Mehdi Amini <mehdi.amini@apple.com>; Ristow, Warren <warren.ristow@sony.com>; andrew.kaylor@intel.com<br>
<b>Subject:</b> what does -ffp-contract=fast allow?</span></p>
<p class="MsoNormal"> </p>
<div>
<p class="MsoNormal" style="margin-bottom: 12pt;">This is just paraphrasing from D26602, so credit to Nicolai for first raising the issue there.<br>
<br>
float foo(float x, float y) {<br>
  return x * (y + 1);<br>
}<br>
<br>
$ ./clang -O2 xy1.c -S -o - -target aarch64  -ffp-contract=fast | grep fm<br>
    fmadd    s0, s1, s0, s0<br>
<br>
Is this a bug? We transformed the original expression into:<br>
x * y + x<br>
<br>
When x=INF and y=0, the code returns INF if we don't reassociate. With reassociation to FMA, it returns NAN because 0 * INF = NAN.<br>
<br>
1. I used aarch64 as the example target, but this is not target-dependent (as long as the target has FMA).<br>
<br>
2. This is *not* -ffast-math...or is it? The C standard only shows on/off settings for the associated FP_CONTRACT pragma.<br>
<br>
3. AFAIK, clang has no documentation for -ffp-contract:<br>
<a href="http://clang.llvm.org/docs/UsersManual.html" target="_blank">http://clang.llvm.org/docs/UsersManual.html</a><br>
<br>
4. GCC says:<br>
<a href="https://gcc.gnu.org/onlinedocs/gcc-6.2.0/gcc/Optimize-Options.html#Optimize-Options" target="_blank">https://gcc.gnu.org/onlinedocs/gcc-6.2.0/gcc/Optimize-Options.html#Optimize-Options</a><br>
"-ffp-contract=fast enables floating-point expression contraction such as forming of fused multiply-add operations if the target has native support for them."<br>
<br>
5. The LLVM backend (where this reassociation currently happens) shows:<br>
FPOpFusion::Fast - Enable fusion of FP ops wherever it's profitable.</p>
</div>
</div>
</blockquote><br><br><br>-- <br><div><span name="x"></span>Hal Finkel<br>Lead, Compiler Technology and Programming Languages<br>Leadership Computing Facility<br>Argonne National Laboratory<span name="x"></span><br></div></div></body></html>