[llvm-branch-commits] [llvm] [MustExecute][NFC] Look up the blocks of contexts by their number (PR #243918)

Pablo Reble via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Nov 18 09:51:18 PST 2026


https://github.com/reble updated https://github.com/llvm/llvm-project/pull/243918

>From f14accd72c8b466c5eac98c765d83204f1f6588e Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Mon, 12 Oct 2026 08:21:28 -0700
Subject: [PATCH 1/4] MustExecute: Look up the blocks of contexts by their
 number

isInContextOf looked up the block of every queried instruction in a hash
map of the forward context and a hash set of the backward context, which
was a few percent of the full Attributor on large functions. The blocks of
a context are all included except for a prefix of the last block of a
forward context, so keep a bit vector indexed by block number instead,
and the end of the last block. NFC.

CTMark instructions:u with -attributor-enable=full:
  O3: -0.74% (sqlite3 -2.22%)
  ThinLTO: -1.27% (tramp3d-v4 -4.62%, sqlite3 -2.73%)

Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
---
 llvm/include/llvm/Analysis/MustExecute.h | 41 +++++++++++++++---------
 llvm/lib/Analysis/MustExecute.cpp        | 37 ++++++++++++---------
 2 files changed, 47 insertions(+), 31 deletions(-)

diff --git a/llvm/include/llvm/Analysis/MustExecute.h b/llvm/include/llvm/Analysis/MustExecute.h
index cd35bd99e3b73..b933e813f0f26 100644
--- a/llvm/include/llvm/Analysis/MustExecute.h
+++ b/llvm/include/llvm/Analysis/MustExecute.h
@@ -23,9 +23,9 @@
 #ifndef LLVM_ANALYSIS_MUSTEXECUTE_H
 #define LLVM_ANALYSIS_MUSTEXECUTE_H
 
+#include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseSet.h"
-#include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Analysis/InstructionPrecedenceTracking.h"
@@ -577,30 +577,39 @@ struct MustBeExecutedContextExplorer {
   DenseMap<const BasicBlock *, SmallVector<const Instruction *, 2>>
       NonTransferringMap;
 
-  /// Map from blocks to the blocks in the forward context after their
-  /// terminator. Each block maps to the last instruction in the context, or
-  /// null if all of its instructions are in the context.
-  using ForwardContextTy = MapVector<const BasicBlock *, const Instruction *>;
-  DenseMap<const BasicBlock *, std::unique_ptr<ForwardContextTy>>
-      ForwardContextMap;
-
-  /// Map from blocks to the blocks in the backward context before their first
-  /// instruction. All instructions of these blocks are in the context.
-  using BackwardContextTy = SmallSetVector<const BasicBlock *, 8>;
-  DenseMap<const BasicBlock *, std::unique_ptr<BackwardContextTy>>
+  /// The blocks in a forward or backward context, in the order they are
+  /// explored. All instructions of these blocks are in the context, except
+  /// that a forward context ends at LastStop in its last block, if set.
+  struct BlockContext {
+    SmallVector<const BasicBlock *, 8> Blocks;
+    /// The blocks by their number, to look them up quickly.
+    BitVector Contains;
+    const Instruction *LastStop = nullptr;
+
+    bool contains(const BasicBlock *BB) const {
+      unsigned Number = BB->getNumber();
+      return Number < Contains.size() && Contains.test(Number);
+    }
+  };
+
+  /// Map from blocks to the forward context after their terminator.
+  DenseMap<const BasicBlock *, std::unique_ptr<BlockContext>> ForwardContextMap;
+
+  /// Map from blocks to the backward context before their first instruction.
+  DenseMap<const BasicBlock *, std::unique_ptr<BlockContext>>
       BackwardContextMap;
 
   /// The last program point queried by isInContextOf and its summaries.
   const Instruction *LastPP = nullptr;
   const Instruction *LastForwardStop = nullptr;
-  const ForwardContextTy *LastForwardContext = nullptr;
-  const BackwardContextTy *LastBackwardContext = nullptr;
+  const BlockContext *LastForwardContext = nullptr;
+  const BlockContext *LastBackwardContext = nullptr;
 
   /// Return the forward context after the terminator of \p BB.
-  const ForwardContextTy &getForwardContext(const BasicBlock *BB);
+  const BlockContext &getForwardContext(const BasicBlock *BB);
 
   /// Return the backward context before the first instruction of \p BB.
-  const BackwardContextTy &getBackwardContext(const BasicBlock *BB);
+  const BlockContext &getBackwardContext(const BasicBlock *BB);
 
   /// A unique end iterator.
   MustBeExecutedIterator EndIterator;
diff --git a/llvm/lib/Analysis/MustExecute.cpp b/llvm/lib/Analysis/MustExecute.cpp
index 92f5f6b40310a..d741355f43022 100644
--- a/llvm/lib/Analysis/MustExecute.cpp
+++ b/llvm/lib/Analysis/MustExecute.cpp
@@ -765,29 +765,36 @@ MustBeExecutedContextExplorer::getBackwardNextBlock(const BasicBlock *BB) {
   return It->second;
 }
 
-const MustBeExecutedContextExplorer::ForwardContextTy &
+const MustBeExecutedContextExplorer::BlockContext &
 MustBeExecutedContextExplorer::getForwardContext(const BasicBlock *BB) {
   auto &Context = ForwardContextMap[BB];
   if (!Context) {
-    Context = std::make_unique<ForwardContextTy>();
+    Context = std::make_unique<BlockContext>();
+    Context->Contains.resize(BB->getParent()->getMaxBlockNumber());
     for (const BasicBlock *CurBB = getForwardNextBlock(BB); CurBB;) {
-      const Instruction *Stop = getForwardStop(&CurBB->front());
-      if (!Context->try_emplace(CurBB, Stop).second)
+      if (Context->contains(CurBB))
         break;
-      CurBB = Stop ? nullptr : getForwardNextBlock(CurBB);
+      Context->Blocks.push_back(CurBB);
+      Context->Contains.set(CurBB->getNumber());
+      Context->LastStop = getForwardStop(&CurBB->front());
+      CurBB = Context->LastStop ? nullptr : getForwardNextBlock(CurBB);
     }
   }
   return *Context;
 }
 
-const MustBeExecutedContextExplorer::BackwardContextTy &
+const MustBeExecutedContextExplorer::BlockContext &
 MustBeExecutedContextExplorer::getBackwardContext(const BasicBlock *BB) {
   auto &Context = BackwardContextMap[BB];
   if (!Context) {
-    Context = std::make_unique<BackwardContextTy>();
+    Context = std::make_unique<BlockContext>();
+    Context->Contains.resize(BB->getParent()->getMaxBlockNumber());
     for (const BasicBlock *CurBB = getBackwardNextBlock(BB);
-         CurBB && Context->insert(CurBB);)
-      CurBB = getBackwardNextBlock(CurBB);
+         CurBB && !Context->contains(CurBB);
+         CurBB = getBackwardNextBlock(CurBB)) {
+      Context->Blocks.push_back(CurBB);
+      Context->Contains.set(CurBB->getNumber());
+    }
   }
   return *Context;
 }
@@ -822,10 +829,10 @@ bool MustBeExecutedContextExplorer::isInContextOf(const Instruction *I,
     const Instruction *Stop = LastForwardStop;
     if (IBB == PPBB && PP->comesBefore(I) && (!Stop || !Stop->comesBefore(I)))
       return true;
-    if (LastForwardContext) {
-      auto It = LastForwardContext->find(IBB);
-      if (It != LastForwardContext->end() &&
-          (!It->second || !It->second->comesBefore(I)))
+    if (LastForwardContext && LastForwardContext->contains(IBB)) {
+      const Instruction *LastStop = LastForwardContext->LastStop;
+      if (!LastStop || IBB != LastStop->getParent() ||
+          !LastStop->comesBefore(I))
         return true;
     }
   }
@@ -858,13 +865,13 @@ bool MustBeExecutedContextExplorer::checkForAllTerminatorsInContext(
   if (!ExploreInterBlock)
     return true;
   if (ExploreCFGForward && !getForwardStop(PP)) {
-    for (const auto &[BB, Stop] : getForwardContext(PPBB)) {
+    for (const BasicBlock *BB : getForwardContext(PPBB).Blocks) {
       if (!CheckBlock(BB))
         return false;
     }
   }
   if (ExploreCFGBackward) {
-    for (const BasicBlock *BB : getBackwardContext(PPBB)) {
+    for (const BasicBlock *BB : getBackwardContext(PPBB).Blocks) {
       if (!CheckBlock(BB))
         return false;
     }

>From a9bafcc36558a27f3ae63e02b2cab757da515f8d Mon Sep 17 00:00:00 2001
From: Pablo Reble <pablo.reble at amd.com>
Date: Thu, 12 Nov 2026 13:52:05 -0600
Subject: [PATCH 2/4] [MustExecute] Fix the memory regression of block number
 contexts

The previous commit gave every block context a bit vector with a bit for
every block of the function, which takes memory quadratic in the number
of blocks: +110 MB for a switch with 30000 cases. Use a set until the
bit vector is not larger than the list of blocks of the context.

Also assert that the block numbers are not outdated, as DominatorTree
does, and drop the unused SetVector include.

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
 llvm/include/llvm/Analysis/MustExecute.h | 37 ++++++++++++++++++++++--
 llvm/lib/Analysis/MustExecute.cpp        | 18 ++++--------
 2 files changed, 40 insertions(+), 15 deletions(-)

diff --git a/llvm/include/llvm/Analysis/MustExecute.h b/llvm/include/llvm/Analysis/MustExecute.h
index b933e813f0f26..79cc3a7a6dc59 100644
--- a/llvm/include/llvm/Analysis/MustExecute.h
+++ b/llvm/include/llvm/Analysis/MustExecute.h
@@ -26,12 +26,13 @@
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseSet.h"
-#include "llvm/ADT/SetVector.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Analysis/InstructionPrecedenceTracking.h"
 #include "llvm/IR/EHPersonalities.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Support/Compiler.h"
+#include <climits>
 
 namespace llvm {
 
@@ -582,14 +583,46 @@ struct MustBeExecutedContextExplorer {
   /// that a forward context ends at LastStop in its last block, if set.
   struct BlockContext {
     SmallVector<const BasicBlock *, 8> Blocks;
-    /// The blocks by their number, to look them up quickly.
+    /// The blocks by their number, to look them up quickly. To keep contexts
+    /// of few blocks small in functions with many blocks, this is only used
+    /// once it is not larger than Blocks, and BlockSet is used until then.
     BitVector Contains;
+    /// The block number epoch of the function when Contains was built.
+    unsigned BlockNumberEpoch = 0;
+    SmallPtrSet<const BasicBlock *, 8> BlockSet;
     const Instruction *LastStop = nullptr;
 
     bool contains(const BasicBlock *BB) const {
+      if (Contains.empty())
+        return BlockSet.contains(BB);
+      assert(BlockNumberEpoch == BB->getParent()->getBlockNumberEpoch() &&
+             "must-be-executed context used with outdated block numbers");
       unsigned Number = BB->getNumber();
       return Number < Contains.size() && Contains.test(Number);
     }
+
+    /// Add \p BB to the context. Return false if it is already in it.
+    bool insert(const BasicBlock *BB) {
+      if (!Contains.empty()) {
+        if (contains(BB))
+          return false;
+        Contains.set(BB->getNumber());
+        Blocks.push_back(BB);
+        return true;
+      }
+      if (!BlockSet.insert(BB).second)
+        return false;
+      Blocks.push_back(BB);
+      unsigned MaxNumber = BB->getParent()->getMaxBlockNumber();
+      if (Blocks.size() * sizeof(const BasicBlock *) * CHAR_BIT < MaxNumber)
+        return true;
+      BlockNumberEpoch = BB->getParent()->getBlockNumberEpoch();
+      Contains.resize(MaxNumber);
+      for (const BasicBlock *Block : Blocks)
+        Contains.set(Block->getNumber());
+      BlockSet = {};
+      return true;
+    }
   };
 
   /// Map from blocks to the forward context after their terminator.
diff --git a/llvm/lib/Analysis/MustExecute.cpp b/llvm/lib/Analysis/MustExecute.cpp
index d741355f43022..757f96e3e20a2 100644
--- a/llvm/lib/Analysis/MustExecute.cpp
+++ b/llvm/lib/Analysis/MustExecute.cpp
@@ -770,12 +770,8 @@ MustBeExecutedContextExplorer::getForwardContext(const BasicBlock *BB) {
   auto &Context = ForwardContextMap[BB];
   if (!Context) {
     Context = std::make_unique<BlockContext>();
-    Context->Contains.resize(BB->getParent()->getMaxBlockNumber());
-    for (const BasicBlock *CurBB = getForwardNextBlock(BB); CurBB;) {
-      if (Context->contains(CurBB))
-        break;
-      Context->Blocks.push_back(CurBB);
-      Context->Contains.set(CurBB->getNumber());
+    for (const BasicBlock *CurBB = getForwardNextBlock(BB);
+         CurBB && Context->insert(CurBB);) {
       Context->LastStop = getForwardStop(&CurBB->front());
       CurBB = Context->LastStop ? nullptr : getForwardNextBlock(CurBB);
     }
@@ -788,13 +784,9 @@ MustBeExecutedContextExplorer::getBackwardContext(const BasicBlock *BB) {
   auto &Context = BackwardContextMap[BB];
   if (!Context) {
     Context = std::make_unique<BlockContext>();
-    Context->Contains.resize(BB->getParent()->getMaxBlockNumber());
-    for (const BasicBlock *CurBB = getBackwardNextBlock(BB);
-         CurBB && !Context->contains(CurBB);
-         CurBB = getBackwardNextBlock(CurBB)) {
-      Context->Blocks.push_back(CurBB);
-      Context->Contains.set(CurBB->getNumber());
-    }
+    const BasicBlock *CurBB = getBackwardNextBlock(BB);
+    while (CurBB && Context->insert(CurBB))
+      CurBB = getBackwardNextBlock(CurBB);
   }
   return *Context;
 }

>From 8a368f347ca839cba48d3a349cdbdad9df3ae785 Mon Sep 17 00:00:00 2001
From: Pablo Reble <pablo.reble at amd.com>
Date: Wed, 18 Nov 2026 11:07:42 -0600
Subject: [PATCH 3/4] [MustExecute] Define the block context helpers out of
 line

They use BasicBlock, which MustExecute.h only declares, so the header
did not compile without precompiled headers.

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
 llvm/include/llvm/Analysis/MustExecute.h | 32 ++---------------------
 llvm/lib/Analysis/MustExecute.cpp        | 33 ++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/llvm/include/llvm/Analysis/MustExecute.h b/llvm/include/llvm/Analysis/MustExecute.h
index 79cc3a7a6dc59..03d603ade8436 100644
--- a/llvm/include/llvm/Analysis/MustExecute.h
+++ b/llvm/include/llvm/Analysis/MustExecute.h
@@ -32,7 +32,6 @@
 #include "llvm/IR/EHPersonalities.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Support/Compiler.h"
-#include <climits>
 
 namespace llvm {
 
@@ -592,37 +591,10 @@ struct MustBeExecutedContextExplorer {
     SmallPtrSet<const BasicBlock *, 8> BlockSet;
     const Instruction *LastStop = nullptr;
 
-    bool contains(const BasicBlock *BB) const {
-      if (Contains.empty())
-        return BlockSet.contains(BB);
-      assert(BlockNumberEpoch == BB->getParent()->getBlockNumberEpoch() &&
-             "must-be-executed context used with outdated block numbers");
-      unsigned Number = BB->getNumber();
-      return Number < Contains.size() && Contains.test(Number);
-    }
+    bool contains(const BasicBlock *BB) const;
 
     /// Add \p BB to the context. Return false if it is already in it.
-    bool insert(const BasicBlock *BB) {
-      if (!Contains.empty()) {
-        if (contains(BB))
-          return false;
-        Contains.set(BB->getNumber());
-        Blocks.push_back(BB);
-        return true;
-      }
-      if (!BlockSet.insert(BB).second)
-        return false;
-      Blocks.push_back(BB);
-      unsigned MaxNumber = BB->getParent()->getMaxBlockNumber();
-      if (Blocks.size() * sizeof(const BasicBlock *) * CHAR_BIT < MaxNumber)
-        return true;
-      BlockNumberEpoch = BB->getParent()->getBlockNumberEpoch();
-      Contains.resize(MaxNumber);
-      for (const BasicBlock *Block : Blocks)
-        Contains.set(Block->getNumber());
-      BlockSet = {};
-      return true;
-    }
+    bool insert(const BasicBlock *BB);
   };
 
   /// Map from blocks to the forward context after their terminator.
diff --git a/llvm/lib/Analysis/MustExecute.cpp b/llvm/lib/Analysis/MustExecute.cpp
index 757f96e3e20a2..6cca838ffdb98 100644
--- a/llvm/lib/Analysis/MustExecute.cpp
+++ b/llvm/lib/Analysis/MustExecute.cpp
@@ -22,6 +22,7 @@
 #include "llvm/IR/PassManager.h"
 #include "llvm/Support/FormattedStream.h"
 #include "llvm/Support/raw_ostream.h"
+#include <climits>
 
 using namespace llvm;
 
@@ -765,6 +766,38 @@ MustBeExecutedContextExplorer::getBackwardNextBlock(const BasicBlock *BB) {
   return It->second;
 }
 
+bool MustBeExecutedContextExplorer::BlockContext::contains(
+    const BasicBlock *BB) const {
+  if (Contains.empty())
+    return BlockSet.contains(BB);
+  assert(BlockNumberEpoch == BB->getParent()->getBlockNumberEpoch() &&
+         "must-be-executed context used with outdated block numbers");
+  unsigned Number = BB->getNumber();
+  return Number < Contains.size() && Contains.test(Number);
+}
+
+bool MustBeExecutedContextExplorer::BlockContext::insert(const BasicBlock *BB) {
+  if (!Contains.empty()) {
+    if (contains(BB))
+      return false;
+    Contains.set(BB->getNumber());
+    Blocks.push_back(BB);
+    return true;
+  }
+  if (!BlockSet.insert(BB).second)
+    return false;
+  Blocks.push_back(BB);
+  unsigned MaxNumber = BB->getParent()->getMaxBlockNumber();
+  if (Blocks.size() * sizeof(const BasicBlock *) * CHAR_BIT < MaxNumber)
+    return true;
+  BlockNumberEpoch = BB->getParent()->getBlockNumberEpoch();
+  Contains.resize(MaxNumber);
+  for (const BasicBlock *Block : Blocks)
+    Contains.set(Block->getNumber());
+  BlockSet = {};
+  return true;
+}
+
 const MustBeExecutedContextExplorer::BlockContext &
 MustBeExecutedContextExplorer::getForwardContext(const BasicBlock *BB) {
   auto &Context = ForwardContextMap[BB];

>From 7d4b151300160a8b785977416f2d93647dd13b35 Mon Sep 17 00:00:00 2001
From: Pablo Reble <pablo.reble at amd.com>
Date: Wed, 18 Nov 2026 11:42:03 -0600
Subject: [PATCH 4/4] [MustExecute] Annotate the block context helpers with
 LLVM_ABI

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
 llvm/include/llvm/Analysis/MustExecute.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Analysis/MustExecute.h b/llvm/include/llvm/Analysis/MustExecute.h
index 03d603ade8436..5657830ff2985 100644
--- a/llvm/include/llvm/Analysis/MustExecute.h
+++ b/llvm/include/llvm/Analysis/MustExecute.h
@@ -591,10 +591,10 @@ struct MustBeExecutedContextExplorer {
     SmallPtrSet<const BasicBlock *, 8> BlockSet;
     const Instruction *LastStop = nullptr;
 
-    bool contains(const BasicBlock *BB) const;
+    LLVM_ABI bool contains(const BasicBlock *BB) const;
 
     /// Add \p BB to the context. Return false if it is already in it.
-    bool insert(const BasicBlock *BB);
+    LLVM_ABI bool insert(const BasicBlock *BB);
   };
 
   /// Map from blocks to the forward context after their terminator.



More information about the llvm-branch-commits mailing list