Skip to content

Add a -verify-linetable LLVM option. #19225

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
Sep 10, 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
45 changes: 42 additions & 3 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
using namespace swift;
using namespace irgen;

llvm::cl::opt<bool> VerifyLineTable(
"verify-linetable", llvm::cl::init(false),
llvm::cl::desc(
"Verify that the debug locations within one scope are contiguous."));

namespace {
using TrackingDIRefMap =
llvm::DenseMap<const llvm::MDString *, llvm::TrackingMDNodeRef>;
Expand Down Expand Up @@ -105,6 +110,20 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
SmallVector<std::pair<SILLocation::DebugLoc, const SILDebugScope *>, 8>
LocationStack;

#ifndef NDEBUG
using UUSTuple = std::pair<std::pair<unsigned, unsigned>, StringRef>;
struct DebugLocKey : public UUSTuple {
DebugLocKey(SILLocation::DebugLoc DL)
: UUSTuple({{DL.Line, DL.Column}, DL.Filename}) {}
inline bool operator==(const SILLocation::DebugLoc &DL) const {
return first.first == DL.Line && first.second == DL.Column &&
second.equals(DL.Filename);
}
};
llvm::DenseSet<UUSTuple> PreviousLineEntries;
SILLocation::DebugLoc PreviousDebugLoc;
#endif

public:
IRGenDebugInfoImpl(const IRGenOptions &Opts, ClangImporter &CI,
IRGenModule &IGM, llvm::Module &M,
Expand Down Expand Up @@ -307,6 +326,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
}
return true;
}

/// Assert that within one lexical block, each location is only visited once.
bool lineEntryIsSane(SILLocation::DebugLoc DL, const SILDebugScope *DS);

#endif

llvm::DIFile *getOrCreateFile(StringRef Filename) {
Expand Down Expand Up @@ -1564,6 +1587,23 @@ void IRGenDebugInfoImpl::finalize() {
DBuilder.finalize();
}

bool IRGenDebugInfoImpl::lineEntryIsSane(SILLocation::DebugLoc DL,
const SILDebugScope *DS) {
// All bets are off for optimized code.
if (!VerifyLineTable || Opts.shouldOptimize())
return true;
// We entered a new lexical block.
if (DS != LastScope)
PreviousLineEntries.clear();
if (DL.Line == 0 || DL == PreviousDebugLoc)
return true;
// Save the last non-zero line entry.
PreviousDebugLoc = DL;
auto ItNew = PreviousLineEntries.insert(DebugLocKey(DL));
// Return true iff DL was not yet in PreviousLineEntries.
return ItNew.second;
}

void IRGenDebugInfoImpl::setCurrentLoc(IRBuilder &Builder,
const SILDebugScope *DS,
SILLocation Loc) {
Expand Down Expand Up @@ -1612,9 +1652,8 @@ void IRGenDebugInfoImpl::setCurrentLoc(IRBuilder &Builder,
}

// FIXME: Enable this assertion.
// assert(lineNumberIsSane(Builder, L.Line) &&
// "-Onone, but line numbers are not monotonically increasing within
// bb");
assert(lineEntryIsSane(L, DS) &&
"non-contiguous debug location in same scope at -Onone");
LastDebugLoc = L;
LastScope = DS;

Expand Down