[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:43 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()) {
----------------
PrasoonMishra wrote:
Yes , you are correct. `removeChildLoop` invalidates the iterator since it modifies the underlying SubLoops vector. `make_early_inc_range()` would work for iterator-based containers that support stable increment-before-erase, but SubLoops is a std::vector, so erasing from the middle shifts all subsequent elements and invalidates iterators. The `while (!isInnermost())` pattern avoids this entirely.
This logic is somewhat taken from` LoopInfo::erase()` which exists for the IR-level` LoopInf`o but not for `MachineLoopInfo`.
https://github.com/llvm/llvm-project/pull/178868
More information about the llvm-commits
mailing list