Skip to content

[SYCL][COMPAT] defs.hpp update with Windows macros. SYCLCOMPAT_CHECK_ERROR added. #13027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions sycl/doc/syclcompat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1177,16 +1177,47 @@ kernel names during machine translation.
`get_sycl_language_version` returns an integer representing the version of the
SYCL spec supported by the current SYCL compiler.

The `SYCLCOMPAT_CHECK_ERROR` macro encapsulates an error-handling mechanism for
expressions that might throw `sycl::exception` and `std::runtime_error`.
If no exceptions are thrown, it returns `syclcompat::error_code::SUCCESS`.
If a `sycl::exception` is caught, it returns `syclcompat::error_code::BACKEND_ERROR`.
If a `std::runtime_error` exception is caught,
`syclcompat::error_code::DEFAULT_ERROR` is returned instead. For both cases, it
prints the error message to the standard error stream.

``` c++
namespace syclcompat {

#define __sycl_compat_align__(n) __attribute__((aligned(n)))
#define __sycl_compat_inline__ __inline__ __attribute__((always_inline))
template <class... Args> class syclcompat_kernel_name;
template <int Arg> class syclcompat_kernel_scalar;

#if defined(_MSC_VER)
#define __syclcompat_align__(n) __declspec(align(n))
#define __syclcompat_inline__ __forceinline
#else
#define __syclcompat_align__(n) __attribute__((aligned(n)))
#define __syclcompat_inline__ __inline__ __attribute__((always_inline))
#endif

#if defined(_MSC_VER)
#define __syclcompat_noinline__ __declspec(noinline)
#else
#define __syclcompat_noinline__ __attribute__((noinline))
#endif

#define SYCLCOMPAT_COMPATIBILITY_TEMP (600)

#define __sycl_compat_noinline__ __attribute__((noinline))
#ifdef _WIN32
#define SYCLCOMPAT_EXPORT __declspec(dllexport)
#else
#define SYCLCOMPAT_EXPORT
#endif

namespace syclcompat {
enum error_code { SUCCESS = 0, BACKEND_ERROR = 1, DEFAULT_ERROR = 999 };
}

template <class... Args> class sycl_compat_kernel_name;
template <int Arg> class sycl_compat_kernel_scalar;
#define SYCLCOMPAT_CHECK_ERROR(expr)

int get_sycl_language_version();

Expand Down
43 changes: 37 additions & 6 deletions sycl/include/syclcompat/defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,43 @@

#pragma once

template <class... Args> class sycl_compat_kernel_name;
template <int Arg> class sycl_compat_kernel_scalar;
#include <iostream>

#define __sycl_compat_align__(n) alignas(n)
#define __sycl_compat_inline__ __inline__ __attribute__((always_inline))
template <class... Args> class syclcompat_kernel_name;
template <int Arg> class syclcompat_kernel_scalar;

#define __sycl_compat_noinline__ __attribute__((noinline))
#if defined(_MSC_VER)
#define __syclcompat_align__(n) __declspec(align(n))
#define __syclcompat_inline__ __forceinline
#define __syclcompat_noinline__ __declspec(noinline)
#else
#define __syclcompat_align__(n) __attribute__((aligned(n)))
#define __syclcompat_inline__ __inline__ __attribute__((always_inline))
#define __syclcompat_noinline__ __attribute__((noinline))
#endif

#define SYCL_COMPAT_COMPATIBILITY_TEMP (600)
#define SYCLCOMPAT_COMPATIBILITY_TEMP (600)

#ifdef _WIN32
#define SYCLCOMPAT_EXPORT __declspec(dllexport)
#else
#define SYCLCOMPAT_EXPORT
#endif

namespace syclcompat {
enum error_code { SUCCESS = 0, BACKEND_ERROR = 1, DEFAULT_ERROR = 999 };
}

#define SYCLCOMPAT_CHECK_ERROR(expr) \
[&]() { \
try { \
expr; \
return syclcompat::error_code::SUCCESS; \
} catch (sycl::exception const &e) { \
std::cerr << e.what() << std::endl; \
return syclcompat::error_code::BACKEND_ERROR; \
} catch (std::runtime_error const &e) { \
std::cerr << e.what() << std::endl; \
return syclcompat::error_code::DEFAULT_ERROR; \
} \
}()
39 changes: 35 additions & 4 deletions sycl/test-e2e/syclcompat/defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,53 @@
* Defs.cpp
*
* Description:
* __sycl_compat_align__ tests
* Syclcompat macros tests
**************************************************************************/

// RUN: %clangxx -fsycl %s -o %t.out
// RUN: %{run} %t.out

#include <cassert>
#include <iostream>

#include <sycl/sycl.hpp>

#include <syclcompat/defs.hpp>

int main() {
struct __sycl_compat_align__(16) {
void test_align() {
std::cout << __PRETTY_FUNCTION__ << std::endl;

constexpr std::size_t expected_size = 16;
struct __syclcompat_align__(expected_size) {
int a;
char c;
}
s;
assert(sizeof(s) == 16);
assert(sizeof(s) == expected_size);
}

void test_check_error() {
std::cout << __PRETTY_FUNCTION__ << std::endl;

auto sycl_error_throw = []() {
throw sycl::exception(sycl::make_error_code(sycl::errc::invalid),
"Expected invalid exception in test_check_error");
};

auto runtime_error_throw = []() {
throw std::runtime_error("Expected invalid exception in test_check_error");
};

assert(syclcompat::error_code::SUCCESS == SYCLCOMPAT_CHECK_ERROR());
assert(syclcompat::error_code::BACKEND_ERROR ==
SYCLCOMPAT_CHECK_ERROR(sycl_error_throw()));
assert(syclcompat::error_code::DEFAULT_ERROR ==
SYCLCOMPAT_CHECK_ERROR(runtime_error_throw()));
}

int main() {
test_align();
test_check_error();

return 0;
}