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

Madhur Amilkanthwar via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Nov 3 23:03:11 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);
----------------
madhur13490 wrote:

The loop ID also stays with its own loop, so some attributes are still valid after interchange: llvm.loop.unroll.disable, llvm.loop.mustprogress, and llvm.loop.estimated_trip_count. Dropping them means, for example, that a user's #pragma clang loop unroll(disable) is silently ignored once the loop is interchanged, and C loops lose mustprogress. Could we drop only the attributes whose meaning depends on the loop's position in the nest (parallel_accesses, vectorize.*, unroll_and_jam.*, ...)? Or keep a list of attributes known to be safe? parallel_accesses is definitely a case that should be dropped, as the comment says.

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


More information about the llvm-branch-commits mailing list