DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
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
10 #include <CL/sycl/context.hpp>
11 #include <CL/sycl/exception.hpp>
12 
13 #include <cstring>
14 
16 namespace sycl {
17 
18 namespace { // anonymous
19 constexpr char ReservedForErrorcode[] =
20  "01234567812345678"; // 17 (string terminator plus error code)
21 std::error_code SYCL121ProxyErrorcode = make_error_code(sycl::errc::invalid);
22 } // namespace
23 
24 exception::exception(std::error_code EC, const char *Msg)
25  : exception(EC, nullptr, Msg) {}
26 
27 exception::exception(std::error_code EC, const std::string &Msg)
28  : exception(EC, nullptr, Msg) {}
29 
30 // new SYCL 2020 constructors
31 exception::exception(std::error_code EC) : exception(EC, nullptr, "") {}
32 
33 exception::exception(int EV, const std::error_category &ECat,
34  const std::string &WhatArg)
35  : exception({EV, ECat}, nullptr, WhatArg) {}
36 
37 exception::exception(int EV, const std::error_category &ECat,
38  const char *WhatArg)
39  : exception({EV, ECat}, nullptr, std::string(WhatArg)) {}
40 
41 exception::exception(int EV, const std::error_category &ECat)
42  : exception({EV, ECat}, nullptr, "") {}
43 
44 exception::exception(context Ctx, std::error_code EC,
45  const std::string &WhatArg)
46  : exception(EC, std::make_shared<context>(Ctx), WhatArg) {}
47 
48 exception::exception(context Ctx, std::error_code EC, const char *WhatArg)
49  : exception(Ctx, EC, std::string(WhatArg)) {}
50 
51 exception::exception(context Ctx, std::error_code EC)
52  : exception(Ctx, EC, "") {}
53 
54 exception::exception(context Ctx, int EV, const std::error_category &ECat,
55  const char *WhatArg)
56  : exception(Ctx, {EV, ECat}, std::string(WhatArg)) {}
57 
58 exception::exception(context Ctx, int EV, const std::error_category &ECat,
59  const std::string &WhatArg)
60  : exception(Ctx, {EV, ECat}, WhatArg) {}
61 
62 exception::exception(context Ctx, int EV, const std::error_category &ECat)
63  : exception(Ctx, EV, ECat, "") {}
64 
65 // protected base constructor for all SYCL 2020 constructors
66 exception::exception(std::error_code EC, std::shared_ptr<context> SharedPtrCtx,
67  const std::string &WhatArg)
68  : MMsg(WhatArg + ReservedForErrorcode), MCLErr(PI_INVALID_VALUE),
69  MContext(SharedPtrCtx) {
70  // For compatibility with previous implementation, we are "hiding" the
71  // std::error_code in the MMsg string, behind the null string terminator
72  const int StringTermPoint = MMsg.length() - strlen(ReservedForErrorcode);
73  char *ReservedPtr = &MMsg[StringTermPoint];
74  ReservedPtr[0] = '\0';
75  ReservedPtr++;
76  // insert error code
77  std::error_code *ECPtr = reinterpret_cast<std::error_code *>(ReservedPtr);
78  memcpy(ECPtr, &EC, sizeof(std::error_code));
79 }
80 
81 const std::error_code &exception::code() const noexcept {
82  const char *WhatStr = MMsg.c_str();
83  // advance to inner string-terminator
84  int StringTermPoint = MMsg.length() - strlen(ReservedForErrorcode);
85  if (StringTermPoint >= 0) {
86  const char *ReservedPtr = &WhatStr[StringTermPoint];
87  // check for string terminator, which denotes a SYCL 2020 exception
88  if (ReservedPtr[0] == '\0') {
89  ReservedPtr++;
90  const std::error_code *ECPtr =
91  reinterpret_cast<const std::error_code *>(ReservedPtr);
92  return *ECPtr;
93  }
94  }
95  // else the exception originates from some SYCL 1.2.1 source
96  return SYCL121ProxyErrorcode;
97 }
98 
99 const std::error_category &exception::category() const noexcept {
100  return code().category();
101 }
102 
103 const char *exception::what() const noexcept { return MMsg.c_str(); }
104 
105 bool exception::has_context() const { return (MContext != nullptr); }
106 
108  if (!has_context())
109  throw invalid_object_error();
110 
111  return *MContext;
112 }
113 
114 cl_int exception::get_cl_code() const { return MCLErr; }
115 
116 const std::error_category &sycl_category() noexcept {
117  static const detail::SYCLCategory SYCLCategoryObj;
118  return SYCLCategoryObj;
119 }
120 
121 std::error_code make_error_code(sycl::errc Err) noexcept {
122  return {static_cast<int>(Err), sycl_category()};
123 }
124 
125 } // namespace sycl
126 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::exception::get_cl_code
cl_int get_cl_code() const
Definition: exception.cpp:114
cl::sycl::sycl_category
const std::error_category & sycl_category() noexcept
Definition: exception.cpp:116
cl::sycl::errc
errc
Definition: exception.hpp:27
context.hpp
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::exception::exception
exception(std::error_code, const char *Msg)
Definition: exception.cpp:24
cl::sycl::exception::get_context
context get_context() const
Definition: exception.cpp:107
cl::sycl::detail::memcpy
void memcpy(void *Dst, const void *Src, std::size_t Size)
cl::sycl::exception::category
const std::error_category & category() const noexcept
Definition: exception.cpp:99
cl::sycl::exception::code
const std::error_code & code() const noexcept
Definition: exception.cpp:81
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::exception::has_context
bool has_context() const
Definition: exception.cpp:105
PI_INVALID_VALUE
@ PI_INVALID_VALUE
Definition: pi.h:87
cl::sycl::cl_int
std::int32_t cl_int
Definition: aliases.hpp:82
exception.hpp
std
Definition: accessor.hpp:2397
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:35
cl::sycl::detail::SYCLCategory
Definition: exception.hpp:53
cl::sycl::exception
Definition: exception.hpp:63
cl::sycl::exception::what
const char * what() const noexcept final
Definition: exception.cpp:103
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12