Skip to content

[SYCL][ESIMD] Support bool conversion to simd_mask in invoke_simd #8524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions sycl/include/std/experimental/simd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ class simd_mask {
public:
using value_type = bool;
#ifdef ENABLE_SYCL_EXT_ONEAPI_INVOKE_SIMD
using reference = __simd_mask_reference<_Tp, _Abi>;
using reference = __simd_mask_reference<element_type, _Abi>;
#else
// TODO: this is strawman implementation. Turn it into a proxy type.
using reference = bool&;
Expand Down Expand Up @@ -1682,12 +1682,14 @@ class simd_mask {

// implicit type conversion constructor
#ifdef ENABLE_SYCL_EXT_ONEAPI_INVOKE_SIMD
// TODO inefficient, use this's and __v's storage directly
template <class _Up>
simd_mask(const simd_mask<_Up, simd_abi::fixed_size<size()>>& __v) noexcept {
for (size_t __i = 0; __i < size(); __i++) {
(*this)[__i] = static_cast<element_type>(__v[__i]);
}
copyElements(__v);
}

template <class _Up>
simd_mask(const simd_mask<_Up, abi_type>& __v) noexcept {
copyElements(__v);
}
#else
template <class _Up>
Expand Down Expand Up @@ -1735,6 +1737,14 @@ class simd_mask {
#ifdef ENABLE_SYCL_EXT_ONEAPI_INVOKE_SIMD
private:
__simd_storage<element_type, _Abi> __s_;

// TODO inefficient, use this's and __v's storage directly
template <class _Up, class _UAbi>
inline void copyElements(const simd_mask<_Up, _UAbi> & __v) noexcept {
for (size_t __i = 0; __i < size(); __i++) {
(*this)[__i] = static_cast<element_type>(__v[__i]);
}
}
#endif // ENABLE_SYCL_EXT_ONEAPI_INVOKE_SIMD
};

Expand Down
18 changes: 15 additions & 3 deletions sycl/include/sycl/ext/oneapi/experimental/invoke_simd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
#include <functional>

// TODOs:
// * (a) TODO bool translation in spmd2simd.
// * (b) TODO enforce constness of a functor/lambda's () operator
// * (c) TODO support lambdas and functors in BE
// * (a) TODO enforce constness of a functor/lambda's () operator
// * (b) TODO support lambdas and functors in BE

/// Middle End - to - Back End interface to invoke explicit SIMD functions from
/// SPMD SYCL context. Must not be used by user code. BEs are expected to
Expand Down Expand Up @@ -116,6 +115,14 @@ struct spmd2simd<T, N, std::enable_if_t<std::is_arithmetic_v<T>>> {
using type = simd<T, N>;
};

// * bool converts to `simd_mask` with a user specified element type.
// Arbitrarily use unsigned char for the element type for subgroup size
// deduction and rely on the implicit conversion operator for the the actual
// user type.
template <int N> struct spmd2simd<bool, N> {
using type = simd_mask<unsigned char, N>;
};

// This structure performs the SIMD-to-SPMD return type conversion as defined
// by the spec.
template <class, class = void> struct simd2spmd;
Expand All @@ -137,6 +144,11 @@ struct simd2spmd<T, std::enable_if_t<std::is_arithmetic_v<T>>> {
using type = uniform<T>;
};

// * `simd_mask` converts to bool
template <class T, int N> struct simd2spmd<simd_mask<T, N>> {
using type = bool;
};

template <> struct simd2spmd<void> { using type = void; };

// Determine number of elements in a simd type.
Expand Down