diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst --- a/clang/docs/LanguageExtensions.rst +++ b/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 +`_. + +**Example of Use**: + +.. code-block:: c++ + + template + void foo(T *par){ + __private __remove_address_space var3; + } + + 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 --- a/clang/lib/Headers/opencl-c-base.h +++ b/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 struct __remove_address_space { using type = _Tp; }; +template struct __remove_address_space<__generic _Tp> { + using type = _Tp; +}; +template struct __remove_address_space<__global _Tp> { + using type = _Tp; +}; +template struct __remove_address_space<__private _Tp> { + using type = _Tp; +}; +template struct __remove_address_space<__local _Tp> { + using type = _Tp; +}; +template 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 --- /dev/null +++ b/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 +struct is_same { + static const bool value = false; +}; + +template +struct is_same { + 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::type, const volatile int>::value, + "non-address-space qualifiers inappropriately removed by __remove_address_space"); +}