[llvm] [AMDGPU] Teach SIPreEmitPeephole pass to preserve MachineLoopInfo (PR #178868)

Carl Ritson via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 15 22:05:56 PST 2026


================
@@ -101,6 +113,50 @@ char SIPreEmitPeepholeLegacy::ID = 0;
 
 char &llvm::SIPreEmitPeepholeID = SIPreEmitPeepholeLegacy::ID;
 
+void SIPreEmitPeephole::updateMLIBeforeRemovingEdge(
+    MachineBasicBlock *From, MachineBasicBlock *To) const {
+  if (!MLI)
+    return;
+
+  // Only handle back-edges: To must be a loop header with From inside the loop.
+  MachineLoop *Loop = MLI->getLoopFor(To);
+  if (!Loop || Loop->getHeader() != To || !Loop->contains(From))
+    return;
+
+  // Count back-edges
+  unsigned BackEdgeCount = 0;
+  for (MachineBasicBlock *Pred : To->predecessors()) {
+    if (Loop->contains(Pred))
+      BackEdgeCount++;
+  }
+
+  if (BackEdgeCount <= 1) {
+    MachineLoop *ParentLoop = Loop->getParentLoop();
+
+    // Re-map blocks directly owned by this loop to the parent.
+    for (MachineBasicBlock *BB : Loop->blocks()) {
+      if (MLI->getLoopFor(BB) == Loop)
+        MLI->changeLoopFor(BB, ParentLoop);
+    }
+
+    // Reparent all child loops.
+    while (!Loop->isInnermost()) {
----------------
perlfu wrote:

I assume this is structured this way because you cannot maintain an iterator into the subloops while updating them? Even with `make_early_inc_range()`?

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


More information about the llvm-commits mailing list