Skip to content

Commit de44455

Browse files
Add zlib compression to the debug_str_offset section
In MCCAS, the DWARF5 size is larger than the DWARF4 size because of the presence of the debug_str_offset section. There is not much we can do to improve the deduplication rate of that section, so the only thing to do is to compress the data in that cas block, which is what this patch adds support for.
1 parent 9384d0c commit de44455

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

llvm/include/llvm/MCCAS/MCCASObjectV1.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ class MCCASBuilder {
588588

589589
Expected<SmallVector<DebugStrRef, 0>> createDebugStringRefs();
590590

591+
std::optional<Expected<DebugStrOffsetsRef>> createDebugStrOffsetsRef();
592+
591593
template <typename SectionTy>
592594
std::optional<Expected<SectionTy>> createGenericDebugRef(MCSection *Section);
593595

llvm/lib/MCCAS/MCCASObjectV1.cpp

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,9 +1375,46 @@ static Expected<uint64_t> materializeGenericDebugSection(MCCASReader &Reader,
13751375
Expected<uint64_t>
13761376
DebugStringOffsetsSectionRef::materialize(MCCASReader &Reader,
13771377
raw_ostream *Stream) const {
1378+
// Start a new section for relocations.
1379+
Reader.Relocations.emplace_back();
1380+
SmallVector<char, 0> SectionContents;
1381+
raw_svector_ostream SectionStream(SectionContents);
1382+
1383+
unsigned Size = 0;
13781384
StringRef Remaining = getData();
1379-
return materializeGenericDebugSection<DebugStringOffsetsSectionRef>(
1380-
Reader, Remaining, *this);
1385+
auto Refs = DebugStringOffsetsSectionRef::decodeReferences(*this, Remaining);
1386+
if (!Refs)
1387+
return Refs.takeError();
1388+
1389+
for (auto ID : *Refs) {
1390+
auto FragmentSize = Reader.materializeSection(ID, &SectionStream);
1391+
if (!FragmentSize)
1392+
return FragmentSize.takeError();
1393+
Size += *FragmentSize;
1394+
}
1395+
1396+
if (auto E = decodeRelocations(Reader, Remaining))
1397+
return std::move(E);
1398+
1399+
#if LLVM_ENABLE_ZLIB
1400+
StringRef SectionStringRef = toStringRef(SectionContents);
1401+
ArrayRef<uint8_t> BufRef = arrayRefFromStringRef(SectionStringRef);
1402+
assert(BufRef.size() >= 8 &&
1403+
"Debug String Offset buffer less than 8 bytes in size!");
1404+
// The zlib decompress function needs to know the uncompressed size of the
1405+
// buffer. That size is stored as a ULEB at the end of the buffer
1406+
auto UncompressedSize = decodeULEB128(BufRef.data() + BufRef.size() - 8);
1407+
BufRef = BufRef.drop_back(8);
1408+
SmallVector<uint8_t> OutBuff;
1409+
if (auto E = compression::zlib::decompress(BufRef, OutBuff, UncompressedSize))
1410+
return E;
1411+
SectionStringRef = toStringRef(OutBuff);
1412+
Reader.OS << SectionStringRef;
1413+
return UncompressedSize;
1414+
#endif
1415+
1416+
Reader.OS << SectionContents;
1417+
return Size;
13811418
}
13821419

13831420
Expected<uint64_t> DebugLocSectionRef::materialize(MCCASReader &Reader,
@@ -2625,10 +2662,44 @@ MCCASBuilder::createGenericDebugRef(MCSection *Section) {
26252662
return *DebugCASRef;
26262663
}
26272664

2665+
std::optional<Expected<DebugStrOffsetsRef>>
2666+
MCCASBuilder::createDebugStrOffsetsRef() {
2667+
2668+
if (!DwarfSections.StrOffsets ||
2669+
!DwarfSections.StrOffsets->getFragmentList().size())
2670+
return std::nullopt;
2671+
2672+
auto DebugStrOffsetsData = mergeMCFragmentContents(
2673+
DwarfSections.StrOffsets->getFragmentList(), false);
2674+
2675+
if (!DebugStrOffsetsData)
2676+
return DebugStrOffsetsData.takeError();
2677+
2678+
#if LLVM_ENABLE_ZLIB
2679+
SmallVector<uint8_t> CompressedBuff;
2680+
compression::zlib::compress(
2681+
arrayRefFromStringRef(toStringRef(*DebugStrOffsetsData)), CompressedBuff);
2682+
// Reserve 8 bytes for ULEB to store the size of the uncompressed data.
2683+
CompressedBuff.append(8, 0);
2684+
encodeULEB128(DebugStrOffsetsData->size(), CompressedBuff.end() - 8,
2685+
8 /*Pad to*/);
2686+
auto DbgStrOffsetsRef =
2687+
DebugStrOffsetsRef::create(*this, toStringRef(CompressedBuff));
2688+
if (!DbgStrOffsetsRef)
2689+
return DbgStrOffsetsRef.takeError();
2690+
return *DbgStrOffsetsRef;
2691+
#else
2692+
auto DbgStrOffsetsRef =
2693+
DebugStrOffsetsRef::create(*this, toStringRef(CompressedBuff));
2694+
if (!DbgStrOffsetsRef)
2695+
return DbgStrOffsetsRef.takeError();
2696+
return *DbgStrOffsetsRef;
2697+
#endif
2698+
}
2699+
26282700
Error MCCASBuilder::createDebugStrOffsetsSection() {
26292701

2630-
auto MaybeDebugStringOffsetsRef =
2631-
createGenericDebugRef<DebugStrOffsetsRef>(DwarfSections.StrOffsets);
2702+
auto MaybeDebugStringOffsetsRef = createDebugStrOffsetsRef();
26322703
if (!MaybeDebugStringOffsetsRef)
26332704
return Error::success();
26342705

0 commit comments

Comments
 (0)