Skip to content

Commit 69f2838

Browse files
Merge commit '7dbfcfa735f28a3bd33b465c686a20c4974373ae' into pulldown-ww25
2 parents 567240b + 7dbfcfa commit 69f2838

File tree

1,491 files changed

+102499
-46081
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,491 files changed

+102499
-46081
lines changed

bolt/include/bolt/Core/DebugData.h

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ namespace llvm {
3434

3535
namespace bolt {
3636

37+
struct AttrInfo {
38+
DWARFFormValue V;
39+
const DWARFAbbreviationDeclaration *AbbrevDecl;
40+
uint64_t Offset;
41+
uint32_t Size; // Size of the attribute.
42+
};
43+
44+
/// Finds attributes FormValue and Offset.
45+
///
46+
/// \param DIE die to look up in.
47+
/// \param AbbrevDecl abbrev declaration for the die.
48+
/// \param Index an index in Abbrev declaration entry.
49+
Optional<AttrInfo>
50+
findAttributeInfo(const DWARFDie DIE,
51+
const DWARFAbbreviationDeclaration *AbbrevDecl,
52+
uint32_t Index);
53+
54+
/// Finds attributes FormValue and Offset.
55+
///
56+
/// \param DIE die to look up in.
57+
/// \param Attr the attribute to extract.
58+
/// \return an optional AttrInfo with DWARFFormValue and Offset.
59+
Optional<AttrInfo> findAttributeInfo(const DWARFDie DIE, dwarf::Attribute Attr);
60+
3761
// DWARF5 Header in order of encoding.
3862
// Types represent encodnig sizes.
3963
using UnitLengthType = uint32_t;
@@ -447,23 +471,31 @@ class DebugStrWriter {
447471
BinaryContext &BC;
448472
};
449473

474+
class DebugInfoBinaryPatcher;
475+
class DebugAbbrevWriter;
450476
enum class LocWriterKind { DebugLocWriter, DebugLoclistWriter };
451477

452478
/// Serializes part of a .debug_loc DWARF section with LocationLists.
453479
class SimpleBinaryPatcher;
454480
class DebugLocWriter {
481+
protected:
482+
DebugLocWriter(uint8_t DwarfVersion, LocWriterKind Kind)
483+
: DwarfVersion(DwarfVersion), Kind(Kind) {
484+
init();
485+
}
486+
455487
public:
456-
DebugLocWriter() = delete;
457-
DebugLocWriter(BinaryContext *BC);
488+
DebugLocWriter() { init(); };
458489
virtual ~DebugLocWriter(){};
459490

460491
/// Writes out location lists and stores internal patches.
461-
virtual void addList(uint64_t AttrOffset, uint32_t LocListIndex,
462-
DebugLocationsVector &&LocList);
492+
virtual void addList(AttrInfo &AttrVal, DebugLocationsVector &LocList,
493+
DebugInfoBinaryPatcher &DebugInfoPatcher,
494+
DebugAbbrevWriter &AbbrevWriter);
463495

464496
/// Writes out locations in to a local buffer, and adds Debug Info patches.
465-
virtual void finalize(uint64_t SectionOffset,
466-
SimpleBinaryPatcher &DebugInfoPatcher);
497+
virtual void finalize(DebugInfoBinaryPatcher &DebugInfoPatcher,
498+
DebugAbbrevWriter &AbbrevWriter);
467499

468500
/// Return internal buffer.
469501
virtual std::unique_ptr<DebugBufferVector> getBuffer();
@@ -485,13 +517,15 @@ class DebugLocWriter {
485517
std::unique_ptr<raw_svector_ostream> LocStream;
486518
/// Current offset in the section (updated as new entries are written).
487519
/// Starts with 0 here since this only writes part of a full location lists
488-
/// section. In the final section, the first 16 bytes are reserved for an
489-
/// empty list.
490-
uint32_t SectionOffset{0};
520+
/// section. In the final section, for DWARF4, the first 16 bytes are reserved
521+
/// for an empty list.
522+
static uint32_t LocSectionOffset;
491523
uint8_t DwarfVersion{4};
492524
LocWriterKind Kind{LocWriterKind::DebugLocWriter};
493525

494526
private:
527+
/// Inits all the related data structures.
528+
void init();
495529
struct LocListDebugInfoPatchType {
496530
uint64_t DebugInfoAttrOffset;
497531
uint64_t LocListOffset;
@@ -501,36 +535,39 @@ class DebugLocWriter {
501535
/// The list of debug info patches to be made once individual
502536
/// location list writers have been filled
503537
VectorLocListDebugInfoPatchType LocListDebugInfoPatches;
504-
505-
using VectorEmptyLocListAttributes = std::vector<uint64_t>;
506-
/// Contains all the attributes pointing to empty location list.
507-
VectorEmptyLocListAttributes EmptyAttrLists;
508538
};
509539

510540
class DebugLoclistWriter : public DebugLocWriter {
511541
public:
512542
~DebugLoclistWriter() {}
513543
DebugLoclistWriter() = delete;
514-
DebugLoclistWriter(BinaryContext *BC, DWARFUnit &Unit,
515-
uint32_t LocListsBaseAttrOffset, uint8_t DV, bool SD)
516-
: DebugLocWriter(BC), CU(Unit),
517-
LocListsBaseAttrOffset(LocListsBaseAttrOffset), IsSplitDwarf(SD) {
518-
Kind = LocWriterKind::DebugLoclistWriter;
519-
DwarfVersion = DV;
544+
DebugLoclistWriter(DWARFUnit &Unit, uint8_t DV, bool SD)
545+
: DebugLocWriter(DV, LocWriterKind::DebugLoclistWriter), CU(Unit),
546+
IsSplitDwarf(SD) {
520547
assert(DebugLoclistWriter::AddrWriter &&
521548
"Please use SetAddressWriter to initialize "
522549
"DebugAddrWriter before instantiation.");
550+
if (DwarfVersion >= 5) {
551+
LocBodyBuffer = std::make_unique<DebugBufferVector>();
552+
LocBodyStream = std::make_unique<raw_svector_ostream>(*LocBodyBuffer);
553+
} else {
554+
// Writing out empty location list to which all references to empty
555+
// location lists will point.
556+
const char Zeroes[16] = {0};
557+
*LocStream << StringRef(Zeroes, 16);
558+
}
523559
}
524560

525561
static void setAddressWriter(DebugAddrWriter *AddrW) { AddrWriter = AddrW; }
526562

527563
/// Stores location lists internally to be written out during finalize phase.
528-
virtual void addList(uint64_t AttrOffset, uint32_t LocListIndex,
529-
DebugLocationsVector &&LocList) override;
564+
virtual void addList(AttrInfo &AttrVal, DebugLocationsVector &LocList,
565+
DebugInfoBinaryPatcher &DebugInfoPatcher,
566+
DebugAbbrevWriter &AbbrevWriter) override;
530567

531568
/// Writes out locations in to a local buffer and applies debug info patches.
532-
void finalize(uint64_t SectionOffset,
533-
SimpleBinaryPatcher &DebugInfoPatcher) override;
569+
void finalize(DebugInfoBinaryPatcher &DebugInfoPatcher,
570+
DebugAbbrevWriter &AbbrevWriter) override;
534571

535572
/// Returns CU ID.
536573
/// For Skelton CU it is a CU Offset.
@@ -548,36 +585,21 @@ class DebugLoclistWriter : public DebugLocWriter {
548585
bool isSplitDwarf() const { return IsSplitDwarf; }
549586

550587
constexpr static uint32_t InvalidIndex = UINT32_MAX;
551-
constexpr static uint32_t InvalidLocListsBaseAttrOffset = UINT32_MAX;
552588

553589
private:
554590
/// Writes out locations in to a local buffer and applies debug info patches.
555-
void finalizeDWARFLegacy(uint64_t SectionOffset,
556-
SimpleBinaryPatcher &DebugInfoPatcher);
557-
558-
/// Writes out locations in to a local buffer and applies debug info patches.
559-
void finalizeDWARF5(uint64_t SectionOffset,
560-
SimpleBinaryPatcher &DebugInfoPatcher);
561-
562-
struct LocPatch {
563-
uint64_t AttrOffset{0};
564-
uint32_t Index;
565-
DebugLocationsVector LocList;
566-
};
567-
using LocPatchVec = SmallVector<LocPatch, 4>;
568-
LocPatchVec Patches;
591+
void finalizeDWARF5(DebugInfoBinaryPatcher &DebugInfoPatcher,
592+
DebugAbbrevWriter &AbbrevWriter);
569593

570-
class Patch {
571-
public:
572-
Patch() = delete;
573-
Patch(uint64_t O, uint64_t A) : Offset(O), Address(A) {}
574-
uint64_t Offset{0};
575-
uint64_t Address{0};
576-
};
577594
static DebugAddrWriter *AddrWriter;
578595
DWARFUnit &CU;
579-
uint32_t LocListsBaseAttrOffset{InvalidLocListsBaseAttrOffset};
580596
bool IsSplitDwarf{false};
597+
// Used for DWARF5 to store location lists before being finalized.
598+
std::unique_ptr<DebugBufferVector> LocBodyBuffer;
599+
std::unique_ptr<raw_svector_ostream> LocBodyStream;
600+
std::vector<uint32_t> RelativeLocListOffsets;
601+
uint32_t NumberOfEntries{0};
602+
static uint32_t LoclistBaseOffset;
581603
};
582604

583605
enum class PatcherKind { SimpleBinaryPatcher, DebugInfoBinaryPatcher };
@@ -1156,18 +1178,6 @@ class DwarfLineTable {
11561178
// Returns DWARF Version for this line table.
11571179
uint16_t getDwarfVersion() const { return DwarfVersion; }
11581180
};
1159-
1160-
struct AttrInfo {
1161-
DWARFFormValue V;
1162-
uint64_t Offset;
1163-
uint32_t Size; // Size of the attribute.
1164-
};
1165-
1166-
Optional<AttrInfo>
1167-
findAttributeInfo(const DWARFDie DIE,
1168-
const DWARFAbbreviationDeclaration *AbbrevDecl,
1169-
uint32_t Index);
1170-
11711181
} // namespace bolt
11721182
} // namespace llvm
11731183

bolt/include/bolt/Passes/CallGraph.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
#ifndef BOLT_PASSES_CALLGRAPH_H
1010
#define BOLT_PASSES_CALLGRAPH_H
1111

12+
#include "llvm/Support/FileSystem.h"
13+
#include "llvm/Support/raw_ostream.h"
1214
#include <cassert>
1315
#include <cstdint>
14-
#include <cstdio>
1516
#include <unordered_set>
1617
#include <vector>
1718

@@ -160,31 +161,31 @@ class CallGraph {
160161
};
161162

162163
template <class L> void CallGraph::printDot(char *FileName, L GetLabel) const {
163-
FILE *File = fopen(FileName, "wt");
164-
if (!File)
164+
std::error_code EC;
165+
raw_fd_ostream OS(std::string(FileName), EC, sys::fs::OF_None);
166+
if (EC)
165167
return;
166168

167-
fprintf(File, "digraph g {\n");
169+
OS << "digraph g {\n";
168170
for (NodeId F = 0; F < Nodes.size(); F++) {
169171
if (Nodes[F].samples() == 0)
170172
continue;
171-
fprintf(File, "f%lu [label=\"%s\\nsamples=%u\\nsize=%u\"];\n", F,
172-
GetLabel(F), Nodes[F].samples(), Nodes[F].size());
173+
OS << "f" << F << " [label=\"" << GetLabel(F)
174+
<< "\\nsamples=" << Nodes[F].samples() << "\\nsize=" << Nodes[F].size()
175+
<< "\"];\n";
173176
}
174177
for (NodeId F = 0; F < Nodes.size(); F++) {
175178
if (Nodes[F].samples() == 0)
176179
continue;
177180
for (NodeId Dst : Nodes[F].successors()) {
178181
ArcConstIterator Arc = findArc(F, Dst);
179-
fprintf(
180-
File,
181-
"f%lu -> f%u [label=\"normWgt=%.3lf,weight=%.0lf,callOffset=%.1lf\"];"
182-
"\n",
183-
F, Dst, Arc->normalizedWeight(), Arc->weight(), Arc->avgCallOffset());
182+
OS << "f" << F << " -> f" << Dst
183+
<< " [label=\"normWgt=" << format("%.3lf", Arc->normalizedWeight())
184+
<< ",weight=" << format("%.0lf", Arc->weight())
185+
<< ",callOffset=" << format("%.1lf", Arc->avgCallOffset()) << "\"];\n";
184186
}
185187
}
186-
fprintf(File, "}\n");
187-
fclose(File);
188+
OS << "}\n";
188189
}
189190

190191
} // namespace bolt

bolt/include/bolt/Rewrite/DWARFRewriter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class DWARFRewriter {
112112
Optional<uint64_t> RangesBase = None);
113113

114114
std::unique_ptr<DebugBufferVector>
115-
makeFinalLocListsSection(SimpleBinaryPatcher &DebugInfoPatcher,
115+
makeFinalLocListsSection(DebugInfoBinaryPatcher &DebugInfoPatcher,
116116
DWARFVersion Version);
117117

118118
/// Finalize debug sections in the main binary.

0 commit comments

Comments
 (0)