<html><body>
<p><font size="2" face="sans-serif">Hi Sanjay,</font><br>
<br>
<font size="2" face="sans-serif">You are right. I tried XL and gcc 4.8.2 for PPC and I also got multiply-and-add operations.</font><br>
<br>
<font size="2" face="sans-serif">I supported my statement on what I read in the gcc man page. -ffast-math is used in clang to set fp-contract to fast (default is standard) and in gcc it activates (among others) the flag -funsafe-math-optimizations whose description includes:</font><br>
<br>
<font size="2" face="Arial">"Allow optimizations for floating-point arithmetic that (a) assume that arguments and results are valid and (b) may violate IEEE or ANSI standards."</font><br>
<br>
<font size="2" face="sans-serif">I am not a floating point expert, for the applications I care usually more precision is better, and that is what muladd provides. Given Tim's explanation, I thought that muladd would conflict with (b) and some user would expect the exact roundings for the mul and add. However, I find this statement in Section 5 of IEEE floating point standard: </font><br>
<br>
<font size="2" face="TimesNewRomanPSMT">"Each of the computational operations that return a numeric result specified by this standard shall be performed as if it first produced an intermediate result correct to infinite precision and with unbounded range, and then rounded that intermediate result,</font><font size="2" face="sans-serif"> ..."</font><br>
<br>
<font size="2" face="sans-serif">which perfectly fits what the muladd instructions in PPC and also in avx2 are doing: using infinite precision after the multiply.</font><br>
<br>
<font size="2" face="sans-serif">It may be possible there is something in the C/C++ standards I am not aware, that makes the fusing illegal. As you said, another reason may be just implementation choice. But in that case I believe we would be doing a bad choice as I suspect there are much more users looking for faster execution that taking advantage of a particular rounding property. </font><br>
<br>
<font size="2" face="sans-serif">Maybe there is someone who can shed some light on this?</font><br>
<br>
<font size="2" face="sans-serif">Thanks,</font><br>
<font size="2" face="sans-serif">Samuel </font><br>
<br>
<tt><font size="2">Sanjay Patel <spatel@rotateright.com> wrote on 08/06/2014 02:30:17 PM:<br>
<br>
> From: Sanjay Patel <spatel@rotateright.com></font></tt><br>
<tt><font size="2">> To: Samuel F Antao/Watson/IBM@IBMUS</font></tt><br>
<tt><font size="2">> Cc: Tim Northover <t.p.northover@gmail.com>, Olivier H Sallenave/<br>
> Watson/IBM@IBMUS, "llvmdev@cs.uiuc.edu" <llvmdev@cs.uiuc.edu></font></tt><br>
<tt><font size="2">> Date: 08/06/2014 02:30 PM</font></tt><br>
<tt><font size="2">> Subject: Re: [LLVMdev] FPOpFusion = Fast and Multiply-and-add combines</font></tt><br>
<tt><font size="2">> <br>
> Hi Samuel,<br>
</font></tt><br>
<tt><font size="2">> I don't think clang follows what gcc does regarding FMA - at least <br>
> by default. I don't have a PPC compiler to test with, but for x86-64<br>
> using clang trunk and gcc 4.9:<br>
> <br>
> $ cat fma.c <br>
> float foo(float x, float y, float z) { return x * y + z; }<br>
> <br>
> $ ./clang -march=core-avx2 -O2 -S fma.c -o - | grep ss<br>
> vmulss %xmm1, %xmm0, %xmm0<br>
> vaddss %xmm2, %xmm0, %xmm0<br>
> <br>
> $ ./gcc -march=core-avx2 -O2 -S fma.c -o - | grep ss<br>
> vfmadd132ss %xmm1, %xmm2, %xmm0<br>
> <br>
> ----------------------------------------------------------------------<br>
</font></tt><br>
<tt><font size="2">> This was brought up in Dec 2013 on this list:<br>
> <a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2013-December/068868.html">http://lists.cs.uiuc.edu/pipermail/llvmdev/2013-December/068868.html</a></font></tt><br>
<tt><font size="2">> <br>
> I don't see an answer as to whether this is a bug for all the other <br>
> compilers, a deficiency in clang's default settings, or just an <br>
> implementation choice.</font></tt><br>
<tt><font size="2">> <br>
> Sanjay</font></tt><br>
<tt><font size="2">> <br>
> On Thu, Jul 31, 2014 at 9:50 AM, Samuel F Antao <sfantao@us.ibm.com> wrote:</font></tt><br>
<tt><font size="2">> Hi Tim,<br>
> <br>
> Thanks for the thorough explanation. It makes perfect sense.<br>
> <br>
> I was not aware fast-math is supposed to prevent more precision <br>
> being used than what is in the standard.<br>
> <br>
> I came across this issue while looking into the output or different <br>
> compilers. XL and Microsoft compiler seem<br>
> to have that turned on by default. But I assume that clang follows <br>
> what gcc does, and have that turned off.<br>
> <br>
> Thanks again,<br>
> Samuel<br>
> <br>
> Tim Northover <t.p.northover@gmail.com> wrote on 07/31/2014 09:54:55 AM:<br>
> <br>
> > From: Tim Northover <t.p.northover@gmail.com><br>
> > To: Samuel F Antao/Watson/IBM@IBMUS<br>
> > Cc: "llvmdev@cs.uiuc.edu" <llvmdev@cs.uiuc.edu>, Olivier H <br>
> > Sallenave/Watson/IBM@IBMUS<br>
> > Date: 07/31/2014 09:55 AM<br>
> > Subject: Re: [LLVMdev] FPOpFusion = Fast and Multiply-and-add combines</font></tt><br>
<tt><font size="2">> <br>
> > <br>
> > Hi Samuel,<br>
> > <br>
> > On 30 July 2014 22:37, Samuel F Antao <sfantao@us.ibm.com> wrote:<br>
> > > In the DAGCombiner, during the combination of mul and add/subtract into<br>
> > > multiply-and-add/subtract, this option is expected to be Fast in order to<br>
> > > enable the combine. This means, that by default no multiply-and-<br>
> add opcodes<br>
> > > are going to be generated. If I understand it correctly, this is<br>
> undesirable<br>
> > > given that multiply-and-add for targets like PPC (I am not sure about all<br>
> > > the other targets) does not pose any rounding problem and it can even be<br>
> > > more accurate than performing the two operations separately.<br>
> > <br>
> > That extra precision is actually what we're being very careful to<br>
> > avoid unless specifically told we're allowed. It can be just as<br>
> > harmful to carefully written floating-point code as dropping precision<br>
> > would be.<br>
> > <br>
> > > Also, in TargetOptions.h I read:<br>
> > ><br>
> > > Standard, // Only allow fusion of 'blessed' ops (currently just fmuladd)<br>
> > ><br>
> > > which made me suspect that the check against Fast in the <br>
> DAGCombiner is not<br>
> > > correct.<br>
> > <br>
> > I think it's OK. In the IR there are 3 different ways to express mul + add:<br>
> > <br>
> > 1. fmul + fadd. This must not be fused into a single step without<br>
> > intermediate rounding (unless we're in Fast mode).<br>
> > 2. call @llvm.fmuladd. This *may* be fused or not, depending on<br>
> > profitability (unless we're in Strict mode, in which case it's<br>
> > separate).<br>
> > 3. call @llvm.fma. This must not be split into two operations (unless<br>
> > we're in Fast mode).<br>
> > <br>
> > That middle one is there because C actually allows you to allow &<br>
> > disallow contraction within a limited region with "#pragma STDC<br>
> > FP_CONTRACT ON". So we need a way to represent the idea that it's not<br>
> > usually OK to fuse them (i.e. not Fast mode), but this particular one<br>
> > actually is OK.<br>
> > <br>
> > Cheers.<br>
> > <br>
> > Tim.<br>
> > </font></tt><br>
<tt><font size="2">> <br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> LLVMdev@cs.uiuc.edu <a href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</font></tt></body></html>