Blame 0001-BPF-annotate-DIType-metadata-for-builtin-preseve_arr.patch

1243758
From f2ccdd2700174c717dc55a0f4c3f5a91ae73ff42 Mon Sep 17 00:00:00 2001
1243758
From: Yonghong Song <yhs@fb.com>
1243758
Date: Fri, 2 Aug 2019 21:28:28 +0000
1243758
Subject: [PATCH] [BPF] annotate DIType metadata for builtin
1243758
 preseve_array_access_index()
1243758
1243758
Previously, debuginfo types are annotated to
1243758
IR builtin preserve_struct_access_index() and
1243758
preserve_union_access_index(), but not
1243758
preserve_array_access_index(). The debug info
1243758
is useful to identify the root type name which
1243758
later will be used for type comparison.
1243758
1243758
For user access without explicit type conversions,
1243758
the previous scheme works as we can ignore intermediate
1243758
compiler generated type conversions (e.g., from union types to
1243758
union members) and still generate correct access index string.
1243758
1243758
The issue comes with user explicit type conversions, e.g.,
1243758
converting an array to a structure like below:
1243758
  struct t { int a; char b[40]; };
1243758
  struct p { int c; int d; };
1243758
  struct t *var = ...;
1243758
  ... __builtin_preserve_access_index(&(((struct p *)&(var->b[0]))->d)) ...
1243758
Although BPF backend can derive the type of &(var->b[0]),
1243758
explicit type annotation make checking more consistent
1243758
and less error prone.
1243758
1243758
Another benefit is for multiple dimension array handling.
1243758
For example,
1243758
  struct p { int c; int d; } g[8][9][10];
1243758
  ... __builtin_preserve_access_index(&g[2][3][4].d) ...
1243758
It would be possible to calculate the number of "struct p"'s
1243758
before accessing its member "d" if array debug info is
1243758
available as it contains each dimension range.
1243758
1243758
This patch enables to annotate IR builtin preserve_array_access_index()
1243758
with proper debuginfo type. The unit test case and language reference
1243758
is updated as well.
1243758
1243758
Signed-off-by: Yonghong Song <yhs@fb.com>
1243758
1243758
Differential Revision: https://reviews.llvm.org/D65664
1243758
1243758
llvm-svn: 367724
1243758
(cherry picked from commit d0ea05d5eff475a27a5d3bbe4d9fd389935f9cb2)
1243758
1243758
Also added back
1243758
Value *CreatePreserveArrayAccessIndex(Value *Base, unsigned Dimension,
1243758
                                      unsigned LastIndex);
1243758
1243758
To avoid breaking the ABI.
1243758
---
1243758
 clang/lib/CodeGen/CGExpr.cpp                       | 12 ++++++++---
1243758
 .../CodeGen/builtin-preserve-access-index-array.c  | 18 +++++++++++++++++
1243758
 clang/test/CodeGen/builtin-preserve-access-index.c | 23 +++++++++++-----------
1243758
 llvm/docs/LangRef.rst                              |  4 ++++
1243758
 llvm/include/llvm/IR/IRBuilder.h                   | 13 ++++++++++--
1243758
 llvm/test/CodeGen/BPF/CORE/intrinsic-array.ll      |  2 +-
1243758
 6 files changed, 55 insertions(+), 17 deletions(-)
1243758
 create mode 100644 clang/test/CodeGen/builtin-preserve-access-index-array.c
1243758
1243758
diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
1243758
index 87e8a55..b63e3af 100644
1243758
--- a/llvm/docs/LangRef.rst
1243758
+++ b/llvm/docs/LangRef.rst
1243758
@@ -17349,6 +17349,10 @@ based on array base ``base``, array dimension ``dim`` and the last access index
1243758
 into the array. The return type ``ret_type`` is a pointer type to the array element.
1243758
 The array ``dim`` and ``index`` are preserved which is more robust than
1243758
 getelementptr instruction which may be subject to compiler transformation.
1243758
+The ``llvm.preserve.access.index`` type of metadata is attached to this call instruction
1243758
+to provide array or pointer debuginfo type.
1243758
+The metadata is a ``DICompositeType`` or ``DIDerivedType`` representing the
1243758
+debuginfo version of ``type``.
1243758
 
1243758
 Arguments:
1243758
 """"""""""
1243758
diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
1243758
index a74364d..c2fa9a3 100644
1243758
--- a/llvm/include/llvm/IR/IRBuilder.h
1243758
+++ b/llvm/include/llvm/IR/IRBuilder.h
1243758
@@ -2455,6 +2455,11 @@ public:
1243758
 
1243758
   Value *CreatePreserveArrayAccessIndex(Value *Base, unsigned Dimension,
1243758
                                         unsigned LastIndex) {
1243758
+    return CreatePreserveArrayAccessIndex(Base, Dimension, LastIndex, nullptr);
1243758
+  }
1243758
+
1243758
+  Value *CreatePreserveArrayAccessIndex(Value *Base, unsigned Dimension,
1243758
+                                        unsigned LastIndex, MDNode *DbgInfo) {
1243758
     assert(isa<PointerType>(Base->getType()) &&
1243758
            "Invalid Base ptr type for preserve.array.access.index.");
1243758
     auto *BaseType = Base->getType();
1243758
@@ -2476,6 +2481,8 @@ public:
1243758
     Value *DimV = getInt32(Dimension);
1243758
     CallInst *Fn =
1243758
         CreateCall(FnPreserveArrayAccessIndex, {Base, DimV, LastIndexV});
1243758
+    if (DbgInfo)
1243758
+      Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo);
1243758
 
1243758
     return Fn;
1243758
   }
1243758
@@ -2493,7 +2500,8 @@ public:
1243758
     Value *DIIndex = getInt32(FieldIndex);
1243758
     CallInst *Fn =
1243758
         CreateCall(FnPreserveUnionAccessIndex, {Base, DIIndex});
1243758
-    Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo);
1243758
+    if (DbgInfo)
1243758
+      Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo);
1243758
 
1243758
     return Fn;
1243758
   }
1243758
@@ -2516,7 +2524,8 @@ public:
1243758
     Value *DIIndex = getInt32(FieldIndex);
1243758
     CallInst *Fn = CreateCall(FnPreserveStructAccessIndex,
1243758
                               {Base, GEPIndex, DIIndex});
1243758
-    Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo);
1243758
+    if (DbgInfo)
1243758
+      Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo);
1243758
 
1243758
     return Fn;
1243758
   }
1243758
diff --git a/llvm/test/CodeGen/BPF/CORE/intrinsic-array.ll b/llvm/test/CodeGen/BPF/CORE/intrinsic-array.ll
1243758
index adbcb9f..fe2c196 100644
1243758
--- a/llvm/test/CodeGen/BPF/CORE/intrinsic-array.ll
1243758
+++ b/llvm/test/CodeGen/BPF/CORE/intrinsic-array.ll
1243758
@@ -14,7 +14,7 @@
1243758
 define dso_local i32 @test(%struct.s* %arg) local_unnamed_addr #0 !dbg !7 {
1243758
 entry:
1243758
   call void @llvm.dbg.value(metadata %struct.s* %arg, metadata !17, metadata !DIExpression()), !dbg !18
1243758
-  %0 = tail call %struct.s* @llvm.preserve.array.access.index.p0s_struct.ss.p0s_struct.ss(%struct.s* %arg, i32 0, i32 2), !dbg !19
1243758
+  %0 = tail call %struct.s* @llvm.preserve.array.access.index.p0s_struct.ss.p0s_struct.ss(%struct.s* %arg, i32 0, i32 2), !dbg !19, !llvm.preserve.access.index !11
1243758
   %1 = tail call i32* @llvm.preserve.struct.access.index.p0i32.p0s_struct.ss(%struct.s* %0, i32 1, i32 1), !dbg !19, !llvm.preserve.access.index !12
1243758
   %2 = bitcast i32* %1 to i8*, !dbg !19
1243758
   %call = tail call i32 @get_value(i8* %2) #4, !dbg !20
1243758
-- 
1243758
1.8.3.1
1243758