Skip to content

[LifetimeSafety] Add loan expiry analysis#148712

Merged
usx95 merged 1 commit into
mainfrom
users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry
Jul 23, 2025
Merged

[LifetimeSafety] Add loan expiry analysis#148712
usx95 merged 1 commit into
mainfrom
users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry

Conversation

@usx95

@usx95 usx95 commented Jul 14, 2025

Copy link
Copy Markdown
Contributor

This PR adds the ExpiredLoansAnalysis class to track which loans have expired. The analysis uses a dataflow lattice (ExpiredLattice) to maintain the set of expired loans at each program point.

This is a very light weight dataflow analysis and is expected to reach fixed point in ~2 iterations.
In principle, this does not need a dataflow analysis but is used for convenience in favour of lean code.

usx95 commented Jul 14, 2025

Copy link
Copy Markdown
Contributor Author

@usx95 usx95 changed the title users/usx95/lifetime-safety-add-loan-expiry [LifetimeSafety] Add loan expiry analysis Jul 14, 2025
@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch from 880cf3b to ed73e3b Compare July 14, 2025 19:49
@usx95 usx95 force-pushed the users/usx95/lifetime-safety-liveness branch from c217d0d to 702d255 Compare July 14, 2025 19:49
@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch from ed73e3b to a50b00e Compare July 14, 2025 19:52
@usx95 usx95 force-pushed the users/usx95/lifetime-safety-liveness branch 2 times, most recently from 7e098b0 to 793d58e Compare July 14, 2025 20:02
@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch from a50b00e to a9f1e8d Compare July 14, 2025 20:03
@usx95 usx95 self-assigned this Jul 14, 2025
@usx95 usx95 force-pushed the users/usx95/lifetime-safety-liveness branch from 793d58e to 2bff132 Compare July 15, 2025 09:09
@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch 5 times, most recently from 6ce3173 to 9265536 Compare July 15, 2025 10:27
@usx95 usx95 force-pushed the users/usx95/lifetime-safety-liveness branch from 2253ff0 to c073ef3 Compare July 15, 2025 10:27
@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch from 9265536 to 9da8f3a Compare July 15, 2025 10:47
@usx95 usx95 force-pushed the users/usx95/lifetime-safety-liveness branch from c073ef3 to 51afc3e Compare July 15, 2025 10:47
@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch from 9da8f3a to 04d6193 Compare July 15, 2025 17:53
@usx95 usx95 force-pushed the users/usx95/lifetime-safety-liveness branch from 51afc3e to 7b26a72 Compare July 15, 2025 17:53
@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch from 04d6193 to 2073937 Compare July 15, 2025 21:33
@usx95 usx95 changed the base branch from users/usx95/lifetime-safety-liveness to users/usx95/07-15-add-backward-analysis-capability July 15, 2025 21:35
@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch 2 times, most recently from 5f09913 to aca68c2 Compare July 15, 2025 21:54
@usx95 usx95 force-pushed the users/usx95/07-15-add-backward-analysis-capability branch from b109b6a to 7502ee5 Compare July 15, 2025 21:54
@usx95 usx95 requested a review from jvoung July 15, 2025 22:03
@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch from 8c5c8f1 to c29040a Compare July 18, 2025 14:27
@usx95 usx95 requested a review from Xazax-hun July 18, 2025 14:28
@usx95

usx95 commented Jul 18, 2025

Copy link
Copy Markdown
Contributor Author

Added unit tests for loan expiry!

Comment thread clang/lib/Analysis/LifetimeSafety.cpp Outdated

// ========================================================================= //
// Expired Loans Analysis
// ========================================================================= //

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add dash.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a suggestion? I don't quite get the location where you are referring.

}

Lattice transfer(Lattice In, const IssueFact &F) {
return Lattice(Factory.remove(In.Expired, F.getLoanID()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth explaining why this is necessary. Specifically, now its possible that the loan would appear in In.Expired at all to be worth removing. I assume this arises because of backedges.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. There is a subtle false negative due to this as well. Mentioned that.

@Xazax-hun Xazax-hun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the comments are addressed it looks good to me.

@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch from c29040a to 6ad27da Compare July 21, 2025 14:48
@usx95 usx95 force-pushed the users/usx95/07-16-lifetime-safety-add-unit-tests branch from e6fc855 to ad8d8a8 Compare July 21, 2025 22:04
@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch from 6ad27da to 7ec322f Compare July 21, 2025 22:05
Base automatically changed from users/usx95/07-16-lifetime-safety-add-unit-tests to main July 22, 2025 10:32
@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch from 7ec322f to 65f5402 Compare July 22, 2025 10:33

@jvoung jvoung left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG too!

@usx95 usx95 force-pushed the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch from 65f5402 to 7a78e40 Compare July 23, 2025 09:53

usx95 commented Jul 23, 2025

Copy link
Copy Markdown
Contributor Author

Merge activity

  • Jul 23, 10:12 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Jul 23, 10:14 AM UTC: @usx95 merged this pull request with Graphite.

@usx95 usx95 merged commit ffdada1 into main Jul 23, 2025
7 of 9 checks passed
@usx95 usx95 deleted the users/usx95/07-14-users_usx95_lifetime-safety-add-loan-expiry branch July 23, 2025 10:14
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Lifetime Safety in Clang Jul 23, 2025
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
This PR adds the `ExpiredLoansAnalysis` class to track which loans have expired. The analysis uses a dataflow lattice (`ExpiredLattice`) to maintain the set of expired loans at each program point.

This is a very light weight dataflow analysis and is expected to reach fixed point in ~2 iterations.
In principle, this does not need a dataflow analysis but is used for convenience in favour of lean code.
@usx95 usx95 added the clang:temporal-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr) label Aug 22, 2025
Xazax-hun pushed a commit to swiftlang/llvm-project that referenced this pull request Dec 12, 2025
Refactored the lifetime safety analysis to use a generic dataflow framework with a policy-based design.

- Introduced a generic `DataflowAnalysis` template class that can be specialized for different analyses
- Renamed `LifetimeLattice` to `LoanPropagationLattice` to better reflect its purpose
- Created a `LoanPropagationAnalysis` class that inherits from the generic framework
- Moved transfer functions from the standalone `Transferer` class into the analysis class
- Restructured the code to separate the dataflow engine from the specific analysis logic
- Updated debug output and test expectations to use the new class names

In order to add more analyses, e.g. [loan expiry](llvm#148712) and origin liveness, the previous implementation would have separate, nearly identical dataflow runners for each analysis. This change creates a single, reusable component, which will make it much simpler to add subsequent analyses without repeating boilerplate code.

This is quite close to the existing dataflow framework!
Xazax-hun pushed a commit to swiftlang/llvm-project that referenced this pull request Dec 12, 2025
This PR adds the `ExpiredLoansAnalysis` class to track which loans have expired. The analysis uses a dataflow lattice (`ExpiredLattice`) to maintain the set of expired loans at each program point.

This is a very light weight dataflow analysis and is expected to reach fixed point in ~2 iterations.
In principle, this does not need a dataflow analysis but is used for convenience in favour of lean code.
markrvmurray pushed a commit to markrvmurray/llvm-mc6809 that referenced this pull request Jun 14, 2026
Refactored the lifetime safety analysis to use a generic dataflow framework with a policy-based design.

### Changes

- Introduced a generic `DataflowAnalysis` template class that can be specialized for different analyses
- Renamed `LifetimeLattice` to `LoanPropagationLattice` to better reflect its purpose
- Created a `LoanPropagationAnalysis` class that inherits from the generic framework
- Moved transfer functions from the standalone `Transferer` class into the analysis class
- Restructured the code to separate the dataflow engine from the specific analysis logic
- Updated debug output and test expectations to use the new class names

### Motivation

In order to add more analyses, e.g. [loan expiry](llvm/llvm-project#148712) and origin liveness, the previous implementation would have separate, nearly identical dataflow runners for each analysis. This change creates a single, reusable component, which will make it much simpler to add subsequent analyses without repeating boilerplate code.


This is quite close to the existing dataflow framework!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:analysis clang:temporal-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr) clang Clang issues not falling into any other category

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants