[clang] dc00c96 - [OpenCL] Change extension handling for -fdeclare-opencl-builtins

Sven van Haastregt via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 2 03:15:45 PST 2021


Author: Sven van Haastregt
Date: 2021-02-02T11:15:29Z
New Revision: dc00c96b2d1bcae56ef598b614ba65a1cc26c4de

URL: https://github.com/llvm/llvm-project/commit/dc00c96b2d1bcae56ef598b614ba65a1cc26c4de
DIFF: https://github.com/llvm/llvm-project/commit/dc00c96b2d1bcae56ef598b614ba65a1cc26c4de.diff

LOG: [OpenCL] Change extension handling for -fdeclare-opencl-builtins

Until now, the `-fdeclare-opencl-builtins` option behaved differently
compared to inclusion of `opencl-c.h`: builtins that are part of an
extension were only available if the extension was enabled using the
corresponding pragma.

Builtins that belong to an extension are guarded using a preprocessor
macro (that is named after the extension) in `opencl-c.h`.  Align the
behaviour of `-fdeclare-opencl-builtins` with this.

Co-authored-by: Anastasia Stulova

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

Added: 
    

Modified: 
    clang/lib/Sema/SemaLookup.cpp
    clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index 29038ab9fe1c..3556f07b1fe5 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -743,18 +743,6 @@ static void GetOpenCLBuiltinFctOverloads(
   }
 }
 
-/// Add extensions to the function declaration.
-/// \param S (in/out) The Sema instance.
-/// \param BIDecl (in) Description of the builtin.
-/// \param FDecl (in/out) FunctionDecl instance.
-static void AddOpenCLExtensions(Sema &S, const OpenCLBuiltinStruct &BIDecl,
-                                FunctionDecl *FDecl) {
-  // Fetch extension associated with a function prototype.
-  StringRef E = FunctionExtensionTable[BIDecl.Extension];
-  if (E != "")
-    S.setOpenCLExtensionForDecl(FDecl, E);
-}
-
 /// When trying to resolve a function name, if isOpenCLBuiltin() returns a
 /// non-null <Index, Len> pair, then the name is referencing an OpenCL
 /// builtin function.  Add all candidate signatures to the LookUpResult.
@@ -790,6 +778,13 @@ static void InsertOCLBuiltinDeclarationsFromTable(Sema &S, LookupResult &LR,
         (OpenCLVersion >= OpenCLBuiltin.MaxVersion))
       continue;
 
+    // Ignore this builtin function if it carries an extension macro that is
+    // not defined. This indicates that the extension is not supported by the
+    // target, so the builtin function should not be available.
+    StringRef Ext = FunctionExtensionTable[OpenCLBuiltin.Extension];
+    if (!Ext.empty() && !S.getPreprocessor().isMacroDefined(Ext))
+      continue;
+
     SmallVector<QualType, 1> RetTypes;
     SmallVector<SmallVector<QualType, 1>, 5> ArgTypes;
 
@@ -843,8 +838,6 @@ static void InsertOCLBuiltinDeclarationsFromTable(Sema &S, LookupResult &LR,
       if (!S.getLangOpts().OpenCLCPlusPlus)
         NewOpenCLBuiltin->addAttr(OverloadableAttr::CreateImplicit(Context));
 
-      AddOpenCLExtensions(S, OpenCLBuiltin, NewOpenCLBuiltin);
-
       LR.addDecl(NewOpenCLBuiltin);
     }
   }

diff  --git a/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl b/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
index 3cf963eb2fba..773998a60941 100644
--- a/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
+++ b/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
@@ -38,26 +38,6 @@ kernel void test_pointers(volatile global void *global_p, global const int4 *a)
 
   prefetch(a, 2);
 
-  atom_add((volatile __global int *)global_p, i);
-#if !defined(__OPENCL_CPP_VERSION__) && __OPENCL_C_VERSION__ < CL_VERSION_1_1
-// expected-error at -2{{no matching function for call to 'atom_add'}}
-
-// There are two potential definitions of the function "atom_add", both are
-// currently disabled because the associated extension is disabled.
-// expected-note at -6{{candidate function not viable: cannot pass pointer to address space '__global' as a pointer to address space '__local' in 1st argument}}
-// expected-note at -7{{candidate function not viable: no known conversion}}
-// expected-note at -8{{candidate function not viable: no known conversion}}
-// expected-note at -9{{candidate function not viable: no known conversion}}
-// expected-note at -10{{candidate unavailable as it requires OpenCL extension 'cl_khr_global_int32_base_atomics' to be enabled}}
-// expected-note at -11{{candidate unavailable as it requires OpenCL extension 'cl_khr_global_int32_base_atomics' to be enabled}}
-// expected-note at -12{{candidate unavailable as it requires OpenCL extension 'cl_khr_int64_base_atomics' to be enabled}}
-// expected-note at -13{{candidate unavailable as it requires OpenCL extension 'cl_khr_int64_base_atomics' to be enabled}}
-#endif
-
-#if __OPENCL_C_VERSION__ < CL_VERSION_1_1
-#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
-#endif
-
   atom_add((volatile __global int *)global_p, i);
   atom_cmpxchg((volatile __global unsigned int *)global_p, ui, ui);
 }
@@ -146,11 +126,9 @@ kernel void basic_image_writeonly(write_only image1d_buffer_t image_write_only_i
 
 kernel void basic_subgroup(global uint *out) {
   out[0] = get_sub_group_size();
-#if defined(__OPENCL_CPP_VERSION__)
-  // expected-error at -2{{no matching function for call to 'get_sub_group_size'}}
-  // expected-note at -3{{candidate unavailable as it requires OpenCL extension 'cl_khr_subgroups' to be enabled}}
-#else
-  // expected-error at -5{{use of declaration 'get_sub_group_size' requires cl_khr_subgroups extension to be enabled}}
+#if __OPENCL_C_VERSION__ <= CL_VERSION_1_2 && !defined(__OPENCL_CPP_VERSION__)
+  // expected-error at -2{{implicit declaration of function 'get_sub_group_size' is invalid in OpenCL}}
+  // expected-error at -3{{implicit conversion changes signedness}}
 #endif
 }
 


        


More information about the cfe-commits mailing list