<html><body>
<p><font size="2" face="sans-serif">Hi Hal,</font><br>
<br>
<font size="2" face="sans-serif">Thanks for your feedback. Attached is the patch with the corrections and the regression test.</font><br>
<br>
<font size="2" face="sans-serif">Regarding the recursive call, I believe it is fine since it's guarded by some conditions which ensure it can only take an FNEG node as parameter — therefore the depth of the recursion cannot be greater than 1. If you still don't like it, we can replace the recursive call with an iteration over the uses of the FNEG node.</font><br>
<br>
<font size="2" face="sans-serif">More importantly, I found out that the following tests now fail:</font><br>
<font size="2" face="sans-serif">CodeGen/AArch64/fp-dp3.ll</font><br>
<font size="2" face="sans-serif">CodeGen/AArch64/neon-fma.ll</font><br>
<br>
<font size="2" face="sans-serif">The failing tests are basically the same, and check explicitly that FMUL isn't combined with two distinct FADD (see below). I am wondering why ARM doesn't want to combine here. It seems that FMUL and FMADD take 2 cycles, whereas FADD takes 1 cycle. Could that be the reason? If so, maybe we could check the latency of the instructions we combine?</font><br>
<br>
<font size="2" face="sans-serif">; Another set of tests that check for multiply single use</font><br>
<br>
<font size="2" face="sans-serif">define float @test_fmadd_unfused_su(float %a, float %b, float %c) {</font><br>
<font size="2" face="sans-serif">; CHECK-LABEL: test_fmadd_unfused_su:</font><br>
<font size="2" face="sans-serif">  %prod = fmul float %b, %c</font><br>
<font size="2" face="sans-serif">  %sum = fadd float %a, %prod</font><br>
<font size="2" face="sans-serif">  %res = fadd float %sum, %prod</font><br>
<font size="2" face="sans-serif">; CHECK-NOT: fmadd {{s[0-9]+}}, {{s[0-9]+}}, {{s[0-9]+}}, {{s[0-9]+}}</font><br>
<font size="2" face="sans-serif">; CHECK: fmul {{s[0-9]+}}, {{s[0-9]+}}, {{s[0-9]+}}</font><br>
<font size="2" face="sans-serif">; CHECK: fadd {{s[0-9]+}}, {{s[0-9]+}}, {{s[0-9]+}}</font><br>
<font size="2" face="sans-serif">; CHECK: fadd {{s[0-9]+}}, {{s[0-9]+}}, {{s[0-9]+}}</font><br>
<font size="2" face="sans-serif">  ret float %res</font><br>
<font size="2" face="sans-serif">}</font><br>
<br>
<font size="2" face="sans-serif">Thanks very much for your help,</font><br>
<font size="2" face="sans-serif">Olivier</font><br>
<br>
<i>(See attached file: fold-fma.diff)</i><br>
<br>
<img width="16" height="16" src="cid:2__=0ABBF7DADFE29CF08f9e8a93df938@us.ibm.com" border="0" alt="Inactive hide details for Hal Finkel ---09/03/2014 03:29:08 PM---Hi Olivier, Thanks for working on this. A few comments:"><font size="2" color="#424282" face="sans-serif">Hal Finkel ---09/03/2014 03:29:08 PM---Hi Olivier, Thanks for working on this. A few comments:</font><br>
<br>
<font size="1" color="#5F5F5F" face="sans-serif">From:      </font><font size="1" face="sans-serif">Hal Finkel <hfinkel@anl.gov></font><br>
<font size="1" color="#5F5F5F" face="sans-serif">To:        </font><font size="1" face="sans-serif">Olivier H Sallenave/Watson/IBM@IBMUS</font><br>
<font size="1" color="#5F5F5F" face="sans-serif">Cc:        </font><font size="1" face="sans-serif"><llvm-commits@cs.uiuc.edu></font><br>
<font size="1" color="#5F5F5F" face="sans-serif">Date:      </font><font size="1" face="sans-serif">09/03/2014 03:29 PM</font><br>
<font size="1" color="#5F5F5F" face="sans-serif">Subject:   </font><font size="1" face="sans-serif">Re: Improve FMA folding</font><br>
<hr width="100%" size="2" align="left" noshade style="color:#8091A5; "><br>
<br>
<br>
<tt><font size="2">Hi Olivier,<br>
<br>
Thanks for working on this. A few comments:<br>
<br>
+// operation. This is used to ensure that combining into FMA will actually<br>
<br>
into FMA -> into an FMA<br>
<br>
+  if (N->getOpcode() != ISD::FMUL &&<br>
+      N->getOpcode() != ISD::FNEG)<br>
+    return false;<br>
<br>
These conditions guard every call to the function, so this can be an assert; something like this:<br>
<br>
  assert((N->getOpcode() == ISD::FMUL || N->getOpcode() == ISD::FNEG) &&<br>
         "Expecting fmul or fneg node");<br>
<br>
+        else if (!isAlwaysFoldedIntoFMA(*UI))<br>
+          return false;<br>
<br>
Recursion here seems overly-general. Is that the best way to do it? Should you have a depth limit?<br>
<br>
And finally, you'll need an LLVM IR-level CodeGen regression test. Look in test/CodeGen/PowerPC or test/CodeGen/X86 (it should be something like test/CodeGen/PowerPC/fma.ll).<br>
<br>
Thanks,<br>
Hal<br>
<br>
----- Original Message -----<br>
> From: "Olivier H Sallenave" <ohsallen@us.ibm.com><br>
> To: llvm-commits@cs.uiuc.edu<br>
> Sent: Wednesday, September 3, 2014 11:21:53 AM<br>
> Subject: Improve FMA folding<br>
> <br>
> <br>
> <br>
> <br>
> Hi,<br>
> <br>
> Here is a patch to improve FMA folding in DAGCombiner.cpp.<br>
> <br>
> Previously, when looking at a FMUL and a FADD, it would combine them<br>
> into FMA if the FMUL is only used by the FADD. This was to ensure<br>
> that the FMUL will be removed, meaning that the number of<br>
> instructions will actually be reduced. However, if the FMUL has many<br>
> uses but can be combined with each one of them (i.e. they are either<br>
> FADD or FSUB), it should still be combined. Attached is a patch<br>
> which checks all uses of the FMUL.<br>
> <br>
> Olivier<br>
> <br>
> <br>
> (See attached file: fma.diff) (See attached file: test-fma.c)<br>
> <br>
> ----- Forwarded by Olivier H Sallenave/Watson/IBM on 09/03/2014 12:08<br>
> PM -----<br>
> <br>
> From: Hal Finkel <hfinkel@anl.gov><br>
> To: Kevin K O'Brien/Watson/IBM@IBMUS<br>
> Cc: Samuel F Antao/Watson/IBM@IBMUS, <llvmdev@cs.uiuc.edu>, Olivier H<br>
> Sallenave/Watson/IBM@IBMUS<br>
> Date: 08/26/2014 11:33 AM<br>
> Subject: Re: [LLVMdev] Multiply-add combining<br>
> <br>
> <br>
> <br>
> <br>
> Kevin, Olivier,<br>
> <br>
> The target independent heuristic could certainly check all uses,<br>
> patches welcome. We could also consider each case separately, as<br>
> Kevin suggests, but that might not be optimal on targets with only<br>
> one floating-point pipeline, so we'd need to make it opt-in. You<br>
> should also look at the MachineCombiner Pass (added in r214832,<br>
> currently only used by the ARM backend I think) the tries to solve<br>
> this problem in a more-sophisticated way.<br>
> <br>
> -Hal<br>
> <br>
> ----- Original Message -----<br>
> > From: "Kevin K O'Brien" <caomhin@us.ibm.com><br>
> > To: "Olivier H Sallenave" <ohsallen@us.ibm.com><br>
> > Cc: "Samuel F Antao" <sfantao@us.ibm.com>, llvmdev@cs.uiuc.edu<br>
> > Sent: Tuesday, August 26, 2014 10:16:23 AM<br>
> > Subject: Re: [LLVMdev] Multiply-add combining<br>
> > <br>
> > <br>
> > <br>
> > <br>
> > Hi Olivier,<br>
> > I think we discussed this last Thursday? My feeling is that each<br>
> > use<br>
> > of the multiply can be considered separately. If it can be<br>
> > combined,<br>
> > then we should do so. The multiply should be left in place and<br>
> > removed by a dead code elimination pass sometime later. This is<br>
> > what<br>
> > TOBEY does. If you want me to explain the XL method in more detail,<br>
> > come talk to me.<br>
> > <br>
> > Kevin<br>
> > ----------------------------------------------<br>
> > Kevin O'Brien<br>
> > Manager, Advanced Compiler Technology<br>
> > IBM T.J Watson Research Center, Yorktown Heights, NY<br>
> > <br>
> > Inactive hide details for Olivier H Sallenave---08/26/2014 11:12:04<br>
> > AM---Hi, I tried to compile the following using -ffp-contraOlivier<br>
> > H<br>
> > Sallenave---08/26/2014 11:12:04 AM---Hi, I tried to compile the<br>
> > following using -ffp-contract=fast:<br>
> > <br>
> > <br>
> > <br>
> > <br>
> > From:<br>
> > Olivier H Sallenave/Watson/IBM<br>
> > <br>
> > <br>
> > <br>
> > To:<br>
> > llvmdev@cs.uiuc.edu,<br>
> > <br>
> > <br>
> > <br>
> > Cc:<br>
> > Samuel F Antao/Watson/IBM@IBMUS, Kevin K O'Brien/Watson/IBM@IBMUS<br>
> > <br>
> > <br>
> > <br>
> > Date:<br>
> > 08/26/2014 11:12 AM<br>
> > <br>
> > <br>
> > <br>
> > Subject:<br>
> > Multiply-add combining<br>
> > <br>
> > <br>
> > Hi,<br>
> > <br>
> > I tried to compile the following using -ffp-contract=fast:<br>
> > <br>
> > %mul = fmul double %sub5, %x<br>
> > %add = fadd double %add6, %mul<br>
> > %sub = fsub double %sub5, %mul<br>
> > <br>
> > I expected fadd and fsub to be contracted with fmul, which didn't<br>
> > happen.<br>
> > <br>
> > When looking in DAGCombiner.cpp, it appears the result of the fmul<br>
> > needs to be used only once, which isn't the case here as it is used<br>
> > by both the fadd and the fsub:<br>
> > <br>
> > // fold (fadd (fmul x, y), z) -> (fma x, y, z)<br>
> > if (N0.getOpcode() == ISD::FMUL && N0.hasOneUse())<br>
> > return DAG.getNode(ISD::FMA, SDLoc(N), VT, N0.getOperand(0),<br>
> > N0.getOperand(1), N1);<br>
> > <br>
> > This heuristic looks a little conservative, could we instead check<br>
> > that every instruction using the result of the fmul are combinable<br>
> > (i.e., they are either fadd or fsub)?<br>
> > <br>
> > <br>
> > Thanks in advance,<br>
> > Olivier<br>
> > <br>
> > <br>
> > _______________________________________________<br>
> > LLVM Developers mailing list<br>
> > LLVMdev@cs.uiuc.edu </font></tt><tt><font size="2"><a href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a></font></tt><tt><font size="2"><br>
> > </font></tt><tt><font size="2"><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a></font></tt><tt><font size="2"><br>
> > <br>
> <br>
> --<br>
> Hal Finkel<br>
> Assistant Computational Scientist<br>
> Leadership Computing Facility<br>
> Argonne National Laboratory<br>
> <br>
> <br>
> _______________________________________________<br>
> llvm-commits mailing list<br>
> llvm-commits@cs.uiuc.edu<br>
> </font></tt><tt><font size="2"><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a></font></tt><tt><font size="2"><br>
> <br>
<br>
-- <br>
Hal Finkel<br>
Assistant Computational Scientist<br>
Leadership Computing Facility<br>
Argonne National Laboratory<br>
<br>
</font></tt><br>
</body></html>