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

[SYCL] Make swizzle mutating operators const friends #13012

Merged
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
33 changes: 20 additions & 13 deletions sycl/include/sycl/vector_preview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,14 +856,21 @@ class SwizzleOp {
#error "Undefine __SYCL_OPASSIGN macro."
#endif
#define __SYCL_OPASSIGN(OPASSIGN, OP) \
SwizzleOp &operator OPASSIGN(const DataT & Rhs) { \
operatorHelper<OP>(vec_t(Rhs)); \
return *this; \
friend const SwizzleOp &operator OPASSIGN(const SwizzleOp & Lhs, \
const DataT & Rhs) { \
Lhs.operatorHelper<OP>(vec_t(Rhs)); \
return Lhs; \
} \
template <typename RhsOperation> \
SwizzleOp &operator OPASSIGN(const RhsOperation & Rhs) { \
operatorHelper<OP>(Rhs); \
return *this; \
friend const SwizzleOp &operator OPASSIGN(const SwizzleOp & Lhs, \
const RhsOperation & Rhs) { \
Lhs.operatorHelper<OP>(Rhs); \
return Lhs; \
} \
friend const SwizzleOp &operator OPASSIGN(const SwizzleOp & Lhs, \
const vec_t & Rhs) { \
Lhs.operatorHelper<OP>(Rhs); \
return Lhs; \
}

__SYCL_OPASSIGN(+=, std::plus)
Expand All @@ -882,13 +889,13 @@ class SwizzleOp {
#error "Undefine __SYCL_UOP macro"
#endif
#define __SYCL_UOP(UOP, OPASSIGN) \
SwizzleOp &operator UOP() { \
*this OPASSIGN static_cast<DataT>(1); \
return *this; \
friend const SwizzleOp &operator UOP(const SwizzleOp & sv) { \
sv OPASSIGN static_cast<DataT>(1); \
return sv; \
} \
vec_t operator UOP(int) { \
vec_t Ret = *this; \
*this OPASSIGN static_cast<DataT>(1); \
friend vec_t operator UOP(const SwizzleOp &sv, int) { \
vec_t Ret = sv; \
sv OPASSIGN static_cast<DataT>(1); \
return Ret; \
}

Expand Down Expand Up @@ -1429,7 +1436,7 @@ class SwizzleOp {
}

template <template <typename> class Operation, typename RhsOperation>
void operatorHelper(const RhsOperation &Rhs) {
void operatorHelper(const RhsOperation &Rhs) const {
Operation<DataT> Op;
std::array<int, getNumElements()> Idxs{Indexes...};
for (size_t I = 0; I < Idxs.size(); ++I) {
Expand Down
84 changes: 84 additions & 0 deletions sycl/test-e2e/Regression/swizzle_opassign.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// RUN: %{build} -o %t.out
// RUN: %{run} %t.out
// RUN: %if preview-breaking-changes-supported %{ %{build} -fpreview-breaking-changes -o %t2.out %}
// RUN: %if preview-breaking-changes-supported %{ %{run} %t2.out %}

// Tests that the mutating operators (+=, -=, ..., ++, --) on swizzles compile
// and correctly mutate the elements in the corresponding vector.

#include <sycl/detail/core.hpp>
#include <sycl/types.hpp>
#include <sycl/usm.hpp>

constexpr std::string_view OpNames[] = {
"+=", "-=", "*=", "/=", "%=", "&=", "|=",
"^=", "<<=", ">>=", "prefix ++", "prefix --", "postfix ++", "prefix ++"};
constexpr size_t NumOps = std::size(OpNames);

int main() {
sycl::queue Q;
bool Results[NumOps] = {false};

{
sycl::buffer<bool> ResultsBuff{Results, NumOps};

Q.submit([&](sycl::handler &CGH) {
sycl::accessor ResultsAcc{ResultsBuff, CGH, sycl::write_only};

CGH.single_task([=]() {
int I = 0;
#define TestCase(OP) \
{ \
sycl::vec<int, 4> VecVal{1, 2, 3, 4}; \
int ExpectedRes = VecVal[1] OP 2; \
ResultsAcc[I++] = (VecVal.swizzle<1>() OP## = 2)[0] == ExpectedRes && \
VecVal[1] == ExpectedRes; \
}
TestCase(+);
TestCase(-);
TestCase(*);
TestCase(/);
TestCase(%);
TestCase(&);
TestCase(|);
TestCase(^);
TestCase(<<);
TestCase(>>);
{
sycl::vec<int, 4> VecVal{1, 2, 3, 4};
int ExpectedRes = VecVal[1] + 1;
ResultsAcc[I++] = (++VecVal.swizzle<1>())[0] == ExpectedRes &&
VecVal[1] == ExpectedRes;
}
{
sycl::vec<int, 4> VecVal{1, 2, 3, 4};
int ExpectedRes = VecVal[1] - 1;
ResultsAcc[I++] = (--VecVal.swizzle<1>())[0] == ExpectedRes &&
VecVal[1] == ExpectedRes;
}
{
sycl::vec<int, 4> VecVal{1, 2, 3, 4};
int ExpectedRes = VecVal[1] + 1;
ResultsAcc[I++] = (VecVal.swizzle<1>()++)[0] == (ExpectedRes - 1) &&
VecVal[1] == ExpectedRes;
}
{
sycl::vec<int, 4> VecVal{1, 2, 3, 4};
int ExpectedRes = VecVal[1] - 1;
ResultsAcc[I++] = (VecVal.swizzle<1>()--)[0] == (ExpectedRes + 1) &&
VecVal[1] == ExpectedRes;
}
});
});
}

int Failures = 0;
for (size_t I = 0; I < NumOps; ++I) {
if (!Results[I]) {
std::cout << "Failed for " << OpNames[I] << std::endl;
++Failures;
}
}

return Failures;
}
Loading