[clang] c9680c4 - [LifetimeSafety] Never destroy the dataflow states (#248628)

via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 5 13:47:57 PST 2026


Author: Gábor Horváth
Date: 2026-12-05T22:47:52+01:00
New Revision: c9680c4c01ff5e4c296f1ebca9e4b81f5b1b1862

URL: https://github.com/llvm/llvm-project/commit/c9680c4c01ff5e4c296f1ebca9e4b81f5b1b1862
DIFF: https://github.com/llvm/llvm-project/commit/c9680c4c01ff5e4c296f1ebca9e4b81f5b1b1862.diff

LOG: [LifetimeSafety] Never destroy the dataflow states (#248628)

Added: 
    

Modified: 
    clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h
    clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
    clang/lib/Analysis/LifetimeSafety/Dataflow.h

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h
index 7d32db996b356e..2ac380c13de0c2 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h
@@ -25,6 +25,7 @@
 #include "llvm/ADT/PointerUnion.h"
 #include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/Allocator.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cstdint>
@@ -505,6 +506,9 @@ class FactManager {
 
   unsigned getNumFacts() const { return NextFactID.Value; }
 
+  /// The per-function arena backing the facts, loans and origins.
+  llvm::BumpPtrAllocator &getAllocator() { return FactAllocator; }
+
   /// Returns the origins that are referenced from more than one basic block.
   /// Only those need to survive block boundaries in the dataflow analyses; the
   /// rest are block-local.

diff  --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
index 6368c1f2a48f4d..95b24c261ae6da 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
@@ -252,6 +252,7 @@ class LifetimeSafetyAnalysis {
   LifetimeSafetySemaHelper *SemaHelper;
   const LifetimeSafetyOpts LSOpts;
   /// Backs the facts, origins, loans and lattices of the analyzed function.
+  /// Also backs the never-destroyed dataflow states of the analyses below.
   llvm::BumpPtrAllocator Allocator;
   LifetimeFactory Factory{Allocator};
   std::unique_ptr<FactManager> FactMgr;

diff  --git a/clang/lib/Analysis/LifetimeSafety/Dataflow.h b/clang/lib/Analysis/LifetimeSafety/Dataflow.h
index 7c232dc72cf936..26579511fc1b13 100644
--- a/clang/lib/Analysis/LifetimeSafety/Dataflow.h
+++ b/clang/lib/Analysis/LifetimeSafety/Dataflow.h
@@ -19,9 +19,13 @@
 #include "clang/Analysis/AnalysisDeclContext.h"
 #include "clang/Analysis/CFG.h"
 #include "clang/Analysis/FlowSensitive/DataflowWorklist.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/Support/Allocator.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/TimeProfiler.h"
+#include <cstddef>
+#include <memory>
 #include <optional>
 
 namespace clang::lifetimes::internal {
@@ -50,6 +54,10 @@ using ProgramPoint = const Fact *;
 /// It may additionally override `Lattice transferAtBlockExit(Lattice);` to
 /// drop state that is not visible outside the block it was computed in.
 ///
+/// The states are allocated from the FactManager's arena and never destroyed,
+/// which saves recursively freeing every tree node just before the arena is
+/// released. A `Lattice` must therefore not own memory outside the arena.
+///
 /// \tparam Derived The CRTP derived class that implements the specific
 /// analysis.
 /// \tparam LatticeType The dataflow lattice used by the analysis.
@@ -68,17 +76,26 @@ class DataflowAnalysis {
 
   /// The dataflow state before each basic block is processed, indexed by block
   /// ID. Unset for blocks that have not been reached yet.
-  llvm::SmallVector<std::optional<Lattice>> InStates;
+  llvm::MutableArrayRef<std::optional<Lattice>> InStates;
   /// The dataflow state after each basic block is processed, indexed by block
   /// ID.
-  llvm::SmallVector<Lattice> OutStates;
+  llvm::MutableArrayRef<Lattice> OutStates;
   /// Dataflow state at each program point, indexed by Fact ID.
   /// In a forward analysis, this is the state after the Fact at that point has
   /// been applied, while in a backward analysis, it is the state before.
-  llvm::SmallVector<Lattice> PointToState;
+  llvm::MutableArrayRef<Lattice> PointToState;
 
   static constexpr bool isForward() { return Dir == Direction::Forward; }
 
+  /// Allocates \p N value-initialized states that are never destroyed.
+  template <typename T>
+  static llvm::MutableArrayRef<T> allocateStates(llvm::BumpPtrAllocator &Alloc,
+                                                 size_t N) {
+    T *States = Alloc.Allocate<T>(N);
+    std::uninitialized_value_construct_n(States, N);
+    return {States, N};
+  }
+
 protected:
   FactManager &FactMgr;
 
@@ -91,9 +108,11 @@ class DataflowAnalysis {
     Derived &D = static_cast<Derived &>(*this);
     llvm::TimeTraceScope Time(D.getAnalysisName());
 
-    PointToState.resize(FactMgr.getNumFacts());
-    InStates.resize(Cfg.getNumBlockIDs());
-    OutStates.resize(Cfg.getNumBlockIDs());
+    llvm::BumpPtrAllocator &Alloc = FactMgr.getAllocator();
+    PointToState = allocateStates<Lattice>(Alloc, FactMgr.getNumFacts());
+    InStates =
+        allocateStates<std::optional<Lattice>>(Alloc, Cfg.getNumBlockIDs());
+    OutStates = allocateStates<Lattice>(Alloc, Cfg.getNumBlockIDs());
 
     using Worklist =
         std::conditional_t<Dir == Direction::Forward, ForwardDataflowWorklist,


        


More information about the cfe-commits mailing list