Skip to content
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

[NFCI][SYCL] Remove vec/swizzle's getNumElements #16472

Merged
merged 2 commits into from
Dec 30, 2024
Merged
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
98 changes: 47 additions & 51 deletions sycl/include/sycl/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ class __SYCL_EBO vec
private:
#endif // __SYCL_DEVICE_ONLY__

static constexpr int getNumElements() { return NumElements; }

// SizeChecker is needed for vec(const argTN &... args) ctor to validate args.
template <int Counter, int MaxValue, class...>
struct SizeChecker : std::conditional_t<Counter == MaxValue, std::true_type,
Expand Down Expand Up @@ -548,6 +546,27 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<
OperationCurrentT, Indexes...>,
sizeof...(Indexes)> {
using DataT = typename VecT::element_type;

public:
using element_type = DataT;
using value_type = DataT;

__SYCL2020_DEPRECATED("get_count() is deprecated, please use size() instead")
size_t get_count() const { return size(); }
static constexpr size_t size() noexcept { return sizeof...(Indexes); }

template <int Num = size()>
__SYCL2020_DEPRECATED(
"get_size() is deprecated, please use byte_size() instead")
size_t get_size() const {
return byte_size<Num>();
}

template <int Num = size()> size_t byte_size() const noexcept {
return sizeof(DataT) * (Num == 3 ? 4 : Num);
}

private:
// Certain operators return a vector with a different element type. Also, the
// left and right operand types may differ. CommonDataT selects a result type
// based on these types to ensure that the result value can be represented.
Expand Down Expand Up @@ -579,7 +598,6 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<
using CommonDataT = std::conditional_t<
sizeof(DataT) >= sizeof(std::common_type_t<OpLeftDataT, OpRightDataT>),
DataT, std::common_type_t<OpLeftDataT, OpRightDataT>>;
static constexpr int getNumElements() { return sizeof...(Indexes); }

using rel_t = detail::rel_t<DataT>;
using vec_t = vec<DataT, sizeof...(Indexes)>;
Expand All @@ -594,7 +612,7 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<

template <typename OperationRightT_,
template <typename> class OperationCurrentT_, int... Idx_>
using NewRelOp = SwizzleOp<vec<rel_t, VecT::getNumElements()>,
using NewRelOp = SwizzleOp<vec<rel_t, VecT::size()>,
SwizzleOp<VecT, OperationLeftT, OperationRightT,
OperationCurrentT, Indexes...>,
OperationRightT_, OperationCurrentT_, Idx_...>;
Expand All @@ -607,12 +625,12 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<
OperationCurrentT_, Idx_...>;

template <int IdxNum, typename T = void>
using EnableIfOneIndex = typename std::enable_if_t<
1 == IdxNum && SwizzleOp::getNumElements() == IdxNum, T>;
using EnableIfOneIndex =
typename std::enable_if_t<1 == IdxNum && SwizzleOp::size() == IdxNum, T>;

template <int IdxNum, typename T = void>
using EnableIfMultipleIndexes = typename std::enable_if_t<
1 != IdxNum && SwizzleOp::getNumElements() == IdxNum, T>;
using EnableIfMultipleIndexes =
typename std::enable_if_t<1 != IdxNum && SwizzleOp::size() == IdxNum, T>;

template <typename T>
using EnableIfScalarType =
Expand All @@ -633,40 +651,22 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<
SwizzleOp<const VecT, GetOp<DataT>, GetOp<DataT>, GetOp, Indices...>;

public:
using element_type = DataT;
using value_type = DataT;

#ifdef __SYCL_DEVICE_ONLY__
using vector_t = typename vec_t::vector_t;
#endif // __SYCL_DEVICE_ONLY__

const DataT &operator[](int i) const {
std::array<int, getNumElements()> Idxs{Indexes...};
std::array<int, size()> Idxs{Indexes...};
return (*m_Vector)[Idxs[i]];
}

template <typename _T = VecT>
std::enable_if_t<!std::is_const_v<_T>, DataT> &operator[](int i) {
std::array<int, getNumElements()> Idxs{Indexes...};
std::array<int, size()> Idxs{Indexes...};
return (*m_Vector)[Idxs[i]];
}

__SYCL2020_DEPRECATED("get_count() is deprecated, please use size() instead")
size_t get_count() const { return size(); }
static constexpr size_t size() noexcept { return getNumElements(); }

template <int Num = getNumElements()>
__SYCL2020_DEPRECATED(
"get_size() is deprecated, please use byte_size() instead")
size_t get_size() const {
return byte_size<Num>();
}

template <int Num = getNumElements()> size_t byte_size() const noexcept {
return sizeof(DataT) * (Num == 3 ? 4 : Num);
}

template <typename T, int IdxNum = getNumElements(),
template <typename T, int IdxNum = size(),
typename = EnableIfOneIndex<IdxNum>,
typename = EnableIfScalarType<T>>
operator T() const {
Expand Down Expand Up @@ -870,8 +870,7 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<
__SYCL_RELLOGOP(||, (!detail::is_byte_v<T> && !detail::is_vgenfloat_v<T>))
#undef __SYCL_RELLOGOP

template <int IdxNum = getNumElements(),
typename = EnableIfMultipleIndexes<IdxNum>>
template <int IdxNum = size(), typename = EnableIfMultipleIndexes<IdxNum>>
SwizzleOp &operator=(const vec<DataT, IdxNum> &Rhs) {
std::array<int, IdxNum> Idxs{Indexes...};
for (size_t I = 0; I < Idxs.size(); ++I) {
Expand All @@ -880,15 +879,14 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<
return *this;
}

template <int IdxNum = getNumElements(), typename = EnableIfOneIndex<IdxNum>>
template <int IdxNum = size(), typename = EnableIfOneIndex<IdxNum>>
SwizzleOp &operator=(const DataT &Rhs) {
std::array<int, IdxNum> Idxs{Indexes...};
(*m_Vector)[Idxs[0]] = Rhs;
return *this;
}

template <int IdxNum = getNumElements(),
EnableIfMultipleIndexes<IdxNum, bool> = true>
template <int IdxNum = size(), EnableIfMultipleIndexes<IdxNum, bool> = true>
SwizzleOp &operator=(const DataT &Rhs) {
std::array<int, IdxNum> Idxs{Indexes...};
for (auto Idx : Idxs) {
Expand All @@ -897,7 +895,7 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<
return *this;
}

template <int IdxNum = getNumElements(), typename = EnableIfOneIndex<IdxNum>>
template <int IdxNum = size(), typename = EnableIfOneIndex<IdxNum>>
SwizzleOp &operator=(DataT &&Rhs) {
std::array<int, IdxNum> Idxs{Indexes...};
(*m_Vector)[Idxs[0]] = Rhs;
Expand Down Expand Up @@ -1046,24 +1044,22 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<
return NewLHOp<RhsOperation, LShift, Indexes...>(m_Vector, *this, Rhs);
}

template <
typename T1, typename T2, typename T3, template <typename> class T4,
int... T5,
typename = typename std::enable_if_t<sizeof...(T5) == getNumElements()>>
template <typename T1, typename T2, typename T3, template <typename> class T4,
int... T5,
typename = typename std::enable_if_t<sizeof...(T5) == size()>>
SwizzleOp &operator=(const SwizzleOp<T1, T2, T3, T4, T5...> &Rhs) {
std::array<int, getNumElements()> Idxs{Indexes...};
std::array<int, size()> Idxs{Indexes...};
for (size_t I = 0; I < Idxs.size(); ++I) {
(*m_Vector)[Idxs[I]] = Rhs.getValue(I);
}
return *this;
}

template <
typename T1, typename T2, typename T3, template <typename> class T4,
int... T5,
typename = typename std::enable_if_t<sizeof...(T5) == getNumElements()>>
template <typename T1, typename T2, typename T3, template <typename> class T4,
int... T5,
typename = typename std::enable_if_t<sizeof...(T5) == size()>>
SwizzleOp &operator=(SwizzleOp<T1, T2, T3, T4, T5...> &&Rhs) {
std::array<int, getNumElements()> Idxs{Indexes...};
std::array<int, size()> Idxs{Indexes...};
for (size_t I = 0; I < Idxs.size(); ++I) {
(*m_Vector)[Idxs[I]] = Rhs.getValue(I);
}
Expand Down Expand Up @@ -1212,7 +1208,7 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<
vec<convertT, sizeof...(Indexes)> convert() const {
// First materialize the swizzle to vec_t and then apply convert() to it.
vec_t Tmp;
std::array<int, getNumElements()> Idxs{Indexes...};
std::array<int, size()> Idxs{Indexes...};
for (size_t I = 0; I < Idxs.size(); ++I) {
Tmp[I] = (*m_Vector)[Idxs[I]];
}
Expand Down Expand Up @@ -1251,21 +1247,21 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<
// or reading values from actual vector. Perform implicit type conversion when
// the number of elements == 1

template <int IdxNum = getNumElements()>
template <int IdxNum = size()>
CommonDataT getValue(EnableIfOneIndex<IdxNum, size_t> Index) const {
if (std::is_same_v<OperationCurrentT<DataT>, GetOp<DataT>>) {
std::array<int, getNumElements()> Idxs{Indexes...};
std::array<int, size()> Idxs{Indexes...};
return (*m_Vector)[Idxs[Index]];
}
auto Op = OperationCurrentT<CommonDataT>();
return Op(m_LeftOperation.getValue(Index),
m_RightOperation.getValue(Index));
}

template <int IdxNum = getNumElements()>
template <int IdxNum = size()>
DataT getValue(EnableIfMultipleIndexes<IdxNum, size_t> Index) const {
if (std::is_same_v<OperationCurrentT<DataT>, GetOp<DataT>>) {
std::array<int, getNumElements()> Idxs{Indexes...};
std::array<int, size()> Idxs{Indexes...};
return (*m_Vector)[Idxs[Index]];
}
auto Op = OperationCurrentT<DataT>();
Expand All @@ -1276,7 +1272,7 @@ class SwizzleOp : public detail::NamedSwizzlesMixinBoth<
template <template <typename> class Operation, typename RhsOperation>
void operatorHelper(const RhsOperation &Rhs) const {
Operation<DataT> Op;
std::array<int, getNumElements()> Idxs{Indexes...};
std::array<int, size()> Idxs{Indexes...};
for (size_t I = 0; I < Idxs.size(); ++I) {
DataT Res = Op((*m_Vector)[Idxs[I]], Rhs.getValue(I));
(*m_Vector)[Idxs[I]] = Res;
Expand Down
Loading