[DWARF] Support new TAG DW_TAG_LLVM_annotation
authorYonghong Song <yhs@fb.com>
Mon, 19 Jul 2021 16:54:33 +0000 (09:54 -0700)
committerYonghong Song <yhs@fb.com>
Wed, 1 Sep 2021 02:22:17 +0000 (19:22 -0700)
A new LLVM specific TAG DW_TAG_LLVM_annotation is added.
The name is suggested by Paul Robinson ([1]).
Currently, this tag is used to output __attribute__((btf_tag("string")))
annotations in dwarf. The following is an example for a global
variable with two btf_tag attributes:
  0x0000002a:   DW_TAG_variable
                  DW_AT_name      ("g1")
                  DW_AT_type      (0x00000052 "int")
                  DW_AT_external  (true)
                  DW_AT_decl_file ("/tmp/home/yhs/work/tests/llvm/btf_tag/t.c")
                  DW_AT_decl_line (8)
                  DW_AT_location  (DW_OP_addr 0x0)

  0x0000003f:     DW_TAG_LLVM_annotation
                    DW_AT_name    ("btf_tag")
                    DW_AT_const_value     ("tag1")

  0x00000048:     DW_TAG_LLVM_annotation
                    DW_AT_name    ("btf_tag")
                    DW_AT_const_value     ("tag2")

  0x00000051:     NULL

In the future, DW_TAG_LLVM_annotation may encode other type
of non-string const value.

 [1] https://lists.llvm.org/pipermail/llvm-dev/2021-June/151250.html

Differential Revision: https://reviews.llvm.org/D106621

llvm/include/llvm/BinaryFormat/Dwarf.def
llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
llvm/test/DebugInfo/attr-btf_tag.ll [new file with mode: 0644]

index 34f124b..61f3f27 100644 (file)
@@ -248,6 +248,9 @@ HANDLE_DW_TAG(0x5103, ALTIUM_rev_carry_type, 0, ALTIUM, DW_KIND_NONE)
 // M16 __rom qualifier
 HANDLE_DW_TAG(0x5111, ALTIUM_rom, 0, ALTIUM, DW_KIND_NONE)
 
+// LLVM
+HANDLE_DW_TAG(0x6000, LLVM_annotation, 0, LLVM, DW_KIND_NONE)
+
 // Green Hills.
 HANDLE_DW_TAG(0x8004, GHS_namespace, 0, GHS, DW_KIND_NONE)
 HANDLE_DW_TAG(0x8005, GHS_using_namespace, 0, GHS, DW_KIND_NONE)
index 7edc44c..79c56a5 100644 (file)
@@ -183,6 +183,8 @@ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(
   else
     addGlobalName(GV->getName(), *VariableDIE, DeclContext);
 
+  addAnnotation(*VariableDIE, GV->getAnnotations());
+
   if (uint32_t AlignInBytes = GV->getAlignInBytes())
     addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
             AlignInBytes);
@@ -1480,10 +1482,12 @@ void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var,
   if (!Name.empty())
     addString(VariableDie, dwarf::DW_AT_name, Name);
   const auto *DIVar = Var.getVariable();
-  if (DIVar)
+  if (DIVar) {
     if (uint32_t AlignInBytes = DIVar->getAlignInBytes())
       addUInt(VariableDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
               AlignInBytes);
+    addAnnotation(VariableDie, DIVar->getAnnotations());
+  }
 
   addSourceLine(VariableDie, DIVar);
   addType(VariableDie, Var.getType());
index 9d7b3d6..7643728 100644 (file)
@@ -833,6 +833,23 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
     addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
 }
 
+void DwarfUnit::addAnnotation(DIE &Buffer, DINodeArray Annotations) {
+  if (!Annotations)
+    return;
+
+  for (const Metadata *Annotation : Annotations->operands()) {
+    const MDNode *MD = cast<MDNode>(Annotation);
+    const MDString *Name = cast<MDString>(MD->getOperand(0));
+
+    // Currently, only MDString is supported with btf_tag attribute.
+    const MDString *Value = cast<MDString>(MD->getOperand(1));
+
+    DIE &AnnotationDie = createAndAddDIE(dwarf::DW_TAG_LLVM_annotation, Buffer);
+    addString(AnnotationDie, dwarf::DW_AT_name, Name->getString());
+    addString(AnnotationDie, dwarf::DW_AT_const_value, Value->getString());
+  }
+}
+
 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
   // Add name if not anonymous or intermediate type.
   StringRef Name = CTy->getName();
@@ -961,6 +978,8 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
   if (!Name.empty())
     addString(Buffer, dwarf::DW_AT_name, Name);
 
+  addAnnotation(Buffer, CTy->getAnnotations());
+
   if (Tag == dwarf::DW_TAG_enumeration_type ||
       Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
       Tag == dwarf::DW_TAG_union_type) {
@@ -1197,6 +1216,8 @@ void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
   if (!SP->getName().empty())
     addString(SPDie, dwarf::DW_AT_name, SP->getName());
 
+  addAnnotation(SPDie, SP->getAnnotations());
+
   if (!SkipSPSourceLocation)
     addSourceLine(SPDie, SP);
 
@@ -1547,6 +1568,8 @@ DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
   if (!Name.empty())
     addString(MemberDie, dwarf::DW_AT_name, Name);
 
+  addAnnotation(MemberDie, DT->getAnnotations());
+
   if (DIType *Resolved = DT->getBaseType())
     addType(MemberDie, Resolved);
 
index 4d31dd0..8140279 100644 (file)
@@ -294,6 +294,9 @@ public:
   void addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
                        const MCSymbol *Label, const MCSymbol *Sec);
 
+  /// Add DW_TAG_LLVM_annotation.
+  void addAnnotation(DIE &Buffer, DINodeArray Annotations);
+
   /// Get context owner's DIE.
   DIE *createTypeDIE(const DICompositeType *Ty);
 
diff --git a/llvm/test/DebugInfo/attr-btf_tag.ll b/llvm/test/DebugInfo/attr-btf_tag.ll
new file mode 100644 (file)
index 0000000..adf43e6
--- /dev/null
@@ -0,0 +1,135 @@
+; REQUIRES: x86-registered-target
+; RUN: llc -filetype=obj -o %t %s
+; RUN: llvm-dwarfdump -debug-info %t | FileCheck %s
+; Source:
+;   #define __tag1 __attribute__((btf_tag("tag1")))
+;   #define __tag2 __attribute__((btf_tag("tag2")))
+;
+;   struct t1 {
+;     int a __tag1 __tag2;
+;   } __tag1 __tag2;
+;
+;   int g1 __tag1 __tag2;
+;
+;   int __tag1 __tag2 foo(struct t1 *arg __tag1 __tag2) {
+;     return arg->a;
+;   }
+; Compilation flag:
+;   clang -target x86_64 -g -S -emit-llvm t.c
+
+%struct.t1 = type { i32 }
+
+@g1 = dso_local global i32 0, align 4, !dbg !0
+
+; Function Attrs: noinline nounwind optnone uwtable
+define dso_local i32 @foo(%struct.t1* %arg) #0 !dbg !16 {
+entry:
+  %arg.addr = alloca %struct.t1*, align 8
+  store %struct.t1* %arg, %struct.t1** %arg.addr, align 8
+  call void @llvm.dbg.declare(metadata %struct.t1** %arg.addr, metadata !23, metadata !DIExpression()), !dbg !24
+  %0 = load %struct.t1*, %struct.t1** %arg.addr, align 8, !dbg !25
+  %a = getelementptr inbounds %struct.t1, %struct.t1* %0, i32 0, i32 0, !dbg !26
+  %1 = load i32, i32* %a, align 4, !dbg !26
+  ret i32 %1, !dbg !27
+}
+
+; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
+declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
+
+attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+attributes #1 = { nofree nosync nounwind readnone speculatable willreturn }
+
+!llvm.dbg.cu = !{!2}
+!llvm.module.flags = !{!10, !11, !12, !13, !14}
+!llvm.ident = !{!15}
+
+!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+!1 = distinct !DIGlobalVariable(name: "g1", scope: !2, file: !3, line: 8, type: !6, isLocal: false, isDefinition: true, annotations: !7)
+
+; CHECK:       DW_TAG_variable
+; CHECK-NEXT:     DW_AT_name      ("g1")
+; CHECK:          DW_TAG_LLVM_annotation
+; CHECK-NEXT:       DW_AT_name    ("btf_tag")
+; CHECK-NEXT:       DW_AT_const_value     ("tag1")
+; CHECK-EMPTY:
+; CHECK-NEXT:     DW_TAG_LLVM_annotation
+; CHECK-NEXT:       DW_AT_name    ("btf_tag")
+; CHECK-NEXT:       DW_AT_const_value     ("tag2")
+; CHECK-EMPTY:
+; CHECK-NEXT:     NULL
+
+!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 13.0.0 (https://github.com/llvm/llvm-project.git 305231a4f71b68945b4dd92925c76ff49e377c86)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
+!3 = !DIFile(filename: "t.c", directory: "/tmp/home/yhs/work/tests/llvm/btf_tag")
+!4 = !{}
+!5 = !{!0}
+!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!7 = !{!8, !9}
+!8 = !{!"btf_tag", !"tag1"}
+!9 = !{!"btf_tag", !"tag2"}
+!10 = !{i32 7, !"Dwarf Version", i32 4}
+!11 = !{i32 2, !"Debug Info Version", i32 3}
+!12 = !{i32 1, !"wchar_size", i32 4}
+!13 = !{i32 7, !"uwtable", i32 1}
+!14 = !{i32 7, !"frame-pointer", i32 2}
+!15 = !{!"clang version 13.0.0 (https://github.com/llvm/llvm-project.git 305231a4f71b68945b4dd92925c76ff49e377c86)"}
+!16 = distinct !DISubprogram(name: "foo", scope: !3, file: !3, line: 10, type: !17, scopeLine: 10, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !4, annotations: !7)
+
+; CHECK:      DW_TAG_subprogram
+; CHECK:        DW_AT_name      ("foo")
+; CHECK:        DW_TAG_formal_parameter
+; CHECK:          DW_TAG_LLVM_annotation
+; CHECK-NEXT:       DW_AT_name    ("btf_tag")
+; CHECK-NEXT:       DW_AT_const_value     ("tag1")
+; CHECK-EMPTY:
+; CHECK-NEXT:     DW_TAG_LLVM_annotation
+; CHECK-NEXT:       DW_AT_name    ("btf_tag")
+; CHECK-NEXT:       DW_AT_const_value     ("tag2")
+; CHECK-EMPTY:
+; CHECK-NEXT:     NULL
+; CHECK-EMPTY:
+; CHECK-NEXT:   DW_TAG_LLVM_annotation
+; CHECK-NEXT:     DW_AT_name    ("btf_tag")
+; CHECK-NEXT:     DW_AT_const_value     ("tag1")
+; CHECK-EMPTY:
+; CHECK-NEXT:   DW_TAG_LLVM_annotation
+; CHECK-NEXT:     DW_AT_name    ("btf_tag")
+; CHECK-NEXT:     DW_AT_const_value     ("tag2")
+; CHECK-EMPTY:
+; CHECK-NEXT:   NULL
+
+!17 = !DISubroutineType(types: !18)
+!18 = !{!6, !19}
+!19 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !20, size: 64)
+!20 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "t1", file: !3, line: 4, size: 32, elements: !21, annotations: !7)
+!21 = !{!22}
+!22 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !20, file: !3, line: 5, baseType: !6, size: 32, annotations: !7)
+
+; CHECK:      DW_TAG_structure_type
+; CHECK-NEXT:   DW_AT_name      ("t1")
+; CHECK:        DW_TAG_member
+; CHECK-NEXT:     DW_AT_name      ("a")
+; CHECK:          DW_TAG_LLVM_annotation
+; CHECK-NEXT:       DW_AT_name    ("btf_tag")
+; CHECK-NEXT:       DW_AT_const_value     ("tag1")
+; CHECK-EMPTY:
+; CHECK-NEXT:     DW_TAG_LLVM_annotation
+; CHECK-NEXT:       DW_AT_name    ("btf_tag")
+; CHECK-NEXT:       DW_AT_const_value     ("tag2")
+; CHECK-EMPTY:
+; CHECK-NEXT:     NULL
+; CHECK-EMPTY:
+; CHECK-NEXT:   DW_TAG_LLVM_annotation
+; CHECK-NEXT:     DW_AT_name    ("btf_tag")
+; CHECK-NEXT:     DW_AT_const_value     ("tag1")
+; CHECK-EMPTY:
+; CHECK-NEXT:   DW_TAG_LLVM_annotation
+; CHECK-NEXT:     DW_AT_name    ("btf_tag")
+; CHECK-NEXT:     DW_AT_const_value     ("tag2")
+; CHECK-EMPTY:
+; CHECK-NEXT:   NULL
+
+!23 = !DILocalVariable(name: "arg", arg: 1, scope: !16, file: !3, line: 10, type: !19, annotations: !7)
+!24 = !DILocation(line: 10, column: 48, scope: !16)
+!25 = !DILocation(line: 11, column: 10, scope: !16)
+!26 = !DILocation(line: 11, column: 15, scope: !16)
+!27 = !DILocation(line: 11, column: 3, scope: !16)