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/defines_elementary.hpp> // for __SYCL2020_DEPRECATED
16 #include <sycl/detail/export.hpp> // for __SYCL_EXPORT
17 #include <sycl/detail/pi.h> // for pi_int32
18 #ifdef __INTEL_PREVIEW_BREAKING_CHANGES
19 #include <sycl/detail/string.hpp>
20 #endif
21 
22 #include <exception> // for exception
23 #include <memory> // for allocator, shared_ptr, make...
24 #include <string> // for string, basic_string, opera...
25 #include <system_error> // for error_code, error_category
26 #include <type_traits> // for true_type
27 
28 namespace sycl {
29 inline namespace _V1 {
30 
31 // Forward declaration
32 class context;
33 
34 enum class errc : unsigned int {
35  success = 0,
36  runtime = 1,
37  kernel = 2,
38  accessor = 3,
39  nd_range = 4,
40  event = 5,
41  kernel_argument = 6,
42  build = 7,
43  invalid = 8,
45  platform = 10,
46  profiling = 11,
49  backend_mismatch = 14,
50 };
51 
52 template <backend B> using errc_for = typename backend_traits<B>::errc;
53 
56 
57 __SYCL_EXPORT const std::error_category &sycl_category() noexcept;
58 
59 namespace detail {
60 __SYCL_EXPORT const char *stringifyErrorCode(pi_int32 error);
61 
62 inline std::string codeToString(pi_int32 code) {
63  return std::string(std::to_string(code) + " (" + stringifyErrorCode(code) +
64  ")");
65 }
66 
67 class __SYCL_EXPORT SYCLCategory : public std::error_category {
68 public:
69  const char *name() const noexcept override { return "sycl"; }
70  std::string message(int) const override { return "SYCL Error"; }
71 };
72 } // namespace detail
73 
74 // Derive from std::exception so uncaught exceptions are printed in c++ default
75 // exception handler.
77 class __SYCL_EXPORT exception : public virtual std::exception {
78 public:
79  __SYCL2020_DEPRECATED("The version of an exception constructor which takes "
80  "no arguments is deprecated.")
81  exception() = default;
82  virtual ~exception();
83 
84  exception(std::error_code, const char *Msg);
85 
86  exception(std::error_code Ec, const std::string &Msg)
87  : exception(Ec, nullptr, Msg.c_str()) {}
88 
89  // new SYCL 2020 constructors
91  exception(int EV, const std::error_category &ECat, const std::string &WhatArg)
92  : exception(EV, ECat, WhatArg.c_str()) {}
93  exception(int, const std::error_category &, const char *);
94  exception(int, const std::error_category &);
95 
96  // context.hpp depends on exception.hpp but we can't define these ctors in
97  // exception.hpp while context is still an incomplete type.
98  // So, definition of ctors that require a context parameter are moved to
99  // context.hpp.
100  exception(context, std::error_code, const std::string &);
101  exception(context, std::error_code, const char *);
103  exception(context, int, const std::error_category &, const std::string &);
104  exception(context, int, const std::error_category &, const char *);
105  exception(context, int, const std::error_category &);
106 
107  const std::error_code &code() const noexcept;
108  const std::error_category &category() const noexcept;
109 
110  const char *what() const noexcept final;
111 
112  bool has_context() const noexcept;
113 
114  context get_context() const;
115 
117  cl_int get_cl_code() const;
118 
119 private:
120  // Exceptions must be noexcept copy constructible, so cannot use std::string
121  // directly.
122 #ifdef __INTEL_PREVIEW_BREAKING_CHANGES
123  std::shared_ptr<detail::string> MMsg;
124 #else
125  std::shared_ptr<std::string> MMsg;
126 #endif
127  pi_int32 MPIErr = 0;
128  std::shared_ptr<context> MContext;
129  std::error_code MErrC = make_error_code(sycl::errc::invalid);
130 
131 protected:
132  // base constructors used by SYCL 1.2.1 exception subclasses
133  exception(std::error_code Ec, const char *Msg, const pi_int32 PIErr,
134  std::shared_ptr<context> Context = nullptr)
135  : exception(Ec, std::string(Msg), PIErr, Context) {}
136 
137  exception(std::error_code Ec, const std::string &Msg, const pi_int32 PIErr,
138  std::shared_ptr<context> Context = nullptr)
139  : exception(Ec, Context, Msg + " " + detail::codeToString(PIErr)) {
140  MPIErr = PIErr;
141  }
142 
143  exception(const std::string &Msg)
144 #ifdef __INTEL_PREVIEW_BREAKING_CHANGES
145  : MMsg(std::make_shared<detail::string>(Msg)), MContext(nullptr){}
146 #else
147  : MMsg(std::make_shared<std::string>(Msg)), MContext(nullptr) {
148  }
149 #endif
150 
151  // base constructor for all SYCL 2020 constructors
152  // exception(context *ctxPtr, std::error_code Ec, const std::string
153  // &what_arg);
154  exception(std::error_code Ec, std::shared_ptr<context> SharedPtrCtx,
155  const std::string &what_arg)
156  : exception(Ec, SharedPtrCtx, what_arg.c_str()) {
157  }
158  exception(std::error_code Ec, std::shared_ptr<context> SharedPtrCtx,
159  const char *WhatArg);
160 };
161 
163  "use sycl::exception with sycl::errc::runtime instead.") runtime_error
164  : public exception {
165 public:
166  runtime_error() : exception(make_error_code(errc::runtime)) {}
167 
168  runtime_error(const char *Msg, pi_int32 Err)
169  : runtime_error(std::string(Msg), Err) {}
170 
171  runtime_error(const std::string &Msg, pi_int32 Err)
172  : exception(make_error_code(errc::runtime), Msg, Err) {}
173 
174  runtime_error(std::error_code Ec, const std::string &Msg,
175  const pi_int32 PIErr)
176  : exception(Ec, Msg, PIErr) {}
177 
178 protected:
179  runtime_error(std::error_code Ec) : exception(Ec) {}
180 };
181 
182 class __SYCL2020_DEPRECATED("use sycl::exception with sycl::errc::kernel or "
183  "errc::kernel_argument instead.") kernel_error
184  : public runtime_error {
185 public:
186  kernel_error() : runtime_error(make_error_code(errc::kernel)) {}
187 
188  kernel_error(const char *Msg, pi_int32 Err)
189  : kernel_error(std::string(Msg), Err) {}
190 
191  kernel_error(const std::string &Msg, pi_int32 Err)
192  : runtime_error(make_error_code(errc::kernel), Msg, Err) {}
193 };
194 
196  "use sycl::exception with sycl::errc::accessor instead.") accessor_error
197  : public runtime_error {
198 public:
199  accessor_error() : runtime_error(make_error_code(errc::accessor)) {}
200 
201  accessor_error(const char *Msg, pi_int32 Err)
202  : accessor_error(std::string(Msg), Err) {}
203 
204  accessor_error(const std::string &Msg, pi_int32 Err)
205  : runtime_error(make_error_code(errc::accessor), Msg, Err) {}
206 };
207 
209  "use sycl::exception with sycl::errc::nd_range instead.") nd_range_error
210  : public runtime_error {
211 public:
212  nd_range_error() : runtime_error(make_error_code(errc::nd_range)) {}
213 
214  nd_range_error(const char *Msg, pi_int32 Err)
215  : nd_range_error(std::string(Msg), Err) {}
216 
217  nd_range_error(const std::string &Msg, pi_int32 Err)
218  : runtime_error(make_error_code(errc::nd_range), Msg, Err) {}
219 };
220 
222  "use sycl::exception with sycl::errc::event instead.") event_error
223  : public runtime_error {
224 public:
225  event_error() : runtime_error(make_error_code(errc::event)) {}
226 
227  event_error(const char *Msg, pi_int32 Err)
228  : event_error(std::string(Msg), Err) {}
229 
230  event_error(const std::string &Msg, pi_int32 Err)
231  : runtime_error(make_error_code(errc::event), Msg, Err) {}
232 };
233 
235  "use sycl::exception with a sycl::errc enum value instead.")
236  invalid_parameter_error : public runtime_error {
237 public:
238  invalid_parameter_error()
239  : runtime_error(make_error_code(errc::kernel_argument)) {}
240 
241  invalid_parameter_error(const char *Msg, pi_int32 Err)
242  : invalid_parameter_error(std::string(Msg), Err) {}
243 
244  invalid_parameter_error(const std::string &Msg, pi_int32 Err)
245  : runtime_error(make_error_code(errc::kernel_argument), Msg, Err) {}
246 };
247 
249  "use sycl::exception with a sycl::errc enum value instead.") device_error
250  : public exception {
251 public:
252  device_error() : exception(make_error_code(errc::invalid)) {}
253 
254  device_error(const char *Msg, pi_int32 Err)
255  : device_error(std::string(Msg), Err) {}
256 
257  device_error(const std::string &Msg, pi_int32 Err)
258  : exception(make_error_code(errc::invalid), Msg, Err) {}
259 
260 protected:
261  device_error(std::error_code Ec) : exception(Ec) {}
262 
263  device_error(std::error_code Ec, const std::string &Msg, const pi_int32 PIErr)
264  : exception(Ec, Msg, PIErr) {}
265 };
266 
268  "use sycl::exception with a sycl::errc enum value instead.")
269  compile_program_error : public device_error {
270 public:
271  compile_program_error() : device_error(make_error_code(errc::build)) {}
272 
273  compile_program_error(const char *Msg, pi_int32 Err)
274  : compile_program_error(std::string(Msg), Err) {}
275 
276  compile_program_error(const std::string &Msg, pi_int32 Err)
277  : device_error(make_error_code(errc::build), Msg, Err) {}
278 };
279 
281  "use sycl::exception with a sycl::errc enum value instead.")
282  link_program_error : public device_error {
283 public:
284  link_program_error() : device_error(make_error_code(errc::build)) {}
285 
286  link_program_error(const char *Msg, pi_int32 Err)
287  : link_program_error(std::string(Msg), Err) {}
288 
289  link_program_error(const std::string &Msg, pi_int32 Err)
290  : device_error(make_error_code(errc::build), Msg, Err) {}
291 };
292 
294  "use sycl::exception with a sycl::errc enum value instead.")
295  invalid_object_error : public device_error {
296 public:
297  invalid_object_error() : device_error(make_error_code(errc::invalid)) {}
298 
299  invalid_object_error(const char *Msg, pi_int32 Err)
300  : invalid_object_error(std::string(Msg), Err) {}
301 
302  invalid_object_error(const std::string &Msg, pi_int32 Err)
303  : device_error(make_error_code(errc::invalid), Msg, Err) {}
304 };
305 
307  "use sycl::exception with sycl::errc::memory_allocation instead.")
308  memory_allocation_error : public device_error {
309 public:
310  memory_allocation_error()
311  : device_error(make_error_code(errc::memory_allocation)) {}
312 
313  memory_allocation_error(const char *Msg, pi_int32 Err)
314  : memory_allocation_error(std::string(Msg), Err) {}
315 
316  memory_allocation_error(const std::string &Msg, pi_int32 Err)
317  : device_error(make_error_code(errc::memory_allocation), Msg, Err) {}
318 };
319 
321  "use sycl::exception with sycl::errc::platform instead.") platform_error
322  : public device_error {
323 public:
324  platform_error() : device_error(make_error_code(errc::platform)) {}
325 
326  platform_error(const char *Msg, pi_int32 Err)
327  : platform_error(std::string(Msg), Err) {}
328 
329  platform_error(const std::string &Msg, pi_int32 Err)
330  : device_error(make_error_code(errc::platform), Msg, Err) {}
331 };
332 
334  "use sycl::exception with sycl::errc::profiling instead.") profiling_error
335  : public device_error {
336 public:
337  profiling_error() : device_error(make_error_code(errc::profiling)) {}
338 
339  profiling_error(const char *Msg, pi_int32 Err)
340  : profiling_error(std::string(Msg), Err) {}
341 
342  profiling_error(const std::string &Msg, pi_int32 Err)
343  : device_error(make_error_code(errc::profiling), Msg, Err) {}
344 };
345 
347  "use sycl::exception with sycl::errc::feature_not_supported instead.")
348  feature_not_supported : public device_error {
349 public:
351  : device_error(make_error_code(errc::feature_not_supported)) {}
352 
353  feature_not_supported(const char *Msg, pi_int32 Err)
354  : feature_not_supported(std::string(Msg), Err) {}
355 
356  feature_not_supported(const std::string &Msg, pi_int32 Err)
357  : device_error(make_error_code(errc::feature_not_supported), Msg, Err) {}
358 };
359 
360 } // namespace _V1
361 } // namespace sycl
362 
363 namespace std {
364 template <> struct is_error_code_enum<sycl::errc> : true_type {};
365 } // 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:70
const char * name() const noexcept override
Definition: exception.hpp:69
exception(std::error_code Ec, std::shared_ptr< context > SharedPtrCtx, const std::string &what_arg)
Definition: exception.hpp:154
exception(std::error_code Ec, const std::string &Msg, const pi_int32 PIErr, std::shared_ptr< context > Context=nullptr)
Definition: exception.hpp:137
exception(const std::string &Msg)
Definition: exception.hpp:143
exception(std::error_code Ec, const char *Msg, const pi_int32 PIErr, std::shared_ptr< context > Context=nullptr)
Definition: exception.hpp:133
__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:91
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:76
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: exception.hpp:62
const char * stringifyErrorCode(pi_int32 error)
Definition: exception.cpp:98
std::int32_t cl_int
Definition: aliases.hpp:134
typename backend_traits< B >::errc errc_for
Definition: exception.hpp:52
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:88
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:93
Definition: access.hpp:18
error_code
Definition: defs.hpp:59
int32_t pi_int32
Definition: pi.h:238
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324