Skip to content

Commit 5291d98

Browse files
author
Andrew Savonichev
committed
[SYCL] Move devicelib assert wrapper to header files
Object file is no longer required to enable devicelib assert function. Support for other devicelib functions will follow. Signed-off-by: Andrew Savonichev <[email protected]>
1 parent bc01115 commit 5291d98

File tree

6 files changed

+134
-41
lines changed

6 files changed

+134
-41
lines changed

libdevice/cmake/modules/SYCLLibdevice.cmake

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ if (WIN32)
33
else()
44
set(binary_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
55
endif()
6+
set(include_dir "${LLVM_BINARY_DIR}/include/sycl")
67

78
set(clang $<TARGET_FILE:clang>)
89

@@ -18,28 +19,6 @@ set(compile_opts
1819
-sycl-std=2017
1920
)
2021

21-
if (WIN32)
22-
set(devicelib-obj-file ${binary_dir}/libsycl-msvc.o)
23-
add_custom_command(OUTPUT ${devicelib-obj-file}
24-
COMMAND ${clang} -fsycl -c
25-
${compile_opts}
26-
${CMAKE_CURRENT_SOURCE_DIR}/msvc_wrapper.cpp
27-
-o ${devicelib-obj-file}
28-
MAIN_DEPENDENCY msvc_wrapper.cpp
29-
DEPENDS wrapper.h device.h clang
30-
VERBATIM)
31-
else()
32-
set(devicelib-obj-file ${binary_dir}/libsycl-glibc.o)
33-
add_custom_command(OUTPUT ${devicelib-obj-file}
34-
COMMAND ${clang} -fsycl -c
35-
${compile_opts}
36-
${CMAKE_CURRENT_SOURCE_DIR}/glibc_wrapper.cpp
37-
-o ${devicelib-obj-file}
38-
MAIN_DEPENDENCY glibc_wrapper.cpp
39-
DEPENDS wrapper.h device.h clang
40-
VERBATIM)
41-
endif()
42-
4322
set(devicelib-obj-complex ${binary_dir}/libsycl-complex.o)
4423
add_custom_command(OUTPUT ${devicelib-obj-complex}
4524
COMMAND ${clang} -fsycl -c
@@ -125,8 +104,20 @@ add_custom_command(OUTPUT ${binary_dir}/libsycl-fallback-cmath-fp64.spv
125104
DEPENDS device_math.h device.h clang llvm-spirv
126105
VERBATIM)
127106

107+
add_custom_target(libsycldevice-inc)
108+
add_custom_command(
109+
TARGET libsycldevice-inc POST_BUILD
110+
COMMAND ${CMAKE_COMMAND} -E make_directory ${include_dir}/CL/sycl/devicelib
111+
COMMAND ${CMAKE_COMMAND} -E copy
112+
${CMAKE_CURRENT_SOURCE_DIR}/include/devicelib.h
113+
${include_dir}/CL/sycl/devicelib
114+
COMMAND ${CMAKE_COMMAND} -E copy
115+
${CMAKE_CURRENT_SOURCE_DIR}/include/devicelib-assert.h
116+
${include_dir}/CL/sycl/devicelib
117+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/include/devicelib.h
118+
${CMAKE_CURRENT_SOURCE_DIR}/include/devicelib-assert.h)
119+
128120
add_custom_target(libsycldevice-obj DEPENDS
129-
${devicelib-obj-file}
130121
${devicelib-obj-complex}
131122
${devicelib-obj-complex-fp64}
132123
${devicelib-obj-cmath}
@@ -139,7 +130,7 @@ add_custom_target(libsycldevice-spv DEPENDS
139130
${binary_dir}/libsycl-fallback-cmath.spv
140131
${binary_dir}/libsycl-fallback-cmath-fp64.spv
141132
)
142-
add_custom_target(libsycldevice DEPENDS libsycldevice-obj libsycldevice-spv)
133+
add_custom_target(libsycldevice DEPENDS libsycldevice-inc libsycldevice-obj libsycldevice-spv)
143134

144135
# Place device libraries near the libsycl.so library in an install
145136
# directory as well
@@ -149,8 +140,12 @@ else()
149140
set(install_dest lib${LLVM_LIBDIR_SUFFIX})
150141
endif()
151142

152-
install(FILES ${devicelib-obj-file}
153-
${binary_dir}/libsycl-fallback-cassert.spv
143+
install(FILES ${include_dir}/CL/sycl/devicelib/devicelib.h
144+
${include_dir}/CL/sycl/devicelib/devicelib-assert.h
145+
DESTINATION include/sycl/CL/sycl/devicelib
146+
COMPONENT libsycldevice)
147+
148+
install(FILES ${binary_dir}/libsycl-fallback-cassert.spv
154149
${devicelib-obj-complex}
155150
${binary_dir}/libsycl-fallback-complex.spv
156151
${devicelib-obj-complex-fp64}

libdevice/include/devicelib-assert.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//==--------- devicelib-assert.h - wrapper definition for C assert ---------==//
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+
#include "devicelib.h"
10+
#include <cstddef>
11+
#include <cstdint>
12+
13+
DEVICE_EXTERNAL size_t __spirv_GlobalInvocationId_x();
14+
DEVICE_EXTERNAL size_t __spirv_GlobalInvocationId_y();
15+
DEVICE_EXTERNAL size_t __spirv_GlobalInvocationId_z();
16+
17+
DEVICE_EXTERNAL size_t __spirv_LocalInvocationId_x();
18+
DEVICE_EXTERNAL size_t __spirv_LocalInvocationId_y();
19+
DEVICE_EXTERNAL size_t __spirv_LocalInvocationId_z();
20+
21+
DEVICE_EXTERN_C
22+
void __devicelib_assert_fail(const char *expr, const char *file, int32_t line,
23+
const char *func, uint64_t gid0, uint64_t gid1,
24+
uint64_t gid2, uint64_t lid0, uint64_t lid1,
25+
uint64_t lid2);
26+
27+
28+
#ifdef __DEVICELIB_GLIBC__
29+
EXTERN_C inline void __assert_fail(const char *expr, const char *file,
30+
unsigned int line, const char *func) {
31+
__devicelib_assert_fail(
32+
expr, file, line, func, __spirv_GlobalInvocationId_x(),
33+
__spirv_GlobalInvocationId_y(), __spirv_GlobalInvocationId_z(),
34+
__spirv_LocalInvocationId_x(), __spirv_LocalInvocationId_y(),
35+
__spirv_LocalInvocationId_z());
36+
37+
}
38+
#elif defined(__DEVICELIB_MSLIBC__)
39+
// Truncates a wide (16 or 32 bit) string (wstr) into an ASCII string (str).
40+
// Any non-ASCII characters are replaced by question mark '?'.
41+
static inline void __truncate_wchar_char_str(const wchar_t *wstr, char *str,
42+
size_t str_size) {
43+
str_size -= 1; // reserve for NULL terminator
44+
while (str_size > 0 && *wstr != L'\0') {
45+
wchar_t w = *wstr++;
46+
*str++ = (w > 0 && w < 127) ? (char)w : '?';
47+
str_size--;
48+
}
49+
*str = '\0';
50+
}
51+
52+
EXTERN_C inline void _wassert(const wchar_t *wexpr, const wchar_t *wfile,
53+
unsigned line) {
54+
// Paths and expressions that are longer than 256 characters are going to be
55+
// truncated.
56+
char file[256];
57+
__truncate_wchar_char_str(wfile, file, sizeof(file));
58+
char expr[256];
59+
__truncate_wchar_char_str(wexpr, expr, sizeof(expr));
60+
61+
__devicelib_assert_fail(
62+
expr, file, line, /*func=*/nullptr, __spirv_GlobalInvocationId_x(),
63+
__spirv_GlobalInvocationId_y(), __spirv_GlobalInvocationId_z(),
64+
__spirv_LocalInvocationId_x(), __spirv_LocalInvocationId_y(),
65+
__spirv_LocalInvocationId_z());
66+
}
67+
68+
#endif

libdevice/include/devicelib.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//==------------ devicelib.h - macros for devicelib wrappers ---------------==//
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+
#ifndef _WIN32
10+
#include <features.h> // for GLIBC macro
11+
#endif
12+
13+
// Devicelib wraps a system C or C++ library
14+
#ifdef __GLIBC__
15+
#define __DEVICELIB_GLIBC__ 1
16+
#elif defined(_WIN32)
17+
#define __DEVICELIB_MSLIBC__ 1
18+
#endif
19+
20+
#ifdef __cplusplus
21+
#define EXTERN_C extern "C"
22+
#else // __cplusplus
23+
#define EXTERN_C
24+
#endif // __cplusplus
25+
26+
#ifdef CL_SYCL_LANGUAGE_VERSION
27+
#ifndef SYCL_EXTERNAL
28+
#define SYCL_EXTERNAL
29+
#endif // SYCL_EXTERNAL
30+
31+
#ifdef __SYCL_DEVICE_ONLY__
32+
#define DEVICE_EXTERNAL SYCL_EXTERNAL __attribute__((weak))
33+
#else // __SYCL_DEVICE_ONLY__
34+
#define DEVICE_EXTERNAL static
35+
#undef EXTERN_C
36+
#define EXTERN_C
37+
#endif // __SYCL_DEVICE_ONLY__
38+
#else // CL_SYCL_LANGUAGE_VERSION
39+
#define DEVICE_EXTERNAL
40+
#endif // CL_SYCL_LANGUAGE_VERSION
41+
42+
#define DEVICE_EXTERN_C DEVICE_EXTERNAL EXTERN_C

sycl/include/CL/sycl/builtins.hpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,17 +1547,7 @@ detail::enable_if_t<detail::is_genfloatf<T>::value, T> tan(T x) __NOEXC {
15471547
} // __SYCL_INLINE_NAMESPACE(cl)
15481548

15491549
#ifdef __SYCL_DEVICE_ONLY__
1550-
#ifdef __GLIBC__
1551-
extern "C" {
1552-
extern SYCL_EXTERNAL void __assert_fail(const char *expr, const char *file,
1553-
unsigned int line, const char *func);
1554-
}
1555-
#elif defined(_WIN32)
1556-
extern "C" {
1557-
extern SYCL_EXTERNAL void _wassert(const wchar_t *wexpr, const wchar_t *wfile,
1558-
unsigned line);
1559-
}
1560-
#endif
1550+
#include "devicelib/devicelib-assert.h"
15611551
#endif // __SYCL_DEVICE_ONLY__
15621552

15631553
#undef __NOEXC

sycl/test/devicelib/assert-windows.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
// Disable the test until the fix reaches SYCL test infrastructure.
55
// XFAIL: *
66
//
7-
// RUN: %clangxx -fsycl -c %s -o %t.o
8-
// RUN: %clangxx -fsycl %t.o %sycl_libs_dir/../bin/libsycl-msvc.o -o %t.out
7+
// RUN: %clangxx -fsycl %s -o %t.out
98
//
109
// MSVC implementation of assert does not call an unreachable built-in, so the
1110
// program doesn't terminate when fallback is used.

sycl/test/devicelib/assert.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// REQUIRES: cpu,linux
2-
// RUN: %clangxx -fsycl -c %s -o %t.o
3-
// RUN: %clangxx -fsycl %t.o %sycl_libs_dir/libsycl-glibc.o -o %t.out
2+
// RUN: %clangxx -fsycl %s -o %t.out
43
// (see the other RUN lines below; it is a bit complicated)
54
//
65
// assert() call in device code guarantees nothing: on some devices it behaves

0 commit comments

Comments
 (0)