Skip to content

Commit ed9e4aa

Browse files
pwisniewskimobicaChenyang-L
authored andcommitted
[SYCL] Adapt to sycl 2020 exceptions (#9771)
Due to issue #7268 I changed old exceptions to new ones (`sycl::exception`)
1 parent 138c387 commit ed9e4aa

File tree

9 files changed

+127
-100
lines changed

9 files changed

+127
-100
lines changed

sycl/source/backend.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ static const PluginPtr &getPlugin(backend Backend) {
3838
case backend::ext_oneapi_cuda:
3939
return pi::getPlugin<backend::ext_oneapi_cuda>();
4040
default:
41-
throw sycl::runtime_error{"getPlugin: Unsupported backend",
42-
PI_ERROR_INVALID_OPERATION};
41+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
42+
"getPlugin: Unsupported backend " +
43+
detail::codeToString(PI_ERROR_INVALID_OPERATION));
4344
}
4445
}
4546

@@ -196,21 +197,19 @@ make_kernel_bundle(pi_native_handle NativeHandle, const context &TargetContext,
196197
case (PI_PROGRAM_BINARY_TYPE_COMPILED_OBJECT):
197198
case (PI_PROGRAM_BINARY_TYPE_LIBRARY):
198199
if (State == bundle_state::input)
199-
// TODO SYCL2020 exception
200-
throw sycl::runtime_error(errc::invalid,
201-
"Program and kernel_bundle state mismatch",
202-
PI_ERROR_INVALID_VALUE);
200+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
201+
"Program and kernel_bundle state mismatch " +
202+
detail::codeToString(PI_ERROR_INVALID_VALUE));
203203
if (State == bundle_state::executable)
204204
Plugin->call<errc::build, PiApiKind::piProgramLink>(
205205
ContextImpl->getHandleRef(), 1, &Dev, nullptr, 1, &PiProgram,
206206
nullptr, nullptr, &PiProgram);
207207
break;
208208
case (PI_PROGRAM_BINARY_TYPE_EXECUTABLE):
209209
if (State == bundle_state::input || State == bundle_state::object)
210-
// TODO SYCL2020 exception
211-
throw sycl::runtime_error(errc::invalid,
212-
"Program and kernel_bundle state mismatch",
213-
PI_ERROR_INVALID_VALUE);
210+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
211+
"Program and kernel_bundle state mismatch " +
212+
detail::codeToString(PI_ERROR_INVALID_VALUE));
214213
break;
215214
}
216215
}
@@ -264,9 +263,10 @@ kernel make_kernel(const context &TargetContext,
264263
pi::PiProgram PiProgram = nullptr;
265264
if (Backend == backend::ext_oneapi_level_zero) {
266265
if (KernelBundleImpl->size() != 1)
267-
throw sycl::runtime_error{
268-
"make_kernel: kernel_bundle must have single program image",
269-
PI_ERROR_INVALID_PROGRAM};
266+
throw sycl::exception(
267+
sycl::make_error_code(sycl::errc::runtime),
268+
"make_kernel: kernel_bundle must have single program image " +
269+
detail::codeToString(PI_ERROR_INVALID_PROGRAM));
270270

271271
const device_image<bundle_state::executable> &DeviceImage =
272272
*KernelBundle.begin();

sycl/source/detail/allowlist.cpp

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
7272
const char DelimiterBtwDeviceDescs = '|';
7373

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

8182
const std::string &DeprecatedKeyNameDeviceName = DeviceNameKeyName;
8283
const std::string &DeprecatedKeyNamePlatformName = PlatformNameKeyName;
@@ -95,12 +96,13 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
9596
if (std::find(SupportedAllowListKeyNames.begin(),
9697
SupportedAllowListKeyNames.end(),
9798
Key) == SupportedAllowListKeyNames.end()) {
98-
throw sycl::runtime_error(
99+
throw sycl::exception(
100+
sycl::make_error_code(sycl::errc::runtime),
99101
"Unrecognized key in SYCL_DEVICE_ALLOWLIST. For details, please "
100102
"refer to "
101103
"https://github.com/intel/llvm/blob/sycl/sycl/doc/"
102-
"EnvironmentVariables.md",
103-
PI_ERROR_INVALID_VALUE);
104+
"EnvironmentVariables.md " +
105+
codeToString(PI_ERROR_INVALID_VALUE));
104106
}
105107

106108
if (Key == DeprecatedKeyNameDeviceName) {
@@ -149,13 +151,14 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
149151
break;
150152
}
151153
if (!ValueIsValid)
152-
throw sycl::runtime_error(
154+
throw sycl::exception(
155+
sycl::make_error_code(sycl::errc::runtime),
153156
"Value " + Value + " for key " + Key +
154157
" is not valid in "
155158
"SYCL_DEVICE_ALLOWLIST. For details, please refer to "
156159
"https://github.com/intel/llvm/blob/sycl/sycl/doc/"
157-
"EnvironmentVariables.md",
158-
PI_ERROR_INVALID_VALUE);
160+
"EnvironmentVariables.md " +
161+
codeToString(PI_ERROR_INVALID_VALUE));
159162
}
160163
};
161164

@@ -168,14 +171,15 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
168171
if (Key == DeviceVendorIdKeyName) {
169172
// DeviceVendorId should have hex format
170173
if (!std::regex_match(Value, std::regex("0[xX][0-9a-fA-F]+"))) {
171-
throw sycl::runtime_error(
174+
throw sycl::exception(
175+
sycl::make_error_code(sycl::errc::runtime),
172176
"Value " + Value + " for key " + Key +
173177
" is not valid in "
174178
"SYCL_DEVICE_ALLOWLIST. It should have the hex format. For "
175179
"details, please refer to "
176180
"https://github.com/intel/llvm/blob/sycl/sycl/doc/"
177-
"EnvironmentVariables.md",
178-
PI_ERROR_INVALID_VALUE);
181+
"EnvironmentVariables.md " +
182+
codeToString(PI_ERROR_INVALID_VALUE));
179183
}
180184
}
181185
}
@@ -187,11 +191,12 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
187191
// TODO: can be changed to string_view::starts_with after switching
188192
// DPC++ RT to C++20
189193
if (Prefix != AllowListRaw.substr(ValueStart, Prefix.length())) {
190-
throw sycl::runtime_error("Key " + Key +
191-
" of SYCL_DEVICE_ALLOWLIST should have "
192-
"value which starts with " +
193-
Prefix,
194-
PI_ERROR_INVALID_VALUE);
194+
throw sycl::exception(
195+
sycl::make_error_code(sycl::errc::runtime),
196+
"Key " + Key +
197+
" of SYCL_DEVICE_ALLOWLIST should have "
198+
"value which starts with " +
199+
Prefix + " " + detail::codeToString(PI_ERROR_INVALID_VALUE));
195200
}
196201
// cut off prefix from the value
197202
ValueStart += Prefix.length();
@@ -205,12 +210,13 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
205210
// if it is the last iteration and next 2 symbols are not a postfix,
206211
// throw exception
207212
if (ValueEnd == AllowListRaw.length() - Postfix.length())
208-
throw sycl::runtime_error(
213+
throw sycl::exception(
214+
sycl::make_error_code(sycl::errc::runtime),
209215
"Key " + Key +
210216
" of SYCL_DEVICE_ALLOWLIST should have "
211217
"value which ends with " +
212-
Postfix,
213-
PI_ERROR_INVALID_VALUE);
218+
Postfix + " " +
219+
detail::codeToString(PI_ERROR_INVALID_VALUE));
214220
}
215221
size_t NextExpectedDelimiterPos = ValueEnd + Postfix.length();
216222
// if it is not the end of the string, check that symbol next to a
@@ -219,13 +225,14 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
219225
(AllowListRaw[NextExpectedDelimiterPos] !=
220226
DelimiterBtwItemsInDeviceDesc) &&
221227
(AllowListRaw[NextExpectedDelimiterPos] != DelimiterBtwDeviceDescs))
222-
throw sycl::runtime_error(
228+
throw sycl::exception(
229+
sycl::make_error_code(sycl::errc::runtime),
223230
"Unexpected symbol on position " +
224231
std::to_string(NextExpectedDelimiterPos) + ": " +
225232
AllowListRaw[NextExpectedDelimiterPos] +
226233
". Should be either " + DelimiterBtwItemsInDeviceDesc +
227-
" or " + DelimiterBtwDeviceDescs,
228-
PI_ERROR_INVALID_VALUE);
234+
" or " + DelimiterBtwDeviceDescs +
235+
codeToString(PI_ERROR_INVALID_VALUE));
229236

230237
if (AllowListRaw[NextExpectedDelimiterPos] == DelimiterBtwDeviceDescs)
231238
ShouldAllocateNewDeviceDescMap = true;
@@ -241,10 +248,11 @@ AllowListParsedT parseAllowList(const std::string &AllowListRaw) {
241248
// add key and value to the map
242249
DeviceDescMap.emplace(Key, Value);
243250
} else
244-
throw sycl::runtime_error("Re-definition of key " + Key +
245-
" is not allowed in "
246-
"SYCL_DEVICE_ALLOWLIST",
247-
PI_ERROR_INVALID_VALUE);
251+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
252+
"Re-definition of key " + Key +
253+
" is not allowed in "
254+
"SYCL_DEVICE_ALLOWLIST " +
255+
codeToString(PI_ERROR_INVALID_VALUE));
248256

249257
KeyStart = ValueEnd;
250258
if (KeyStart != std::string::npos)

sycl/source/detail/event_impl.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,21 @@ event_impl::event_impl(sycl::detail::pi::PiEvent Event,
130130
MIsFlushed(true), MState(HES_Complete) {
131131

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

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

@@ -160,7 +160,9 @@ event_impl::event_impl(const QueueImplPtr &Queue)
160160
if (Queue->has_property<property::queue::enable_profiling>()) {
161161
MHostProfilingInfo.reset(new HostProfilingInfo());
162162
if (!MHostProfilingInfo)
163-
throw runtime_error("Out of host memory", PI_ERROR_OUT_OF_HOST_MEMORY);
163+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
164+
"Out of host memory " +
165+
codeToString(PI_ERROR_OUT_OF_HOST_MEMORY));
164166
}
165167
return;
166168
}
@@ -290,8 +292,10 @@ event_impl::get_profiling_info<info::event_profiling::command_start>() {
290292
return 0;
291293
}
292294
if (!MHostProfilingInfo)
293-
throw invalid_object_error("Profiling info is not available.",
294-
PI_ERROR_PROFILING_INFO_NOT_AVAILABLE);
295+
throw sycl::exception(
296+
sycl::make_error_code(sycl::errc::invalid),
297+
"Profiling info is not available. " +
298+
codeToString(PI_ERROR_PROFILING_INFO_NOT_AVAILABLE));
295299
return MHostProfilingInfo->getStartTime();
296300
}
297301

@@ -305,8 +309,10 @@ uint64_t event_impl::get_profiling_info<info::event_profiling::command_end>() {
305309
return 0;
306310
}
307311
if (!MHostProfilingInfo)
308-
throw invalid_object_error("Profiling info is not available.",
309-
PI_ERROR_PROFILING_INFO_NOT_AVAILABLE);
312+
throw sycl::exception(
313+
sycl::make_error_code(sycl::errc::invalid),
314+
"Profiling info is not available. " +
315+
codeToString(PI_ERROR_PROFILING_INFO_NOT_AVAILABLE));
310316
return MHostProfilingInfo->getEndTime();
311317
}
312318

sycl/source/detail/scheduler/commands.cpp

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,8 +1529,9 @@ AllocaCommandBase *ExecCGCommand::getAllocaForReq(Requirement *Req) {
15291529
if (Dep.MDepRequirement == Req)
15301530
return Dep.MAllocaCmd;
15311531
}
1532-
throw runtime_error("Alloca for command not found",
1533-
PI_ERROR_INVALID_OPERATION);
1532+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
1533+
"Alloca for command not found " +
1534+
codeToString(PI_ERROR_INVALID_OPERATION));
15341535
}
15351536

15361537
std::vector<std::shared_ptr<const void>>
@@ -2237,10 +2238,11 @@ static pi_result SetKernelParamsAndLaunch(
22372238
}
22382239
case kernel_param_kind_t::kind_specialization_constants_buffer: {
22392240
if (Queue->is_host()) {
2240-
throw sycl::feature_not_supported(
2241+
throw sycl::exception(
2242+
sycl::make_error_code(sycl::errc::feature_not_supported),
22412243
"SYCL2020 specialization constants are not yet supported on host "
2242-
"device",
2243-
PI_ERROR_INVALID_OPERATION);
2244+
"device " +
2245+
codeToString(PI_ERROR_INVALID_OPERATION));
22442246
}
22452247
assert(DeviceImageImpl != nullptr);
22462248
sycl::detail::pi::PiMem SpecConstsBuffer =
@@ -2253,7 +2255,9 @@ static pi_result SetKernelParamsAndLaunch(
22532255
break;
22542256
}
22552257
case kernel_param_kind_t::kind_invalid:
2256-
throw runtime_error("Invalid kernel param kind", PI_ERROR_INVALID_VALUE);
2258+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
2259+
"Invalid kernel param kind " +
2260+
codeToString(PI_ERROR_INVALID_VALUE));
22572261
break;
22582262
}
22592263
};
@@ -2502,8 +2506,9 @@ pi_int32 ExecCGCommand::enqueueImp() {
25022506
switch (MCommandGroup->getType()) {
25032507

25042508
case CG::CGTYPE::UpdateHost: {
2505-
throw runtime_error("Update host should be handled by the Scheduler.",
2506-
PI_ERROR_INVALID_OPERATION);
2509+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
2510+
"Update host should be handled by the Scheduler. " +
2511+
codeToString(PI_ERROR_INVALID_VALUE));
25072512
}
25082513
case CG::CGTYPE::CopyAccToPtr: {
25092514
CGCopy *Copy = (CGCopy *)MCommandGroup.get();
@@ -2644,13 +2649,15 @@ pi_int32 ExecCGCommand::enqueueImp() {
26442649

26452650
switch (Error) {
26462651
case PI_ERROR_INVALID_OPERATION:
2647-
throw sycl::runtime_error(
2648-
"Device doesn't support run_on_host_intel tasks.", Error);
2652+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
2653+
"Device doesn't support run_on_host_intel tasks. " +
2654+
detail::codeToString(Error));
26492655
case PI_SUCCESS:
26502656
return Error;
26512657
default:
2652-
throw sycl::runtime_error("Enqueueing run_on_host_intel task has failed.",
2653-
Error);
2658+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
2659+
"Enqueueing run_on_host_intel task has failed. " +
2660+
detail::codeToString(Error));
26542661
}
26552662
}
26562663
case CG::CGTYPE::Kernel: {
@@ -2808,7 +2815,9 @@ pi_int32 ExecCGCommand::enqueueImp() {
28082815
break;
28092816
}
28102817
default:
2811-
throw runtime_error("Unsupported arg type", PI_ERROR_INVALID_VALUE);
2818+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
2819+
"Unsupported arg type " +
2820+
codeToString(PI_ERROR_INVALID_VALUE));
28122821
}
28132822
}
28142823

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

2838-
throw runtime_error(
2839-
"Can't get memory object due to no allocation available",
2840-
PI_ERROR_INVALID_MEM_OBJECT);
2848+
throw sycl::exception(
2849+
sycl::make_error_code(sycl::errc::runtime),
2850+
"Can't get memory object due to no allocation available " +
2851+
codeToString(PI_ERROR_INVALID_MEM_OBJECT));
28412852
};
28422853
std::for_each(std::begin(HandlerReq), std::end(HandlerReq), ReqToMemConv);
28432854
std::sort(std::begin(ReqToMem), std::end(ReqToMem));
@@ -2919,7 +2930,9 @@ pi_int32 ExecCGCommand::enqueueImp() {
29192930
throw runtime_error("CG type not implemented.", PI_ERROR_INVALID_OPERATION);
29202931
}
29212932
case CG::CGTYPE::None:
2922-
throw runtime_error("CG type not implemented.", PI_ERROR_INVALID_OPERATION);
2933+
throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
2934+
"CG type not implemented. " +
2935+
codeToString(PI_ERROR_INVALID_OPERATION));
29232936
}
29242937
return PI_ERROR_INVALID_OPERATION;
29252938
}

sycl/source/exception.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ bool exception::has_context() const noexcept { return (MContext != nullptr); }
7979

8080
context exception::get_context() const {
8181
if (!has_context())
82-
throw invalid_object_error();
82+
throw sycl::exception(sycl::errc::invalid);
8383

8484
return *MContext;
8585
}

sycl/test-e2e/Basic/info.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ int main() {
337337
dev, "Preferred interop user sync");
338338
try {
339339
print_info<info::device::parent_device, device>(dev, "Parent device");
340-
} catch (invalid_object_error e) {
340+
} catch (sycl::exception e) {
341341
std::cout << "Expected exception has been caught: " << e.what()
342342
<< std::endl;
343343
}

0 commit comments

Comments
 (0)