yonghong-song created this revision.
yonghong-song added a reviewer: aaron.ballman.
yonghong-song requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Current ASTContext.getAttributedType() takes attribute kind,
ModifiedType and EquivType as the hash to decide whether an AST node
has been generated or note. But this is not enough for btf_type_tag
as the attribute might have the same ModifiedType and EquivType, but 
still have different string associated with attribute.

For example, for a data structure like below,

  struct map_value {
        int __attribute__((btf_type_tag("tag1"))) 
__attribute__((btf_type_tag("tag3"))) *a; 
        int __attribute__((btf_type_tag("tag2"))) 
__attribute__((btf_type_tag("tag4"))) *b; 
  };  

The current ASTContext.getAttributedType() will produce
an AST similar to below:

  struct map_value {
        int __attribute__((btf_type_tag("tag1"))) 
__attribute__((btf_type_tag("tag3"))) *a; 
        int __attribute__((btf_type_tag("tag1"))) 
__attribute__((btf_type_tag("tag3"))) *b; 
  };  

and this is incorrect.

To fix the issue, an optional StringRef parameter is passed to
getAttributedType() so the btf_type_tag string can be part of
the hash. This resolved the issue since the cached btf_type_tag
attributes have empty tag string so there is never a match
between cached entries and the new btf_type_tag.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120296

Files:
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/AST/Type.h
  clang/lib/AST/ASTContext.cpp
  clang/lib/Sema/SemaType.cpp
  clang/test/CodeGen/attr-btf_type_tag-similar-type.c

Index: clang/test/CodeGen/attr-btf_type_tag-similar-type.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/attr-btf_type_tag-similar-type.c
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s
+
+struct map_value {
+        int __attribute__((btf_type_tag("tag1"))) __attribute__((btf_type_tag("tag3"))) *a;
+        int __attribute__((btf_type_tag("tag2"))) __attribute__((btf_type_tag("tag4"))) *b;
+};
+
+struct map_value *func(void);
+
+int test(struct map_value *arg)
+{
+        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"}
Index: clang/lib/Sema/SemaType.cpp
===================================================================
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -258,9 +258,11 @@
     /// Get an attributed type for the given attribute, and remember the Attr
     /// object so that we can attach it to the AttributedTypeLoc.
     QualType getAttributedType(Attr *A, QualType ModifiedType,
-                               QualType EquivType) {
+                               QualType EquivType,
+                               StringRef ExtraInfo = StringRef()) {
       QualType T =
-          sema.Context.getAttributedType(A->getKind(), ModifiedType, EquivType);
+          sema.Context.getAttributedType(A->getKind(), ModifiedType, EquivType,
+                                         ExtraInfo);
       AttrsForTypes.push_back({cast<AttributedType>(T.getTypePtr()), A});
       AttrsForTypesSorted = false;
       return T;
@@ -6553,8 +6555,9 @@
 
   ASTContext &Ctx = S.Context;
   StringRef BTFTypeTag = StrLiteral->getString();
-  Type = State.getAttributedType(
-      ::new (Ctx) BTFTypeTagAttr(Ctx, Attr, BTFTypeTag), Type, Type);
+  Type =
+      State.getAttributedType(::new (Ctx) BTFTypeTagAttr(Ctx, Attr, BTFTypeTag),
+                              Type, Type, BTFTypeTag);
 }
 
 /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
Index: clang/lib/AST/ASTContext.cpp
===================================================================
--- clang/lib/AST/ASTContext.cpp
+++ clang/lib/AST/ASTContext.cpp
@@ -4665,9 +4665,11 @@
 
 QualType ASTContext::getAttributedType(attr::Kind attrKind,
                                        QualType modifiedType,
-                                       QualType equivalentType) {
+                                       QualType equivalentType,
+                                       StringRef extraInfo) {
   llvm::FoldingSetNodeID id;
-  AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
+  AttributedType::Profile(id, attrKind, modifiedType, equivalentType,
+                          extraInfo);
 
   void *insertPos = nullptr;
   AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
Index: clang/include/clang/AST/Type.h
===================================================================
--- clang/include/clang/AST/Type.h
+++ clang/include/clang/AST/Type.h
@@ -4775,10 +4775,12 @@
   }
 
   static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind,
-                      QualType modified, QualType equivalent) {
+                      QualType modified, QualType equivalent,
+                      StringRef extraInfo = StringRef()) {
     ID.AddInteger(attrKind);
     ID.AddPointer(modified.getAsOpaquePtr());
     ID.AddPointer(equivalent.getAsOpaquePtr());
+    ID.AddString(extraInfo);
   }
 
   static bool classof(const Type *T) {
Index: clang/include/clang/AST/ASTContext.h
===================================================================
--- clang/include/clang/AST/ASTContext.h
+++ clang/include/clang/AST/ASTContext.h
@@ -1590,7 +1590,8 @@
 
   QualType getAttributedType(attr::Kind attrKind,
                              QualType modifiedType,
-                             QualType equivalentType);
+                             QualType equivalentType,
+                             StringRef extraInfo = StringRef());
 
   QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced,
                                         QualType Replacement) const;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to