<div dir="ltr">On 22 July 2013 01:11, Kuperstein, Michael M <span dir="ltr"><<a href="mailto:michael.m.kuperstein@intel.com" target="_blank">michael.m.kuperstein@intel.com</a>></span> wrote:<br><div class="gmail_extra">

<div class="gmail_quote"><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">Of course frontends are free to put attributes, but it would be nice if optimizations actually used them. ;-)<br>


My use case is that of proprietary frontend that happens to know some library function calls - which are only resolved at link time - have no side effects and are safe to execute speculatively, and wants to tell the optimizer it can move them around however it likes. I'll gladly submit a patch that uses these hints, but I'd like to reach some consensus on what the desired attributes actually are first. The last thing I want is to add attributes that are only useful to myself.<br>


<br>
Regarding having several orthogonal attributes vs. things like "safetospeculate":<br>
<br>
To know a function is safe to speculatively execute, I need at least:<br>
1) readnone (readonly is insufficient, unless I know all accessed pointers are valid)<br>
2) nounwind<br>
3) nolongjmp (I guess?)<br>
4) no undefined behavior. This includes things like "halting" and "no division by zero", but that's not, by far, an exhaustive list.<br>
<br>
I guess there are several ways to handle (4).<br>
Ideally, I agree with you, we'd like a set of orthogonal attributes that, taken together, imply that the function's behavior is not undefined.<br>
But that requires mapping all sources of undefined behavior (I don't think this is currently documented for LLVM IR, at least not in a centralized fashion) and adding a very specific attribute for each of them. I'm not sure having function declarations with "readnone, nounwind, nolongjmp, halting, nodivbyzero, nopoisonval, nocomparelabels, nounreachable, ..." is desirable.<br>


<br>
We could also have a "welldefined" attribute and a "halting" attribute where "welldefined" subsumes "halting", if the specific case of a function which halts but may have undefined behavior is important.<br>


While the two are not orthogonal, it's similar to the situation with "readnone" and "readonly". Does that sound reasonable?<br></blockquote><div><br></div><div>You're entirely right. I forgot about undefined behaviour.</div>

<div><br></div><div>If you want a 'speculatable' attribute, I would review that patch. Please audit the intrinsics (at least the target-independent ones) and appropriate library functions for whether you can apply this attribute to them. I think the only optimization that it can trigger is that "isSafeToSpeculativelyExecute" returns true on it. Anything else? Is it safe to infer readnone and nounwind from speculatable?<br>

</div><div><br></div><div>I should mention that speculatable functions are extraordinarily limited in what they can do in the general (non<span style="color:rgb(68,68,68);font-family:arial,sans-serif;line-height:16px">–</span>LLVM-as-a-library) case. They may be hoisted above calls to fork or pthread_create, they may be moved into global constructors (and thus can't depend on global state), etc. However, since you have a specific library you want to generate code against, you have the power to make use of it. I don't expect clang or dragonegg to be able to make use of it.</div>

<div><br></div><div>Nick</div><div> </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"><div class=""><div class="h5">


-----Original Message-----<br>
From: Nick Lewycky [mailto:<a href="mailto:nicholas@mxc.ca">nicholas@mxc.ca</a>]<br>
Sent: Monday, July 22, 2013 10:24<br>
To: Kuperstein, Michael M<br>
Cc: Andrew Trick; <a href="mailto:llvmdev@cs.uiuc.edu">llvmdev@cs.uiuc.edu</a><br>
Subject: Re: [LLVMdev] Does nounwind have semantics?<br>
<br>
Kuperstein, Michael M wrote:<br>
> I'm not sure I understand why it's blocked on that, by the way.<br>
<br>
It blocks our ability to automatically deduce the halting attribute in the optimizer, which was necessary for the use case I had at the time.<br>
If you have a use case of your own, feel free to propose the patch!<br>
<br>
(Technically it's not *blocked* -- see how my patch does it! -- but the workarounds are too horrible to be committed.)<br>
<br>
> Even if we can't apply the attribute ourselves, I don't see why we wouldn't expose that ability to frontends.<br>
<br>
Frontends are free to put attributes on functions if they want to. Go for it!<br>
<br>
> I'm not entirely sure "halting" is the right attribute either, by the way.<br>
> What I, personally, would like to see is a way to specify a function call is safe to speculatively execute. That implies readnone (not just readonly), nounwind, halting - and Eris knows what else. Nick, is that too strong for you?<br>


<br>
I strongly prefer the approach of having orthogonal attributes. There are optimizations that you can do with each of these attributes on their own. In particular I think that readonly+halting+nounwind+nolongjmp is going to be common and I'd feel  silly if we had a special case for<br>


readnone+halting+nounwind+nolongjmp and thus couldn't optimize the more<br>
common case.<br>
<br>
That said, I'm also going to feel silly if we don't end up with enough<br>
attributes to allow isSafeToSpeculate to deduce it, which is where we<br>
are right now. I was planning to get back to fixing this after<br>
Chandler's promised PassManager work.<br>
<br>
Nick<br>
<br>
><br>
> Michael<br>
><br>
> -----Original Message-----<br>
> From: <a href="mailto:llvmdev-bounces@cs.uiuc.edu">llvmdev-bounces@cs.uiuc.edu</a> [mailto:<a href="mailto:llvmdev-bounces@cs.uiuc.edu">llvmdev-bounces@cs.uiuc.edu</a>] On Behalf Of Nick Lewycky<br>
> Sent: Monday, July 22, 2013 07:08<br>
> To: Andrew Trick<br>
> Cc: <a href="mailto:llvmdev@cs.uiuc.edu">llvmdev@cs.uiuc.edu</a><br>
> Subject: Re: [LLVMdev] Does nounwind have semantics?<br>
><br>
> Andrew Trick wrote:<br>
>> Does 'nounwind' have semantics that inform optimization passes? It seems to in some cases, but not consistently. For example...<br>
>><br>
>> int32_t foo(int32_t* ptr) {<br>
>>     int i = 0;<br>
>>     int result;<br>
>>     do {<br>
>>       bar(ptr);<br>
>>       result = *ptr;<br>
>>       bar(ptr);<br>
>>     } while (i++<   *ptr);<br>
>>     return result;<br>
>> }<br>
>><br>
>> Say we have a front end that declares bar as...<br>
>><br>
>> declare void @bar(i32*) readonly;<br>
>><br>
>> So 'bar' is 'readonly' and 'may-unwind'.<br>
>><br>
>> When LICM tries to hoist the load it interprets the 'may-unwind' as "MayThrow" in LICM-language and bails. However, when it tries to sink the call itself it sees the 'readonly', assumes no side effects and sinks it below the loads. Hmm...<br>


>><br>
>> There doesn't appear to be a way to declare a function that is guaranteed not to write to memory in a way that affects the caller, but may have another well-defined side effect like aborting the program. This is interesting, because that is the way runtime checks for safe languages would like to be defined. I'm perfectly happy telling front ends to generate control flow for well-defined traps, since I like lots of basic blocks in my IR. But I'm still curious how others deal with this.<br>


><br>
> Yes, we went through a phase where people would try to use "nounwind+readonly == no side-effects" to optimize. All such optimizations are wrong. Unless otherwise proven, a function may inf-loop, terminate the program, or longjmp.<br>


><br>
> I tried to add 'halting' to help solve part of this a long time ago, but it never went in. The problem is that determining whether you have loops requires a FunctionPass (LoopInfo to find loops and SCEV to determine an upper bound) and applying function attributes is an SCC operation (indeed, an SCC is itself a loop), so it's all blocked behind fixing the PassManager to allow CGSGGPasses to depend on FunctionPasses.<br>


> <a href="http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20100705/103670.html" target="_blank">http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20100705/103670.html</a><br>
><br>
> I'm now in a similar situation where I want 'nounwind' to mean "only exits by terminating the program or a return instruction" but unfortunately functions which longjmp are considered nounwind. I would like to change llvm to make longjmp'ing a form of unwinding (an exceptional exit to the function), but if I were to apply that rule today then we'd start putting dwarf eh tables on all our C code, oops.<br>


><br>
> Nick<br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
> ---------------------------------------------------------------------<br>
> Intel Israel (74) Limited<br>
><br>
> This e-mail and any attachments may contain confidential material for<br>
> the sole use of the intended recipient(s). Any review or distribution<br>
> by others is strictly prohibited. If you are not the intended<br>
> recipient, please contact the sender and delete all copies.<br>
><br>
><br>
<br>
---------------------------------------------------------------------<br>
Intel Israel (74) Limited<br>
<br>
This e-mail and any attachments may contain confidential material for<br>
the sole use of the intended recipient(s). Any review or distribution<br>
by others is strictly prohibited. If you are not the intended<br>
recipient, please contact the sender and delete all copies.<br>
<br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</div></div></blockquote></div></div></div>