Skip to content

[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 1 commit into from
Jan 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions lib/SIL/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ static llvm::cl::opt<bool> AbortOnFailure(
"verify-abort-on-failure",
llvm::cl::init(true));

static llvm::cl::opt<bool> VerifyDIHoles(
Copy link
Contributor

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?

Copy link
Member Author

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.

"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.

Expand Down Expand Up @@ -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;
Copy link
Contributor

@JDevlieghere JDevlieghere Dec 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you always continue in the if-branch you might as well omit the else.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.
Expand All @@ -4360,6 +4436,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
}

SILInstructionVisitor::visitSILBasicBlock(BB);
verifyDebugScopeHoles(BB);
}

void visitBasicBlockArguments(SILBasicBlock *BB) {
Expand Down