[llvm-branch-commits] [clang] [LifetimeSafety] Add lattice tracking per program point (PR #149069)
Utkarsh Saxena via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Jul 16 07:08:50 PDT 2025
https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/149069
>From 266cf01d3afeac7d9d675e4c605f212c758689c5 Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
Date: Wed, 16 Jul 2025 10:44:37 +0000
Subject: [PATCH] add-point-level-granularity-to-analyses
---
clang/lib/Analysis/LifetimeSafety.cpp | 43 ++++++++++++++++++++-------
1 file changed, 33 insertions(+), 10 deletions(-)
diff --git a/clang/lib/Analysis/LifetimeSafety.cpp b/clang/lib/Analysis/LifetimeSafety.cpp
index e12f4aaf1c4b0..a740149a2734c 100644
--- a/clang/lib/Analysis/LifetimeSafety.cpp
+++ b/clang/lib/Analysis/LifetimeSafety.cpp
@@ -500,6 +500,9 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
// Generic Dataflow Analysis
// ========================================================================= //
+// DO NOT SUBMIT: TODO: Document notion of before or after in the analyses.
+using ProgramPoint = std::pair<const CFGBlock *, const Fact *>;
+
enum class Direction { Forward, Backward };
/// A generic, policy-based driver for dataflow analyses. It combines
@@ -532,6 +535,7 @@ class DataflowAnalysis {
llvm::DenseMap<const CFGBlock *, Lattice> InStates;
llvm::DenseMap<const CFGBlock *, Lattice> OutStates;
+ llvm::DenseMap<ProgramPoint, Lattice> PerPointStates;
static constexpr bool isForward() { return Dir == Direction::Forward; }
@@ -577,6 +581,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 +596,22 @@ 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[{Block, F}] = State;
+ }
+ } else {
+ for (const Fact *F : llvm::reverse(Facts)) {
State = transferFact(State, F);
+ PerPointStates[{Block, F}] = State;
+ }
+ }
return State;
}
@@ -771,6 +781,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))
@@ -849,6 +863,14 @@ class LiveOriginAnalysis
Lattice transfer(Lattice In, const ReturnOfOriginFact &F) {
return Lattice(SetFactory.add(In.LiveOrigins, F.getReturnedOriginID()));
}
+
+ bool isLive(OriginID OID, ProgramPoint P) const {
+ return getState(P).LiveOrigins.contains(OID);
+ }
+
+ OriginSet getLiveOrigins(ProgramPoint P) const {
+ return getState(P).LiveOrigins;
+ }
};
// ========================================================================= //
@@ -908,14 +930,15 @@ class ExpiredLoansAnalysis
Lattice transfer(Lattice In, const IssueFact &F) {
return Lattice(Factory.remove(In.Expired, F.getLoanID()));
}
+
+ bool isExpired(LoanID LID, ProgramPoint P) const {
+ return getState(P).Expired.contains(LID);
+ }
};
// ========================================================================= //
// 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.
+// - Add error reporting <DO NOT SUBMIT> Add how would it work.
// ========================================================================= //
} // anonymous namespace
More information about the llvm-branch-commits
mailing list