Skip to content

Conversation

maarquitos14
Copy link
Contributor

Our implementation of sycl::vec<bool, N> has signed char rather than bool for the underlying data structure. This causes that our underlying implementation can have values other than 0/1, which produces wrong results for some operations. This PR fixes that by doing appropriate casts and data conversions when dealing with bool type and operations that can lead to results other than 0/1.

Our implementation of sycl::vec<bool, N> has `signed char` rather than
`bool`  for the underlying data structure. This causes that our underlying
implementation can have values other than `0/1`, which produces wrong
results for some operations. This commit fixes that by doing appropriate
casts and data conversions when dealing with bool type and operations that
can lead to results other than `0/1`.

Signed-off-by: Maronas, Marcos <marcos.maronas@intel.com>
@maarquitos14 maarquitos14 requested a review from a team as a code owner April 4, 2023 10:57
@maarquitos14 maarquitos14 requested a review from bso-intel April 4, 2023 10:57
@maarquitos14 maarquitos14 changed the title [SYCL]Fixes incorrect behaviors in some operations using sycl::vec<bool, N> [SYCL]Fixes incorrect behaviors in some operations using sycl::vec<bool, N> Apr 4, 2023
@maarquitos14 maarquitos14 changed the title [SYCL]Fixes incorrect behaviors in some operations using sycl::vec<bool, N> [SYCL]Fix incorrect behaviors in some operations using sycl::vec<bool, N> Apr 4, 2023
@maarquitos14 maarquitos14 temporarily deployed to aws April 4, 2023 11:26 — with GitHub Actions Inactive
@maarquitos14 maarquitos14 temporarily deployed to aws April 4, 2023 11:34 — with GitHub Actions Inactive
@@ -778,7 +792,8 @@ template <typename Type, int NumElements> class vec {

template <typename Ty = DataT>
explicit constexpr vec(const EnableIfNotHostHalf<Ty> &arg)
: m_Data{(DataType)vec_data<Ty>::get(arg)} {}
: m_Data{DataType(vec_data<Ty>::get(arg))} {
}
Copy link
Contributor

@jzc jzc Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will still initialize a m_Data with -1 if given true as input - the argument given to initialize a bool ext_vector should be casted to something other than bool to avoid the special behavior of splatting a bool.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the operator= overload right below this function will still have the same bool splatting behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will still initialize a m_Data with -1 if given true as input - the argument given to initialize a bool ext_vector should be casted to something the special behavior of splatting a bool.

It looks like this change will first cast the bool value to the underlying std::intN_t and then splat. That should prevent the vector from being initialized to -1.

@maarquitos14, can you add a test case to show that the sycl::vec<bool, N> operations are correct with this change?

Copy link
Contributor

@jzc jzc Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sycl::vec<bool, N>, If Ty = bool, then the following declarations suggest that vec_data<Ty>::get is a bool:

template <typename T> T vec_helper<T>::get();
template <typename T> using vec_data = detail::vec_helper<T>;

Copy link
Contributor Author

@maarquitos14 maarquitos14 Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maarquitos14, can you add a test case to show that the sycl::vec<bool, N> operations are correct with this change?

I have just added a file with many tests, but please, feel free to suggest new ones if you think they should be added.

Also, the operator= overload right below this function will still have the same bool splatting behavior.

I believe it's not necessary to change the operator= since the rhs should have correct values already. I might be wrong, though, so if you find a case where this could be an issue, please, share it with me so that I can add it to the test file and also update the code accordingly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that makes sense. And I realized I didn't see/forgot about the specialization added for vec_helper so that's why I thought this wouldn't work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that makes sense. And I realized I didn't see/forgot about the specialization added for vec_helper so that's why I thought this wouldn't work.

Ah, right, that is indeed the key to make this work.

Signed-off-by: Maronas, Marcos <marcos.maronas@intel.com>
@maarquitos14 maarquitos14 temporarily deployed to aws April 5, 2023 15:07 — with GitHub Actions Inactive
@maarquitos14 maarquitos14 temporarily deployed to aws April 5, 2023 15:28 — with GitHub Actions Inactive
@maarquitos14 maarquitos14 temporarily deployed to aws April 6, 2023 08:53 — with GitHub Actions Inactive
@maarquitos14 maarquitos14 temporarily deployed to aws April 6, 2023 09:00 — with GitHub Actions Inactive
Signed-off-by: Maronas, Marcos <marcos.maronas@intel.com>
@maarquitos14 maarquitos14 temporarily deployed to aws April 6, 2023 09:47 — with GitHub Actions Inactive
@maarquitos14 maarquitos14 temporarily deployed to aws April 6, 2023 10:18 — with GitHub Actions Inactive
@maarquitos14
Copy link
Contributor Author

Friendly ping @bso-intel @intel/llvm-reviewers-runtime.

@steffenlarsen steffenlarsen merged commit 61f6ce4 into intel:sycl Apr 11, 2023
0x12CC added a commit to 0x12CC/llvm that referenced this pull request Apr 11, 2023
Update a `sycl::vec` constructor to ensure that element values are first
converted to `DataT` before being stored as `vec_data_t<DataT>`. Add a
test case that uses the updated constructor. Fixes bug from intel#8942.

Signed-off-by: Michael Aziz <michael.aziz@intel.com>
steffenlarsen pushed a commit that referenced this pull request Apr 12, 2023
Update a `sycl::vec` constructor to ensure that element values are first
converted to `DataT` before being stored as `vec_data_t<DataT>`. Add a
test case that uses the updated constructor. Fixes bug from #8942.

Signed-off-by: Michael Aziz <michael.aziz@intel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants