yonghong-song created this revision.
yonghong-song added a reviewer: ast.
Herald added subscribers: cfe-commits, arphaman.
Herald added a project: clang.

Commit c15aa241f821 
<https://reviews.llvm.org/rGc15aa241f8213334f6980b7981cee1f28f81112a> 
("[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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67883

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

Index: clang/test/Sema/builtin-preserve-access-index.c
===================================================================
--- clang/test/Sema/builtin-preserve-access-index.c
+++ clang/test/Sema/builtin-preserve-access-index.c
@@ -4,8 +4,8 @@
   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 @@
 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; }));
+}
Index: clang/test/CodeGen/builtin-preserve-access-index-nonptr.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/builtin-preserve-access-index-nonptr.c
@@ -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"
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -191,22 +191,12 @@
   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;
 }
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -9916,7 +9916,4 @@
   "__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.
Index: clang/docs/LanguageExtensions.rst
===================================================================
--- clang/docs/LanguageExtensions.rst
+++ clang/docs/LanguageExtensions.rst
@@ -2354,13 +2354,13 @@
 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 @@
     } 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
 ----------------------------------
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D67883: [CLANG]... Yonghong Song via Phabricator via cfe-commits

Reply via email to