<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 27, 2015, at 10:29 PM, David Majnemer <<a href="mailto:david.majnemer@gmail.com" class="">david.majnemer@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">On Tue, Jan 27, 2015 at 10:18 PM, Mehdi Amini <span dir="ltr" class=""><<a href="mailto:mehdi.amini@apple.com" target="_blank" class="">mehdi.amini@apple.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><br class="">
> On Jan 27, 2015, at 6:50 PM, David Majnemer <<a href="mailto:david.majnemer@gmail.com" class="">david.majnemer@gmail.com</a>> wrote:<br class="">
><br class="">
> Hello,<br class="">
><br class="">
> What follows is my attempt to describe how poison works. Let me know what you think.<br class="">
><br class="">
> --<br class="">
> David<br class="">
><br class="">
><br class="">
> # LLVM Poison Semantics<br class="">
><br class="">
> Poison is an LLVM concept which exists solely to enable further optimization of LLVM IR. The exact behavior of poison has been, to say the least, confusing for users, researchers and engineers working with LLVM.<br class="">
><br class="">
> This document hopes to clear up some of the confusion of poison and hopefully explain *why* it has its semantics.<br class="">
><br class="">
> ## A Quick Introduction to Poison<br class="">
><br class="">
> Let's start with a concrete motivating example in C:<br class="">
> ```<br class="">
> int isSumGreater(int a, int b) {<br class="">
> return a + b > a;<br class="">
> }<br class="">
> ```<br class="">
><br class="">
> The C specification permits us to optimize the comparison in `isSumGreater` to `b > 0` because signed overflow results in undefined behavior. A reasonable translation of `isSumGreater` to LLVM IR could be:<br class="">
<br class="">
</span>Typo:<br class="">
to `b > 0`<br class="">
should be<br class="">
to `a + b > 0`<br class="">
IIUC.<br class="">
<br class="">
I mention in case you intend to publish this in the doc.<br class=""></blockquote><div class=""><br class=""></div><div class="">I intended what I wrote. We perform this optimization in InstCombine today: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?revision=226783&view=markup#l3193" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?revision=226783&view=markup#l3193</a></div></div></div></div></div></blockquote><div><br class=""></div><div>Yeah sorry I was completely wrong.</div><div><br class=""></div><div>Mehdi</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br class="">
<br class="">
—<br class="">
Mehdi<br class="">
<div class=""><div class="h5"><br class="">
<br class="">
<br class="">
<br class="">
><br class="">
> ```<br class="">
> define i32 @isSumGreater(i32 %a, i32 %b) {<br class="">
> entry:<br class="">
> %add = add i32 %a, %b<br class="">
> %cmp = icmp sgt i32 %add, %a<br class="">
> %conv = zext i1 %cmp to i32<br class="">
> ret i32 %conv<br class="">
> }<br class="">
> ```<br class="">
><br class="">
> However, LLVM cannot determine that `%cmp` should not consider cases where `%add` resulted in signed overflow. We need a way to communicate this information to LLVM.<br class="">
><br class="">
> This is where the `nsw` and `nuw` flags come into play. `nsw` is short for "no signed wrap", `nuw` is short for "no unsigned wrap".<br class="">
><br class="">
> With these, we can come up with a new formulation of `%add`: `add i32 nsw %a, %b`.<br class="">
> LLVM can take this into account when it is optimizing the `%cmp` and replace it with: `icmp sgt i32 %b, 0`.<br class="">
><br class="">
> ## Differences Between LLVM and C/C++<br class="">
><br class="">
> There are some interesting differences between what C++ and C specify and how LLVM behaves with respect to performing an operationg which is not permitted to overflow.<br class="">
><br class="">
> Perhaps chief among them is that evaluating an expression in C++ or C which results performs an overflow is undefined behavior. In LLVM, executing an instruction which is marked `nsw` but which violates signed overflow results in poison. Values which have no relationship with poisoned values are not effected by them.<br class="">
><br class="">
> Let us take the following C program into consideration:<br class="">
> ```<br class="">
> int calculateImportantResult(int a, int b) {<br class="">
> int result = 0;<br class="">
> if (a) {<br class="">
> result = a + b;<br class="">
> }<br class="">
> return result;<br class="">
> }<br class="">
> ```<br class="">
><br class="">
> A straightforward lowering to LLVM IR could be:<br class="">
> ```<br class="">
> define i32 @calculateImportantResult(i32 %a, i32 %b) {<br class="">
> entry:<br class="">
> %tobool = icmp ne i32 %a, 0<br class="">
> br i1 %tobool, label %if.then, label %if.end<br class="">
><br class="">
> if.then:<br class="">
> %add = add nsw i32 %a, %b<br class="">
> br label %if.end<br class="">
><br class="">
> if.end:<br class="">
> %result = phi i32 [ %add, %if.then ], [ 0, %entry ]<br class="">
> ret i32 %result<br class="">
> }<br class="">
> ```<br class="">
><br class="">
> Moving `%add` to the `%entry` block would be preferable and would allow further optimizations:<br class="">
> ```<br class="">
> define i32 @calculateImportantResult(i32 %a, i32 %b) {<br class="">
> entry:<br class="">
> %tobool = icmp ne i32 %a, 0<br class="">
> %add = add nsw i32 %a, %b<br class="">
> %result = select i1 %tobool, i32 0, i32 %add<br class="">
> ret i32 %result<br class="">
> }<br class="">
> ```<br class="">
><br class="">
> In the original code, the calculation of `%add` was control dependent.<br class="">
> Now, `%add` might result in signed overflow in violation of the `nsw` flag.<br class="">
> Despite this, the program should behave as it did before because the poisoned value is masked-out by the select. The next section will dive into this in greater detail.<br class="">
><br class="">
> # Computation Involving Poison Values<br class="">
> Poison in a computation results in poison if the result cannot be constrained by its non-poison operands.<br class="">
><br class="">
> Examples of this rule which will result in poison:<br class="">
> ```<br class="">
> %add = add i32 %x, %always_poison<br class="">
> %sub = sub i32 %x, %always_poison<br class="">
> %xor = xor i32 %x, %always_poison<br class="">
> %mul = mul i32 %always_poison, 1<br class="">
> ```<br class="">
><br class="">
> Examples of this rule which do not result in poison:<br class="">
> ```<br class="">
> %or = or i32 %always_poison, 2<br class="">
> %and = and i32 %always_poison, 2<br class="">
> %mul = mul i32 %always_poison, 0<br class="">
> ```<br class="">
><br class="">
> In fact, it would be reasonable to optimize `%or` to `2` and `%and` to `0`. In this respect, poison is not different from `undef`.<br class="">
><br class="">
> The following example is only poison if `%cond` is false:<br class="">
> ```<br class="">
> %sel = select i1 %cond, i32 2, %always_poison<br class="">
> ```<br class="">
><br class="">
> ### Is it safe to have poison as a `call` argument?<br class="">
><br class="">
> A `call` instruction may or may not result in poison depending on exactly how the callee uses the supplied arguments, it is not necessarily the case that `call i32 @someFunction(i32 %always_poison)` results in poison.<br class="">
><br class="">
> LLVM cannot forbid poison from entering `call` arguments without prohibiting an optimization pass from outlining code.<br class="">
><br class="">
> ### Is it safe to store poison to memory?<br class="">
><br class="">
> `store i32 %always_poison, i32* %mem` does not result in undefined behavior. A subsequent load instruction like `%load = load i32* %mem` will result in `%load` being a poison value.<br class="">
><br class="">
> ### Is it safe to load or store a poison memory location?<br class="">
><br class="">
> No. Poison works just like `undef` in this respect.<br class="">
><br class="">
> ### Does comparing a poison value result in poison?<br class="">
><br class="">
> It depends. If the comparison couldn't solely be determined by looking at the other operand, the result is poison.<br class="">
><br class="">
> For example, `icmp i32 ule %always_poison, 4294967295` is `true`, not poison.<br class="">
> However, `icmp i32 ne %always_poison, 7` is poison.<br class="">
><br class="">
> ### What if the condition operand in a `select` is poison?<br class="">
><br class="">
> In the example `%sel = select i1 %always_poison, i1 true, false`, `%sel` is either `true` or `false`. Because, `%sel` depends on `%always_poison` it too is poison.<br class="">
</div></div>> _______________________________________________<br class="">
> LLVM Developers mailing list<br class="">
> <a href="mailto:LLVMdev@cs.uiuc.edu" class="">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu/" target="_blank" class="">http://llvm.cs.uiuc.edu</a><br class="">
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank" class="">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br class="">
<br class="">
</blockquote></div><br class=""></div></div>
</div></blockquote></div><br class=""></body></html>