Skip to content

Commit e5949ca

Browse files
authored
Merge pull request #19225 from adrian-prantl/verify-linetable
2 parents b8b2820 + 1ed922b commit e5949ca

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
using namespace swift;
5959
using namespace irgen;
6060

61+
llvm::cl::opt<bool> VerifyLineTable(
62+
"verify-linetable", llvm::cl::init(false),
63+
llvm::cl::desc(
64+
"Verify that the debug locations within one scope are contiguous."));
65+
6166
namespace {
6267
using TrackingDIRefMap =
6368
llvm::DenseMap<const llvm::MDString *, llvm::TrackingMDNodeRef>;
@@ -105,6 +110,20 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
105110
SmallVector<std::pair<SILLocation::DebugLoc, const SILDebugScope *>, 8>
106111
LocationStack;
107112

113+
#ifndef NDEBUG
114+
using UUSTuple = std::pair<std::pair<unsigned, unsigned>, StringRef>;
115+
struct DebugLocKey : public UUSTuple {
116+
DebugLocKey(SILLocation::DebugLoc DL)
117+
: UUSTuple({{DL.Line, DL.Column}, DL.Filename}) {}
118+
inline bool operator==(const SILLocation::DebugLoc &DL) const {
119+
return first.first == DL.Line && first.second == DL.Column &&
120+
second.equals(DL.Filename);
121+
}
122+
};
123+
llvm::DenseSet<UUSTuple> PreviousLineEntries;
124+
SILLocation::DebugLoc PreviousDebugLoc;
125+
#endif
126+
108127
public:
109128
IRGenDebugInfoImpl(const IRGenOptions &Opts, ClangImporter &CI,
110129
IRGenModule &IGM, llvm::Module &M,
@@ -307,6 +326,10 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
307326
}
308327
return true;
309328
}
329+
330+
/// Assert that within one lexical block, each location is only visited once.
331+
bool lineEntryIsSane(SILLocation::DebugLoc DL, const SILDebugScope *DS);
332+
310333
#endif
311334

312335
llvm::DIFile *getOrCreateFile(StringRef Filename) {
@@ -1564,6 +1587,23 @@ void IRGenDebugInfoImpl::finalize() {
15641587
DBuilder.finalize();
15651588
}
15661589

1590+
bool IRGenDebugInfoImpl::lineEntryIsSane(SILLocation::DebugLoc DL,
1591+
const SILDebugScope *DS) {
1592+
// All bets are off for optimized code.
1593+
if (!VerifyLineTable || Opts.shouldOptimize())
1594+
return true;
1595+
// We entered a new lexical block.
1596+
if (DS != LastScope)
1597+
PreviousLineEntries.clear();
1598+
if (DL.Line == 0 || DL == PreviousDebugLoc)
1599+
return true;
1600+
// Save the last non-zero line entry.
1601+
PreviousDebugLoc = DL;
1602+
auto ItNew = PreviousLineEntries.insert(DebugLocKey(DL));
1603+
// Return true iff DL was not yet in PreviousLineEntries.
1604+
return ItNew.second;
1605+
}
1606+
15671607
void IRGenDebugInfoImpl::setCurrentLoc(IRBuilder &Builder,
15681608
const SILDebugScope *DS,
15691609
SILLocation Loc) {
@@ -1612,9 +1652,8 @@ void IRGenDebugInfoImpl::setCurrentLoc(IRBuilder &Builder,
16121652
}
16131653

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

0 commit comments

Comments
 (0)