Skip to content

Commit

Permalink
[BOLT][DWARF] Reduce overhead for sized dealloc
Browse files Browse the repository at this point in the history
This is a follow up to Fix size mismatch error with jemalloc.
4243b65
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
  • Loading branch information
ayermolo committed Jan 15, 2022
1 parent 0a46b6e commit ea6c8b0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
37 changes: 34 additions & 3 deletions bolt/include/bolt/Core/DebugData.h
Expand Up @@ -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;
}
Expand Down Expand Up @@ -656,9 +654,42 @@ class DebugInfoBinaryPatcher : public SimpleBinaryPatcher {
std::unordered_map<uint32_t, uint32_t> computeNewOffsets();

private:
struct PatchDeleter {
void operator()(Patch *P) const {
Patch *Pp = reinterpret_cast<Patch *>(P);
switch (Pp->Kind) {
default:
llvm_unreachable(
"Not all delete instances handled for Patch derived classes");
case DebugPatchKind::PatchValue32:
delete reinterpret_cast<DebugPatch32 *>(P);
break;
case DebugPatchKind::PatchValue64to32:
delete reinterpret_cast<DebugPatch64to32 *>(P);
break;
case DebugPatchKind::PatchValue64:
delete reinterpret_cast<DebugPatch64 *>(P);
break;
case DebugPatchKind::PatchValueVariable:
delete reinterpret_cast<DebugPatchVariableSize *>(P);
break;
case DebugPatchKind::ReferencePatchValue:
delete reinterpret_cast<DebugPatchReference *>(P);
break;
case DebugPatchKind::DWARFUnitOffsetBaseLabel:
delete reinterpret_cast<DWARFUnitOffsetBaseLabel *>(P);
break;
case DebugPatchKind::DestinationReferenceLabel:
delete reinterpret_cast<DestinationReferenceLabel *>(P);
break;
}
}
};
using UniquePatchPtrType = std::unique_ptr<Patch, PatchDeleter>;

uint64_t DWPUnitOffset{0};
uint32_t ChangeInSize{0};
std::vector<std::unique_ptr<Patch>> DebugPatches;
std::vector<UniquePatchPtrType> DebugPatches;
/// Mutex used for parallel processing of debug info.
std::mutex WriterMutex;
/// Stores fully resolved addresses of DIEs that are being referenced.
Expand Down
31 changes: 14 additions & 17 deletions bolt/lib/Core/DebugData.cpp
Expand Up @@ -367,7 +367,7 @@ DebugAddrWriter *DebugLoclistWriter::AddrWriter = nullptr;
void DebugInfoBinaryPatcher::addUnitBaseOffsetLabel(uint64_t Offset) {
Offset -= DWPUnitOffset;
std::lock_guard<std::mutex> Lock(WriterMutex);
DebugPatches.emplace_back(std::make_unique<DWARFUnitOffsetBaseLabel>(Offset));
DebugPatches.emplace_back(new DWARFUnitOffsetBaseLabel(Offset));
}

void DebugInfoBinaryPatcher::addDestinationReferenceLabel(uint64_t Offset) {
Expand All @@ -377,8 +377,7 @@ void DebugInfoBinaryPatcher::addDestinationReferenceLabel(uint64_t Offset) {
if (!RetVal.second)
return;

DebugPatches.emplace_back(
std::make_unique<DestinationReferenceLabel>(Offset));
DebugPatches.emplace_back(new DestinationReferenceLabel(Offset));
}

void DebugInfoBinaryPatcher::addReferenceToPatch(uint64_t Offset,
Expand All @@ -388,33 +387,32 @@ void DebugInfoBinaryPatcher::addReferenceToPatch(uint64_t Offset,
Offset -= DWPUnitOffset;
DestinationOffset -= DWPUnitOffset;
std::lock_guard<std::mutex> Lock(WriterMutex);
DebugPatches.emplace_back(std::make_unique<DebugPatchReference>(
Offset, OldValueSize, DestinationOffset, Form));
DebugPatches.emplace_back(
new DebugPatchReference(Offset, OldValueSize, DestinationOffset, Form));
}

void DebugInfoBinaryPatcher::addUDataPatch(uint64_t Offset, uint64_t NewValue,
uint32_t OldValueSize) {
Offset -= DWPUnitOffset;
std::lock_guard<std::mutex> Lock(WriterMutex);
DebugPatches.emplace_back(
std::make_unique<DebugPatchVariableSize>(Offset, OldValueSize, NewValue));
new DebugPatchVariableSize(Offset, OldValueSize, NewValue));
}

void DebugInfoBinaryPatcher::addLE64Patch(uint64_t Offset, uint64_t NewValue) {
Offset -= DWPUnitOffset;
std::lock_guard<std::mutex> Lock(WriterMutex);
DebugPatches.emplace_back(std::make_unique<DebugPatch64>(Offset, NewValue));
DebugPatches.emplace_back(new DebugPatch64(Offset, NewValue));
}

void DebugInfoBinaryPatcher::addLE32Patch(uint64_t Offset, uint32_t NewValue,
uint32_t OldValueSize) {
Offset -= DWPUnitOffset;
std::lock_guard<std::mutex> Lock(WriterMutex);
if (OldValueSize == 4)
DebugPatches.emplace_back(std::make_unique<DebugPatch32>(Offset, NewValue));
DebugPatches.emplace_back(new DebugPatch32(Offset, NewValue));
else
DebugPatches.emplace_back(
std::make_unique<DebugPatch64to32>(Offset, NewValue));
DebugPatches.emplace_back(new DebugPatch64to32(Offset, NewValue));
}

void SimpleBinaryPatcher::addBinaryPatch(uint64_t Offset,
Expand Down Expand Up @@ -477,15 +475,14 @@ std::string SimpleBinaryPatcher::patchBinary(StringRef BinaryContents) {
std::unordered_map<uint32_t, uint32_t>
DebugInfoBinaryPatcher::computeNewOffsets() {
std::unordered_map<uint32_t, uint32_t> CUMap;
std::sort(
DebugPatches.begin(), DebugPatches.end(),
[](const std::unique_ptr<Patch> &V1, const std::unique_ptr<Patch> &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<Patch> &PatchBase : DebugPatches) {
for (UniquePatchPtrType &PatchBase : DebugPatches) {
Patch *P = PatchBase.get();
switch (P->Kind) {
default:
Expand Down Expand Up @@ -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<Patch> &PatchBase : DebugPatches) {
for (const UniquePatchPtrType &PatchBase : DebugPatches) {
Patch *P = PatchBase.get();
switch (P->Kind) {
default:
Expand Down

0 comments on commit ea6c8b0

Please sign in to comment.