Skip to content

Commit b462ba4

Browse files
committed
[SYCL][COMPAT] Added Check Error Macro. Updated macro definitions.
1 parent cf402b8 commit b462ba4

File tree

3 files changed

+101
-15
lines changed

3 files changed

+101
-15
lines changed

sycl/doc/syclcompat/README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,16 +1177,45 @@ kernel names during machine translation.
11771177
`get_sycl_language_version` returns an integer representing the version of the
11781178
SYCL spec supported by the current SYCL compiler.
11791179

1180+
The `SYCLCOMPAT_CHECK_ERROR` macro encapsulates an error-handling mechanism for
1181+
expressions that might throw `sycl::exception`. If no exceptions are thrown, it
1182+
returns `syclcompat::error_code::SUCCESS`. If an exception is caught, it prints
1183+
the error message to the standard error stream and returns
1184+
`syclcompat::error_code::DEFAULT_ERROR`.
1185+
11801186
``` c++
11811187
namespace syclcompat {
11821188

1183-
#define __sycl_compat_align__(n) __attribute__((aligned(n)))
1184-
#define __sycl_compat_inline__ __inline__ __attribute__((always_inline))
1189+
template <class... Args> class syclcompat_kernel_name;
1190+
template <int Arg> class syclcompat_kernel_scalar;
1191+
1192+
#if defined(_MSC_VER)
1193+
#define __syclcompat_align__(n) __declspec(align(n))
1194+
#define __syclcompat_inline__ __forceinline
1195+
#else
1196+
#define __syclcompat_align__(n) __attribute__((aligned(n)))
1197+
#define __syclcompat_inline__ __inline__ __attribute__((always_inline))
1198+
#endif
1199+
1200+
#if defined(_MSC_VER)
1201+
#define __syclcompat_noinline__ __declspec(noinline)
1202+
#else
1203+
#define __syclcompat_noinline__ __attribute__((noinline))
1204+
#endif
1205+
1206+
#define SYCLCOMPAT_COMPATIBILITY_TEMP (600)
11851207

1186-
#define __sycl_compat_noinline__ __attribute__((noinline))
1208+
#ifdef _WIN32
1209+
#define SYCLCOMPAT_EXPORT __declspec(dllexport)
1210+
#else
1211+
#define SYCLCOMPAT_EXPORT
1212+
#endif
1213+
1214+
namespace syclcompat {
1215+
enum error_code { SUCCESS = 0, DEFAULT_ERROR = 999 };
1216+
}
11871217

1188-
template <class... Args> class sycl_compat_kernel_name;
1189-
template <int Arg> class sycl_compat_kernel_scalar;
1218+
#define SYCLCOMPAT_CHECK_ERROR(expr)
11901219

11911220
int get_sycl_language_version();
11921221

sycl/include/syclcompat/defs.hpp

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,44 @@
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+
#else
44+
#define __syclcompat_align__(n) __attribute__((aligned(n)))
45+
#define __syclcompat_inline__ __inline__ __attribute__((always_inline))
46+
#endif
4247

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

sycl/test-e2e/syclcompat/defs.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,47 @@
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 error_throw = []() {
49+
throw sycl::exception(sycl::make_error_code(sycl::errc::invalid),
50+
"Expected invalid exception in test_check_error");
51+
};
52+
53+
assert(syclcompat::error_code::SUCCESS == SYCLCOMPAT_CHECK_ERROR());
54+
assert(syclcompat::error_code::DEFAULT_ERROR ==
55+
SYCLCOMPAT_CHECK_ERROR(error_throw()));
56+
}
57+
58+
int main() {
59+
test_align();
60+
test_check_error();
3661

3762
return 0;
3863
}

0 commit comments

Comments
 (0)