Skip to content

Conversation

tromey
Copy link
Contributor

@tromey tromey commented Sep 30, 2025

It's reasonably normal for DWARF not to explicitly state the size of a member of a composite type -- in this case the size is taken from the type. LLVM does allow a member to have a NULL size, but in this case it emits a size of "0". This patch cleans this up a bit, changing the DWARF emission code to simply not emit a size in this situation.

@llvmbot
Copy link
Member

llvmbot commented Sep 30, 2025

@llvm/pr-subscribers-debuginfo

Author: Tom Tromey (tromey)

Changes

It's reasonably normal for DWARF not to explicitly state the size of a member of a composite type -- in this case the size is taken from the type. LLVM does allow a member to have a NULL size, but in this case it emits a size of "0". This patch cleans this up a bit, changing the DWARF emission code to simply not emit a size in this situation.


Full diff: https://github.com/llvm/llvm-project/pull/161423.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp (+4-1)
  • (modified) llvm/test/DebugInfo/X86/dynamic-bitfield.ll (+11-2)
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index 62fb5eb011cf2..e9d1b1133b8c4 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -1889,7 +1889,10 @@ DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
     bool IsBitfield = DT->isBitField();
 
     // Handle the size.
-    if (auto *Var = dyn_cast_or_null<DIVariable>(DT->getRawSizeInBits())) {
+    if (DT->getRawSizeInBits() == nullptr) {
+      // No size, just ignore.
+    } else if (auto *Var =
+                   dyn_cast_or_null<DIVariable>(DT->getRawSizeInBits())) {
       if (auto *VarDIE = getDIE(Var))
         addDIEEntry(MemberDie, dwarf::DW_AT_bit_size, *VarDIE);
     } else if (auto *Exp =
diff --git a/llvm/test/DebugInfo/X86/dynamic-bitfield.ll b/llvm/test/DebugInfo/X86/dynamic-bitfield.ll
index c9148ca4582f6..f8935977c64e7 100644
--- a/llvm/test/DebugInfo/X86/dynamic-bitfield.ll
+++ b/llvm/test/DebugInfo/X86/dynamic-bitfield.ll
@@ -27,7 +27,7 @@ source_filename = "bitfield.c"
 !6 = !{}
 !7 = !{!0, !2}
 !8 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "PackedBits", file: !5, line: 3, size: 40, elements: !9)
-!9 = !{!10, !12, !16}
+!9 = !{!10, !12, !16, !21}
 !10 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !8, file: !5, line: 5, baseType: !11, size: 8)
 ; CHECK: DW_TAG_member
 ; CHECK-NEXT: DW_AT_name{{.*}}"a"
@@ -60,5 +60,14 @@ source_filename = "bitfield.c"
 ; CHECK:      DW_AT_bit_size             [DW_FORM_exprloc]	(DW_OP_lit27)
 ; CHECK-NEXT: DW_AT_data_bit_offset      [DW_FORM_exprloc]	(DW_OP_lit13)
 ; CHECK-NOT:  DW_AT_data_member_location
-; CHECK: DW_TAG
 !20 = !{!"clang version 3.9.0 (trunk 267633)"}
+!21 = !DIDerivedType(tag: DW_TAG_member, name: "d", scope: !8, file: !5, line: 7, baseType: !13, offset: !DIExpression(DW_OP_constu, 15), flags: DIFlagBitField)
+; CHECK: DW_TAG_member
+; CHECK-NEXT: DW_AT_name{{.*}}"d"
+; CHECK-NOT:  DW_TAG
+; CHECK-NOT:  DW_AT_bit_offset
+; CHECK-NOT:  DW_AT_byte_size
+; CHECK-NOT:  DW_AT_bit_size
+; CHECK:      DW_AT_data_bit_offset      [DW_FORM_exprloc]	(DW_OP_lit15)
+; CHECK-NOT:  DW_AT_data_member_location
+; CHECK: DW_TAG

Copy link
Member

@Michael137 Michael137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me

It's reasonably normal for DWARF not to explicitly state the size of a
member of a composite type -- in this case the size is taken from the
type.  LLVM does allow a member to have a NULL size, but in this case
it emits a size of "0".  This patch cleans this up a bit, changing the
DWARF emission code to simply not emit a size in this situation.
@tromey tromey force-pushed the topic/omit-size-from-dwarf branch from fdeeb68 to a4b71a6 Compare October 2, 2025 15:52
@tromey
Copy link
Contributor Author

tromey commented Oct 2, 2025

Seems reasonable to me

I don't have permissions to land this, could you please do it? Thank you.

@Michael137 Michael137 enabled auto-merge (squash) October 2, 2025 16:09
@Michael137 Michael137 merged commit c2ef022 into llvm:main Oct 2, 2025
9 checks passed
@tromey tromey deleted the topic/omit-size-from-dwarf branch October 2, 2025 18:21
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants