<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Ryan:</div><div class=""><br class=""></div>I think this will take more, here is the SPIR-V relevant text ( <a href="https://www.khronos.org/registry/spir-v/specs/1.0/SPIRV.pdf" class="">https://www.khronos.org/registry/spir-v/specs/1.0/SPIRV.pdf</a> ): <div class=""><br class=""></div><div class="">
<div class="page" title="Page 53">
<div class="section">
<div class="layoutArea">
<div class="column"><p class=""><span style="font-size: 10pt; font-family: NimbusRomNo9L;" class=""><start></span></p><p class=""><span style="font-size: 10pt; font-family: NimbusRomNo9L;" class="">NoContraction
</span></p><p class=""><span style="font-size: 10.000000pt; font-family: 'NimbusRomNo9L'" class="">Apply to an </span><span style="font-size: 10.000000pt; font-family: 'NimbusRomNo9L'; color: rgb(0.000000%, 0.000000%, 100.000000%)" class="">arithmetic instruction </span><span style="font-size: 10.000000pt; font-family: 'NimbusRomNo9L'" class="">to indicate the
operation cannot be combined with another
instruction to form a single operation. For
example, if applied to an </span><span style="font-size: 10.000000pt; font-family: 'NimbusRomNo9L'; color: rgb(0.000000%, 0.000000%, 100.000000%)" class="">OpFMul</span><span style="font-size: 10.000000pt; font-family: 'NimbusRomNo9L'" class="">, that multiply
can’t be combined with an addition to yield a
fused multiply-add operation. Furthermore, such
operations are not allowed to reassociate; e.g.,
add(a + add(b+c)) cannot be transformed to
add(add(a+b) + c). </span></p><div class=""><end></div><div class=""><br class=""></div><div class="">The problem is that the spec does not mention the reassoc attribute, where an implication is made to tie both together as one control without specifying precisely that. I do not think we need to add another flag. I think we can work this within the definition. The text implies NoContraction is both contract=off and reassoc=off, baring global context. Since we are testing for both in isContractable in some fmul contexts and always in fadd context, it should suffice. This and the global context should be manageable in a SPIR env. The above text does not specify if the add should control fusing or not, but leaves it open to interpretation.</div><div class=""><br class=""></div><div class="">Our current definition of contract is:</div><div class=""><br class=""></div><div class="">``contract``<br class="">Allow floating-point contraction (e.g. fusing a multiply followed by an addition into a fused multiply-and-add).</div><div class=""><br class=""></div><div class="">Is somewhat loose, but then we do have a good deal of internal context no made evident that exists in the source.</div><div class="">Lets put a stronger bound on the problem to solidify the goal. I believe front ends can be SPIR-V compliant and use the current framework. It seems more a management of the environment and IR than adding new feature controls.</div><div class=""><br class=""></div><div class="">Regards,</div><div class="">Michael</div><div class=""><br class=""></div>
</div>
</div>
</div>
</div><div><br class=""><blockquote type="cite" class=""><div class="">On Aug 23, 2018, at 10:51 AM, Ryan Taylor <<a href="mailto:ryta1203@gmail.com" class="">ryta1203@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">-Ryan</div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Thu, Aug 23, 2018 at 1:36 PM Sanjay Patel <<a href="mailto:spatel@rotateright.com" class="">spatel@rotateright.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="">If we have this:</div><div class="">r = (X * Y) + Z</div><div class=""><br class=""></div><div class="">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 class=""></div><div class=""><br class=""></div><div class="">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 class=""></div><div class=""><br class=""></div><div class="">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 class=""></div><div class=""><br class=""></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Aug 22, 2018 at 9:41 AM, Nicolai Hähnle via llvm-dev <span dir="ltr" class=""><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 22.08.2018 13:29, Ryan Taylor wrote:<br class="">
<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 class="">
<br class="">
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 class="">
</blockquote>
<br class=""></span>
Okay, I see that the SPIR-V spec specifically calls out this example.<br class="">
<br class="">
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 class="">
<br class="">
``contract``<br class="">
Allow floating-point contraction (e.g. fusing a multiply followed by<br class="">
an addition into a fused multiply-and-add). This flag must be present<br class="">
on all affected instruction.<br class="">
<br class="">
And we should probably say the same about ``reassoc`` as well.<br class="">
<br class="">
Cheers,<br class="">
Nicolai<br class="">
<br class="">
<br class="">
<br class="">
<br class="">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
<br class="">
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" class="">llvm-dev@lists.llvm.org</a> <mailto:<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>>> wrote:<br class="">
<br class="">
On 21.08.2018 16:08, Ryan Taylor via llvm-dev wrote:<br class="">
> So I have a test case where:<br class="">
><br class="">
> %20 = fmul nnan arcp float %15, %19<br class="">
> %21 = fadd reassoc nnan arcp contract float %20, -1.000000e+00<br class="">
><br class="">
> is being contracted in DAG to fmad. Is this correct since the<br class="">
fmul has<br class="">
> no reassoc or contract fast math flag?<br class="">
<br class="">
By having the reassoc and contract flags on fadd, the frontend is<br class="">
essentially saying "different rounding on the value produced by the<br class="">
fadd<br class="">
is okay".<br class="">
<br class="">
So I'd say contracting this to fma is correct.<br class="">
<br class="">
Where does this code come from, and why do you think contracting to fma<br class="">
is wrong?<br class="">
<br class="">
Cheers,<br class="">
Nicolai<br class="">
<br class="">
<br class="">
<br class="">
<br class="">
><br class="">
> Thanks.<br class="">
><br class="">
> On Mon, Aug 20, 2018 at 12:56 PM Ryan Taylor <<a href="mailto:ryta1203@gmail.com" target="_blank" class="">ryta1203@gmail.com</a><br class="">
<mailto:<a href="mailto:ryta1203@gmail.com" target="_blank" class="">ryta1203@gmail.com</a>><br class=""></span><div class=""><div class="m_7215311322655097858m_-6566450083406253586h5">
> <mailto:<a href="mailto:ryta1203@gmail.com" target="_blank" class="">ryta1203@gmail.com</a> <mailto:<a href="mailto:ryta1203@gmail.com" target="_blank" class="">ryta1203@gmail.com</a>>>> wrote:<br class="">
><br class="">
> I'm curious why the condition to fuse is this:<br class="">
><br class="">
> // Floating-point multiply-add with intermediate rounding.<br class="">
> bool HasFMAD = (LegalOperations &&<br class="">
> TLI.isOperationLegal(ISD::FMAD, VT));<br class="">
><br class="">
> static bool isContractable(SDNode *N) {<br class="">
> SDNodeFlags F = N->getFlags();<br class="">
> return F.hasAllowContract() || F.hasAllowReassociation();<br class="">
> }<br class="">
><br class="">
> bool CanFuse = Options.UnsafeFPMath || isContractable(N);<br class="">
> bool AllowFusionGlobally = (Options.AllowFPOpFusion ==<br class="">
> FPOpFusion::Fast || CanFuse || HasFMAD);<br class="">
> // If the addition is not contractable, do not combine.<br class="">
> if (!AllowFusionGlobally && !isContractable(N))<br class="">
> return SDValue();<br class="">
><br class="">
> Specifically the AllowFusionGlobally, I would have expected<br class="">
> something more like:<br class="">
><br class="">
> bool AllowFusionGlobally = (Options.AllowFPOpFusion ==<br class="">
> FPOpFusion::Fast && CanFuse && HasFMAD);<br class="">
><br class="">
> or at the very least:<br class="">
><br class="">
> bool AllowFusionGlobally = ((Options.AllowFPOpFusion ==<br class="">
> FPOpFusion::Fast || CanFuse) && HasFMAD);<br class="">
><br class="">
> It seems that as long as the target can do fmad it does do fmad<br class="">
> since HasFMAD is true.<br class="">
><br class="">
> Thanks.<br class="">
><br class="">
><br class="">
><br class="">
><br class="">
><br class="">
> _______________________________________________<br class="">
> LLVM Developers mailing list<br class=""></div></div>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a> <mailto:<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>><span class=""><br class="">
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="">
><br class="">
<br class="">
<br class="">
-- Lerne, wie die Welt wirklich ist,<br class="">
Aber vergiss niemals, wie sie sein sollte.<br class="">
_______________________________________________<br class="">
LLVM Developers mailing list<br class=""></span>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a> <mailto:<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>><br class="">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="">
<br class="">
</blockquote><div class="m_7215311322655097858m_-6566450083406253586HOEnZb"><div class="m_7215311322655097858m_-6566450083406253586h5">
<br class="">
<br class="">
-- <br class="">
Lerne, wie die Welt wirklich ist,<br class="">
Aber vergiss niemals, wie sie sein sollte.<br class="">
_______________________________________________<br class="">
LLVM Developers mailing list<br class="">
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a><br class="">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="">
</div></div></blockquote></div><br class=""></div></div>
</blockquote></div>
</div></blockquote></div><br class=""></div></body></html>