Skip to content

Commit d39bc36

Browse files
author
Chen Zheng
committed
[debug-info] refactor emitDwarfUnitLength
remove `Hi` `Lo` argument from `emitDwarfUnitLength`, so we can make caller of emitDwarfUnitLength easier. Reviewed By: MaskRay, dblaikie, ikudrin Differential Revision: https://reviews.llvm.org/D96409
1 parent c90dac2 commit d39bc36

File tree

11 files changed

+69
-53
lines changed

11 files changed

+69
-53
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,9 @@ class AsmPrinter : public MachineFunctionPass {
645645

646646
/// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen
647647
/// according to the settings.
648-
void emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo,
649-
const Twine &Comment) const;
648+
/// Return the end symbol generated inside, the caller needs to emit it.
649+
MCSymbol *emitDwarfUnitLength(const Twine &Prefix,
650+
const Twine &Comment) const;
650651

651652
/// Emit reference to a call site with a specified encoding
652653
void emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo,

llvm/include/llvm/MC/MCStreamer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,8 +1089,9 @@ class MCStreamer {
10891089

10901090
/// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen
10911091
/// according to the settings.
1092-
virtual void emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo,
1093-
const Twine &Comment);
1092+
/// Return the end symbol generated inside, the caller needs to emit it.
1093+
virtual MCSymbol *emitDwarfUnitLength(const Twine &Prefix,
1094+
const Twine &Comment);
10941095
};
10951096

10961097
/// Create a dummy machine code streamer, which does nothing. This is useful for

llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class Dwarf5AccelTableWriter : public AccelTableWriter {
205205
: CompUnitCount(CompUnitCount), BucketCount(BucketCount),
206206
NameCount(NameCount) {}
207207

208-
void emit(const Dwarf5AccelTableWriter &Ctx) const;
208+
void emit(Dwarf5AccelTableWriter &Ctx);
209209
};
210210
struct AttributeEncoding {
211211
dwarf::Index Index;
@@ -216,8 +216,7 @@ class Dwarf5AccelTableWriter : public AccelTableWriter {
216216
DenseMap<uint32_t, SmallVector<AttributeEncoding, 2>> Abbreviations;
217217
ArrayRef<MCSymbol *> CompUnits;
218218
llvm::function_ref<unsigned(const DataT &)> getCUIndexForEntry;
219-
MCSymbol *ContributionStart = Asm->createTempSymbol("names_start");
220-
MCSymbol *ContributionEnd = Asm->createTempSymbol("names_end");
219+
MCSymbol *ContributionEnd = nullptr;
221220
MCSymbol *AbbrevStart = Asm->createTempSymbol("names_abbrev_start");
222221
MCSymbol *AbbrevEnd = Asm->createTempSymbol("names_abbrev_end");
223222
MCSymbol *EntryPool = Asm->createTempSymbol("names_entries");
@@ -240,7 +239,7 @@ class Dwarf5AccelTableWriter : public AccelTableWriter {
240239
ArrayRef<MCSymbol *> CompUnits,
241240
llvm::function_ref<unsigned(const DataT &)> GetCUIndexForEntry);
242241

243-
void emit() const;
242+
void emit();
244243
};
245244
} // namespace
246245

@@ -361,14 +360,12 @@ void AppleAccelTableWriter::emit() const {
361360
}
362361

363362
template <typename DataT>
364-
void Dwarf5AccelTableWriter<DataT>::Header::emit(
365-
const Dwarf5AccelTableWriter &Ctx) const {
363+
void Dwarf5AccelTableWriter<DataT>::Header::emit(Dwarf5AccelTableWriter &Ctx) {
366364
assert(CompUnitCount > 0 && "Index must have at least one CU.");
367365

368366
AsmPrinter *Asm = Ctx.Asm;
369-
Asm->emitDwarfUnitLength(Ctx.ContributionEnd, Ctx.ContributionStart,
370-
"Header: unit length");
371-
Asm->OutStreamer->emitLabel(Ctx.ContributionStart);
367+
Ctx.ContributionEnd =
368+
Asm->emitDwarfUnitLength("names", "Header: unit length");
372369
Asm->OutStreamer->AddComment("Header: version");
373370
Asm->emitInt16(Version);
374371
Asm->OutStreamer->AddComment("Header: padding");
@@ -526,7 +523,7 @@ Dwarf5AccelTableWriter<DataT>::Dwarf5AccelTableWriter(
526523
Abbreviations.try_emplace(Tag, UniformAttributes);
527524
}
528525

529-
template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emit() const {
526+
template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emit() {
530527
Header.emit(*this);
531528
emitCUList();
532529
emitBuckets();

llvm/lib/CodeGen/AsmPrinter/AddressPool.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
2525

2626
MCSymbol *AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
2727
static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize();
28-
StringRef Prefix = "debug_addr_";
29-
MCSymbol *BeginLabel = Asm.createTempSymbol(Prefix + "start");
30-
MCSymbol *EndLabel = Asm.createTempSymbol(Prefix + "end");
3128

32-
Asm.emitDwarfUnitLength(EndLabel, BeginLabel, "Length of contribution");
33-
Asm.OutStreamer->emitLabel(BeginLabel);
29+
MCSymbol *EndLabel =
30+
Asm.emitDwarfUnitLength("debug_addr", "Length of contribution");
3431
Asm.OutStreamer->AddComment("DWARF version number");
3532
Asm.emitInt16(Asm.getDwarfVersion());
3633
Asm.OutStreamer->AddComment("Address size");

llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ void AsmPrinter::emitDwarfUnitLength(uint64_t Length,
203203
OutStreamer->emitDwarfUnitLength(Length, Comment);
204204
}
205205

206-
void AsmPrinter::emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo,
207-
const Twine &Comment) const {
208-
OutStreamer->emitDwarfUnitLength(Hi, Lo, Comment);
206+
MCSymbol *AsmPrinter::emitDwarfUnitLength(const Twine &Prefix,
207+
const Twine &Comment) const {
208+
return OutStreamer->emitDwarfUnitLength(Prefix, Comment);
209209
}
210210

211211
void AsmPrinter::emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo,

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,12 +2374,8 @@ void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name,
23742374
TheU = Skeleton;
23752375

23762376
// Emit the header.
2377-
MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin");
2378-
MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end");
2379-
Asm->emitDwarfUnitLength(EndLabel, BeginLabel,
2380-
"Length of Public " + Name + " Info");
2381-
2382-
Asm->OutStreamer->emitLabel(BeginLabel);
2377+
MCSymbol *EndLabel = Asm->emitDwarfUnitLength(
2378+
"pub" + Name, "Length of Public " + Name + " Info");
23832379

23842380
Asm->OutStreamer->AddComment("DWARF Version");
23852381
Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION);

llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,13 +1701,10 @@ DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) {
17011701

17021702
void DwarfUnit::emitCommonHeader(bool UseOffsets, dwarf::UnitType UT) {
17031703
// Emit size of content not including length itself
1704-
if (!DD->useSectionsAsReferences()) {
1705-
StringRef Prefix = isDwoUnit() ? "debug_info_dwo_" : "debug_info_";
1706-
MCSymbol *BeginLabel = Asm->createTempSymbol(Prefix + "start");
1707-
EndLabel = Asm->createTempSymbol(Prefix + "end");
1708-
Asm->emitDwarfUnitLength(EndLabel, BeginLabel, "Length of Unit");
1709-
Asm->OutStreamer->emitLabel(BeginLabel);
1710-
} else
1704+
if (!DD->useSectionsAsReferences())
1705+
EndLabel = Asm->emitDwarfUnitLength(
1706+
isDwoUnit() ? "debug_info_dwo" : "debug_info", "Length of Unit");
1707+
else
17111708
Asm->emitDwarfUnitLength(getHeaderSize() + getUnitDie().getSize(),
17121709
"Length of Unit");
17131710

llvm/lib/MC/MCStreamer.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,12 +1006,19 @@ void MCStreamer::emitDwarfUnitLength(uint64_t Length, const Twine &Comment) {
10061006
emitIntValue(Length, dwarf::getDwarfOffsetByteSize(Context.getDwarfFormat()));
10071007
}
10081008

1009-
void MCStreamer::emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo,
1010-
const Twine &Comment) {
1009+
MCSymbol *MCStreamer::emitDwarfUnitLength(const Twine &Prefix,
1010+
const Twine &Comment) {
10111011
maybeEmitDwarf64Mark();
10121012
AddComment(Comment);
1013+
MCSymbol *Lo = Context.createTempSymbol(Prefix + "_start");
1014+
MCSymbol *Hi = Context.createTempSymbol(Prefix + "_end");
1015+
10131016
emitAbsoluteSymbolDiff(
10141017
Hi, Lo, dwarf::getDwarfOffsetByteSize(Context.getDwarfFormat()));
1018+
// emit the begin symbol after we generate the length field.
1019+
emitLabel(Lo);
1020+
// Return the Hi symbol to the caller.
1021+
return Hi;
10151022
}
10161023

10171024
void MCStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {

llvm/test/DebugInfo/X86/dwarf-pubnames-split.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
; Check that we get a symbol off of the debug_info section when using split dwarf and pubnames.
99

10-
; CHECK: .LpubTypes_begin0:
10+
; CHECK: .LpubTypes_start0:
1111
; CHECK-NEXT: .short 2 # DWARF Version
1212
; CHECK-NEXT: .long .Lcu_begin0 # Offset of Compilation Unit Info
1313

llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,21 @@ class AsmPrinterEmitDwarfSymbolReferenceTest : public AsmPrinterFixtureBase {
5050
if (!AsmPrinterFixtureBase::init(TripleStr, DwarfVersion, DwarfFormat))
5151
return false;
5252

53-
// Create a symbol which will be emitted in the tests and associate it
54-
// with a section because that is required in some code paths.
53+
// AsmPrinter::emitDwarfSymbolReference(Label, true) gets the associated
54+
// section from `Label` to find its BeginSymbol.
55+
// Prepare the test symbol `Val` accordingly.
5556

5657
Val = TestPrinter->getCtx().createTempSymbol();
57-
Sec = TestPrinter->getCtx().getELFSection(".tst", ELF::SHT_PROGBITS, 0);
58+
MCSection *Sec =
59+
TestPrinter->getCtx().getELFSection(".tst", ELF::SHT_PROGBITS, 0);
5860
SecBeginSymbol = Sec->getBeginSymbol();
5961
TestPrinter->getMS().SwitchSection(Sec);
60-
TestPrinter->getMS().emitLabel(Val);
62+
Val->setFragment(&Sec->getDummyFragment());
63+
6164
return true;
6265
}
6366

6467
MCSymbol *Val = nullptr;
65-
MCSection *Sec = nullptr;
6668
MCSymbol *SecBeginSymbol = nullptr;
6769
};
6870

@@ -326,32 +328,49 @@ class AsmPrinterEmitDwarfUnitLengthAsHiLoDiffTest
326328
if (!AsmPrinterFixtureBase::init(TripleStr, DwarfVersion, DwarfFormat))
327329
return false;
328330

329-
Hi = TestPrinter->getCtx().createTempSymbol();
330-
Lo = TestPrinter->getCtx().createTempSymbol();
331331
return true;
332332
}
333-
334-
MCSymbol *Hi = nullptr;
335-
MCSymbol *Lo = nullptr;
336333
};
337334

338335
TEST_F(AsmPrinterEmitDwarfUnitLengthAsHiLoDiffTest, DWARF32) {
339336
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
340337
return;
341338

342-
EXPECT_CALL(TestPrinter->getMS(), emitAbsoluteSymbolDiff(Hi, Lo, 4));
343-
TestPrinter->getAP()->emitDwarfUnitLength(Hi, Lo, "");
339+
InSequence S;
340+
const MCSymbol *Hi = nullptr;
341+
const MCSymbol *Lo = nullptr;
342+
EXPECT_CALL(TestPrinter->getMS(), emitAbsoluteSymbolDiff(_, _, 4))
343+
.WillOnce(DoAll(SaveArg<0>(&Hi), SaveArg<1>(&Lo)));
344+
MCSymbol *LTmp = nullptr;
345+
EXPECT_CALL(TestPrinter->getMS(), emitLabel(_, _))
346+
.WillOnce(SaveArg<0>(&LTmp));
347+
348+
MCSymbol *HTmp = TestPrinter->getAP()->emitDwarfUnitLength("", "");
349+
EXPECT_NE(Lo, nullptr);
350+
EXPECT_EQ(Lo, LTmp);
351+
EXPECT_NE(Hi, nullptr);
352+
EXPECT_EQ(Hi, HTmp);
344353
}
345354

346355
TEST_F(AsmPrinterEmitDwarfUnitLengthAsHiLoDiffTest, DWARF64) {
347356
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF64))
348357
return;
349358

350359
InSequence S;
360+
const MCSymbol *Hi = nullptr;
361+
const MCSymbol *Lo = nullptr;
351362
EXPECT_CALL(TestPrinter->getMS(), emitIntValue(dwarf::DW_LENGTH_DWARF64, 4));
352-
EXPECT_CALL(TestPrinter->getMS(), emitAbsoluteSymbolDiff(Hi, Lo, 8));
353-
354-
TestPrinter->getAP()->emitDwarfUnitLength(Hi, Lo, "");
363+
EXPECT_CALL(TestPrinter->getMS(), emitAbsoluteSymbolDiff(_, _, 8))
364+
.WillOnce(DoAll(SaveArg<0>(&Hi), SaveArg<1>(&Lo)));
365+
MCSymbol *LTmp = nullptr;
366+
EXPECT_CALL(TestPrinter->getMS(), emitLabel(_, _))
367+
.WillOnce(SaveArg<0>(&LTmp));
368+
369+
MCSymbol *HTmp = TestPrinter->getAP()->emitDwarfUnitLength("", "");
370+
EXPECT_NE(Lo, nullptr);
371+
EXPECT_EQ(Lo, LTmp);
372+
EXPECT_NE(Hi, nullptr);
373+
EXPECT_EQ(Hi, HTmp);
355374
}
356375

357376
class AsmPrinterHandlerTest : public AsmPrinterFixtureBase {

0 commit comments

Comments
 (0)