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