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