[llvm-branch-commits] [Support] Use block numbers for LoopInfo BBMap (PR #103400)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Aug 13 12:38:47 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-aarch64

@llvm/pr-subscribers-llvm-support

Author: Alexis Engelke (aengelke)

<details>
<summary>Changes</summary>

Replace the DenseMap from blocks to their innermost loop a vector
indexed by block numbers, when possible.

This requires updating the loop info when blocks are renumbered. This
update is currently implemented by iterating over all loops and their
blocks, as there is no mapping from the previous block number to the
block (as opposed to the dominator tree). This makes the update O(n^2)
in the worst case: a block in a loop with nesting level x will be
considered x times to determine the innermost loop. In practice, it
should be acceptable, though (but probably not in the long run, O(n^2)
algorithms are generally bad).

NB: I'm generally not happy with the way loops are stored. As I think
that there's room for improvement, I don't want to touch the
representation at this point.

I'm also considering to remove the number updating facility in favor of
recomputing the analysis, it natural loop analysis isn't that expensive
and it might give more freedom for data structure design to have a fixed
numbering without needing to worry about numbering changes.


---

Patch is 21.21 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/103400.diff


13 Files Affected:

- (modified) llvm/include/llvm/Support/GenericLoopInfo.h (+107-12) 
- (modified) llvm/include/llvm/Support/GenericLoopInfoImpl.h (+28-9) 
- (modified) llvm/lib/CodeGen/BasicBlockSections.cpp (+6-1) 
- (modified) llvm/lib/CodeGen/BranchFolding.cpp (+4-2) 
- (modified) llvm/lib/CodeGen/EarlyIfConversion.cpp (+2-2) 
- (modified) llvm/lib/CodeGen/MIRSampleProfile.cpp (+3-1) 
- (modified) llvm/lib/CodeGen/MachineBlockPlacement.cpp (+3) 
- (modified) llvm/lib/CodeGen/UnreachableBlockElim.cpp (+2) 
- (modified) llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp (+1-1) 
- (modified) llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp (+4-2) 
- (modified) llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp (+4-2) 
- (modified) llvm/lib/Transforms/IPO/LoopExtractor.cpp (+4-1) 
- (modified) llvm/lib/Transforms/Utils/LoopUtils.cpp (+8-11) 


``````````diff
diff --git a/llvm/include/llvm/Support/GenericLoopInfo.h b/llvm/include/llvm/Support/GenericLoopInfo.h
index d560ca648132c9..3612cddbf15c31 100644
--- a/llvm/include/llvm/Support/GenericLoopInfo.h
+++ b/llvm/include/llvm/Support/GenericLoopInfo.h
@@ -521,7 +521,16 @@ raw_ostream &operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
 
 template <class BlockT, class LoopT> class LoopInfoBase {
   // BBMap - Mapping of basic blocks to the inner most loop they occur in
-  DenseMap<const BlockT *, LoopT *> BBMap;
+  std::conditional_t<GraphHasNodeNumbers<const BlockT *>, SmallVector<LoopT *>,
+                     DenseMap<const BlockT *, LoopT *>>
+      BBMap;
+
+  using ParentT = decltype(std::declval<const BlockT *>()->getParent());
+#if LLVM_ENABLE_ABI_BREAKING_CHECKS
+  mutable ParentT ParentPtr = nullptr;
+  mutable unsigned BlockNumberEpoch;
+#endif
+
   std::vector<LoopT *> TopLevelLoops;
   BumpPtrAllocator LoopAllocator;
 
@@ -539,11 +548,19 @@ template <class BlockT, class LoopT> class LoopInfoBase {
       : BBMap(std::move(Arg.BBMap)),
         TopLevelLoops(std::move(Arg.TopLevelLoops)),
         LoopAllocator(std::move(Arg.LoopAllocator)) {
+#if LLVM_ENABLE_ABI_BREAKING_CHECKS
+    ParentPtr = Arg.ParentPtr;
+    BlockNumberEpoch = Arg.BlockNumberEpoch;
+#endif
     // We have to clear the arguments top level loops as we've taken ownership.
     Arg.TopLevelLoops.clear();
   }
   LoopInfoBase &operator=(LoopInfoBase &&RHS) {
     BBMap = std::move(RHS.BBMap);
+#if LLVM_ENABLE_ABI_BREAKING_CHECKS
+    ParentPtr = RHS.ParentPtr;
+    BlockNumberEpoch = RHS.BlockNumberEpoch;
+#endif
 
     for (auto *L : TopLevelLoops)
       L->~LoopT();
@@ -556,6 +573,9 @@ template <class BlockT, class LoopT> class LoopInfoBase {
 
   void releaseMemory() {
     BBMap.clear();
+#if LLVM_ENABLE_ABI_BREAKING_CHECKS
+    ParentPtr = nullptr;
+#endif
 
     for (auto *L : TopLevelLoops)
       L->~LoopT();
@@ -597,9 +617,38 @@ template <class BlockT, class LoopT> class LoopInfoBase {
   /// reverse program order.
   SmallVector<LoopT *, 4> getLoopsInReverseSiblingPreorder() const;
 
+private:
+  /// Verify that used block numbers are still valid. Initializes the block
+  /// number epoch and parent when no blocks exist so far.
+  void verifyBlockNumberEpoch(ParentT BBParent) const {
+#if LLVM_ENABLE_ABI_BREAKING_CHECKS
+    if constexpr (GraphHasNodeNumbers<BlockT *>) {
+      if (ParentPtr == nullptr)
+        ParentPtr = BBParent;
+      else
+        assert(ParentPtr == BBParent &&
+               "loop info queried with block of other function");
+      if (BBMap.empty())
+        BlockNumberEpoch = GraphTraits<ParentT>::getNumberEpoch(ParentPtr);
+      else
+        assert(BlockNumberEpoch ==
+                   GraphTraits<ParentT>::getNumberEpoch(ParentPtr) &&
+               "loop info used with outdated block numbers");
+    }
+#endif
+  }
+
+public:
   /// Return the inner most loop that BB lives in. If a basic block is in no
   /// loop (for example the entry node), null is returned.
-  LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
+  LoopT *getLoopFor(const BlockT *BB) const {
+    if constexpr (GraphHasNodeNumbers<const BlockT *>) {
+      verifyBlockNumberEpoch(BB->getParent());
+      unsigned Number = GraphTraits<const BlockT *>::getNumber(BB);
+      return Number < BBMap.size() ? BBMap[Number] : nullptr;
+    } else
+      return BBMap.lookup(BB);
+  }
 
   /// Same as getLoopFor.
   const LoopT *operator[](const BlockT *BB) const { return getLoopFor(BB); }
@@ -623,6 +672,30 @@ template <class BlockT, class LoopT> class LoopInfoBase {
   /// Return the top-level loops.
   std::vector<LoopT *> &getTopLevelLoopsVector() { return TopLevelLoops; }
 
+  /// Update block numbers after renumbering. As we don't store the blocks in
+  /// the array, re-extract information by traversing the loop forest in DFS and
+  /// and looking at all blocks. Note that this is O(n^2).
+  template <class BlockT_ = BlockT>
+  std::enable_if_t<GraphHasNodeNumbers<BlockT_ *>, void> updateBlockNumbers() {
+    BBMap.clear();
+    // DFS stack with (it, end).
+    using IteratorT = typename LoopT::iterator;
+    SmallVector<std::pair<IteratorT, IteratorT>> Stack;
+    Stack.push_back(std::make_pair(TopLevelLoops.begin(), TopLevelLoops.end()));
+    while (!Stack.empty()) {
+      auto &Entry = Stack.back();
+      if (Entry.first == Entry.second) {
+        Stack.pop_back();
+        continue;
+      }
+      LoopT *L = *Entry.first++;
+      for (BlockT *BB : L->blocks())
+        changeLoopFor(BB, L);
+      if (L->begin() != L->end())
+        Stack.push_back(std::make_pair(L->begin(), L->end()));
+    }
+  }
+
   /// This removes the specified top-level loop from this loop info object.
   /// The loop is not deleted, as it will presumably be inserted into
   /// another loop.
@@ -637,12 +710,23 @@ template <class BlockT, class LoopT> class LoopInfoBase {
   /// Change the top-level loop that contains BB to the specified loop.
   /// This should be used by transformations that restructure the loop hierarchy
   /// tree.
-  void changeLoopFor(BlockT *BB, LoopT *L) {
-    if (!L) {
-      BBMap.erase(BB);
-      return;
+  void changeLoopFor(const BlockT *BB, LoopT *L) {
+    if constexpr (GraphHasNodeNumbers<const BlockT *>) {
+      verifyBlockNumberEpoch(BB->getParent());
+      unsigned Number = GraphTraits<const BlockT *>::getNumber(BB);
+      if (Number >= BBMap.size()) {
+        unsigned Max = GraphTraits<decltype(BB->getParent())>::getMaxNumber(
+            BB->getParent());
+        BBMap.resize(Number >= Max ? Number + 1 : Max);
+      }
+      BBMap[Number] = L;
+    } else {
+      if (!L) {
+        BBMap.erase(BB);
+        return;
+      }
+      BBMap[BB] = L;
     }
-    BBMap[BB] = L;
   }
 
   /// Replace the specified loop in the top-level loops list with the indicated
@@ -665,12 +749,23 @@ template <class BlockT, class LoopT> class LoopInfoBase {
   /// including all of the Loop objects it is nested in and our mapping from
   /// BasicBlocks to loops.
   void removeBlock(BlockT *BB) {
-    auto I = BBMap.find(BB);
-    if (I != BBMap.end()) {
-      for (LoopT *L = I->second; L; L = L->getParentLoop())
-        L->removeBlockFromLoop(BB);
+    if constexpr (GraphHasNodeNumbers<BlockT *>) {
+      verifyBlockNumberEpoch(BB->getParent());
+      unsigned Number = GraphTraits<BlockT *>::getNumber(BB);
+      if (Number >= BBMap.size())
+        return;
 
-      BBMap.erase(I);
+      for (LoopT *L = BBMap[Number]; L; L = L->getParentLoop())
+        L->removeBlockFromLoop(BB);
+      BBMap[Number] = nullptr;
+    } else {
+      auto I = BBMap.find(BB);
+      if (I != BBMap.end()) {
+        for (LoopT *L = I->second; L; L = L->getParentLoop())
+          L->removeBlockFromLoop(BB);
+
+        BBMap.erase(I);
+      }
     }
   }
 
diff --git a/llvm/include/llvm/Support/GenericLoopInfoImpl.h b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
index d19022729ace32..b7dc79edbf1ece 100644
--- a/llvm/include/llvm/Support/GenericLoopInfoImpl.h
+++ b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
@@ -284,7 +284,7 @@ void LoopBase<BlockT, LoopT>::addBasicBlockToLoop(
   LoopT *L = static_cast<LoopT *>(this);
 
   // Add the loop mapping to the LoopInfo object...
-  LIB.BBMap[NewBB] = L;
+  LIB.changeLoopFor(NewBB, L);
 
   // Add the basic block to this loop and all parent loops...
   while (L) {
@@ -714,14 +714,33 @@ void LoopInfoBase<BlockT, LoopT>::verify(
 
 // Verify that blocks are mapped to valid loops.
 #ifndef NDEBUG
-  for (auto &Entry : BBMap) {
-    const BlockT *BB = Entry.first;
-    LoopT *L = Entry.second;
-    assert(Loops.count(L) && "orphaned loop");
-    assert(L->contains(BB) && "orphaned block");
-    for (LoopT *ChildLoop : *L)
-      assert(!ChildLoop->contains(BB) &&
-             "BBMap should point to the innermost loop containing BB");
+  if constexpr (GraphHasNodeNumbers<const BlockT *>) {
+    for (auto It : enumerate(BBMap)) {
+      LoopT *L = It.value();
+      unsigned Number = It.index();
+      if (!L)
+        continue;
+      assert(Loops.count(L) && "orphaned loop");
+      // We have no way to map block numbers back to blocks, so find it.
+      auto BBIt = find_if(L->Blocks, [&Number](BlockT *BB) {
+        return GraphTraits<BlockT *>::getNumber(BB) == Number;
+      });
+      BlockT *BB = BBIt != L->Blocks.end() ? *BBIt : nullptr;
+      assert(BB && "orphaned block");
+      for (LoopT *ChildLoop : *L)
+        assert(!ChildLoop->contains(BB) &&
+               "BBMap should point to the innermost loop containing BB");
+    }
+  } else {
+    for (auto &Entry : BBMap) {
+      const BlockT *BB = Entry.first;
+      LoopT *L = Entry.second;
+      assert(Loops.count(L) && "orphaned loop");
+      assert(L->contains(BB) && "orphaned block");
+      for (LoopT *ChildLoop : *L)
+        assert(!ChildLoop->contains(BB) &&
+               "BBMap should point to the innermost loop containing BB");
+    }
   }
 
   // Recompute LoopInfo to verify loops structure.
diff --git a/llvm/lib/CodeGen/BasicBlockSections.cpp b/llvm/lib/CodeGen/BasicBlockSections.cpp
index 0071284c862099..ee9a8a17d47f28 100644
--- a/llvm/lib/CodeGen/BasicBlockSections.cpp
+++ b/llvm/lib/CodeGen/BasicBlockSections.cpp
@@ -75,6 +75,7 @@
 #include "llvm/CodeGen/MachineDominators.h"
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineLoopInfo.h"
 #include "llvm/CodeGen/MachinePostDominators.h"
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/CodeGen/TargetInstrInfo.h"
@@ -396,11 +397,14 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
   // Handle basic block address map after basic block sections are finalized.
   auto R2 = handleBBAddrMap(MF);
 
-  // We renumber blocks, so update the dominator tree we want to preserve.
+  // We renumber blocks, so update the dominator tree and the loop info we want
+  // to preserve.
   if (auto *WP = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>())
     WP->getDomTree().updateBlockNumbers();
   if (auto *WP = getAnalysisIfAvailable<MachinePostDominatorTreeWrapperPass>())
     WP->getPostDomTree().updateBlockNumbers();
+  if (auto *WP = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>())
+    WP->getLI().updateBlockNumbers();
 
   return R1 || R2;
 }
@@ -410,6 +414,7 @@ void BasicBlockSections::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.addRequired<BasicBlockSectionsProfileReaderWrapperPass>();
   AU.addUsedIfAvailable<MachineDominatorTreeWrapperPass>();
   AU.addUsedIfAvailable<MachinePostDominatorTreeWrapperPass>();
+  AU.addUsedIfAvailable<MachineLoopInfoWrapperPass>();
   MachineFunctionPass::getAnalysisUsage(AU);
 }
 
diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 1dc278586f1178..31cf148d0ef924 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -171,10 +171,10 @@ void BranchFolder::RemoveDeadBlock(MachineBasicBlock *MBB) {
       MF->eraseCallSiteInfo(&MI);
 
   // Remove the block.
-  MF->erase(MBB);
-  EHScopeMembership.erase(MBB);
   if (MLI)
     MLI->removeBlock(MBB);
+  MF->erase(MBB);
+  EHScopeMembership.erase(MBB);
 }
 
 bool BranchFolder::OptimizeFunction(MachineFunction &MF,
@@ -1214,6 +1214,8 @@ bool BranchFolder::OptimizeBranches(MachineFunction &MF) {
   MF.RenumberBlocks();
   // Renumbering blocks alters EH scope membership, recalculate it.
   EHScopeMembership = getEHScopeMembership(MF);
+  if (MLI)
+    MLI->updateBlockNumbers();
 
   for (MachineBasicBlock &MBB :
        llvm::make_early_inc_range(llvm::drop_begin(MF))) {
diff --git a/llvm/lib/CodeGen/EarlyIfConversion.cpp b/llvm/lib/CodeGen/EarlyIfConversion.cpp
index 0de8112fb72c89..8a1b5e0cc501ff 100644
--- a/llvm/lib/CodeGen/EarlyIfConversion.cpp
+++ b/llvm/lib/CodeGen/EarlyIfConversion.cpp
@@ -1070,9 +1070,9 @@ bool EarlyIfConverter::tryConvertIf(MachineBasicBlock *MBB) {
     IfConv.convertIf(RemoveBlocks);
     Changed = true;
     updateDomTree(DomTree, IfConv, RemoveBlocks);
+    updateLoops(Loops, RemoveBlocks);
     for (MachineBasicBlock *MBB : RemoveBlocks)
       MBB->eraseFromParent();
-    updateLoops(Loops, RemoveBlocks);
   }
   return Changed;
 }
@@ -1210,9 +1210,9 @@ bool EarlyIfPredicator::tryConvertIf(MachineBasicBlock *MBB) {
     IfConv.convertIf(RemoveBlocks, /*Predicate*/ true);
     Changed = true;
     updateDomTree(DomTree, IfConv, RemoveBlocks);
+    updateLoops(Loops, RemoveBlocks);
     for (MachineBasicBlock *MBB : RemoveBlocks)
       MBB->eraseFromParent();
-    updateLoops(Loops, RemoveBlocks);
   }
   return Changed;
 }
diff --git a/llvm/lib/CodeGen/MIRSampleProfile.cpp b/llvm/lib/CodeGen/MIRSampleProfile.cpp
index 23db09b89599aa..d0c2cc008526ec 100644
--- a/llvm/lib/CodeGen/MIRSampleProfile.cpp
+++ b/llvm/lib/CodeGen/MIRSampleProfile.cpp
@@ -369,13 +369,15 @@ bool MIRProfileLoaderPass::runOnMachineFunction(MachineFunction &MF) {
   auto *MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
   auto *MPDT =
       &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
+  auto *MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
 
   MF.RenumberBlocks();
   MDT->updateBlockNumbers();
   MPDT->updateBlockNumbers();
+  MLI->updateBlockNumbers();
 
   MIRSampleLoader->setInitVals(
-      MDT, MPDT, &getAnalysis<MachineLoopInfoWrapperPass>().getLI(), MBFI,
+      MDT, MPDT, MLI, MBFI,
       &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE());
 
   if (ViewBFIBefore && ViewBlockLayoutWithBFI != GVDT_None &&
diff --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
index be783bc4e29738..254508d0d15fde 100644
--- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp
+++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
@@ -3654,6 +3654,9 @@ void MachineBlockPlacement::assignBlockOrder(
   // TODO: move this to the point where the dominator tree is actually
   // invalidated (i.e., where blocks are removed without updating the domtree).
   MPDT = nullptr;
+  // Likewise, make an attempt to keep the loop information valid, even if the
+  // analysis is possibly outdated...
+  MLI->updateBlockNumbers();
 
   bool HasChanges = false;
   for (size_t I = 0; I < NewBlockOrder.size(); I++) {
diff --git a/llvm/lib/CodeGen/UnreachableBlockElim.cpp b/llvm/lib/CodeGen/UnreachableBlockElim.cpp
index 6e3b69b4b96116..54bbd0ce482b42 100644
--- a/llvm/lib/CodeGen/UnreachableBlockElim.cpp
+++ b/llvm/lib/CodeGen/UnreachableBlockElim.cpp
@@ -197,6 +197,8 @@ bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
   F.RenumberBlocks();
   if (MDT)
     MDT->updateBlockNumbers();
+  if (MLI)
+    MLI->updateBlockNumbers();
 
   return (!DeadBlocks.empty() || ModifiedPHI);
 }
diff --git a/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp b/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp
index 9669a393bc2b94..2f77b0dac3b59b 100644
--- a/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp
@@ -917,9 +917,9 @@ bool AArch64ConditionalCompares::tryConvert(MachineBasicBlock *MBB) {
     CmpConv.convert(RemovedBlocks);
     Changed = true;
     updateDomTree(RemovedBlocks);
+    updateLoops(RemovedBlocks);
     for (MachineBasicBlock *MBB : RemovedBlocks)
       MBB->eraseFromParent();
-    updateLoops(RemovedBlocks);
   }
   return Changed;
 }
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp
index 04eada18ef0d01..be33d720729438 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp
@@ -184,13 +184,14 @@ struct Entry {
 /// interrupted by blocks not dominated by their header.
 /// TODO: There are many opportunities for improving the heuristics here.
 /// Explore them.
-static void sortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI,
+static void sortBlocks(MachineFunction &MF, MachineLoopInfo &MLI,
                        const WebAssemblyExceptionInfo &WEI,
                        MachineDominatorTree &MDT) {
   // Remember original layout ordering, so we can update terminators after
   // reordering to point to the original layout successor.
   MF.RenumberBlocks();
   MDT.updateBlockNumbers();
+  MLI.updateBlockNumbers();
 
   // Prepare for a topological sort: Record the number of predecessors each
   // block has, ignoring loop backedges.
@@ -332,6 +333,7 @@ static void sortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI,
   assert(Entries.empty() && "Active sort region list not finished");
   MF.RenumberBlocks();
   MDT.updateBlockNumbers();
+  MLI.updateBlockNumbers();
 
 #ifndef NDEBUG
   SmallSetVector<const SortRegion *, 8> OnStack;
@@ -387,7 +389,7 @@ bool WebAssemblyCFGSort::runOnMachineFunction(MachineFunction &MF) {
                        "********** Function: "
                     << MF.getName() << '\n');
 
-  const auto &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
+  auto &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
   const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
   auto &MDT = getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
   // Liveness is not tracked for VALUE_STACK physreg.
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
index c7001ef2b33e62..f1c642d8cfcd92 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
@@ -46,6 +46,7 @@ STATISTIC(NumCatchUnwindMismatches, "Number of catch unwind mismatches found");
 namespace {
 class WebAssemblyCFGStackify final : public MachineFunctionPass {
   MachineDominatorTree *MDT;
+  MachineLoopInfo *MLI;
 
   StringRef getPassName() const override { return "WebAssembly CFG Stackify"; }
 
@@ -468,9 +469,8 @@ void WebAssemblyCFGStackify::placeTryMarker(MachineBasicBlock &MBB) {
   MachineFunction &MF = *MBB.getParent();
   auto &MDT = getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
-  const auto &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
   const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
-  SortRegionInfo SRI(MLI, WEI);
+  SortRegionInfo SRI(*MLI, WEI);
   const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
 
   // Compute the nearest common dominator of all unwind predecessors
@@ -1441,6 +1441,7 @@ void WebAssemblyCFGStackify::recalculateScopeTops(MachineFunction &MF) {
   // created and inserted during fixing unwind mismatches.
   MF.RenumberBlocks();
   MDT->updateBlockNumbers();
+  MLI->updateBlockNumbers();
   ScopeTops.clear();
   ScopeTops.resize(MF.getNumBlockIDs());
   for (auto &MBB : reverse(MF)) {
@@ -1744,6 +1745,7 @@ bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction &MF) {
                     << MF.getName() << '\n');
   const MCAsmInfo *MCAI = MF.getTarget().getMCAsmInfo();
   MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
+  MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
 
   releaseMemory();
 
diff --git a/llvm/lib/Transforms/IPO/LoopExtractor.cpp b/llvm/lib/Transforms/IPO/LoopExtractor.cpp
index 24b3c425180767..1db3ff8e4bfd5e 100644
--- a/llvm/lib/Transforms/IPO/LoopExtractor.cpp
+++ b/llvm/lib/Transforms/IPO/LoopExtractor.cpp
@@ -242,8 +242,11 @@ bool LoopExtractor::extractLoop(Loop *L, LoopInfo &LI, DominatorTree &DT) {
   AssumptionCache *AC = LookupAssumptionCache(Func);
   CodeExtractorAnalysisCache CEAC(Func);
   CodeExtractor Extractor(L->getBlocks(), &DT, false, nullptr, nullptr, AC);
-  if (Extractor.extractCodeRegion(CEAC)) {
+  if (Extractor.isEligible()) {
+    // Remove loop while blocks are still in the current function
     LI.erase(L);
+    [[maybe_unused]] Function *ExtrF = Extractor.extractCodeRegion(CEAC);
+    assert(ExtrF && "CodeExtractor didn't extact eligible loop");
     --NumLoops;
     ++NumExtracted;
     return true;
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index f1f2d522f1cbaa..ef03a13f1793eb 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -689,20 +689,...
[truncated]

``````````

</details>


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


More information about the llvm-branch-commits mailing list