Skip to content

Commit b87f456

Browse files
[SYCL] More cleanup to use SYCL 2020 exception (#14510)
... instead of deprecated SYCL 1.2 subclasses that we're going to remove during this ABI breaking window. In many cases there is no clear choice of using `errc::runtime` vs `errc::invalid` or something else. I tried to use my best judgement. Reviewers, feel free to start inline discussions in comments if you disagree with my choice(s).
1 parent 860fd7c commit b87f456

25 files changed

+152
-167
lines changed

sycl/include/sycl/ext/oneapi/backend/hip.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ inline namespace _V1 {
1717
template <>
1818
inline backend_return_t<backend::ext_oneapi_hip, device>
1919
get_native<backend::ext_oneapi_hip, device>(const device &Obj) {
20-
// TODO swap with SYCL 2020 exception when in ABI-break window
2120
if (Obj.get_backend() != backend::ext_oneapi_hip) {
22-
throw sycl::runtime_error(errc::backend_mismatch, "Backends mismatch",
23-
PI_ERROR_INVALID_OPERATION);
21+
throw exception(errc::backend_mismatch, "Backends mismatch");
2422
}
2523
// HIP uses a 32-bit int instead of an opaque pointer like other backends,
2624
// so we need a specialization with static_cast instead of reinterpret_cast.

sycl/include/sycl/ext/oneapi/bf16_storage_builtins.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ std::enable_if_t<detail::is_bf16_storage_type<T>::value, T> fabs(T x) {
4949
return __clc_fabs(x);
5050
#else
5151
(void)x;
52-
throw runtime_error("bf16 is not supported on host device.",
53-
PI_ERROR_INVALID_DEVICE);
52+
throw exception(make_error_code(errc::runtime),
53+
"bf16 is not supported on host.");
5454
#endif
5555
}
5656
template <typename T>
@@ -60,8 +60,8 @@ std::enable_if_t<detail::is_bf16_storage_type<T>::value, T> fmin(T x, T y) {
6060
#else
6161
(void)x;
6262
(void)y;
63-
throw runtime_error("bf16 is not supported on host device.",
64-
PI_ERROR_INVALID_DEVICE);
63+
throw exception(make_error_code(errc::runtime),
64+
"bf16 is not supported on host.");
6565
#endif
6666
}
6767
template <typename T>
@@ -71,8 +71,8 @@ std::enable_if_t<detail::is_bf16_storage_type<T>::value, T> fmax(T x, T y) {
7171
#else
7272
(void)x;
7373
(void)y;
74-
throw runtime_error("bf16 is not supported on host device.",
75-
PI_ERROR_INVALID_DEVICE);
74+
throw exception(make_error_code(errc::runtime),
75+
"bf16 is not supported on host.");
7676
#endif
7777
}
7878
template <typename T>
@@ -83,8 +83,8 @@ std::enable_if_t<detail::is_bf16_storage_type<T>::value, T> fma(T x, T y, T z) {
8383
(void)x;
8484
(void)y;
8585
(void)z;
86-
throw runtime_error("bf16 is not supported on host device.",
87-
PI_ERROR_INVALID_DEVICE);
86+
throw exception(make_error_code(errc::runtime),
87+
"bf16 is not supported on host.");
8888
#endif
8989
}
9090

sycl/source/backend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ backend convertBackend(pi_platform_backend PiBackend) {
6161
case PI_EXT_PLATFORM_BACKEND_NATIVE_CPU:
6262
return backend::ext_oneapi_native_cpu;
6363
}
64-
throw sycl::runtime_error{"convertBackend: Unsupported backend",
65-
PI_ERROR_INVALID_OPERATION};
64+
throw exception(make_error_code(errc::runtime),
65+
"convertBackend: Unsupported backend");
6666
}
6767

6868
platform make_platform(pi_native_handle NativeHandle, backend Backend) {

sycl/source/detail/config.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ template <> class SYCLConfig<SYCL_CACHE_PERSISTENT> {
381381
std::string Msg =
382382
std::string{"Invalid value for bool configuration variable "} +
383383
getName() + std::string{": "} + ValStr;
384-
throw runtime_error(Msg, PI_ERROR_INVALID_OPERATION);
384+
throw exception(make_error_code(errc::runtime), Msg);
385385
}
386386
return ValStr[0] == '1';
387387
}
@@ -603,7 +603,7 @@ template <> class SYCLConfig<SYCL_CACHE_IN_MEM> {
603603
std::string Msg =
604604
std::string{"Invalid value for bool configuration variable "} +
605605
getName() + std::string{": "} + ValStr;
606-
throw runtime_error(Msg, PI_ERROR_INVALID_OPERATION);
606+
throw exception(make_error_code(errc::runtime), Msg);
607607
}
608608
return ValStr[0] == '1';
609609
}

sycl/source/detail/error_handling/error_handling.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,8 @@ void handleInvalidWorkGroupSize(const device_impl &DeviceImpl, pi_kernel Kernel,
301301
// consistent with the required number of sub-groups for kernel in the
302302
// program source.
303303

304-
// Fallback
305-
constexpr pi_result Error = PI_ERROR_INVALID_WORK_GROUP_SIZE;
306-
throw runtime_error(
307-
"PI backend failed. PI backend returns: " + codeToString(Error), Error);
304+
throw exception(make_error_code(errc::nd_range),
305+
"internal error: expected HasLocalSize");
308306
}
309307

310308
void handleInvalidWorkItemSize(const device_impl &DeviceImpl,
@@ -348,9 +346,7 @@ void handleInvalidValue(const device_impl &DeviceImpl,
348346
}
349347

350348
// fallback
351-
constexpr pi_result Error = PI_ERROR_INVALID_VALUE;
352-
throw runtime_error(
353-
"Native API failed. Native API returns: " + codeToString(Error), Error);
349+
throw exception(make_error_code(errc::nd_range), "unknown internal error");
354350
}
355351

356352
void handleErrorOrWarning(pi_result Error, const device_impl &DeviceImpl,
@@ -424,8 +420,8 @@ void handleErrorOrWarning(pi_result Error, const device_impl &DeviceImpl,
424420
// TODO: Handle other error codes
425421

426422
default:
427-
throw runtime_error(
428-
"Native API failed. Native API returns: " + codeToString(Error), Error);
423+
throw detail::set_pi_error(
424+
exception(make_error_code(errc::runtime), "PI error"), Error);
429425
}
430426
}
431427

sycl/source/detail/filter_selector_impl.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ filter create_filter(const std::string &Input) {
5656
// There should only be up to 3 tokens.
5757
// BE:Device Type:Device Num
5858
if (Tokens.size() > 3)
59-
throw sycl::runtime_error(Error, PI_ERROR_INVALID_VALUE);
59+
throw exception(make_error_code(errc::invalid), Error);
6060

6161
for (const std::string &Token : Tokens) {
6262
if (Token == "cpu" && !Result.DeviceType) {
@@ -77,10 +77,10 @@ filter create_filter(const std::string &Input) {
7777
try {
7878
Result.DeviceNum = std::stoi(Token);
7979
} catch (std::logic_error &) {
80-
throw sycl::runtime_error(Error, PI_ERROR_INVALID_VALUE);
80+
throw exception(make_error_code(errc::invalid), Error);
8181
}
8282
} else {
83-
throw sycl::runtime_error(Error, PI_ERROR_INVALID_VALUE);
83+
throw exception(make_error_code(errc::invalid), Error);
8484
}
8585
}
8686

@@ -141,9 +141,9 @@ int filter_selector_impl::operator()(const device &Dev) const {
141141

142142
mNumDevicesSeen++;
143143
if ((mNumDevicesSeen == mNumTotalDevices) && !mMatchFound) {
144-
throw sycl::runtime_error(
145-
"Could not find a device that matches the specified filter(s)!",
146-
PI_ERROR_DEVICE_NOT_FOUND);
144+
throw exception(
145+
make_error_code(errc::runtime),
146+
"Could not find a device that matches the specified filter(s)!");
147147
}
148148

149149
return Score;

sycl/source/detail/kernel_bundle_impl.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ class kernel_bundle_impl {
152152
break;
153153
case bundle_state::input:
154154
case bundle_state::ext_oneapi_source:
155-
throw sycl::runtime_error("Internal error. The target state should not "
156-
"be input or ext_oneapi_source",
157-
PI_ERROR_INVALID_OPERATION);
155+
throw exception(make_error_code(errc::runtime),
156+
"Internal error. The target state should not be input "
157+
"or ext_oneapi_source");
158158
break;
159159
}
160160
}

sycl/source/detail/memory_manager.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,9 @@ void *MemoryManager::allocateMemSubBuffer(ContextImplPtr TargetContext,
459459
Error);
460460

461461
if (Error != PI_SUCCESS) {
462-
Plugin->reportPiError(Error, "allocateMemSubBuffer()");
462+
throw set_pi_error(exception(make_error_code(errc::runtime),
463+
"allocateMemSubBuffer() failed"),
464+
Error);
463465
}
464466

465467
return NewMem;
@@ -750,8 +752,8 @@ static void copyH2H(SYCLMemObjI *, char *SrcMem, QueueImplPtr,
750752
if ((DimSrc != 1 || DimDst != 1) &&
751753
(SrcOffset != id<3>{0, 0, 0} || DstOffset != id<3>{0, 0, 0} ||
752754
SrcSize != SrcAccessRange || DstSize != DstAccessRange)) {
753-
throw runtime_error("Not supported configuration of memcpy requested",
754-
PI_ERROR_INVALID_OPERATION);
755+
throw exception(make_error_code(errc::feature_not_supported),
756+
"Not supported configuration of memcpy requested");
755757
}
756758

757759
SrcMem += SrcOffset[0] * SrcElemSize;
@@ -842,8 +844,8 @@ void MemoryManager::fill(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
842844
}
843845
// The sycl::handler uses a parallel_for kernel in the case of unusable
844846
// Range or Offset, not CG:Fill. So we should not be here.
845-
throw runtime_error("Not supported configuration of fill requested",
846-
PI_ERROR_INVALID_OPERATION);
847+
throw exception(make_error_code(errc::runtime),
848+
"Not supported configuration of fill requested");
847849
} else {
848850
if (OutEventImpl != nullptr)
849851
OutEventImpl->setHostEnqueueTime();
@@ -863,8 +865,8 @@ void *MemoryManager::map(SYCLMemObjI *, void *Mem, QueueImplPtr Queue,
863865
std::vector<sycl::detail::pi::PiEvent> DepEvents,
864866
sycl::detail::pi::PiEvent &OutEvent) {
865867
if (!Queue) {
866-
throw runtime_error("Not supported configuration of map requested",
867-
PI_ERROR_INVALID_OPERATION);
868+
throw exception(make_error_code(errc::runtime),
869+
"Not supported configuration of map requested");
868870
}
869871

870872
pi_map_flags Flags = 0;
@@ -909,8 +911,8 @@ void MemoryManager::unmap(SYCLMemObjI *, void *Mem, QueueImplPtr Queue,
909911

910912
// Execution on host is not supported here.
911913
if (!Queue) {
912-
throw runtime_error("Not supported configuration of unmap requested",
913-
PI_ERROR_INVALID_OPERATION);
914+
throw exception(make_error_code(errc::runtime),
915+
"Not supported configuration of unmap requested");
914916
}
915917
// All DepEvents are to the same Context.
916918
// Using the plugin of the Queue.
@@ -939,8 +941,8 @@ void MemoryManager::copy_usm(const void *SrcMem, QueueImplPtr SrcQueue,
939941
}
940942

941943
if (!SrcMem || !DstMem)
942-
throw runtime_error("NULL pointer argument in memory copy operation.",
943-
PI_ERROR_INVALID_VALUE);
944+
throw exception(make_error_code(errc::invalid),
945+
"NULL pointer argument in memory copy operation.");
944946

945947
const PluginPtr &Plugin = SrcQueue->getPlugin();
946948
if (OutEventImpl != nullptr)
@@ -968,8 +970,8 @@ void MemoryManager::fill_usm(void *Mem, QueueImplPtr Queue, size_t Length,
968970
}
969971

970972
if (!Mem)
971-
throw runtime_error("NULL pointer argument in memory fill operation.",
972-
PI_ERROR_INVALID_VALUE);
973+
throw exception(make_error_code(errc::invalid),
974+
"NULL pointer argument in memory fill operation.");
973975
if (OutEventImpl != nullptr)
974976
OutEventImpl->setHostEnqueueTime();
975977
const PluginPtr &Plugin = Queue->getPlugin();
@@ -1551,8 +1553,8 @@ void MemoryManager::ext_oneapi_copy_usm_cmd_buffer(
15511553
void *DstMem, std::vector<sycl::detail::pi::PiExtSyncPoint> Deps,
15521554
sycl::detail::pi::PiExtSyncPoint *OutSyncPoint) {
15531555
if (!SrcMem || !DstMem)
1554-
throw runtime_error("NULL pointer argument in memory copy operation.",
1555-
PI_ERROR_INVALID_VALUE);
1556+
throw exception(make_error_code(errc::invalid),
1557+
"NULL pointer argument in memory copy operation.");
15561558

15571559
const PluginPtr &Plugin = Context->getPlugin();
15581560
pi_result Result =
@@ -1576,8 +1578,8 @@ void MemoryManager::ext_oneapi_fill_usm_cmd_buffer(
15761578
sycl::detail::pi::PiExtSyncPoint *OutSyncPoint) {
15771579

15781580
if (!DstMem)
1579-
throw runtime_error("NULL pointer argument in memory fill operation.",
1580-
PI_ERROR_INVALID_VALUE);
1581+
throw exception(make_error_code(errc::invalid),
1582+
"NULL pointer argument in memory fill operation.");
15811583

15821584
const PluginPtr &Plugin = Context->getPlugin();
15831585

@@ -1619,8 +1621,8 @@ void MemoryManager::ext_oneapi_fill_cmd_buffer(
16191621
}
16201622
// The sycl::handler uses a parallel_for kernel in the case of unusable
16211623
// Range or Offset, not CG:Fill. So we should not be here.
1622-
throw runtime_error("Not supported configuration of fill requested",
1623-
PI_ERROR_INVALID_OPERATION);
1624+
throw exception(make_error_code(errc::runtime),
1625+
"Not supported configuration of fill requested");
16241626
}
16251627

16261628
void MemoryManager::ext_oneapi_prefetch_usm_cmd_buffer(

sycl/source/detail/pi.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ template <backend BE> const PluginPtr &getPlugin() {
504504
return *Plugin;
505505
}
506506

507-
throw runtime_error("pi::getPlugin couldn't find plugin",
508-
PI_ERROR_INVALID_OPERATION);
507+
throw exception(make_error_code(errc::runtime),
508+
"pi::getPlugin couldn't find plugin");
509509
}
510510

511511
template __SYCL_EXPORT const PluginPtr &getPlugin<backend::opencl>();

sycl/source/detail/platform_util.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ static void cpuid(uint32_t *CPUInfo, uint32_t Type, uint32_t SubType = 0) {
4040
#endif
4141

4242
uint32_t PlatformUtil::getMaxClockFrequency() {
43-
throw runtime_error(
44-
"max_clock_frequency parameter is not supported for host device",
45-
PI_ERROR_INVALID_DEVICE);
43+
throw exception(make_error_code(errc::runtime),
44+
"max_clock_frequency parameter is not supported on host");
4645
return 0;
4746
}
4847

0 commit comments

Comments
 (0)