DPC++ Runtime
Runtime libraries for oneAPI DPC++
exception.hpp
Go to the documentation of this file.
1 //==---------------- exception.hpp - SYCL exception ------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 // 4.9.2 Exception Class Interface
12 
13 #include <sycl/backend_types.hpp> // for backend
14 #include <sycl/detail/cl.h> // for cl_int
15 #include <sycl/detail/common.hpp> // for codeToString
16 #include <sycl/detail/defines_elementary.hpp> // for __SYCL2020_DEPRECATED
17 #include <sycl/detail/export.hpp> // for __SYCL_EXPORT
18 #include <sycl/detail/pi.h> // for pi_int32
19 #ifdef __INTEL_PREVIEW_BREAKING_CHANGES
20 #include <sycl/detail/string.hpp>
21 #endif
22 
23 #include <exception> // for exception
24 #include <memory> // for allocator, shared_ptr, make...
25 #include <string> // for string, basic_string, opera...
26 #include <system_error> // for error_code, error_category
27 #include <type_traits> // for true_type
28 
29 namespace sycl {
30 inline namespace _V1 {
31 
32 // Forward declaration
33 class context;
34 
35 enum class errc : unsigned int {
36  success = 0,
37  runtime = 1,
38  kernel = 2,
39  accessor = 3,
40  nd_range = 4,
41  event = 5,
42  kernel_argument = 6,
43  build = 7,
44  invalid = 8,
46  platform = 10,
47  profiling = 11,
50  backend_mismatch = 14,
51 };
52 
53 template <backend B> using errc_for = typename backend_traits<B>::errc;
54 
57 
58 __SYCL_EXPORT const std::error_category &sycl_category() noexcept;
59 
60 namespace detail {
61 class __SYCL_EXPORT SYCLCategory : public std::error_category {
62 public:
63  const char *name() const noexcept override { return "sycl"; }
64  std::string message(int) const override { return "SYCL Error"; }
65 };
66 } // namespace detail
67 
68 // Derive from std::exception so uncaught exceptions are printed in c++ default
69 // exception handler.
71 class __SYCL_EXPORT exception : public virtual std::exception {
72 public:
73  __SYCL2020_DEPRECATED("The version of an exception constructor which takes "
74  "no arguments is deprecated.")
75  exception() = default;
76  virtual ~exception();
77 
78  exception(std::error_code, const char *Msg);
79 
80  exception(std::error_code Ec, const std::string &Msg)
81  : exception(Ec, nullptr, Msg.c_str()) {}
82 
83  // new SYCL 2020 constructors
85  exception(int EV, const std::error_category &ECat, const std::string &WhatArg)
86  : exception(EV, ECat, WhatArg.c_str()) {}
87  exception(int, const std::error_category &, const char *);
88  exception(int, const std::error_category &);
89 
90  exception(context, std::error_code, const std::string &);
91  exception(context, std::error_code, const char *);
93  exception(context, int, const std::error_category &, const std::string &);
94  exception(context, int, const std::error_category &, const char *);
95  exception(context, int, const std::error_category &);
96 
97  const std::error_code &code() const noexcept;
98  const std::error_category &category() const noexcept;
99 
100  const char *what() const noexcept final;
101 
102  bool has_context() const noexcept;
103 
104  context get_context() const;
105 
107  cl_int get_cl_code() const;
108 
109 private:
110  // Exceptions must be noexcept copy constructible, so cannot use std::string
111  // directly.
112 #ifdef __INTEL_PREVIEW_BREAKING_CHANGES
113  std::shared_ptr<detail::string> MMsg;
114 #else
115  std::shared_ptr<std::string> MMsg;
116 #endif
117  pi_int32 MPIErr = 0;
118  std::shared_ptr<context> MContext;
119  std::error_code MErrC = make_error_code(sycl::errc::invalid);
120 
121 protected:
122  // base constructors used by SYCL 1.2.1 exception subclasses
123  exception(std::error_code Ec, const char *Msg, const pi_int32 PIErr,
124  std::shared_ptr<context> Context = nullptr)
125  : exception(Ec, std::string(Msg), PIErr, Context) {}
126 
127  exception(std::error_code Ec, const std::string &Msg, const pi_int32 PIErr,
128  std::shared_ptr<context> Context = nullptr)
129  : exception(Ec, Context, Msg + " " + detail::codeToString(PIErr)) {
130  MPIErr = PIErr;
131  }
132 
133  exception(const std::string &Msg)
134 #ifdef __INTEL_PREVIEW_BREAKING_CHANGES
135  : MMsg(std::make_shared<detail::string>(Msg)), MContext(nullptr){}
136 #else
137  : MMsg(std::make_shared<std::string>(Msg)), MContext(nullptr) {
138  }
139 #endif
140 
141  // base constructor for all SYCL 2020 constructors
142  // exception(context *ctxPtr, std::error_code Ec, const std::string
143  // &what_arg);
144  exception(std::error_code Ec, std::shared_ptr<context> SharedPtrCtx,
145  const std::string &what_arg)
146  : exception(Ec, SharedPtrCtx, what_arg.c_str()) {
147  }
148  exception(std::error_code Ec, std::shared_ptr<context> SharedPtrCtx,
149  const char *WhatArg);
150 };
151 
153  "use sycl::exception with sycl::errc::runtime instead.") runtime_error
154  : public exception {
155 public:
156  runtime_error() : exception(make_error_code(errc::runtime)) {}
157 
158  runtime_error(const char *Msg, pi_int32 Err)
159  : runtime_error(std::string(Msg), Err) {}
160 
161  runtime_error(const std::string &Msg, pi_int32 Err)
162  : exception(make_error_code(errc::runtime), Msg, Err) {}
163 
164  runtime_error(std::error_code Ec, const std::string &Msg,
165  const pi_int32 PIErr)
166  : exception(Ec, Msg, PIErr) {}
167 
168 protected:
169  runtime_error(std::error_code Ec) : exception(Ec) {}
170 };
171 
172 class __SYCL2020_DEPRECATED("use sycl::exception with sycl::errc::kernel or "
173  "errc::kernel_argument instead.") kernel_error
174  : public runtime_error {
175 public:
176  kernel_error() : runtime_error(make_error_code(errc::kernel)) {}
177 
178  kernel_error(const char *Msg, pi_int32 Err)
179  : kernel_error(std::string(Msg), Err) {}
180 
181  kernel_error(const std::string &Msg, pi_int32 Err)
182  : runtime_error(make_error_code(errc::kernel), Msg, Err) {}
183 };
184 
186  "use sycl::exception with sycl::errc::accessor instead.") accessor_error
187  : public runtime_error {
188 public:
189  accessor_error() : runtime_error(make_error_code(errc::accessor)) {}
190 
191  accessor_error(const char *Msg, pi_int32 Err)
192  : accessor_error(std::string(Msg), Err) {}
193 
194  accessor_error(const std::string &Msg, pi_int32 Err)
195  : runtime_error(make_error_code(errc::accessor), Msg, Err) {}
196 };
197 
199  "use sycl::exception with sycl::errc::nd_range instead.") nd_range_error
200  : public runtime_error {
201 public:
202  nd_range_error() : runtime_error(make_error_code(errc::nd_range)) {}
203 
204  nd_range_error(const char *Msg, pi_int32 Err)
205  : nd_range_error(std::string(Msg), Err) {}
206 
207  nd_range_error(const std::string &Msg, pi_int32 Err)
208  : runtime_error(make_error_code(errc::nd_range), Msg, Err) {}
209 };
210 
212  "use sycl::exception with sycl::errc::event instead.") event_error
213  : public runtime_error {
214 public:
215  event_error() : runtime_error(make_error_code(errc::event)) {}
216 
217  event_error(const char *Msg, pi_int32 Err)
218  : event_error(std::string(Msg), Err) {}
219 
220  event_error(const std::string &Msg, pi_int32 Err)
221  : runtime_error(make_error_code(errc::event), Msg, Err) {}
222 };
223 
225  "use sycl::exception with a sycl::errc enum value instead.")
226  invalid_parameter_error : public runtime_error {
227 public:
228  invalid_parameter_error()
229  : runtime_error(make_error_code(errc::kernel_argument)) {}
230 
231  invalid_parameter_error(const char *Msg, pi_int32 Err)
232  : invalid_parameter_error(std::string(Msg), Err) {}
233 
234  invalid_parameter_error(const std::string &Msg, pi_int32 Err)
235  : runtime_error(make_error_code(errc::kernel_argument), Msg, Err) {}
236 };
237 
239  "use sycl::exception with a sycl::errc enum value instead.") device_error
240  : public exception {
241 public:
242  device_error() : exception(make_error_code(errc::invalid)) {}
243 
244  device_error(const char *Msg, pi_int32 Err)
245  : device_error(std::string(Msg), Err) {}
246 
247  device_error(const std::string &Msg, pi_int32 Err)
248  : exception(make_error_code(errc::invalid), Msg, Err) {}
249 
250 protected:
251  device_error(std::error_code Ec) : exception(Ec) {}
252 
253  device_error(std::error_code Ec, const std::string &Msg, const pi_int32 PIErr)
254  : exception(Ec, Msg, PIErr) {}
255 };
256 
258  "use sycl::exception with a sycl::errc enum value instead.")
259  compile_program_error : public device_error {
260 public:
261  compile_program_error() : device_error(make_error_code(errc::build)) {}
262 
263  compile_program_error(const char *Msg, pi_int32 Err)
264  : compile_program_error(std::string(Msg), Err) {}
265 
266  compile_program_error(const std::string &Msg, pi_int32 Err)
267  : device_error(make_error_code(errc::build), Msg, Err) {}
268 };
269 
271  "use sycl::exception with a sycl::errc enum value instead.")
272  link_program_error : public device_error {
273 public:
274  link_program_error() : device_error(make_error_code(errc::build)) {}
275 
276  link_program_error(const char *Msg, pi_int32 Err)
277  : link_program_error(std::string(Msg), Err) {}
278 
279  link_program_error(const std::string &Msg, pi_int32 Err)
280  : device_error(make_error_code(errc::build), Msg, Err) {}
281 };
282 
284  "use sycl::exception with a sycl::errc enum value instead.")
285  invalid_object_error : public device_error {
286 public:
287  invalid_object_error() : device_error(make_error_code(errc::invalid)) {}
288 
289  invalid_object_error(const char *Msg, pi_int32 Err)
290  : invalid_object_error(std::string(Msg), Err) {}
291 
292  invalid_object_error(const std::string &Msg, pi_int32 Err)
293  : device_error(make_error_code(errc::invalid), Msg, Err) {}
294 };
295 
297  "use sycl::exception with sycl::errc::memory_allocation instead.")
298  memory_allocation_error : public device_error {
299 public:
300  memory_allocation_error()
301  : device_error(make_error_code(errc::memory_allocation)) {}
302 
303  memory_allocation_error(const char *Msg, pi_int32 Err)
304  : memory_allocation_error(std::string(Msg), Err) {}
305 
306  memory_allocation_error(const std::string &Msg, pi_int32 Err)
307  : device_error(make_error_code(errc::memory_allocation), Msg, Err) {}
308 };
309 
311  "use sycl::exception with sycl::errc::platform instead.") platform_error
312  : public device_error {
313 public:
314  platform_error() : device_error(make_error_code(errc::platform)) {}
315 
316  platform_error(const char *Msg, pi_int32 Err)
317  : platform_error(std::string(Msg), Err) {}
318 
319  platform_error(const std::string &Msg, pi_int32 Err)
320  : device_error(make_error_code(errc::platform), Msg, Err) {}
321 };
322 
324  "use sycl::exception with sycl::errc::profiling instead.") profiling_error
325  : public device_error {
326 public:
327  profiling_error() : device_error(make_error_code(errc::profiling)) {}
328 
329  profiling_error(const char *Msg, pi_int32 Err)
330  : profiling_error(std::string(Msg), Err) {}
331 
332  profiling_error(const std::string &Msg, pi_int32 Err)
333  : device_error(make_error_code(errc::profiling), Msg, Err) {}
334 };
335 
337  "use sycl::exception with sycl::errc::feature_not_supported instead.")
338  feature_not_supported : public device_error {
339 public:
341  : device_error(make_error_code(errc::feature_not_supported)) {}
342 
343  feature_not_supported(const char *Msg, pi_int32 Err)
344  : feature_not_supported(std::string(Msg), Err) {}
345 
346  feature_not_supported(const std::string &Msg, pi_int32 Err)
347  : device_error(make_error_code(errc::feature_not_supported), Msg, Err) {}
348 };
349 
350 } // namespace _V1
351 } // namespace sycl
352 
353 namespace std {
354 template <> struct is_error_code_enum<sycl::errc> : true_type {};
355 } // namespace std
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:50
std::string message(int) const override
Definition: exception.hpp:64
const char * name() const noexcept override
Definition: exception.hpp:63
exception(std::error_code Ec, std::shared_ptr< context > SharedPtrCtx, const std::string &what_arg)
Definition: exception.hpp:144
exception(std::error_code Ec, const std::string &Msg, const pi_int32 PIErr, std::shared_ptr< context > Context=nullptr)
Definition: exception.hpp:127
exception(const std::string &Msg)
Definition: exception.hpp:133
exception(std::error_code Ec, const char *Msg, const pi_int32 PIErr, std::shared_ptr< context > Context=nullptr)
Definition: exception.hpp:123
__SYCL2020_DEPRECATED("The version of an exception constructor which takes " "no arguments is deprecated.") exception()=default
exception(int EV, const std::error_category &ECat, const std::string &WhatArg)
Definition: exception.hpp:85
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:77
Defines the iteration domain of both the work-groups and the overall dispatch.
Definition: nd_range.hpp:22
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:99
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor accessor(buffer< DataT, Dimensions, AllocatorT >) -> accessor< DataT, Dimensions, access::mode::read_write, target::device, access::placeholder::true_t >
Buffer accessor.
std::string codeToString(pi_int32 code)
Definition: common.hpp:153
std::int32_t cl_int
Definition: aliases.hpp:134
typename backend_traits< B >::errc errc_for
Definition: exception.hpp:53
kernel_bundle< bundle_state::executable > build(const kernel_bundle< bundle_state::input > &InputBundle, const std::vector< device > &Devs, const property_list &PropList={})
signed char __SYCL2020_DEPRECATED
Definition: aliases.hpp:94
const std::error_category & sycl_category() noexcept
Definition: exception.cpp:86
std::uint8_t instead
Definition: aliases.hpp:93
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
Definition: exception.cpp:91
Definition: access.hpp:18
error_code
Definition: defs.hpp:59
int32_t pi_int32
Definition: pi.h:212
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324