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

Dark Steve via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 15 22:19:49 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()) {
+      MachineLoop *Child = Loop->removeChildLoop(std::prev(Loop->end()));
----------------
PrasoonMishra wrote:

Erasing from back of a vector is cheaper whereas erasing from the front will also lead to shifting which will increase the time taken to complete this operation.

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


More information about the llvm-commits mailing list