DPC++ Runtime
Runtime libraries for oneAPI DPC++
exception.cpp
Go to the documentation of this file.
1 //==---------------- exception.cpp - 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 // 4.9.2 Exception Class Interface
11 #include <sycl/context.hpp>
12 #include <sycl/exception.hpp>
13 
14 #include <cstring>
15 
16 namespace sycl {
17 inline namespace _V1 {
18 
20  : exception(EC, nullptr, Msg) {}
21 
22 // new SYCL 2020 constructors
24 
25 exception::exception(int EV, const std::error_category &ECat,
26  const char *WhatArg)
27  : exception({EV, ECat}, nullptr, std::string(WhatArg)) {}
28 
29 exception::exception(int EV, const std::error_category &ECat)
30  : exception({EV, ECat}, nullptr, "") {}
31 
33  const std::string &WhatArg)
34  : exception(EC, std::make_shared<context>(Ctx), WhatArg) {}
35 
36 exception::exception(context Ctx, std::error_code EC, const char *WhatArg)
37  : exception(Ctx, EC, std::string(WhatArg)) {}
38 
40  : exception(Ctx, EC, "") {}
41 
42 exception::exception(context Ctx, int EV, const std::error_category &ECat,
43  const char *WhatArg)
44  : exception(Ctx, {EV, ECat}, std::string(WhatArg)) {}
45 
46 exception::exception(context Ctx, int EV, const std::error_category &ECat,
47  const std::string &WhatArg)
48  : exception(Ctx, {EV, ECat}, WhatArg) {}
49 
50 exception::exception(context Ctx, int EV, const std::error_category &ECat)
51  : exception(Ctx, EV, ECat, "") {}
52 
53 // protected base constructor for all SYCL 2020 constructors
54 exception::exception(std::error_code EC, std::shared_ptr<context> SharedPtrCtx,
55  const char *WhatArg)
56 #ifdef __INTEL_PREVIEW_BREAKING_CHANGES
57  : MMsg(std::make_shared<detail::string>(WhatArg)),
58 #else
59  : MMsg(std::make_shared<std::string>(WhatArg)),
60 #endif
61  MPIErr(PI_ERROR_INVALID_VALUE), MContext(SharedPtrCtx), MErrC(EC) {
63 }
64 
66 
67 const std::error_code &exception::code() const noexcept { return MErrC; }
68 
69 const std::error_category &exception::category() const noexcept {
70  return code().category();
71 }
72 
73 const char *exception::what() const noexcept { return MMsg->c_str(); }
74 
75 bool exception::has_context() const noexcept { return (MContext != nullptr); }
76 
78  if (!has_context())
79  throw sycl::exception(sycl::errc::invalid);
80 
81  return *MContext;
82 }
83 
84 cl_int exception::get_cl_code() const { return MPIErr; }
85 
86 const std::error_category &sycl_category() noexcept {
87  static const detail::SYCLCategory SYCLCategoryObj;
88  return SYCLCategoryObj;
89 }
90 
92  return {static_cast<int>(Err), sycl_category()};
93 }
94 
95 } // namespace _V1
96 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:50
void TraceEventXPTI(const char *Message)
static GlobalHandler & instance()
bool has_context() const noexcept
Definition: exception.cpp:75
context get_context() const
Definition: exception.cpp:77
const char * what() const noexcept final
Definition: exception.cpp:73
const std::error_code & code() const noexcept
Definition: exception.cpp:67
cl_int get_cl_code() const
Definition: exception.cpp:84
virtual ~exception()
Definition: exception.cpp:65
const std::error_category & category() const noexcept
Definition: exception.cpp:69
exception(std::error_code, const char *Msg)
Definition: exception.cpp:19
std::int32_t cl_int
Definition: aliases.hpp:134
const std::error_category & sycl_category() noexcept
Definition: exception.cpp:86
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
Definition: exception.cpp:91
Definition: access.hpp:18
error_code
Definition: defs.hpp:59
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324