DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
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 
16 #include <CL/sycl/detail/pi.h>
17 #include <CL/sycl/stl.hpp>
18 
19 #include <exception>
20 
22 namespace sycl {
23 
24 // Forward declaration
25 class context;
26 
27 enum class errc : unsigned int {
28  success = 0,
29  runtime = 1,
30  kernel = 2,
31  accessor = 3,
32  nd_range = 4,
33  event = 5,
34  kernel_argument = 6,
35  build = 7,
36  invalid = 8,
38  platform = 10,
39  profiling = 11,
42  backend_mismatch = 14,
43 };
44 
45 template <backend B> using errc_for = typename backend_traits<B>::errc;
46 
48 __SYCL_EXPORT std::error_code make_error_code(sycl::errc E) noexcept;
49 
50 __SYCL_EXPORT const std::error_category &sycl_category() noexcept;
51 
52 namespace detail {
53 class __SYCL_EXPORT SYCLCategory : public std::error_category {
54 public:
55  const char *name() const noexcept override { return "sycl"; }
56  std::string message(int) const override { return "SYCL Error"; }
57 };
58 } // namespace detail
59 
60 // Derive from std::exception so uncaught exceptions are printed in c++ default
61 // exception handler.
63 class __SYCL_EXPORT exception : public std::exception {
64 public:
65  __SYCL2020_DEPRECATED("The version of an exception constructor which takes "
66  "no arguments is deprecated.")
67  exception() = default;
68 
69  exception(std::error_code, const char *Msg);
70 
71  exception(std::error_code, const std::string &Msg);
72 
73  // new SYCL 2020 constructors
74  exception(std::error_code);
75  exception(int, const std::error_category &, const std::string &);
76  exception(int, const std::error_category &, const char *);
77  exception(int, const std::error_category &);
78 
79  exception(context, std::error_code, const std::string &);
80  exception(context, std::error_code, const char *);
81  exception(context, std::error_code);
82  exception(context, int, const std::error_category &, const std::string &);
83  exception(context, int, const std::error_category &, const char *);
84  exception(context, int, const std::error_category &);
85 
86  const std::error_code &code() const noexcept;
87  const std::error_category &category() const noexcept;
88 
89  const char *what() const noexcept final;
90 
91  bool has_context() const;
92 
93  context get_context() const;
94 
95  __SYCL2020_DEPRECATED("use sycl::exception.code() instead.")
96  cl_int get_cl_code() const;
97 
98 private:
99  std::string MMsg;
100  cl_int MCLErr;
101  std::shared_ptr<context> MContext;
102 
103 protected:
104  // these two constructors are no longer used. Kept for ABI compatability.
105  exception(const char *Msg, const cl_int CLErr,
106  std::shared_ptr<context> Context = nullptr)
107  : exception(std::string(Msg), CLErr, Context) {}
108  exception(const std::string &Msg, const cl_int CLErr,
109  std::shared_ptr<context> Context = nullptr)
110  : MMsg(Msg + " " + detail::codeToString(CLErr)), MCLErr(CLErr),
111  MContext(Context) {}
112 
113  // base constructors used by SYCL 1.2.1 exception subclasses
114  exception(std::error_code ec, const char *Msg, const cl_int CLErr,
115  std::shared_ptr<context> Context = nullptr)
116  : exception(ec, std::string(Msg), CLErr, Context) {}
117 
118  exception(std::error_code ec, const std::string &Msg, const cl_int CLErr,
119  std::shared_ptr<context> Context = nullptr)
120  : exception(ec, Context, Msg + " " + detail::codeToString(CLErr)) {
121  MCLErr = CLErr;
122  }
123 
124  exception(const std::string &Msg) : MMsg(Msg), MContext(nullptr) {}
125 
126  // base constructor for all SYCL 2020 constructors
127  // exception(context *ctxPtr, std::error_code ec, const std::string
128  // &what_arg);
129  exception(std::error_code ec, std::shared_ptr<context> SharedPtrCtx,
130  const std::string &what_arg);
131 };
132 
134  "use sycl::exception with sycl::errc::runtime instead.") runtime_error
135  : public exception {
136 public:
137  runtime_error() = default;
138 
139  runtime_error(const char *Msg, cl_int Err)
140  : runtime_error(std::string(Msg), Err) {}
141 
142  runtime_error(const std::string &Msg, cl_int Err)
143  : exception(make_error_code(errc::runtime), Msg, Err) {}
144 
145 protected:
146  runtime_error(std::error_code ec, const std::string &Msg, const cl_int CLErr)
147  : exception(ec, Msg, CLErr) {}
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() = default;
155 
156  kernel_error(const char *Msg, cl_int Err)
157  : kernel_error(std::string(Msg), Err) {}
158 
159  kernel_error(const std::string &Msg, cl_int 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() = default;
168 
169  accessor_error(const char *Msg, cl_int Err)
170  : accessor_error(std::string(Msg), Err) {}
171 
172  accessor_error(const std::string &Msg, cl_int 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() = default;
181 
182  nd_range_error(const char *Msg, cl_int Err)
183  : nd_range_error(std::string(Msg), Err) {}
184 
185  nd_range_error(const std::string &Msg, cl_int 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() = default;
194 
195  event_error(const char *Msg, cl_int Err)
196  : event_error(std::string(Msg), Err) {}
197 
198  event_error(const std::string &Msg, cl_int 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() = default;
207 
208  invalid_parameter_error(const char *Msg, cl_int Err)
209  : invalid_parameter_error(std::string(Msg), Err) {}
210 
211  invalid_parameter_error(const std::string &Msg, cl_int Err)
212  : runtime_error(make_error_code(errc::kernel_argument), Msg, Err) {}
213 };
214 
216  "use sycl::exception with a sycl::errc enum value instead.") device_error
217  : public exception {
218 public:
219  device_error() = default;
220 
221  device_error(const char *Msg, cl_int Err)
222  : device_error(std::string(Msg), Err) {}
223 
224  device_error(const std::string &Msg, cl_int Err)
225  : exception(make_error_code(errc::invalid), Msg, Err) {}
226 
227 protected:
228  device_error(std::error_code ec, const std::string &Msg, const cl_int CLErr)
229  : exception(ec, Msg, CLErr) {}
230 };
231 
233  "use sycl::exception with a sycl::errc enum value instead.")
234  compile_program_error : public device_error {
235 public:
236  compile_program_error() = default;
237 
238  compile_program_error(const char *Msg, cl_int Err)
239  : compile_program_error(std::string(Msg), Err) {}
240 
241  compile_program_error(const std::string &Msg, cl_int Err)
242  : device_error(make_error_code(errc::build), Msg, Err) {}
243 };
244 
246  "use sycl::exception with a sycl::errc enum value instead.")
247  link_program_error : public device_error {
248 public:
249  link_program_error() = default;
250 
251  link_program_error(const char *Msg, cl_int Err)
252  : link_program_error(std::string(Msg), Err) {}
253 
254  link_program_error(const std::string &Msg, cl_int Err)
255  : device_error(make_error_code(errc::build), Msg, Err) {}
256 };
257 
259  "use sycl::exception with a sycl::errc enum value instead.")
260  invalid_object_error : public device_error {
261 public:
262  invalid_object_error() = default;
263 
264  invalid_object_error(const char *Msg, cl_int Err)
265  : invalid_object_error(std::string(Msg), Err) {}
266 
267  invalid_object_error(const std::string &Msg, cl_int Err)
268  : device_error(make_error_code(errc::invalid), Msg, Err) {}
269 };
270 
272  "use sycl::exception with sycl::errc::memory_allocation instead.")
273  memory_allocation_error : public device_error {
274 public:
275  memory_allocation_error() = default;
276 
277  memory_allocation_error(const char *Msg, cl_int Err)
278  : memory_allocation_error(std::string(Msg), Err) {}
279 
280  memory_allocation_error(const std::string &Msg, cl_int Err)
281  : device_error(make_error_code(errc::memory_allocation), Msg, Err) {}
282 };
283 
285  "use sycl::exception with sycl::errc::platform instead.") platform_error
286  : public device_error {
287 public:
288  platform_error() = default;
289 
290  platform_error(const char *Msg, cl_int Err)
291  : platform_error(std::string(Msg), Err) {}
292 
293  platform_error(const std::string &Msg, cl_int Err)
294  : device_error(make_error_code(errc::platform), Msg, Err) {}
295 };
296 
298  "use sycl::exception with sycl::errc::profiling instead.") profiling_error
299  : public device_error {
300 public:
301  profiling_error() = default;
302 
303  profiling_error(const char *Msg, cl_int Err)
304  : profiling_error(std::string(Msg), Err) {}
305 
306  profiling_error(const std::string &Msg, cl_int Err)
307  : device_error(make_error_code(errc::profiling), Msg, Err) {}
308 };
309 
311  "use sycl::exception with sycl::errc::feature_not_supported instead.")
312  feature_not_supported : public device_error {
313 public:
314  feature_not_supported() = default;
315 
316  feature_not_supported(const char *Msg, cl_int Err)
317  : feature_not_supported(std::string(Msg), Err) {}
318 
319  feature_not_supported(const std::string &Msg, cl_int Err)
320  : device_error(make_error_code(errc::feature_not_supported), Msg, Err) {}
321 };
322 
323 } // namespace sycl
324 } // __SYCL_INLINE_NAMESPACE(cl)
325 
326 namespace std {
327 template <> struct is_error_code_enum<cl::sycl::errc> : true_type {};
328 } // namespace std
cl::sycl::nd_range
Defines the iteration domain of both the work-groups and the overall dispatch.
Definition: nd_range.hpp:23
cl::sycl::detail::SYCLCategory::name
const char * name() const noexcept override
Definition: exception.hpp:55
pi.h
cl::sycl::sycl_category
const std::error_category & sycl_category() noexcept
Definition: exception.cpp:116
__SYCL2020_DEPRECATED
#define __SYCL2020_DEPRECATED(message)
Definition: defines_elementary.hpp:56
cl::sycl::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:694
cl::sycl::exception::exception
exception(std::error_code ec, const char *Msg, const cl_int CLErr, std::shared_ptr< context > Context=nullptr)
Definition: exception.hpp:114
cl::sycl::errc::backend_mismatch
@ backend_mismatch
stl.hpp
cl::sycl::errc
errc
Definition: exception.hpp:27
cl::sycl::errc::feature_not_supported
@ feature_not_supported
cl::sycl::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:121
cl::sycl::info::kernel
kernel
Definition: info_desc.hpp:229
cl::sycl::detail::backend_errc
backend_errc
Definition: backend.hpp:45
cl::sycl::detail::SYCLCategory::message
std::string message(int) const override
Definition: exception.hpp:56
cl::sycl::exception::exception
exception(const std::string &Msg)
Definition: exception.hpp:124
compile_program_error
export.hpp
cl::sycl::errc_for
typename backend_traits< B >::errc errc_for
Definition: exception.hpp:45
cl::sycl::accessor
Buffer accessor.
Definition: accessor.hpp:225
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::errc::kernel_argument
@ kernel_argument
cl::sycl::image_channel_order::a
@ a
backend_types.hpp
cl::sycl::info::event
event
Definition: info_desc.hpp:282
cl::sycl::cl_int
std::int32_t cl_int
Definition: aliases.hpp:82
cl::sycl::errc::kernel_not_supported
@ kernel_not_supported
cl::sycl::platform
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:34
std
Definition: accessor.hpp:2397
cl::sycl::errc::memory_allocation
@ memory_allocation
cl::sycl::exception::exception
exception(const char *Msg, const cl_int CLErr, std::shared_ptr< context > Context=nullptr)
Definition: exception.hpp:105
cl::sycl::detail::codeToString
static std::string codeToString(cl_int code)
Definition: common.hpp:96
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:35
cl::sycl::errc::invalid
@ invalid
cl::sycl::detail::SYCLCategory
Definition: exception.hpp:53
cl::sycl::errc::success
@ success
common.hpp
cl::sycl::errc::nd_range
@ nd_range
cl::sycl::exception
Definition: exception.hpp:63
cl::sycl::info::platform
platform
Definition: info_desc.hpp:30
cl::sycl::instead
std::uint8_t instead
Definition: aliases.hpp:68
cl::sycl::info::context
context
Definition: info_desc.hpp:41
cl::sycl::errc::profiling
@ profiling
cl::sycl::exception::exception
exception(std::error_code ec, const std::string &Msg, const cl_int CLErr, std::shared_ptr< context > Context=nullptr)
Definition: exception.hpp:118
cl::sycl::kernel
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:67
cl::sycl::exception::exception
exception(const std::string &Msg, const cl_int CLErr, std::shared_ptr< context > Context=nullptr)
Definition: exception.hpp:108
cl::sycl::errc::runtime
@ runtime
cl::sycl::errc::accessor
@ accessor
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12