<div dir="ltr">Hi Serge,<div><br></div><div>> %r = fadd undef, %x</div>> --><br>> poison<div><br></div><div>Supporting this transformation is slightly complex because a value can be partially undef:</div><div><br></div><div>%r = fadd undef, %x ; let's assume that this is poison</div><div>%r = fadd (or undef, 1), %x ; what about this?<br></div><div>%r = fadd (or undef, 0x7F..FF), %x ; this has a single undef bit only; maybe it isn't undef enough to yield poison.</div><div><br></div><div>So, relying on 'fadd poison %x -> poison' and making poison appear as frequently as possible might be a cleaner option (in my opinion).</div><div><br></div><div><br></div><div>About moving away from undef:</div><div>Let me share how things are going, since people might be interested in it.</div><div>There are three sources of undef currently, and for each of them some kind of progress has been made (thanks to reviewers and people):<br></div><div><br></div><div><br></div><div>1. Undef is being used as a don't-care value.</div><div>A creation of a vector value is done via a sequence of insertelement, e.g., insertelement(insertelement undef, x, 1), y, 2.</div><div>As shown in this expression, undef is used as a don't-care value.</div><div>There are a few patches landed to make instructions to use poison instead. For example, IRBuilder::CreateShuffleVector is now using poison for its second vector operand.</div><div>However, there are so many places where UndefValue is being used, so they aren't fully updated. :(</div><div>Updating transformations to use poison for insertelement/insertvalue/phi/etc's don't-care value will facilitate further optimizations.</div><div>One successful case I observed was InstCombine's unit test removing unreachable instructions after switching to poison.</div><div><br></div><div>2. Undef value is used in the semantics of shufflevector's undef mask.</div><div>Shufflevector is currently defined to yield undef if the mask is undef.</div><div>This is because optimizations want to regard shufflevector of a specific form to be equivalent to 'insertelement undef, ...'.</div><div>If don't-care values are fully updated to be poison (item 1), the semantics of shufflevector can be finally updated to return poison.</div><div>There is another blocker though; X86-64's mm*_undefined* intrinsics are supposed to return an uninitialized vector which is not *undefined*; unlike undef, each read should return a consistent value.</div><div>Using shufflevector with undef mask to encode mm*_undefined* was already wrong, and making shufflevector return poison will make it worse.<br></div><div><br></div><div>3. Undef value is used to represent the value of the uninitialized memory.</div><div>Poison can be used instead, but (in the case of C/C++) two cases should be treated carefully:</div><div>(1) Translating bit fields into IR: since there is no bitwise load and store in IR, poison bits can contaminate the whole loaded value.</div><div>(2) A variable whose address is escaped: I heard from a few people studying C standard that an uninitialized variable whose address is escaped contains an unspecified value, which is more defined than both undef and poison IIUC.</div><div>Precisely encoding this case will come at a cost.</div><div><br></div><div><br></div><div>Fully addressing these requires correctly understanding a number of transformations/analyses in LLVM; it is pretty scary to fix them as well :/</div><div><br></div><div>Best,</div><div>Juneyoung</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jul 23, 2021 at 7:24 PM Serge Pavlov via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Thank you for the reference. I saw an even older discussion on this topic in the IRC channel. It looks like the problem of understanding `undef` has been persisting since long ago. Probably it is because `undef` is "one of the set" value, but the set itself is not specified. For floating point values it generally includes all possible values, but for example if `-fffast-math` is in action, NaNs are not in this set.<br><br>Another source of problems is replacing `undef` with concrete value. It turns "one of the set" into one value and this contraction cannot be equally good for all cases. For example:<br><br> %A = select undef, %X, %Y<br> %B = select undef, %X, 42<br> %C = icmp eq %A, %B<br><br>Contraction of `select` instructions to the first operands, as recommended in LLVM Language Reference Manual would make the compiler deduce that %C is true, which is not correct in general case.<br><br>The concept of poison seems more clear and consistent. I wonder if we could make transformations like:<br><br>%r = fadd undef, %x<br>--><br>poison<br><br>and similar for constrained intrinsics. Using `poison` is consistent with using `undef` for values on which the result does not depend. When poison needs representation in machine code, it could be lowered to NaN, which behaves similarly in runtime. The same solution is already made for shufflevector. Does anything prevents from such transformation?<div><br clear="all"><div><div dir="ltr">Thanks,<br>--Serge<br></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jul 22, 2021 at 7:29 PM Sanjay Patel <<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Unfortunately, it's not as easy as "any undef in --> undef out". That's a big reason for moving away from undef in IR.</div><div><br></div><div>If you read this page bottom-up (there must be a better link somewhere?) and then read the follow-ups in the thread, you'll see how we arrived at the current rules for the standard FP ops:</div><div><a href="https://lists.llvm.org/pipermail/llvm-dev/2018-March/121481.html" target="_blank">https://lists.llvm.org/pipermail/llvm-dev/2018-March/121481.html</a></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jul 22, 2021 at 6:34 AM Serge Pavlov <<a href="mailto:sepavloff@gmail.com" target="_blank">sepavloff@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">The concept of undefined value has always been obscure and caused many questions. I'd like to share my opinion, however I am not sure if I understand this concept correctly.<br><br>LLVM documentation (<a href="https://llvm.org/docs/LangRef.html#undefined-values" target="_blank">https://llvm.org/docs/LangRef.html#undefined-values</a>) describes undefined values:<br>"Undefined values are useful because they indicate to the compiler that the program is well defined no matter what value is used". So these are values on which the result of program execution does not depend. This is why an undefined value may be replaced by an arbitrary value of proper type and range. The choice of the replacement value is dictated mainly by convenience. If however the produced result depends on this choice, it means the value of `undef` affects results, so the initial supposition is broken and we have undefined behavior.<br><br>I agree with Sanjay that constrained intrinsics should behave in the same way as regular FP operations with respect to `undef`. Control modes (like rounding mode) influence result value, but we know that particular value of `undef` is not important. FP exceptions are a bit more complex. If the value of `undef` may be arbitrary, it is not possible to guarantee that FP exceptions would be the same for all possible values. So we can assume that `undef` operands do not affect FP exceptions. Either such operation is eliminated, because its value is not used, or the operation itself does not use the `undef` argument.<br><br>If any of standard IR FP operations has undef argument, the result may be either `undef` or any FP value. It is convenient to use NaN in such cases. It does not make the program more correct but it can help to detect undefined behavior in some FP environments. However `undef` result seems better choice than NaN, because in this case the user of `undef` value may choose a convenient representation for `undef`.<br><br>I do not see any reason to distinguish between the cases "all operands are undefs" and "only one operand is undef". In both cases we get a value that is not used in the correct program.<br><br>So I would propose transformations:<br><br>%r = call float @llvm.experimental.constrained.fadd.f32(float undef, float undef, metadata !"round.dynamic", metadata !"fpexcept.strict")<br> --><br> %r = undef<br><br>And<br><br>%r = call float @llvm.experimental.constrained.fadd.f32(float undef, float %x, metadata !"round.dynamic", metadata !"fpexcept.strict")<br> --><br> %r = undef<div><br></div><div>What do you think about it?</div><div><br><div><div dir="ltr">Thanks,<br>--Serge<br></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 21, 2021 at 8:15 PM Sanjay Patel <<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Can we use the regular FP instructions (fadd, fmul, etc.) as a model?<br></div><div><br></div><div>If both operands to any of the binops are undef, then the result is undef. So for the corresponding constrained intrinsic, if both operands are undef, the result is undef and the exception state is also undef:</div><div><br></div><div> %r = call float @llvm.experimental.constrained.fadd.f32(float undef, float undef, metadata !"round.dynamic", metadata !"fpexcept.strict")</div><div> --></div><div> %r = undef</div><div><br></div><div><div> %r = call float @llvm.experimental.constrained.fadd.f32(float
undef, float undef, metadata !"round.dynamic", metadata
!"fpexcept.maytrap")</div><div> --></div><div> %r = undef</div><div><br></div></div><div><br></div><div>If one operand is undef and the other is regular value, assume that the undef value takes on some encoding of SNaN:</div><div><div><br></div><div> %r = call float @llvm.experimental.constrained.fadd.f32(float
undef, float %x, metadata !"round.dynamic", metadata
!"fpexcept.strict")</div></div><div> --></div><div><div><div> %r = call float @llvm.experimental.constrained.fadd.f32(float SNaN, float %x, metadata !"round.dynamic", metadata
!"fpexcept.strict") ; raise invalid op exception<br></div></div></div><div> (%r could be folded to QNaN here, but we can't get rid of the call, so don't bother?)<br></div><div><br></div><div><div><div> %r = call float @llvm.experimental.constrained.fadd.f32(float
undef, float %x, metadata !"round.dynamic", metadata
!"fpexcept.maytrap")</div></div><div> --></div><div> %r = QNaN ; exception state does not have to be preserved<br></div><div><br></div><div>Does that match the proposed behavior in <a href="https://reviews.llvm.org/D102673" target="_blank">https://reviews.llvm.org/D102673</a> <span><span>(cc @sepavloff)?</span></span></div><div><br></div><div>We could go further (potentially reduce to poison) if we have fast-math-flags on the calls -- just as we partially do with the regular instructions -- but it probably doesn't matter much to real code.<br></div></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jul 9, 2021 at 12:06 PM Kevin Neal via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
<font size="2" face="Courier New"><span style="font-size:11pt">
<div>How should the constrained FP intrinsics behave when called with an operand that is “undef” and the FP environment is _<i>not</i>_ the default environment? I’m specifically working in the middle end passes if it matters. Let me start with the assumption
that the rounding mode is not relevant. That still leaves the exception handling as a factor:</div>
<div><font face="Calibri"> </font></div>
<div>With “fpexcept.maytrap” we are allowed to drop instructions that could or would cause a trap at run-time. Does this imply we can fold the entire instruction to a new undef?</div>
<div> </div>
<div>With “fpexcept.strict” we are _<i>not</i>_ allowed to lose or reorder traps. So how does that affect undef? What happens in the backend? Perhaps the middle end should leave the instruction with the undef and let the backend do something reasonable?</div>
<div> </div>
<div>The “maytrap” case is the one I’m most interested in. An earlier version of D103169 would fold away undef constrained intrinsics in the maytrap case. This was removed so it could be handled without affecting the rest of the patch I believe.</div>
<div><font face="Calibri"> </font></div>
<div>Opinions?</div>
<div><font size="2" face="Calibri"><span style="font-size:10pt">--<font size="2"><span style="font-size:11pt">
<br>
</span></font><font face="Courier New">Kevin P. Neal<br>
</font><font face="Courier New">SAS/C and SAS/C++ Compiler</font></span></font></div>
<div><font size="2"><span style="font-size:10pt">Compute Services</span></font></div>
<div><font size="2"><span style="font-size:10pt">SAS Institute, Inc.</span></font></div>
<div><font face="Calibri"> </font></div>
<div><font face="Calibri"> </font></div>
<div><font face="Calibri"> </font></div>
<div><font face="Calibri"> </font></div>
</span></font>
</div>
_______________________________________________<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="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
_______________________________________________<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="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><br></div><font size="1">Juneyoung Lee</font><div><font size="1">Software Foundation Lab, Seoul National University</font></div></div></div>