[PATCH] D106785: [C++4OpenCL] Introduces __remove_address_space utility

Justas Janickas via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 26 04:14:00 PDT 2021


Topotuna created this revision.
Topotuna added a reviewer: Anastasia.
Herald added subscribers: ldrumm, yaxunl.
Topotuna requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D106785

Files:
  clang/docs/LanguageExtensions.rst
  clang/lib/Headers/opencl-c-base.h
  clang/test/CodeGenOpenCLCXX/remove-address-space.clcpp


Index: clang/test/CodeGenOpenCLCXX/remove-address-space.clcpp
===================================================================
--- /dev/null
+++ clang/test/CodeGenOpenCLCXX/remove-address-space.clcpp
@@ -0,0 +1,26 @@
+// 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_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");
+}
Index: clang/lib/Headers/opencl-c-base.h
===================================================================
--- clang/lib/Headers/opencl-c-base.h
+++ clang/lib/Headers/opencl-c-base.h
@@ -565,6 +565,26 @@
 #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 \
Index: clang/docs/LanguageExtensions.rst
===================================================================
--- clang/docs/LanguageExtensions.rst
+++ clang/docs/LanguageExtensions.rst
@@ -1961,6 +1961,28 @@
     global OnlySL *d,
   );
 
+Address space removal
+---------------------
+
+``__remove_address_space`` allows to derive types in C++ for OpenCL
+that have address space qualifiers removed, as described in `C++ for
+OpenCL v1.0 s2.3.12
+<https://www.khronos.org/opencl/assets/CXX_for_OpenCL.html#remove-addrspace>`_.
+
+**Example of Use**:
+
+.. code-block:: c++
+
+  template<typename T>
+  void foo(T *par){
+    __private __remove_address_space<T> var3;
+  }
+
+  void bar(){
+    __global int* ptr;
+    foo(ptr);
+  }
+
 Legacy 1.x atomics with generic address space
 ---------------------------------------------
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106785.361617.patch
Type: text/x-patch
Size: 3491 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210726/57133b7b/attachment.bin>


More information about the cfe-commits mailing list