[llvm] f6811ce - [DebugInfo] Support typedef with btf_decl_tag attributes

Yonghong Song via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 21 08:50:15 PDT 2021


Author: Yonghong Song
Date: 2021-10-21T08:42:58-07:00
New Revision: f6811cec84218912d1c7c9b0b8d308834e6e24e3

URL: https://github.com/llvm/llvm-project/commit/f6811cec84218912d1c7c9b0b8d308834e6e24e3
DIFF: https://github.com/llvm/llvm-project/commit/f6811cec84218912d1c7c9b0b8d308834e6e24e3.diff

LOG: [DebugInfo] Support typedef with btf_decl_tag attributes

Clang patch ([1]) added support for btf_decl_tag attributes with typedef
types. This patch added llvm support including dwarf generation.
For example, for typedef
   typedef unsigned * __u __attribute__((btf_decl_tag("tag1")));
   __u u;
the following shows llvm-dwarfdump result:
   0x00000033:   DW_TAG_typedef
                   DW_AT_type      (0x00000048 "unsigned int *")
                   DW_AT_name      ("__u")
                   DW_AT_decl_file ("/home/yhs/work/tests/llvm/btf_tag/t.c")
                   DW_AT_decl_line (1)

   0x0000003e:     DW_TAG_LLVM_annotation
                     DW_AT_name    ("btf_decl_tag")
                     DW_AT_const_value     ("tag1")

   0x00000047:     NULL

  [1] https://reviews.llvm.org/D110127

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

Added: 
    clang/test/CodeGen/attr-btf_tag-typedef.c
    llvm/test/Bitcode/attr-btf_tag-typedef.ll
    llvm/test/DebugInfo/X86/attr-btf_tag-typedef.ll

Modified: 
    clang/lib/CodeGen/CGDebugInfo.cpp
    llvm/include/llvm/IR/DIBuilder.h
    llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
    llvm/lib/IR/DIBuilder.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 132b5c28e8f55..6e477d40b83e6 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -1276,9 +1276,11 @@ llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
 
   uint32_t Align = getDeclAlignIfRequired(Ty->getDecl(), CGM.getContext());
   // Typedefs are derived from some other type.
+  llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(Ty->getDecl());
   return DBuilder.createTypedef(Underlying, Ty->getDecl()->getName(),
                                 getOrCreateFile(Loc), getLineNumber(Loc),
-                                getDeclContextDescriptor(Ty->getDecl()), Align);
+                                getDeclContextDescriptor(Ty->getDecl()), Align,
+                                Annotations);
 }
 
 static unsigned getDwarfCC(CallingConv CC) {

diff  --git a/clang/test/CodeGen/attr-btf_tag-typedef.c b/clang/test/CodeGen/attr-btf_tag-typedef.c
new file mode 100644
index 0000000000000..57aafde15893d
--- /dev/null
+++ b/clang/test/CodeGen/attr-btf_tag-typedef.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s
+
+#define __tag1 __attribute__((btf_decl_tag("tag1")))
+typedef struct { int a; } __s __tag1;
+typedef unsigned * __u __tag1;
+__s a;
+__u u;
+
+// CHECK: !DIDerivedType(tag: DW_TAG_typedef, name: "__u", file: ![[#]], line: [[#]], baseType: ![[#]], annotations: ![[ANNOT:[0-9]+]])
+// CHECK: ![[ANNOT]] = !{![[TAG1:[0-9]+]]}
+// CHECK: ![[TAG1]] = !{!"btf_decl_tag", !"tag1"}
+
+// CHECK: !DIDerivedType(tag: DW_TAG_typedef, name: "__s", file: ![[#]], line: [[#]], baseType: ![[#]], annotations: ![[ANNOT]])

diff  --git a/llvm/include/llvm/IR/DIBuilder.h b/llvm/include/llvm/IR/DIBuilder.h
index 28c70877aa2e8..90165095bb0c0 100644
--- a/llvm/include/llvm/IR/DIBuilder.h
+++ b/llvm/include/llvm/IR/DIBuilder.h
@@ -250,9 +250,11 @@ namespace llvm {
     /// \param LineNo      Line number.
     /// \param Context     The surrounding context for the typedef.
     /// \param AlignInBits Alignment. (optional)
+    /// \param Annotations Annotations. (optional)
     DIDerivedType *createTypedef(DIType *Ty, StringRef Name, DIFile *File,
                                  unsigned LineNo, DIScope *Context,
-                                 uint32_t AlignInBits = 0);
+                                 uint32_t AlignInBits = 0,
+                                 DINodeArray Annotations = nullptr);
 
     /// Create debugging information entry for a 'friend'.
     DIDerivedType *createFriend(DIType *Ty, DIType *FriendTy);

diff  --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index d4a7808b9d1e3..f1af9e2373f81 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -754,6 +754,8 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
   if (!Name.empty())
     addString(Buffer, dwarf::DW_AT_name, Name);
 
+  addAnnotation(Buffer, DTy->getAnnotations());
+
   // If alignment is specified for a typedef , create and insert DW_AT_alignment
   // attribute in DW_TAG_typedef DIE.
   if (Tag == dwarf::DW_TAG_typedef && DD->getDwarfVersion() >= 5) {

diff  --git a/llvm/lib/IR/DIBuilder.cpp b/llvm/lib/IR/DIBuilder.cpp
index 8ff4c2182d2df..de4ab5981883d 100644
--- a/llvm/lib/IR/DIBuilder.cpp
+++ b/llvm/lib/IR/DIBuilder.cpp
@@ -324,10 +324,12 @@ DIDerivedType *DIBuilder::createReferenceType(
 DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
                                         DIFile *File, unsigned LineNo,
                                         DIScope *Context,
-                                        uint32_t AlignInBits) {
+                                        uint32_t AlignInBits,
+                                        DINodeArray Annotations) {
   return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
                             LineNo, getNonCompileUnitScope(Context), Ty, 0,
-                            AlignInBits, 0, None, DINode::FlagZero);
+                            AlignInBits, 0, None, DINode::FlagZero, nullptr,
+                            Annotations);
 }
 
 DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {

diff  --git a/llvm/test/Bitcode/attr-btf_tag-typedef.ll b/llvm/test/Bitcode/attr-btf_tag-typedef.ll
new file mode 100644
index 0000000000000..c7be5d4eebc7f
--- /dev/null
+++ b/llvm/test/Bitcode/attr-btf_tag-typedef.ll
@@ -0,0 +1,54 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+;
+; Source:
+;   #define __tag1 __attribute__((btf_decl_tag("tag1")))
+;   typedef struct { int a; } __s __tag1;
+;   typedef unsigned * __u __tag1;
+;   __s a;
+;   __u u;
+; Compilation flag:
+;   clang -S -g -emit-llvm typedef.c
+
+%struct.__s = type { i32 }
+
+ at a = dso_local global %struct.__s zeroinitializer, align 4, !dbg !0
+ at u = dso_local global i32* null, align 8, !dbg !5
+
+!llvm.dbg.cu = !{!2}
+!llvm.module.flags = !{!17, !18, !19, !20, !21}
+!llvm.ident = !{!22}
+
+!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+!1 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 4, type: !12, isLocal: false, isDefinition: true)
+!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 14.0.0 (https://github.com/llvm/llvm-project.git b9757992b73e823edf1fa699372ff9cd29db6cb7)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: None)
+!3 = !DIFile(filename: "typedef.c", directory: "/home/yhs/work/tests/llvm/btf_tag")
+!4 = !{!0, !5}
+!5 = !DIGlobalVariableExpression(var: !6, expr: !DIExpression())
+!6 = distinct !DIGlobalVariable(name: "u", scope: !2, file: !3, line: 5, type: !7, isLocal: false, isDefinition: true)
+!7 = !DIDerivedType(tag: DW_TAG_typedef, name: "__u", file: !3, line: 3, baseType: !8, annotations: !10)
+!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 64)
+!9 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
+!10 = !{!11}
+!11 = !{!"btf_decl_tag", !"tag1"}
+
+; CHECK:      !DIDerivedType(tag: DW_TAG_typedef, name: "__u"
+; CHECK-SAME: annotations: ![[ANNOT:[0-9]+]]
+; CHECK:      ![[ANNOT]] = !{![[TAG1:[0-9]+]]}
+; CHECK:      ![[TAG1]] = !{!"btf_decl_tag", !"tag1"}
+
+!12 = !DIDerivedType(tag: DW_TAG_typedef, name: "__s", file: !3, line: 2, baseType: !13, annotations: !10)
+
+; CHECK:      !DIDerivedType(tag: DW_TAG_typedef, name: "__s"
+; CHECK-SAME: annotations: ![[ANNOT]]
+
+!13 = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !3, line: 2, size: 32, elements: !14)
+!14 = !{!15}
+!15 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !13, file: !3, line: 2, baseType: !16, size: 32)
+
+!16 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!17 = !{i32 7, !"Dwarf Version", i32 4}
+!18 = !{i32 2, !"Debug Info Version", i32 3}
+!19 = !{i32 1, !"wchar_size", i32 4}
+!20 = !{i32 7, !"uwtable", i32 1}
+!21 = !{i32 7, !"frame-pointer", i32 2}
+!22 = !{!"clang version 14.0.0 (https://github.com/llvm/llvm-project.git b9757992b73e823edf1fa699372ff9cd29db6cb7)"}

diff  --git a/llvm/test/DebugInfo/X86/attr-btf_tag-typedef.ll b/llvm/test/DebugInfo/X86/attr-btf_tag-typedef.ll
new file mode 100644
index 0000000000000..e36a84a185219
--- /dev/null
+++ b/llvm/test/DebugInfo/X86/attr-btf_tag-typedef.ll
@@ -0,0 +1,69 @@
+; RUN: llc -mtriple=x86_64-linux -filetype=obj -o %t %s
+; RUN: llvm-dwarfdump -debug-info %t | FileCheck %s
+;
+; Source:
+;   #define __tag1 __attribute__((btf_decl_tag("tag1")))
+;   typedef struct { int a; } __s __tag1;
+;   typedef unsigned * __u __tag1;
+;   __s a;
+;   __u u;
+; Compilation flag:
+;   clang -S -g -emit-llvm typedef.c
+
+%struct.__s = type { i32 }
+
+ at a = dso_local global %struct.__s zeroinitializer, align 4, !dbg !0
+ at u = dso_local global i32* null, align 8, !dbg !5
+
+!llvm.dbg.cu = !{!2}
+!llvm.module.flags = !{!17, !18, !19, !20, !21}
+!llvm.ident = !{!22}
+
+!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+!1 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 4, type: !12, isLocal: false, isDefinition: true)
+!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 14.0.0 (https://github.com/llvm/llvm-project.git b9757992b73e823edf1fa699372ff9cd29db6cb7)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: None)
+!3 = !DIFile(filename: "typedef.c", directory: "/home/yhs/work/tests/llvm/btf_tag")
+!4 = !{!0, !5}
+!5 = !DIGlobalVariableExpression(var: !6, expr: !DIExpression())
+!6 = distinct !DIGlobalVariable(name: "u", scope: !2, file: !3, line: 5, type: !7, isLocal: false, isDefinition: true)
+!7 = !DIDerivedType(tag: DW_TAG_typedef, name: "__u", file: !3, line: 3, baseType: !8, annotations: !10)
+!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 64)
+!9 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
+!10 = !{!11}
+!11 = !{!"btf_decl_tag", !"tag1"}
+!12 = !DIDerivedType(tag: DW_TAG_typedef, name: "__s", file: !3, line: 2, baseType: !13, annotations: !10)
+!13 = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !3, line: 2, size: 32, elements: !14)
+!14 = !{!15}
+!15 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !13, file: !3, line: 2, baseType: !16, size: 32)
+!16 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+
+; CHECK:        DW_TAG_typedef
+; CHECK-NEXT:     DW_AT_type
+; CHECK-NEXT:     DW_AT_name      ("__s")
+; CHECK-NEXT:     DW_AT_decl_file
+; CHECK-NEXT:     DW_AT_decl_line
+; CHECK-EMPTY:
+; CHECK-NEXT:     DW_TAG_LLVM_annotation
+; CHECK-NEXT:       DW_AT_name    ("btf_decl_tag")
+; CHECK-NEXT:       DW_AT_const_value     ("tag1")
+; CHECK-EMPTY:
+; CHECK-NEXT:     NULL
+
+; CHECK:        DW_TAG_typedef
+; CHECK-NEXT:     DW_AT_type
+; CHECK-NEXT:     DW_AT_name      ("__u")
+; CHECK-NEXT:     DW_AT_decl_file
+; CHECK-NEXT:     DW_AT_decl_line
+; CHECK-EMPTY:
+; CHECK-NEXT:     DW_TAG_LLVM_annotation
+; CHECK-NEXT:       DW_AT_name    ("btf_decl_tag")
+; CHECK-NEXT:       DW_AT_const_value     ("tag1")
+; CHECK-EMPTY:
+; CHECK-NEXT:     NULL
+
+!17 = !{i32 7, !"Dwarf Version", i32 4}
+!18 = !{i32 2, !"Debug Info Version", i32 3}
+!19 = !{i32 1, !"wchar_size", i32 4}
+!20 = !{i32 7, !"uwtable", i32 1}
+!21 = !{i32 7, !"frame-pointer", i32 2}
+!22 = !{!"clang version 14.0.0 (https://github.com/llvm/llvm-project.git b9757992b73e823edf1fa699372ff9cd29db6cb7)"}


        


More information about the llvm-commits mailing list