llvm-project
80fd8f85 - [CIR] Add a points-to analysis skeleton (#244356)

Commit
1 day ago
[CIR] Add a points-to analysis skeleton (#244356) The CIR alias analysis stops at loads, so it cannot tell anything about pointers loaded from memory. Since Clang emits a cir.alloca for every local, including local pointers, most pointer uses in CIR go through a load. This adds a minimal points-to analysis that follows pointers through memory. It is inclusion based and flow insensitive, tracks cir.alloca objects through cir.store and cir.load, and treats any other use of an address as an escape. Its work is bounded by PointsToLimits, using the limits of a similar analysis tuned on large C++ code bases downstream. Results are reported as optimization remarks in the points-to category, which the tests check on CIR and on C++ sources. This is the base for the lifetime checker in the ClangIR incubator and for other downstream optimizations we'd like to upstream later. More operations, track unknown states, an alias analysis built on the points-to sets, and more optimizations to the solver will come in follow up PRs, see `clang/docs/CIR/PointsToAnalysis.md` for the roadmap. ## Why it doesn't use MLIR dataflow framework MLIR's sparse data-flow framework skips operations without results, so it never sees cir.store and can't follow pointers through memory. Doing that needs either the dense, flow-sensitive framework or a custom analysis on DataFlowSolver, and both also depend on DeadCodeAnalysis and SparseConstantPropagation. I wrote the same analysis on DataFlowSolver, and it gives identical points-to sets on all 7.2M functions of LLVM, Clang and MLIR at -O3. It is 9.7x slower alone, 22x slower with the analyses it depends on (0.74% of compile time, up to 4.5% for one TU), and 78x slower as a dense flow-sensitive version (2.3 s on one PassBuilder.cpp function, against under 1 ms). This PR approach costs 0.033% of compile time. The gap is the framework's bookkeeping: a state per value and per object, dependency tracking between states, and a worklist of program points. Its worklist only helps on long reverse copy chains, which a cheaper worklist solver, already planned, will handle.
Author
Parents
Loading