[llvm-branch-commits] [llvm] [LoopInterchange] Drop loop metadata and branch weights of interchanged loops (PR #235892)

Ryotaro Kasuga via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Nov 22 07:22:21 PST 2026


================
@@ -2168,6 +2170,26 @@ void LoopInterchangeTransform::restructureLoops(
   SE->forgetLoop(NewOuter);
 }
 
+/// Update the metadata attached to the latch branches of the two loops. The
+/// loop metadata and the branch weights are dropped. Some attributes of the
+/// loop metadata may not hold after interchange (e.g.,
+/// llvm.loop.parallel_accesses of the inner loop doesn't imply that the loop is
+/// parallel when it becomes the outer loop), and no followup attributes are
+/// defined for interchange.
+/// TODO: Preserve the branch weights by swapping them.
+void LoopInterchangeTransform::updateLatchMetadata() {
+  auto *OuterLoopLatchBI =
+      cast<CondBrInst>(OuterLoop->getLoopLatch()->getTerminator());
+  auto *InnerLoopLatchBI =
+      cast<CondBrInst>(InnerLoop->getLoopLatch()->getTerminator());
+
+  for (CondBrInst *BI : {OuterLoopLatchBI, InnerLoopLatchBI}) {
+    BI->setMetadata(LLVMContext::MD_loop, nullptr);
----------------
kasuga-fj wrote:

I agree that keeping the attributes known to be safe makes sense, but I'd prefer to do it as a follow-up after #235896 and keep this patch as simple as possible. In #235896, each latch ends up controlling the other loop's iterations, so the attributes we keep would also need to be moved to the other latch. If I understand correctly, interchange isn't triggered very often at the moment, and interchanging loops with user hints (e.g., `unroll(disable)`) should be even rarer, so I think we can ignore the impact for now. As for `mustprogress`, I think dropping it has little impact, since the trip counts of interchanged loops are always computable.

https://github.com/llvm/llvm-project/pull/235892


More information about the llvm-branch-commits mailing list