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 virtual std::exception {
65 public:
66  __SYCL2020_DEPRECATED("The version of an exception constructor which takes "
67  "no arguments is deprecated.")
68  exception() = default;
69  virtual ~exception();
70 
71  exception(std::error_code, const char *Msg);
72 
73  exception(std::error_code, const std::string &Msg);
74 
75  // new SYCL 2020 constructors
76  exception(std::error_code);
77  exception(int, const std::error_category &, const std::string &);
78  exception(int, const std::error_category &, const char *);
79  exception(int, const std::error_category &);
80 
81  exception(context, std::error_code, const std::string &);
82  exception(context, std::error_code, const char *);
83  exception(context, std::error_code);
84  exception(context, int, const std::error_category &, const std::string &);
85  exception(context, int, const std::error_category &, const char *);
86  exception(context, int, const std::error_category &);
87 
88  const std::error_code &code() const noexcept;
89  const std::error_category &category() const noexcept;
90 
91  const char *what() const noexcept final;
92 
93  bool has_context() const noexcept;
94 
95  context get_context() const;
96 
97  __SYCL2020_DEPRECATED("use sycl::exception.code() instead.")
98  cl_int get_cl_code() const;
99 
100 private:
101  // Exceptions must be noexcept copy constructible, so cannot use std::string
102  // directly.
103  std::shared_ptr<std::string> MMsg;
104  pi_int32 MPIErr;
105  std::shared_ptr<context> MContext;
106  std::error_code MErrC = make_error_code(sycl::errc::invalid);
107 
108 protected:
109  // base constructors used by SYCL 1.2.1 exception subclasses
110  exception(std::error_code ec, const char *Msg, const pi_int32 PIErr,
111  std::shared_ptr<context> Context = nullptr)
112  : exception(ec, std::string(Msg), PIErr, Context) {}
113 
114  exception(std::error_code ec, const std::string &Msg, const pi_int32 PIErr,
115  std::shared_ptr<context> Context = nullptr)
116  : exception(ec, Context, Msg + " " + detail::codeToString(PIErr)) {
117  MPIErr = PIErr;
118  }
119 
120  exception(const std::string &Msg)
121  : MMsg(std::make_shared<std::string>(Msg)), MContext(nullptr) {}
122 
123  // base constructor for all SYCL 2020 constructors
124  // exception(context *ctxPtr, std::error_code ec, const std::string
125  // &what_arg);
126  exception(std::error_code ec, std::shared_ptr<context> SharedPtrCtx,
127  const std::string &what_arg);
128 };
129 
131  "use sycl::exception with sycl::errc::runtime instead.") runtime_error
132  : public exception {
133 public:
134  runtime_error() : exception(make_error_code(errc::runtime)) {}
135 
136  runtime_error(const char *Msg, pi_int32 Err)
137  : runtime_error(std::string(Msg), Err) {}
138 
139  runtime_error(const std::string &Msg, pi_int32 Err)
140  : exception(make_error_code(errc::runtime), Msg, Err) {}
141 
142  runtime_error(std::error_code ec, const std::string &Msg,
143  const pi_int32 PIErr)
144  : exception(ec, Msg, PIErr) {}
145 
146 protected:
147  runtime_error(std::error_code ec) : exception(ec) {}
148 };
149 
150 class __SYCL2020_DEPRECATED("use sycl::exception with sycl::errc::kernel or "
151  "errc::kernel_argument instead.") kernel_error
152  : public runtime_error {
153 public:
154  kernel_error() : runtime_error(make_error_code(errc::kernel)) {}
155 
156  kernel_error(const char *Msg, pi_int32 Err)
157  : kernel_error(std::string(Msg), Err) {}
158 
159  kernel_error(const std::string &Msg, pi_int32 Err)
160  : runtime_error(make_error_code(errc::kernel), Msg, Err) {}
161 };
162 
164  "use sycl::exception with sycl::errc::accessor instead.") accessor_error
165  : public runtime_error {
166 public:
167  accessor_error() : runtime_error(make_error_code(errc::accessor)) {}
168 
169  accessor_error(const char *Msg, pi_int32 Err)
170  : accessor_error(std::string(Msg), Err) {}
171 
172  accessor_error(const std::string &Msg, pi_int32 Err)
173  : runtime_error(make_error_code(errc::accessor), Msg, Err) {}
174 };
175 
177  "use sycl::exception with sycl::errc::nd_range instead.") nd_range_error
178  : public runtime_error {
179 public:
180  nd_range_error() : runtime_error(make_error_code(errc::nd_range)) {}
181 
182  nd_range_error(const char *Msg, pi_int32 Err)
183  : nd_range_error(std::string(Msg), Err) {}
184 
185  nd_range_error(const std::string &Msg, pi_int32 Err)
186  : runtime_error(make_error_code(errc::nd_range), Msg, Err) {}
187 };
188 
190  "use sycl::exception with sycl::errc::event instead.") event_error
191  : public runtime_error {
192 public:
193  event_error() : runtime_error(make_error_code(errc::event)) {}
194 
195  event_error(const char *Msg, pi_int32 Err)
196  : event_error(std::string(Msg), Err) {}
197 
198  event_error(const std::string &Msg, pi_int32 Err)
199  : runtime_error(make_error_code(errc::event), Msg, Err) {}
200 };
201 
203  "use sycl::exception with a sycl::errc enum value instead.")
204  invalid_parameter_error : public runtime_error {
205 public:
206  invalid_parameter_error()
207  : runtime_error(make_error_code(errc::kernel_argument)) {}
208 
209  invalid_parameter_error(const char *Msg, pi_int32 Err)
210  : invalid_parameter_error(std::string(Msg), Err) {}
211 
212  invalid_parameter_error(const std::string &Msg, pi_int32 Err)
213  : runtime_error(make_error_code(errc::kernel_argument), Msg, Err) {}
214 };
215 
217  "use sycl::exception with a sycl::errc enum value instead.") device_error
218  : public exception {
219 public:
220  device_error() : exception(make_error_code(errc::invalid)) {}
221 
222  device_error(const char *Msg, pi_int32 Err)
223  : device_error(std::string(Msg), Err) {}
224 
225  device_error(const std::string &Msg, pi_int32 Err)
226  : exception(make_error_code(errc::invalid), Msg, Err) {}
227 
228 protected:
229  device_error(std::error_code ec) : exception(ec) {}
230 
231  device_error(std::error_code ec, const std::string &Msg, const pi_int32 PIErr)
232  : exception(ec, Msg, PIErr) {}
233 };
234 
236  "use sycl::exception with a sycl::errc enum value instead.")
237  compile_program_error : public device_error {
238 public:
239  compile_program_error() : device_error(make_error_code(errc::build)) {}
240 
241  compile_program_error(const char *Msg, pi_int32 Err)
242  : compile_program_error(std::string(Msg), Err) {}
243 
244  compile_program_error(const std::string &Msg, pi_int32 Err)
245  : device_error(make_error_code(errc::build), Msg, Err) {}
246 };
247 
249  "use sycl::exception with a sycl::errc enum value instead.")
250  link_program_error : public device_error {
251 public:
252  link_program_error() : device_error(make_error_code(errc::build)) {}
253 
254  link_program_error(const char *Msg, pi_int32 Err)
255  : link_program_error(std::string(Msg), Err) {}
256 
257  link_program_error(const std::string &Msg, pi_int32 Err)
258  : device_error(make_error_code(errc::build), Msg, Err) {}
259 };
260 
262  "use sycl::exception with a sycl::errc enum value instead.")
263  invalid_object_error : public device_error {
264 public:
265  invalid_object_error() : device_error(make_error_code(errc::invalid)) {}
266 
267  invalid_object_error(const char *Msg, pi_int32 Err)
268  : invalid_object_error(std::string(Msg), Err) {}
269 
270  invalid_object_error(const std::string &Msg, pi_int32 Err)
271  : device_error(make_error_code(errc::invalid), Msg, Err) {}
272 };
273 
275  "use sycl::exception with sycl::errc::memory_allocation instead.")
276  memory_allocation_error : public device_error {
277 public:
278  memory_allocation_error()
279  : device_error(make_error_code(errc::memory_allocation)) {}
280 
281  memory_allocation_error(const char *Msg, pi_int32 Err)
282  : memory_allocation_error(std::string(Msg), Err) {}
283 
284  memory_allocation_error(const std::string &Msg, pi_int32 Err)
285  : device_error(make_error_code(errc::memory_allocation), Msg, Err) {}
286 };
287 
289  "use sycl::exception with sycl::errc::platform instead.") platform_error
290  : public device_error {
291 public:
292  platform_error() : device_error(make_error_code(errc::platform)) {}
293 
294  platform_error(const char *Msg, pi_int32 Err)
295  : platform_error(std::string(Msg), Err) {}
296 
297  platform_error(const std::string &Msg, pi_int32 Err)
298  : device_error(make_error_code(errc::platform), Msg, Err) {}
299 };
300 
302  "use sycl::exception with sycl::errc::profiling instead.") profiling_error
303  : public device_error {
304 public:
305  profiling_error() : device_error(make_error_code(errc::profiling)) {}
306 
307  profiling_error(const char *Msg, pi_int32 Err)
308  : profiling_error(std::string(Msg), Err) {}
309 
310  profiling_error(const std::string &Msg, pi_int32 Err)
311  : device_error(make_error_code(errc::profiling), Msg, Err) {}
312 };
313 
315  "use sycl::exception with sycl::errc::feature_not_supported instead.")
316  feature_not_supported : public device_error {
317 public:
319  : device_error(make_error_code(errc::feature_not_supported)) {}
320 
321  feature_not_supported(const char *Msg, pi_int32 Err)
322  : feature_not_supported(std::string(Msg), Err) {}
323 
324  feature_not_supported(const std::string &Msg, pi_int32 Err)
325  : device_error(make_error_code(errc::feature_not_supported), Msg, Err) {}
326 };
327 
328 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
329 } // namespace sycl
330 
331 namespace std {
332 template <> struct is_error_code_enum<sycl::errc> : true_type {};
333 } // namespace std
sycl::_V1::build
kernel_bundle< bundle_state::executable > build(const kernel_bundle< bundle_state::input > &InputBundle, const std::vector< device > &Devs, const property_list &PropList={})
Definition: kernel_bundle.hpp:723
sycl::_V1::detail::codeToString
static std::string codeToString(pi_int32 code)
Definition: common.hpp:186
sycl::_V1::detail::SYCLCategory
Definition: exception.hpp:54
sycl::_V1::__SYCL2020_DEPRECATED
signed char __SYCL2020_DEPRECATED
Definition: aliases.hpp:96
sycl::_V1::errc::backend_mismatch
@ backend_mismatch
sycl::_V1::opencl::cl_int
std::int32_t cl_int
Definition: aliases.hpp:136
sycl::_V1::detail::SYCLCategory::name
const char * name() const noexcept override
Definition: exception.hpp:56
sycl::_V1::instead
std::uint8_t instead
Definition: aliases.hpp:95
pi.h
sycl::_V1::errc::platform
@ platform
sycl::_V1::sycl_category
const std::error_category & sycl_category() noexcept
Definition: exception.cpp:89
sycl::_V1::detail::backend_errc
backend_errc
Definition: backend.hpp:53
sycl::_V1::make_error_code
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
Definition: exception.cpp:94
stl.hpp
sycl::_V1::errc::feature_not_supported
@ feature_not_supported
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
sycl::_V1::errc::event
@ event
sycl::_V1::errc::kernel_argument
@ kernel_argument
sycl::_V1::exception::exception
exception(std::error_code ec, const char *Msg, const pi_int32 PIErr, std::shared_ptr< context > Context=nullptr)
Definition: exception.hpp:110
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
sycl::_V1::accessor
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.
sycl::_V1::errc::runtime
@ runtime
compile_program_error
sycl::_V1::kernel
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:72
sycl::_V1::exception::exception
exception(const std::string &Msg)
Definition: exception.hpp:120
sycl::_V1::detail::SYCLCategory::message
std::string message(int) const override
Definition: exception.hpp:57
export.hpp
sycl::_V1::errc_for
typename backend_traits< B >::errc errc_for
Definition: exception.hpp:46
sycl::_V1::errc::profiling
@ profiling
cl.h
sycl::_V1::exception
Definition: exception.hpp:64
common.hpp
sycl::_V1::errc::memory_allocation
@ memory_allocation
sycl::_V1::errc::success
@ success
sycl::_V1::nd_range
Defines the iteration domain of both the work-groups and the overall dispatch.
Definition: uniform.hpp:36
sycl::_V1::errc
errc
Definition: exception.hpp:28
sycl::_V1::errc::invalid
@ invalid
sycl::_V1::accessor
Definition: accessor.hpp:225
sycl::_V1::ext::oneapi::experimental::matrix::use::a
@ a
sycl::_V1::errc::kernel_not_supported
@ kernel_not_supported
backend_types.hpp
std
Definition: accessor.hpp:3914
sycl::_V1::errc::nd_range
@ nd_range
sycl::_V1::ext::oneapi::experimental::matrix::use
use
Definition: matrix-unified-utils.hpp:17
sycl::_V1::platform
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:45
sycl::_V1::errc::kernel
@ kernel
__SYCL2020_DEPRECATED
#define __SYCL2020_DEPRECATED(message)
Definition: defines_elementary.hpp:57
pi_int32
int32_t pi_int32
Definition: pi.h:141
sycl::_V1::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:41
sycl::_V1::exception::exception
exception(std::error_code ec, const std::string &Msg, const pi_int32 PIErr, std::shared_ptr< context > Context=nullptr)
Definition: exception.hpp:114