[PATCH] D67979: [BPF] Generate array dimension size properly for zero-size elements

Yonghong Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 24 13:26:01 PDT 2019


yonghong-song created this revision.
yonghong-song added a reviewer: ast.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Currently, if an array element type size is 0, the number of
array elements will be set to 0, regardless of what user
specified. This implementation is done in the beginning where
BTF is mostly used to calculate the member offset.

For example,

  struct s {}; 
  struct s1 {
        int b;
        struct s a[2];
  };  
  struct s1 s1; 

The BTF will have struct "s1" member "a" with element count 0.

Now BTF types are used for compile-once and run-everywhere
relocations and we need more precise type representation
for type comparison. Andrii reported the issue as there
are differences between original structure and BTF-generated
structure.

This patch made the change to correctly assign "2" 
as the number elements of member "a".
Some dead codes related to ElemSize compuation are also removed.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67979

Files:
  llvm/lib/Target/BPF/BTFDebug.cpp
  llvm/test/CodeGen/BPF/BTF/array-size-0.ll


Index: llvm/test/CodeGen/BPF/BTF/array-size-0.ll
===================================================================
--- llvm/test/CodeGen/BPF/BTF/array-size-0.ll
+++ llvm/test/CodeGen/BPF/BTF/array-size-0.ll
@@ -32,7 +32,7 @@
 ; CHECK-NEXT:        .long   0
 ; CHECK-NEXT:        .long   1
 ; CHECK-NEXT:        .long   3
-; CHECK-NEXT:        .long   0
+; CHECK-NEXT:        .long   10
 ; CHECK-NEXT:        .long   3                       # BTF_KIND_INT(id = 3)
 ; CHECK-NEXT:        .long   16777216                # 0x1000000
 ; CHECK-NEXT:        .long   4
Index: llvm/lib/Target/BPF/BTFDebug.cpp
===================================================================
--- llvm/lib/Target/BPF/BTFDebug.cpp
+++ llvm/lib/Target/BPF/BTFDebug.cpp
@@ -473,35 +473,29 @@
 
 void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
   // Visit array element type.
-  uint32_t ElemTypeId, ElemSize;
+  uint32_t ElemTypeId;
   const DIType *ElemType = CTy->getBaseType();
   visitTypeEntry(ElemType, ElemTypeId, false, false);
 
-  // Strip qualifiers from element type to get accurate element size.
-  ElemSize = ElemType->getSizeInBits() >> 3;
-
-  if (!CTy->getSizeInBits()) {
-    auto TypeEntry = std::make_unique<BTFTypeArray>(ElemTypeId, 0);
-    ElemTypeId = addType(std::move(TypeEntry), CTy);
-  } else {
-    // Visit array dimensions.
-    DINodeArray Elements = CTy->getElements();
-    for (int I = Elements.size() - 1; I >= 0; --I) {
-      if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
-        if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
-          const DISubrange *SR = cast<DISubrange>(Element);
-          auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
-          int64_t Count = CI->getSExtValue();
-
-          auto TypeEntry =
-              std::make_unique<BTFTypeArray>(ElemTypeId, Count);
-          if (I == 0)
-            ElemTypeId = addType(std::move(TypeEntry), CTy);
-          else
-            ElemTypeId = addType(std::move(TypeEntry));
-          ElemSize = ElemSize * Count;
-        }
-    }
+  // Visit array dimensions.
+  DINodeArray Elements = CTy->getElements();
+  for (int I = Elements.size() - 1; I >= 0; --I) {
+    if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
+      if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
+        const DISubrange *SR = cast<DISubrange>(Element);
+        auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
+        int64_t Count = CI->getSExtValue();
+
+        // For struct s { int b; char c[]; }, the c[] will be represented
+        // as an array with Count = -1.
+        auto TypeEntry =
+            std::make_unique<BTFTypeArray>(ElemTypeId,
+                Count >= 0 ? Count : 0);
+        if (I == 0)
+          ElemTypeId = addType(std::move(TypeEntry), CTy);
+        else
+          ElemTypeId = addType(std::move(TypeEntry));
+      }
   }
 
   // The array TypeId is the type id of the outermost dimension.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67979.221581.patch
Type: text/x-patch
Size: 2973 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190924/31e237ec/attachment.bin>


More information about the llvm-commits mailing list