Skip to content

Commit 89eeb02

Browse files
AlcpzAidanBeltonS
andauthored
[SYCL][COMPAT] defs.hpp update with Windows macros. SYCLCOMPAT_CHECK_ERROR added. (#13027)
This PR: - unifies the names of the definitions in `defs.hpp` from `sycl_compat` to `syclcompat` - extends definitions to work with MSVC - Adds a macro to handle error codes from expressions that could throw `sycl::exception`. --------- Co-authored-by: AidanBeltonS <[email protected]>
1 parent af491ee commit 89eeb02

File tree

3 files changed

+108
-15
lines changed

3 files changed

+108
-15
lines changed

sycl/doc/syclcompat/README.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,16 +1232,47 @@ kernel names during machine translation.
12321232
`get_sycl_language_version` returns an integer representing the version of the
12331233
SYCL spec supported by the current SYCL compiler.
12341234

1235+
The `SYCLCOMPAT_CHECK_ERROR` macro encapsulates an error-handling mechanism for
1236+
expressions that might throw `sycl::exception` and `std::runtime_error`.
1237+
If no exceptions are thrown, it returns `syclcompat::error_code::SUCCESS`.
1238+
If a `sycl::exception` is caught, it returns `syclcompat::error_code::BACKEND_ERROR`.
1239+
If a `std::runtime_error` exception is caught,
1240+
`syclcompat::error_code::DEFAULT_ERROR` is returned instead. For both cases, it
1241+
prints the error message to the standard error stream.
1242+
12351243
``` c++
12361244
namespace syclcompat {
12371245

1238-
#define __sycl_compat_align__(n) __attribute__((aligned(n)))
1239-
#define __sycl_compat_inline__ __inline__ __attribute__((always_inline))
1246+
template <class... Args> class syclcompat_kernel_name;
1247+
template <int Arg> class syclcompat_kernel_scalar;
1248+
1249+
#if defined(_MSC_VER)
1250+
#define __syclcompat_align__(n) __declspec(align(n))
1251+
#define __syclcompat_inline__ __forceinline
1252+
#else
1253+
#define __syclcompat_align__(n) __attribute__((aligned(n)))
1254+
#define __syclcompat_inline__ __inline__ __attribute__((always_inline))
1255+
#endif
1256+
1257+
#if defined(_MSC_VER)
1258+
#define __syclcompat_noinline__ __declspec(noinline)
1259+
#else
1260+
#define __syclcompat_noinline__ __attribute__((noinline))
1261+
#endif
1262+
1263+
#define SYCLCOMPAT_COMPATIBILITY_TEMP (600)
12401264

1241-
#define __sycl_compat_noinline__ __attribute__((noinline))
1265+
#ifdef _WIN32
1266+
#define SYCLCOMPAT_EXPORT __declspec(dllexport)
1267+
#else
1268+
#define SYCLCOMPAT_EXPORT
1269+
#endif
1270+
1271+
namespace syclcompat {
1272+
enum error_code { SUCCESS = 0, BACKEND_ERROR = 1, DEFAULT_ERROR = 999 };
1273+
}
12421274

1243-
template <class... Args> class sycl_compat_kernel_name;
1244-
template <int Arg> class sycl_compat_kernel_scalar;
1275+
#define SYCLCOMPAT_CHECK_ERROR(expr)
12451276

12461277
int get_sycl_language_version();
12471278

sycl/include/syclcompat/defs.hpp

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,43 @@
3232

3333
#pragma once
3434

35-
template <class... Args> class sycl_compat_kernel_name;
36-
template <int Arg> class sycl_compat_kernel_scalar;
35+
#include <iostream>
3736

38-
#define __sycl_compat_align__(n) alignas(n)
39-
#define __sycl_compat_inline__ __inline__ __attribute__((always_inline))
37+
template <class... Args> class syclcompat_kernel_name;
38+
template <int Arg> class syclcompat_kernel_scalar;
4039

41-
#define __sycl_compat_noinline__ __attribute__((noinline))
40+
#if defined(_MSC_VER)
41+
#define __syclcompat_align__(n) __declspec(align(n))
42+
#define __syclcompat_inline__ __forceinline
43+
#define __syclcompat_noinline__ __declspec(noinline)
44+
#else
45+
#define __syclcompat_align__(n) __attribute__((aligned(n)))
46+
#define __syclcompat_inline__ __inline__ __attribute__((always_inline))
47+
#define __syclcompat_noinline__ __attribute__((noinline))
48+
#endif
4249

43-
#define SYCL_COMPAT_COMPATIBILITY_TEMP (600)
50+
#define SYCLCOMPAT_COMPATIBILITY_TEMP (600)
51+
52+
#ifdef _WIN32
53+
#define SYCLCOMPAT_EXPORT __declspec(dllexport)
54+
#else
55+
#define SYCLCOMPAT_EXPORT
56+
#endif
57+
58+
namespace syclcompat {
59+
enum error_code { SUCCESS = 0, BACKEND_ERROR = 1, DEFAULT_ERROR = 999 };
60+
}
61+
62+
#define SYCLCOMPAT_CHECK_ERROR(expr) \
63+
[&]() { \
64+
try { \
65+
expr; \
66+
return syclcompat::error_code::SUCCESS; \
67+
} catch (sycl::exception const &e) { \
68+
std::cerr << e.what() << std::endl; \
69+
return syclcompat::error_code::BACKEND_ERROR; \
70+
} catch (std::runtime_error const &e) { \
71+
std::cerr << e.what() << std::endl; \
72+
return syclcompat::error_code::DEFAULT_ERROR; \
73+
} \
74+
}()

sycl/test-e2e/syclcompat/defs.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,53 @@
1717
* Defs.cpp
1818
*
1919
* Description:
20-
* __sycl_compat_align__ tests
20+
* Syclcompat macros tests
2121
**************************************************************************/
2222

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

2626
#include <cassert>
27+
#include <iostream>
28+
29+
#include <sycl/sycl.hpp>
30+
2731
#include <syclcompat/defs.hpp>
2832

29-
int main() {
30-
struct __sycl_compat_align__(16) {
33+
void test_align() {
34+
std::cout << __PRETTY_FUNCTION__ << std::endl;
35+
36+
constexpr std::size_t expected_size = 16;
37+
struct __syclcompat_align__(expected_size) {
3138
int a;
3239
char c;
3340
}
3441
s;
35-
assert(sizeof(s) == 16);
42+
assert(sizeof(s) == expected_size);
43+
}
44+
45+
void test_check_error() {
46+
std::cout << __PRETTY_FUNCTION__ << std::endl;
47+
48+
auto sycl_error_throw = []() {
49+
throw sycl::exception(sycl::make_error_code(sycl::errc::invalid),
50+
"Expected invalid exception in test_check_error");
51+
};
52+
53+
auto runtime_error_throw = []() {
54+
throw std::runtime_error("Expected invalid exception in test_check_error");
55+
};
56+
57+
assert(syclcompat::error_code::SUCCESS == SYCLCOMPAT_CHECK_ERROR());
58+
assert(syclcompat::error_code::BACKEND_ERROR ==
59+
SYCLCOMPAT_CHECK_ERROR(sycl_error_throw()));
60+
assert(syclcompat::error_code::DEFAULT_ERROR ==
61+
SYCLCOMPAT_CHECK_ERROR(runtime_error_throw()));
62+
}
63+
64+
int main() {
65+
test_align();
66+
test_check_error();
3667

3768
return 0;
3869
}

0 commit comments

Comments
 (0)