diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h --- a/clang/lib/CodeGen/CGDebugInfo.h +++ b/clang/lib/CodeGen/CGDebugInfo.h @@ -170,6 +170,8 @@ /// The key is coroutine real parameters, value is DIVariable in LLVM IR. Param2DILocTy ParamDbgMappings; + std::vector AnnotationPlaceholders; + /// Helper functions for getOrCreateType. /// @{ /// Currently the checksum of an interface includes the number of @@ -217,6 +219,7 @@ llvm::DIType *CreateType(const MemberPointerType *Ty, llvm::DIFile *F); llvm::DIType *CreateType(const AtomicType *Ty, llvm::DIFile *F); llvm::DIType *CreateType(const PipeType *Ty, llvm::DIFile *F); + llvm::DIType *CreateType(const BTFTagAttributedType *Ty, llvm::DIFile *F); /// Get enumeration type. llvm::DIType *CreateEnumType(const EnumType *Ty); llvm::DIType *CreateTypeDefinition(const EnumType *Ty); diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -1175,6 +1175,129 @@ return RetTy; } +static QualType collectBTFTypeTagAnnotations( + llvm::LLVMContext &Context, llvm::DIBuilder &DBuilder, + llvm::SmallVectorImpl &Annots, + const BTFTagAttributedType *BTFAttrTy, const char *TagName) { + QualType WrappedTy; + + do { + StringRef TagValue = BTFAttrTy->getAttr()->getBTFTypeTag(); + if (!TagValue.empty()) { + llvm::Metadata *Ops[] = { + llvm::MDString::get(Context, TagName), + llvm::MDString::get(Context, TagValue), + }; + Annots.insert(Annots.begin(), llvm::MDNode::get(Context, Ops)); + } + WrappedTy = BTFAttrTy->getWrappedType(); + BTFAttrTy = dyn_cast(WrappedTy); + } while (BTFAttrTy); + + return WrappedTy; +} + +static bool retreiveCVR(llvm::DIDerivedType *DTy, QualifierCollector &Qc) { + switch (DTy->getTag()) { + case llvm::dwarf::DW_TAG_const_type: + Qc.addConst(); + return true; + case llvm::dwarf::DW_TAG_volatile_type: + Qc.addVolatile(); + return true; + case llvm::dwarf::DW_TAG_restrict_type: + Qc.addRestrict(); + return true; + default: + return false; + } +} + +// Tags returned by QualifierCollector::getNextQualifier() should be +// applied in the reverse order, thus use recursive function. +static llvm::DIType *applyQualifiers(llvm::DIBuilder &DBuilder, + llvm::DIType *Ty, QualifierCollector &Qc) { + llvm::dwarf::Tag Tag = getNextQualifier(Qc); + if (!Tag) + return Ty; + Ty = applyQualifiers(DBuilder, Ty, Qc); + return DBuilder.createQualifiedType(Tag, Ty); +} + +static bool isAnnotationsPlaceholder(llvm::DIDerivedType *DTy) { + return DTy->isTemporary() && + DTy->getTag() == llvm::dwarf::DW_TAG_LLVM_annotation; +} + +llvm::DIType *CGDebugInfo::CreateType(const BTFTagAttributedType *Ty, + llvm::DIFile *Unit) { + SmallVector Annotations; + auto WrappedTy = collectBTFTypeTagAnnotations( + CGM.getLLVMContext(), DBuilder, Annotations, Ty, "btf:type_tag"); + + // After discussion with GCC BPF team in [1] it was decided to avoid + // attaching BTF type tags to const/volatile/restrict DWARF DIEs. + // So, strip qualifiers from WrappedTy and apply those to a final + // annotations placeholder instance at the end of this function. + // + // [1] https://reviews.llvm.org/D143967 + QualifierCollector Qc; + Qc.addCVRQualifiers(WrappedTy.getLocalCVRQualifiers()); + WrappedTy.removeLocalFastQualifiers(Qualifiers::CVRMask); + + llvm::DIType *WrappedDI = getOrCreateType(WrappedTy, Unit); + if (Annotations.size() == 0) + return WrappedDI; + + if (WrappedDI == nullptr) + WrappedDI = DBuilder.createUnspecifiedType("void"); + + // Stripping local CVR qualifiers might not be enough in cases like this: + // + // #define __tag __attribute__((btf_type_tag("tag"))) + // const int *foo; + // const int *bar(void) { + // return (typeof(*foo) __tag *)(0); + // } + // + // Here the AST looks like: + // + // BTFTagAttributedType + // | 'typeof (*foo) __attribute__((btf_type_tag("tag")))' sugar + // `-TypeOfExprType 'typeof (*foo)' sugar + // |-ParenExpr 'const int' lvalue + // | `- ... + // `-QualType 'const int' const + // `-BuiltinType 'int' + // + // The BTFTagAttributedType is applied to TypeOfExpr. + // For TypeOfExpr the getOrCreateType(), would return instance of + // DIDerivedType with tag DW_TAG_const_type. + // + // To avoid repeating UnwrapTypeForDebugInfo() logic here just + // rebuild CVR metadata nodes if necessary. + // The above local CVR qualifiers processing is redundant, + // but avoids rebuilding metadata nodes in the most common case. + while (auto *DTy = dyn_cast(WrappedDI)) { + if (!retreiveCVR(DTy, Qc)) + break; + WrappedDI = DTy->getBaseType(); + } + + if (auto *DTy = dyn_cast(WrappedDI)) + if (isAnnotationsPlaceholder(DTy)) { + WrappedDI = DTy->getBaseType(); + for (llvm::Metadata *O : DTy->getAnnotations()->operands()) + Annotations.push_back(O); + } + + auto *Placeholder = DBuilder.createAnnotationsPlaceholder( + WrappedDI, DBuilder.getOrCreateArray(Annotations)); + AnnotationPlaceholders.push_back(Placeholder); + + return applyQualifiers(DBuilder, Placeholder, Qc); +} + llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty, QualType PointeeTy, @@ -1187,32 +1310,23 @@ CGM.getTarget().getDWARFAddressSpace( CGM.getTypes().getTargetAddressSpace(PointeeTy)); - SmallVector Annots; - auto *BTFAttrTy = dyn_cast(PointeeTy); - while (BTFAttrTy) { - StringRef Tag = BTFAttrTy->getAttr()->getBTFTypeTag(); - if (!Tag.empty()) { - llvm::Metadata *Ops[2] = { - llvm::MDString::get(CGM.getLLVMContext(), StringRef("btf_type_tag")), - llvm::MDString::get(CGM.getLLVMContext(), Tag)}; - Annots.insert(Annots.begin(), - llvm::MDNode::get(CGM.getLLVMContext(), Ops)); - } - BTFAttrTy = dyn_cast(BTFAttrTy->getWrappedType()); - } - llvm::DINodeArray Annotations = nullptr; - if (Annots.size() > 0) - Annotations = DBuilder.getOrCreateArray(Annots); + if (auto *BTFAttrTy = + dyn_cast(PointeeTy.getTypePtr())) { + SmallVector AnnotationsVec; + collectBTFTypeTagAnnotations(CGM.getLLVMContext(), DBuilder, AnnotationsVec, + BTFAttrTy, "btf_type_tag"); + Annotations = DBuilder.getOrCreateArray(AnnotationsVec); + } if (Tag == llvm::dwarf::DW_TAG_reference_type || Tag == llvm::dwarf::DW_TAG_rvalue_reference_type) return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit), Size, Align, DWARFAddressSpace); - else - return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size, - Align, DWARFAddressSpace, StringRef(), - Annotations); + + return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size, + Align, DWARFAddressSpace, StringRef(), + Annotations); } llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name, @@ -3447,9 +3561,6 @@ case Type::Attributed: T = cast(T)->getEquivalentType(); break; - case Type::BTFTagAttributed: - T = cast(T)->getWrappedType(); - break; case Type::Elaborated: T = cast(T)->getNamedType(); break; @@ -3641,9 +3752,11 @@ case Type::TemplateSpecialization: return CreateType(cast(Ty), Unit); + case Type::BTFTagAttributed: + return CreateType(cast(Ty), Unit); + case Type::Auto: case Type::Attributed: - case Type::BTFTagAttributed: case Type::Adjusted: case Type::Decayed: case Type::DeducedTemplateSpecialization: @@ -5781,6 +5894,35 @@ TheCU->setDWOId(Signature); } +static llvm::DIType *copyAnnotations(llvm::DIBuilder &DBuilder, + llvm::DIDerivedType *Placeholder) { + auto *WrappedDI = Placeholder->getBaseType(); + SmallVector Annotations; + + for (const llvm::Metadata *O : Placeholder->getAnnotations()->operands()) + Annotations.push_back(const_cast(O)); + + auto AddAnnotations = [&](auto *Type) { + if (llvm::DINodeArray OldAnnotations = Type->getAnnotations()) + for (const llvm::Metadata *O : OldAnnotations->operands()) + Annotations.push_back(const_cast(O)); + auto Clone = Type->clone(); + Clone->replaceAnnotations(DBuilder.getOrCreateArray(Annotations)); + return llvm::MDNode::replaceWithPermanent(std::move(Clone)); + }; + + if (auto *Ty = dyn_cast(WrappedDI)) + return AddAnnotations(Ty); + if (auto *Ty = dyn_cast(WrappedDI)) + return AddAnnotations(Ty); + if (auto *Ty = dyn_cast(WrappedDI)) + return AddAnnotations(Ty); + if (auto *Ty = dyn_cast(WrappedDI)) + return AddAnnotations(Ty); + + return WrappedDI; +} + void CGDebugInfo::finalize() { // Creating types might create further types - invalidating the current // element and the size(), so don't cache/reference them. @@ -5854,6 +5996,10 @@ if (auto MD = TypeCache[RT]) DBuilder.retainType(cast(MD)); + for (auto &Placeholder : AnnotationPlaceholders) + DBuilder.replaceTemporary(llvm::TempDIType(Placeholder), + copyAnnotations(DBuilder, Placeholder)); + DBuilder.finalize(); } diff --git a/clang/test/CodeGen/attr-btf_type_tag-circular.c b/clang/test/CodeGen/attr-btf_type_tag-circular.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/attr-btf_type_tag-circular.c @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s + +#define __tag1 __attribute__((btf_type_tag("tag1"))) + +struct st { + struct st __tag1 *self; +} g; + +// CHECK: distinct !DIGlobalVariable(name: "g", scope: ![[#]], file: ![[#]], line: [[#]], type: ![[L1:[0-9]+]], isLocal: false, isDefinition: true) +// CHECK: ![[L1]] = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "st", file: ![[#]], line: [[#]], size: [[#]], elements: ![[L2:[0-9]+]]) +// CHECK: ![[L2]] = !{![[L3:[0-9]+]]} +// CHECK: ![[L3]] = !DIDerivedType(tag: DW_TAG_member, name: "self", scope: ![[L1]], file: ![[#]], line: [[#]], baseType: ![[L4:[0-9]+]], size: [[#]]) +// CHECK: ![[L4]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L5:[0-9]+]], size: [[#]], annotations: ![[L6:[0-9]+]]) +// CHECK: ![[L5]] = !DICompositeType(tag: DW_TAG_structure_type, name: "st", file: ![[#]], line: [[#]], size: [[#]], elements: ![[L2]], annotations: ![[L7:[0-9]+]]) +// CHECK: ![[L7]] = !{![[L8:[0-9]+]]} +// CHECK: ![[L8]] = !{!"btf:type_tag", !"tag1"} +// CHECK: ![[L6]] = !{![[L9:[0-9]+]]} +// CHECK: ![[L9]] = !{!"btf_type_tag", !"tag1"} diff --git a/clang/test/CodeGen/attr-btf_type_tag-const.c b/clang/test/CodeGen/attr-btf_type_tag-const.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/attr-btf_type_tag-const.c @@ -0,0 +1,43 @@ +// RUN: %clang_cc1 \ +// RUN: -triple %itanium_abi_triple -debug-info-kind=limited \ +// RUN: -S -emit-llvm -o - %s | FileCheck %s + +// Check that BTF type tags are not attached to DW_TAG_const_type DIEs +// in presence of "sugar" expressions that are transparent for +// CGDebugInfo.cpp:UnwrapTypeForDebugInfo(), but are not transparent +// for local qualifiers. +// +// For details see: +// CGDebugInfo::CreateType(const BTFTagAttributedType, llvm::DIFile) + +#define __tag1 __attribute__((btf_type_tag("tag1"))) +#define __tag2 __attribute__((btf_type_tag("tag2"))) +#define __tag3 __attribute__((btf_type_tag("tag3"))) + +const int *foo; +typeof(*foo) __tag1 bar; + +// CHECK: distinct !DIGlobalVariable(name: "bar", {{.*}}, type: ![[L01:[0-9]+]], {{.*}}) +// CHECK: ![[L01]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[L02:[0-9]+]]) +// CHECK: ![[L02]] = !DIBasicType(name: "int", {{.*}}, annotations: ![[L03:[0-9]+]]) +// CHECK: ![[L03]] = !{![[L04:[0-9]+]]} +// CHECK: ![[L04]] = !{!"btf:type_tag", !"tag1"} + +const int __tag2 *buz; + +// CHECK: distinct !DIGlobalVariable(name: "buz", {{.*}}, type: ![[L05:[0-9]+]], {{.*}}) +// CHECK: ![[L05]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L06:[0-9]+]], {{.*}}, annotations: ![[L07:[0-9]+]]) +// CHECK: ![[L06]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[L08:[0-9]+]]) +// CHECK: ![[L08]] = !DIBasicType(name: "int", size: [[#]], {{.*}}, annotations: ![[L09:[0-9]+]]) +// CHECK: ![[L09]] = !{![[L10:[0-9]+]]} +// CHECK: ![[L10]] = !{!"btf:type_tag", !"tag2"} +// CHECK: ![[L07]] = !{![[L11:[0-9]+]]} +// CHECK: ![[L11]] = !{!"btf_type_tag", !"tag2"} + +typeof(*buz) __tag3 quux; + +// CHECK: distinct !DIGlobalVariable(name: "quux", {{.*}}, type: ![[L12:[0-9]+]], {{.*}}) +// CHECK: ![[L12]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[L13:[0-9]+]]) +// CHECK: ![[L13]] = !DIBasicType(name: "int", {{.*}}, annotations: ![[L14:[0-9]+]]) +// CHECK: ![[L14]] = !{![[L15:[0-9]+]], ![[L10]]} +// CHECK: ![[L15]] = !{!"btf:type_tag", !"tag3"} diff --git a/clang/test/CodeGen/attr-btf_type_tag-func-ptr.c b/clang/test/CodeGen/attr-btf_type_tag-func-ptr.c --- a/clang/test/CodeGen/attr-btf_type_tag-func-ptr.c +++ b/clang/test/CodeGen/attr-btf_type_tag-func-ptr.c @@ -8,8 +8,10 @@ return arg->a; } -// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "f" -// CHECK-SAME: baseType: ![[L18:[0-9]+]] -// CHECK: ![[L18]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: [[#]], annotations: ![[L21:[0-9]+]]) -// CHECK: ![[L21]] = !{![[L22:[0-9]+]]} -// CHECK: ![[L22]] = !{!"btf_type_tag", !"rcu"} +// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "f", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L1:[0-9]+]], size: [[#]]) +// CHECK: ![[L1]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L2:[0-9]+]], size: [[#]], annotations: ![[L3:[0-9]+]]) +// CHECK: ![[L2]] = !DISubroutineType(types: ![[#]], annotations: ![[L4:[0-9]+]]) +// CHECK: ![[L4]] = !{![[L5:[0-9]+]]} +// CHECK: ![[L5]] = !{!"btf:type_tag", !"rcu"} +// CHECK: ![[L3]] = !{![[L6:[0-9]+]]} +// CHECK: ![[L6]] = !{!"btf_type_tag", !"rcu"} diff --git a/clang/test/CodeGen/attr-btf_type_tag-func.c b/clang/test/CodeGen/attr-btf_type_tag-func.c --- a/clang/test/CodeGen/attr-btf_type_tag-func.c +++ b/clang/test/CodeGen/attr-btf_type_tag-func.c @@ -15,14 +15,17 @@ int __tag1 * __tag2 *foo(int __tag1 * __tag2 *arg) { return arg; } -// CHECK: distinct !DISubprogram(name: "foo", scope: ![[#]], file: ![[#]], line: [[#]], type: ![[L9:[0-9]+]] -// CHECK: ![[L9]] = !DISubroutineType(types: ![[L10:[0-9]+]] -// CHECK: ![[L10]] = !{![[L11:[0-9]+]], ![[L11]]} -// CHECK: ![[L11]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L12:[0-9]+]], size: [[#]], annotations: ![[L16:[0-9]+]] -// CHECK: ![[L12]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L13:[0-9]+]], size: [[#]], annotations: ![[L14:[0-9]+]] -// CHECK: ![[L13]] = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed -// CHECK: ![[L14]] = !{![[L15:[0-9]+]]} -// CHECK: ![[L15]] = !{!"btf_type_tag", !"tag1"} -// CHECK: ![[L16]] = !{![[L17:[0-9]+]]} -// CHECK: ![[L17]] = !{!"btf_type_tag", !"tag2"} -// CHECK: !DILocalVariable(name: "arg", arg: 1, scope: ![[#]], file: ![[#]], line: [[#]], type: ![[L11]]) +// CHECK: distinct !DISubprogram(name: "foo", scope: ![[#]], file: ![[#]], line: [[#]], type: ![[L01:[0-9]+]], {{.*}}) +// CHECK: ![[L01]] = !DISubroutineType(types: ![[L02:[0-9]+]]) +// CHECK: ![[L02]] = !{![[L03:[0-9]+]], ![[L03]]} +// CHECK: ![[L03]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L04:[0-9]+]], size: [[#]], annotations: ![[L05:[0-9]+]]) +// CHECK: ![[L04]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L06:[0-9]+]], size: [[#]], annotations: ![[L07:[0-9]+]]) +// CHECK: ![[L06]] = !DIBasicType(name: "int", size: [[#]], encoding: DW_ATE_signed, annotations: ![[L08:[0-9]+]]) +// CHECK: ![[L08]] = !{![[L09:[0-9]+]]} +// CHECK: ![[L09]] = !{!"btf:type_tag", !"tag1"} +// CHECK: ![[L07]] = !{![[L10:[0-9]+]], ![[L11:[0-9]+]]} +// CHECK: ![[L10]] = !{!"btf:type_tag", !"tag2"} +// CHECK: ![[L11]] = !{!"btf_type_tag", !"tag1"} +// CHECK: ![[L05]] = !{![[L12:[0-9]+]]} +// CHECK: ![[L12]] = !{!"btf_type_tag", !"tag2"} +// CHECK: !DILocalVariable(name: "arg", arg: 1, scope: ![[#]], file: ![[#]], line: [[#]], type: ![[L03]]) diff --git a/clang/test/CodeGen/attr-btf_type_tag-restrict.c b/clang/test/CodeGen/attr-btf_type_tag-restrict.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/attr-btf_type_tag-restrict.c @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 \ +// RUN: -triple %itanium_abi_triple -debug-info-kind=limited \ +// RUN: -S -emit-llvm -o - %s | FileCheck %s + +// See attr-btf_type_tag-const.c for reasoning behind this test. +// Alternatively, see the following method: +// CGDebugInfo::CreateType(const BTFTagAttributedType, llvm::DIFile) + +#define __tag1 __attribute__((btf_type_tag("tag1"))) + +void foo(int * restrict bar, typeof(bar) __tag1 buz) {} + +// CHECK: ![[#]] = !DISubroutineType(types: ![[L1:[0-9]+]]) +// CHECK: ![[L1]] = !{null, ![[L2:[0-9]+]], ![[L3:[0-9]+]]} +// CHECK: ![[L2]] = !DIDerivedType(tag: DW_TAG_restrict_type, baseType: ![[L4:[0-9]+]]) +// CHECK: ![[L4]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L5:[0-9]+]], {{.*}}) +// CHECK: ![[L5]] = !DIBasicType(name: "int", {{.*}}, encoding: DW_ATE_signed) +// CHECK: ![[L3]] = !DIDerivedType(tag: DW_TAG_restrict_type, baseType: ![[L6:[0-9]+]]) +// CHECK: ![[L6]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L5]], {{.*}}, annotations: ![[L7:[0-9]+]]) +// CHECK: ![[L7]] = !{![[L8:[0-9]+]]} +// CHECK: ![[L8]] = !{!"btf:type_tag", !"tag1"} diff --git a/clang/test/CodeGen/attr-btf_type_tag-similar-type.c b/clang/test/CodeGen/attr-btf_type_tag-similar-type.c --- a/clang/test/CodeGen/attr-btf_type_tag-similar-type.c +++ b/clang/test/CodeGen/attr-btf_type_tag-similar-type.c @@ -12,15 +12,23 @@ return *arg->a; } -// CHECK: distinct !DICompositeType(tag: DW_TAG_structure_type, name: "map_value", file: ![[#]], line: [[#]], size: [[#]], elements: ![[L14:[0-9]+]] -// CHECK: ![[L14]] = !{![[L15:[0-9]+]], ![[L20:[0-9]+]]} -// CHECK: ![[L15]] = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L16:[0-9]+]] -// CHECK: ![[L16]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: [[#]], annotations: ![[L17:[0-9]+]] -// CHECK: ![[L17]] = !{![[L18:[0-9]+]], ![[L19:[0-9]+]]} -// CHECK: ![[L18]] = !{!"btf_type_tag", !"tag1"} -// CHECK: ![[L19]] = !{!"btf_type_tag", !"tag3"} -// CHECK: ![[L20]] = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L21:[0-9]+]] -// CHECK: ![[L21:[0-9]+]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: [[#]], annotations: ![[L22:[0-9]+]] -// CHECK: ![[L22]] = !{![[L23:[0-9]+]], ![[L24:[0-9]+]]} -// CHECK: ![[L23]] = !{!"btf_type_tag", !"tag2"} -// CHECK: ![[L24]] = !{!"btf_type_tag", !"tag4"} +// CHECK: distinct !DICompositeType(tag: DW_TAG_structure_type, name: "map_value", file: ![[#]], line: [[#]], size: [[#]], elements: ![[L01:[0-9]+]]) +// CHECK: ![[L01]] = !{![[L02:[0-9]+]], ![[L03:[0-9]+]]} +// CHECK: ![[L02]] = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L04:[0-9]+]], size: [[#]]) +// CHECK: ![[L04]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L05:[0-9]+]], size: [[#]], annotations: ![[L06:[0-9]+]]) +// CHECK: ![[L05]] = !DIBasicType(name: "int", size: [[#]], encoding: DW_ATE_signed, annotations: ![[L07:[0-9]+]]) +// CHECK: ![[L07]] = !{![[L08:[0-9]+]], ![[L09:[0-9]+]]} +// CHECK: ![[L08]] = !{!"btf:type_tag", !"tag1"} +// CHECK: ![[L09]] = !{!"btf:type_tag", !"tag3"} +// CHECK: ![[L06]] = !{![[L10:[0-9]+]], ![[L11:[0-9]+]]} +// CHECK: ![[L10]] = !{!"btf_type_tag", !"tag1"} +// CHECK: ![[L11]] = !{!"btf_type_tag", !"tag3"} +// CHECK: ![[L03]] = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L12:[0-9]+]], size: [[#]], offset: [[#]]) +// CHECK: ![[L12]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L13:[0-9]+]], size: [[#]], annotations: ![[L14:[0-9]+]]) +// CHECK: ![[L13]] = !DIBasicType(name: "int", size: [[#]], encoding: DW_ATE_signed, annotations: ![[L15:[0-9]+]]) +// CHECK: ![[L15]] = !{![[L16:[0-9]+]], ![[L17:[0-9]+]]} +// CHECK: ![[L16]] = !{!"btf:type_tag", !"tag2"} +// CHECK: ![[L17]] = !{!"btf:type_tag", !"tag4"} +// CHECK: ![[L14]] = !{![[L18:[0-9]+]], ![[L19:[0-9]+]]} +// CHECK: ![[L18]] = !{!"btf_type_tag", !"tag2"} +// CHECK: ![[L19]] = !{!"btf_type_tag", !"tag4"} diff --git a/clang/test/CodeGen/attr-btf_type_tag-typedef-field.c b/clang/test/CodeGen/attr-btf_type_tag-typedef-field.c --- a/clang/test/CodeGen/attr-btf_type_tag-typedef-field.c +++ b/clang/test/CodeGen/attr-btf_type_tag-typedef-field.c @@ -14,22 +14,27 @@ return (int *)a1->c; } -// CHECK: ![[L4:[0-9]+]] = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) -// CHECK: distinct !DICompositeType(tag: DW_TAG_structure_type, name: "t", file: ![[#]], line: [[#]], size: [[#]], elements: ![[L16:[0-9]+]]) -// CHECK: ![[L16]] = !{![[L17:[0-9]+]], ![[L24:[0-9]+]], ![[L31:[0-9]+]]} -// CHECK: ![[L17]] = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L18:[0-9]+]], size: [[#]]) -// CHECK: ![[L18]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L19:[0-9]+]], size: [[#]], annotations: ![[L22:[0-9]+]]) -// CHECK: ![[L19]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L4]], size: [[#]], annotations: ![[L20:[0-9]+]]) -// CHECK: ![[L20]] = !{![[L21:[0-9]+]]} -// CHECK: ![[L21]] = !{!"btf_type_tag", !"tag1"} -// CHECK: ![[L22]] = !{![[L23:[0-9]+]]} -// CHECK: ![[L23]] = !{!"btf_type_tag", !"tag2"} -// CHECK: ![[L24]] = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L25:[0-9]+]] -// CHECK: ![[L25]] = !DIDerivedType(tag: DW_TAG_typedef, name: "__fn2_t", file: ![[#]], line: [[#]], baseType: ![[L26:[0-9]+]]) -// CHECK: ![[L26]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L27:[0-9]+]], size: [[#]], annotations: ![[L30:[0-9]+]]) -// CHECK: ![[L27]] = !DIDerivedType(tag: DW_TAG_typedef, name: "__fn_t", file: ![[#]], line: [[#]], baseType: ![[L28:[0-9]+]]) -// CHECK: ![[L28]] = !DISubroutineType(types: ![[L29:[0-9]+]]) -// CHECK: ![[L29]] = !{null, ![[L4]]} -// CHECK: ![[L30]] = !{![[L21]], ![[L23]]} -// CHECK: ![[L31]] = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: ![[#]], file: ![[#]], line: [[#]]1, baseType: ![[L32:[0-9]+]] -// CHECK: ![[L32]] = !DIBasicType(name: "long", size: [[#]], encoding: DW_ATE_signed) +// CHECK: ![[L01:[0-9]+]] = !DIBasicType(name: "int", size: [[#]], encoding: DW_ATE_signed) +// CHECK: distinct !DICompositeType(tag: DW_TAG_structure_type, name: "t", file: ![[#]], line: [[#]], size: [[#]], elements: ![[L02:[0-9]+]]) +// CHECK: ![[L02]] = !{![[L03:[0-9]+]], ![[L04:[0-9]+]], ![[L05:[0-9]+]]} +// CHECK: ![[L03]] = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L06:[0-9]+]], size: [[#]]) +// CHECK: ![[L06]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L07:[0-9]+]], size: [[#]], annotations: ![[L08:[0-9]+]]) +// CHECK: ![[L07]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L09:[0-9]+]], size: [[#]], annotations: ![[L10:[0-9]+]]) +// CHECK: ![[L09]] = !DIBasicType(name: "int", size: [[#]], encoding: DW_ATE_signed, annotations: ![[L11:[0-9]+]]) +// CHECK: ![[L11]] = !{![[L12:[0-9]+]]} +// CHECK: ![[L12]] = !{!"btf:type_tag", !"tag1"} +// CHECK: ![[L10]] = !{![[L13:[0-9]+]], ![[L14:[0-9]+]]} +// CHECK: ![[L13]] = !{!"btf:type_tag", !"tag2"} +// CHECK: ![[L14]] = !{!"btf_type_tag", !"tag1"} +// CHECK: ![[L08]] = !{![[L15:[0-9]+]]} +// CHECK: ![[L15]] = !{!"btf_type_tag", !"tag2"} +// CHECK: ![[L04]] = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L16:[0-9]+]], size: [[#]], offset: [[#]]) +// CHECK: ![[L16]] = !DIDerivedType(tag: DW_TAG_typedef, name: "__fn2_t", file: ![[#]], line: [[#]], baseType: ![[L17:[0-9]+]]) +// CHECK: ![[L17]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L18:[0-9]+]], size: [[#]], annotations: ![[L19:[0-9]+]]) +// CHECK: ![[L18]] = !DIDerivedType(tag: DW_TAG_typedef, name: "__fn_t", file: ![[#]], line: [[#]], baseType: ![[L20:[0-9]+]], annotations: ![[L21:[0-9]+]]) +// CHECK: ![[L20]] = !DISubroutineType(types: ![[L22:[0-9]+]]) +// CHECK: ![[L22]] = !{null, ![[L01]]} +// CHECK: ![[L21]] = !{![[L12]], ![[L13]]} +// CHECK: ![[L19]] = !{![[L14]], ![[L15]]} +// CHECK: ![[L05]] = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: ![[#]], file: ![[#]], line: [[#]], baseType: ![[L23:[0-9]+]], size: [[#]], offset: [[#]]) +// CHECK: ![[L23]] = !DIBasicType(name: "long", size: [[#]], encoding: DW_ATE_signed) diff --git a/clang/test/CodeGen/attr-btf_type_tag-var.c b/clang/test/CodeGen/attr-btf_type_tag-var.c --- a/clang/test/CodeGen/attr-btf_type_tag-var.c +++ b/clang/test/CodeGen/attr-btf_type_tag-var.c @@ -21,23 +21,30 @@ const int __tag1 __tag2 volatile * const __tag3 __tag4 volatile * __tag5 __tag6 const volatile * g; #endif -// CHECK: distinct !DIGlobalVariable(name: "g", scope: ![[#]], file: ![[#]], line: [[#]], type: ![[L6:[0-9]+]] -// CHECK: ![[L6]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L7:[0-9]+]], size: [[#]], annotations: ![[L22:[0-9]+]] -// CHECK: ![[L7]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[L8:[0-9]+]] -// CHECK: ![[L8]] = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: ![[L9:[0-9]+]] -// CHECK: ![[L9]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L10:[0-9]+]], size: [[#]], annotations: ![[L19:[0-9]+]] -// CHECK: ![[L10]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[L11:[0-9]+]] -// CHECK: ![[L11]] = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: ![[L12:[0-9]+]] -// CHECK: ![[L12]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L13:[0-9]+]], size: [[#]], annotations: ![[L16:[0-9]+]] -// CHECK: ![[L13]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[L14:[0-9]+]] -// CHECK: ![[L14]] = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: ![[L15:[0-9]+]] -// CHECK: ![[L15]] = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed -// CHECK: ![[L16]] = !{![[L17:[0-9]+]], ![[L18:[0-9]+]]} -// CHECK: ![[L17]] = !{!"btf_type_tag", !"tag1"} -// CHECK: ![[L18]] = !{!"btf_type_tag", !"tag2"} -// CHECK: ![[L19]] = !{![[L20:[0-9]+]], ![[L21:[0-9]+]]} -// CHECK: ![[L20]] = !{!"btf_type_tag", !"tag3"} -// CHECK: ![[L21]] = !{!"btf_type_tag", !"tag4"} -// CHECK: ![[L22]] = !{![[L23:[0-9]+]], ![[L24:[0-9]+]]} -// CHECK: ![[L23]] = !{!"btf_type_tag", !"tag5"} -// CHECK: ![[L24]] = !{!"btf_type_tag", !"tag6"} +// CHECK: distinct !DIGlobalVariable(name: "g", scope: ![[#]], file: ![[#]], line: [[#]], type: ![[L01:[0-9]+]], isLocal: false, isDefinition: true) +// CHECK: ![[L01]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L02:[0-9]+]], size: [[#]], annotations: ![[L03:[0-9]+]]) +// CHECK: ![[L02]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[L04:[0-9]+]]) +// CHECK: ![[L04]] = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: ![[L05:[0-9]+]]) +// CHECK: ![[L05]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L06:[0-9]+]], size: [[#]], annotations: ![[L07:[0-9]+]]) +// CHECK: ![[L06]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[L08:[0-9]+]]) +// CHECK: ![[L08]] = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: ![[L09:[0-9]+]]) +// CHECK: ![[L09]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L10:[0-9]+]], size: [[#]], annotations: ![[L11:[0-9]+]]) +// CHECK: ![[L10]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[L12:[0-9]+]]) +// CHECK: ![[L12]] = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: ![[L13:[0-9]+]]) +// CHECK: ![[L13]] = !DIBasicType(name: "int", size: [[#]], encoding: DW_ATE_signed, annotations: ![[L14:[0-9]+]]) +// CHECK: ![[L14]] = !{![[L15:[0-9]+]], ![[L16:[0-9]+]]} +// CHECK: ![[L15]] = !{!"btf:type_tag", !"tag1"} +// CHECK: ![[L16]] = !{!"btf:type_tag", !"tag2"} +// CHECK: ![[L11]] = !{![[L17:[0-9]+]], ![[L18:[0-9]+]], ![[L19:[0-9]+]], ![[L20:[0-9]+]]} +// CHECK: ![[L17]] = !{!"btf:type_tag", !"tag3"} +// CHECK: ![[L18]] = !{!"btf:type_tag", !"tag4"} +// CHECK: ![[L19]] = !{!"btf_type_tag", !"tag1"} +// CHECK: ![[L20]] = !{!"btf_type_tag", !"tag2"} +// CHECK: ![[L07]] = !{![[L21:[0-9]+]], ![[L22:[0-9]+]], ![[L23:[0-9]+]], ![[L24:[0-9]+]]} +// CHECK: ![[L21]] = !{!"btf:type_tag", !"tag5"} +// CHECK: ![[L22]] = !{!"btf:type_tag", !"tag6"} +// CHECK: ![[L23]] = !{!"btf_type_tag", !"tag3"} +// CHECK: ![[L24]] = !{!"btf_type_tag", !"tag4"} +// CHECK: ![[L03]] = !{![[L25:[0-9]+]], ![[L26:[0-9]+]]} +// CHECK: ![[L25]] = !{!"btf_type_tag", !"tag5"} +// CHECK: ![[L26]] = !{!"btf_type_tag", !"tag6"} diff --git a/clang/test/CodeGen/attr-btf_type_tag-void.c b/clang/test/CodeGen/attr-btf_type_tag-void.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/attr-btf_type_tag-void.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s + +#define __tag1 __attribute__((btf_type_tag("tag1"))) +void __tag1 *g; + +// CHECK: distinct !DIGlobalVariable(name: "g", scope: ![[#]], file: ![[#]], line: [[#]], type: ![[L1:[0-9]+]], isLocal: false, isDefinition: true) +// CHECK: ![[L1]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[L2:[0-9]+]], size: [[#]], annotations: ![[L3:[0-9]+]]) +// CHECK: ![[L2]] = !DIBasicType(tag: DW_TAG_unspecified_type, name: "void", annotations: ![[L4:[0-9]+]]) +// CHECK: ![[L4]] = !{![[L5:[0-9]+]]} +// CHECK: ![[L5]] = !{!"btf:type_tag", !"tag1"} +// CHECK: ![[L3]] = !{![[L6:[0-9]+]]} +// CHECK: ![[L6]] = !{!"btf_type_tag", !"tag1"} diff --git a/clang/test/CodeGen/attr-btf_type_tag-volatile.c b/clang/test/CodeGen/attr-btf_type_tag-volatile.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/attr-btf_type_tag-volatile.c @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 \ +// RUN: -triple %itanium_abi_triple -debug-info-kind=limited \ +// RUN: -S -emit-llvm -o - %s | FileCheck %s + +// See attr-btf_type_tag-const.c for reasoning behind this test. +// Alternatively, see the following method: +// CGDebugInfo::CreateType(const BTFTagAttributedType, llvm::DIFile) + +#define __tag1 __attribute__((btf_type_tag("tag1"))) + +volatile int foo; +typeof(foo) __tag1 bar; + +// CHECK: ![[#]] = distinct !DIGlobalVariable(name: "bar", {{.*}}, type: ![[L1:[0-9]+]], {{.*}}) +// CHECK: ![[L1]] = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: ![[L2:[0-9]+]]) +// CHECK: ![[L2]] = !DIBasicType(name: "int", size: [[#]], {{.*}}, annotations: ![[L3:[0-9]+]]) +// CHECK: ![[L3]] = !{![[L4:[0-9]+]]} +// CHECK: ![[L4]] = !{!"btf:type_tag", !"tag1"} diff --git a/llvm/include/llvm/IR/DIBuilder.h b/llvm/include/llvm/IR/DIBuilder.h --- a/llvm/include/llvm/IR/DIBuilder.h +++ b/llvm/include/llvm/IR/DIBuilder.h @@ -287,6 +287,9 @@ DINode::DIFlags Flags = DINode::FlagZero, DINodeArray Annotations = nullptr); + DIDerivedType *createAnnotationsPlaceholder(DIType *Ty, + DINodeArray Annotations); + /// Create debugging information entry for a 'friend'. DIDerivedType *createFriend(DIType *Ty, DIType *FriendTy); diff --git a/llvm/lib/IR/DIBuilder.cpp b/llvm/lib/IR/DIBuilder.cpp --- a/llvm/lib/IR/DIBuilder.cpp +++ b/llvm/lib/IR/DIBuilder.cpp @@ -340,6 +340,17 @@ Annotations); } +DIDerivedType * +DIBuilder::createAnnotationsPlaceholder(DIType *Ty, DINodeArray Annotations) { + auto *RetTy = + DIDerivedType::getTemporary( + VMContext, dwarf::DW_TAG_LLVM_annotation, "", nullptr, 0, nullptr, Ty, + 0, 0, 0, std::nullopt, DINode::FlagZero, nullptr, Annotations) + .release(); + trackIfUnresolved(RetTy); + return RetTy; +} + DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) { assert(Ty && "Invalid type!"); assert(FriendTy && "Invalid friend type!");