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] Use C-arrays as storage in sycl::vec #17025

Open
wants to merge 1 commit into
base: sycl
Choose a base branch
from

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.

@@ -23,7 +22,6 @@ SYCL_EXTERNAL void foo(sycl::vec<bool, 16>) {}

// CHECK: 0 | class sycl::vec<_Bool, 16>
// ignore empty base classes
// CHECK: 0 | struct std::array<_Bool, 16> m_Data
// CHECK-NEXT: 0 | typename {{.+}}::_Type _M_elems
// CHECK: 0 | DataType m_Data
Copy link
Contributor

Choose a reason for hiding this comment

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

Although this does not change the size of the vec type, I worry that this is breaking ABI as the vec type crosses the library boundary. To understand why this could be an issue, imagine the following scenario:

Let this change be part of a version Y of the runtime and headers, which came after a version X without it. By ABI/API guarantees we make, programs compiled with X should be runable with Y. However, if such a program passes vec across the library boundary, the runtime library of Y will think the data representation inside the type is a C-style array, while it is actually a std::array.

Granted, it's likely that C-style arrays and std::array are the exact same layout, but making such assumptions is also potentially playing with fire.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

However, if such a program passes vec across the library boundary, the runtime library of Y will think the data representation inside the type is a C-style array, while it is actually a std::array.

Granted, it's likely that C-style arrays and std::array are the exact same layout, but making such assumptions is also potentially playing with fire.

Doesn't the spec guarantees that std::array and C-style arrays will have the same memory layout? C++ spec says (https://en.cppreference.com/w/cpp/container/array):

This (as in std::array) container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member.

If that's the case and a program passes sycl::vec across boundaries, what's the worse that can happen: we might treat std::array as a C-style array. But, looking at usages of m_Data in sycl::vec, we either access m_Data using [] operator, bitcast to vector_t, or memcpy into m_Data (change made in this PR). If std::array and C-style array share the same memory layout always, I think the above-mentioned operations on m_Data will produce the same results, even if we treat std::array as a C-style 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 agree, by the definition given by CPPReference it should (depending on how we should read "semantics") always have the same layout. That said, it gets awfully close to breaking strict-aliasing rules in a way you don't see every day.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. To be on a safer side, I've put these changes under preview-breaking flag.

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