r372294 - [CLANG][BPF] change __builtin_preserve_access_index() signature

Yonghong Song via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 18 19:59:43 PDT 2019


Author: yhs
Date: Wed Sep 18 19:59:43 2019
New Revision: 372294

URL: http://llvm.org/viewvc/llvm-project?rev=372294&view=rev
Log:
[CLANG][BPF] change __builtin_preserve_access_index() signature

The clang intrinsic __builtin_preserve_access_index() currently
has signature:
  const void * __builtin_preserve_access_index(const void * ptr)

This may cause compiler warning when:
  - parameter type is "volatile void *" or "const volatile void *", or
  - the assign-to type of the intrinsic does not have "const" qualifier.
Further, this signature does not allow dereference of the
builtin result pointer as it is a "const void *" type, which
adds extra step for the user to do type casting.

Let us change the signature to:
  PointerT __builtin_preserve_access_index(PointerT ptr)
such that the result and argument types are the same.
With this, directly dereferencing the builtin return value
becomes possible.

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

Modified:
    cfe/trunk/docs/LanguageExtensions.rst
    cfe/trunk/include/clang/Basic/Builtins.def
    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=372294&r1=372293&r2=372294&view=diff
==============================================================================
--- cfe/trunk/docs/LanguageExtensions.rst (original)
+++ cfe/trunk/docs/LanguageExtensions.rst Wed Sep 18 19:59:43 2019
@@ -2353,12 +2353,14 @@ and other similar allocation libraries,
 array subscript access and structure/union member access are relocatable
 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.
 
 **Syntax**:
 
 .. code-block:: c
 
-  const void * __builtin_preserve_access_index(const void * ptr)
+  PointerT __builtin_preserve_access_index(PointerT ptr)
 
 **Example of Use**:
 

Modified: cfe/trunk/include/clang/Basic/Builtins.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Builtins.def?rev=372294&r1=372293&r2=372294&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Builtins.def (original)
+++ cfe/trunk/include/clang/Basic/Builtins.def Wed Sep 18 19:59:43 2019
@@ -1470,7 +1470,7 @@ BUILTIN(__builtin_operator_new, "v*z", "
 BUILTIN(__builtin_operator_delete, "vv*", "tn")
 BUILTIN(__builtin_char_memchr, "c*cC*iz", "n")
 BUILTIN(__builtin_dump_struct, "ivC*v*", "tn")
-BUILTIN(__builtin_preserve_access_index, "vC*vC*", "nU")
+BUILTIN(__builtin_preserve_access_index, "v.", "t")
 
 // Safestack builtins
 BUILTIN(__builtin___get_unsafe_stack_start, "v*", "Fn")

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=372294&r1=372293&r2=372294&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Sep 18 19:59:43 2019
@@ -9925,4 +9925,7 @@ 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=372294&r1=372293&r2=372294&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Wed Sep 18 19:59:43 2019
@@ -191,12 +191,22 @@ static bool SemaBuiltinAddressof(Sema &S
   return false;
 }
 
-/// Check the number of arguments, and set the result type to
+/// Check the number of arguments and arg type, 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;
 }

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=372294&r1=372293&r2=372294&view=diff
==============================================================================
--- cfe/trunk/test/Sema/builtin-preserve-access-index.c (original)
+++ cfe/trunk/test/Sema/builtin-preserve-access-index.c Wed Sep 18 19:59:43 2019
@@ -4,10 +4,28 @@ 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}}
 }
 
-void *invalid2(const int *arg) {
-  return __builtin_preserve_access_index(&arg[1]); // expected-warning {{returning 'const void *' from a function with result type 'void *' discards qualifiers}}
+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'}}
 }
 
-const void *invalid3(const int *arg) {
-  return __builtin_preserve_access_index(1); // expected-warning {{incompatible integer to pointer conversion passing 'int' to parameter of type 'const void *'}}
+void *invalid3(const int *arg) {
+  return __builtin_preserve_access_index(&arg[1]); // expected-warning {{returning 'const int *' from a function with result type 'void *' discards qualifiers}}
+}
+
+const void *invalid4(volatile const int *arg) {
+  return __builtin_preserve_access_index(arg); // expected-warning {{returning 'const volatile int *' from a function with result type 'const void *' discards qualifiers}}
+}
+
+int *valid5(int *arg) {
+  return __builtin_preserve_access_index(arg);
+}
+
+int valid6(const volatile int *arg) {
+  return *__builtin_preserve_access_index(arg);
+}
+
+struct s { int a; int b; };
+
+int valid7(struct s *arg) {
+  return *__builtin_preserve_access_index(&arg->b);
 }




More information about the cfe-commits mailing list