Skip to content

Commit 270e78d

Browse files
[SYCL] Ensure correct sycl::errc for SYCL2020 (#5582)
The SYCL2020 specification has quite a few errc values it expects thrown exceptions to have. Most of the existing ones are fine, but there are several cases that need updating. After this there are some other changes that will need to be done for errc conformance, but those changes are slightly more involved. They will be done in a follow up PR s. The tests were updated as well. intel/llvm-test-suite#857 has the matching changes.
1 parent bb0261c commit 270e78d

File tree

13 files changed

+144
-72
lines changed

13 files changed

+144
-72
lines changed

sycl/include/CL/sycl/accessor.hpp

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ class __SYCL_SPECIAL_CLASS accessor :
10231023
getAdjustedMode(PropertyList),
10241024
detail::getSyclObjImpl(BufferRef).get(), AdjustedDim, sizeof(DataT),
10251025
BufferRef.OffsetInBytes, BufferRef.IsSubBuffer) {
1026-
checkDeviceAccessorBufferSize(BufferRef.size());
1026+
preScreenAccessor(BufferRef.size(), PropertyList);
10271027
if (!IsPlaceH)
10281028
addHostAccessorAndWait(AccessorBaseHost::impl.get());
10291029
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
@@ -1054,7 +1054,7 @@ class __SYCL_SPECIAL_CLASS accessor :
10541054
getAdjustedMode(PropertyList),
10551055
detail::getSyclObjImpl(BufferRef).get(), AdjustedDim, sizeof(DataT),
10561056
BufferRef.OffsetInBytes, BufferRef.IsSubBuffer) {
1057-
checkDeviceAccessorBufferSize(BufferRef.size());
1057+
preScreenAccessor(BufferRef.size(), PropertyList);
10581058
if (!IsPlaceH)
10591059
addHostAccessorAndWait(AccessorBaseHost::impl.get());
10601060
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
@@ -1084,7 +1084,7 @@ class __SYCL_SPECIAL_CLASS accessor :
10841084
getAdjustedMode(PropertyList),
10851085
detail::getSyclObjImpl(BufferRef).get(), Dimensions, sizeof(DataT),
10861086
BufferRef.OffsetInBytes, BufferRef.IsSubBuffer) {
1087-
checkDeviceAccessorBufferSize(BufferRef.size());
1087+
preScreenAccessor(BufferRef.size(), PropertyList);
10881088
detail::associateWithHandler(CommandGroupHandler, this, AccessTarget);
10891089
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
10901090
detail::AccessorBaseHost::impl.get(),
@@ -1115,7 +1115,7 @@ class __SYCL_SPECIAL_CLASS accessor :
11151115
getAdjustedMode(PropertyList),
11161116
detail::getSyclObjImpl(BufferRef).get(), Dimensions, sizeof(DataT),
11171117
BufferRef.OffsetInBytes, BufferRef.IsSubBuffer) {
1118-
checkDeviceAccessorBufferSize(BufferRef.size());
1118+
preScreenAccessor(BufferRef.size(), PropertyList);
11191119
detail::associateWithHandler(CommandGroupHandler, this, AccessTarget);
11201120
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
11211121
detail::AccessorBaseHost::impl.get(),
@@ -1145,7 +1145,7 @@ class __SYCL_SPECIAL_CLASS accessor :
11451145
getAdjustedMode(PropertyList),
11461146
detail::getSyclObjImpl(BufferRef).get(), Dimensions, sizeof(DataT),
11471147
BufferRef.OffsetInBytes, BufferRef.IsSubBuffer) {
1148-
checkDeviceAccessorBufferSize(BufferRef.size());
1148+
preScreenAccessor(BufferRef.size(), PropertyList);
11491149
if (!IsPlaceH)
11501150
addHostAccessorAndWait(AccessorBaseHost::impl.get());
11511151
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
@@ -1178,7 +1178,7 @@ class __SYCL_SPECIAL_CLASS accessor :
11781178
getAdjustedMode(PropertyList),
11791179
detail::getSyclObjImpl(BufferRef).get(), Dimensions, sizeof(DataT),
11801180
BufferRef.OffsetInBytes, BufferRef.IsSubBuffer) {
1181-
checkDeviceAccessorBufferSize(BufferRef.size());
1181+
preScreenAccessor(BufferRef.size(), PropertyList);
11821182
if (!IsPlaceH)
11831183
addHostAccessorAndWait(AccessorBaseHost::impl.get());
11841184
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
@@ -1237,7 +1237,7 @@ class __SYCL_SPECIAL_CLASS accessor :
12371237
getAdjustedMode(PropertyList),
12381238
detail::getSyclObjImpl(BufferRef).get(), Dimensions, sizeof(DataT),
12391239
BufferRef.OffsetInBytes, BufferRef.IsSubBuffer) {
1240-
checkDeviceAccessorBufferSize(BufferRef.size());
1240+
preScreenAccessor(BufferRef.size(), PropertyList);
12411241
detail::associateWithHandler(CommandGroupHandler, this, AccessTarget);
12421242
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
12431243
detail::AccessorBaseHost::impl.get(),
@@ -1269,7 +1269,7 @@ class __SYCL_SPECIAL_CLASS accessor :
12691269
getAdjustedMode(PropertyList),
12701270
detail::getSyclObjImpl(BufferRef).get(), Dimensions, sizeof(DataT),
12711271
BufferRef.OffsetInBytes, BufferRef.IsSubBuffer) {
1272-
checkDeviceAccessorBufferSize(BufferRef.size());
1272+
preScreenAccessor(BufferRef.size(), PropertyList);
12731273
detail::associateWithHandler(CommandGroupHandler, this, AccessTarget);
12741274
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
12751275
detail::AccessorBaseHost::impl.get(),
@@ -1443,7 +1443,14 @@ class __SYCL_SPECIAL_CLASS accessor :
14431443
detail::getSyclObjImpl(BufferRef).get(), Dimensions,
14441444
sizeof(DataT), BufferRef.OffsetInBytes,
14451445
BufferRef.IsSubBuffer) {
1446-
checkDeviceAccessorBufferSize(BufferRef.size());
1446+
preScreenAccessor(BufferRef.size(), PropertyList);
1447+
if (BufferRef.isOutOfBounds(AccessOffset, AccessRange,
1448+
BufferRef.get_range()))
1449+
throw sycl::invalid_object_error(
1450+
"accessor with requested offset and range would exceed the bounds of "
1451+
"the buffer",
1452+
PI_INVALID_VALUE);
1453+
14471454
if (!IsPlaceH)
14481455
addHostAccessorAndWait(AccessorBaseHost::impl.get());
14491456
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
@@ -1477,7 +1484,14 @@ class __SYCL_SPECIAL_CLASS accessor :
14771484
detail::getSyclObjImpl(BufferRef).get(), Dimensions,
14781485
sizeof(DataT), BufferRef.OffsetInBytes,
14791486
BufferRef.IsSubBuffer) {
1480-
checkDeviceAccessorBufferSize(BufferRef.size());
1487+
preScreenAccessor(BufferRef.size(), PropertyList);
1488+
if (BufferRef.isOutOfBounds(AccessOffset, AccessRange,
1489+
BufferRef.get_range()))
1490+
throw sycl::invalid_object_error(
1491+
"accessor with requested offset and range would exceed the bounds of "
1492+
"the buffer",
1493+
PI_INVALID_VALUE);
1494+
14811495
if (!IsPlaceH)
14821496
addHostAccessorAndWait(AccessorBaseHost::impl.get());
14831497
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
@@ -1538,7 +1552,14 @@ class __SYCL_SPECIAL_CLASS accessor :
15381552
detail::getSyclObjImpl(BufferRef).get(), Dimensions,
15391553
sizeof(DataT), BufferRef.OffsetInBytes,
15401554
BufferRef.IsSubBuffer) {
1541-
checkDeviceAccessorBufferSize(BufferRef.size());
1555+
preScreenAccessor(BufferRef.size(), PropertyList);
1556+
if (BufferRef.isOutOfBounds(AccessOffset, AccessRange,
1557+
BufferRef.get_range()))
1558+
throw sycl::invalid_object_error(
1559+
"accessor with requested offset and range would exceed the bounds of "
1560+
"the buffer",
1561+
PI_INVALID_VALUE);
1562+
15421563
detail::associateWithHandler(CommandGroupHandler, this, AccessTarget);
15431564
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
15441565
detail::AccessorBaseHost::impl.get(),
@@ -1571,7 +1592,14 @@ class __SYCL_SPECIAL_CLASS accessor :
15711592
detail::getSyclObjImpl(BufferRef).get(), Dimensions,
15721593
sizeof(DataT), BufferRef.OffsetInBytes,
15731594
BufferRef.IsSubBuffer) {
1574-
checkDeviceAccessorBufferSize(BufferRef.size());
1595+
preScreenAccessor(BufferRef.size(), PropertyList);
1596+
if (BufferRef.isOutOfBounds(AccessOffset, AccessRange,
1597+
BufferRef.get_range()))
1598+
throw sycl::invalid_object_error(
1599+
"accessor with requested offset and range would exceed the bounds of "
1600+
"the buffer",
1601+
PI_INVALID_VALUE);
1602+
15751603
detail::associateWithHandler(CommandGroupHandler, this, AccessTarget);
15761604
detail::constructorNotification(detail::getSyclObjImpl(BufferRef).get(),
15771605
detail::AccessorBaseHost::impl.get(),
@@ -1761,12 +1789,22 @@ class __SYCL_SPECIAL_CLASS accessor :
17611789
return getQualifiedPtr();
17621790
}
17631791

1764-
void checkDeviceAccessorBufferSize(const size_t elemInBuffer) {
1792+
void preScreenAccessor(const size_t elemInBuffer,
1793+
const PropertyListT &PropertyList) {
1794+
// check device accessor buffer size
17651795
if (!IsHostBuf && elemInBuffer == 0)
1766-
throw cl::sycl::invalid_object_error(
1796+
throw sycl::invalid_object_error(
17671797
"SYCL buffer size is zero. To create a device accessor, SYCL "
17681798
"buffer size must be greater than zero.",
17691799
PI_INVALID_VALUE);
1800+
1801+
// check that no_init property is compatible with access mode
1802+
if (PropertyList.template has_property<property::no_init>() &&
1803+
AccessMode == access::mode::read) {
1804+
throw sycl::invalid_object_error(
1805+
"accessor would cannot be both read_only and no_init",
1806+
PI_INVALID_VALUE);
1807+
}
17701808
}
17711809
};
17721810

sycl/include/CL/sycl/backend.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ auto get_native(const SyclObjectT &Obj)
6969
-> backend_return_t<BackendName, SyclObjectT> {
7070
// TODO use SYCL 2020 exception when implemented
7171
if (Obj.get_backend() != BackendName) {
72-
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
72+
throw sycl::runtime_error(errc::backend_mismatch, "Backends mismatch",
73+
PI_INVALID_OPERATION);
7374
}
7475
return Obj.template get_native<BackendName>();
7576
}
@@ -82,7 +83,8 @@ inline backend_return_t<backend::opencl, event>
8283
get_native<backend::opencl, event>(const event &Obj) {
8384
// TODO use SYCL 2020 exception when implemented
8485
if (Obj.get_backend() != backend::opencl) {
85-
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
86+
throw sycl::runtime_error(errc::backend_mismatch, "Backends mismatch",
87+
PI_INVALID_OPERATION);
8688
}
8789
backend_return_t<backend::opencl, event> ReturnValue;
8890
for (auto const &element : Obj.getNativeVector()) {
@@ -104,7 +106,8 @@ inline backend_return_t<backend::opencl, event> get_native<
104106
backend::opencl, event>(const event &Obj) {
105107
// TODO use SYCL 2020 exception when implemented
106108
if (Obj.get_backend() != backend::opencl) {
107-
throw runtime_error("Backends mismatch", PI_INVALID_OPERATION);
109+
throw sycl::runtime_error(errc::backend_mismatch, "Backends mismatch",
110+
PI_INVALID_OPERATION);
108111
}
109112
return reinterpret_cast<
110113
typename detail::interop<backend::opencl, event>::type>(Obj.getNative());

sycl/include/CL/sycl/buffer.hpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ class buffer {
403403
handler &commandGroupHandler, range<dimensions> accessRange,
404404
id<dimensions> accessOffset = {},
405405
const detail::code_location CodeLoc = detail::code_location::current()) {
406+
if (isOutOfBounds(accessOffset, accessRange, this->Range))
407+
throw cl::sycl::invalid_object_error(
408+
"Requested accessor would exceed the bounds of the buffer",
409+
PI_INVALID_VALUE);
410+
406411
return accessor<T, dimensions, mode, target, access::placeholder::false_t,
407412
ext::oneapi::accessor_property_list<>>(
408413
*this, commandGroupHandler, accessRange, accessOffset, {}, CodeLoc);
@@ -414,6 +419,11 @@ class buffer {
414419
get_access(
415420
range<dimensions> accessRange, id<dimensions> accessOffset = {},
416421
const detail::code_location CodeLoc = detail::code_location::current()) {
422+
if (isOutOfBounds(accessOffset, accessRange, this->Range))
423+
throw cl::sycl::invalid_object_error(
424+
"Requested accessor would exceed the bounds of the buffer",
425+
PI_INVALID_VALUE);
426+
417427
return accessor<T, dimensions, mode, access::target::host_buffer,
418428
access::placeholder::false_t,
419429
ext::oneapi::accessor_property_list<>>(
@@ -499,6 +509,17 @@ class buffer {
499509
return impl->template get_property<propertyT>();
500510
}
501511

512+
protected:
513+
bool isOutOfBounds(const id<dimensions> &offset,
514+
const range<dimensions> &newRange,
515+
const range<dimensions> &parentRange) {
516+
bool outOfBounds = false;
517+
for (int i = 0; i < dimensions; ++i)
518+
outOfBounds |= newRange[i] + offset[i] > parentRange[i];
519+
520+
return outOfBounds;
521+
}
522+
502523
private:
503524
std::shared_ptr<detail::buffer_impl> impl;
504525
template <class Obj>
@@ -554,16 +575,6 @@ class buffer {
554575
return detail::getLinearIndex(offset, range) * sizeof(Type);
555576
}
556577

557-
bool isOutOfBounds(const id<dimensions> &offset,
558-
const range<dimensions> &newRange,
559-
const range<dimensions> &parentRange) {
560-
bool outOfBounds = false;
561-
for (int i = 0; i < dimensions; ++i)
562-
outOfBounds |= newRange[i] + offset[i] > parentRange[i];
563-
564-
return outOfBounds;
565-
}
566-
567578
bool isContiguousRegion(const id<1> &, const range<1> &, const range<1> &) {
568579
// 1D sub buffer always has contiguous region
569580
return true;

sycl/include/CL/sycl/exception.hpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,26 @@ class __SYCL2020_DEPRECATED(
134134
"use sycl::exception with sycl::errc::runtime instead.") runtime_error
135135
: public exception {
136136
public:
137-
runtime_error() = default;
137+
runtime_error() : exception(make_error_code(errc::runtime)) {}
138138

139139
runtime_error(const char *Msg, cl_int Err)
140140
: runtime_error(std::string(Msg), Err) {}
141141

142142
runtime_error(const std::string &Msg, cl_int Err)
143143
: exception(make_error_code(errc::runtime), Msg, Err) {}
144144

145-
protected:
146145
runtime_error(std::error_code ec, const std::string &Msg, const cl_int CLErr)
147146
: exception(ec, Msg, CLErr) {}
147+
148+
protected:
149+
runtime_error(std::error_code ec) : exception(ec) {}
148150
};
149151

150152
class __SYCL2020_DEPRECATED("use sycl::exception with sycl::errc::kernel or "
151153
"errc::kernel_argument instead.") kernel_error
152154
: public runtime_error {
153155
public:
154-
kernel_error() = default;
156+
kernel_error() : runtime_error(make_error_code(errc::kernel)) {}
155157

156158
kernel_error(const char *Msg, cl_int Err)
157159
: kernel_error(std::string(Msg), Err) {}
@@ -164,7 +166,7 @@ class __SYCL2020_DEPRECATED(
164166
"use sycl::exception with sycl::errc::accessor instead.") accessor_error
165167
: public runtime_error {
166168
public:
167-
accessor_error() = default;
169+
accessor_error() : runtime_error(make_error_code(errc::accessor)) {}
168170

169171
accessor_error(const char *Msg, cl_int Err)
170172
: accessor_error(std::string(Msg), Err) {}
@@ -177,7 +179,7 @@ class __SYCL2020_DEPRECATED(
177179
"use sycl::exception with sycl::errc::nd_range instead.") nd_range_error
178180
: public runtime_error {
179181
public:
180-
nd_range_error() = default;
182+
nd_range_error() : runtime_error(make_error_code(errc::nd_range)) {}
181183

182184
nd_range_error(const char *Msg, cl_int Err)
183185
: nd_range_error(std::string(Msg), Err) {}
@@ -190,7 +192,7 @@ class __SYCL2020_DEPRECATED(
190192
"use sycl::exception with sycl::errc::event instead.") event_error
191193
: public runtime_error {
192194
public:
193-
event_error() = default;
195+
event_error() : runtime_error(make_error_code(errc::event)) {}
194196

195197
event_error(const char *Msg, cl_int Err)
196198
: event_error(std::string(Msg), Err) {}
@@ -203,7 +205,8 @@ class __SYCL2020_DEPRECATED(
203205
"use sycl::exception with a sycl::errc enum value instead.")
204206
invalid_parameter_error : public runtime_error {
205207
public:
206-
invalid_parameter_error() = default;
208+
invalid_parameter_error()
209+
: runtime_error(make_error_code(errc::kernel_argument)) {}
207210

208211
invalid_parameter_error(const char *Msg, cl_int Err)
209212
: invalid_parameter_error(std::string(Msg), Err) {}
@@ -216,7 +219,7 @@ class __SYCL2020_DEPRECATED(
216219
"use sycl::exception with a sycl::errc enum value instead.") device_error
217220
: public exception {
218221
public:
219-
device_error() = default;
222+
device_error() : exception(make_error_code(errc::invalid)) {}
220223

221224
device_error(const char *Msg, cl_int Err)
222225
: device_error(std::string(Msg), Err) {}
@@ -225,6 +228,8 @@ class __SYCL2020_DEPRECATED(
225228
: exception(make_error_code(errc::invalid), Msg, Err) {}
226229

227230
protected:
231+
device_error(std::error_code ec) : exception(ec) {}
232+
228233
device_error(std::error_code ec, const std::string &Msg, const cl_int CLErr)
229234
: exception(ec, Msg, CLErr) {}
230235
};
@@ -233,7 +238,7 @@ class __SYCL2020_DEPRECATED(
233238
"use sycl::exception with a sycl::errc enum value instead.")
234239
compile_program_error : public device_error {
235240
public:
236-
compile_program_error() = default;
241+
compile_program_error() : device_error(make_error_code(errc::build)) {}
237242

238243
compile_program_error(const char *Msg, cl_int Err)
239244
: compile_program_error(std::string(Msg), Err) {}
@@ -246,7 +251,7 @@ class __SYCL2020_DEPRECATED(
246251
"use sycl::exception with a sycl::errc enum value instead.")
247252
link_program_error : public device_error {
248253
public:
249-
link_program_error() = default;
254+
link_program_error() : device_error(make_error_code(errc::build)) {}
250255

251256
link_program_error(const char *Msg, cl_int Err)
252257
: link_program_error(std::string(Msg), Err) {}
@@ -259,7 +264,7 @@ class __SYCL2020_DEPRECATED(
259264
"use sycl::exception with a sycl::errc enum value instead.")
260265
invalid_object_error : public device_error {
261266
public:
262-
invalid_object_error() = default;
267+
invalid_object_error() : device_error(make_error_code(errc::invalid)) {}
263268

264269
invalid_object_error(const char *Msg, cl_int Err)
265270
: invalid_object_error(std::string(Msg), Err) {}
@@ -272,7 +277,8 @@ class __SYCL2020_DEPRECATED(
272277
"use sycl::exception with sycl::errc::memory_allocation instead.")
273278
memory_allocation_error : public device_error {
274279
public:
275-
memory_allocation_error() = default;
280+
memory_allocation_error()
281+
: device_error(make_error_code(errc::memory_allocation)) {}
276282

277283
memory_allocation_error(const char *Msg, cl_int Err)
278284
: memory_allocation_error(std::string(Msg), Err) {}
@@ -285,7 +291,7 @@ class __SYCL2020_DEPRECATED(
285291
"use sycl::exception with sycl::errc::platform instead.") platform_error
286292
: public device_error {
287293
public:
288-
platform_error() = default;
294+
platform_error() : device_error(make_error_code(errc::platform)) {}
289295

290296
platform_error(const char *Msg, cl_int Err)
291297
: platform_error(std::string(Msg), Err) {}
@@ -298,7 +304,7 @@ class __SYCL2020_DEPRECATED(
298304
"use sycl::exception with sycl::errc::profiling instead.") profiling_error
299305
: public device_error {
300306
public:
301-
profiling_error() = default;
307+
profiling_error() : device_error(make_error_code(errc::profiling)) {}
302308

303309
profiling_error(const char *Msg, cl_int Err)
304310
: profiling_error(std::string(Msg), Err) {}
@@ -311,7 +317,8 @@ class __SYCL2020_DEPRECATED(
311317
"use sycl::exception with sycl::errc::feature_not_supported instead.")
312318
feature_not_supported : public device_error {
313319
public:
314-
feature_not_supported() = default;
320+
feature_not_supported()
321+
: device_error(make_error_code(errc::feature_not_supported)) {}
315322

316323
feature_not_supported(const char *Msg, cl_int Err)
317324
: feature_not_supported(std::string(Msg), Err) {}

0 commit comments

Comments
 (0)