<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Feb 18, 2021 at 10:57 PM Michael Kruse <<a href="mailto:llvmdev@meinersbur.de">llvmdev@meinersbur.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">I wrote the lines in the LangRef and can answer the questions.<br>
<br>
When LoopIDs were first conceived, the idea was that each loop has its<br>
own identifier (so you could, e.g. build a DenseMap to information for<br>
a loop). To avoid that the MDNode of two different would be collapsed<br>
into one, the design made the first item of the MDNode reference<br>
itself. This worked because the uniquifier did not do a GVN or<br>
similar, just compared the content (IMHO still a bad idea, the<br>
uniquing algorithm could be improved later). Later, 'distinct' MDNodes<br>
were introduced, but by then code exists that depend on this. Either<br>
because they check for valid LoopID MDNodes eg. in an assert, skip the<br>
first element in a LoopID MDNode, or, in case of llvm.access.group<br>
(which allows either be a single LoopID or a list of LoopIDs) only<br>
handle the list of LoopIDs case because if it;s only a single LoopID,<br>
the MDNode again contains the self-reference LoopID.<br>
<br>
<br>
There is a problem with using a LoopID as "identifier":<br>
<br>
1. The MDNode changes when changing the loop properties, e.g.<br>
LoopVectorize adds llvm.loop.isvectorized to loops it vectorize and<br>
loops that it determined to be not vectorizable to avoid processing it<br>
again. Hence, LoopIDs are not persistent.<br>
<br>
2. There is some code that makes copies of code, such as LoopUnroll,<br>
inlining or LoopVersioning. These copy all the instructions including<br>
the MDNodes and you get multiple loops with the same LoopID. There is<br>
even a LoopUnroll test case that checks that. Hence, a LoopID is not<br>
unique.<br>
<br>
Therefore, a LoopID does not fulfill the basic requirements to<br>
identify a loop. Instead, we went to interpret the MDNode as a bag of<br>
loop properties which can be changed.<br>
<br>
<br>
With this reinterpretation, I did not change the name 'LoopID' nor<br>
removed the self-reference to avoid breaking things.<br>
<br>
<br>
<br>
Am Do., 18. Feb. 2021 um 19:12 Uhr schrieb Mehdi AMINI via llvm-dev<br>
<<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>:<br>
> The last sentence isn't clear to me, I don't see the relationship between "the loop-metadata node can be attached to multiple nodes" and "the ‘distinct’ keyword has become unnecessary"?<br>
<br>
See above. The distinct/self-reference was meant to stop the same<br>
LoopID to be assigned to multiple loops by the MDNode uniquifier.</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
However, there are other mechanisms that assign the same LoopID to two<br>
different loops.<br></blockquote><div><br></div><div>OK thanks, I get the sentence now: it isn't only the distinct keyword that is unnecessary but also the self-reference. Basically the metadata does not need to be distinct, I think that is what wasn't clear in the sentence unclear for me.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
> Also It isn't clear why we don't use a distinct metadata instead of a self-referencing one?<br>
<br>
To not break existing code that assumes the self-reference.<br></blockquote><div><br></div><div>So this is just tech debt that can be cleaned up then? We could sweep through the code base and make it work with unique MDs?</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
><br>
> Then LangRef mentions:<br>
><br>
> > Loop metadata nodes cannot be used as unique identifiers. They are neither persistent for the same loop through transformations nor necessarily unique to just one loop.<br>
><br>
> So the metadata itself can't be used as a unique identifier for the loop, however in a loop all latches must have the same MDNode for LoopInfo to recognize it as valid.<br>
<br>
A single loop can have multiple latches. These should be consistent,<br>
otherwise we don't know which one is the correct information for this<br>
loop. We could instead choose the first one encountered, but I don't<br>
think its a solution for robustness. In contrast, metadata is defined<br>
to always be able to be dropped without losing semantic correctes.<br>
<br>
<br>
> It isn't clear to me why it prevents using unique metadata here without the self-referencing trick, and rely on content-based uniquing for equality?<br>
<br>
To not break existing code that assumes the self-reference. </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<br>
<br>
> Loop info could still rely on pointer-equality to ensure that the metadata is consistent on every backedge, two loops with the same metadata would share the same MDNode, but it shouldn't be an issue since these aren't used as unique identifiers per LangRef.<br>
<br>
Correct.<br>
<br>
However, it requires work to fix all the places that assumes the<br>
self-reference, makes existing IR incompatible and may cause trouble<br>
downstream. Hal Finkel and me found that this such a change is not<br>
worth the risk.<br></blockquote><div><br></div><div>I'm happy to give it a try to clean-up the codebase and remove all this!</div><div> <br>
</div><div>Thanks for the clarification :)</div><div><br></div><div>-- </div><div>Mehdi</div><div><br></div></div></div>