llvm / llvm/llvm-project

misc-static-initialization-cycle is very slow

Closed Beginner friendly
#224,219 2 comments 0 reactions 0 assignees View on GitHub
clang-tidy performance
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

After upgrading from LLVM19 to 23, we did some performance tests on a selection of large files in our codebase.
The check `misc-static-initialization-cycle` seems to take a significant part of the time.
````
===-------------------------------------------------------------------------===
clang-tidy checks profiling
===-------------------------------------------------------------------------===
Total Execution Time: 29.0469 seconds (29.0015 wall clock)

---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Name ---
2.8125 ( 10.5%) 0.0938 ( 4.3%) 2.9062 ( 10.0%) 2.8914 ( 10.0%) misc-static-initialization-cycle
````

A first check of this code points out the following code:
````
bool VisitDeclRefExpr(DeclRefExpr *DRE) override {
if (const auto *VarD = dyn_cast(DRE->getDecl())) {
if (!shouldIgnoreRef(DRE, Node->getDecl()) &&
(VarD->hasGlobalStorage() || VarD->isStaticLocal()))
Node->Uses.emplace_back(DRE, G.addNode(VarD->getCanonicalDecl()));
}
return true;
}
````
The `shouldIgnoreRef` method is very expensive, as it calls `ParentMapContext::getParents(...)` in all cases followed by some recursive looping towards the grandparents.

If I understand the code correctly, the method `shouldIgnoreRef` has no side effects. So by swapping the arguments of `&&`, several calls into `shouldIgnoreRef` could be prevented as a quick win.

Contributor guide

Open the contributing guide

Research direction

Start at misc-static-initialization-cycle's VisitDeclRefExpr and inspect shouldIgnoreRef, especially its ParentMapContext::getParents calls and recursive parent traversal. Check whether evaluating the global-storage or static-local condition first preserves behavior while avoiding unnecessary calls, then compare clang-tidy profiling results and ensure the check still reports the same cycles.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, performance
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.