|
| 1 | +//===----------------------------------------------------------------------===// |
| 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 | +/// \file |
| 10 | +/// This file contains the declaration of the SYCL 2020 Exception class |
| 11 | +/// interface (4.13.2.) |
| 12 | +/// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#ifndef _LIBSYCL___IMPL_EXCEPTION_HPP |
| 16 | +#define _LIBSYCL___IMPL_EXCEPTION_HPP |
| 17 | + |
| 18 | +#include <sycl/__impl/detail/config.hpp> |
| 19 | + |
| 20 | +#include <exception> |
| 21 | +#include <memory> |
| 22 | +#include <string> |
| 23 | +#include <system_error> |
| 24 | +#include <type_traits> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +_LIBSYCL_BEGIN_NAMESPACE_SYCL |
| 28 | + |
| 29 | +class context; |
| 30 | + |
| 31 | +enum class errc : 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, |
| 41 | + memory_allocation = 9, |
| 42 | + platform = 10, |
| 43 | + profiling = 11, |
| 44 | + feature_not_supported = 12, |
| 45 | + kernel_not_supported = 13, |
| 46 | + backend_mismatch = 14, |
| 47 | +}; |
| 48 | + |
| 49 | +/// Constructs an error code using E and sycl_category() |
| 50 | +_LIBSYCL_EXPORT std::error_code make_error_code(sycl::errc E) noexcept; |
| 51 | + |
| 52 | +/// Obtains a reference to the static error category object for SYCL errors. |
| 53 | +_LIBSYCL_EXPORT const std::error_category &sycl_category() noexcept; |
| 54 | + |
| 55 | +// Derive from std::exception so uncaught exceptions are printed in c++ default |
| 56 | +// exception handler. |
| 57 | +// Virtual inheritance is mandated by SYCL 2020. |
| 58 | +// 4.13.2. Exception class interface |
| 59 | +class _LIBSYCL_EXPORT exception : public virtual std::exception { |
| 60 | +public: |
| 61 | + exception(std::error_code, const char *); |
| 62 | + exception(std::error_code Ec, const std::string &Msg) |
| 63 | + : exception(Ec, Msg.c_str()) {} |
| 64 | + |
| 65 | + exception(std::error_code EC) : exception(EC, "") {} |
| 66 | + exception(int EV, const std::error_category &ECat, const std::string &WhatArg) |
| 67 | + : exception(EV, ECat, WhatArg.c_str()) {} |
| 68 | + exception(int EV, const std::error_category &ECat, const char *WhatArg) |
| 69 | + : exception({EV, ECat}, WhatArg) {} |
| 70 | + exception(int EV, const std::error_category &ECat) |
| 71 | + : exception({EV, ECat}, "") {} |
| 72 | + |
| 73 | + virtual ~exception(); |
| 74 | + |
| 75 | + const std::error_code &code() const noexcept; |
| 76 | + const std::error_category &category() const noexcept; |
| 77 | + |
| 78 | + const char *what() const noexcept final; |
| 79 | + |
| 80 | + bool has_context() const noexcept; |
| 81 | + |
| 82 | +private: |
| 83 | + // Exceptions must be noexcept copy constructible, so cannot use std::string |
| 84 | + // directly. |
| 85 | + std::shared_ptr<std::string> MMessage; |
| 86 | + std::error_code MErrC = make_error_code(sycl::errc::invalid); |
| 87 | +}; |
| 88 | + |
| 89 | +/// Used as a container for a list of asynchronous exceptions |
| 90 | +/// |
| 91 | +class _LIBSYCL_EXPORT exception_list { |
| 92 | +public: |
| 93 | + using value_type = std::exception_ptr; |
| 94 | + using reference = value_type &; |
| 95 | + using const_reference = const value_type &; |
| 96 | + using size_type = std::size_t; |
| 97 | + using iterator = std::vector<std::exception_ptr>::const_iterator; |
| 98 | + using const_iterator = std::vector<std::exception_ptr>::const_iterator; |
| 99 | + |
| 100 | + size_type size() const; |
| 101 | + // first asynchronous exception |
| 102 | + iterator begin() const; |
| 103 | + // refer to past-the-end last asynchronous exception |
| 104 | + iterator end() const; |
| 105 | + |
| 106 | +private: |
| 107 | + std::vector<std::exception_ptr> MList; |
| 108 | +}; |
| 109 | + |
| 110 | +_LIBSYCL_END_NAMESPACE_SYCL |
| 111 | + |
| 112 | +namespace std { |
| 113 | +template <> struct is_error_code_enum<sycl::errc> : true_type {}; |
| 114 | +} // namespace std |
| 115 | + |
| 116 | +#endif // _LIBSYCL___IMPL_EXCEPTION_HPP |
0 commit comments