Skip to content

[SYCL] Use C-arrays as storage in sycl::vec #17025

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
Feb 27, 2025

Conversation

uditagarwal97
Copy link
Contributor

@uditagarwal97 uditagarwal97 commented Feb 14, 2025

#14130 caused sycl::vec to use std::array as its underlying storage. However, operations on std::array may emit debug-mode-only functions, on which the device compiler may fail.

This PR replaces std::array with C-style arrays as storage in sycl::vec.

@uditagarwal97
Copy link
Contributor Author

@aelovikov-intel @steffenlarsen ping. Please approve if changes looks good.

@@ -930,7 +933,11 @@ vec<convertT, NumElements> vec<DataT, NumElements>::convert() const {
auto val =
detail::convertImpl<T, R, roundingMode, NumElements, OpenCLVecT,
OpenCLVecR>(bit_cast<OpenCLVecT>(*this));
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
sycl::detail::memcpy_no_adl(&Result.m_Data, &val, sizeof(Result));
Copy link
Contributor

Choose a reason for hiding this comment

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

Would there be a problem in just switching to memcpy_no_adl for all paths?

Copy link
Contributor Author

@uditagarwal97 uditagarwal97 Feb 25, 2025

Choose a reason for hiding this comment

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

We can, but I don't see any improvement with that. If we were to do that, we would have something like the following which doesn't simplify the code.

#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
      sycl::detail::memcpy_no_adl(&Result.m_Data, &val, sizeof(Result)); // For C-array
#else
     sycl::detail::memcpy_no_adl(Result.m_Data.data(), &val, sizeof(Result)); // For std::array
#endif

We couldn't have done sycl::detail::memcpy_no_adl(&Result.m_Data, &val, sizeof(Result)); for std::array as base address of std::array is not guaranteed by the standard to be same as that of underlying C-array: https://stackoverflow.com/questions/69500721/is-the-address-of-a-stdarray-guaranteed-the-same-as-its-data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On a second though, as SO answer mentioned, most popular implementations does guarantee std::array and underlying C-array to have same address. So, I can do something like:

static_assert(sizeof(Result.m_Data) == sizeof(DataT[AdjustedNum]))
sycl::detail::memcpy_no_adl(&Result.m_Data, &val, sizeof(Result)); // for both C-array and std::array

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 having the static_assert should be fine. We only risk this hitting weird implementations until preview changes are promoted, so I think the risk is low while the reduced complexity is nice. 😄

@@ -287,7 +291,12 @@ class __SYCL_EBO vec
typename vector_t_ = vector_t,
typename = typename std::enable_if_t<std::is_same_v<vector_t_, vector_t>>>
constexpr vec(vector_t_ openclVector) {
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
sycl::detail::memcpy_no_adl(&this->m_Data, &openclVector,
sizeof(openclVector));
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question as above.

@aelovikov-intel
Copy link
Contributor

@steffenlarsen , please approve this. You were the one pushing to do this under preview, so you should be approving it.

Copy link
Contributor

@steffenlarsen steffenlarsen left a comment

Choose a reason for hiding this comment

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

Sorry, I hadn't noticed the recent changes. Looks good!

@steffenlarsen steffenlarsen merged commit 47404a4 into intel:sycl Feb 27, 2025
20 checks passed
AlexeySachkov added a commit to AlexeySachkov/llvm that referenced this pull request Mar 26, 2025
The problem with using `std::array` in `sycl::vec` is that we cannot
compile it in some environments (namely, Windows) because the former may
use something that is illegal in SYCL device code.

intel#17025 fixed that, but only did so under preview breaking
changes mode, which does not satisfy some of our customers immediately.

This PR introduces two main changes:
- it allows to opt-in for new behavior through passing
  `-D__SYCL_USE_NEW_VEC_IMPL=1` macro without using
  `-fpreview-breaking-changes` flag. That allows for a more gradual
  opt-in from customers who are interested in this fix
- it switches the imlpementation to use the new approach with C-style
  arrays if their size and alignment is the same as for the
  corresponding `std::array` - in that case their memory layout is
  expected to be absolutely the same and therefore it should be safe to
  use the new approach without fear of some ABI incompatibilities. This
  allows for customers to benefit from the fix without specifying any
  extra macro (which should be the case for the most common platforms
  out there)
AlexeySachkov added a commit that referenced this pull request Mar 27, 2025
The problem with using `std::array` in `sycl::vec` is that we cannot
compile it in some environments (namely, Windows) because the former may
use something that is illegal in SYCL device code.

#17025 fixed that, but only did so under preview breaking
changes mode, which does not satisfy some of our customers immediately.

This PR introduces two main changes:
- it allows to opt-in for new behavior through passing
`-D__SYCL_USE_NEW_VEC_IMPL=1` macro without using
`-fpreview-breaking-changes` flag. That allows for a more gradual opt-in
from customers who are interested in this fix
- it switches the imlpementation to use the new approach with C-style
arrays if their size and alignment is the same as for the corresponding
`std::array` - in that case their memory layout is expected to be
absolutely the same and therefore it should be safe to use the new
approach without fear of some ABI incompatibilities. This allows for
customers to benefit from the fix without specifying any extra macro
(which should be the case for the most common platforms out there)
AlexeySachkov added a commit to AlexeySachkov/llvm that referenced this pull request Mar 27, 2025
The problem with using `std::array` in `sycl::vec` is that we cannot
compile it in some environments (namely, Windows) because the former may
use something that is illegal in SYCL device code.

intel#17025 fixed that, but only did so under preview breaking
changes mode, which does not satisfy some of our customers immediately.

This PR introduces two main changes:
- it allows to opt-in for new behavior through passing
`-D__SYCL_USE_NEW_VEC_IMPL=1` macro without using
`-fpreview-breaking-changes` flag. That allows for a more gradual opt-in
from customers who are interested in this fix
- it switches the imlpementation to use the new approach with C-style
arrays if their size and alignment is the same as for the corresponding
`std::array` - in that case their memory layout is expected to be
absolutely the same and therefore it should be safe to use the new
approach without fear of some ABI incompatibilities. This allows for
customers to benefit from the fix without specifying any extra macro
(which should be the case for the most common platforms out there)

This is a cherry-pick of intel#17656
AlexeySachkov added a commit that referenced this pull request Mar 31, 2025
…ses" to sycl-rel-6_0_0 (#17697)

This is a cherry-pick of #17656 +
changes required to resolve merge conflicts.

--------------------------------------

The problem with using std::array in sycl::vec is that we cannot compile
it in some environments (namely, Windows) because the former may use
something that is illegal in SYCL device code.

#17025 fixed that, but only did so
under preview breaking changes mode, which does not satisfy some of our
customers immediately.

This PR introduces two main changes:

it allows to opt-in for new behavior through passing
-D__SYCL_USE_NEW_VEC_IMPL=1 macro without using
-fpreview-breaking-changes flag. That allows for a more gradual opt-in
from customers who are interested in this fix
it switches the imlpementation to use the new approach with C-style
arrays if their size and alignment is the same as for the corresponding
std::array - in that case their memory layout is expected to be
absolutely the same and therefore it should be safe to use the new
approach without fear of some ABI incompatibilities. This allows for
customers to benefit from the fix without specifying any extra macro
(which should be the case for the most common platforms out there)

---------

Co-authored-by: Alexey Sachkov <alexey.sachkov@intel.com>
KornevNikita pushed a commit that referenced this pull request Apr 2, 2025
The problem with using `std::array` in `sycl::vec` is that we cannot
compile it in some environments (namely, Windows) because the former may
use something that is illegal in SYCL device code.

#17025 fixed that, but only did so under preview breaking
changes mode, which does not satisfy some of our customers immediately.

This PR introduces two main changes:
- it allows to opt-in for new behavior through passing
`-D__SYCL_USE_NEW_VEC_IMPL=1` macro without using
`-fpreview-breaking-changes` flag. That allows for a more gradual opt-in
from customers who are interested in this fix
- it switches the imlpementation to use the new approach with C-style
arrays if their size and alignment is the same as for the corresponding
`std::array` - in that case their memory layout is expected to be
absolutely the same and therefore it should be safe to use the new
approach without fear of some ABI incompatibilities. This allows for
customers to benefit from the fix without specifying any extra macro
(which should be the case for the most common platforms out there)
AlexeySachkov added a commit that referenced this pull request Apr 3, 2025
### Cherry-pick of: #17656 and some
changes to resolve merge conflicts & make it work.

The problem with using `std::array` in `sycl::vec` is that we cannot
compile it in some environments (namely, Windows) because the former may
use something that is illegal in SYCL device code.

#17025 fixed that, but only did so under preview breaking
changes mode, which does not satisfy some of our customers immediately.

This PR introduces two main changes:
- it allows to opt-in for new behavior through passing
`-D__SYCL_USE_NEW_VEC_IMPL=1` macro without using
`-fpreview-breaking-changes` flag. That allows for a more gradual opt-in
from customers who are interested in this fix
- it switches the imlpementation to use the new approach with C-style
arrays if their size and alignment is the same as for the corresponding
`std::array` - in that case their memory layout is expected to be
absolutely the same and therefore it should be safe to use the new
approach without fear of some ABI incompatibilities. This allows for
customers to benefit from the fix without specifying any extra macro
(which should be the case for the most common platforms out there)

Co-authored-by: Alexey Sachkov <alexey.sachkov@intel.com>
KornevNikita pushed a commit that referenced this pull request May 27, 2025
The problem with using `std::array` in `sycl::vec` is that we cannot
compile it in some environments (namely, Windows) because the former may
use something that is illegal in SYCL device code.

#17025 fixed that, but only did so under preview breaking
changes mode, which does not satisfy some of our customers immediately.

This PR introduces two main changes:
- it allows to opt-in for new behavior through passing
`-D__SYCL_USE_NEW_VEC_IMPL=1` macro without using
`-fpreview-breaking-changes` flag. That allows for a more gradual opt-in
from customers who are interested in this fix
- it switches the imlpementation to use the new approach with C-style
arrays if their size and alignment is the same as for the corresponding
`std::array` - in that case their memory layout is expected to be
absolutely the same and therefore it should be safe to use the new
approach without fear of some ABI incompatibilities. This allows for
customers to benefit from the fix without specifying any extra macro
(which should be the case for the most common platforms out there)
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.

3 participants