Skip to content

Commit

Permalink
[SYCL] More cleanup to use SYCL 2020 exception (#14510)
Browse files Browse the repository at this point in the history
... 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).
  • Loading branch information
aelovikov-intel authored Jul 10, 2024
1 parent 860fd7c commit b87f456
Show file tree
Hide file tree
Showing 25 changed files with 152 additions and 167 deletions.
4 changes: 1 addition & 3 deletions sycl/include/sycl/ext/oneapi/backend/hip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ inline namespace _V1 {
template <>
inline backend_return_t<backend::ext_oneapi_hip, device>
get_native<backend::ext_oneapi_hip, device>(const device &Obj) {
// TODO swap with SYCL 2020 exception when in ABI-break window
if (Obj.get_backend() != backend::ext_oneapi_hip) {
throw sycl::runtime_error(errc::backend_mismatch, "Backends mismatch",
PI_ERROR_INVALID_OPERATION);
throw exception(errc::backend_mismatch, "Backends mismatch");
}
// HIP uses a 32-bit int instead of an opaque pointer like other backends,
// so we need a specialization with static_cast instead of reinterpret_cast.
Expand Down
16 changes: 8 additions & 8 deletions sycl/include/sycl/ext/oneapi/bf16_storage_builtins.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ std::enable_if_t<detail::is_bf16_storage_type<T>::value, T> fabs(T x) {
return __clc_fabs(x);
#else
(void)x;
throw runtime_error("bf16 is not supported on host device.",
PI_ERROR_INVALID_DEVICE);
throw exception(make_error_code(errc::runtime),
"bf16 is not supported on host.");
#endif
}
template <typename T>
Expand All @@ -60,8 +60,8 @@ std::enable_if_t<detail::is_bf16_storage_type<T>::value, T> fmin(T x, T y) {
#else
(void)x;
(void)y;
throw runtime_error("bf16 is not supported on host device.",
PI_ERROR_INVALID_DEVICE);
throw exception(make_error_code(errc::runtime),
"bf16 is not supported on host.");
#endif
}
template <typename T>
Expand All @@ -71,8 +71,8 @@ std::enable_if_t<detail::is_bf16_storage_type<T>::value, T> fmax(T x, T y) {
#else
(void)x;
(void)y;
throw runtime_error("bf16 is not supported on host device.",
PI_ERROR_INVALID_DEVICE);
throw exception(make_error_code(errc::runtime),
"bf16 is not supported on host.");
#endif
}
template <typename T>
Expand All @@ -83,8 +83,8 @@ std::enable_if_t<detail::is_bf16_storage_type<T>::value, T> fma(T x, T y, T z) {
(void)x;
(void)y;
(void)z;
throw runtime_error("bf16 is not supported on host device.",
PI_ERROR_INVALID_DEVICE);
throw exception(make_error_code(errc::runtime),
"bf16 is not supported on host.");
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions sycl/source/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ backend convertBackend(pi_platform_backend PiBackend) {
case PI_EXT_PLATFORM_BACKEND_NATIVE_CPU:
return backend::ext_oneapi_native_cpu;
}
throw sycl::runtime_error{"convertBackend: Unsupported backend",
PI_ERROR_INVALID_OPERATION};
throw exception(make_error_code(errc::runtime),
"convertBackend: Unsupported backend");
}

platform make_platform(pi_native_handle NativeHandle, backend Backend) {
Expand Down
4 changes: 2 additions & 2 deletions sycl/source/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ template <> class SYCLConfig<SYCL_CACHE_PERSISTENT> {
std::string Msg =
std::string{"Invalid value for bool configuration variable "} +
getName() + std::string{": "} + ValStr;
throw runtime_error(Msg, PI_ERROR_INVALID_OPERATION);
throw exception(make_error_code(errc::runtime), Msg);
}
return ValStr[0] == '1';
}
Expand Down Expand Up @@ -603,7 +603,7 @@ template <> class SYCLConfig<SYCL_CACHE_IN_MEM> {
std::string Msg =
std::string{"Invalid value for bool configuration variable "} +
getName() + std::string{": "} + ValStr;
throw runtime_error(Msg, PI_ERROR_INVALID_OPERATION);
throw exception(make_error_code(errc::runtime), Msg);
}
return ValStr[0] == '1';
}
Expand Down
14 changes: 5 additions & 9 deletions sycl/source/detail/error_handling/error_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,8 @@ void handleInvalidWorkGroupSize(const device_impl &DeviceImpl, pi_kernel Kernel,
// consistent with the required number of sub-groups for kernel in the
// program source.

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

void handleInvalidWorkItemSize(const device_impl &DeviceImpl,
Expand Down Expand Up @@ -348,9 +346,7 @@ void handleInvalidValue(const device_impl &DeviceImpl,
}

// fallback
constexpr pi_result Error = PI_ERROR_INVALID_VALUE;
throw runtime_error(
"Native API failed. Native API returns: " + codeToString(Error), Error);
throw exception(make_error_code(errc::nd_range), "unknown internal error");
}

void handleErrorOrWarning(pi_result Error, const device_impl &DeviceImpl,
Expand Down Expand Up @@ -424,8 +420,8 @@ void handleErrorOrWarning(pi_result Error, const device_impl &DeviceImpl,
// TODO: Handle other error codes

default:
throw runtime_error(
"Native API failed. Native API returns: " + codeToString(Error), Error);
throw detail::set_pi_error(
exception(make_error_code(errc::runtime), "PI error"), Error);
}
}

Expand Down
12 changes: 6 additions & 6 deletions sycl/source/detail/filter_selector_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ filter create_filter(const std::string &Input) {
// There should only be up to 3 tokens.
// BE:Device Type:Device Num
if (Tokens.size() > 3)
throw sycl::runtime_error(Error, PI_ERROR_INVALID_VALUE);
throw exception(make_error_code(errc::invalid), Error);

for (const std::string &Token : Tokens) {
if (Token == "cpu" && !Result.DeviceType) {
Expand All @@ -77,10 +77,10 @@ filter create_filter(const std::string &Input) {
try {
Result.DeviceNum = std::stoi(Token);
} catch (std::logic_error &) {
throw sycl::runtime_error(Error, PI_ERROR_INVALID_VALUE);
throw exception(make_error_code(errc::invalid), Error);
}
} else {
throw sycl::runtime_error(Error, PI_ERROR_INVALID_VALUE);
throw exception(make_error_code(errc::invalid), Error);
}
}

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

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

return Score;
Expand Down
6 changes: 3 additions & 3 deletions sycl/source/detail/kernel_bundle_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ class kernel_bundle_impl {
break;
case bundle_state::input:
case bundle_state::ext_oneapi_source:
throw sycl::runtime_error("Internal error. The target state should not "
"be input or ext_oneapi_source",
PI_ERROR_INVALID_OPERATION);
throw exception(make_error_code(errc::runtime),
"Internal error. The target state should not be input "
"or ext_oneapi_source");
break;
}
}
Expand Down
40 changes: 21 additions & 19 deletions sycl/source/detail/memory_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,9 @@ void *MemoryManager::allocateMemSubBuffer(ContextImplPtr TargetContext,
Error);

if (Error != PI_SUCCESS) {
Plugin->reportPiError(Error, "allocateMemSubBuffer()");
throw set_pi_error(exception(make_error_code(errc::runtime),
"allocateMemSubBuffer() failed"),
Error);
}

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

SrcMem += SrcOffset[0] * SrcElemSize;
Expand Down Expand Up @@ -842,8 +844,8 @@ void MemoryManager::fill(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
}
// The sycl::handler uses a parallel_for kernel in the case of unusable
// Range or Offset, not CG:Fill. So we should not be here.
throw runtime_error("Not supported configuration of fill requested",
PI_ERROR_INVALID_OPERATION);
throw exception(make_error_code(errc::runtime),
"Not supported configuration of fill requested");
} else {
if (OutEventImpl != nullptr)
OutEventImpl->setHostEnqueueTime();
Expand All @@ -863,8 +865,8 @@ void *MemoryManager::map(SYCLMemObjI *, void *Mem, QueueImplPtr Queue,
std::vector<sycl::detail::pi::PiEvent> DepEvents,
sycl::detail::pi::PiEvent &OutEvent) {
if (!Queue) {
throw runtime_error("Not supported configuration of map requested",
PI_ERROR_INVALID_OPERATION);
throw exception(make_error_code(errc::runtime),
"Not supported configuration of map requested");
}

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

// Execution on host is not supported here.
if (!Queue) {
throw runtime_error("Not supported configuration of unmap requested",
PI_ERROR_INVALID_OPERATION);
throw exception(make_error_code(errc::runtime),
"Not supported configuration of unmap requested");
}
// All DepEvents are to the same Context.
// Using the plugin of the Queue.
Expand Down Expand Up @@ -939,8 +941,8 @@ void MemoryManager::copy_usm(const void *SrcMem, QueueImplPtr SrcQueue,
}

if (!SrcMem || !DstMem)
throw runtime_error("NULL pointer argument in memory copy operation.",
PI_ERROR_INVALID_VALUE);
throw exception(make_error_code(errc::invalid),
"NULL pointer argument in memory copy operation.");

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

if (!Mem)
throw runtime_error("NULL pointer argument in memory fill operation.",
PI_ERROR_INVALID_VALUE);
throw exception(make_error_code(errc::invalid),
"NULL pointer argument in memory fill operation.");
if (OutEventImpl != nullptr)
OutEventImpl->setHostEnqueueTime();
const PluginPtr &Plugin = Queue->getPlugin();
Expand Down Expand Up @@ -1551,8 +1553,8 @@ void MemoryManager::ext_oneapi_copy_usm_cmd_buffer(
void *DstMem, std::vector<sycl::detail::pi::PiExtSyncPoint> Deps,
sycl::detail::pi::PiExtSyncPoint *OutSyncPoint) {
if (!SrcMem || !DstMem)
throw runtime_error("NULL pointer argument in memory copy operation.",
PI_ERROR_INVALID_VALUE);
throw exception(make_error_code(errc::invalid),
"NULL pointer argument in memory copy operation.");

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

if (!DstMem)
throw runtime_error("NULL pointer argument in memory fill operation.",
PI_ERROR_INVALID_VALUE);
throw exception(make_error_code(errc::invalid),
"NULL pointer argument in memory fill operation.");

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

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

void MemoryManager::ext_oneapi_prefetch_usm_cmd_buffer(
Expand Down
4 changes: 2 additions & 2 deletions sycl/source/detail/pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ template <backend BE> const PluginPtr &getPlugin() {
return *Plugin;
}

throw runtime_error("pi::getPlugin couldn't find plugin",
PI_ERROR_INVALID_OPERATION);
throw exception(make_error_code(errc::runtime),
"pi::getPlugin couldn't find plugin");
}

template __SYCL_EXPORT const PluginPtr &getPlugin<backend::opencl>();
Expand Down
5 changes: 2 additions & 3 deletions sycl/source/detail/platform_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ static void cpuid(uint32_t *CPUInfo, uint32_t Type, uint32_t SubType = 0) {
#endif

uint32_t PlatformUtil::getMaxClockFrequency() {
throw runtime_error(
"max_clock_frequency parameter is not supported for host device",
PI_ERROR_INVALID_DEVICE);
throw exception(make_error_code(errc::runtime),
"max_clock_frequency parameter is not supported on host");
return 0;
}

Expand Down
10 changes: 0 additions & 10 deletions sycl/source/detail/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,6 @@ class plugin {
__SYCL_CHECK_CODE_THROW_VIA_ERRC(pi_result, errc);
}

void reportPiError(sycl::detail::pi::PiResult pi_result,
const char *context) const {
if (pi_result != PI_SUCCESS) {
throw sycl::runtime_error(std::string(context) +
" API failed with error: " +
sycl::detail::codeToString(pi_result),
pi_result);
}
}

/// Calls the PiApi, traces the call, and returns the result.
///
/// Usage:
Expand Down
Loading

0 comments on commit b87f456

Please sign in to comment.