[clang] [LifetimeSafety] Add per-program-point lattice tracking (PR #149199)

2025-07-21 Thread Utkarsh Saxena via cfe-commits

usx95 wrote:

### Merge activity

* **Jul 21, 9:57 PM UTC**: A user started a stack merge that includes this pull 
request via 
[Graphite](https://app.graphite.dev/github/pr/llvm/llvm-project/149199).


https://github.com/llvm/llvm-project/pull/149199
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [LifetimeSafety] Add per-program-point lattice tracking (PR #149199)

2025-07-17 Thread Gábor Horváth via cfe-commits

https://github.com/Xazax-hun approved this pull request.

LG, thanks! 

https://github.com/llvm/llvm-project/pull/149199
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [LifetimeSafety] Add per-program-point lattice tracking (PR #149199)

2025-07-17 Thread Gábor Horváth via cfe-commits


@@ -524,14 +531,20 @@ template 
 class DataflowAnalysis {
 public:
   using Lattice = LatticeType;
-  using Base = DataflowAnalysis;
+  using Base = DataflowAnalysis;
 
 private:
   const CFG &Cfg;
   AnalysisDeclContext ∾
 
+  /// The dataflow state before a basic block is processed.
   llvm::DenseMap InStates;
+  /// The dataflow state after a basic block is processed.
   llvm::DenseMap OutStates;
+  /// The dataflow state at a Program Point.
+  /// 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::DenseMap PerPointStates;

Xazax-hun wrote:

Ah never mind. Now I remember you want to keep these states instead of 
replaying the analysis for the blocks. 

https://github.com/llvm/llvm-project/pull/149199
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [LifetimeSafety] Add per-program-point lattice tracking (PR #149199)

2025-07-17 Thread Gábor Horváth via cfe-commits


@@ -524,14 +531,20 @@ template 
 class DataflowAnalysis {
 public:
   using Lattice = LatticeType;
-  using Base = DataflowAnalysis;
+  using Base = DataflowAnalysis;
 
 private:
   const CFG &Cfg;
   AnalysisDeclContext ∾
 
+  /// The dataflow state before a basic block is processed.
   llvm::DenseMap InStates;
+  /// The dataflow state after a basic block is processed.
   llvm::DenseMap OutStates;
+  /// The dataflow state at a Program Point.
+  /// 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::DenseMap PerPointStates;

Xazax-hun wrote:

Do you think all of the analyses need `PerPointStates`? E.g., what about the 
`ExpiredLoansAnalysis`? I wonder if we should have a boolean policy here. 

https://github.com/llvm/llvm-project/pull/149199
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [LifetimeSafety] Add per-program-point lattice tracking (PR #149199)

2025-07-17 Thread Utkarsh Saxena via cfe-commits


@@ -502,6 +502,12 @@ class FactGenerator : public 
ConstStmtVisitor {
 
 enum class Direction { Forward, Backward };
 
+/// A program point is a pair of a CFGBlock and a Fact within that block.
+///
+/// This is used to represent the state of the program *after* the Fact is
+/// executed.
+using ProgramPoint = std::pair;

usx95 wrote:

Hmm. Looks like an oversight. Thanks for spotting.

https://github.com/llvm/llvm-project/pull/149199
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [LifetimeSafety] Add per-program-point lattice tracking (PR #149199)

2025-07-17 Thread Utkarsh Saxena via cfe-commits

https://github.com/usx95 updated 
https://github.com/llvm/llvm-project/pull/149199

>From 3e67c98b4a365948910a3da9ad5d56a6087a527a Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena 
Date: Wed, 16 Jul 2025 21:57:03 +
Subject: [PATCH] address comment

---
 clang/lib/Analysis/LifetimeSafety.cpp | 39 +--
 1 file changed, 31 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Analysis/LifetimeSafety.cpp 
b/clang/lib/Analysis/LifetimeSafety.cpp
index e3a03cf93880e..a95db6d8013bd 100644
--- a/clang/lib/Analysis/LifetimeSafety.cpp
+++ b/clang/lib/Analysis/LifetimeSafety.cpp
@@ -502,6 +502,13 @@ class FactGenerator : public 
ConstStmtVisitor {
 
 enum class Direction { Forward, Backward };
 
+/// A `ProgramPoint` identifies a location in the CFG by pointing to a specific
+/// `Fact`. identified by a lifetime-related event (`Fact`).
+///
+/// A `ProgramPoint` has "after" semantics: it represents the location
+/// immediately after its corresponding `Fact`.
+using ProgramPoint = const Fact *;
+
 /// A generic, policy-based driver for dataflow analyses. It combines
 /// the dataflow runner and the transferer logic into a single class hierarchy.
 ///
@@ -524,14 +531,20 @@ template 
 class DataflowAnalysis {
 public:
   using Lattice = LatticeType;
-  using Base = DataflowAnalysis;
+  using Base = DataflowAnalysis;
 
 private:
   const CFG &Cfg;
   AnalysisDeclContext ∾
 
+  /// The dataflow state before a basic block is processed.
   llvm::DenseMap InStates;
+  /// The dataflow state after a basic block is processed.
   llvm::DenseMap OutStates;
+  /// The dataflow state at a Program Point.
+  /// 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::DenseMap PerPointStates;
 
   static constexpr bool isForward() { return Dir == Direction::Forward; }
 
@@ -577,6 +590,8 @@ class DataflowAnalysis {
 }
   }
 
+  Lattice getState(ProgramPoint P) const { return PerPointStates.lookup(P); }
+
   Lattice getInState(const CFGBlock *B) const { return InStates.lookup(B); }
 
   Lattice getOutState(const CFGBlock *B) const { return OutStates.lookup(B); }
@@ -590,18 +605,23 @@ class DataflowAnalysis {
 getOutState(&B).dump(llvm::dbgs());
   }
 
+private:
   /// Computes the state at one end of a block by applying all its facts
   /// sequentially to a given state from the other end.
-  /// TODO: We might need to store intermediate states per-fact in the block 
for
-  /// later analysis.
   Lattice transferBlock(const CFGBlock *Block, Lattice State) {
 auto Facts = AllFacts.getFacts(Block);
-if constexpr (isForward())
-  for (const Fact *F : Facts)
+if constexpr (isForward()) {
+  for (const Fact *F : Facts) {
 State = transferFact(State, F);
-else
-  for (const Fact *F : llvm::reverse(Facts))
+PerPointStates[F] = State;
+  }
+} else {
+  for (const Fact *F : llvm::reverse(Facts)) {
+// In backward analysis, capture the state before applying the fact.
+PerPointStates[F] = State;
 State = transferFact(State, F);
+  }
+}
 return State;
   }
 
@@ -769,6 +789,10 @@ class LoanPropagationAnalysis
 Factory.OriginMapFactory.add(In.Origins, DestOID, SrcLoans));
   }
 
+  LoanSet getLoans(OriginID OID, ProgramPoint P) {
+return getLoans(getState(P), OID);
+  }
+
 private:
   LoanSet getLoans(Lattice L, OriginID OID) {
 if (auto *Loans = L.Origins.lookup(OID))
@@ -779,7 +803,6 @@ class LoanPropagationAnalysis
 
 // = //
 //  TODO:
-// - Modifying loan propagation to answer `LoanSet getLoans(Origin O, Point P)`
 // - Modify loan expiry analysis to answer `bool isExpired(Loan L, Point P)`
 // - Modify origin liveness analysis to answer `bool isLive(Origin O, Point P)`
 // - Using the above three to perform the final error reporting.

___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [LifetimeSafety] Add per-program-point lattice tracking (PR #149199)

2025-07-17 Thread Gábor Horváth via cfe-commits


@@ -502,6 +502,12 @@ class FactGenerator : public 
ConstStmtVisitor {
 
 enum class Direction { Forward, Backward };
 
+/// A program point is a pair of a CFGBlock and a Fact within that block.
+///
+/// This is used to represent the state of the program *after* the Fact is
+/// executed.
+using ProgramPoint = std::pair;

Xazax-hun wrote:

Why do we need the CfgBlock? Wouldn't a fact alone be sufficient?

https://github.com/llvm/llvm-project/pull/149199
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits