[llvm-dev] Is trapping allowed when an add with nsw flag overflows?
Manuel Jacob via llvm-dev
llvm-dev at lists.llvm.org
Mon Apr 18 09:48:45 PDT 2016
On 2016-04-18 18:19, David Majnemer wrote:
> It comes down to observability. It's fine to store poison into a
> location and load it back; however, the optimizer is free to delete
> of poison. What is not ok is depending on the value of a load of a
> value in code which may have observable results (volatile, system
Thank you for the clarification.
To take up the initial question, is trapping OK when a non-volatile
store stores a poison value?
> On Mon, Apr 18, 2016 at 11:18 AM, Manuel Jacob <me at manueljacob.de>
>> [This mail could be an answer to the other responses as well, as they
>> basically are the same.]
>> Ah, I think I understand now what poison is for. Adds are defined to
>> have side-effects, so the dependence rule is needed so the optimizer
>> allowed to exploit undefined behavior. Is this correct?
>> I forgot to mention in my original mail that our trapping arithmetic
>> operations are fully speculable. This means that the trap won't
>> until the result is e.g. stored to memory. A bit like poison values
>> Something is still unclear to me: while, according to the examples in
>> LangRef, a volatile store has undefined behavior when a poison value
>> stored, this seems to not be true in case of non-volatile stores. Can
>> someone clarify please?
>> On 2016-04-15 19:44, John Regehr via llvm-dev wrote:
>>> No, trapping is not allowed, since an overflowing add nsw is defined
>>> to produce a poison value, which sort of explodes into undefined
>>> behavior if it reaches a side-effect. This is to support speculative
>>> If you emit trapping adds for nsw, you'll see spurious traps every
>>> and then.
>>> There's a stronger form of undefined behavior, exhibited by things
>>> like divide by zero, that permits traps.
>>> On 4/15/16 7:28 PM, Manuel Jacob via llvm-dev wrote:
>>>> In our backend, we currently emit add operations that trap on
>>>> if the IR operation has the nsw flag set. Is this allowed?
>>>> According to the documentation about poison values, overflowing a
>>>> add is undefined behavior. However I didn't find a formal
>>>> definition of
>>>> undefined behavior in LLVM. Judging from previous discussions on
>>>> mailing list, there seems to be a vague line of what LLVM is allowed
>>>> do in case of undefined behavior. Is trapping allowed?
More information about the llvm-dev