[llvm] [DWARFLinker] Don't duplicate classes with in-class static decls (PR #196442)
Jonas Devlieghere via llvm-commits
llvm-commits at lists.llvm.org
Thu May 7 15:52:52 PDT 2026
https://github.com/JDevlieghere created https://github.com/llvm/llvm-project/pull/196442
An in-class static declaration was forced to PlainDwarf placement and cascaded that up to its enclosing class. If the class was already in the type table via the out-of-line definition's specification, it ended up with Both placement and cloneDIE emitted two copies. Keep in-class static declarations in the type table so they stay with their enclosing type.
>From 3969787c164a1eb84bb7e4659331b6db83824659 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Thu, 7 May 2026 15:51:59 -0700
Subject: [PATCH] [DWARFLinker] Don't duplicate classes with in-class static
decls
An in-class static declaration was forced to PlainDwarf placement and
cascaded that up to its enclosing class. If the class was already in
the type table via the out-of-line definition's specification, it
ended up with Both placement and cloneDIE emitted two copies. Keep
in-class static declarations in the type table so they stay with
their enclosing type.
---
.../Parallel/DependencyTracker.cpp | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/llvm/lib/DWARFLinker/Parallel/DependencyTracker.cpp b/llvm/lib/DWARFLinker/Parallel/DependencyTracker.cpp
index 68b01dd7ddad3..46f2a20f474c5 100644
--- a/llvm/lib/DWARFLinker/Parallel/DependencyTracker.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/DependencyTracker.cpp
@@ -397,6 +397,32 @@ getFinalPlacementForEntry(const UnitEntryPairTy &Entry,
return CompileUnit::PlainDwarf;
if (Entry.DieEntry->getTag() == dwarf::DW_TAG_variable) {
+ // In-class static member declarations (e.g. "static constexpr int x = 1;")
+ // are DW_TAG_variable children of a DW_TAG_class_type /
+ // DW_TAG_structure_type / DW_TAG_union_type with DW_AT_declaration set.
+ // They are part of the class type and belong in the TypeTable together with
+ // the class. Forcing them into PlainDwarf would also drag the parent class
+ // into PlainDwarf (via markParentsAsKeepingChildren), producing a duplicate
+ // empty class declaration DIE alongside the full class definition emitted
+ // in another CU.
+ bool IsDeclaration = dwarf::toUnsigned(
+ Entry.CU->find(Entry.DieEntry, dwarf::DW_AT_declaration), 0);
+ bool ParentIsType = false;
+ if (IsDeclaration) {
+ if (std::optional<uint32_t> ParentIdx = Entry.DieEntry->getParentIdx()) {
+ dwarf::Tag ParentTag =
+ Entry.CU->getDebugInfoEntry(*ParentIdx)->getTag();
+ ParentIsType = ParentTag == dwarf::DW_TAG_class_type ||
+ ParentTag == dwarf::DW_TAG_structure_type ||
+ ParentTag == dwarf::DW_TAG_union_type;
+ }
+ }
+ if (IsDeclaration && ParentIsType) {
+ // Pure declarations have no runtime address; they belong with the class
+ // type. Always place in TypeTable regardless of how they were reached.
+ return CompileUnit::TypeTable;
+ }
+
// Do not put variable into the "TypeTable" and "PlainDwarf" at the same
// time.
if (EntryInfo.getPlacement() == CompileUnit::PlainDwarf ||
More information about the llvm-commits
mailing list