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 
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() : exception(make_error_code(errc::runtime)) {}
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  runtime_error(std::error_code ec, const std::string &Msg, const cl_int CLErr)
146  : exception(ec, Msg, CLErr) {}
147 
148 protected:
149  runtime_error(std::error_code ec) : exception(ec) {}
150 };
151 
152 class __SYCL2020_DEPRECATED("use sycl::exception with sycl::errc::kernel or "
153  "errc::kernel_argument instead.") kernel_error
154  : public runtime_error {
155 public:
156  kernel_error() : runtime_error(make_error_code(errc::kernel)) {}
157 
158  kernel_error(const char *Msg, cl_int Err)
159  : kernel_error(std::string(Msg), Err) {}
160 
161  kernel_error(const std::string &Msg, cl_int Err)
162  : runtime_error(make_error_code(errc::kernel), Msg, Err) {}
163 };
164 
166  "use sycl::exception with sycl::errc::accessor instead.") accessor_error
167  : public runtime_error {
168 public:
169  accessor_error() : runtime_error(make_error_code(errc::accessor)) {}
170 
171  accessor_error(const char *Msg, cl_int Err)
172  : accessor_error(std::string(Msg), Err) {}
173 
174  accessor_error(const std::string &Msg, cl_int Err)
175  : runtime_error(make_error_code(errc::accessor), Msg, Err) {}
176 };
177 
179  "use sycl::exception with sycl::errc::nd_range instead.") nd_range_error
180  : public runtime_error {
181 public:
182  nd_range_error() : runtime_error(make_error_code(errc::nd_range)) {}
183 
184  nd_range_error(const char *Msg, cl_int Err)
185  : nd_range_error(std::string(Msg), Err) {}
186 
187  nd_range_error(const std::string &Msg, cl_int Err)
188  : runtime_error(make_error_code(errc::nd_range), Msg, Err) {}
189 };
190 
192  "use sycl::exception with sycl::errc::event instead.") event_error
193  : public runtime_error {
194 public:
195  event_error() : runtime_error(make_error_code(errc::event)) {}
196 
197  event_error(const char *Msg, cl_int Err)
198  : event_error(std::string(Msg), Err) {}
199 
200  event_error(const std::string &Msg, cl_int Err)
201  : runtime_error(make_error_code(errc::event), Msg, Err) {}
202 };
203 
205  "use sycl::exception with a sycl::errc enum value instead.")
206  invalid_parameter_error : public runtime_error {
207 public:
208  invalid_parameter_error()
209  : runtime_error(make_error_code(errc::kernel_argument)) {}
210 
211  invalid_parameter_error(const char *Msg, cl_int Err)
212  : invalid_parameter_error(std::string(Msg), Err) {}
213 
214  invalid_parameter_error(const std::string &Msg, cl_int Err)
215  : runtime_error(make_error_code(errc::kernel_argument), Msg, Err) {}
216 };
217 
219  "use sycl::exception with a sycl::errc enum value instead.") device_error
220  : public exception {
221 public:
222  device_error() : exception(make_error_code(errc::invalid)) {}
223 
224  device_error(const char *Msg, cl_int Err)
225  : device_error(std::string(Msg), Err) {}
226 
227  device_error(const std::string &Msg, cl_int Err)
228  : exception(make_error_code(errc::invalid), Msg, Err) {}
229 
230 protected:
231  device_error(std::error_code ec) : exception(ec) {}
232 
233  device_error(std::error_code ec, const std::string &Msg, const cl_int CLErr)
234  : exception(ec, Msg, CLErr) {}
235 };
236 
238  "use sycl::exception with a sycl::errc enum value instead.")
239  compile_program_error : public device_error {
240 public:
241  compile_program_error() : device_error(make_error_code(errc::build)) {}
242 
243  compile_program_error(const char *Msg, cl_int Err)
244  : compile_program_error(std::string(Msg), Err) {}
245 
246  compile_program_error(const std::string &Msg, cl_int Err)
247  : device_error(make_error_code(errc::build), Msg, Err) {}
248 };
249 
251  "use sycl::exception with a sycl::errc enum value instead.")
252  link_program_error : public device_error {
253 public:
254  link_program_error() : device_error(make_error_code(errc::build)) {}
255 
256  link_program_error(const char *Msg, cl_int Err)
257  : link_program_error(std::string(Msg), Err) {}
258 
259  link_program_error(const std::string &Msg, cl_int Err)
260  : device_error(make_error_code(errc::build), Msg, Err) {}
261 };
262 
264  "use sycl::exception with a sycl::errc enum value instead.")
265  invalid_object_error : public device_error {
266 public:
267  invalid_object_error() : device_error(make_error_code(errc::invalid)) {}
268 
269  invalid_object_error(const char *Msg, cl_int Err)
270  : invalid_object_error(std::string(Msg), Err) {}
271 
272  invalid_object_error(const std::string &Msg, cl_int Err)
273  : device_error(make_error_code(errc::invalid), Msg, Err) {}
274 };
275 
277  "use sycl::exception with sycl::errc::memory_allocation instead.")
278  memory_allocation_error : public device_error {
279 public:
280  memory_allocation_error()
281  : device_error(make_error_code(errc::memory_allocation)) {}
282 
283  memory_allocation_error(const char *Msg, cl_int Err)
284  : memory_allocation_error(std::string(Msg), Err) {}
285 
286  memory_allocation_error(const std::string &Msg, cl_int Err)
287  : device_error(make_error_code(errc::memory_allocation), Msg, Err) {}
288 };
289 
291  "use sycl::exception with sycl::errc::platform instead.") platform_error
292  : public device_error {
293 public:
294  platform_error() : device_error(make_error_code(errc::platform)) {}
295 
296  platform_error(const char *Msg, cl_int Err)
297  : platform_error(std::string(Msg), Err) {}
298 
299  platform_error(const std::string &Msg, cl_int Err)
300  : device_error(make_error_code(errc::platform), Msg, Err) {}
301 };
302 
304  "use sycl::exception with sycl::errc::profiling instead.") profiling_error
305  : public device_error {
306 public:
307  profiling_error() : device_error(make_error_code(errc::profiling)) {}
308 
309  profiling_error(const char *Msg, cl_int Err)
310  : profiling_error(std::string(Msg), Err) {}
311 
312  profiling_error(const std::string &Msg, cl_int Err)
313  : device_error(make_error_code(errc::profiling), Msg, Err) {}
314 };
315 
317  "use sycl::exception with sycl::errc::feature_not_supported instead.")
318  feature_not_supported : public device_error {
319 public:
321  : device_error(make_error_code(errc::feature_not_supported)) {}
322 
323  feature_not_supported(const char *Msg, cl_int Err)
324  : feature_not_supported(std::string(Msg), Err) {}
325 
326  feature_not_supported(const std::string &Msg, cl_int Err)
327  : device_error(make_error_code(errc::feature_not_supported), Msg, Err) {}
328 };
329 
330 } // namespace sycl
331 } // __SYCL_INLINE_NAMESPACE(cl)
332 
333 namespace std {
334 template <> struct is_error_code_enum<cl::sycl::errc> : true_type {};
335 } // 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:714
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:236
cl::sycl::detail::backend_errc
backend_errc
Definition: backend.hpp:48
sycl
Definition: invoke_simd.hpp:68
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:223
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:289
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:2616
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:31
cl::sycl::instead
std::uint8_t instead
Definition: aliases.hpp:68
cl::sycl::info::context
context
Definition: info_desc.hpp:42
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