[llvm] [DebugMetadata][DwarfDebug][CodeView] Support function-local static variables in lexical block scopes (6/7) (PR #187927)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 22 05:37:48 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-aarch64
Author: Vladislav Dzhidzhoev (dzhidzhoev)
<details>
<summary>Changes</summary>
RFC https://discourse.llvm.org/t/rfc-dwarfdebug-fix-and-improve-handling-imported-entities-types-and-static-local-in-subprogram-and-lexical-block-scopes/68544
Similar to imported declarations and types, the patch tracks function-local static variables (globals in terms of LLVM IR) in DISubprogram's 'retainedNodes'. DwarfDebug is adjusted in accordance with the aforementioned metadata change and provided a support of static locals scoped by a lexical block. CodeViewDebug is modified to collect global variables from DISubprogram's 'retainedNodes' too.
The patch assumes that DICompileUnit's 'globals' no longer tracks static locals and DwarfDebug would assert if any locally-scoped variables get placed there.
This change has already been reviewed in https://reviews.llvm.org/D144008, but I’m opening a new PR because it has been a while since it was approved.
Authored-by: Kristina Bessonova <kbessonova@<!-- -->accesssoftek.com>
---
Patch is 114.29 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/187927.diff
53 Files Affected:
- (modified) llvm/include/llvm/IR/DIBuilder.h (+3-3)
- (modified) llvm/include/llvm/IR/DebugInfo.h (+1)
- (modified) llvm/include/llvm/IR/DebugInfoMetadata.h (+35-11)
- (modified) llvm/lib/Bitcode/Reader/MetadataLoader.cpp (+17-5)
- (modified) llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp (+55-47)
- (modified) llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h (+4)
- (modified) llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp (+26-16)
- (modified) llvm/lib/IR/DIBuilder.cpp (+7-4)
- (modified) llvm/lib/IR/DebugInfo.cpp (+13-8)
- (modified) llvm/lib/IR/DebugInfoMetadata.cpp (+3-21)
- (modified) llvm/lib/IR/Verifier.cpp (+9-5)
- (modified) llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp (+1-1)
- (modified) llvm/lib/Target/BPF/BTFDebug.cpp (+2-2)
- (modified) llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp (+1)
- (modified) llvm/lib/Transforms/Utils/CloneFunction.cpp (+15)
- (modified) llvm/lib/Transforms/Utils/Debugify.cpp (+2-2)
- (modified) llvm/test/Bitcode/DIModule-fortran-external-module.ll (+2-3)
- (added) llvm/test/Bitcode/upgrade-cu-static-locals.ll (+108)
- (added) llvm/test/Bitcode/upgrade-cu-static-locals.ll.bc ()
- (modified) llvm/test/CodeGen/AArch64/arm64-2011-03-17-AsmPrinterCrash.ll (+2-2)
- (modified) llvm/test/CodeGen/BPF/BTF/static-var-inited-sec.ll (+3-2)
- (modified) llvm/test/CodeGen/BPF/BTF/static-var-inited.ll (+3-2)
- (modified) llvm/test/CodeGen/BPF/BTF/static-var-readonly-sec.ll (+3-2)
- (modified) llvm/test/CodeGen/BPF/BTF/static-var-readonly.ll (+3-2)
- (modified) llvm/test/CodeGen/BPF/BTF/static-var-sec.ll (+3-2)
- (modified) llvm/test/CodeGen/BPF/BTF/static-var.ll (+3-2)
- (modified) llvm/test/CodeGen/BPF/dwarfdump.ll (+2-3)
- (modified) llvm/test/CodeGen/PowerPC/pr24546.ll (+2-3)
- (modified) llvm/test/CodeGen/X86/dbg-distringtype-uint.ll (+2-3)
- (modified) llvm/test/DebugInfo/COFF/fortran-basic.ll (+2-2)
- (modified) llvm/test/DebugInfo/COFF/fortran-contained-proc.ll (+2-2)
- (modified) llvm/test/DebugInfo/COFF/global_visibility.ll (+5-3)
- (modified) llvm/test/DebugInfo/COFF/type-quals.ll (+2-2)
- (modified) llvm/test/DebugInfo/Generic/2009-11-05-DeadGlobalVariable.ll (+2-2)
- (modified) llvm/test/DebugInfo/Generic/inlined-static-var.ll (+5-4)
- (added) llvm/test/DebugInfo/Generic/lexical-block-static-var.ll (+145)
- (added) llvm/test/DebugInfo/Generic/verifier-invalid-dicompileunit.ll (+62)
- (modified) llvm/test/DebugInfo/Generic/verifier-invalid-disubprogram.ll (+1-1)
- (modified) llvm/test/DebugInfo/Symbolize/XCOFF/xcoff-symbolize-data.ll (+3-2)
- (modified) llvm/test/DebugInfo/X86/DW_AT_specification.ll (+2-2)
- (modified) llvm/test/DebugInfo/X86/dbg-value-transfer-order.ll (+2-2)
- (modified) llvm/test/DebugInfo/X86/dimodule-external-fortran.ll (+2-3)
- (modified) llvm/test/DebugInfo/X86/distringtype.ll (+2-2)
- (modified) llvm/test/DebugInfo/X86/global-sra-struct-fit-segment.ll (+2-2)
- (modified) llvm/test/DebugInfo/X86/global-sra-struct-part-overlap-segment.ll (+3-2)
- (modified) llvm/test/DebugInfo/X86/gnu-public-names.ll (+3-2)
- (modified) llvm/test/DebugInfo/X86/namelist1.ll (+2-2)
- (modified) llvm/test/DebugInfo/X86/sdag-dbgvalue-phi-use-4.ll (+53-54)
- (modified) llvm/test/DebugInfo/duplicate_dbgvalue.ll (+3-2)
- (modified) llvm/test/Instrumentation/InstrProfiling/debug-info-correlate.ll (+2-1)
- (modified) llvm/test/Transforms/Util/clone-dicompileunit.ll (+8-3)
- (modified) llvm/test/tools/llvm-debuginfo-analyzer/DWARF/Inputs/ThreadLocalStorage.ll (+2-2)
- (modified) llvm/unittests/Transforms/Utils/CloningTest.cpp (+24-16)
``````````diff
diff --git a/llvm/include/llvm/IR/DIBuilder.h b/llvm/include/llvm/IR/DIBuilder.h
index 9753c363166d9..04c360b57322e 100644
--- a/llvm/include/llvm/IR/DIBuilder.h
+++ b/llvm/include/llvm/IR/DIBuilder.h
@@ -53,7 +53,7 @@ namespace llvm {
/// Track the RetainTypes, since they can be updated later on.
SmallVector<TrackingMDNodeRef, 4> AllRetainTypes;
SmallVector<DISubprogram *, 4> AllSubprograms;
- SmallVector<Metadata *, 4> AllGVs;
+ SmallVector<Metadata *, 4> Globals;
SmallVector<TrackingMDNodeRef, 4> ImportedModules;
/// Map Macro parent (which can be DIMacroFile or nullptr) to a list of
/// Metadata all of type DIMacroNode.
@@ -64,8 +64,8 @@ namespace llvm {
SmallVector<TrackingMDNodeRef, 4> UnresolvedNodes;
bool AllowUnresolvedNodes;
- /// Each subprogram's preserved local variables, labels, imported entities,
- /// and types.
+ /// Each subprogram's preserved local and static local variables, labels,
+ /// imported entities, and types.
///
/// Do not use a std::vector. Some versions of libc++ apparently copy
/// instead of move on grow operations, and TrackingMDRef is expensive to
diff --git a/llvm/include/llvm/IR/DebugInfo.h b/llvm/include/llvm/IR/DebugInfo.h
index cb8f6fa1b132b..dc3ff31449a2f 100644
--- a/llvm/include/llvm/IR/DebugInfo.h
+++ b/llvm/include/llvm/IR/DebugInfo.h
@@ -124,6 +124,7 @@ class DebugInfoFinder {
private:
void processCompileUnit(DICompileUnit *CU);
+ void processGlobalVariableExpression(DIGlobalVariableExpression *GVE);
void processScope(DIScope *Scope);
void processType(DIType *DT);
void processImportedEntity(const DIImportedEntity *Import);
diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h b/llvm/include/llvm/IR/DebugInfoMetadata.h
index 3ce899db66217..3ffff4314916c 100644
--- a/llvm/include/llvm/IR/DebugInfoMetadata.h
+++ b/llvm/include/llvm/IR/DebugInfoMetadata.h
@@ -2291,7 +2291,7 @@ class DISubprogram : public DILocalScope {
unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
DISPFlags SPFlags, DICompileUnit *Unit,
DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
- DINodeArray RetainedNodes, DITypeArray ThrownTypes,
+ MDNodeArray RetainedNodes, DITypeArray ThrownTypes,
DINodeArray Annotations, StringRef TargetFuncName,
bool UsesKeyInstructions, StorageType Storage,
bool ShouldCreate = true) {
@@ -2332,7 +2332,7 @@ class DISubprogram : public DILocalScope {
DIType *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
DIFlags Flags, DISPFlags SPFlags, DICompileUnit *Unit,
DITemplateParameterArray TemplateParams = nullptr,
- DISubprogram *Declaration = nullptr, DINodeArray RetainedNodes = nullptr,
+ DISubprogram *Declaration = nullptr, MDNodeArray RetainedNodes = nullptr,
DITypeArray ThrownTypes = nullptr, DINodeArray Annotations = nullptr,
StringRef TargetFuncName = "", bool UsesKeyInstructions = false),
(Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
@@ -2461,7 +2461,7 @@ class DISubprogram : public DILocalScope {
return cast_or_null<DISubprogram>(getRawDeclaration());
}
void replaceDeclaration(DISubprogram *Decl) { replaceOperandWith(6, Decl); }
- DINodeArray getRetainedNodes() const {
+ MDNodeArray getRetainedNodes() const {
return cast_or_null<MDTuple>(getRawRetainedNodes());
}
DITypeArray getThrownTypes() const {
@@ -2500,19 +2500,17 @@ class DISubprogram : public DILocalScope {
void replaceRawLinkageName(MDString *LinkageName) {
replaceOperandWith(3, LinkageName);
}
- void replaceRetainedNodes(DINodeArray N) {
- replaceOperandWith(7, N.get());
- }
+ void replaceRetainedNodes(MDNodeArray N) { replaceOperandWith(7, N.get()); }
/// For the given retained node of DISubprogram, applies one of the
/// given functions depending on the type of the node.
template <typename T, typename MetadataT, typename FuncLVT,
typename FuncLabelT, typename FuncImportedEntityT,
- typename FuncTypeT, typename FuncUnknownT>
+ typename FuncTypeT, typename FuncGVET, typename FuncUnknownT>
static T visitRetainedNode(MetadataT *N, FuncLVT &&FuncLV,
FuncLabelT &&FuncLabel,
FuncImportedEntityT &&FuncIE, FuncTypeT &&FuncType,
- FuncUnknownT &&FuncUnknown) {
+ FuncGVET &&FuncGVE, FuncUnknownT &&FuncUnknown) {
static_assert(std::is_base_of_v<Metadata, MetadataT>,
"N must point to Metadata or const Metadata");
@@ -2524,6 +2522,8 @@ class DISubprogram : public DILocalScope {
return FuncIE(IE);
if (auto *Ty = dyn_cast<DIType>(N))
return FuncType(Ty);
+ if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(N))
+ return FuncGVE(GVE);
return FuncUnknown(N);
}
@@ -2537,12 +2537,13 @@ class DISubprogram : public DILocalScope {
/// For each retained node, applies one of the given functions depending
/// on the type of a node.
template <typename FuncLVT, typename FuncLabelT, typename FuncImportedEntityT,
- typename FuncTypeT>
+ typename FuncTypeT, typename FuncGVET>
void forEachRetainedNode(FuncLVT &&FuncLV, FuncLabelT &&FuncLabel,
- FuncImportedEntityT &&FuncIE, FuncTypeT &&FuncType) {
+ FuncImportedEntityT &&FuncIE, FuncTypeT &&FuncType,
+ FuncGVET &&FuncGVE) {
for (MDNode *N : getRetainedNodes())
visitRetainedNode<void>(
- N, FuncLV, FuncLabel, FuncIE, FuncType,
+ N, FuncLV, FuncLabel, FuncIE, FuncType, FuncGVE,
[](auto *N) { llvm_unreachable("Unexpected retained node!"); });
}
@@ -2574,6 +2575,29 @@ class DISubprogram : public DILocalScope {
/// of a current scope for expression evaluation.
void cleanupRetainedNodes();
+ template <typename T> void cleanupRetainedNodes(T &&RemovePred) {
+ // As this is expected to be called during module loading, before
+ // stripping old or incorrect debug info, perform minimal sanity check.
+ if (!isa_and_present<MDTuple>(getRawRetainedNodes()))
+ return;
+
+ MDTuple *RetainedNodes = cast<MDTuple>(getRawRetainedNodes());
+ SmallVector<Metadata *> MDs;
+ MDs.reserve(RetainedNodes->getNumOperands());
+ for (const MDOperand &Node : RetainedNodes->operands()) {
+ // Ignore malformed retainedNodes.
+ if (Node && !isa<MDNode>(Node))
+ return;
+
+ auto *N = cast_or_null<MDNode>(Node);
+ if (!RemovePred(N))
+ MDs.push_back(N);
+ }
+
+ if (MDs.size() != RetainedNodes->getNumOperands())
+ replaceRetainedNodes(MDNode::get(getContext(), MDs));
+ }
+
/// Calls SP->cleanupRetainedNodes() for a range of DISubprograms.
template <typename RangeT>
static void cleanupRetainedNodes(const RangeT &NewDistinctSPs) {
diff --git a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
index 4bc55232a6df6..d5c5965aab14f 100644
--- a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
+++ b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
@@ -545,10 +545,8 @@ class MetadataLoader::MetadataLoaderImpl {
llvm::dyn_cast_or_null<DISubprogram>(S);
}
- /// Move local imports from DICompileUnit's 'imports' field to
- /// DISubprogram's retainedNodes.
- /// Move function-local enums from DICompileUnit's enums
- /// to DISubprogram's retainedNodes.
+ /// Move function-local entities from DICompileUnit's 'imports',
+ /// 'enums', and 'globals' fields to DISubprogram's retainedNodes.
void upgradeCULocals() {
NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu");
if (!CUNodes)
@@ -579,6 +577,14 @@ class MetadataLoader::MetadataLoaderImpl {
continue;
SetVector<Metadata *> MetadataToRemove;
+ // Collect globals to be moved.
+ if (CU->getRawGlobalVariables())
+ for (Metadata *Op : CU->getGlobalVariables()->operands()) {
+ auto *GVE = cast<DIGlobalVariableExpression>(Op);
+ if (isa_and_nonnull<DILocalScope>(GVE->getVariable()->getScope()))
+ MetadataToRemove.insert(GVE);
+ }
+
// Collect imported entities to be moved.
if (CU->getRawImportedEntities())
for (Metadata *Op : CU->getImportedEntities()->operands()) {
@@ -586,6 +592,7 @@ class MetadataLoader::MetadataLoaderImpl {
if (isa_and_nonnull<DILocalScope>(IE->getScope()))
MetadataToRemove.insert(IE);
}
+
// Collect enums to be moved.
if (CU->getRawEnumTypes())
for (Metadata *Op : CU->getEnumTypes()->operands()) {
@@ -597,6 +604,11 @@ class MetadataLoader::MetadataLoaderImpl {
if (MetadataToRemove.empty())
continue;
+ // Remove globals with local scope from CU.
+ if (CU->getRawGlobalVariables())
+ CU->replaceGlobalVariables(
+ FilterTuple(CU->getGlobalVariables().get(), MetadataToRemove));
+
// Remove entities with local scope from CU.
if (CU->getRawImportedEntities())
CU->replaceImportedEntities(
@@ -611,7 +623,7 @@ class MetadataLoader::MetadataLoaderImpl {
SmallDenseMap<DISubprogram *, SmallVector<Metadata *>> SPToEntities;
for (auto *I : MetadataToRemove) {
DILocalScope *Scope =
- DISubprogram::getRetainedNodeScope(cast<DINode>(I));
+ DISubprogram::getRetainedNodeScope(cast<MDNode>(I));
if (auto *SP = findEnclosingSubprogram(Scope))
SPToEntities[SP].push_back(I);
}
diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
index 4b6b76c48dcbf..b674a5b7be3dd 100644
--- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
@@ -1466,6 +1466,10 @@ void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
// Grab the variable info that was squirreled away in the MMI side-table.
collectVariableInfoFromMFTable(Processed);
+ for (const MDNode *N : SP->getRetainedNodes())
+ if (const auto *GVE = dyn_cast<DIGlobalVariableExpression>(N))
+ collectGlobalOrStaticLocalVariableInfo(GVE);
+
for (const auto &I : DbgValues) {
InlinedEntity IV = I.first;
if (Processed.count(IV))
@@ -3242,9 +3246,57 @@ void CodeViewDebug::emitDebugInfoForUDTs(
}
}
+void CodeViewDebug::collectGlobalOrStaticLocalVariableInfo(
+ const DIGlobalVariableExpression *GVE) {
+ const DIGlobalVariable *DIGV = GVE->getVariable();
+ const DIExpression *DIE = GVE->getExpression();
+ // Don't emit string literals in CodeView, as the only useful parts are
+ // generally the filename and line number, which isn't possible to output
+ // in CodeView. String literals should be the only unnamed GlobalVariable
+ // with debug info.
+ if (DIGV->getName().empty())
+ return;
+
+ if ((DIE->getNumElements() == 2) &&
+ (DIE->getElement(0) == dwarf::DW_OP_plus_uconst))
+ // Record the constant offset for the variable.
+ //
+ // A Fortran common block uses this idiom to encode the offset
+ // of a variable from the common block's starting address.
+ CVGlobalVariableOffsets.insert(std::make_pair(DIGV, DIE->getElement(1)));
+
+ // Emit constant global variables in a global symbol section.
+ if (GlobalMap.count(GVE) == 0 && DIE->isConstant()) {
+ CVGlobalVariable CVGV = {DIGV, DIE};
+ GlobalVariables.emplace_back(std::move(CVGV));
+ }
+
+ const auto *GV = GlobalMap.lookup(GVE);
+ if (!GV || GV->isDeclarationForLinker())
+ return;
+
+ DIScope *Scope = DIGV->getScope();
+ SmallVector<CVGlobalVariable, 1> *VariableList;
+ if (Scope && isa<DILocalScope>(Scope)) {
+ // Locate a global variable list for this scope, creating one if
+ // necessary.
+ auto Insertion =
+ ScopeGlobals.insert({Scope, std::unique_ptr<GlobalVariableList>()});
+ if (Insertion.second)
+ Insertion.first->second = std::make_unique<GlobalVariableList>();
+ VariableList = Insertion.first->second.get();
+ } else if (GV->hasComdat()) {
+ // Emit this global variable into a COMDAT section.
+ VariableList = &ComdatVariables;
+ } else {
+ // Emit this global variable in a single global symbol section.
+ VariableList = &GlobalVariables;
+ }
+ CVGlobalVariable CVGV = {DIGV, GV};
+ VariableList->emplace_back(std::move(CVGV));
+}
+
void CodeViewDebug::collectGlobalVariableInfo() {
- DenseMap<const DIGlobalVariableExpression *, const GlobalVariable *>
- GlobalMap;
for (const GlobalVariable &GV : MMI->getModule()->globals()) {
SmallVector<DIGlobalVariableExpression *, 1> GVEs;
GV.getDebugInfo(GVEs);
@@ -3256,51 +3308,7 @@ void CodeViewDebug::collectGlobalVariableInfo() {
for (const MDNode *Node : CUs->operands()) {
const auto *CU = cast<DICompileUnit>(Node);
for (const auto *GVE : CU->getGlobalVariables()) {
- const DIGlobalVariable *DIGV = GVE->getVariable();
- const DIExpression *DIE = GVE->getExpression();
- // Don't emit string literals in CodeView, as the only useful parts are
- // generally the filename and line number, which isn't possible to output
- // in CodeView. String literals should be the only unnamed GlobalVariable
- // with debug info.
- if (DIGV->getName().empty()) continue;
-
- if ((DIE->getNumElements() == 2) &&
- (DIE->getElement(0) == dwarf::DW_OP_plus_uconst))
- // Record the constant offset for the variable.
- //
- // A Fortran common block uses this idiom to encode the offset
- // of a variable from the common block's starting address.
- CVGlobalVariableOffsets.insert(
- std::make_pair(DIGV, DIE->getElement(1)));
-
- // Emit constant global variables in a global symbol section.
- if (GlobalMap.count(GVE) == 0 && DIE->isConstant()) {
- CVGlobalVariable CVGV = {DIGV, DIE};
- GlobalVariables.emplace_back(std::move(CVGV));
- }
-
- const auto *GV = GlobalMap.lookup(GVE);
- if (!GV || GV->isDeclarationForLinker())
- continue;
-
- DIScope *Scope = DIGV->getScope();
- SmallVector<CVGlobalVariable, 1> *VariableList;
- if (Scope && isa<DILocalScope>(Scope)) {
- // Locate a global variable list for this scope, creating one if
- // necessary.
- auto Insertion = ScopeGlobals.insert(
- {Scope, std::unique_ptr<GlobalVariableList>()});
- if (Insertion.second)
- Insertion.first->second = std::make_unique<GlobalVariableList>();
- VariableList = Insertion.first->second.get();
- } else if (GV->hasComdat())
- // Emit this global variable into a COMDAT section.
- VariableList = &ComdatVariables;
- else
- // Emit this global variable in a single global symbol section.
- VariableList = &GlobalVariables;
- CVGlobalVariable CVGV = {DIGV, GV};
- VariableList->emplace_back(std::move(CVGV));
+ collectGlobalOrStaticLocalVariableInfo(GVE);
}
}
}
diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h
index 7fd2cec8c74f2..ba3cd077a4884 100644
--- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h
+++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h
@@ -237,6 +237,8 @@ class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
// belong in. A null local scope represents the global scope.
typedef SmallVector<CVGlobalVariable, 1> GlobalVariableList;
DenseMap<const DIScope*, std::unique_ptr<GlobalVariableList> > ScopeGlobals;
+ DenseMap<const DIGlobalVariableExpression *, const GlobalVariable *>
+ GlobalMap;
// Array of global variables which need to be emitted into a COMDAT section.
SmallVector<CVGlobalVariable, 1> ComdatVariables;
@@ -389,6 +391,8 @@ class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
void collectGlobalVariableInfo();
+ void
+ collectGlobalOrStaticLocalVariableInfo(const DIGlobalVariableExpression *GVE);
void collectVariableInfo(const DISubprogram *SP);
void collectVariableInfoFromMFTable(DenseSet<InlinedEntity> &Processed);
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 72022fcddf316..44fed8d966df3 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -1518,6 +1518,8 @@ void DwarfDebug::endModule() {
DenseSet<DIGlobalVariable *> Processed;
for (auto *GVE : CUNode->getGlobalVariables()) {
DIGlobalVariable *GV = GVE->getVariable();
+ assert(!isa_and_nonnull<DILocalScope>(GV->getScope()) &&
+ "Unexpected function-local entity in 'globals' CU field.");
if (Processed.insert(GV).second)
CU->getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV]));
}
@@ -1530,14 +1532,20 @@ void DwarfDebug::endModule() {
}
// Emit function-local entities.
- for (const auto *D : CU->getDeferredLocalDecls()) {
- if (auto *IE = dyn_cast<DIImportedEntity>(D))
- CU->getOrCreateImportedEntityDIE(IE);
- else if (auto *Ty = dyn_cast<DIType>(D))
- CU->getOrCreateTypeDIE(Ty);
- else
- llvm_unreachable("Unexpected local retained node!");
- }
+ const auto Unexpected = [](const Metadata *N) {
+ llvm_unreachable("Unexpected local retained node!");
+ };
+ for (const auto *D : CU->getDeferredLocalDecls())
+ DISubprogram::visitRetainedNode<void>(
+ D, Unexpected, Unexpected,
+ [CU](const auto *IE) { CU->getOrCreateImportedEntityDIE(IE); },
+ [CU](const auto *Ty) { CU->getOrCreateTypeDIE(Ty); },
+ [&](const auto *GVE) {
+ DIGlobalVariable *GV = GVE->getVariable();
+ if (Processed.insert(GV).second)
+ CU->getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV]));
+ },
+ Unexpected);
// Emit base types.
CU->createBaseTypeDIEs();
@@ -2084,16 +2092,17 @@ void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU,
}
// Collect info for retained nodes.
- for (const DINode *DN : SP->getRetainedNodes()) {
- const auto *LS = getRetainedNodeScope(DN);
- if (isa<DILocalVariable>(DN) || isa<DILabel>(DN)) {
+ for (const MDNode *N : SP->getRetainedNodes()) {
+ const auto *LS = getRetainedNodeScope(N);
+ if (isa<DILocalVariable>(N) || isa<DILabel>(N)) {
+ auto *DN = cast<DINode>(N);
if (!Processed.insert(InlinedEntity(DN, nullptr)).second)
continue;
LexicalScope *LexS = LScopes.findLexicalScope(LS);
if (LexS)
createConcreteEntity(TheCU, *LexS, DN, nullptr);
} else {
- LocalDeclsPerLS[LS].insert(DN);
+ LocalDeclsPerLS[LS].insert(N);
}
}
}
@@ -2893,12 +2902,13 @@ void DwarfDebug::endFunctionImpl(const MachineFunction *MF) {
#endif
for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
const auto *SP = cast<DISubprogram>(AScope->getScopeNode());
- for (const DINode *DN : SP->getRetainedNodes()) {
- const auto *LS = getRetainedNodeScope(DN);
+ for (const MDNode *N : SP->getRetainedNodes()) {
+ const auto *LS = getRetainedNodeScope(N);
// Ensure LexicalScope is created for the scope of this node.
auto *LexS = LScopes.getOrCreateAbstractScope(LS);
assert(LexS && "Expected the LexicalScope to be created.");
- if (isa<DILocalVariable>(DN) || isa<DILabel>(DN)) {
+ if (isa<DILocalVariable>(N) || isa<DILabel>(N)) {
+ auto *DN = cast<DINode>(N);
// Collect info for variables/labels that were optimized out.
if (!Processed.insert(InlinedEntity(DN, nullptr)).second ||
TheCU.getExistingAbstractEntity(DN))
@@ -2906,7 +2916,7 @@ void DwarfDebug::endFunctionImpl(const MachineFunction *MF) {
TheCU.createAbstractEntity(DN, LexS);
} else {
// Remember the node if this is a local declarations.
- LocalDeclsPerLS[LS].insert(DN);
+ LocalDeclsPerLS[LS].insert(N);
}
assert(
LScopes.getAbstractScopesList().size() == NumAbstractSubprograms &&
diff --git a/llvm/lib/IR/DIBuilder.cpp b/llvm/lib/IR/DI...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/187927
More information about the llvm-commits
mailing list