Skip to content

[SYCL] Adapt to sycl 2020 exceptions #9771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions sycl/source/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ static const PluginPtr &getPlugin(backend Backend) {
case backend::ext_oneapi_cuda:
return pi::getPlugin<backend::ext_oneapi_cuda>();
default:
throw sycl::runtime_error{"getPlugin: Unsupported backend",
PI_ERROR_INVALID_OPERATION};
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"getPlugin: Unsupported backend " +
detail::codeToString(PI_ERROR_INVALID_OPERATION));
}
}

Expand Down Expand Up @@ -196,21 +197,19 @@ make_kernel_bundle(pi_native_handle NativeHandle, const context &TargetContext,
case (PI_PROGRAM_BINARY_TYPE_COMPILED_OBJECT):
case (PI_PROGRAM_BINARY_TYPE_LIBRARY):
if (State == bundle_state::input)
// TODO SYCL2020 exception
throw sycl::runtime_error(errc::invalid,
"Program and kernel_bundle state mismatch",
PI_ERROR_INVALID_VALUE);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"Program and kernel_bundle state mismatch " +
detail::codeToString(PI_ERROR_INVALID_VALUE));
if (State == bundle_state::executable)
Plugin->call<errc::build, PiApiKind::piProgramLink>(
ContextImpl->getHandleRef(), 1, &Dev, nullptr, 1, &PiProgram,
nullptr, nullptr, &PiProgram);
break;
case (PI_PROGRAM_BINARY_TYPE_EXECUTABLE):
if (State == bundle_state::input || State == bundle_state::object)
// TODO SYCL2020 exception
throw sycl::runtime_error(errc::invalid,
"Program and kernel_bundle state mismatch",
PI_ERROR_INVALID_VALUE);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"Program and kernel_bundle state mismatch " +
detail::codeToString(PI_ERROR_INVALID_VALUE));
break;
}
}
Expand Down Expand Up @@ -264,9 +263,10 @@ kernel make_kernel(const context &TargetContext,
pi::PiProgram PiProgram = nullptr;
if (Backend == backend::ext_oneapi_level_zero) {
if (KernelBundleImpl->size() != 1)
throw sycl::runtime_error{
"make_kernel: kernel_bundle must have single program image",
PI_ERROR_INVALID_PROGRAM};
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"make_kernel: kernel_bundle must have single program image " +
detail::codeToString(PI_ERROR_INVALID_PROGRAM));

const device_image<bundle_state::executable> &DeviceImage =
*KernelBundle.begin();
Expand Down
66 changes: 37 additions & 29 deletions sycl/source/detail/allowlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
const char DelimiterBtwDeviceDescs = '|';

if (AllowListRaw.find(DelimiterBtwKeyAndValue, KeyStart) == std::string::npos)
throw sycl::runtime_error("SYCL_DEVICE_ALLOWLIST has incorrect format. For "
"details, please refer to "
"https://github.com/intel/llvm/blob/sycl/sycl/"
"doc/EnvironmentVariables.md",
PI_ERROR_INVALID_VALUE);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"SYCL_DEVICE_ALLOWLIST has incorrect format. For "
"details, please refer to "
"https://github.com/intel/llvm/blob/sycl/sycl/"
"doc/EnvironmentVariables.md " +
codeToString(PI_ERROR_INVALID_VALUE));

const std::string &DeprecatedKeyNameDeviceName = DeviceNameKeyName;
const std::string &DeprecatedKeyNamePlatformName = PlatformNameKeyName;
Expand All @@ -95,12 +96,13 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
if (std::find(SupportedAllowListKeyNames.begin(),
SupportedAllowListKeyNames.end(),
Key) == SupportedAllowListKeyNames.end()) {
throw sycl::runtime_error(
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Unrecognized key in SYCL_DEVICE_ALLOWLIST. For details, please "
"refer to "
"https://github.com/intel/llvm/blob/sycl/sycl/doc/"
"EnvironmentVariables.md",
PI_ERROR_INVALID_VALUE);
"EnvironmentVariables.md " +
codeToString(PI_ERROR_INVALID_VALUE));
}

if (Key == DeprecatedKeyNameDeviceName) {
Expand Down Expand Up @@ -149,13 +151,14 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
break;
}
if (!ValueIsValid)
throw sycl::runtime_error(
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Value " + Value + " for key " + Key +
" is not valid in "
"SYCL_DEVICE_ALLOWLIST. For details, please refer to "
"https://github.com/intel/llvm/blob/sycl/sycl/doc/"
"EnvironmentVariables.md",
PI_ERROR_INVALID_VALUE);
"EnvironmentVariables.md " +
codeToString(PI_ERROR_INVALID_VALUE));
}
};

Expand All @@ -168,14 +171,15 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
if (Key == DeviceVendorIdKeyName) {
// DeviceVendorId should have hex format
if (!std::regex_match(Value, std::regex("0[xX][0-9a-fA-F]+"))) {
throw sycl::runtime_error(
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Value " + Value + " for key " + Key +
" is not valid in "
"SYCL_DEVICE_ALLOWLIST. It should have the hex format. For "
"details, please refer to "
"https://github.com/intel/llvm/blob/sycl/sycl/doc/"
"EnvironmentVariables.md",
PI_ERROR_INVALID_VALUE);
"EnvironmentVariables.md " +
codeToString(PI_ERROR_INVALID_VALUE));
}
}
}
Expand All @@ -187,11 +191,12 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
// TODO: can be changed to string_view::starts_with after switching
// DPC++ RT to C++20
if (Prefix != AllowListRaw.substr(ValueStart, Prefix.length())) {
throw sycl::runtime_error("Key " + Key +
" of SYCL_DEVICE_ALLOWLIST should have "
"value which starts with " +
Prefix,
PI_ERROR_INVALID_VALUE);
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Key " + Key +
" of SYCL_DEVICE_ALLOWLIST should have "
"value which starts with " +
Prefix + " " + detail::codeToString(PI_ERROR_INVALID_VALUE));
}
// cut off prefix from the value
ValueStart += Prefix.length();
Expand All @@ -205,12 +210,13 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
// if it is the last iteration and next 2 symbols are not a postfix,
// throw exception
if (ValueEnd == AllowListRaw.length() - Postfix.length())
throw sycl::runtime_error(
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Key " + Key +
" of SYCL_DEVICE_ALLOWLIST should have "
"value which ends with " +
Postfix,
PI_ERROR_INVALID_VALUE);
Postfix + " " +
detail::codeToString(PI_ERROR_INVALID_VALUE));
}
size_t NextExpectedDelimiterPos = ValueEnd + Postfix.length();
// if it is not the end of the string, check that symbol next to a
Expand All @@ -219,13 +225,14 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
(AllowListRaw[NextExpectedDelimiterPos] !=
DelimiterBtwItemsInDeviceDesc) &&
(AllowListRaw[NextExpectedDelimiterPos] != DelimiterBtwDeviceDescs))
throw sycl::runtime_error(
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Unexpected symbol on position " +
std::to_string(NextExpectedDelimiterPos) + ": " +
AllowListRaw[NextExpectedDelimiterPos] +
". Should be either " + DelimiterBtwItemsInDeviceDesc +
" or " + DelimiterBtwDeviceDescs,
PI_ERROR_INVALID_VALUE);
" or " + DelimiterBtwDeviceDescs +
codeToString(PI_ERROR_INVALID_VALUE));

if (AllowListRaw[NextExpectedDelimiterPos] == DelimiterBtwDeviceDescs)
ShouldAllocateNewDeviceDescMap = true;
Expand All @@ -241,10 +248,11 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
// add key and value to the map
DeviceDescMap.emplace(Key, Value);
} else
throw sycl::runtime_error("Re-definition of key " + Key +
" is not allowed in "
"SYCL_DEVICE_ALLOWLIST",
PI_ERROR_INVALID_VALUE);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"Re-definition of key " + Key +
" is not allowed in "
"SYCL_DEVICE_ALLOWLIST " +
codeToString(PI_ERROR_INVALID_VALUE));

KeyStart = ValueEnd;
if (KeyStart != std::string::npos)
Expand Down
32 changes: 19 additions & 13 deletions sycl/source/detail/event_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,21 @@ event_impl::event_impl(sycl::detail::pi::PiEvent Event,
MIsFlushed(true), MState(HES_Complete) {

if (MContext->is_host()) {
throw sycl::invalid_parameter_error(
"The syclContext must match the OpenCL context associated with the "
"clEvent.",
PI_ERROR_INVALID_CONTEXT);
throw sycl::exception(sycl::make_error_code(sycl::errc::invalid),
"The syclContext must match the OpenCL context "
"associated with the clEvent. " +
codeToString(PI_ERROR_INVALID_CONTEXT));
}

sycl::detail::pi::PiContext TempContext;
getPlugin()->call<PiApiKind::piEventGetInfo>(
MEvent, PI_EVENT_INFO_CONTEXT, sizeof(sycl::detail::pi::PiContext),
&TempContext, nullptr);
if (MContext->getHandleRef() != TempContext) {
throw sycl::invalid_parameter_error(
"The syclContext must match the OpenCL context associated with the "
"clEvent.",
PI_ERROR_INVALID_CONTEXT);
throw sycl::exception(sycl::make_error_code(sycl::errc::invalid),
"The syclContext must match the OpenCL context "
"associated with the clEvent. " +
codeToString(PI_ERROR_INVALID_CONTEXT));
}
}

Expand All @@ -160,7 +160,9 @@ event_impl::event_impl(const QueueImplPtr &Queue)
if (Queue->has_property<property::queue::enable_profiling>()) {
MHostProfilingInfo.reset(new HostProfilingInfo());
if (!MHostProfilingInfo)
throw runtime_error("Out of host memory", PI_ERROR_OUT_OF_HOST_MEMORY);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"Out of host memory " +
codeToString(PI_ERROR_OUT_OF_HOST_MEMORY));
}
return;
}
Expand Down Expand Up @@ -290,8 +292,10 @@ event_impl::get_profiling_info<info::event_profiling::command_start>() {
return 0;
}
if (!MHostProfilingInfo)
throw invalid_object_error("Profiling info is not available.",
PI_ERROR_PROFILING_INFO_NOT_AVAILABLE);
throw sycl::exception(
sycl::make_error_code(sycl::errc::invalid),
"Profiling info is not available. " +
codeToString(PI_ERROR_PROFILING_INFO_NOT_AVAILABLE));
return MHostProfilingInfo->getStartTime();
}

Expand All @@ -305,8 +309,10 @@ uint64_t event_impl::get_profiling_info<info::event_profiling::command_end>() {
return 0;
}
if (!MHostProfilingInfo)
throw invalid_object_error("Profiling info is not available.",
PI_ERROR_PROFILING_INFO_NOT_AVAILABLE);
throw sycl::exception(
sycl::make_error_code(sycl::errc::invalid),
"Profiling info is not available. " +
codeToString(PI_ERROR_PROFILING_INFO_NOT_AVAILABLE));
return MHostProfilingInfo->getEndTime();
}

Expand Down
49 changes: 31 additions & 18 deletions sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1529,8 +1529,9 @@ AllocaCommandBase *ExecCGCommand::getAllocaForReq(Requirement *Req) {
if (Dep.MDepRequirement == Req)
return Dep.MAllocaCmd;
}
throw runtime_error("Alloca for command not found",
PI_ERROR_INVALID_OPERATION);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"Alloca for command not found " +
codeToString(PI_ERROR_INVALID_OPERATION));
}

std::vector<std::shared_ptr<const void>>
Expand Down Expand Up @@ -2237,10 +2238,11 @@ static pi_result SetKernelParamsAndLaunch(
}
case kernel_param_kind_t::kind_specialization_constants_buffer: {
if (Queue->is_host()) {
throw sycl::feature_not_supported(
throw sycl::exception(
sycl::make_error_code(sycl::errc::feature_not_supported),
"SYCL2020 specialization constants are not yet supported on host "
"device",
PI_ERROR_INVALID_OPERATION);
"device " +
codeToString(PI_ERROR_INVALID_OPERATION));
}
assert(DeviceImageImpl != nullptr);
sycl::detail::pi::PiMem SpecConstsBuffer =
Expand All @@ -2253,7 +2255,9 @@ static pi_result SetKernelParamsAndLaunch(
break;
}
case kernel_param_kind_t::kind_invalid:
throw runtime_error("Invalid kernel param kind", PI_ERROR_INVALID_VALUE);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"Invalid kernel param kind " +
codeToString(PI_ERROR_INVALID_VALUE));
break;
}
};
Expand Down Expand Up @@ -2502,8 +2506,9 @@ pi_int32 ExecCGCommand::enqueueImp() {
switch (MCommandGroup->getType()) {

case CG::CGTYPE::UpdateHost: {
throw runtime_error("Update host should be handled by the Scheduler.",
PI_ERROR_INVALID_OPERATION);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"Update host should be handled by the Scheduler. " +
codeToString(PI_ERROR_INVALID_VALUE));
}
case CG::CGTYPE::CopyAccToPtr: {
CGCopy *Copy = (CGCopy *)MCommandGroup.get();
Expand Down Expand Up @@ -2644,13 +2649,15 @@ pi_int32 ExecCGCommand::enqueueImp() {

switch (Error) {
case PI_ERROR_INVALID_OPERATION:
throw sycl::runtime_error(
"Device doesn't support run_on_host_intel tasks.", Error);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"Device doesn't support run_on_host_intel tasks. " +
detail::codeToString(Error));
case PI_SUCCESS:
return Error;
default:
throw sycl::runtime_error("Enqueueing run_on_host_intel task has failed.",
Error);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"Enqueueing run_on_host_intel task has failed. " +
detail::codeToString(Error));
}
}
case CG::CGTYPE::Kernel: {
Expand Down Expand Up @@ -2808,7 +2815,9 @@ pi_int32 ExecCGCommand::enqueueImp() {
break;
}
default:
throw runtime_error("Unsupported arg type", PI_ERROR_INVALID_VALUE);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"Unsupported arg type " +
codeToString(PI_ERROR_INVALID_VALUE));
}
}

Expand All @@ -2817,7 +2826,8 @@ pi_int32 ExecCGCommand::enqueueImp() {
if (HostTask->MHostTask->isInteropTask()) {
// Extract the Mem Objects for all Requirements, to ensure they are
// available if a user asks for them inside the interop task scope
const std::vector<Requirement *> &HandlerReq = HostTask->getRequirements();
const std::vector<Requirement *> &HandlerReq =
HostTask->getRequirements();
auto ReqToMemConv = [&ReqToMem, HostTask](Requirement *Req) {
const std::vector<AllocaCommandBase *> &AllocaCmds =
Req->MSYCLMemObj->MRecord->MAllocaCommands;
Expand All @@ -2835,9 +2845,10 @@ pi_int32 ExecCGCommand::enqueueImp() {
assert(false &&
"Can't get memory object due to no allocation available");

throw runtime_error(
"Can't get memory object due to no allocation available",
PI_ERROR_INVALID_MEM_OBJECT);
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Can't get memory object due to no allocation available " +
codeToString(PI_ERROR_INVALID_MEM_OBJECT));
};
std::for_each(std::begin(HandlerReq), std::end(HandlerReq), ReqToMemConv);
std::sort(std::begin(ReqToMem), std::end(ReqToMem));
Expand Down Expand Up @@ -2919,7 +2930,9 @@ pi_int32 ExecCGCommand::enqueueImp() {
throw runtime_error("CG type not implemented.", PI_ERROR_INVALID_OPERATION);
}
case CG::CGTYPE::None:
throw runtime_error("CG type not implemented.", PI_ERROR_INVALID_OPERATION);
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
"CG type not implemented. " +
codeToString(PI_ERROR_INVALID_OPERATION));
}
return PI_ERROR_INVALID_OPERATION;
}
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool exception::has_context() const noexcept { return (MContext != nullptr); }

context exception::get_context() const {
if (!has_context())
throw invalid_object_error();
throw sycl::exception(sycl::errc::invalid);

return *MContext;
}
Expand Down
2 changes: 1 addition & 1 deletion sycl/test-e2e/Basic/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ int main() {
dev, "Preferred interop user sync");
try {
print_info<info::device::parent_device, device>(dev, "Parent device");
} catch (invalid_object_error e) {
} catch (sycl::exception e) {
std::cout << "Expected exception has been caught: " << e.what()
<< std::endl;
}
Expand Down
Loading