[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:09 PST 2026


https://github.com/kasuga-fj updated https://github.com/llvm/llvm-project/pull/235892

>From d2fa5296da2be419d4ddd6124f530e387675ebb2 Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <ryotaro.kasuga at gmail.com>
Date: Sun, 25 Oct 2026 10:07:55 +0900
Subject: [PATCH 1/2] [LoopInterchange] Drop loop metadata and branch weights
 of interchanged loops

This patch drops the loop metadata and the branch weights of the latches
when interchanging loops. As for the loop metadata, some attributes may
no longer hold after interchange (e.g., parallel_accesses of the inner
loop), and its semantics are partially ambiguous (e.g., what should we
do with unroll_and_jam?), so it is conservatively removed for now. As
for the branch weights, I believe we don't need to drop them
unconditionally, but they are also conservatively removed (or marked as
unknown if the function has profile data) for now. Moving them to the
appropriate place is left as future work.

Note: I'm not entirely sure that dropping these is sufficient. Please
let me know if there are other kinds of data that should be dropped when
interchanging loops.

Part of #235496

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
 .../lib/Transforms/Scalar/LoopInterchange.cpp | 24 ++++++++++++++
 .../LoopInterchange/loop-metadata.ll          | 31 +++++++------------
 2 files changed, 36 insertions(+), 19 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index 610b64d0de7cc5..6a766ca7831f91 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -36,6 +36,7 @@
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/Instructions.h"
+#include "llvm/IR/ProfDataUtils.h"
 #include "llvm/IR/User.h"
 #include "llvm/IR/Value.h"
 #include "llvm/IR/Verifier.h"
@@ -629,6 +630,7 @@ class LoopInterchangeTransform {
 
 private:
   void adjustLoopBranches();
+  void updateLatchMetadata();
 
   Loop *OuterLoop;
   Loop *InnerLoop;
@@ -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);
+    BI->setMetadata(LLVMContext::MD_prof, nullptr);
+    setExplicitlyUnknownBranchWeightsIfProfiled(*BI, DEBUG_TYPE);
+  }
+}
+
 ///  User can write, or optimizers can generate the reduction for inner loop.
 ///  To make the interchange valid, apply Reduction2Mem by moving the
 ///  initializer and store instructions into the inner loop. So far we only
@@ -2233,6 +2255,8 @@ void LoopInterchangeTransform::transform(
     ArrayRef<Instruction *> DropNoWrapInsts,
     ArrayRef<Instruction *> DropNoInfInsts) {
 
+  updateLatchMetadata();
+
   ArrayRef<LoopInterchangeLegality::InnerReduction> InnerReductions =
       LIL.getInnerReductions();
   if (InnerReductions.size() == 1)
diff --git a/llvm/test/Transforms/LoopInterchange/loop-metadata.ll b/llvm/test/Transforms/LoopInterchange/loop-metadata.ll
index 3e23a345bb9e4e..3a6b03e7130277 100644
--- a/llvm/test/Transforms/LoopInterchange/loop-metadata.ll
+++ b/llvm/test/Transforms/LoopInterchange/loop-metadata.ll
@@ -35,11 +35,11 @@ define void @loop_id(ptr %A) {
 ; CHECK:       [[INNER_HEADER_SPLIT]]:
 ; CHECK-NEXT:    [[TMP0]] = add nuw nsw i64 [[J]], 1
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[TMP0]], 256
-; CHECK-NEXT:    br i1 [[TMP1]], label %[[EXIT:.*]], label %[[INNER_HEADER]], !llvm.loop [[LOOP1:![0-9]+]]
+; CHECK-NEXT:    br i1 [[TMP1]], label %[[EXIT:.*]], label %[[INNER_HEADER]]
 ; CHECK:       [[OUTER_LATCH]]:
 ; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
 ; CHECK-NEXT:    [[EC_I:%.*]] = icmp eq i64 [[I_NEXT]], 256
-; CHECK-NEXT:    br i1 [[EC_I]], label %[[INNER_HEADER_SPLIT]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP3:![0-9]+]]
+; CHECK-NEXT:    br i1 [[EC_I]], label %[[INNER_HEADER_SPLIT]], label %[[OUTER_HEADER]]
 ; CHECK:       [[EXIT]]:
 ; CHECK-NEXT:    ret void
 ;
@@ -97,11 +97,11 @@ define void @branch_weights(ptr %A) {
 ; CHECK:       [[INNER_HEADER_SPLIT]]:
 ; CHECK-NEXT:    [[TMP0]] = add nuw nsw i64 [[J]], 1
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[TMP0]], 64
-; CHECK-NEXT:    br i1 [[TMP1]], label %[[EXIT:.*]], label %[[INNER_HEADER]], !prof [[PROF5:![0-9]+]]
+; CHECK-NEXT:    br i1 [[TMP1]], label %[[EXIT:.*]], label %[[INNER_HEADER]]
 ; CHECK:       [[OUTER_LATCH]]:
 ; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
 ; CHECK-NEXT:    [[EC_I:%.*]] = icmp ne i64 [[I_NEXT]], 128
-; CHECK-NEXT:    br i1 [[EC_I]], label %[[OUTER_HEADER]], label %[[INNER_HEADER_SPLIT]], !prof [[PROF6:![0-9]+]]
+; CHECK-NEXT:    br i1 [[EC_I]], label %[[OUTER_HEADER]], label %[[INNER_HEADER_SPLIT]]
 ; CHECK:       [[EXIT]]:
 ; CHECK-NEXT:    ret void
 ;
@@ -159,11 +159,11 @@ define void @unknown_branch_weights(ptr %A) {
 ; CHECK:       [[INNER_HEADER_SPLIT]]:
 ; CHECK-NEXT:    [[TMP0]] = add nuw nsw i64 [[J]], 1
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[TMP0]], 64
-; CHECK-NEXT:    br i1 [[TMP1]], label %[[EXIT:.*]], label %[[INNER_HEADER]], !prof [[PROF7:![0-9]+]]
+; CHECK-NEXT:    br i1 [[TMP1]], label %[[EXIT:.*]], label %[[INNER_HEADER]]
 ; CHECK:       [[OUTER_LATCH]]:
 ; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
 ; CHECK-NEXT:    [[EC_I:%.*]] = icmp eq i64 [[I_NEXT]], 128
-; CHECK-NEXT:    br i1 [[EC_I]], label %[[INNER_HEADER_SPLIT]], label %[[OUTER_HEADER]], !prof [[PROF8:![0-9]+]]
+; CHECK-NEXT:    br i1 [[EC_I]], label %[[INNER_HEADER_SPLIT]], label %[[OUTER_HEADER]]
 ; CHECK:       [[EXIT]]:
 ; CHECK-NEXT:    ret void
 ;
@@ -221,7 +221,7 @@ define void @branch_weights_on_one_latch(ptr %A) {
 ; CHECK:       [[INNER_HEADER_SPLIT]]:
 ; CHECK-NEXT:    [[TMP0]] = add nuw nsw i64 [[J]], 1
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[TMP0]], 64
-; CHECK-NEXT:    br i1 [[TMP1]], label %[[EXIT:.*]], label %[[INNER_HEADER]], !prof [[PROF5]]
+; CHECK-NEXT:    br i1 [[TMP1]], label %[[EXIT:.*]], label %[[INNER_HEADER]]
 ; CHECK:       [[OUTER_LATCH]]:
 ; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
 ; CHECK-NEXT:    [[EC_I:%.*]] = icmp eq i64 [[I_NEXT]], 128
@@ -261,7 +261,7 @@ exit:
 ;
 define void @profiled_branch_weights(ptr %A) !prof !9 {
 ; CHECK-LABEL: define void @profiled_branch_weights(
-; CHECK-SAME: ptr [[A:%.*]]) !prof [[PROF9:![0-9]+]] {
+; CHECK-SAME: ptr [[A:%.*]]) !prof [[PROF1:![0-9]+]] {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
 ; CHECK-NEXT:    br label %[[INNER_HEADER_PREHEADER:.*]]
 ; CHECK:       [[OUTER_HEADER_PREHEADER:.*]]:
@@ -283,11 +283,11 @@ define void @profiled_branch_weights(ptr %A) !prof !9 {
 ; CHECK:       [[INNER_HEADER_SPLIT]]:
 ; CHECK-NEXT:    [[TMP0]] = add nuw nsw i64 [[J]], 1
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[TMP0]], 64
-; CHECK-NEXT:    br i1 [[TMP1]], label %[[EXIT:.*]], label %[[INNER_HEADER]], !prof [[PROF5]]
+; CHECK-NEXT:    br i1 [[TMP1]], label %[[EXIT:.*]], label %[[INNER_HEADER]], !prof [[PROF2:![0-9]+]]
 ; CHECK:       [[OUTER_LATCH]]:
 ; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
 ; CHECK-NEXT:    [[EC_I:%.*]] = icmp ne i64 [[I_NEXT]], 128
-; CHECK-NEXT:    br i1 [[EC_I]], label %[[OUTER_HEADER]], label %[[INNER_HEADER_SPLIT]], !prof [[PROF6]]
+; CHECK-NEXT:    br i1 [[EC_I]], label %[[OUTER_HEADER]], label %[[INNER_HEADER_SPLIT]], !prof [[PROF2]]
 ; CHECK:       [[EXIT]]:
 ; CHECK-NEXT:    ret void
 ;
@@ -327,13 +327,6 @@ exit:
 !9 = !{!"function_entry_count", i64 100}
 ;.
 ; CHECK: [[ACC_GRP0]] = distinct !{}
-; CHECK: [[LOOP1]] = distinct !{[[LOOP1]], [[META2:![0-9]+]]}
-; CHECK: [[META2]] = !{!"llvm.loop.parallel_accesses", [[ACC_GRP0]]}
-; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META4:![0-9]+]]}
-; CHECK: [[META4]] = !{!"llvm.loop.unroll.disable"}
-; CHECK: [[PROF5]] = !{!"branch_weights", i32 1, i32 63}
-; CHECK: [[PROF6]] = !{!"branch_weights", i32 127, i32 1}
-; CHECK: [[PROF7]] = !{!"unknown", !"test"}
-; CHECK: [[PROF8]] = !{!"branch_weights", i32 1, i32 127}
-; CHECK: [[PROF9]] = !{!"function_entry_count", i64 100}
+; CHECK: [[PROF1]] = !{!"function_entry_count", i64 100}
+; CHECK: [[PROF2]] = !{!"unknown", !"loop-interchange"}
 ;.

>From 661d5a47f15d9096a1b3a2fcbceabf0a9bc8d9a4 Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <ryotaro.kasuga at gmail.com>
Date: Fri, 20 Nov 2026 00:30:47 +0900
Subject: [PATCH 2/2] address review

---
 llvm/lib/Transforms/Scalar/LoopInterchange.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index 6a766ca7831f91..b99829dd0d8917 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -2176,7 +2176,8 @@ void LoopInterchangeTransform::restructureLoops(
 /// 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.
+/// TODO: Preserve the branch weights and the loop attributes that remain valid
+/// after interchange.
 void LoopInterchangeTransform::updateLatchMetadata() {
   auto *OuterLoopLatchBI =
       cast<CondBrInst>(OuterLoop->getLoopLatch()->getTerminator());
@@ -2185,6 +2186,8 @@ void LoopInterchangeTransform::updateLatchMetadata() {
 
   for (CondBrInst *BI : {OuterLoopLatchBI, InnerLoopLatchBI}) {
     BI->setMetadata(LLVMContext::MD_loop, nullptr);
+    // Drop the branch weights. If the function has profile data, mark them as
+    // unknown instead.
     BI->setMetadata(LLVMContext::MD_prof, nullptr);
     setExplicitlyUnknownBranchWeightsIfProfiled(*BI, DEBUG_TYPE);
   }



More information about the llvm-branch-commits mailing list