Skip to content

Commit b1bd74e

Browse files
authored
[LLVM][DebugInfo] Allow ExtraData field to be a node reference (#165023)
This change enhances debug info metadata handling to support node references in the `extraData` field for `DW_TAG_member`, `DW_TAG_variable`, and `DW_TAG_inheritance` tags. The change enables LLVM to handle both direct constant values (e.g., extraData: i8 1) and node references (e.g., extraData: !18 where !18 = !{ i8 1 }).
1 parent 1af0424 commit b1bd74e

File tree

2 files changed

+118
-4
lines changed

2 files changed

+118
-4
lines changed

llvm/lib/IR/DebugInfoMetadata.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -962,16 +962,29 @@ DIType *DIDerivedType::getClassType() const {
962962
assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
963963
return cast_or_null<DIType>(getExtraData());
964964
}
965+
966+
// Helper function to extract ConstantAsMetadata from ExtraData,
967+
// handling extra data MDTuple unwrapping if needed.
968+
static ConstantAsMetadata *extractConstantMetadata(Metadata *ExtraData) {
969+
Metadata *ED = ExtraData;
970+
if (auto *Tuple = dyn_cast_or_null<MDTuple>(ED)) {
971+
if (Tuple->getNumOperands() != 1)
972+
return nullptr;
973+
ED = Tuple->getOperand(0);
974+
}
975+
return cast_or_null<ConstantAsMetadata>(ED);
976+
}
977+
965978
uint32_t DIDerivedType::getVBPtrOffset() const {
966979
assert(getTag() == dwarf::DW_TAG_inheritance);
967-
if (auto *CM = cast_or_null<ConstantAsMetadata>(getExtraData()))
980+
if (auto *CM = extractConstantMetadata(getExtraData()))
968981
if (auto *CI = dyn_cast_or_null<ConstantInt>(CM->getValue()))
969982
return static_cast<uint32_t>(CI->getZExtValue());
970983
return 0;
971984
}
972985
Constant *DIDerivedType::getStorageOffsetInBits() const {
973986
assert(getTag() == dwarf::DW_TAG_member && isBitField());
974-
if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
987+
if (auto *C = extractConstantMetadata(getExtraData()))
975988
return C->getValue();
976989
return nullptr;
977990
}
@@ -980,13 +993,13 @@ Constant *DIDerivedType::getConstant() const {
980993
assert((getTag() == dwarf::DW_TAG_member ||
981994
getTag() == dwarf::DW_TAG_variable) &&
982995
isStaticMember());
983-
if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
996+
if (auto *C = extractConstantMetadata(getExtraData()))
984997
return C->getValue();
985998
return nullptr;
986999
}
9871000
Constant *DIDerivedType::getDiscriminantValue() const {
9881001
assert(getTag() == dwarf::DW_TAG_member && !isStaticMember());
989-
if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
1002+
if (auto *C = extractConstantMetadata(getExtraData()))
9901003
return C->getValue();
9911004
return nullptr;
9921005
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
;; Test verifies that node reference in the extraData field are handled correctly
2+
;; when used with tags like DW_TAG_member, DW_TAG_inheritance etc.
3+
4+
; REQUIRES: object-emission
5+
; RUN: %llc_dwarf %s -filetype=obj -o - | llvm-dwarfdump - | FileCheck %s
6+
; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s -check-prefix=CHECK-IR
7+
; RUN: verify-uselistorder %s
8+
9+
; Example 1: BitField with storage offset (extraData: i64 0)
10+
%struct.BitField = type { i8 }
11+
@bf = global %struct.BitField zeroinitializer, !dbg !9
12+
13+
; Example 2: Static member with constant value (extraData: i32 42)
14+
%struct.Static = type { i32 }
15+
@st = global %struct.Static zeroinitializer, !dbg !16
16+
17+
; Example 3: Discriminant value for variant (extraData: i32 100)
18+
%union.Variant = type { [8 x i8] }
19+
@var = global %union.Variant zeroinitializer, !dbg !24
20+
21+
; Example 4: Inheritance VBPtr offset (extraData: i32 0)
22+
%class.Derived = type { i32 }
23+
@der = global %class.Derived zeroinitializer, !dbg !35
24+
25+
!llvm.dbg.cu = !{!0}
26+
!llvm.module.flags = !{!2, !3, !4}
27+
28+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 11.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !8)
29+
!1 = !DIFile(filename: "test.cpp", directory: ".")
30+
!2 = !{i32 2, !"Debug Info Version", i32 3}
31+
!3 = !{i32 1, !"wchar_size", i32 4}
32+
!4 = !{i32 2, !"Dwarf Version", i32 5}
33+
!8 = !{!9, !16, !24, !35}
34+
35+
; extraData node definitions
36+
!15 = !{i64 0} ; BitField storage offset
37+
!22 = !{i32 42} ; Static member constant value
38+
!33 = !{i32 100} ; Discriminant value
39+
!41 = !{i32 0} ; VBPtr offset
40+
41+
; CHECK-IR: !9 = !DIDerivedType(tag: DW_TAG_member, name: "const_val", scope: !7, file: !3, line: 11, baseType: !10, flags: DIFlagStaticMember, extraData: !12)
42+
; CHECK-IR: !12 = !{i32 42}
43+
; CHECK-IR: !20 = !DIDerivedType(tag: DW_TAG_member, name: "variant_some", scope: !17, file: !3, baseType: !11, size: 32, extraData: !21)
44+
; CHECK-IR: !21 = !{i32 100}
45+
; CHECK-IR: !27 = !DIDerivedType(tag: DW_TAG_inheritance, scope: !25, baseType: !28, extraData: !29)
46+
; CHECK-IR: !29 = !{i32 0}
47+
; CHECK-IR: !32 = !DIDerivedType(tag: DW_TAG_member, name: "field", scope: !30, file: !3, line: 6, baseType: !11, size: 3, flags: DIFlagBitField, extraData: !33)
48+
; CHECK-IR: !33 = !{i64 0}
49+
50+
; CHECK: {{.*}} DW_TAG_variable
51+
; CHECK: {{.*}} DW_AT_name ("bf")
52+
; CHECK: {{.*}} DW_TAG_member
53+
; CHECK: {{.*}} DW_AT_name ("field")
54+
; === BitField: extraData holds storage offset ===
55+
!9 = !DIGlobalVariableExpression(var: !10, expr: !DIExpression())
56+
!10 = distinct !DIGlobalVariable(name: "bf", scope: !0, file: !1, line: 5, type: !11, isLocal: false, isDefinition: true)
57+
!11 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "BitField", file: !1, line: 5, size: 8, elements: !12)
58+
!12 = !{!13}
59+
!13 = !DIDerivedType(tag: DW_TAG_member, name: "field", scope: !11, file: !1, line: 6, baseType: !14, size: 3, flags: DIFlagBitField, extraData: !15)
60+
!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
61+
62+
; CHECK: {{.*}} DW_TAG_variable
63+
; CHECK: {{.*}} DW_AT_name ("st")
64+
; CHECK: {{.*}} DW_TAG_member
65+
; CHECK: {{.*}} DW_AT_name ("const_val")
66+
; CHECK: {{.*}} DW_AT_const_value (42)
67+
; === Static Member: extraData holds constant value ===
68+
!16 = !DIGlobalVariableExpression(var: !17, expr: !DIExpression())
69+
!17 = distinct !DIGlobalVariable(name: "st", scope: !0, file: !1, line: 10, type: !18, isLocal: false, isDefinition: true)
70+
!18 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Static", file: !1, line: 10, size: 32, elements: !19)
71+
!19 = !{!20}
72+
!20 = !DIDerivedType(tag: DW_TAG_member, name: "const_val", scope: !18, file: !1, line: 11, baseType: !21, flags: DIFlagStaticMember, extraData: !22)
73+
!21 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !14)
74+
75+
; CHECK: {{.*}} DW_TAG_variable
76+
; CHECK: {{.*}} DW_AT_name ("var")
77+
; CHECK: {{.*}} DW_TAG_member
78+
; CHECK: {{.*}} DW_AT_name ("variant_none")
79+
; CHECK: {{.*}} DW_AT_discr_value (0x64)
80+
; === Discriminant: extraData holds discriminant value ===
81+
!24 = !DIGlobalVariableExpression(var: !25, expr: !DIExpression())
82+
!25 = distinct !DIGlobalVariable(name: "var", scope: !0, file: !1, line: 15, type: !26, isLocal: false, isDefinition: true)
83+
!26 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Variant", file: !1, line: 15, size: 128, elements: !27)
84+
!27 = !{!28}
85+
!28 = !DICompositeType(tag: DW_TAG_variant_part, scope: !26, file: !1, size: 128, elements: !29, discriminator: !30)
86+
!29 = !{!31, !32}
87+
!30 = !DIDerivedType(tag: DW_TAG_member, scope: !28, file: !1, baseType: !14, size: 32, align: 32, flags: DIFlagArtificial)
88+
!31 = !DIDerivedType(tag: DW_TAG_member, name: "variant_none", scope: !28, file: !1, baseType: !14, size: 32)
89+
!32 = !DIDerivedType(tag: DW_TAG_member, name: "variant_some", scope: !28, file: !1, baseType: !14, size: 32, extraData: !33)
90+
91+
; CHECK: {{.*}} DW_TAG_variable
92+
; CHECK: {{.*}} DW_AT_name ("der")
93+
; CHECK: {{.*}} DW_TAG_inheritance
94+
; CHECK: {{.*}} DW_AT_type ({{.*}} "Base")
95+
; === Inheritance: extraData holds VBPtr offset ===
96+
!35 = !DIGlobalVariableExpression(var: !36, expr: !DIExpression())
97+
!36 = distinct !DIGlobalVariable(name: "der", scope: !0, file: !1, line: 20, type: !37, isLocal: false, isDefinition: true)
98+
!37 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Derived", file: !1, line: 20, size: 32, elements: !38)
99+
!38 = !{!39}
100+
!39 = !DIDerivedType(tag: DW_TAG_inheritance, scope: !37, baseType: !40, extraData: !41)
101+
!40 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Base", file: !1, line: 19, size: 32)

0 commit comments

Comments
 (0)