https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/148976
>From e940cf07ac7e01df86f92ea90071c6decdc34ada Mon Sep 17 00:00:00 2001 From: Utkarsh Saxena <u...@google.com> Date: Tue, 15 Jul 2025 22:19:48 +0000 Subject: [PATCH] add-liveness-finally --- .../clang/Analysis/Analyses/LifetimeSafety.h | 2 + clang/lib/Analysis/LifetimeSafety.cpp | 78 +++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety.h index 1c00558d32f63..3720385217325 100644 --- a/clang/include/clang/Analysis/Analyses/LifetimeSafety.h +++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety.h @@ -34,6 +34,7 @@ class Fact; class FactManager; class LoanPropagationAnalysis; class ExpiredLoansAnalysis; +class LiveOriginAnalysis; struct LifetimeFactory; /// A generic, type-safe wrapper for an ID, distinguished by its `Tag` type. @@ -114,6 +115,7 @@ class LifetimeSafetyAnalysis { std::unique_ptr<FactManager> FactMgr; std::unique_ptr<LoanPropagationAnalysis> LoanPropagation; std::unique_ptr<ExpiredLoansAnalysis> ExpiredLoans; + std::unique_ptr<LiveOriginAnalysis> LiveOrigins; }; } // namespace internal } // namespace clang::lifetimes diff --git a/clang/lib/Analysis/LifetimeSafety.cpp b/clang/lib/Analysis/LifetimeSafety.cpp index 815a36e13412c..6591312ba4995 100644 --- a/clang/lib/Analysis/LifetimeSafety.cpp +++ b/clang/lib/Analysis/LifetimeSafety.cpp @@ -727,12 +727,14 @@ join(llvm::ImmutableMap<K, V> A, llvm::ImmutableMap<K, V> B, // ========================================================================= // using OriginLoanMap = llvm::ImmutableMap<OriginID, LoanSet>; +using OriginSet = llvm::ImmutableSet<OriginID>; /// An object to hold the factories for immutable collections, ensuring /// that all created states share the same underlying memory management. struct LifetimeFactory { OriginLoanMap::Factory OriginMapFactory; LoanSet::Factory LoanSetFactory; + OriginSet::Factory OriginSetFactory; /// Creates a singleton set containing only the given loan ID. LoanSet createLoanSet(LoanID LID) { @@ -833,6 +835,78 @@ class LoanPropagationAnalysis } }; +// ========================================================================= // +// Live Origins Analysis +// ========================================================================= // + +/// The dataflow lattice for origin liveness analysis. +/// It tracks the set of origins that are live at a given program point. +struct LivenessLattice { + OriginSet LiveOrigins; + + LivenessLattice() : LiveOrigins(nullptr) {}; + explicit LivenessLattice(OriginSet S) : LiveOrigins(S) {} + + bool operator==(const LivenessLattice &Other) const { + return LiveOrigins == Other.LiveOrigins; + } + bool operator!=(const LivenessLattice &Other) const { + return !(*this == Other); + } + + void dump(llvm::raw_ostream &OS) const { + OS << "LivenessLattice State:\n"; + if (LiveOrigins.isEmpty()) + OS << " <empty>\n"; + for (const OriginID &OID : LiveOrigins) + OS << " Origin " << OID << " is live\n"; + } +}; + +/// The analysis that tracks which origins are live. This is a backward +/// analysis. +class LiveOriginAnalysis + : public DataflowAnalysis<LiveOriginAnalysis, LivenessLattice, + Direction::Backward> { + + OriginSet::Factory &SetFactory; + +public: + LiveOriginAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F, + OriginSet::Factory &SF) + : DataflowAnalysis(C, AC, F), SetFactory(SF) {} + + using DataflowAnalysis<LiveOriginAnalysis, Lattice, + Direction::Backward>::transfer; + + StringRef getAnalysisName() const { return "LiveOrigins"; } + + Lattice getInitialState() { return Lattice(SetFactory.getEmptySet()); } + + /// Merges two lattices by taking the union of the live origin sets. + Lattice join(Lattice L1, Lattice L2) const { + return Lattice(utils::join(L1.LiveOrigins, L2.LiveOrigins, SetFactory)); + } + + /// An assignment `p = q` kills the liveness of `p` and generates liveness + /// for `q`. + Lattice transfer(Lattice In, const AssignOriginFact &F) { + OriginSet S = SetFactory.remove(In.LiveOrigins, F.getDestOriginID()); + S = SetFactory.add(S, F.getSrcOriginID()); + return Lattice(S); + } + + /// Issuing a new loan to an origin kills its liveness. + Lattice transfer(Lattice In, const IssueFact &F) { + return Lattice(SetFactory.remove(In.LiveOrigins, F.getOriginID())); + } + + /// A return statement generates liveness for the returned origin. + Lattice transfer(Lattice In, const ReturnOfOriginFact &F) { + return Lattice(SetFactory.add(In.LiveOrigins, F.getReturnedOriginID())); + } +}; + // ========================================================================= // // Expired Loans Analysis // ========================================================================= // @@ -937,6 +1011,10 @@ void LifetimeSafetyAnalysis::run() { ExpiredLoans = std::make_unique<ExpiredLoansAnalysis>(Cfg, AC, *FactMgr, *Factory); ExpiredLoans->run(); + + LiveOrigins = std::make_unique<LiveOriginAnalysis>(Cfg, AC, *FactMgr, + Factory->OriginSetFactory); + LiveOrigins->run(); } LoanSet LifetimeSafetyAnalysis::getLoansAtPoint(OriginID OID, _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits