[llvm-branch-commits] [llvm-branch] r367211 - Merging r367062:

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Jul 29 02:01:43 PDT 2019


Author: hans
Date: Mon Jul 29 02:01:42 2019
New Revision: 367211

URL: http://llvm.org/viewvc/llvm-project?rev=367211&view=rev
Log:
Merging r367062:
------------------------------------------------------------------------
r367062 | yhs | 2019-07-25 23:47:27 +0200 (Thu, 25 Jul 2019) | 30 lines

[BPF] fix typedef issue for offset relocation

Currently, the CO-RE offset relocation does not work
if any struct/union member or array element is a typedef.
For example,
  typedef const int arr_t[7];
  struct input {
      arr_t a;
  };
  func(...) {
       struct input *in = ...;
       ... __builtin_preserve_access_index(&in->a[1]) ...
  }
The BPF backend calculated default offset is 0 while
4 is the correct answer. Similar issues exist for struct/union
typedef's.

When getting struct/union member or array element type,
we should trace down to the type by skipping typedef
and qualifiers const/volatile as this is what clang did
to generate getelementptr instructions.
(const/volatile member type qualifiers are already
ignored by clang.)

This patch fixed this issue, for each access index,
skipping typedef and const/volatile/restrict BTF types.

Signed-off-by: Yonghong Song <yhs at fb.com>

Differential Revision: https://reviews.llvm.org/D65259
------------------------------------------------------------------------

Added:
    llvm/branches/release_90/test/CodeGen/BPF/CORE/offset-reloc-typedef-array.ll
      - copied unchanged from r367062, llvm/trunk/test/CodeGen/BPF/CORE/offset-reloc-typedef-array.ll
    llvm/branches/release_90/test/CodeGen/BPF/CORE/offset-reloc-typedef-struct.ll
      - copied unchanged from r367062, llvm/trunk/test/CodeGen/BPF/CORE/offset-reloc-typedef-struct.ll
    llvm/branches/release_90/test/CodeGen/BPF/CORE/offset-reloc-typedef-union.ll
      - copied unchanged from r367062, llvm/trunk/test/CodeGen/BPF/CORE/offset-reloc-typedef-union.ll
    llvm/branches/release_90/test/CodeGen/BPF/CORE/offset-reloc-typedef.ll
      - copied unchanged from r367062, llvm/trunk/test/CodeGen/BPF/CORE/offset-reloc-typedef.ll
Modified:
    llvm/branches/release_90/   (props changed)
    llvm/branches/release_90/lib/Target/BPF/BTFDebug.cpp
    llvm/branches/release_90/lib/Target/BPF/BTFDebug.h

Propchange: llvm/branches/release_90/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Jul 29 02:01:42 2019
@@ -1,3 +1,3 @@
 /llvm/branches/Apple/Pertwee:110850,110961
 /llvm/branches/type-system-rewrite:133420-134817
-/llvm/trunk:155241,366431,366481,366527,366570,366925,367030
+/llvm/trunk:155241,366431,366481,366527,366570,366925,367030,367062

Modified: llvm/branches/release_90/lib/Target/BPF/BTFDebug.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_90/lib/Target/BPF/BTFDebug.cpp?rev=367211&r1=367210&r2=367211&view=diff
==============================================================================
--- llvm/branches/release_90/lib/Target/BPF/BTFDebug.cpp (original)
+++ llvm/branches/release_90/lib/Target/BPF/BTFDebug.cpp Mon Jul 29 02:01:42 2019
@@ -30,6 +30,18 @@ static const char *BTFKindStr[] = {
 #include "BTF.def"
 };
 
+static const DIType * stripQualifiers(const DIType *Ty) {
+  while (const auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
+    unsigned Tag = DTy->getTag();
+    if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
+        Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_restrict_type)
+      break;
+    Ty = DTy->getBaseType();
+  }
+
+  return Ty;
+}
+
 /// Emit a BTF common type.
 void BTFTypeBase::emitType(MCStreamer &OS) {
   OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) +
@@ -184,9 +196,9 @@ void BTFTypeEnum::emitType(MCStreamer &O
   }
 }
 
-BTFTypeArray::BTFTypeArray(uint32_t ElemTypeId, uint32_t ElemSize,
-                           uint32_t NumElems)
-    : ElemSize(ElemSize) {
+BTFTypeArray::BTFTypeArray(const DIType *Ty, uint32_t ElemTypeId,
+                           uint32_t ElemSize, uint32_t NumElems)
+    : ElemTyNoQual(Ty), ElemSize(ElemSize) {
   Kind = BTF::BTF_KIND_ARRAY;
   BTFType.NameOff = 0;
   BTFType.Info = Kind << 24;
@@ -207,6 +219,9 @@ void BTFTypeArray::completeType(BTFDebug
   // created during initial type traversal. Just
   // retrieve that type id.
   ArrayInfo.IndexType = BDebug.getArrayIndexTypeId();
+
+  ElemTypeNoQual = ElemTyNoQual ? BDebug.getTypeId(ElemTyNoQual)
+                                : ArrayInfo.ElemType;
 }
 
 void BTFTypeArray::emitType(MCStreamer &OS) {
@@ -218,7 +233,7 @@ void BTFTypeArray::emitType(MCStreamer &
 
 void BTFTypeArray::getLocInfo(uint32_t Loc, uint32_t &LocOffset,
                               uint32_t &ElementTypeId) {
-  ElementTypeId = ArrayInfo.ElemType;
+  ElementTypeId = ElemTypeNoQual;
   LocOffset = Loc * ElemSize;
 }
 
@@ -251,7 +266,9 @@ void BTFTypeStruct::completeType(BTFDebu
     } else {
       BTFMember.Offset = DDTy->getOffsetInBits();
     }
-    BTFMember.Type = BDebug.getTypeId(DDTy->getBaseType());
+    const auto *BaseTy = DDTy->getBaseType();
+    BTFMember.Type = BDebug.getTypeId(BaseTy);
+    MemberTypeNoQual.push_back(BDebug.getTypeId(stripQualifiers(BaseTy)));
     Members.push_back(BTFMember);
   }
 }
@@ -270,7 +287,7 @@ std::string BTFTypeStruct::getName() { r
 
 void BTFTypeStruct::getMemberInfo(uint32_t Loc, uint32_t &MemberOffset,
                                   uint32_t &MemberType) {
-  MemberType = Members[Loc].Type;
+  MemberType = MemberTypeNoQual[Loc];
   MemberOffset =
       HasBitField ? Members[Loc].Offset & 0xffffff : Members[Loc].Offset;
 }
@@ -492,10 +509,13 @@ void BTFDebug::visitArrayType(const DICo
   uint32_t ElemTypeId, ElemSize;
   const DIType *ElemType = CTy->getBaseType();
   visitTypeEntry(ElemType, ElemTypeId, false, false);
+
+  // Strip qualifiers from element type to get accurate element size.
+  ElemType = stripQualifiers(ElemType);
   ElemSize = ElemType->getSizeInBits() >> 3;
 
   if (!CTy->getSizeInBits()) {
-    auto TypeEntry = llvm::make_unique<BTFTypeArray>(ElemTypeId, 0, 0);
+    auto TypeEntry = llvm::make_unique<BTFTypeArray>(ElemType, ElemTypeId, 0, 0);
     ArrayTypes.push_back(TypeEntry.get());
     ElemTypeId = addType(std::move(TypeEntry), CTy);
   } else {
@@ -507,9 +527,11 @@ void BTFDebug::visitArrayType(const DICo
           const DISubrange *SR = cast<DISubrange>(Element);
           auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
           int64_t Count = CI->getSExtValue();
+          const DIType *ArrayElemTy = (I == 0) ? ElemType : nullptr;
 
           auto TypeEntry =
-              llvm::make_unique<BTFTypeArray>(ElemTypeId, ElemSize, Count);
+              llvm::make_unique<BTFTypeArray>(ArrayElemTy, ElemTypeId,
+                                              ElemSize, Count);
           ArrayTypes.push_back(TypeEntry.get());
           if (I == 0)
             ElemTypeId = addType(std::move(TypeEntry), CTy);
@@ -1039,7 +1061,10 @@ void BTFDebug::generateOffsetReloc(const
         Offset += LocOffset;
         PrevArrayType = nullptr;
         setTypeFromId(ElementTypeId, &PrevStructType, &PrevArrayType);
+      } else {
+        llvm_unreachable("Internal Error: BTF offset relocation type traversal error");
       }
+
       Start = End + 1;
       End = Start;
     }

Modified: llvm/branches/release_90/lib/Target/BPF/BTFDebug.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_90/lib/Target/BPF/BTFDebug.h?rev=367211&r1=367210&r2=367211&view=diff
==============================================================================
--- llvm/branches/release_90/lib/Target/BPF/BTFDebug.h (original)
+++ llvm/branches/release_90/lib/Target/BPF/BTFDebug.h Mon Jul 29 02:01:42 2019
@@ -104,11 +104,14 @@ public:
 
 /// Handle array type.
 class BTFTypeArray : public BTFTypeBase {
+  const DIType *ElemTyNoQual;
   uint32_t ElemSize;
   struct BTF::BTFArray ArrayInfo;
+  uint32_t ElemTypeNoQual;
 
 public:
-  BTFTypeArray(uint32_t ElemTypeId, uint32_t ElemSize, uint32_t NumElems);
+  BTFTypeArray(const DIType *Ty, uint32_t ElemTypeId,
+               uint32_t ElemSize, uint32_t NumElems);
   uint32_t getSize() { return BTFTypeBase::getSize() + BTF::BTFArraySize; }
   void completeType(BTFDebug &BDebug);
   void emitType(MCStreamer &OS);
@@ -120,6 +123,7 @@ class BTFTypeStruct : public BTFTypeBase
   const DICompositeType *STy;
   bool HasBitField;
   std::vector<struct BTF::BTFMember> Members;
+  std::vector<uint32_t> MemberTypeNoQual;
 
 public:
   BTFTypeStruct(const DICompositeType *STy, bool IsStruct, bool HasBitField,




More information about the llvm-branch-commits mailing list