tstellar / rpms / llvm

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