Skip to content

Commit

Permalink
[SYCL] Add error_code support for SYCL 1.2.1 exception classes (#4574)
Browse files Browse the repository at this point in the history
For ABI compatibility, we are keeping the existing SYCL 1.2.1
exception classes until later. But to improve their conformance
to the SYCL 2020 specification, we want those classes to set
the correct error_code.

Signed-off-by: Chris Perkins <[email protected]>
  • Loading branch information
cperkinsintel authored Sep 16, 2021
1 parent 136730b commit 15e0ab1
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 48 deletions.
210 changes: 164 additions & 46 deletions sycl/include/CL/sycl/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@ namespace sycl {
// Forward declaration
class context;

enum class errc : unsigned int {
success = 0,
runtime = 1,
kernel = 2,
accessor = 3,
nd_range = 4,
event = 5,
kernel_argument = 6,
build = 7,
invalid = 8,
memory_allocation = 9,
platform = 10,
profiling = 11,
feature_not_supported = 12,
kernel_not_supported = 13,
backend_mismatch = 14,
};

template <backend B> using errc_for = typename backend_traits<B>::errc;

/// Constructs an error code using e and sycl_category()
__SYCL_EXPORT std::error_code make_error_code(sycl::errc E) noexcept;

__SYCL_EXPORT const std::error_category &sycl_category() noexcept;

namespace detail {
class __SYCL_EXPORT SYCLCategory : public std::error_category {
public:
const char *name() const noexcept override { return "sycl"; }
std::string message(int) const override { return "SYCL Error"; }
};
} // namespace detail

// Derive from std::exception so uncaught exceptions are printed in c++ default
// exception handler.
/// \ingroup sycl_api
Expand Down Expand Up @@ -68,15 +101,26 @@ class __SYCL_EXPORT exception : public std::exception {
std::shared_ptr<context> MContext;

protected:
// these two constructors are no longer used. Kept for ABI compatability.
exception(const char *Msg, const cl_int CLErr,
std::shared_ptr<context> Context = nullptr)
: exception(std::string(Msg), CLErr, Context) {}

exception(const std::string &Msg, const cl_int CLErr,
std::shared_ptr<context> Context = nullptr)
: MMsg(Msg + " " + detail::codeToString(CLErr)), MCLErr(CLErr),
MContext(Context) {}

// base constructors used by SYCL 1.2.1 exception subclasses
exception(std::error_code ec, const char *Msg, const cl_int CLErr,
std::shared_ptr<context> Context = nullptr)
: exception(ec, std::string(Msg), CLErr, Context) {}

exception(std::error_code ec, const std::string &Msg, const cl_int CLErr,
std::shared_ptr<context> Context = nullptr)
: exception(ec, Context, Msg + " " + detail::codeToString(CLErr)) {
MCLErr = CLErr;
}

exception(const std::string &Msg) : MMsg(Msg), MContext(nullptr) {}

// base constructor for all SYCL 2020 constructors
Expand All @@ -95,33 +139,79 @@ class __SYCL2020_DEPRECATED(
runtime_error(const char *Msg, cl_int Err)
: runtime_error(std::string(Msg), Err) {}

runtime_error(const std::string &Msg, cl_int Err) : exception(Msg, Err) {}
runtime_error(const std::string &Msg, cl_int Err)
: exception(make_error_code(errc::runtime), Msg, Err) {}

protected:
runtime_error(std::error_code ec, const std::string &Msg, const cl_int CLErr)
: exception(ec, Msg, CLErr) {}
};

class __SYCL2020_DEPRECATED("use sycl::exception with sycl::errc::kernel or "
"errc::kernel_argument instead.") kernel_error
: public runtime_error {
using runtime_error::runtime_error;
public:
kernel_error() = default;

kernel_error(const char *Msg, cl_int Err)
: kernel_error(std::string(Msg), Err) {}

kernel_error(const std::string &Msg, cl_int Err)
: runtime_error(make_error_code(errc::kernel), Msg, Err) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with sycl::errc::accessor instead.") accessor_error
: public runtime_error {
using runtime_error::runtime_error;
public:
accessor_error() = default;

accessor_error(const char *Msg, cl_int Err)
: accessor_error(std::string(Msg), Err) {}

accessor_error(const std::string &Msg, cl_int Err)
: runtime_error(make_error_code(errc::accessor), Msg, Err) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with sycl::errc::nd_range instead.") nd_range_error
: public runtime_error {
using runtime_error::runtime_error;
public:
nd_range_error() = default;

nd_range_error(const char *Msg, cl_int Err)
: nd_range_error(std::string(Msg), Err) {}

nd_range_error(const std::string &Msg, cl_int Err)
: runtime_error(make_error_code(errc::nd_range), Msg, Err) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with sycl::errc::event instead.") event_error
: public runtime_error {
using runtime_error::runtime_error;
public:
event_error() = default;

event_error(const char *Msg, cl_int Err)
: event_error(std::string(Msg), Err) {}

event_error(const std::string &Msg, cl_int Err)
: runtime_error(make_error_code(errc::event), Msg, Err) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with a sycl::errc enum value instead.")
invalid_parameter_error : public runtime_error {
using runtime_error::runtime_error;
public:
invalid_parameter_error() = default;

invalid_parameter_error(const char *Msg, cl_int Err)
: invalid_parameter_error(std::string(Msg), Err) {}

invalid_parameter_error(const std::string &Msg, cl_int Err)
: runtime_error(make_error_code(errc::kernel_argument), Msg, Err) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with a sycl::errc enum value instead.") device_error
: public exception {
Expand All @@ -131,76 +221,104 @@ class __SYCL2020_DEPRECATED(
device_error(const char *Msg, cl_int Err)
: device_error(std::string(Msg), Err) {}

device_error(const std::string &Msg, cl_int Err) : exception(Msg, Err) {}
device_error(const std::string &Msg, cl_int Err)
: exception(make_error_code(errc::invalid), Msg, Err) {}

protected:
device_error(std::error_code ec, const std::string &Msg, const cl_int CLErr)
: exception(ec, Msg, CLErr) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with a sycl::errc enum value instead.")
compile_program_error : public device_error {
using device_error::device_error;
public:
compile_program_error() = default;

compile_program_error(const char *Msg, cl_int Err)
: compile_program_error(std::string(Msg), Err) {}

compile_program_error(const std::string &Msg, cl_int Err)
: device_error(make_error_code(errc::build), Msg, Err) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with a sycl::errc enum value instead.")
link_program_error : public device_error {
using device_error::device_error;
public:
link_program_error() = default;

link_program_error(const char *Msg, cl_int Err)
: link_program_error(std::string(Msg), Err) {}

link_program_error(const std::string &Msg, cl_int Err)
: device_error(make_error_code(errc::build), Msg, Err) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with a sycl::errc enum value instead.")
invalid_object_error : public device_error {
using device_error::device_error;
public:
invalid_object_error() = default;

invalid_object_error(const char *Msg, cl_int Err)
: invalid_object_error(std::string(Msg), Err) {}

invalid_object_error(const std::string &Msg, cl_int Err)
: device_error(make_error_code(errc::invalid), Msg, Err) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with sycl::errc::memory_allocation instead.")
memory_allocation_error : public device_error {
using device_error::device_error;
public:
memory_allocation_error() = default;

memory_allocation_error(const char *Msg, cl_int Err)
: memory_allocation_error(std::string(Msg), Err) {}

memory_allocation_error(const std::string &Msg, cl_int Err)
: device_error(make_error_code(errc::memory_allocation), Msg, Err) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with sycl::errc::platform instead.") platform_error
: public device_error {
using device_error::device_error;
public:
platform_error() = default;

platform_error(const char *Msg, cl_int Err)
: platform_error(std::string(Msg), Err) {}

platform_error(const std::string &Msg, cl_int Err)
: device_error(make_error_code(errc::platform), Msg, Err) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with sycl::errc::profiling instead.") profiling_error
: public device_error {
using device_error::device_error;
public:
profiling_error() = default;

profiling_error(const char *Msg, cl_int Err)
: profiling_error(std::string(Msg), Err) {}

profiling_error(const std::string &Msg, cl_int Err)
: device_error(make_error_code(errc::profiling), Msg, Err) {}
};

class __SYCL2020_DEPRECATED(
"use sycl::exception with sycl::errc::feature_not_supported instead.")
feature_not_supported : public device_error {
using device_error::device_error;
};

enum class errc : unsigned int {
success = 0,
runtime = 1,
kernel = 2,
accessor = 3,
nd_range = 4,
event = 5,
kernel_argument = 6,
build = 7,
invalid = 8,
memory_allocation = 9,
platform = 10,
profiling = 11,
feature_not_supported = 12,
kernel_not_supported = 13,
backend_mismatch = 14,
};

template <backend B> using errc_for = typename backend_traits<B>::errc;
public:
feature_not_supported() = default;

/// Constructs an error code using e and sycl_category()
__SYCL_EXPORT std::error_code make_error_code(sycl::errc E) noexcept;
feature_not_supported(const char *Msg, cl_int Err)
: feature_not_supported(std::string(Msg), Err) {}

__SYCL_EXPORT const std::error_category &sycl_category() noexcept;

namespace detail {
class __SYCL_EXPORT SYCLCategory : public std::error_category {
public:
const char *name() const noexcept override { return "sycl"; }
std::string message(int) const override { return "SYCL Error"; }
feature_not_supported(const std::string &Msg, cl_int Err)
: device_error(make_error_code(errc::feature_not_supported), Msg, Err) {}
};
} // namespace detail

} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
Expand Down
6 changes: 4 additions & 2 deletions sycl/test/abi/sycl_symbols_windows.dump
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
??$get_info@$0BAIA@@context@sycl@cl@@QEBAIXZ
??$get_info@$0BAIB@@context@sycl@cl@@QEBA?AV?$vector@Vdevice@sycl@cl@@V?$allocator@Vdevice@sycl@cl@@@std@@@std@@XZ
??$get_info@$0BAIE@@context@sycl@cl@@QEBA?AVplatform@12@XZ
?device_has@queue@sycl@cl@@QEBA_NW4aspect@23@@Z
??$get_info@$0BAJA@@queue@sycl@cl@@QEBA?AVcontext@12@XZ
??$get_info@$0BAJB@@queue@sycl@cl@@QEBA?AVdevice@12@XZ
??$get_info@$0BAJC@@queue@sycl@cl@@QEBAIXZ
Expand Down Expand Up @@ -304,6 +303,8 @@
??0exception@sycl@cl@@IEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z
??0exception@sycl@cl@@IEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HV?$shared_ptr@Vcontext@sycl@cl@@@4@@Z
??0exception@sycl@cl@@IEAA@PEBDHV?$shared_ptr@Vcontext@sycl@cl@@@std@@@Z
??0exception@sycl@cl@@IEAA@Verror_code@std@@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@4@HV?$shared_ptr@Vcontext@sycl@cl@@@4@@Z
??0exception@sycl@cl@@IEAA@Verror_code@std@@PEBDHV?$shared_ptr@Vcontext@sycl@cl@@@4@@Z
??0exception@sycl@cl@@IEAA@Verror_code@std@@V?$shared_ptr@Vcontext@sycl@cl@@@4@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@4@@Z
??0exception@sycl@cl@@QEAA@$$QEAV012@@Z
??0exception@sycl@cl@@QEAA@AEBV012@@Z
Expand Down Expand Up @@ -1596,6 +1597,7 @@
?depends_on@handler@sycl@cl@@QEAAXAEBV?$vector@Vevent@sycl@cl@@V?$allocator@Vevent@sycl@cl@@@std@@@std@@@Z
?depends_on@handler@sycl@cl@@QEAAXVevent@23@@Z
?determineHostPtr@SYCLMemObjT@detail@sycl@cl@@IEAAXAEBV?$shared_ptr@Vcontext_impl@detail@sycl@cl@@@std@@_NAEAPEAXAEA_N@Z
?device_has@queue@sycl@cl@@QEBA_NW4aspect@23@@Z
?die@pi@detail@sycl@cl@@YAXPEBD@Z
?distance@__host_std@cl@@YA?AVhalf@half_impl@detail@sycl@2@V34562@0@Z
?distance@__host_std@cl@@YA?AVhalf@half_impl@detail@sycl@2@V?$vec@Vhalf@half_impl@detail@sycl@cl@@$01@62@0@Z
Expand Down Expand Up @@ -1744,12 +1746,12 @@
?expm1@__host_std@cl@@YANN@Z
?ext_oneapi_barrier@handler@sycl@cl@@QEAAXAEBV?$vector@Vevent@sycl@cl@@V?$allocator@Vevent@sycl@cl@@@std@@@std@@@Z
?ext_oneapi_barrier@handler@sycl@cl@@QEAAXXZ
?ext_oneapi_get_default_context@platform@sycl@cl@@QEBA?AVcontext@23@XZ
?ext_oneapi_submit_barrier@queue@sycl@cl@@QEAA?AVevent@23@AEBUcode_location@detail@23@@Z
?ext_oneapi_submit_barrier@queue@sycl@cl@@QEAA?AVevent@23@AEBV?$vector@Vevent@sycl@cl@@V?$allocator@Vevent@sycl@cl@@@std@@@std@@AEBUcode_location@detail@23@@Z
?extractArgsAndReqs@handler@sycl@cl@@AEAAXXZ
?extractArgsAndReqsFromLambda@handler@sycl@cl@@AEAAXPEAD_KPEBUkernel_param_desc_t@detail@23@@Z
?extractArgsAndReqsFromLambda@handler@sycl@cl@@AEAAXPEAD_KPEBUkernel_param_desc_t@detail@23@_N@Z
?ext_oneapi_get_default_context@platform@sycl@cl@@QEBA?AVcontext@23@XZ
?fabs@__host_std@cl@@YA?AV?$vec@M$00@sycl@2@V342@@Z
?fabs@__host_std@cl@@YA?AV?$vec@M$01@sycl@2@V342@@Z
?fabs@__host_std@cl@@YA?AV?$vec@M$02@sycl@2@V342@@Z
Expand Down

0 comments on commit 15e0ab1

Please sign in to comment.