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 
20 #include <exception> // for exception
21 #include <memory> // for allocator, shared_ptr, make...
22 #include <string> // for string, basic_string, opera...
23 #include <system_error> // for error_code, error_category
24 #include <type_traits> // for true_type
25 
26 namespace sycl {
27 inline namespace _V1 {
28 
29 // Forward declaration
30 class context;
31 
32 enum class errc : unsigned int {
33  success = 0,
34  runtime = 1,
35  kernel = 2,
36  accessor = 3,
37  nd_range = 4,
38  event = 5,
39  kernel_argument = 6,
40  build = 7,
41  invalid = 8,
43  platform = 10,
44  profiling = 11,
47  backend_mismatch = 14,
48 };
49 
50 template <backend B> using errc_for = typename backend_traits<B>::errc;
51 
53 __SYCL_EXPORT std::error_code make_error_code(sycl::errc E) noexcept;
54 
55 __SYCL_EXPORT const std::error_category &sycl_category() noexcept;
56 
57 namespace detail {
58 class __SYCL_EXPORT SYCLCategory : public std::error_category {
59 public:
60  const char *name() const noexcept override { return "sycl"; }
61  std::string message(int) const override { return "SYCL Error"; }
62 };
63 } // namespace detail
64 
65 // Derive from std::exception so uncaught exceptions are printed in c++ default
66 // exception handler.
68 class __SYCL_EXPORT exception : public virtual std::exception {
69 public:
70  __SYCL2020_DEPRECATED("The version of an exception constructor which takes "
71  "no arguments is deprecated.")
72  exception() = default;
73  virtual ~exception();
74 
75  exception(std::error_code, const char *Msg);
76 
77  exception(std::error_code, const std::string &Msg);
78 
79  // new SYCL 2020 constructors
80  exception(std::error_code);
81  exception(int, const std::error_category &, const std::string &);
82  exception(int, const std::error_category &, const char *);
83  exception(int, const std::error_category &);
84 
85  exception(context, std::error_code, const std::string &);
86  exception(context, std::error_code, const char *);
87  exception(context, std::error_code);
88  exception(context, int, const std::error_category &, const std::string &);
89  exception(context, int, const std::error_category &, const char *);
90  exception(context, int, const std::error_category &);
91 
92  const std::error_code &code() const noexcept;
93  const std::error_category &category() const noexcept;
94 
95  const char *what() const noexcept final;
96 
97  bool has_context() const noexcept;
98 
99  context get_context() const;
100 
102  cl_int get_cl_code() const;
103 
104 private:
105  // Exceptions must be noexcept copy constructible, so cannot use std::string
106  // directly.
107  std::shared_ptr<std::string> MMsg;
108  pi_int32 MPIErr = 0;
109  std::shared_ptr<context> MContext;
110  std::error_code MErrC = make_error_code(sycl::errc::invalid);
111 
112 protected:
113  // base constructors used by SYCL 1.2.1 exception subclasses
114  exception(std::error_code ec, const char *Msg, const pi_int32 PIErr,
115  std::shared_ptr<context> Context = nullptr)
116  : exception(ec, std::string(Msg), PIErr, Context) {}
117 
118  exception(std::error_code ec, const std::string &Msg, const pi_int32 PIErr,
119  std::shared_ptr<context> Context = nullptr)
120  : exception(ec, Context, Msg + " " + detail::codeToString(PIErr)) {
121  MPIErr = PIErr;
122  }
123 
124  exception(const std::string &Msg)
125  : MMsg(std::make_shared<std::string>(Msg)), MContext(nullptr) {}
126 
127  // base constructor for all SYCL 2020 constructors
128  // exception(context *ctxPtr, std::error_code ec, const std::string
129  // &what_arg);
130  exception(std::error_code ec, std::shared_ptr<context> SharedPtrCtx,
131  const std::string &what_arg);
132 };
133 
135  "use sycl::exception with sycl::errc::runtime instead.") runtime_error
136  : public exception {
137 public:
138  runtime_error() : exception(make_error_code(errc::runtime)) {}
139 
140  runtime_error(const char *Msg, pi_int32 Err)
141  : runtime_error(std::string(Msg), Err) {}
142 
143  runtime_error(const std::string &Msg, pi_int32 Err)
144  : exception(make_error_code(errc::runtime), Msg, Err) {}
145 
146  runtime_error(std::error_code ec, const std::string &Msg,
147  const pi_int32 PIErr)
148  : exception(ec, Msg, PIErr) {}
149 
150 protected:
151  runtime_error(std::error_code ec) : exception(ec) {}
152 };
153 
154 class __SYCL2020_DEPRECATED("use sycl::exception with sycl::errc::kernel or "
155  "errc::kernel_argument instead.") kernel_error
156  : public runtime_error {
157 public:
158  kernel_error() : runtime_error(make_error_code(errc::kernel)) {}
159 
160  kernel_error(const char *Msg, pi_int32 Err)
161  : kernel_error(std::string(Msg), Err) {}
162 
163  kernel_error(const std::string &Msg, pi_int32 Err)
164  : runtime_error(make_error_code(errc::kernel), Msg, Err) {}
165 };
166 
168  "use sycl::exception with sycl::errc::accessor instead.") accessor_error
169  : public runtime_error {
170 public:
171  accessor_error() : runtime_error(make_error_code(errc::accessor)) {}
172 
173  accessor_error(const char *Msg, pi_int32 Err)
174  : accessor_error(std::string(Msg), Err) {}
175 
176  accessor_error(const std::string &Msg, pi_int32 Err)
177  : runtime_error(make_error_code(errc::accessor), Msg, Err) {}
178 };
179 
181  "use sycl::exception with sycl::errc::nd_range instead.") nd_range_error
182  : public runtime_error {
183 public:
184  nd_range_error() : runtime_error(make_error_code(errc::nd_range)) {}
185 
186  nd_range_error(const char *Msg, pi_int32 Err)
187  : nd_range_error(std::string(Msg), Err) {}
188 
189  nd_range_error(const std::string &Msg, pi_int32 Err)
190  : runtime_error(make_error_code(errc::nd_range), Msg, Err) {}
191 };
192 
194  "use sycl::exception with sycl::errc::event instead.") event_error
195  : public runtime_error {
196 public:
197  event_error() : runtime_error(make_error_code(errc::event)) {}
198 
199  event_error(const char *Msg, pi_int32 Err)
200  : event_error(std::string(Msg), Err) {}
201 
202  event_error(const std::string &Msg, pi_int32 Err)
203  : runtime_error(make_error_code(errc::event), Msg, Err) {}
204 };
205 
207  "use sycl::exception with a sycl::errc enum value instead.")
208  invalid_parameter_error : public runtime_error {
209 public:
210  invalid_parameter_error()
211  : runtime_error(make_error_code(errc::kernel_argument)) {}
212 
213  invalid_parameter_error(const char *Msg, pi_int32 Err)
214  : invalid_parameter_error(std::string(Msg), Err) {}
215 
216  invalid_parameter_error(const std::string &Msg, pi_int32 Err)
217  : runtime_error(make_error_code(errc::kernel_argument), Msg, Err) {}
218 };
219 
221  "use sycl::exception with a sycl::errc enum value instead.") device_error
222  : public exception {
223 public:
224  device_error() : exception(make_error_code(errc::invalid)) {}
225 
226  device_error(const char *Msg, pi_int32 Err)
227  : device_error(std::string(Msg), Err) {}
228 
229  device_error(const std::string &Msg, pi_int32 Err)
230  : exception(make_error_code(errc::invalid), Msg, Err) {}
231 
232 protected:
233  device_error(std::error_code ec) : exception(ec) {}
234 
235  device_error(std::error_code ec, const std::string &Msg, const pi_int32 PIErr)
236  : exception(ec, Msg, PIErr) {}
237 };
238 
240  "use sycl::exception with a sycl::errc enum value instead.")
241  compile_program_error : public device_error {
242 public:
243  compile_program_error() : device_error(make_error_code(errc::build)) {}
244 
245  compile_program_error(const char *Msg, pi_int32 Err)
246  : compile_program_error(std::string(Msg), Err) {}
247 
248  compile_program_error(const std::string &Msg, pi_int32 Err)
249  : device_error(make_error_code(errc::build), Msg, Err) {}
250 };
251 
253  "use sycl::exception with a sycl::errc enum value instead.")
254  link_program_error : public device_error {
255 public:
256  link_program_error() : device_error(make_error_code(errc::build)) {}
257 
258  link_program_error(const char *Msg, pi_int32 Err)
259  : link_program_error(std::string(Msg), Err) {}
260 
261  link_program_error(const std::string &Msg, pi_int32 Err)
262  : device_error(make_error_code(errc::build), Msg, Err) {}
263 };
264 
266  "use sycl::exception with a sycl::errc enum value instead.")
267  invalid_object_error : public device_error {
268 public:
269  invalid_object_error() : device_error(make_error_code(errc::invalid)) {}
270 
271  invalid_object_error(const char *Msg, pi_int32 Err)
272  : invalid_object_error(std::string(Msg), Err) {}
273 
274  invalid_object_error(const std::string &Msg, pi_int32 Err)
275  : device_error(make_error_code(errc::invalid), Msg, Err) {}
276 };
277 
279  "use sycl::exception with sycl::errc::memory_allocation instead.")
280  memory_allocation_error : public device_error {
281 public:
282  memory_allocation_error()
283  : device_error(make_error_code(errc::memory_allocation)) {}
284 
285  memory_allocation_error(const char *Msg, pi_int32 Err)
286  : memory_allocation_error(std::string(Msg), Err) {}
287 
288  memory_allocation_error(const std::string &Msg, pi_int32 Err)
289  : device_error(make_error_code(errc::memory_allocation), Msg, Err) {}
290 };
291 
293  "use sycl::exception with sycl::errc::platform instead.") platform_error
294  : public device_error {
295 public:
296  platform_error() : device_error(make_error_code(errc::platform)) {}
297 
298  platform_error(const char *Msg, pi_int32 Err)
299  : platform_error(std::string(Msg), Err) {}
300 
301  platform_error(const std::string &Msg, pi_int32 Err)
302  : device_error(make_error_code(errc::platform), Msg, Err) {}
303 };
304 
306  "use sycl::exception with sycl::errc::profiling instead.") profiling_error
307  : public device_error {
308 public:
309  profiling_error() : device_error(make_error_code(errc::profiling)) {}
310 
311  profiling_error(const char *Msg, pi_int32 Err)
312  : profiling_error(std::string(Msg), Err) {}
313 
314  profiling_error(const std::string &Msg, pi_int32 Err)
315  : device_error(make_error_code(errc::profiling), Msg, Err) {}
316 };
317 
319  "use sycl::exception with sycl::errc::feature_not_supported instead.")
320  feature_not_supported : public device_error {
321 public:
323  : device_error(make_error_code(errc::feature_not_supported)) {}
324 
325  feature_not_supported(const char *Msg, pi_int32 Err)
326  : feature_not_supported(std::string(Msg), Err) {}
327 
328  feature_not_supported(const std::string &Msg, pi_int32 Err)
329  : device_error(make_error_code(errc::feature_not_supported), Msg, Err) {}
330 };
331 
332 } // namespace _V1
333 } // namespace sycl
334 
335 namespace std {
336 template <> struct is_error_code_enum<sycl::errc> : true_type {};
337 } // namespace std
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:51
std::string message(int) const override
Definition: exception.hpp:61
const char * name() const noexcept override
Definition: exception.hpp:60
exception(std::error_code ec, const std::string &Msg, const pi_int32 PIErr, std::shared_ptr< context > Context=nullptr)
Definition: exception.hpp:118
exception(const std::string &Msg)
Definition: exception.hpp:124
__SYCL2020_DEPRECATED("The version of an exception constructor which takes " "no arguments is deprecated.") exception()=default
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:74
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:63
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:50
kernel_bundle< bundle_state::executable > build(const kernel_bundle< bundle_state::input > &InputBundle, const std::vector< device > &Devs, const property_list &PropList={})
std::uint8_t instead
Definition: aliases.hpp:93
signed char __SYCL2020_DEPRECATED
Definition: aliases.hpp:94
T detail::marray_element_t< T > y T T T maxval[i] T T T a
const std::error_category & sycl_category() noexcept
Definition: exception.cpp:89
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
Definition: exception.cpp:94
Definition: access.hpp:18
int32_t pi_int32
Definition: pi.h:201
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324