[llvm-branch-commits] [llvm-branch] r367210 - Merging r367030:

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


Author: hans
Date: Mon Jul 29 01:59:09 2019
New Revision: 367210

URL: http://llvm.org/viewvc/llvm-project?rev=367210&view=rev
Log:
Merging r367030:
------------------------------------------------------------------------
r367030 | yhs | 2019-07-25 18:01:26 +0200 (Thu, 25 Jul 2019) | 38 lines

[BPF] fix CO-RE incorrect index access string

Currently, we expect the CO-RE offset relocation records
a string encoding the original getelementptr access index,
so kernel bpf loader can decode it correctly.

For example,
  struct s { int a; int b; };
  struct t { int c; int d; };
  #define _(x) (__builtin_preserve_access_index(x))
  int get_value(const void *addr1, const void *addr2);
  int test(struct s *arg1, struct t *arg2) {
    return get_value(_(&arg1->b), _(&arg2->d));
  }

We expect two offset relocations:
  reloc 1: type s, access index 0, 1
  reloc 2: type t, access index 0, 1

Two globals are created to retain access indexes for the
above two relocations with global variable names.
The first global has a name "0:1:". Unfortunately,
the second global has the name "0:1:.1" as the llvm
internals automatically add suffix ".1" to a global
with the same name. Later on, the BPF peels the last
character and record "0:1" and "0:1:." in the
relocation table.

This is not desirable. BPF backend could use the global
variable suffix knowledge to generate correct access str.
This patch rather took an approach not relying on
that knowledge. It generates "s:0:1:" and "t:0:1:" to
avoid global variable suffixes and later on generate
correct index access string "0:1" for both records.

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

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

Added:
    llvm/branches/release_90/test/CodeGen/BPF/CORE/offset-reloc-access-str.ll
      - copied unchanged from r367030, llvm/trunk/test/CodeGen/BPF/CORE/offset-reloc-access-str.ll
Modified:
    llvm/branches/release_90/   (props changed)
    llvm/branches/release_90/lib/Target/BPF/BPFAbstractMemberAccess.cpp
    llvm/branches/release_90/lib/Target/BPF/BTFDebug.cpp

Propchange: llvm/branches/release_90/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Jul 29 01:59:09 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
+/llvm/trunk:155241,366431,366481,366527,366570,366925,367030

Modified: llvm/branches/release_90/lib/Target/BPF/BPFAbstractMemberAccess.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_90/lib/Target/BPF/BPFAbstractMemberAccess.cpp?rev=367210&r1=367209&r2=367210&view=diff
==============================================================================
--- llvm/branches/release_90/lib/Target/BPF/BPFAbstractMemberAccess.cpp (original)
+++ llvm/branches/release_90/lib/Target/BPF/BPFAbstractMemberAccess.cpp Mon Jul 29 01:59:09 2019
@@ -116,9 +116,8 @@ private:
   void replaceWithGEP(std::vector<CallInst *> &CallList,
                       uint32_t NumOfZerosIndex, uint32_t DIIndex);
 
-  Value *computeBaseAndAccessStr(CallInst *Call, std::string &AccessStr,
-                                 std::string &AccessKey, uint32_t Kind,
-                                 MDNode *&TypeMeta);
+  Value *computeBaseAndAccessKey(CallInst *Call, std::string &AccessKey,
+                                 uint32_t Kind, MDNode *&TypeMeta);
   bool getAccessIndex(const Value *IndexValue, uint64_t &AccessIndex);
   bool transformGEPChain(Module &M, CallInst *Call, uint32_t Kind);
 };
@@ -340,8 +339,7 @@ bool BPFAbstractMemberAccess::getAccessI
 /// Compute the base of the whole preserve_*_access_index chains, i.e., the base
 /// pointer of the first preserve_*_access_index call, and construct the access
 /// string, which will be the name of a global variable.
-Value *BPFAbstractMemberAccess::computeBaseAndAccessStr(CallInst *Call,
-                                                        std::string &AccessStr,
+Value *BPFAbstractMemberAccess::computeBaseAndAccessKey(CallInst *Call,
                                                         std::string &AccessKey,
                                                         uint32_t Kind,
                                                         MDNode *&TypeMeta) {
@@ -392,16 +390,16 @@ Value *BPFAbstractMemberAccess::computeB
   if (!LastTypeName.size() || AccessIndices.size() > TypeNameIndex + 2)
     return nullptr;
 
-  // Construct the type string AccessStr.
+  // Construct the type string AccessKey.
   for (unsigned I = 0; I < AccessIndices.size(); ++I)
-    AccessStr = std::to_string(AccessIndices[I]) + ":" + AccessStr;
+    AccessKey = std::to_string(AccessIndices[I]) + ":" + AccessKey;
 
   if (TypeNameIndex == AccessIndices.size() - 1)
-    AccessStr = "0:" + AccessStr;
+    AccessKey = "0:" + AccessKey;
 
   // Access key is the type name + access string, uniquely identifying
   // one kernel memory access.
-  AccessKey = LastTypeName + ":" + AccessStr;
+  AccessKey = LastTypeName + ":" + AccessKey;
 
   return Base;
 }
@@ -410,10 +408,10 @@ Value *BPFAbstractMemberAccess::computeB
 /// transformation to a chain of relocable GEPs.
 bool BPFAbstractMemberAccess::transformGEPChain(Module &M, CallInst *Call,
                                                 uint32_t Kind) {
-  std::string AccessStr, AccessKey;
+  std::string AccessKey;
   MDNode *TypeMeta = nullptr;
   Value *Base =
-      computeBaseAndAccessStr(Call, AccessStr, AccessKey, Kind, TypeMeta);
+      computeBaseAndAccessKey(Call, AccessKey, Kind, TypeMeta);
   if (!Base)
     return false;
 
@@ -432,7 +430,7 @@ bool BPFAbstractMemberAccess::transformG
 
   if (GEPGlobals.find(AccessKey) == GEPGlobals.end()) {
     GV = new GlobalVariable(M, Type::getInt64Ty(BB->getContext()), false,
-                            GlobalVariable::ExternalLinkage, NULL, AccessStr);
+                            GlobalVariable::ExternalLinkage, NULL, AccessKey);
     GV->addAttribute(BPFCoreSharedInfo::AmaAttr);
     // Set the metadata (debuginfo types) for the global.
     if (TypeMeta)

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=367210&r1=367209&r2=367210&view=diff
==============================================================================
--- llvm/branches/release_90/lib/Target/BPF/BTFDebug.cpp (original)
+++ llvm/branches/release_90/lib/Target/BPF/BTFDebug.cpp Mon Jul 29 01:59:09 2019
@@ -1006,19 +1006,20 @@ void BTFDebug::generateOffsetReloc(const
   unsigned RootId = populateStructType(RootTy);
   setTypeFromId(RootId, &PrevStructType, &PrevArrayType);
   unsigned RootTySize = PrevStructType->getStructSize();
+  StringRef IndexPattern = AccessPattern.substr(AccessPattern.find_first_of(':') + 1);
 
   BTFOffsetReloc OffsetReloc;
   OffsetReloc.Label = ORSym;
-  OffsetReloc.OffsetNameOff = addString(AccessPattern.drop_back());
+  OffsetReloc.OffsetNameOff = addString(IndexPattern.drop_back());
   OffsetReloc.TypeID = RootId;
 
   uint32_t Start = 0, End = 0, Offset = 0;
   bool FirstAccess = true;
-  for (auto C : AccessPattern) {
+  for (auto C : IndexPattern) {
     if (C != ':') {
       End++;
     } else {
-      std::string SubStr = AccessPattern.substr(Start, End - Start);
+      std::string SubStr = IndexPattern.substr(Start, End - Start);
       int Loc = std::stoi(SubStr);
 
       if (FirstAccess) {
@@ -1043,7 +1044,7 @@ void BTFDebug::generateOffsetReloc(const
       End = Start;
     }
   }
-  AccessOffsets[RootTy->getName().str() + ":" + AccessPattern.str()] = Offset;
+  AccessOffsets[AccessPattern.str()] = Offset;
   OffsetRelocTable[SecNameOff].push_back(OffsetReloc);
 }
 
@@ -1227,7 +1228,7 @@ bool BTFDebug::InstLower(const MachineIn
         MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
         DIType *Ty = dyn_cast<DIType>(MDN);
         std::string TypeName = Ty->getName();
-        int64_t Imm = AccessOffsets[TypeName + ":" + GVar->getName().str()];
+        int64_t Imm = AccessOffsets[GVar->getName().str()];
 
         // Emit "mov ri, <imm>" for abstract member accesses.
         OutMI.setOpcode(BPF::MOV_ri);




More information about the llvm-branch-commits mailing list