<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi,<br><div><br></div><div>Our frontend (MLIR ;)) is emitting loops into LLVM IR and adding support to emit the !loop metadatas as well: <a href="https://llvm.org/docs/LangRef.html#llvm-loop">https://llvm.org/docs/LangRef.html#llvm-loop</a></div><div>First as an example the metadata look like this: <a href="https://github.com/llvm/llvm-project/blob/main/llvm/test/Transforms/LoopUnroll/unroll-pragmas.ll#L58-L59">https://github.com/llvm/llvm-project/blob/main/llvm/test/Transforms/LoopUnroll/unroll-pragmas.ll#L58-L59</a><br></div><div><br></div><div><div>!1 = !{!1, !2}</div><div>!2 = !{!"llvm.loop.unroll.disable"}}</div></div><div><br></div><div>The !1 will be attached to all "latches" (back edges into the loop header) branches:</div><div><br></div><div>  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !1</div><div> </div><div><br></div><div><div style="color:rgb(0,0,0)">I'm a bit confused by a few things with this metadata at the moment. First the self-referencing of the metadata is called out in LangRef:</div></div><div style="color:rgb(0,0,0)"><br></div><div style="color:rgb(0,0,0)">> For legacy reasons, the first item of a loop metadata node must be a reference to itself. Before the advent of the ‘distinct’ keyword, this forced the preservation of otherwise identical metadata nodes. Since the loop-metadata node can be attached to multiple nodes, the ‘distinct’ keyword has become unnecessary.</div><div style="color:rgb(0,0,0)"><br></div><div style="color:rgb(0,0,0)">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"?</div><div style="color:rgb(0,0,0)"><br></div><div style="color:rgb(0,0,0)">Also It isn't clear why we don't use a distinct metadata instead of a self-referencing one?</div><div style="color:rgb(0,0,0)"><br></div><div style="color:rgb(0,0,0)">Then LangRef mentions:</div><div style="color:rgb(0,0,0)"><br></div><div><div><font color="#000000"><span style="caret-color: rgb(0, 0, 0);">> 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.</span></font></div><div style="color:rgb(0,0,0)"><br></div></div><div style="color:rgb(0,0,0)">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. </div><div style="color:rgb(0,0,0)"><br></div><div style="color:rgb(0,0,0)">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?</div><div style="color:rgb(0,0,0)">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.</div><div style="color:rgb(0,0,0)"><br></div><div style="color:rgb(0,0,0)">Thanks,</div><div style="color:rgb(0,0,0)"><br></div><div style="color:rgb(0,0,0)">-- </div><div style="color:rgb(0,0,0)">Mehdi</div><div style="color:rgb(0,0,0)"><br></div></div></div></div></div></div></div></div></div></div></div></div>