r372516 - [CLANG][BPF] permit any argument type for __builtin_preserve_access_index()

Yonghong Song via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 22 10:33:48 PDT 2019


Author: yhs
Date: Sun Sep 22 10:33:48 2019
New Revision: 372516

URL: http://llvm.org/viewvc/llvm-project?rev=372516&view=rev
Log:
[CLANG][BPF] permit any argument type for __builtin_preserve_access_index()

Commit c15aa241f821 ("[CLANG][BPF] change __builtin_preserve_access_index()
signature") changed the builtin function signature to
  PointerT __builtin_preserve_access_index(PointerT ptr)
with a pointer type as the argument/return type, where argument and
return types must be the same.

There is really no reason for this constraint. The builtin just
presented a code region so that IR builtins
  __builtin_{array, struct, union}_preserve_access_index
can be applied.

This patch removed the pointer type restriction to permit any
argument type as long as it is permitted by the compiler.

Differential Revision: https://reviews.llvm.org/D67883

Added:
    cfe/trunk/test/CodeGen/builtin-preserve-access-index-nonptr.c
Modified:
    cfe/trunk/docs/LanguageExtensions.rst
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Sema/builtin-preserve-access-index.c

Modified: cfe/trunk/docs/LanguageExtensions.rst
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/LanguageExtensions.rst?rev=372516&r1=372515&r2=372516&view=diff
==============================================================================
--- cfe/trunk/docs/LanguageExtensions.rst (original)
+++ cfe/trunk/docs/LanguageExtensions.rst Sun Sep 22 10:33:48 2019
@@ -2354,13 +2354,13 @@ array subscript access and structure/uni
 under bpf compile-once run-everywhere framework. Debuginfo (typically
 with ``-g``) is needed, otherwise, the compiler will exit with an error.
 The return type for the intrinsic is the same as the type of the
-argument, and must be a pointer type.
+argument.
 
 **Syntax**:
 
 .. code-block:: c
 
-  PointerT __builtin_preserve_access_index(PointerT ptr)
+  type __builtin_preserve_access_index(type arg)
 
 **Example of Use**:
 
@@ -2375,7 +2375,8 @@ argument, and must be a pointer type.
     } c[4];
   };
   struct t *v = ...;
-  const void *pb =__builtin_preserve_access_index(&v->c[3].b);
+  int *pb =__builtin_preserve_access_index(&v->c[3].b);
+  __builtin_preserve_access_index(v->j);
 
 Multiprecision Arithmetic Builtins
 ----------------------------------

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=372516&r1=372515&r2=372516&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Sun Sep 22 10:33:48 2019
@@ -9916,7 +9916,4 @@ def err_bit_cast_non_trivially_copyable
   "__builtin_bit_cast %select{source|destination}0 type must be trivially copyable">;
 def err_bit_cast_type_size_mismatch : Error<
   "__builtin_bit_cast source size does not equal destination size (%0 vs %1)">;
-
-def err_builtin_preserve_access_index_invalid_arg : Error<
-  "__builtin_preserve_access_index argument must a pointer type instead of %0">;
 } // end of sema component.

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=372516&r1=372515&r2=372516&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Sun Sep 22 10:33:48 2019
@@ -191,22 +191,12 @@ static bool SemaBuiltinAddressof(Sema &S
   return false;
 }
 
-/// Check the number of arguments and arg type, and set the result type to
+/// Check the number of arguments and set the result type to
 /// the argument type.
 static bool SemaBuiltinPreserveAI(Sema &S, CallExpr *TheCall) {
   if (checkArgCount(S, TheCall, 1))
     return true;
 
-  // The argument type must be a pointer
-  ExprResult Arg = TheCall->getArg(0);
-  QualType Ty = Arg.get()->getType();
-  if (!Ty->isPointerType()) {
-    S.Diag(Arg.get()->getBeginLoc(),
-           diag::err_builtin_preserve_access_index_invalid_arg)
-        << Ty << Arg.get()->getSourceRange();
-    return true;
-  }
-
   TheCall->setType(TheCall->getArg(0)->getType());
   return false;
 }

Added: cfe/trunk/test/CodeGen/builtin-preserve-access-index-nonptr.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/builtin-preserve-access-index-nonptr.c?rev=372516&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/builtin-preserve-access-index-nonptr.c (added)
+++ cfe/trunk/test/CodeGen/builtin-preserve-access-index-nonptr.c Sun Sep 22 10:33:48 2019
@@ -0,0 +1,18 @@
+// RUN: %clang -target x86_64 -emit-llvm -S -g %s -o - | FileCheck %s
+
+#define _(x) (__builtin_preserve_access_index(x))
+
+struct s1 {
+  char a;
+  int b[4];
+};
+
+int unit1(struct s1 *arg) {
+  return _(arg->b[2]);
+}
+// CHECK: define dso_local i32 @unit1
+// CHECK: call [4 x i32]* @llvm.preserve.struct.access.index.p0a4i32.p0s_struct.s1s(%struct.s1* %{{[0-9a-z]+}}, i32 1, i32 1), !dbg !{{[0-9]+}}, !llvm.preserve.access.index ![[STRUCT_S1:[0-9]+]]
+// CHECK: call i32* @llvm.preserve.array.access.index.p0i32.p0a4i32([4 x i32]* %{{[0-9a-z]+}}, i32 1, i32 2), !dbg !{{[0-9]+}}, !llvm.preserve.access.index ![[ARRAY:[0-9]+]]
+//
+// CHECK: ![[ARRAY]] = !DICompositeType(tag: DW_TAG_array_type
+// CHECK: ![[STRUCT_S1]] = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s1"

Modified: cfe/trunk/test/Sema/builtin-preserve-access-index.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/builtin-preserve-access-index.c?rev=372516&r1=372515&r2=372516&view=diff
==============================================================================
--- cfe/trunk/test/Sema/builtin-preserve-access-index.c (original)
+++ cfe/trunk/test/Sema/builtin-preserve-access-index.c Sun Sep 22 10:33:48 2019
@@ -4,8 +4,8 @@ const void *invalid1(const int *arg) {
   return __builtin_preserve_access_index(&arg[1], 1); // expected-error {{too many arguments to function call, expected 1, have 2}}
 }
 
-const void *invalid2(const int *arg) {
-  return __builtin_preserve_access_index(1); // expected-error {{__builtin_preserve_access_index argument must a pointer type instead of 'int'}}
+int valid2(void) {
+  return __builtin_preserve_access_index(1);
 }
 
 void *invalid3(const int *arg) {
@@ -29,3 +29,11 @@ struct s { int a; int b; };
 int valid7(struct s *arg) {
   return *__builtin_preserve_access_index(&arg->b);
 }
+
+int valid8(struct s *arg) {
+  return __builtin_preserve_access_index(arg->a + arg->b);
+}
+
+int valid9(struct s *arg) {
+  return __builtin_preserve_access_index(({arg->a = 2; arg->b = 3; }));
+}




More information about the cfe-commits mailing list