<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 1, 2018 at 2:08 AM, Nuno Lopes <span dir="ltr"><<a href="mailto:nunoplopes@sapo.pt" target="_blank">nunoplopes@sapo.pt</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">We can do "add %x, undef" => "undef" because for any value of %x, we can always find a value that when added to %x produces any value in the domain of integers.<br>
<br>
This is not the case with floats since with some inputs, e.g., NaNs, we are not able to produce some values in the domain (e.g., there's no value of %x that makes "fadd NaN, %x" return 42.0).<br>
<br>
In summary, since there's no NaN constant in the IR</blockquote><div><br></div><div>Isn't "float 0x7FF8000000000000" a NaN constant?</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">, all we can do is to canonicalize to some instruction that yields NaN (and treat that as a NaN constant throughout the pipeline, I guess).<br>
<br>
Nuno<br>
<br>
P.S.: With poison you can always fold operations with a poison input to poison (e.g., "fadd %x, poison" => poison).<br>
<br>
<br>
-----Original Message----- From: Kaylor, Andrew<br>
Sent: Wednesday, February 28, 2018 11:29 PM<br>
Subject: RE: how to simplify FP ops with an undef operand?<div><div class="m_-6191499597564934972gmail-h5"><br>
<br>
<br>
<br>
For the first part of Sanjay’s question, I think the answer is, “Yes, we can fold all of these to NaN in the general case.” For the second part, which the nnan FMF is present, I’m not sure. The particulars of the semantics of nnan are unclear to me.<br>
<br>
<br>
<br>
But let me explore what Eli is saying. It sounds reasonable, but I have a question about it.<br>
<br>
<br>
<br>
Suppose we have the nnan FMF set, and we encounter this:<br>
<br>
<br>
<br>
%y = fdiv float %x, undef<br>
<br>
<br>
<br>
If I’ve understood Eli’s interpretation correctly, any of the following transformations would be legal and safe:<br>
<br>
<br>
<br>
%y = 0.0<br>
<br>
%y = -1.0<br>
<br>
%y = inf<br>
<br>
%y = NaN<br>
<br>
<br>
<br>
And so on, covering all possible concrete values, right? Now suppose I don’t change it at all. Now I might have IR that looks like this.<br>
<br>
<br>
<br>
%y = fdiv float %x, undef<br>
<br>
%z = fmul float %q, %y<br>
<br>
<br>
<br>
At this point, while working on %z, I could reconsider and say “If I had transformed the fdiv into ‘%y = 0.0’ I could optimize this fmul away too.” So at that point I can choose to do that, right? And in general I can “retroactively” choose any concrete value that would be convenient for the next transformation. You see where I’m going with this?<br>
<br>
<br>
<br>
How is that different from just folding the fdiv into undef to begin with? Is it because I can’t choose different values on different code paths?<br>
<br>
<br>
<br>
-Andy<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
From: Friedman, Eli [mailto:<a href="mailto:efriedma@codeaurora.org" target="_blank">efriedma@codeaurora.or<wbr>g</a>]<br>
Sent: Wednesday, February 28, 2018 3:07 PM<br>
To: Sanjay Patel <<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a>>; Kaylor, Andrew <<a href="mailto:andrew.kaylor@intel.com" target="_blank">andrew.kaylor@intel.com</a>><br>
Cc: llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>; Nuno Lopes <<a href="mailto:nunoplopes@sapo.pt" target="_blank">nunoplopes@sapo.pt</a>>; Stephen Canon <<a href="mailto:scanon@apple.com" target="_blank">scanon@apple.com</a>>; David Majnemer <<a href="mailto:david.majnemer@gmail.com" target="_blank">david.majnemer@gmail.com</a>>; John Regehr <<a href="mailto:regehr@cs.utah.edu" target="_blank">regehr@cs.utah.edu</a>>; Sanjoy Das <<a href="mailto:sanjoy@playingwithpointers.com" target="_blank">sanjoy@playingwithpointers.co<wbr>m</a>>; Matt Arsenault <<a href="mailto:arsenm2@gmail.com" target="_blank">arsenm2@gmail.com</a>>; Kreitzer, David L <<a href="mailto:david.l.kreitzer@intel.com" target="_blank">david.l.kreitzer@intel.com</a>><br>
Subject: Re: how to simplify FP ops with an undef operand?<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
I'm pretty sure that isn't what nnan is supposed to mean. If the result of nnan math were undefined in the sense of "undef", programs using nnan could have undefined behavior if the result is used in certain ways which would not be undefined for any actual float value (e.g. converting the result to a string), which seems like a surprising result. And I don't think we gain any useful optimization power from saying we can fold to undef instead of something else.<br>
<br>
So I think it's supposed to say "the result is not specified" or something (so an nnan operation which would produce a nan can instead produce any value that isn't undef/poison).<br>
<br>
-Eli<br>
<br>
On 2/28/2018 2:45 PM, Sanjay Patel wrote:<br>
<br>
<br>
<br>
Ah, thanks for explaining. So given that any of these ops will return NaN with a NaN operand, let's choose the undef operand value to be NaN. That means we can fold all of these to a NaN constant in the general case.<br>
<br>
But if we have 'nnan' FMF, then we can fold harder to undef?<br>
nnan - Allow optimizations to assume the arguments and result are not NaN. Such optimizations are required to retain defined behavior over NaNs, but the value of the result is undefined.<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
On Wed, Feb 28, 2018 at 3:25 PM, Kaylor, Andrew <<a href="mailto:andrew.kaylor@intel.com" target="_blank">andrew.kaylor@intel.com</a>> wrote:<br>
<br>
<br>
<br>
What I’m saying is that if we have one operand that is not an undef value then that operand might be NaN and if it is then the result must be NaN. So while it may be true that we don’t have a NaN, it is not true that we definitely do not have a NaN in the example. This is analogous to the example in the language reference where it says “%A = or %X, undef” -> “%A = undef” is unsafe because any bits that are set in %A must be set in the result. If any floating point operand is NaN dynamically, then the result must be NaN.<br>
<br>
<br>
<br>
I don’t believe it’s accurate to say that NaN is “morally equivalent” to undef. There are some similarities, but the important difference is that NaN always has well defined behavior with a specific correct result. There is, perhaps, a sense in which it is analogous to a poison value, but for the purposes of reasoning about the correctness of floating point operations I think it’s best to be pedantic about treating it as the specific value that it is.<br>
<br>
<br>
<br>
Finally, I was pretty sure you knew that fdiv by zero wasn’t undefined. I just wanted to clarify that the “?” in your comment was indicating that the assertion in the language reference was questionable as opposed to this point being in any way actually uncertain.<br>
<br>
<br>
<br>
From: Sanjay Patel [mailto:<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a><wbr>]<br>
Sent: Wednesday, February 28, 2018 1:05 PM<br>
To: Kaylor, Andrew <<a href="mailto:andrew.kaylor@intel.com" target="_blank">andrew.kaylor@intel.com</a>><br>
Cc: llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>; Nuno Lopes <<a href="mailto:nunoplopes@sapo.pt" target="_blank">nunoplopes@sapo.pt</a>>; Stephen Canon <<a href="mailto:scanon@apple.com" target="_blank">scanon@apple.com</a>>; David Majnemer <<a href="mailto:david.majnemer@gmail.com" target="_blank">david.majnemer@gmail.com</a>>; John Regehr <<a href="mailto:regehr@cs.utah.edu" target="_blank">regehr@cs.utah.edu</a>>; Sanjoy Das <<a href="mailto:sanjoy@playingwithpointers.com" target="_blank">sanjoy@playingwithpointers.co<wbr>m</a>>; Friedman, Eli <<a href="mailto:efriedma@codeaurora.org" target="_blank">efriedma@codeaurora.org</a>>; Matt Arsenault <<a href="mailto:arsenm2@gmail.com" target="_blank">arsenm2@gmail.com</a>>; Kreitzer, David L <<a href="mailto:david.l.kreitzer@intel.com" target="_blank">david.l.kreitzer@intel.com</a>><br>
<br>
<br>
<br>
<br>
Subject: Re: how to simplify FP ops with an undef operand?<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
Correct - NaN is not undef in IR. But we don't have a NaN in this example. We have its moral equivalent in LLVM - an uninitialized value, undef.<br>
<br>
So we're not introducing any extra uncertainty by propagating the undef. The backend can choose whatever encoding of undef makes sense when lowering?<br>
<br>
<br>
<br>
And yes, I don't know why FP-div-by-zero would ever be UB. I think that text in the LangRef should be removed regardless of any other outcome here.<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
On Wed, Feb 28, 2018 at 1:18 PM, Kaylor, Andrew <<a href="mailto:andrew.kaylor@intel.com" target="_blank">andrew.kaylor@intel.com</a>> wrote:<br>
<br>
<br>
<br>
Why is NaN “just ‘undef’ in IR”? NaN is a specific value with well-defined behavior. I would think that unless the no-NaNs flag is used we need to preserve the behavior of NaNs.<br>
<br>
<br>
<br>
From: Sanjay Patel [mailto:<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a><wbr>]<br>
Sent: Wednesday, February 28, 2018 12:08 PM<br>
To: Kaylor, Andrew <<a href="mailto:andrew.kaylor@intel.com" target="_blank">andrew.kaylor@intel.com</a>><br>
Cc: llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>; Nuno Lopes <<a href="mailto:nunoplopes@sapo.pt" target="_blank">nunoplopes@sapo.pt</a>>; Stephen Canon <<a href="mailto:scanon@apple.com" target="_blank">scanon@apple.com</a>>; David Majnemer <<a href="mailto:david.majnemer@gmail.com" target="_blank">david.majnemer@gmail.com</a>>; John Regehr <<a href="mailto:regehr@cs.utah.edu" target="_blank">regehr@cs.utah.edu</a>>; Sanjoy Das <<a href="mailto:sanjoy@playingwithpointers.com" target="_blank">sanjoy@playingwithpointers.co<wbr>m</a>>; Friedman, Eli <<a href="mailto:efriedma@codeaurora.org" target="_blank">efriedma@codeaurora.org</a>>; Matt Arsenault <<a href="mailto:arsenm2@gmail.com" target="_blank">arsenm2@gmail.com</a>><br>
Subject: Re: how to simplify FP ops with an undef operand?<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
Yes, if %x is a NaN, we should expect that NaN is propagated.<br>
<br>
I'm still not sure what to do here. We can take comfort in knowing that whatever we do is likely an improvement over the current situation though. :)<br>
<br>
<br>
<br>
That's because the code in InstSimplify is inconsistent with the LangRef:<br>
<a href="http://llvm.org/docs/LangRef.html#undefined-values" rel="noreferrer" target="_blank">http://llvm.org/docs/LangRef.h<wbr>tml#undefined-values</a> (UB for fdiv by 0?)<br>
<br>
<br>
<br>
...and both of those are inconsistent with undef handling in SDAG.<br>
<br>
<br>
<br>
Let me propose an alternate interpretation:<br>
<br>
1. The meaning of snan as written in IEEE754-2008 is: "Signaling NaNs afford representations for uninitialized variables..."<br>
2. That matches our intent with 'undef' here in IR as written in the LangRef: "unspecified bit-pattern".<br>
3. The current fdiv transform is actually correct (any SNaN UB/trapping commentary is irrelevant because we assume exceptions are off by default).<br>
<br>
The undef operand represents an uninitialized variable, and the result of any FP op with that uninitialized variable is well-defined: it's another NaN which is just 'undef' in IR.<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
On Wed, Feb 28, 2018 at 11:43 AM, Kaylor, Andrew <<a href="mailto:andrew.kaylor@intel.com" target="_blank">andrew.kaylor@intel.com</a>> wrote:<br>
<br>
<br>
<br>
I’m not sure the transformation happening with fdiv is correct. If we have “%y = fdiv float %x, undef” and %x is a NaN then the result will be NaN for any value of the undef, right? So if I understand the undef rules correctly (never a certainty) then we can’t safely replace the expression with undef. We could, I think, replace it with “%y = %x” though. I think the same is true for fadd, fsub, fmul, and frem.<br>
<br>
<br>
<br>
-Andy<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
%y = fadd float %x, undef<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
Can we simplify this?<br>
<br>
Currently in IR, we do nothing for fadd/fsub/fmul. For fdiv/frem, we propagate undef. The code comment for fdiv/frem says:<br>
"the undef could be a snan"<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
If that's correct, then shouldn't it be the same for fadd/fsub/fmul? But this can't be correct because we support targets that don't raise exceptions...and even targets that raise exceptions do not trap by default on snan?<br>
<br>
<br>
<br></div></div>
-- Employee of Qualcomm Innovation Center, Inc.Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project <br>
</blockquote></div><br></div></div>