From 6c12ae8163c77c508aa846958360aa96d1c2c00d Mon Sep 17 00:00:00 2001 From: Hyoun Kyu Cho Date: Tue, 24 May 2022 03:04:40 +0000 Subject: [PATCH] Exposes interface to free up caching data structure in DWARFDebugLine and DWARFUnit for memory management This is minimum changes extracted from https://reviews.llvm.org/D78950. The old patch tried to add LRU eviction of caching data structure. Due to multiple layers of interfaces that users could be using, it was not clear where to put the functionality. While we work out on where to put that functionality, it'll be great to add this minimum interface change so that the user could implement their own memory management. More specifically: * Add a clearLineTable method for DWARFDebugLine which erases the given offset from the LineTableMap. * DWARFDebugContext adds the clearLineTableForUnit method that leverages clearLineTable to remove the object corresponding to a given compile unit, for memory management purposes. When it is referred to again, the line table object will be repopulated. Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D90006 --- .../llvm/DebugInfo/DWARF/DWARFContext.h | 4 + .../llvm/DebugInfo/DWARF/DWARFDebugLine.h | 1 + llvm/lib/DebugInfo/DWARF/DWARFContext.cpp | 16 ++++ llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp | 4 + llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp | 3 + .../DebugInfo/DWARF/DWARFDebugLineTest.cpp | 76 +++++++++++++++++++ 6 files changed, 104 insertions(+) diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h index 52ae538c55bb3..bf591ed554c6a 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h @@ -333,6 +333,10 @@ class DWARFContext : public DIContext { getLineTableForUnit(DWARFUnit *U, function_ref RecoverableErrorHandler); + // Clear the line table object corresponding to a compile unit for memory + // management purpose. When it's referred to again, it'll be re-populated. + void clearLineTableForUnit(DWARFUnit *U); + DataExtractor getStringExtractor() const { return DataExtractor(DObj->getStrSection(), false, 0); } diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLine.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLine.h index f3be160f7398a..86f90135f8d4e 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLine.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLine.h @@ -304,6 +304,7 @@ class DWARFDebugLine { getOrParseLineTable(DWARFDataExtractor &DebugLineData, uint64_t Offset, const DWARFContext &Ctx, const DWARFUnit *U, function_ref RecoverableErrorHandler); + void clearLineTable(uint64_t Offset); /// Helper to allow for parsing of an entire .debug_line section in sequence. class SectionParser { diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp index 4422141ff3c97..40c9c420ef6b8 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -1003,6 +1003,22 @@ Expected DWARFContext::getLineTableForUnit( RecoverableErrorHandler); } +void DWARFContext::clearLineTableForUnit(DWARFUnit *U) { + if (!Line) + return; + + auto UnitDIE = U->getUnitDIE(); + if (!UnitDIE) + return; + + auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list)); + if (!Offset) + return; + + uint64_t stmtOffset = *Offset + U->getLineTableOffset(); + Line->clearLineTable(stmtOffset); +} + void DWARFContext::parseNormalUnits() { if (!NormalUnits.empty()) return; diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp index 81394e2b24b74..2e0780e249aa4 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp @@ -601,6 +601,10 @@ Expected DWARFDebugLine::getOrParseLineTable( return LT; } +void DWARFDebugLine::clearLineTable(uint64_t Offset) { + LineTableMap.erase(Offset); +} + static StringRef getOpcodeName(uint8_t Opcode, uint8_t OpcodeBase) { assert(Opcode != 0); if (Opcode < OpcodeBase) diff --git a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp index 3879cf1aabab1..29c769834b06c 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp @@ -380,6 +380,9 @@ void DWARFUnit::clear() { AddrOffsetSectionBase = None; SU = nullptr; clearDIEs(false); + AddrDieMap.clear(); + if (DWO) + DWO->clear(); DWO.reset(); } diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp index 731aa4f0f3d0f..9c5e1c18705ae 100644 --- a/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp +++ b/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp @@ -330,6 +330,82 @@ TEST_P(DebugLineParameterisedFixture, GetOrParseLineTableValidTable) { // correctly. } +#ifdef _AIX +TEST_P(DebugLineParameterisedFixture, DISABLED_ClearLineValidTable) { +#else +TEST_P(DebugLineParameterisedFixture, ClearLineValidTable) { +#endif + if (!setupGenerator(Version)) + GTEST_SKIP(); + + SCOPED_TRACE("Checking Version " + std::to_string(Version) + ", Format " + + (Format == DWARF64 ? "DWARF64" : "DWARF32")); + + LineTable < = Gen->addLineTable(Format); + LT.addExtendedOpcode(9, DW_LNE_set_address, {{0xadd4e55, LineTable::Quad}}); + LT.addStandardOpcode(DW_LNS_copy, {}); + LT.addByte(0xaa); + LT.addExtendedOpcode(1, DW_LNE_end_sequence, {}); + + LineTable <2 = Gen->addLineTable(Format); + LT2.addExtendedOpcode(9, DW_LNE_set_address, {{0x11223344, LineTable::Quad}}); + LT2.addStandardOpcode(DW_LNS_copy, {}); + LT2.addByte(0xbb); + LT2.addExtendedOpcode(1, DW_LNE_end_sequence, {}); + LT2.addExtendedOpcode(9, DW_LNE_set_address, {{0x55667788, LineTable::Quad}}); + LT2.addStandardOpcode(DW_LNS_copy, {}); + LT2.addByte(0xcc); + LT2.addExtendedOpcode(1, DW_LNE_end_sequence, {}); + + generate(); + + // Check that we have what we expect before calling clearLineTable(). + auto ExpectedLineTable = Line.getOrParseLineTable(LineData, 0, *Context, + nullptr, RecordRecoverable); + ASSERT_TRUE((bool)ExpectedLineTable); + EXPECT_FALSE(Recoverable); + const DWARFDebugLine::LineTable *Expected = *ExpectedLineTable; + checkDefaultPrologue(Version, Format, Expected->Prologue, 16); + EXPECT_EQ(Expected->Sequences.size(), 1u); + + uint64_t SecondOffset = + Expected->Prologue.sizeofTotalLength() + Expected->Prologue.TotalLength; + Recoverable = Error::success(); + auto ExpectedLineTable2 = Line.getOrParseLineTable( + LineData, SecondOffset, *Context, nullptr, RecordRecoverable); + ASSERT_TRUE((bool)ExpectedLineTable2); + EXPECT_FALSE(Recoverable); + const DWARFDebugLine::LineTable *Expected2 = *ExpectedLineTable2; + checkDefaultPrologue(Version, Format, Expected2->Prologue, 32); + EXPECT_EQ(Expected2->Sequences.size(), 2u); + + // Check that we no longer get the line tables after clearLineTable(). + Line.clearLineTable(0); + Line.clearLineTable(SecondOffset); + EXPECT_EQ(Line.getLineTable(0), nullptr); + EXPECT_EQ(Line.getLineTable(SecondOffset), nullptr); + + // Check that if the same offset is requested, the contents match what we + // had before. + Recoverable = Error::success(); + auto ExpectedLineTable3 = Line.getOrParseLineTable( + LineData, 0, *Context, nullptr, RecordRecoverable); + ASSERT_TRUE((bool)ExpectedLineTable3); + EXPECT_FALSE(Recoverable); + const DWARFDebugLine::LineTable *Expected3 = *ExpectedLineTable3; + checkDefaultPrologue(Version, Format, Expected3->Prologue, 16); + EXPECT_EQ(Expected3->Sequences.size(), 1u); + + Recoverable = Error::success(); + auto ExpectedLineTable4 = Line.getOrParseLineTable( + LineData, SecondOffset, *Context, nullptr, RecordRecoverable); + ASSERT_TRUE((bool)ExpectedLineTable4); + EXPECT_FALSE(Recoverable); + const DWARFDebugLine::LineTable *Expected4 = *ExpectedLineTable4; + checkDefaultPrologue(Version, Format, Expected4->Prologue, 32); + EXPECT_EQ(Expected4->Sequences.size(), 2u); +} + #ifdef _AIX TEST_F(DebugLineBasicFixture, DISABLED_ErrorForReservedLength) { #else