<div dir="ltr"><div>Maybe there is a cleaner solution but it seems like adding a 'nocontract' flag is close to the intention of spir-v and is an easy check in the DAGCombiner without breaking anything else and its intentions are very clear.</div><div><br></div><div>Right now the DAGCombiner logic doesn't seem to be able to handle the case of having fast math globally with instruction level flags to turn off fast math. Right now, either fast math is global and it's assumed everything can be contracted or fast math is not global and we contract if the contract flag is present on the current instruction. I could be missing something.</div><div><br></div><div>-Ryan</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Aug 23, 2018 at 1:36 PM Sanjay Patel <<a href="mailto:spatel@rotateright.com">spatel@rotateright.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>If we have this:</div><div>r = (X * Y) + Z</div><div><br></div><div>And we want that to become an fma op/node, 'contract' is checked on the fadd because it is the fadd that's being altered to form the (possibly non-standard) result. As I think was noted earlier, whether 'contract' is set on the fmul is irrelevant in our current implementation. This allows the scenario where a strict fmul was inlined into code with looser FP semantics, and we're still free to create an fma. If the end value allows non-standard behavior, then we assume that intermediate ops leading up to that end value can use non-standard behavior too. (cc'ing Michael Berg who did a lot of the DAG FMF work recently)<br></div><div><br></div><div>I'm not familiar with SPIR-V, but it sounds like it has an inverse flag system to what we have in IR and DAG - ops are presumed contract-able unless specified with 'no-contract'? Not sure how to resolve that.<br></div><div><br></div><div>If we want to change the LLVM FMF semantics, then there will be breakage in the IR optimizer too (at least for 'reassoc'; not sure about 'contract'). Either way, I agree that we should try to clarify the LangRef about this because you can't tell how things are supposed to work from the current description.<br></div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Aug 22, 2018 at 9:41 AM, Nicolai Hähnle via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On 22.08.2018 13:29, Ryan Taylor wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The example starts as SPIR-V with the NoContraction decoration flag on the fmul.<br>
<br>
I think what you are saying seems valid in that if the user had put the flag on the fadd instead of the fmul it would not contract and so in this example the user needs to put the NoContraction on the fadd though I'm not sure that's a good expectation of the user. On the surface, I think that if an operation didn't have the contract flag than it wouldn't be contracted, regardless of what flags any other operation has.<br>
</blockquote>
<br></span>
Okay, I see that the SPIR-V spec specifically calls out this example.<br>
<br>
Unless there are conflicting requirements with another frontend, I'd say we should make sure LLVM is aligned with SPIR-V here. Something along the lines of (in LangRef):<br>
<br>
``contract``<br>
   Allow floating-point contraction (e.g. fusing a multiply followed by<br>
   an addition into a fused multiply-and-add). This flag must be present<br>
   on all affected instruction.<br>
<br>
And we should probably say the same about ``reassoc`` as well.<br>
<br>
Cheers,<br>
Nicolai<br>
<br>
<br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>
<br>
On Wed, Aug 22, 2018 at 3:55 AM Nicolai Hähnle via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a> <mailto:<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>> wrote:<br>
<br>
    On 21.08.2018 16:08, Ryan Taylor via llvm-dev wrote:<br>
     > So I have a test case where:<br>
     ><br>
     > %20 = fmul nnan arcp float %15, %19<br>
     > %21 = fadd reassoc nnan arcp contract float %20, -1.000000e+00<br>
     ><br>
     > is being contracted in DAG to fmad. Is this correct since the<br>
    fmul has<br>
     > no reassoc or contract fast math flag?<br>
<br>
    By having the reassoc and contract flags on fadd, the frontend is<br>
    essentially saying "different rounding on the value produced by the<br>
    fadd<br>
    is okay".<br>
<br>
    So I'd say contracting this to fma is correct.<br>
<br>
    Where does this code come from, and why do you think contracting to fma<br>
    is wrong?<br>
<br>
    Cheers,<br>
    Nicolai<br>
<br>
<br>
<br>
<br>
     ><br>
     > Thanks.<br>
     ><br>
     > On Mon, Aug 20, 2018 at 12:56 PM Ryan Taylor <<a href="mailto:ryta1203@gmail.com" target="_blank">ryta1203@gmail.com</a><br>
    <mailto:<a href="mailto:ryta1203@gmail.com" target="_blank">ryta1203@gmail.com</a>><br></span><div><div class="m_7215311322655097858m_-6566450083406253586h5">
     > <mailto:<a href="mailto:ryta1203@gmail.com" target="_blank">ryta1203@gmail.com</a> <mailto:<a href="mailto:ryta1203@gmail.com" target="_blank">ryta1203@gmail.com</a>>>> wrote:<br>
     ><br>
     >     I'm curious why the condition to fuse is this:<br>
     ><br>
     >     // Floating-point multiply-add with intermediate rounding.<br>
     >        bool HasFMAD = (LegalOperations &&<br>
     >     TLI.isOperationLegal(ISD::FMAD, VT));<br>
     ><br>
     >     static bool isContractable(SDNode *N) {<br>
     >        SDNodeFlags F = N->getFlags();<br>
     >        return F.hasAllowContract() || F.hasAllowReassociation();<br>
     >     }<br>
     ><br>
     >     bool CanFuse = Options.UnsafeFPMath || isContractable(N);<br>
     >     bool AllowFusionGlobally = (Options.AllowFPOpFusion ==<br>
     >     FPOpFusion::Fast || CanFuse || HasFMAD);<br>
     >     // If the addition is not contractable, do not combine.<br>
     >     if (!AllowFusionGlobally && !isContractable(N))<br>
     >          return SDValue();<br>
     ><br>
     >     Specifically the AllowFusionGlobally, I would have expected<br>
     >     something more like:<br>
     ><br>
     >     bool AllowFusionGlobally = (Options.AllowFPOpFusion ==<br>
     >     FPOpFusion::Fast && CanFuse && HasFMAD);<br>
     ><br>
     >     or at the very least:<br>
     ><br>
     >     bool AllowFusionGlobally = ((Options.AllowFPOpFusion ==<br>
     >     FPOpFusion::Fast || CanFuse) && HasFMAD);<br>
     ><br>
     >     It seems that as long as the target can do fmad it does do fmad<br>
     >     since HasFMAD is true.<br>
     ><br>
     >     Thanks.<br>
     ><br>
     ><br>
     ><br>
     ><br>
     ><br>
     > _______________________________________________<br>
     > LLVM Developers mailing list<br></div></div>
     > <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a> <mailto:<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><span><br>
     > <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
     ><br>
<br>
<br>
    --     Lerne, wie die Welt wirklich ist,<br>
    Aber vergiss niemals, wie sie sein sollte.<br>
    _______________________________________________<br>
    LLVM Developers mailing list<br></span>
    <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a> <mailto:<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
    <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br>
</blockquote><div class="m_7215311322655097858m_-6566450083406253586HOEnZb"><div class="m_7215311322655097858m_-6566450083406253586h5">
<br>
<br>
-- <br>
Lerne, wie die Welt wirklich ist,<br>
Aber vergiss niemals, wie sie sein sollte.<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</div></div></blockquote></div><br></div></div>
</blockquote></div>