-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[DebugInfo] Add a verifier pass to find holes in lexical scopes. #13491
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 all commits
Commits
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 |
---|---|---|
|
@@ -56,6 +56,10 @@ static llvm::cl::opt<bool> AbortOnFailure( | |
"verify-abort-on-failure", | ||
llvm::cl::init(true)); | ||
|
||
static llvm::cl::opt<bool> VerifyDIHoles( | ||
"verify-di-holes", | ||
llvm::cl::init(false)); | ||
|
||
// The verifier is basically all assertions, so don't compile it with NDEBUG to | ||
// prevent release builds from triggering spurious unused variable warnings. | ||
|
||
|
@@ -4338,6 +4342,78 @@ class SILVerifier : public SILVerifierBase<SILVerifier> { | |
} | ||
} | ||
|
||
/// This pass verifies that there are no hole in debug scopes at -Onone. | ||
void verifyDebugScopeHoles(SILBasicBlock *BB) { | ||
if (!VerifyDIHoles) | ||
return; | ||
|
||
// This check only makes sense at -Onone. Optimizations, | ||
// e.g. inlining, can move scopes around. | ||
llvm::DenseSet<const SILDebugScope *> AlreadySeenScopes; | ||
if (BB->getParent()->getEffectiveOptimizationMode() != | ||
OptimizationMode::NoOptimization) | ||
return; | ||
|
||
// Exit early if this BB is empty. | ||
if (BB->empty()) | ||
return; | ||
|
||
const SILDebugScope *LastSeenScope = nullptr; | ||
for (SILInstruction &SI : *BB) { | ||
if (isa<AllocStackInst>(SI)) | ||
continue; | ||
LastSeenScope = SI.getDebugScope(); | ||
AlreadySeenScopes.insert(LastSeenScope); | ||
break; | ||
} | ||
for (SILInstruction &SI : *BB) { | ||
// `alloc_stack` can create false positive, so we skip it | ||
// for now. | ||
if (isa<AllocStackInst>(SI)) | ||
continue; | ||
|
||
// If we haven't seen this debug scope yet, update the | ||
// map and go on. | ||
auto *DS = SI.getDebugScope(); | ||
assert(DS && "Each instruction should have a debug scope"); | ||
if (!AlreadySeenScopes.count(DS)) { | ||
AlreadySeenScopes.insert(DS); | ||
LastSeenScope = DS; | ||
continue; | ||
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. If you always 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 |
||
} | ||
|
||
// Otherwise, we're allowed to re-enter a scope only if | ||
// the scope is an ancestor of the scope we're currently leaving. | ||
auto isAncestorScope = [](const SILDebugScope *Cur, | ||
const SILDebugScope *Previous) { | ||
const SILDebugScope *Tmp = Previous; | ||
assert(Tmp && "scope can't be null"); | ||
while (Tmp) { | ||
PointerUnion<const SILDebugScope *, SILFunction *> Parent = | ||
Tmp->Parent; | ||
auto *ParentScope = Parent.dyn_cast<const SILDebugScope *>(); | ||
if (!ParentScope) | ||
break; | ||
if (ParentScope == Cur) | ||
return true; | ||
Tmp = ParentScope; | ||
} | ||
return false; | ||
}; | ||
|
||
if (isAncestorScope(DS, LastSeenScope)) { | ||
LastSeenScope = DS; | ||
continue; | ||
} | ||
if (DS != LastSeenScope) { | ||
DEBUG(llvm::dbgs() << "Broken instruction!\n"; SI.dump()); | ||
require( | ||
DS == LastSeenScope, | ||
"Basic block contains a non-contiguous lexical scope at -Onone"); | ||
} | ||
} | ||
} | ||
|
||
void visitSILBasicBlock(SILBasicBlock *BB) { | ||
// Make sure that each of the successors/predecessors of this basic block | ||
// have this basic block in its predecessor/successor list. | ||
|
@@ -4360,6 +4436,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> { | |
} | ||
|
||
SILInstructionVisitor::visitSILBasicBlock(BB); | ||
verifyDebugScopeHoles(BB); | ||
} | ||
|
||
void visitBasicBlockArguments(SILBasicBlock *BB) { | ||
|
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.
Is this option really necessary, shouldn't it just always be on?
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.
I think this is good while we're transitioning, so that if something breaks, we can just turn the option off and investigate rather than reverting the whole commit.