Skip to content

Commit b489479

Browse files
authored
[SYCL] Ensure correct access mode in handler::copy (#3109) (#3111)
This patch introduces compile-time checks to cl::sycl::handler::copy functions that ensure the correctness of accessors' access mode (section 4.8.6 of SYCL-1.2.1 specification).
1 parent 32ca1b9 commit b489479

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

sycl/include/CL/sycl/handler.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,19 @@ class __SYCL_EXPORT handler {
723723
return isConstOrGlobal(AccessTarget) || isImageOrImageArray(AccessTarget);
724724
}
725725

726+
constexpr static bool isValidModeForSourceAccessor(access::mode AccessMode) {
727+
return AccessMode == access::mode::read ||
728+
AccessMode == access::mode::read_write;
729+
}
730+
731+
constexpr static bool
732+
isValidModeForDestinationAccessor(access::mode AccessMode) {
733+
return AccessMode == access::mode::write ||
734+
AccessMode == access::mode::read_write ||
735+
AccessMode == access::mode::discard_write ||
736+
AccessMode == access::mode::discard_read_write;
737+
}
738+
726739
/// Defines and invokes a SYCL kernel function for the specified range.
727740
///
728741
/// The SYCL kernel function is defined as a lambda function or a named
@@ -1674,6 +1687,8 @@ class __SYCL_EXPORT handler {
16741687
throwIfActionIsCreated();
16751688
static_assert(isValidTargetForExplicitOp(AccessTarget),
16761689
"Invalid accessor target for the copy method.");
1690+
static_assert(isValidModeForSourceAccessor(AccessMode),
1691+
"Invalid accessor mode for the copy method.");
16771692
// Make sure data shared_ptr points to is not released until we finish
16781693
// work with it.
16791694
MSharedPtrStorage.push_back(Dst);
@@ -1697,6 +1712,8 @@ class __SYCL_EXPORT handler {
16971712
throwIfActionIsCreated();
16981713
static_assert(isValidTargetForExplicitOp(AccessTarget),
16991714
"Invalid accessor target for the copy method.");
1715+
static_assert(isValidModeForDestinationAccessor(AccessMode),
1716+
"Invalid accessor mode for the copy method.");
17001717
// Make sure data shared_ptr points to is not released until we finish
17011718
// work with it.
17021719
MSharedPtrStorage.push_back(Src);
@@ -1719,6 +1736,8 @@ class __SYCL_EXPORT handler {
17191736
throwIfActionIsCreated();
17201737
static_assert(isValidTargetForExplicitOp(AccessTarget),
17211738
"Invalid accessor target for the copy method.");
1739+
static_assert(isValidModeForSourceAccessor(AccessMode),
1740+
"Invalid accessor mode for the copy method.");
17221741
#ifndef __SYCL_DEVICE_ONLY__
17231742
if (MIsHost) {
17241743
// TODO: Temporary implementation for host. Should be handled by memory
@@ -1756,6 +1775,8 @@ class __SYCL_EXPORT handler {
17561775
throwIfActionIsCreated();
17571776
static_assert(isValidTargetForExplicitOp(AccessTarget),
17581777
"Invalid accessor target for the copy method.");
1778+
static_assert(isValidModeForDestinationAccessor(AccessMode),
1779+
"Invalid accessor mode for the copy method.");
17591780
#ifndef __SYCL_DEVICE_ONLY__
17601781
if (MIsHost) {
17611782
// TODO: Temporary implementation for host. Should be handled by memory
@@ -1801,6 +1822,10 @@ class __SYCL_EXPORT handler {
18011822
"Invalid source accessor target for the copy method.");
18021823
static_assert(isValidTargetForExplicitOp(AccessTarget_Dst),
18031824
"Invalid destination accessor target for the copy method.");
1825+
static_assert(isValidModeForSourceAccessor(AccessMode_Src),
1826+
"Invalid source accessor mode for the copy method.");
1827+
static_assert(isValidModeForDestinationAccessor(AccessMode_Dst),
1828+
"Invalid destination accessor mode for the copy method.");
18041829
assert(Dst.get_size() >= Src.get_size() &&
18051830
"The destination accessor does not fit the copied memory.");
18061831
if (copyAccToAccHelper(Src, Dst))

0 commit comments

Comments
 (0)