Skip to content

Commit 30c2884

Browse files
committed
[DebugInfo] generate btf_tag annotations for DIGlobalVariable
Generate btf_tag annotations for DIGlobalVariable. A field "annotations" is introduced to DIGlobalVariable, and annotations are represented as an DINodeArray, similar to DIComposite elements. The following example illustrates how annotations are encoded in IR: distinct !DIGlobalVariable(..., annotations: !10) !10 = !{!11, !12} !11 = !{!"btf_tag", !"a"} !12 = !{!"btf_tag", !"b"} Differential Revision: https://reviews.llvm.org/D106619
1 parent be19aee commit 30c2884

File tree

11 files changed

+111
-41
lines changed

11 files changed

+111
-41
lines changed

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ namespace llvm {
641641
DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
642642
unsigned LineNo, DIType *Ty, bool IsLocalToUnit, bool isDefined = true,
643643
DIExpression *Expr = nullptr, MDNode *Decl = nullptr,
644-
MDTuple *TemplateParams = nullptr, uint32_t AlignInBits = 0);
644+
MDTuple *TemplateParams = nullptr, uint32_t AlignInBits = 0,
645+
DINodeArray Annotations = nullptr);
645646

646647
/// Identical to createGlobalVariable
647648
/// except that the resulting DbgNode is temporary and meant to be RAUWed.

llvm/include/llvm/IR/DebugInfoMetadata.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2962,25 +2962,28 @@ class DIGlobalVariable : public DIVariable {
29622962
StringRef LinkageName, DIFile *File, unsigned Line, DIType *Type,
29632963
bool IsLocalToUnit, bool IsDefinition,
29642964
DIDerivedType *StaticDataMemberDeclaration, MDTuple *TemplateParams,
2965-
uint32_t AlignInBits, StorageType Storage, bool ShouldCreate = true) {
2965+
uint32_t AlignInBits, DINodeArray Annotations, StorageType Storage,
2966+
bool ShouldCreate = true) {
29662967
return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
29672968
getCanonicalMDString(Context, LinkageName), File, Line, Type,
29682969
IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration,
2969-
cast_or_null<Metadata>(TemplateParams), AlignInBits, Storage,
2970-
ShouldCreate);
2970+
cast_or_null<Metadata>(TemplateParams), AlignInBits,
2971+
Annotations.get(), Storage, ShouldCreate);
29712972
}
29722973
static DIGlobalVariable *
29732974
getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
29742975
MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
29752976
bool IsLocalToUnit, bool IsDefinition,
29762977
Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams,
2977-
uint32_t AlignInBits, StorageType Storage, bool ShouldCreate = true);
2978+
uint32_t AlignInBits, Metadata *Annotations, StorageType Storage,
2979+
bool ShouldCreate = true);
29782980

29792981
TempDIGlobalVariable cloneImpl() const {
29802982
return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
29812983
getFile(), getLine(), getType(), isLocalToUnit(),
29822984
isDefinition(), getStaticDataMemberDeclaration(),
2983-
getTemplateParams(), getAlignInBits());
2985+
getTemplateParams(), getAlignInBits(),
2986+
getAnnotations());
29842987
}
29852988

29862989
public:
@@ -2989,19 +2992,21 @@ class DIGlobalVariable : public DIVariable {
29892992
DIFile *File, unsigned Line, DIType *Type,
29902993
bool IsLocalToUnit, bool IsDefinition,
29912994
DIDerivedType *StaticDataMemberDeclaration,
2992-
MDTuple *TemplateParams, uint32_t AlignInBits),
2995+
MDTuple *TemplateParams, uint32_t AlignInBits,
2996+
DINodeArray Annotations),
29932997
(Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
29942998
IsDefinition, StaticDataMemberDeclaration, TemplateParams,
2995-
AlignInBits))
2999+
AlignInBits, Annotations))
29963000
DEFINE_MDNODE_GET(DIGlobalVariable,
29973001
(Metadata * Scope, MDString *Name, MDString *LinkageName,
29983002
Metadata *File, unsigned Line, Metadata *Type,
29993003
bool IsLocalToUnit, bool IsDefinition,
30003004
Metadata *StaticDataMemberDeclaration,
3001-
Metadata *TemplateParams, uint32_t AlignInBits),
3005+
Metadata *TemplateParams, uint32_t AlignInBits,
3006+
Metadata *Annotations),
30023007
(Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
30033008
IsDefinition, StaticDataMemberDeclaration, TemplateParams,
3004-
AlignInBits))
3009+
AlignInBits, Annotations))
30053010

30063011
TempDIGlobalVariable clone() const { return cloneImpl(); }
30073012

@@ -3012,11 +3017,15 @@ class DIGlobalVariable : public DIVariable {
30123017
DIDerivedType *getStaticDataMemberDeclaration() const {
30133018
return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
30143019
}
3020+
DINodeArray getAnnotations() const {
3021+
return cast_or_null<MDTuple>(getRawAnnotations());
3022+
}
30153023

30163024
MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
30173025
Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(6); }
30183026
Metadata *getRawTemplateParams() const { return getOperand(7); }
30193027
MDTuple *getTemplateParams() const { return getOperandAs<MDTuple>(7); }
3028+
Metadata *getRawAnnotations() const { return getOperand(8); }
30203029

30213030
static bool classof(const Metadata *MD) {
30223031
return MD->getMetadataID() == DIGlobalVariableKind;

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4946,15 +4946,17 @@ bool LLParser::parseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
49464946
OPTIONAL(isDefinition, MDBoolField, (true)); \
49474947
OPTIONAL(templateParams, MDField, ); \
49484948
OPTIONAL(declaration, MDField, ); \
4949-
OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));
4949+
OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
4950+
OPTIONAL(annotations, MDField, );
49504951
PARSE_MD_FIELDS();
49514952
#undef VISIT_MD_FIELDS
49524953

49534954
Result =
49544955
GET_OR_DISTINCT(DIGlobalVariable,
49554956
(Context, scope.Val, name.Val, linkageName.Val, file.Val,
49564957
line.Val, type.Val, isLocal.Val, isDefinition.Val,
4957-
declaration.Val, templateParams.Val, align.Val));
4958+
declaration.Val, templateParams.Val, align.Val,
4959+
annotations.Val));
49584960
return false;
49594961
}
49604962

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,13 +1873,18 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
18731873
unsigned Version = Record[0] >> 1;
18741874

18751875
if (Version == 2) {
1876+
Metadata *Annotations = nullptr;
1877+
if (Record.size() > 12)
1878+
Annotations = getMDOrNull(Record[12]);
1879+
18761880
MetadataList.assignValue(
18771881
GET_OR_DISTINCT(
18781882
DIGlobalVariable,
18791883
(Context, getMDOrNull(Record[1]), getMDString(Record[2]),
18801884
getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
18811885
getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1882-
getMDOrNull(Record[9]), getMDOrNull(Record[10]), Record[11])),
1886+
getMDOrNull(Record[9]), getMDOrNull(Record[10]), Record[11],
1887+
Annotations)),
18831888
NextMetadataNo);
18841889

18851890
NextMetadataNo++;
@@ -1892,7 +1897,8 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
18921897
getMDString(Record[2]), getMDString(Record[3]),
18931898
getMDOrNull(Record[4]), Record[5],
18941899
getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1895-
getMDOrNull(Record[10]), nullptr, Record[11])),
1900+
getMDOrNull(Record[10]), nullptr, Record[11],
1901+
nullptr)),
18961902
NextMetadataNo);
18971903

18981904
NextMetadataNo++;
@@ -1925,7 +1931,7 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
19251931
(Context, getMDOrNull(Record[1]), getMDString(Record[2]),
19261932
getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
19271933
getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1928-
getMDOrNull(Record[10]), nullptr, AlignInBits));
1934+
getMDOrNull(Record[10]), nullptr, AlignInBits, nullptr));
19291935

19301936
DIGlobalVariableExpression *DGVE = nullptr;
19311937
if (Attach || Expr)

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,7 @@ void ModuleBitcodeWriter::writeDIGlobalVariable(
19621962
Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
19631963
Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams()));
19641964
Record.push_back(N->getAlignInBits());
1965+
Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
19651966

19661967
Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
19671968
Record.clear();

llvm/lib/IR/AsmWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,6 +2299,7 @@ static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
22992299
Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
23002300
Printer.printMetadata("templateParams", N->getRawTemplateParams());
23012301
Printer.printInt("align", N->getAlignInBits());
2302+
Printer.printMetadata("annotations", N->getRawAnnotations());
23022303
Out << ")";
23032304
}
23042305

llvm/lib/IR/DIBuilder.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,13 +705,14 @@ DIGlobalVariableExpression *DIBuilder::createGlobalVariableExpression(
705705
DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *F,
706706
unsigned LineNumber, DIType *Ty, bool IsLocalToUnit,
707707
bool isDefined, DIExpression *Expr,
708-
MDNode *Decl, MDTuple *TemplateParams, uint32_t AlignInBits) {
708+
MDNode *Decl, MDTuple *TemplateParams, uint32_t AlignInBits,
709+
DINodeArray Annotations) {
709710
checkGlobalVariableScope(Context);
710711

711712
auto *GV = DIGlobalVariable::getDistinct(
712713
VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
713714
LineNumber, Ty, IsLocalToUnit, isDefined, cast_or_null<DIDerivedType>(Decl),
714-
TemplateParams, AlignInBits);
715+
TemplateParams, AlignInBits, Annotations);
715716
if (!Expr)
716717
Expr = createExpression();
717718
auto *N = DIGlobalVariableExpression::get(VMContext, GV, Expr);
@@ -728,7 +729,8 @@ DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
728729
return DIGlobalVariable::getTemporary(
729730
VMContext, cast_or_null<DIScope>(Context), Name, LinkageName, F,
730731
LineNumber, Ty, IsLocalToUnit, false,
731-
cast_or_null<DIDerivedType>(Decl), TemplateParams, AlignInBits)
732+
cast_or_null<DIDerivedType>(Decl), TemplateParams, AlignInBits,
733+
nullptr)
732734
.release();
733735
}
734736

llvm/lib/IR/DebugInfoMetadata.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,21 +984,24 @@ DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
984984
Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
985985
Metadata *StaticDataMemberDeclaration,
986986
Metadata *TemplateParams, uint32_t AlignInBits,
987-
StorageType Storage, bool ShouldCreate) {
987+
Metadata *Annotations, StorageType Storage,
988+
bool ShouldCreate) {
988989
assert(isCanonical(Name) && "Expected canonical MDString");
989990
assert(isCanonical(LinkageName) && "Expected canonical MDString");
990991
DEFINE_GETIMPL_LOOKUP(DIGlobalVariable, (Scope, Name, LinkageName, File, Line,
991992
Type, IsLocalToUnit, IsDefinition,
992993
StaticDataMemberDeclaration,
993-
TemplateParams, AlignInBits));
994+
TemplateParams, AlignInBits,
995+
Annotations));
994996
Metadata *Ops[] = {Scope,
995997
Name,
996998
File,
997999
Type,
9981000
Name,
9991001
LinkageName,
10001002
StaticDataMemberDeclaration,
1001-
TemplateParams};
1003+
TemplateParams,
1004+
Annotations};
10021005
DEFINE_GETIMPL_STORE(DIGlobalVariable,
10031006
(Line, IsLocalToUnit, IsDefinition, AlignInBits), Ops);
10041007
}

llvm/lib/IR/LLVMContextImpl.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -984,25 +984,27 @@ template <> struct MDNodeKeyImpl<DIGlobalVariable> {
984984
Metadata *StaticDataMemberDeclaration;
985985
Metadata *TemplateParams;
986986
uint32_t AlignInBits;
987+
Metadata *Annotations;
987988

988989
MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
989990
Metadata *File, unsigned Line, Metadata *Type,
990991
bool IsLocalToUnit, bool IsDefinition,
991992
Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams,
992-
uint32_t AlignInBits)
993+
uint32_t AlignInBits, Metadata *Annotations)
993994
: Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
994995
Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
995996
IsDefinition(IsDefinition),
996997
StaticDataMemberDeclaration(StaticDataMemberDeclaration),
997-
TemplateParams(TemplateParams), AlignInBits(AlignInBits) {}
998+
TemplateParams(TemplateParams), AlignInBits(AlignInBits),
999+
Annotations(Annotations) {}
9981000
MDNodeKeyImpl(const DIGlobalVariable *N)
9991001
: Scope(N->getRawScope()), Name(N->getRawName()),
10001002
LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
10011003
Line(N->getLine()), Type(N->getRawType()),
10021004
IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
10031005
StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()),
10041006
TemplateParams(N->getRawTemplateParams()),
1005-
AlignInBits(N->getAlignInBits()) {}
1007+
AlignInBits(N->getAlignInBits()), Annotations(N->getRawAnnotations()) {}
10061008

10071009
bool isKeyOf(const DIGlobalVariable *RHS) const {
10081010
return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
@@ -1013,7 +1015,8 @@ template <> struct MDNodeKeyImpl<DIGlobalVariable> {
10131015
StaticDataMemberDeclaration ==
10141016
RHS->getRawStaticDataMemberDeclaration() &&
10151017
TemplateParams == RHS->getRawTemplateParams() &&
1016-
AlignInBits == RHS->getAlignInBits();
1018+
AlignInBits == RHS->getAlignInBits() &&
1019+
Annotations == RHS->getRawAnnotations();
10171020
}
10181021

10191022
unsigned getHashValue() const {
@@ -1026,7 +1029,7 @@ template <> struct MDNodeKeyImpl<DIGlobalVariable> {
10261029
// TODO: make hashing work fine with such situations
10271030
return hash_combine(Scope, Name, LinkageName, File, Line, Type,
10281031
IsLocalToUnit, IsDefinition, /* AlignInBits, */
1029-
StaticDataMemberDeclaration);
1032+
StaticDataMemberDeclaration, Annotations);
10301033
}
10311034
};
10321035

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; REQUIRES: x86-registered-target
2+
; RUN: llvm-as < %s | llvm-dis | FileCheck %s
3+
4+
%struct.t1 = type { i32 }
5+
6+
@g1 = dso_local global %struct.t1 zeroinitializer, align 4, !dbg !0
7+
8+
!llvm.dbg.cu = !{!2}
9+
!llvm.module.flags = !{!13, !14, !15, !16, !17}
10+
!llvm.ident = !{!18}
11+
12+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
13+
!1 = distinct !DIGlobalVariable(name: "g1", scope: !2, file: !3, line: 7, type: !6, isLocal: false, isDefinition: true, annotations: !10)
14+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 13.0.0 (https://github.com/llvm/llvm-project.git 47af5574a87dc298b5c6c36ff6a969c8c77c8499)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
15+
!3 = !DIFile(filename: "t.c", directory: "/home/yhs/work/tests/llvm/btf_tag")
16+
!4 = !{}
17+
!5 = !{!0}
18+
!6 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "t1", file: !3, line: 4, size: 32, elements: !7)
19+
!7 = !{!8}
20+
!8 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !6, file: !3, line: 5, baseType: !9, size: 32)
21+
!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
22+
!10 = !{!11, !12}
23+
!11 = !{!"btf_tag", !"tag1"}
24+
!12 = !{!"btf_tag", !"tag2"}
25+
26+
; CHECK: distinct !DIGlobalVariable(name: "g1"
27+
; CHECK-SAME: annotations: ![[ANNOT:[0-9]+]]
28+
; CHECK: ![[ANNOT]] = !{![[TAG1:[0-9]+]], ![[TAG2:[0-9]+]]}
29+
; CHECK: ![[TAG1]] = !{!"btf_tag", !"tag1"}
30+
; CHECK: ![[TAG2]] = !{!"btf_tag", !"tag2"}
31+
32+
!13 = !{i32 7, !"Dwarf Version", i32 4}
33+
!14 = !{i32 2, !"Debug Info Version", i32 3}
34+
!15 = !{i32 1, !"wchar_size", i32 4}
35+
!16 = !{i32 7, !"uwtable", i32 1}
36+
!17 = !{i32 7, !"frame-pointer", i32 2}
37+
!18 = !{!"clang version 13.0.0 (https://github.com/llvm/llvm-project.git 47af5574a87dc298b5c6c36ff6a969c8c77c8499)"}

0 commit comments

Comments
 (0)