-
Notifications
You must be signed in to change notification settings - Fork 18.1k
[clang][FlowSensitive] Do a quick check and bail early for massive CFGs #186808
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b6f1adc
[clang][FlowSensitive] Do a quick check and bail early for massive CFGs
jvoung c19a65a
Swap lines
jvoung a62a454
warning
jvoung 724ab37
Fix comparison and add a test. Add comments.
jvoung f651577
Review comments and rename helper
jvoung 2a7cba9
Merge branch 'main' into skip_visit_massive_cfg
jvoung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <cstddef> | ||
| #include <optional> | ||
| #include <system_error> | ||
| #include <utility> | ||
|
|
@@ -34,6 +35,7 @@ | |
| #include "clang/Basic/LLVM.h" | ||
| #include "clang/Support/Compiler.h" | ||
| #include "llvm/ADT/ArrayRef.h" | ||
| #include "llvm/ADT/BitVector.h" | ||
| #include "llvm/ADT/DenseMap.h" | ||
| #include "llvm/ADT/DenseSet.h" | ||
| #include "llvm/ADT/STLExtras.h" | ||
|
|
@@ -479,6 +481,27 @@ transferCFGBlock(const CFGBlock &Block, AnalysisContext &AC, | |
| return State; | ||
| } | ||
|
|
||
| // Returns the number of reachable blocks (would be visited if we only visit | ||
| // each reachable block once). This is a light version of the main fixpoint loop | ||
| // (keep in sync). | ||
| size_t NumBlockVisitsIfVisitOnce(const CFG &CFG) { | ||
| PostOrderCFGView POV(&CFG); | ||
| ForwardDataflowWorklist Worklist(CFG, &POV); | ||
| llvm::BitVector VisitedBlocks(CFG.size()); | ||
| const CFGBlock &Entry = CFG.getEntry(); | ||
| Worklist.enqueueSuccessors(&Entry); | ||
| while (const CFGBlock *Block = Worklist.dequeue()) { | ||
| if (VisitedBlocks[Block->getBlockID()]) | ||
| continue; | ||
| VisitedBlocks[Block->getBlockID()] = true; | ||
| // Do not add unreachable successor blocks to `Worklist`. | ||
| if (Block->hasNoReturnElement()) | ||
| continue; | ||
| Worklist.enqueueSuccessors(Block); | ||
| } | ||
| return VisitedBlocks.count(); | ||
| } | ||
|
|
||
| llvm::Expected<std::vector<std::optional<TypeErasedDataflowAnalysisState>>> | ||
| runTypeErasedDataflowAnalysis( | ||
| const AdornedCFG &ACFG, TypeErasedDataflowAnalysis &Analysis, | ||
|
|
@@ -496,6 +519,20 @@ runTypeErasedDataflowAnalysis( | |
| MaybeStartingEnv ? *MaybeStartingEnv : InitEnv; | ||
|
|
||
| const clang::CFG &CFG = ACFG.getCFG(); | ||
|
|
||
| // Bail out if the number of reachable blocks is already beyond the maximum | ||
| // number of block visits to save time and avoid runnign out of memory for | ||
| // massive CFGs. Note: CFG.size() could have unreachable blocks, | ||
| // but it is a faster to compare that to MaxBlockVisits first before doing the | ||
| // reachable block count. | ||
| if (CFG.size() > static_cast<size_t>(MaxBlockVisits)) { | ||
| if (NumBlockVisitsIfVisitOnce(CFG) > static_cast<size_t>(MaxBlockVisits)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| return llvm::createStringError( | ||
| std::errc::timed_out, "number of blocks in cfg will lead to " | ||
| "exceeding maximum number of block visits"); | ||
| } | ||
| } | ||
|
|
||
| PostOrderCFGView POV(&CFG); | ||
| ForwardDataflowWorklist Worklist(CFG, &POV); | ||
| llvm::SmallDenseSet<const CFGBlock *> NonStructLoopBackedgeNodes = | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done