<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Wed, Dec 5, 2018 at 11:19 AM Philip Reames <<a href="mailto:listmail@philipreames.com">listmail@philipreames.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
On 12/5/18 9:45 AM, John McCall wrote:<br>
> On 5 Dec 2018, at 12:19, Philip Reames wrote:<br>
>> On 12/4/18 3:21 PM, John McCall wrote:<br>
>>><br>
>>> On 4 Dec 2018, at 17:50, Philip Reames wrote:<br>
>>><br>
>>>     Skimming along, apologies if I'm repeating something which already<br>
>>>     got said.<br>
>>><br>
>>>     If I understand this correctly, the basic problem we're trying to<br>
>>>     solve is to use a local hint (the invariant.group) to make a<br>
>>>     global assumption about other code which might exist elsewhere<br>
>>>     outside the function. The attribute proposed can basically be<br>
>>>     phrased as describing a universe of functions within which our<br>
>>>     desired global property holds.  There's an ambiguity about what is<br>
>>>     allowed to be assumed about code outside that universe.<br>
>>><br>
>>>     I think it's important to note that we have a precedent of<br>
>>>     something similar to this in TBAA.  TBAA information coming from<br>
>>>     different modules has the same base problem. We solve it by using<br>
>>>     the "root" of the TBAA tree as a scope descriptor, and essentially<br>
>>>     making two TBAA nodes from distinct roots incomparable.<br>
>>><br>
>>>     Can someone explain concisely why a similar scheme couldn't be<br>
>>>     used to solve this problem?<br>
>>><br>
>>> TBAA is conservative in /two/ ways:<br>
>>> - It allows two accesses to alias if they have TBAA nodes with <br>
>>> different roots.<br>
>>> - It allows two accesses to alias if only one of them has a TBAA node.<br>
>>><br>
>>> The second is what doesn't generalize: there are optimizations where <br>
>>> you need to<br>
>>> rely on transition points being explicitly identified. Looking at a <br>
>>> function<br>
>>> with no identified transition points, you don't know whether it <br>
>>> actually doesn't<br>
>>> transition or whether it was compiled without the transitions being <br>
>>> explicitly<br>
>>> marked. There's no way to extend the TBAA idea to make that work.<br>
>>><br>
>>>     On 12/4/18 11:24 AM, John McCall via llvm-dev wrote:<br>
>>><br>
>>>         Note that IPO is generally permitted to partially inline or<br>
>>>         outline code,<br>
>>>         and so good-faith optimizations that e.g. require two<br>
>>>         instructions to be moved<br>
>>>         in tandem or not at all must use tokens to establish that<br>
>>>         unbreakable<br>
>>>         relationship.<br>
>>><br>
>>>     I think the way your framing this is dangerous.  We absolutely can<br>
>>>     not allow any annotation of this form to *weaken* the semantics of<br>
>>>     the existing IR. We can and should impose a criteria that any<br>
>>>     extension of this variety strictly add information to the IR which<br>
>>>     might not have been previously inferred.  We can then design rules<br>
>>>     for how to preserve our new information as long as possible, but<br>
>>>     framing this in terms of disallowed transformations is really a<br>
>>>     non-starter.<br>
>>><br>
>>> That's exactly what I was trying to convey here. Authors of good-faith<br>
>>> optimizations need to design their representations so that <br>
>>> transformations<br>
>>> that know nothing about their optimizations but merely preserve <br>
>>> semantics<br>
>>> and well-formed IR structure will not break their representations. <br>
>>> The only<br>
>>> transforms that need to know about the existence of good-faith <br>
>>> optimizations<br>
>>> are interprocedural optimizations; furthermore, those optimizations <br>
>>> don't<br>
>>> need to know about any good-faith optimizations specifically, they <br>
>>> just need<br>
>>> to understand how to correctly update the supported_optimizations list.<br>
>>> That is a very small burden on IPO that enables an interesting class of<br>
>>> language-specific optimizations.<br>
>>><br>
>> Two responses:<br>
>><br>
>> 1) My comment was on *framing*, not substance.  I'm not debating the <br>
>> *semantics* you've proposed (here), just the way they're described.  <br>
>> The way they're described here is very likely to lead to problematic <br>
>> misinterpretation.<br>
><br>
> Yes, and I'm trying to improve the way I'm talking about it, so I <br>
> would appreciate feedback on whether you feel that my clarifications <br>
> are still prone to problematic misinterpretations.<br>
Ah, I'd misread your intent with the last response.  I'd read it as <br>
disputing my previous point, not accepting it and proposing a reframing <br>
in that spirit.  Rereading with those eyes, yes, I think your last <br>
description is much better than the previous ones.<br>
><br>
>> 2) Reading back through your description again, it really sounds like <br>
>> you've reinvented the rules for metadata with an alternate framing.  <br>
>> The only part which is possibly new is the IPO rules you want to <br>
>> apply.  Worth noting is that we already have existing support for <br>
>> metadata on both instructions and functions.<br>
><br>
> Optimizations relying wholly on metadata can't have this problem <br>
> because of the existing rules for metadata: if the optimization is <br>
> correct even when metadata is randomly dropped, it's correct when <br>
> metadata is incompletely added in the first place.  The problem is <br>
> specific to intrinsic-centric optimizations, as intrinsics cannot be <br>
> summarily dropped like metadata (which is necessary for the <br>
> correctness of the optimization) but may not have been added to all <br>
> functions in the module because of e.g. LTO.<br>
<br>
How is this not handled by the second rule for TBAA about comparing two <br>
nodes where one has metadata and the other doesn't? In general, as long <br>
as the *absence* of a annotation is enough to *disable* an optimization <br>
then there's no problem with using the root scheme.  The only <br>
problematic case would be if the *absence* of an annotation signaled the <br>
*legality* of an optimization which wouldn't otherwise be legal if the <br>
annotation was present.<br></blockquote><div><br></div><div>That is precisely the situation prior to Piotr's proposal. In an annotated function, the absence of a call to an intrinsic permits optimizations to be performed. Eg:</div><div><br></div><div>// case 1</div><div>f(T *a, T *b) {</div><div>  if (a == b) g(a); // may be rewritten to g(b)</div><div> g(b);</div><div>}</div><div>g(T *p) {</div><div>  h(*p);</div><div>}</div><div><br></div><div>// case 2</div><div>f(T *a, T *b) {</div><div>  if (strip(a) == strip(b)) g(a); // may not be rewritten to g(b)</div><div>  g(b);</div><div>}</div><div>g(T *p) {</div><div>  h(*invar(p));</div><div>}</div><div><br></div><div>And the LTO problem is that some combinations of the above, such as...</div><div><br></div><div><div>f(T *a, T *b) {</div><div>  if (a == b) g(a);</div><div>  g(b);</div><div>}</div><div>g(T *p) {</div><div>  h(*invar(p)); // two loads of the "same" p load the same value</div><div>}</div><br class="gmail-Apple-interchange-newline"></div><div>would allow an incorrect optimization, eg to:</div><div><br></div><div>f(T *a, T *b) {</div><div>  if (a == b) { T v = *b; h(v); h(v); } // no reload here</div><div>  else { h(*b); }</div><div>}</div><div><br></div><div>I think Piotr's proposal does essentially what you're suggesting (and I think that's the point you're making, right?). The only reasonable place to attach the annotation in this case is the function, and the proposal is that we permit (interprocedural) optimizations only if all involved functions have the same annotation, which seems the direct analogue of requiring the same TBAA root node.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
If we do have to handle the last case, we can probably do it by <br>
requiring the function have metadata of the same name which is dropped <br>
if any node within the scope is dropped.  This also bears a lot of <br>
similarity to your proposed attribute semantics, but reframed entirely <br>
as metadata.<br></blockquote><div><br></div><div>Right. I don't see a reason why this couldn't be function metadata instead of a function attribute, except that it violates the usual metadata rules (specifically, interprocedural optimizations that involve the annotated function cannot simply ignore the annotation). [We don't need to drop the function metadata if any !invariant.group metadata is dropped (the latter is proper metadata, and can be blindly dropped with no adverse semantic effects). We would need to drop the function metadata if any of the intrinsic calls were dropped, but there's generally no expectation that you can arbitrarily remove intrinsic calls anyway.]</div><div><br></div><div>Given that IPO must respect this annotation (eg, it is not correct to inline a non-supported-optimization function into a supported-optimization function without removing the annotation from the caller), it seems to me that an attribute is a better match than metadata, but I'm not well-calibrated on such issues.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> Would this conversation would be more productive if we worked through <br>
> an example optimization so that you could see the issues involved?<br>
Might be.  I'd also be happy to jump on a quick call so that we can <br>
explain points with higher bandwidth.<br>
><br>
> John.<br>
</blockquote></div></div>