Skip to content

Commit da90fd7

Browse files
committed
Reland "[llvm][DebugInfo] DWARFv5: static data members declarations are DW_TAG_variable (#72234)"
This was reverted because it broke the OCaml LLVM bindings. Relanding the original patch but without changing the C-API. They'll continue to work just fine as they do today. If in the future there is a need to pass a new tag to the C-API for creating static members, then we'll make the change to the OCaml bindings at that time. Original commit message: """ This patch adds the LLVM-side infrastructure to implement DWARFv5 issue 161118.1: "DW_TAG for C++ static data members". The clang-side of this patch will simply construct the DIDerivedType with a different DW_TAG. """
1 parent 808caa9 commit da90fd7

File tree

7 files changed

+125
-14
lines changed

7 files changed

+125
-14
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,8 @@ CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
16811681
llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD);
16821682
auto Align = getDeclAlignIfRequired(Var, CGM.getContext());
16831683
llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
1684-
RecordTy, VName, VUnit, LineNumber, VTy, Flags, /* Val */ nullptr, Align);
1684+
RecordTy, VName, VUnit, LineNumber, VTy, Flags, /* Val */ nullptr,
1685+
llvm::dwarf::DW_TAG_member, Align);
16851686
StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
16861687
StaticDataMemberDefinitionsToEmit.push_back(Var->getCanonicalDecl());
16871688
return GV;

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,12 @@ namespace llvm {
376376
/// \param Ty Type of the static member.
377377
/// \param Flags Flags to encode member attribute, e.g. private.
378378
/// \param Val Const initializer of the member.
379+
/// \param Tag DWARF tag of the static member.
379380
/// \param AlignInBits Member alignment.
380381
DIDerivedType *createStaticMemberType(DIScope *Scope, StringRef Name,
381382
DIFile *File, unsigned LineNo,
382383
DIType *Ty, DINode::DIFlags Flags,
383-
Constant *Val,
384+
Constant *Val, unsigned Tag,
384385
uint32_t AlignInBits = 0);
385386

386387
/// Create debugging information entry for Objective-C

llvm/lib/IR/DIBuilder.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,11 @@ DIDerivedType *
405405
DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name, DIFile *File,
406406
unsigned LineNumber, DIType *Ty,
407407
DINode::DIFlags Flags, llvm::Constant *Val,
408-
uint32_t AlignInBits) {
408+
unsigned Tag, uint32_t AlignInBits) {
409409
Flags |= DINode::FlagStaticMember;
410-
return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
411-
LineNumber, getNonCompileUnitScope(Scope), Ty, 0,
412-
AlignInBits, 0, std::nullopt, Flags,
413-
getConstantOrNull(Val));
410+
return DIDerivedType::get(VMContext, Tag, Name, File, LineNumber,
411+
getNonCompileUnitScope(Scope), Ty, 0, AlignInBits,
412+
0, std::nullopt, Flags, getConstantOrNull(Val));
414413
}
415414

416415
DIDerivedType *

llvm/lib/IR/DebugInfo.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,17 +1315,15 @@ LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
13151315
return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
13161316
}
13171317

1318-
LLVMMetadataRef
1319-
LLVMDIBuilderCreateStaticMemberType(
1318+
LLVMMetadataRef LLVMDIBuilderCreateStaticMemberType(
13201319
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
13211320
size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
13221321
LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
13231322
uint32_t AlignInBits) {
13241323
return wrap(unwrap(Builder)->createStaticMemberType(
1325-
unwrapDI<DIScope>(Scope), {Name, NameLen},
1326-
unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
1327-
map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
1328-
AlignInBits));
1324+
unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
1325+
LineNumber, unwrapDI<DIType>(Type), map_from_llvmDIFlags(Flags),
1326+
unwrap<Constant>(ConstantVal), DW_TAG_member, AlignInBits));
13291327
}
13301328

13311329
LLVMMetadataRef

llvm/lib/IR/DebugInfoMetadata.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,9 @@ Constant *DIDerivedType::getStorageOffsetInBits() const {
711711
}
712712

713713
Constant *DIDerivedType::getConstant() const {
714-
assert(getTag() == dwarf::DW_TAG_member && isStaticMember());
714+
assert((getTag() == dwarf::DW_TAG_member ||
715+
getTag() == dwarf::DW_TAG_variable) &&
716+
isStaticMember());
715717
if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData()))
716718
return C->getValue();
717719
return nullptr;

llvm/lib/IR/Verifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ void Verifier::visitDIDerivedType(const DIDerivedType &N) {
11721172
N.getTag() == dwarf::DW_TAG_restrict_type ||
11731173
N.getTag() == dwarf::DW_TAG_atomic_type ||
11741174
N.getTag() == dwarf::DW_TAG_member ||
1175+
(N.getTag() == dwarf::DW_TAG_variable && N.isStaticMember()) ||
11751176
N.getTag() == dwarf::DW_TAG_inheritance ||
11761177
N.getTag() == dwarf::DW_TAG_friend ||
11771178
N.getTag() == dwarf::DW_TAG_set_type,
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
; RUN: %llc_dwarf -filetype=obj -o %t.o < %s
2+
; RUN: llvm-dwarfdump -v -debug-info %t.o | FileCheck %s -check-prefix=CHECK
3+
4+
; LLVM IR generated using: clang -emit-llvm -S -g -gdwarf-5
5+
;
6+
; struct C
7+
; {
8+
; static int a;
9+
; const static bool b = true;
10+
; static constexpr int c = 15;
11+
; };
12+
;
13+
; int C::a = 10;
14+
;
15+
; int main()
16+
; {
17+
; C instance_C;
18+
; return C::a + (int)C::b + C::c;
19+
; }
20+
21+
source_filename = "llvm/test/DebugInfo/X86/dwarf5-debug-info-static-member.ll"
22+
23+
%struct.C = type { i8 }
24+
25+
@_ZN1C1aE = global i32 10, align 4, !dbg !0
26+
27+
; Function Attrs: mustprogress noinline norecurse nounwind optnone ssp uwtable(sync)
28+
define noundef i32 @main() #0 !dbg !26 {
29+
entry:
30+
%retval = alloca i32, align 4
31+
%instance_C = alloca %struct.C, align 1
32+
store i32 0, ptr %retval, align 4
33+
call void @llvm.dbg.declare(metadata ptr %instance_C, metadata !30, metadata !DIExpression()), !dbg !31
34+
%0 = load i32, ptr @_ZN1C1aE, align 4, !dbg !32
35+
%add = add nsw i32 %0, 1, !dbg !33
36+
%add1 = add nsw i32 %add, 15, !dbg !34
37+
ret i32 %add1, !dbg !35
38+
}
39+
40+
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
41+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
42+
43+
attributes #0 = { nounwind uwtable }
44+
attributes #1 = { nounwind readnone }
45+
46+
!llvm.dbg.cu = !{!2}
47+
!llvm.module.flags = !{!19, !20, !21, !22, !23, !24}
48+
!llvm.ident = !{!25}
49+
50+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
51+
!1 = distinct !DIGlobalVariable(name: "a", linkageName: "_ZN1C1aE", scope: !2, file: !3, line: 8, type: !5, isLocal: false, isDefinition: true, declaration: !8)
52+
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, producer: "clang version 18.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: !4, globals: !14, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
53+
!3 = !DIFile(filename: "main.cpp", directory: "/tmp", checksumkind: CSK_MD5, checksum: "b2547f7df8f54777c012dbfbd626f155")
54+
!4 = !{!5, !6}
55+
!5 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
56+
!6 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "C", file: !3, line: 1, size: 8, flags: DIFlagTypePassByValue, elements: !7, identifier: "_ZTS1C")
57+
!7 = !{!8, !9, !12}
58+
!8 = !DIDerivedType(tag: DW_TAG_variable, name: "a", scope: !6, file: !3, line: 3, baseType: !5, flags: DIFlagStaticMember)
59+
!9 = !DIDerivedType(tag: DW_TAG_variable, name: "b", scope: !6, file: !3, line: 4, baseType: !10, flags: DIFlagStaticMember)
60+
!10 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !11)
61+
!11 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean)
62+
!12 = !DIDerivedType(tag: DW_TAG_variable, name: "c", scope: !6, file: !3, line: 5, baseType: !13, flags: DIFlagStaticMember)
63+
!13 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !5)
64+
!14 = !{!0, !15, !17}
65+
!15 = !DIGlobalVariableExpression(var: !16, expr: !DIExpression(DW_OP_constu, 1, DW_OP_stack_value))
66+
!16 = distinct !DIGlobalVariable(name: "b", scope: !2, file: !3, line: 4, type: !10, isLocal: true, isDefinition: true, declaration: !9)
67+
!17 = !DIGlobalVariableExpression(var: !18, expr: !DIExpression(DW_OP_constu, 15, DW_OP_stack_value))
68+
!18 = distinct !DIGlobalVariable(name: "c", scope: !2, file: !3, line: 5, type: !13, isLocal: true, isDefinition: true, declaration: !12)
69+
!19 = !{i32 7, !"Dwarf Version", i32 5}
70+
!20 = !{i32 2, !"Debug Info Version", i32 3}
71+
!21 = !{i32 1, !"wchar_size", i32 4}
72+
!22 = !{i32 8, !"PIC Level", i32 2}
73+
!23 = !{i32 7, !"uwtable", i32 1}
74+
!24 = !{i32 7, !"frame-pointer", i32 1}
75+
!25 = !{!"clang version 18.0.0"}
76+
!26 = distinct !DISubprogram(name: "main", scope: !3, file: !3, line: 10, type: !27, scopeLine: 11, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !29)
77+
!27 = !DISubroutineType(types: !28)
78+
!28 = !{!5}
79+
!29 = !{}
80+
!30 = !DILocalVariable(name: "instance_C", scope: !26, file: !3, line: 12, type: !6)
81+
!31 = !DILocation(line: 12, column: 5, scope: !26)
82+
!32 = !DILocation(line: 13, column: 10, scope: !26)
83+
!33 = !DILocation(line: 13, column: 15, scope: !26)
84+
!34 = !DILocation(line: 13, column: 27, scope: !26)
85+
!35 = !DILocation(line: 13, column: 3, scope: !26)
86+
87+
; CHECK: .debug_info contents:
88+
; CHECK: DW_TAG_variable
89+
; CHECK-NEXT: DW_AT_specification {{.*}} "a"
90+
; CHECK: DW_TAG_structure_type
91+
; CHECK: DW_AT_name {{.*}} "C"
92+
; CHECK: DW_TAG_variable
93+
; CHECK-NEXT: DW_AT_name {{.*}} "a"
94+
; CHECK: DW_AT_external
95+
; CHECK: DW_AT_declaration
96+
; CHECK: DW_TAG_variable
97+
; CHECK-NEXT: DW_AT_name {{.*}} "b"
98+
; CHECK: DW_AT_external
99+
; CHECK: DW_AT_declaration
100+
; CHECK: DW_TAG_variable
101+
; CHECK-NEXT: DW_AT_name {{.*}} "c"
102+
; CHECK: DW_AT_external
103+
; CHECK: DW_AT_declaration
104+
; CHECK: NULL
105+
; CHECK: DW_TAG_variable
106+
; CHECK-NEXT: DW_AT_specification {{.*}} "b"
107+
; CHECK-NEXT: DW_AT_const_value
108+
; CHECK: DW_TAG_variable
109+
; CHECK-NEXT: DW_AT_specification {{.*}} "c"

0 commit comments

Comments
 (0)