Skip to content

Commit 31d778d

Browse files
committed
[SYCL][NFC] Unify vec implementation
Unified code related to `vec` alignment to reduce amount of macro-based branching we have for preview breaking changes mode.
1 parent 728b132 commit 31d778d

File tree

2 files changed

+32
-57
lines changed

2 files changed

+32
-57
lines changed

sycl/include/sycl/detail/vector_traits.hpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#pragma once
1010

1111
#include <algorithm> // for std::min and vs2017 win
12+
#include <limits> // for numeric_limits
1213
#include <type_traits> // for integral_constant, conditional_t, remove_cv_t
1314

1415
namespace sycl {
@@ -17,9 +18,36 @@ namespace detail {
1718

1819
// 4.10.2.6 Memory layout and alignment
1920
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
20-
// due to MSVC the maximum alignment for sycl::vec is 64 and this proposed
21-
// change is being brought to the spec committee.
21+
// SYCL 2020 vec alignment requirements have been relaxed in
22+
// KhronosGroup/SYCL-Docs#448. New specification wording only guarantees 64-byte
23+
// alignment of vec class and we leverage this here to avoid dealing with MSVC
24+
// limitations (see below).
2225
constexpr size_t MaxVecAlignment = 64;
26+
#else
27+
// This version is preserved to maintain API/ABI compatibility with older
28+
// releases.
29+
// FIXME: drop this branch once API/ABI break is allowed
30+
31+
#if defined(_WIN32) && (_MSC_VER)
32+
// MSVC Compiler doesn't allow using of function arguments with alignment
33+
// requirements. MSVC Compiler Error C2719: 'parameter': formal parameter with
34+
// __declspec(align('#')) won't be aligned. The align __declspec modifier
35+
// is not permitted on function parameters. Function parameter alignment
36+
// is controlled by the calling convention used.
37+
// For more information, see Calling Conventions
38+
// (https://docs.microsoft.com/en-us/cpp/cpp/calling-conventions).
39+
// For information on calling conventions for x64 processors, see
40+
// Calling Convention
41+
// (https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention).
42+
constexpr size_t MaxVecAlignment = 64;
43+
#else
44+
// To match ABI of previos releases, we don't impose any restrictions on vec
45+
// alignment on Linux
46+
constexpr size_t MaxVecAlignment = std::numeric_limits<size_t>::max();
47+
#endif
48+
49+
#endif // __INTEL_PREVIEW_BREAKING_CHANGES
50+
2351
template <typename T, size_t N>
2452
struct vector_alignment_impl
2553
: std::conditional_t<
@@ -29,12 +57,6 @@ struct vector_alignment_impl
2957
std::integral_constant<size_t,
3058
(std::min)(sizeof(T) * N, MaxVecAlignment)>> {
3159
};
32-
#else
33-
template <typename T, size_t N>
34-
struct vector_alignment_impl
35-
: std::conditional_t<N == 3, std::integral_constant<int, sizeof(T) * 4>,
36-
std::integral_constant<int, sizeof(T) * N>> {};
37-
#endif
3860

3961
template <typename T, size_t N>
4062
struct vector_alignment

sycl/include/sycl/types.hpp

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -272,30 +272,6 @@ template <typename T> using vec_data = detail::vec_helper<T>;
272272
template <typename T>
273273
using vec_data_t = typename detail::vec_helper<T>::RetType;
274274

275-
#if !defined(__INTEL_PREVIEW_BREAKING_CHANGES)
276-
277-
#if defined(_WIN32) && (_MSC_VER)
278-
// MSVC Compiler doesn't allow using of function arguments with alignment
279-
// requirements. MSVC Compiler Error C2719: 'parameter': formal parameter with
280-
// __declspec(align('#')) won't be aligned. The align __declspec modifier
281-
// is not permitted on function parameters. Function parameter alignment
282-
// is controlled by the calling convention used.
283-
// For more information, see Calling Conventions
284-
// (https://docs.microsoft.com/en-us/cpp/cpp/calling-conventions).
285-
// For information on calling conventions for x64 processors, see
286-
// Calling Convention
287-
// (https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention).
288-
#pragma message("Alignment of class vec is not in accordance with SYCL \
289-
specification requirements, a limitation of the MSVC compiler(Error C2719).\
290-
Requested alignment applied, limited at 64.")
291-
#define __SYCL_ALIGNED_VAR(type, x, var) \
292-
type __declspec(align((x < 64) ? x : 64)) var
293-
#else
294-
#define __SYCL_ALIGNED_VAR(type, x, var) alignas(x) type var
295-
#endif
296-
297-
#endif //! defined(__INTEL_PREVIEW_BREAKING_CHANGES)
298-
299275
/// Provides a cross-patform vector class template that works efficiently on
300276
/// SYCL devices as well as in host C++ code.
301277
///
@@ -1458,31 +1434,12 @@ template <typename Type, int NumElements> class vec {
14581434
return (NumElements == 1) ? getValue(Index, 0) : getValue(Index, 0.f);
14591435
}
14601436

1461-
#if defined(__INTEL_PREVIEW_BREAKING_CHANGES)
1462-
14631437
// fields
14641438

1465-
// Alignment is the same as size, to a maximum size of 64.
1466-
// detail::vector_alignment will return that value.
1439+
// Alignment is the same as size, to a maximum size of 64 (with some
1440+
// exceptions, see detail::vector_alignment).
14671441
alignas(detail::vector_alignment<DataT, NumElements>::value) DataType m_Data;
14681442

1469-
#endif // defined(__INTEL_PREVIEW_BREAKING_CHANGES)
1470-
1471-
#if !defined(__INTEL_PREVIEW_BREAKING_CHANGES)
1472-
1473-
// fields
1474-
1475-
// Used "__SYCL_ALIGNED_VAR" instead "alignas" to handle MSVC compiler.
1476-
// For MSVC compiler max alignment is 64, e.g. vec<double, 16> required
1477-
// alignment of 128 and MSVC compiler cann't align a parameter with requested
1478-
// alignment of 128. For alignment request larger than 64, 64-alignment
1479-
// is applied
1480-
__SYCL_ALIGNED_VAR(DataType,
1481-
(detail::vector_alignment<DataT, NumElements>::value),
1482-
m_Data);
1483-
1484-
#endif // !defined(__INTEL_PREVIEW_BREAKING_CHANGES)
1485-
14861443
// friends
14871444
template <typename T1, typename T2, typename T3, template <typename> class T4,
14881445
int... T5>
@@ -2554,7 +2511,3 @@ struct CheckDeviceCopyable<
25542511

25552512
} // namespace _V1
25562513
} // namespace sycl
2557-
2558-
#if !defined(__INTEL_PREVIEW_BREAKING_CHANGES)
2559-
#undef __SYCL_ALIGNED_VAR
2560-
#endif // !defined(__INTEL_PREVIEW_BREAKING_CHANGES)

0 commit comments

Comments
 (0)