From ea6c8b013e4881cbfa590a413c9903143cb27ca7 Mon Sep 17 00:00:00 2001 From: Alexander Yermolovich Date: Fri, 14 Jan 2022 17:25:30 -0800 Subject: [PATCH] [BOLT][DWARF] Reduce overhead for sized dealloc This is a follow up to Fix size mismatch error with jemalloc. 4243b6582cf3bb5fbcde908913d4779ded731321 Although that fix works it increased memory footprint. With this patch we go back to original memory footprint. Reviewed By: maksfb Differential Revision: https://reviews.llvm.org/D117341 --- bolt/include/bolt/Core/DebugData.h | 37 +++++++++++++++++++++++++++--- bolt/lib/Core/DebugData.cpp | 31 +++++++++++-------------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/bolt/include/bolt/Core/DebugData.h b/bolt/include/bolt/Core/DebugData.h index 7a0c3d22d488a4..832123fde38a03 100644 --- a/bolt/include/bolt/Core/DebugData.h +++ b/bolt/include/bolt/Core/DebugData.h @@ -498,8 +498,6 @@ class DebugInfoBinaryPatcher : public SimpleBinaryPatcher { Patch(uint32_t O, DebugPatchKind K) : Offset(O), Kind(K) {} DebugPatchKind getKind() const { return Kind; } - virtual ~Patch(){}; - static bool classof(const Patch *Writer) { return Writer->getKind() == DebugPatchKind::PatchBaseClass; } @@ -656,9 +654,42 @@ class DebugInfoBinaryPatcher : public SimpleBinaryPatcher { std::unordered_map computeNewOffsets(); private: + struct PatchDeleter { + void operator()(Patch *P) const { + Patch *Pp = reinterpret_cast(P); + switch (Pp->Kind) { + default: + llvm_unreachable( + "Not all delete instances handled for Patch derived classes"); + case DebugPatchKind::PatchValue32: + delete reinterpret_cast(P); + break; + case DebugPatchKind::PatchValue64to32: + delete reinterpret_cast(P); + break; + case DebugPatchKind::PatchValue64: + delete reinterpret_cast(P); + break; + case DebugPatchKind::PatchValueVariable: + delete reinterpret_cast(P); + break; + case DebugPatchKind::ReferencePatchValue: + delete reinterpret_cast(P); + break; + case DebugPatchKind::DWARFUnitOffsetBaseLabel: + delete reinterpret_cast(P); + break; + case DebugPatchKind::DestinationReferenceLabel: + delete reinterpret_cast(P); + break; + } + } + }; + using UniquePatchPtrType = std::unique_ptr; + uint64_t DWPUnitOffset{0}; uint32_t ChangeInSize{0}; - std::vector> DebugPatches; + std::vector DebugPatches; /// Mutex used for parallel processing of debug info. std::mutex WriterMutex; /// Stores fully resolved addresses of DIEs that are being referenced. diff --git a/bolt/lib/Core/DebugData.cpp b/bolt/lib/Core/DebugData.cpp index 6185b89d41199c..396bf8aebf52f6 100644 --- a/bolt/lib/Core/DebugData.cpp +++ b/bolt/lib/Core/DebugData.cpp @@ -367,7 +367,7 @@ DebugAddrWriter *DebugLoclistWriter::AddrWriter = nullptr; void DebugInfoBinaryPatcher::addUnitBaseOffsetLabel(uint64_t Offset) { Offset -= DWPUnitOffset; std::lock_guard Lock(WriterMutex); - DebugPatches.emplace_back(std::make_unique(Offset)); + DebugPatches.emplace_back(new DWARFUnitOffsetBaseLabel(Offset)); } void DebugInfoBinaryPatcher::addDestinationReferenceLabel(uint64_t Offset) { @@ -377,8 +377,7 @@ void DebugInfoBinaryPatcher::addDestinationReferenceLabel(uint64_t Offset) { if (!RetVal.second) return; - DebugPatches.emplace_back( - std::make_unique(Offset)); + DebugPatches.emplace_back(new DestinationReferenceLabel(Offset)); } void DebugInfoBinaryPatcher::addReferenceToPatch(uint64_t Offset, @@ -388,8 +387,8 @@ void DebugInfoBinaryPatcher::addReferenceToPatch(uint64_t Offset, Offset -= DWPUnitOffset; DestinationOffset -= DWPUnitOffset; std::lock_guard Lock(WriterMutex); - DebugPatches.emplace_back(std::make_unique( - Offset, OldValueSize, DestinationOffset, Form)); + DebugPatches.emplace_back( + new DebugPatchReference(Offset, OldValueSize, DestinationOffset, Form)); } void DebugInfoBinaryPatcher::addUDataPatch(uint64_t Offset, uint64_t NewValue, @@ -397,13 +396,13 @@ void DebugInfoBinaryPatcher::addUDataPatch(uint64_t Offset, uint64_t NewValue, Offset -= DWPUnitOffset; std::lock_guard Lock(WriterMutex); DebugPatches.emplace_back( - std::make_unique(Offset, OldValueSize, NewValue)); + new DebugPatchVariableSize(Offset, OldValueSize, NewValue)); } void DebugInfoBinaryPatcher::addLE64Patch(uint64_t Offset, uint64_t NewValue) { Offset -= DWPUnitOffset; std::lock_guard Lock(WriterMutex); - DebugPatches.emplace_back(std::make_unique(Offset, NewValue)); + DebugPatches.emplace_back(new DebugPatch64(Offset, NewValue)); } void DebugInfoBinaryPatcher::addLE32Patch(uint64_t Offset, uint32_t NewValue, @@ -411,10 +410,9 @@ void DebugInfoBinaryPatcher::addLE32Patch(uint64_t Offset, uint32_t NewValue, Offset -= DWPUnitOffset; std::lock_guard Lock(WriterMutex); if (OldValueSize == 4) - DebugPatches.emplace_back(std::make_unique(Offset, NewValue)); + DebugPatches.emplace_back(new DebugPatch32(Offset, NewValue)); else - DebugPatches.emplace_back( - std::make_unique(Offset, NewValue)); + DebugPatches.emplace_back(new DebugPatch64to32(Offset, NewValue)); } void SimpleBinaryPatcher::addBinaryPatch(uint64_t Offset, @@ -477,15 +475,14 @@ std::string SimpleBinaryPatcher::patchBinary(StringRef BinaryContents) { std::unordered_map DebugInfoBinaryPatcher::computeNewOffsets() { std::unordered_map CUMap; - std::sort( - DebugPatches.begin(), DebugPatches.end(), - [](const std::unique_ptr &V1, const std::unique_ptr &V2) { - return V1.get()->Offset < V2.get()->Offset; - }); + std::sort(DebugPatches.begin(), DebugPatches.end(), + [](const UniquePatchPtrType &V1, const UniquePatchPtrType &V2) { + return V1.get()->Offset < V2.get()->Offset; + }); // Calculating changes in .debug_info size from Patches to build a map of old // to updated reference destination offsets. - for (std::unique_ptr &PatchBase : DebugPatches) { + for (UniquePatchPtrType &PatchBase : DebugPatches) { Patch *P = PatchBase.get(); switch (P->Kind) { default: @@ -546,7 +543,7 @@ std::string DebugInfoBinaryPatcher::patchBinary(StringRef BinaryContents) { // Applying all the patches replacing current entry. // This might change the size of .debug_info section. - for (const std::unique_ptr &PatchBase : DebugPatches) { + for (const UniquePatchPtrType &PatchBase : DebugPatches) { Patch *P = PatchBase.get(); switch (P->Kind) { default: