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/detail/cl.h> // for cl_int
14 #include <sycl/detail/defines_elementary.hpp> // for __SYCL2020_DEPRECATED
15 #include <sycl/detail/export.hpp> // for __SYCL_EXPORT
16 #include <sycl/detail/string.hpp>
17 
18 #include <exception> // for exception
19 #include <memory> // for allocator, shared_ptr, make...
20 #include <string> // for string, basic_string, opera...
21 #include <system_error> // for error_code, error_category
22 #include <type_traits> // for true_type
23 
24 namespace sycl {
25 inline namespace _V1 {
26 
27 // Forward declaration
28 class context;
29 class exception;
30 
31 enum class errc : unsigned int {
32  success = 0,
33  runtime = 1,
34  kernel = 2,
35  accessor = 3,
36  nd_range = 4,
37  event = 5,
38  kernel_argument = 6,
39  build = 7,
40  invalid = 8,
42  platform = 10,
43  profiling = 11,
46  backend_mismatch = 14,
47 };
48 
51 
52 __SYCL_EXPORT const std::error_category &sycl_category() noexcept;
53 
54 namespace detail {
55 __SYCL_EXPORT const char *stringifyErrorCode(int32_t error);
56 
57 inline std::string codeToString(int32_t code) {
58  return std::to_string(code) + " (" + std::string(stringifyErrorCode(code)) + ")";
59 }
60 
61 class __SYCL_EXPORT SYCLCategory : public std::error_category {
62 public:
63  const char *name() const noexcept override { return "sycl"; }
64  std::string message(int) const override { return "SYCL Error"; }
65 };
66 
67 // Forward declare to declare as a friend in sycl::excepton.
68 int32_t get_ur_error(const exception &e);
69 exception set_ur_error(exception &&e, int32_t ur_err);
70 } // namespace detail
71 
72 // Derive from std::exception so uncaught exceptions are printed in c++ default
73 // exception handler.
75 class __SYCL_EXPORT exception : public virtual std::exception {
76 public:
77  __SYCL2020_DEPRECATED("The version of an exception constructor which takes "
78  "no arguments is deprecated.")
79  exception() = default;
80  virtual ~exception();
81 
82  exception(std::error_code, const char *Msg);
83 
84  exception(std::error_code Ec, const std::string &Msg)
85  : exception(Ec, nullptr, Msg.c_str()) {}
86 
87  // new SYCL 2020 constructors
89  exception(int EV, const std::error_category &ECat, const std::string &WhatArg)
90  : exception(EV, ECat, WhatArg.c_str()) {}
91  exception(int, const std::error_category &, const char *);
92  exception(int, const std::error_category &);
93 
94  // context.hpp depends on exception.hpp but we can't define these ctors in
95  // exception.hpp while context is still an incomplete type.
96  // So, definition of ctors that require a context parameter are moved to
97  // context.hpp.
98  exception(context, std::error_code, const std::string &);
99  exception(context, std::error_code, const char *);
101  exception(context, int, const std::error_category &, const std::string &);
102  exception(context, int, const std::error_category &, const char *);
103  exception(context, int, const std::error_category &);
104 
105  const std::error_code &code() const noexcept;
106  const std::error_category &category() const noexcept;
107 
108  const char *what() const noexcept final;
109 
110  bool has_context() const noexcept;
111 
112  context get_context() const;
113 
114 private:
115  // Exceptions must be noexcept copy constructible, so cannot use std::string
116  // directly.
117  std::shared_ptr<detail::string> MMsg;
118  int32_t MErr = 0;
119  std::shared_ptr<context> MContext;
120  std::error_code MErrC = make_error_code(sycl::errc::invalid);
121 
122 protected:
123  // base constructors used by SYCL 1.2.1 exception subclasses
124  exception(std::error_code Ec, const char *Msg, const int32_t PIErr)
125  : exception(Ec, std::string(Msg), PIErr) {}
126 
127  exception(std::error_code Ec, const std::string &Msg, const int32_t URErr)
128  : exception(Ec, nullptr, Msg + " " + detail::codeToString(URErr)) {
129  MErr = URErr;
130  }
131 
132  // base constructor for all SYCL 2020 constructors
133  // exception(context *, std::error_code, const std::string);
134  exception(std::error_code Ec, std::shared_ptr<context> SharedPtrCtx,
135  const std::string &what_arg)
136  : exception(Ec, SharedPtrCtx, what_arg.c_str()) {}
137  exception(std::error_code Ec, std::shared_ptr<context> SharedPtrCtx,
138  const char *WhatArg);
139 
140  friend int32_t detail::get_ur_error(const exception &);
141  // To be used like this:
142  // throw/return detail::set_ur_error(exception(...), some_ur_error);
143  // *only* when such a error is coming from the UR level. Otherwise it
144  // *should* be left unset/default-initialized and exception should be thrown
145  // as-is using public ctors.
146  friend exception detail::set_ur_error(exception &&e, int32_t ur_err);
147 };
148 
149 namespace detail {
150 // Even though at the moment those functions are only used in library and not
151 // in headers, they were put here in case we will need them to implement some
152 // of OpenCL (and other backends) interop APIs to query native backend error
153 // from an exception.
154 // And we don't want them to be part of our library ABI, because of future
155 // underlying changes (PI -> UR -> Offload).
156 inline int32_t get_ur_error(const exception &e) { return e.MErr; }
157 inline exception set_ur_error(exception &&e, int32_t ur_err) {
158  e.MErr = ur_err;
159  return std::move(e);
160 }
161 } // namespace detail
162 
163 } // namespace _V1
164 } // namespace sycl
165 
166 namespace std {
167 template <> struct is_error_code_enum<sycl::errc> : true_type {};
168 } // 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:64
const char * name() const noexcept override
Definition: exception.hpp:63
exception(std::error_code Ec, const std::string &Msg, const int32_t URErr)
Definition: exception.hpp:127
exception(std::error_code Ec, std::shared_ptr< context > SharedPtrCtx, const std::string &what_arg)
Definition: exception.hpp:134
__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:89
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:22
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:99
std::string codeToString(int32_t code)
Definition: exception.hpp:57
const char * stringifyErrorCode(int32_t error)
Definition: exception.cpp:70
int32_t get_ur_error(const exception &e)
Definition: exception.hpp:156
exception set_ur_error(exception &&e, int32_t ur_err)
Definition: exception.hpp:157
const std::error_category & sycl_category() noexcept
Definition: exception.cpp:60
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
Definition: exception.cpp:65
Definition: access.hpp:18
error_code
Definition: defs.hpp:70
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324