[Umbrella] Relax escapes of static globals
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
https://compiler-explorer.com/z/ncq5x69da
```c++
void clang_analyzer_dump(int);
static int num = 10; // never escapes
void top() {
clang_analyzer_dump(num); // This should only dump 10.
}
```
The problem is that because we don't have escape analysis for globals, we also can't prove that the address of the global was never taken.
Because of this, when we are in top-level context, or call an opaque function, we can't prove that the initial value of the global can't change, thus we assume it was changed to preserve a sound analysis.
This has the consequence that we can practically never know the value of the global - despite it being defined in the current TU.
Solution:
We could check at parse-time the AST if anything takes the address of this global, or passes it as a reference (let's assume here that const references don't cast away const). We must also assume that calling mutable member functions on the global object may escape its address.
We should also honour the [`noescape`](https://clang.llvm.org/docs/AttributeReference.html#noescape) attribute when considering escapes.
With this information, we should adjust the invalidation behavior for opaque calls, and for the initial state for top-level contexts.
Qualification:
Since this involves some AST-traversal - which can be costly - that we measure the performance cost. We must also not deserialize from PCH and modules as the traversal happens because that is even more costly and also unnecessary. This is why I was proposing to do this traversal per decl group and traverse those decls - similar how the analyzer already collects the TUDecls during parsing in the AnalysisConsumer.cpp
Blocked issues:
#217940
#217963
(more)
Contributor guide
Research direction
Start with the AST traversal and declaration-group collection described in AnalysisConsumer.cpp, and reproduce the example using the linked Compiler Explorer case. Determine how globals, references, mutable member calls, and noescape are classified, then measure traversal cost and verify opaque-call and top-level invalidation behavior without deserializing PCH or modules.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100