Skip to content

Commit 826c569

Browse files
[SYCL] Fix vec class alignment on windows platform (#4953)
Currently the sycl::vec type can be copied in the way which doesn't preserve the default alignment on windows. This can causes crashes since the sycl:;vec code expects the vector to be aligned and uses vector instructions. We used default alignment because we cannot set correct alignment in all cases. The patch adds alignment of vector types, if alignment required is larger than 64, it is limited to 64.
1 parent 23ca24b commit 826c569

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

sycl/include/CL/sycl/types.hpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,13 @@ using vec_data_t = typename detail::vec_helper<T>::RetType;
542542
// For information on calling conventions for x64 processors, see
543543
// Calling Convention
544544
// (https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention).
545-
#pragma message ("Alignment of class vec is not in accordance with SYCL \
545+
#pragma message("Alignment of class vec is not in accordance with SYCL \
546546
specification requirements, a limitation of the MSVC compiler(Error C2719).\
547-
Applied default alignment.")
548-
#define __SYCL_ALIGNAS(x)
547+
Requested alignment applied, limited at 64.")
548+
#define __SYCL_ALIGNED_VAR(type, x, var) \
549+
type __declspec(align((x < 64) ? x : 64)) var
549550
#else
550-
#define __SYCL_ALIGNAS(N) alignas(N)
551+
#define __SYCL_ALIGNED_VAR(type, x, var) alignas(x) type var
551552
#endif
552553

553554
/// Provides a cross-patform vector class template that works efficiently on
@@ -1363,12 +1364,14 @@ template <typename Type, int NumElements> class vec {
13631364
}
13641365

13651366
// fields
1366-
// Used "__SYCL_ALIGNAS" instead "alignas" to handle MSVC compiler.
1367+
// Used "__SYCL_ALIGNED_VAR" instead "alignas" to handle MSVC compiler.
13671368
// For MSVC compiler max alignment is 64, e.g. vec<double, 16> required
13681369
// alignment of 128 and MSVC compiler cann't align a parameter with requested
1369-
// alignment of 128.
1370-
__SYCL_ALIGNAS((detail::vector_alignment<DataT, NumElements>::value))
1371-
DataType m_Data;
1370+
// alignment of 128. For alignment request larger than 64, 64-alignment
1371+
// is applied
1372+
__SYCL_ALIGNED_VAR(DataType,
1373+
(detail::vector_alignment<DataT, NumElements>::value),
1374+
m_Data);
13721375

13731376
// friends
13741377
template <typename T1, typename T2, typename T3, template <typename> class T4,
@@ -2497,4 +2500,4 @@ struct CheckDeviceCopyable<
24972500
} // namespace sycl
24982501
} // __SYCL_INLINE_NAMESPACE(cl)
24992502

2500-
#undef __SYCL_ALIGNAS
2503+
#undef __SYCL_ALIGNED_VAR

sycl/test/basic_tests/stdcpp_compat.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
// warning_extension-warning@* 0-1 {{#warning is a language extension}}
1717
//
1818
// The next warning is emitted for windows only
19-
// expected-warning@* 0-1 {{Alignment of class vec is not in accordance with SYCL specification requirements, a limitation of the MSVC compiler(Error C2719).Applied default alignment.}}
20-
19+
// expected-warning@* 0-1 {{Alignment of class vec is not in accordance with SYCL specification requirements, a limitation of the MSVC compiler(Error C2719).Requested alignment applied, limited at 64.}}
2120

2221
class KernelName1;
2322

0 commit comments

Comments
 (0)