Skip to content

Introduce intra-procedural lifetime analysis in Clang #142313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

usx95
Copy link
Contributor

@usx95 usx95 commented Jun 1, 2025

This patch introduces the initial implementation of the intra-procedural, flow-sensitive lifetime analysis for Clang, as proposed in the recent RFC: https://discourse.llvm.org/t/rfc-intra-procedural-lifetime-analysis-in-clang/86291

The primary goal of this initial submission is to establish the core dataflow framework and gather feedback on the overall design, fact representation, and testing strategy. The focus is on the dataflow mechanism itself rather than exhaustively covering all C++ AST edge cases, which will be addressed in subsequent patches.

Key Components

  • Conceptual Model: Introduces the fundamental concepts of Loan, Origin, and Path to model memory borrows and the lifetime of pointers.
  • Fact Generation: A frontend pass traverses the Clang CFG to generate a representation of lifetime-relevant events, such as pointer assignments, taking an address, and variables going out of scope.
  • Dataflow Lattice: A dataflow lattice used to map each pointer's symbolic Origin to the set of Loans it may contain at any given program point.
  • Fixed-Point Analysis: A worklist-based, flow-sensitive analysis that propagates the lattice state across the CFG to a fixed point.
  • Testing: llvm-lit tests validate the analysis by checking the generated facts and final dataflow state.

Next Steps

(Not covered in this PR but planned for subsequent patches)

The following functionality is planned for the upcoming patches to build upon this foundation and make the analysis usable in practice:

  • Placeholder Loans: Introduce placeholder loans to represent the lifetimes of function parameters, forming the basis for analysis involving function calls.
  • Annotation and Opaque Call Handling: Use placeholder loans to correctly model function calls, both by respecting [[clang::lifetimebound]] annotations and by conservatively handling opaque/un-annotated functions.
  • Error Reporting: Implement the final analysis phase that consumes the dataflow results to generate user-facing diagnostics.
  • Strict vs. Permissive Modes: Add the logic to support both high-confidence (permissive) and more comprehensive (strict) warning levels.
  • Expanded C++ Coverage: Broaden support for common patterns, including the lifetimes of temporary objects and pointers within aggregate types (structs/containers).
  • Performance benchmarking
  • Capping number of iterations or number of times a CFGBlock is processed.

Performance on pathological test cases:

The pathological case arise when we have N origins initially holding N loans and we have a cyclic assignment of these origins in a loop. The fixed point reaches after N iterations when all the origins contain all the loans.
For N = 4, the test case would like like:

struct MyObj {
  int id;
  ~MyObj() {}
};

void long_cycle(bool condition) {
  MyObj v1{1};
  MyObj v2{1};
  MyObj v3{1};
  MyObj v4{1};

  MyObj* p1 = &v1;
  MyObj* p2 = &v2;
  MyObj* p3 = &v3;
  MyObj* p4 = &v4;

  while (condition) {
    MyObj* temp = p1;
    p1 = p2;
    p2 = p3;
    p3 = p4;
    p4 = temp;
  }
}

@usx95 usx95 force-pushed the dangling-references-latest branch from c8f6277 to 3624359 Compare June 2, 2025 13:06
Copy link

github-actions bot commented Jun 2, 2025

✅ With the latest revision this PR passed the Python code formatter.

@usx95 usx95 force-pushed the dangling-references-latest branch 4 times, most recently from 668e329 to 7b929ee Compare June 2, 2025 23:41
@usx95 usx95 changed the title Introduce Intra-procedural lifetime analysis in Clang Introduce intra-procedural lifetime analysis in Clang Jun 2, 2025
@usx95 usx95 force-pushed the dangling-references-latest branch 2 times, most recently from 136238f to 942648e Compare June 3, 2025 13:52
@usx95 usx95 requested review from ymand, gribozavr and jvoung June 4, 2025 14:32
@usx95 usx95 force-pushed the dangling-references-latest branch from 942648e to cb7bd7f Compare June 5, 2025 13:52
@usx95 usx95 force-pushed the dangling-references-latest branch from cb7bd7f to 0cd187b Compare June 5, 2025 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant