]> granicus.if.org Git - clang/commitdiff
[OpenCL] Diagnose redundant address space conversion
authorSven van Haastregt <sven.vanhaastregt@arm.com>
Thu, 20 Sep 2018 10:07:27 +0000 (10:07 +0000)
committerSven van Haastregt <sven.vanhaastregt@arm.com>
Thu, 20 Sep 2018 10:07:27 +0000 (10:07 +0000)
Add a warning if a parameter with a named address space is passed
to a to_addr builtin.

For example:

  int i;
  to_private(&i); // generate warning as conversion from private to private is redundant.

Patch by Alistair Davies.

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@342638 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/SemaOpenCL/to_addr_builtin.cl

index 5cb037ffc2977d111410094c08dcb8cfecc83a5d..9aaa7657d4fcb36e7bb5a0d04ae162d293c38d79 100644 (file)
@@ -8613,6 +8613,10 @@ def err_opencl_variadic_function : Error<
   "invalid prototype, variadic arguments are not allowed in OpenCL">;
 def err_opencl_requires_extension : Error<
   "use of %select{type|declaration}0 %1 requires %2 extension to be enabled">;
+def warn_opencl_generic_address_space_arg : Warning<
+  "passing non-generic address space pointer to %0"
+  " may cause dynamic conversion affecting performance">,
+  InGroup<Conversion>, DefaultIgnore;
 
 // OpenCL v2.0 s6.13.6 -- Builtin Pipe Functions
 def err_opencl_builtin_pipe_first_arg : Error<
index a1bfff4fb75a29c91865c4c3a402766caca7d3e4..c46468f1c7a7f86f8614a545d190eba3d92759d6 100644 (file)
@@ -849,6 +849,13 @@ static bool SemaOpenCLBuiltinToAddr(Sema &S, unsigned BuiltinID,
     return true;
   }
 
+  if (RT->getPointeeType().getAddressSpace() != LangAS::opencl_generic) {
+    S.Diag(Call->getArg(0)->getBeginLoc(),
+           diag::warn_opencl_generic_address_space_arg)
+        << Call->getDirectCallee()->getNameInfo().getAsString()
+        << Call->getArg(0)->getSourceRange();
+  }
+
   RT = RT->getPointeeType();
   auto Qual = RT.getQualifiers();
   switch (BuiltinID) {
index 70956f007a5fdd2f771c54906d9cbfd15cd73783..9b95ff4e64b22e332adddb6069eec6f9e0fe34a3 100644 (file)
@@ -1,5 +1,5 @@
 // RUN: %clang_cc1 -verify -fsyntax-only %s
-// RUN: %clang_cc1 -verify -fsyntax-only -cl-std=CL2.0 %s
+// RUN: %clang_cc1 -Wconversion -verify -fsyntax-only -cl-std=CL2.0 %s
 
 void test(void) {
   global int *glob;
@@ -43,6 +43,7 @@ void test(void) {
   // expected-warning@-2{{incompatible integer to pointer conversion assigning to '__local int *' from 'int'}}
 #else
   // expected-error@-4{{assigning '__global int *' to '__local int *' changes address space of pointer}}
+  // expected-warning@-5{{passing non-generic address space pointer to to_global may cause dynamic conversion affecting performance}}
 #endif
 
   global char *glob_c = to_global(loc);
@@ -50,6 +51,7 @@ void test(void) {
   // expected-warning@-2{{incompatible integer to pointer conversion initializing '__global char *' with an expression of type 'int'}}
 #else
   // expected-warning@-4{{incompatible pointer types initializing '__global char *' with an expression of type '__global int *'}}
+  // expected-warning@-5{{passing non-generic address space pointer to to_global may cause dynamic conversion affecting performance}}
 #endif
 
 }