[clang] a5a2f05 - [C++4OpenCL] Introduces __remove_address_space utility

Justas Janickas via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 6 02:41:33 PDT 2021


Author: Justas Janickas
Date: 2021-08-06T10:40:22+01:00
New Revision: a5a2f05dcc803e79a797fb82dc0932a1a00ac46f

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

LOG: [C++4OpenCL] Introduces __remove_address_space utility

This change provides a way to conveniently declare types that have
address space qualifiers removed.

Since OpenCL adds address spaces implicitly even when they are not
specified in source, it is useful to allow deriving address space
unqualified types.

Fixes llvm.org/PR45326

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

Added: 
    clang/test/CodeGenOpenCLCXX/remove-address-space.clcpp

Modified: 
    clang/docs/LanguageExtensions.rst
    clang/lib/Headers/opencl-c-base.h

Removed: 
    


################################################################################
diff  --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index aaee136530fef..6ced38dd2faef 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -1961,6 +1961,30 @@ between the host and device is known to be compatible.
     global OnlySL *d,
   );
 
+Remove address space builtin function
+-------------------------------------
+
+``__remove_address_space`` allows to derive types in C++ for OpenCL
+that have address space qualifiers removed. This utility only affects
+address space qualifiers, therefore, other type qualifiers such as
+``const`` or ``volatile`` remain unchanged.
+
+**Example of Use**:
+
+.. code-block:: c++
+
+  template<typename T>
+  void foo(T *par){
+    T var1; // error - local function variable with global address space
+    __private T var2; // error - conflicting address space qualifiers
+    __private __remove_address_space<T> var3; // var3 is __private int
+  }
+
+  void bar(){
+    __global int* ptr;
+    foo(ptr);
+  }
+
 Legacy 1.x atomics with generic address space
 ---------------------------------------------
 

diff  --git a/clang/lib/Headers/opencl-c-base.h b/clang/lib/Headers/opencl-c-base.h
index 3c5e2c9739368..a9b2364e7457d 100644
--- a/clang/lib/Headers/opencl-c-base.h
+++ b/clang/lib/Headers/opencl-c-base.h
@@ -572,6 +572,26 @@ typedef struct {
 #define as_intptr_t(x) __builtin_astype((x), intptr_t)
 #define as_uintptr_t(x) __builtin_astype((x), uintptr_t)
 
+// C++ for OpenCL - __remove_address_space
+#if defined(__OPENCL_CPP_VERSION__)
+template <typename _Tp> struct __remove_address_space { using type = _Tp; };
+template <typename _Tp> struct __remove_address_space<__generic _Tp> {
+  using type = _Tp;
+};
+template <typename _Tp> struct __remove_address_space<__global _Tp> {
+  using type = _Tp;
+};
+template <typename _Tp> struct __remove_address_space<__private _Tp> {
+  using type = _Tp;
+};
+template <typename _Tp> struct __remove_address_space<__local _Tp> {
+  using type = _Tp;
+};
+template <typename _Tp> struct __remove_address_space<__constant _Tp> {
+  using type = _Tp;
+};
+#endif
+
 // OpenCL v1.1 s6.9, v1.2/2.0 s6.10 - Function qualifiers
 
 #define __kernel_exec(X, typen) __kernel \

diff  --git a/clang/test/CodeGenOpenCLCXX/remove-address-space.clcpp b/clang/test/CodeGenOpenCLCXX/remove-address-space.clcpp
new file mode 100644
index 0000000000000..f6f0c3290aa66
--- /dev/null
+++ b/clang/test/CodeGenOpenCLCXX/remove-address-space.clcpp
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 %s -cl-std=clc++ -fdeclare-opencl-builtins -finclude-default-header
+
+template<typename T, typename U>
+struct is_same {
+  static const bool value = false;
+};
+
+template<typename T>
+struct is_same<T, T> {
+  static const bool value = true;
+};
+
+void test_is_same() {
+  static_assert(is_same<int, int>::value);
+  static_assert(!is_same<int, float>::value);
+  static_assert(!is_same<__private int, int>::value);
+}
+
+void test_remove_address_space() {
+  static_assert(is_same<__remove_address_space<int>::type, int>::value,
+                "type without an address space unexpectedly modified by __remove_address_space");
+  static_assert(is_same<__remove_address_space<__generic int>::type, int>::value,
+                "__generic address space not removed by __remove_address_space");
+  static_assert(is_same<__remove_address_space<__global char>::type, char>::value,
+                "__global address space not removed by __remove_address_space");
+  static_assert(is_same<__remove_address_space<__private ulong>::type, ulong>::value,
+                "__private address space not removed by __remove_address_space");
+  static_assert(is_same<__remove_address_space<__local short>::type, short>::value,
+                "__local address space not removed by __remove_address_space");
+  static_assert(is_same<__remove_address_space<__constant int3>::type, int3>::value,
+                "__constant address space not removed by __remove_address_space");
+  static_assert(is_same<__remove_address_space<const volatile __global int>::type, const volatile int>::value,
+                "non-address-space qualifiers inappropriately removed by __remove_address_space");
+}


        


More information about the cfe-commits mailing list