[llvm] Introduce DwarfUnit::addBlock helper method (PR #168446)
Tom Tromey via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 18 06:27:50 PST 2025
https://github.com/tromey updated https://github.com/llvm/llvm-project/pull/168446
>From ff4b914d2ee30e6839176455260f82014bcb0bbe Mon Sep 17 00:00:00 2001
From: Tom Tromey <tromey at adacore.com>
Date: Thu, 30 Oct 2025 10:53:11 -0600
Subject: [PATCH] Introduce DwarfUnit::addBlock helper method
This patch is just a small cleanup that unifies the various spots that
add a DWARF expression to the output.
---
llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp | 86 ++++++-----------------
llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h | 3 +
2 files changed, 24 insertions(+), 65 deletions(-)
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index 1666a0e36b39a..e1a231e02aeb2 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -441,6 +441,15 @@ void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
addBlock(Die, Attribute, Block->BestForm(), Block);
}
+void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
+ const DIExpression *Expr) {
+ DIELoc *Loc = new (DIEValueAllocator) DIELoc;
+ DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
+ DwarfExpr.setMemoryLocationKind();
+ DwarfExpr.addExpression(Expr);
+ addBlock(Die, Attribute, DwarfExpr.finalize());
+}
+
void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, unsigned Column,
const DIFile *File) {
if (Line == 0)
@@ -824,27 +833,14 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIStringType *STy) {
if (auto *VarDIE = getDIE(Var))
addDIEEntry(Buffer, dwarf::DW_AT_string_length, *VarDIE);
} else if (DIExpression *Expr = STy->getStringLengthExp()) {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- // This is to describe the memory location of the
- // length of a Fortran deferred length string, so
- // lock it down as such.
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(Expr);
- addBlock(Buffer, dwarf::DW_AT_string_length, DwarfExpr.finalize());
+ addBlock(Buffer, dwarf::DW_AT_string_length, Expr);
} else {
uint64_t Size = STy->getSizeInBits() >> 3;
addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, Size);
}
if (DIExpression *Expr = STy->getStringLocationExp()) {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- // This is to describe the memory location of the
- // string, so lock it down as such.
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(Expr);
- addBlock(Buffer, dwarf::DW_AT_data_location, DwarfExpr.finalize());
+ addBlock(Buffer, dwarf::DW_AT_data_location, Expr);
}
if (STy->getEncoding()) {
@@ -1207,11 +1203,7 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
addDIEEntry(Buffer, dwarf::DW_AT_bit_size, *VarDIE);
} else if (auto *Exp =
dyn_cast_or_null<DIExpression>(CTy->getRawSizeInBits())) {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(Exp);
- addBlock(Buffer, dwarf::DW_AT_bit_size, DwarfExpr.finalize());
+ addBlock(Buffer, dwarf::DW_AT_bit_size, Exp);
} else {
uint64_t Size = CTy->getSizeInBits() >> 3;
// Add size if non-zero (derived types might be zero-sized.)
@@ -1607,11 +1599,7 @@ void DwarfUnit::constructSubrangeDIE(DIE &DW_Subrange, const DISubrangeType *SR,
if (auto *VarDIE = getDIE(BV))
addDIEEntry(DW_Subrange, Attr, *VarDIE);
} else if (auto *BE = dyn_cast_if_present<DIExpression *>(Bound)) {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(BE);
- addBlock(DW_Subrange, Attr, DwarfExpr.finalize());
+ addBlock(DW_Subrange, Attr, BE);
} else if (auto *BI = dyn_cast_if_present<ConstantInt *>(Bound)) {
if (Attr == dwarf::DW_AT_GNU_bias) {
if (BI->getSExtValue() != 0)
@@ -1649,11 +1637,7 @@ void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR) {
if (auto *VarDIE = getDIE(BV))
addDIEEntry(DW_Subrange, Attr, *VarDIE);
} else if (auto *BE = dyn_cast_if_present<DIExpression *>(Bound)) {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(BE);
- addBlock(DW_Subrange, Attr, DwarfExpr.finalize());
+ addBlock(DW_Subrange, Attr, BE);
} else if (auto *BI = dyn_cast_if_present<ConstantInt *>(Bound)) {
if (Attr == dwarf::DW_AT_count) {
if (BI->getSExtValue() != -1)
@@ -1699,11 +1683,7 @@ void DwarfUnit::constructGenericSubrangeDIE(DIE &Buffer,
addSInt(DwGenericSubrange, Attr, dwarf::DW_FORM_sdata,
BE->getElement(1));
} else {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(BE);
- addBlock(DwGenericSubrange, Attr, DwarfExpr.finalize());
+ addBlock(DwGenericSubrange, Attr, BE);
}
}
};
@@ -1770,44 +1750,28 @@ void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
if (auto *VarDIE = getDIE(Var))
addDIEEntry(Buffer, dwarf::DW_AT_data_location, *VarDIE);
} else if (DIExpression *Expr = CTy->getDataLocationExp()) {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(Expr);
- addBlock(Buffer, dwarf::DW_AT_data_location, DwarfExpr.finalize());
+ addBlock(Buffer, dwarf::DW_AT_data_location, Expr);
}
if (DIVariable *Var = CTy->getAssociated()) {
if (auto *VarDIE = getDIE(Var))
addDIEEntry(Buffer, dwarf::DW_AT_associated, *VarDIE);
} else if (DIExpression *Expr = CTy->getAssociatedExp()) {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(Expr);
- addBlock(Buffer, dwarf::DW_AT_associated, DwarfExpr.finalize());
+ addBlock(Buffer, dwarf::DW_AT_associated, Expr);
}
if (DIVariable *Var = CTy->getAllocated()) {
if (auto *VarDIE = getDIE(Var))
addDIEEntry(Buffer, dwarf::DW_AT_allocated, *VarDIE);
} else if (DIExpression *Expr = CTy->getAllocatedExp()) {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(Expr);
- addBlock(Buffer, dwarf::DW_AT_allocated, DwarfExpr.finalize());
+ addBlock(Buffer, dwarf::DW_AT_allocated, Expr);
}
if (auto *RankConst = CTy->getRankConst()) {
addSInt(Buffer, dwarf::DW_AT_rank, dwarf::DW_FORM_sdata,
RankConst->getSExtValue());
} else if (auto *RankExpr = CTy->getRankExp()) {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(RankExpr);
- addBlock(Buffer, dwarf::DW_AT_rank, DwarfExpr.finalize());
+ addBlock(Buffer, dwarf::DW_AT_rank, RankExpr);
}
if (auto *BitStride = CTy->getBitStrideConst()) {
@@ -1917,11 +1881,7 @@ DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
if (auto *VarDIE = getDIE(Var))
addDIEEntry(MemberDie, dwarf::DW_AT_bit_size, *VarDIE);
} else if (auto *Exp = dyn_cast<DIExpression>(DT->getRawSizeInBits())) {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(Exp);
- addBlock(MemberDie, dwarf::DW_AT_bit_size, DwarfExpr.finalize());
+ addBlock(MemberDie, dwarf::DW_AT_bit_size, Exp);
} else {
Size = DT->getSizeInBits();
FieldSize = DD->getBaseTypeSize(DT);
@@ -1945,11 +1905,7 @@ DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
} else if (auto *Expr =
dyn_cast_or_null<DIExpression>(DT->getRawOffsetInBits())) {
if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 6) {
- DIELoc *Loc = new (DIEValueAllocator) DIELoc;
- DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
- DwarfExpr.setMemoryLocationKind();
- DwarfExpr.addExpression(Expr);
- addBlock(MemberDie, dwarf::DW_AT_data_bit_offset, DwarfExpr.finalize());
+ addBlock(MemberDie, dwarf::DW_AT_data_bit_offset, Expr);
}
} else {
uint32_t AlignInBytes = DT->getAlignInBytes();
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
index 9c0b68b315b50..740e5a80ca619 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
@@ -216,6 +216,9 @@ class DwarfUnit : public DIEUnit {
void addBlock(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
DIEBlock *Block);
+ /// Add an expression as block data.
+ void addBlock(DIE &Die, dwarf::Attribute Attribute, const DIExpression *Expr);
+
/// Add location information to specified debug information entry.
void addSourceLine(DIE &Die, unsigned Line, unsigned Column,
const DIFile *File);
More information about the llvm-commits
mailing list