Skip to content

Commit 41ea7ab

Browse files
author
Alexander Johnston
committed
[SYCL] Implement required builtins for libdevice
Rather than have a dependency on SYCL headers in libdevice, implement the required spirv builtins in the libdevice project. Signed-off-by: Alexander Johnston <[email protected]>
1 parent 91b92ed commit 41ea7ab

File tree

3 files changed

+102
-9
lines changed

3 files changed

+102
-9
lines changed

libdevice/cmake/modules/SYCLLibdevice.cmake

-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ endif()
66

77
set(clang $<TARGET_FILE:clang>)
88

9-
set(sycl_spirv_headers ${LLVM_EXTERNAL_SYCL_SOURCE_DIR}/include/CL/__spirv)
10-
119
set(compile_opts
1210
# suppress an error about SYCL_EXTERNAL being used for
1311
# a function with a raw pointer parameter.
@@ -18,9 +16,6 @@ set(compile_opts
1816
# Force definition of CL_SYCL_LANGUAGE_VERSION, as long as
1917
# SYCL specific code is guarded by it.
2018
-sycl-std=2017
21-
# We require the spirv_vars.hpp header from the sycl project
22-
# for access to all the required spirv builtin vars
23-
-I${sycl_spirv_headers}
2419
)
2520

2621
if (WIN32)

libdevice/device.h

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
#ifndef __LIBDEVICE_DEVICE_H__
1010
#define __LIBDEVICE_DEVICE_H__
1111

12-
// We need the following header provided by the sycl project to ensure
13-
// definition of all spirv variables required by the wrapper libraries.
14-
#include "spirv_vars.hpp"
15-
1612
#ifdef __cplusplus
1713
#define EXTERN_C extern "C"
1814
#else // __cplusplus

libdevice/spirv_vars.hpp

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//==---------- spirv_vars.hpp --- SPIRV variables -------------------------==//
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+
#pragma once
10+
11+
#include <cstddef>
12+
#include <cstdint>
13+
14+
#ifdef __SYCL_DEVICE_ONLY__
15+
16+
#ifdef __SYCL_NVPTX__
17+
18+
SYCL_EXTERNAL size_t __spirv_GlobalInvocationId_x();
19+
SYCL_EXTERNAL size_t __spirv_GlobalInvocationId_y();
20+
SYCL_EXTERNAL size_t __spirv_GlobalInvocationId_z();
21+
22+
SYCL_EXTERNAL size_t __spirv_LocalInvocationId_x();
23+
SYCL_EXTERNAL size_t __spirv_LocalInvocationId_y();
24+
SYCL_EXTERNAL size_t __spirv_LocalInvocationId_z();
25+
26+
#else // __SYCL_NVPTX__
27+
28+
typedef size_t size_t_vec __attribute__((ext_vector_type(3)));
29+
extern "C" const __attribute__((opencl_constant))
30+
size_t_vec __spirv_BuiltInGlobalInvocationId;
31+
extern "C" const __attribute__((opencl_constant))
32+
size_t_vec __spirv_BuiltInLocalInvocationId;
33+
34+
SYCL_EXTERNAL inline size_t __spirv_GlobalInvocationId_x() {
35+
return __spirv_BuiltInGlobalInvocationId.x;
36+
}
37+
SYCL_EXTERNAL inline size_t __spirv_GlobalInvocationId_y() {
38+
return __spirv_BuiltInGlobalInvocationId.y;
39+
}
40+
SYCL_EXTERNAL inline size_t __spirv_GlobalInvocationId_z() {
41+
return __spirv_BuiltInGlobalInvocationId.z;
42+
}
43+
44+
SYCL_EXTERNAL inline size_t __spirv_LocalInvocationId_x() {
45+
return __spirv_BuiltInLocalInvocationId.x;
46+
}
47+
SYCL_EXTERNAL inline size_t __spirv_LocalInvocationId_y() {
48+
return __spirv_BuiltInLocalInvocationId.y;
49+
}
50+
SYCL_EXTERNAL inline size_t __spirv_LocalInvocationId_z() {
51+
return __spirv_BuiltInLocalInvocationId.z;
52+
}
53+
54+
#endif // __SYCL_NVPTX__
55+
56+
#define DEFINE_FUNC_ID_TO_XYZ_CONVERTER(POSTFIX) \
57+
template <int ID> static inline size_t get##POSTFIX(); \
58+
template <> size_t get##POSTFIX<0>() { return __spirv_##POSTFIX##_x(); } \
59+
template <> size_t get##POSTFIX<1>() { return __spirv_##POSTFIX##_y(); } \
60+
template <> size_t get##POSTFIX<2>() { return __spirv_##POSTFIX##_z(); }
61+
62+
namespace __spirv {
63+
64+
DEFINE_FUNC_ID_TO_XYZ_CONVERTER(GlobalInvocationId);
65+
DEFINE_FUNC_ID_TO_XYZ_CONVERTER(LocalInvocationId);
66+
67+
} // namespace __spirv
68+
69+
#undef DEFINE_FUNC_ID_TO_XYZ_CONVERTER
70+
71+
#define DEFINE_INIT_SIZES(POSTFIX) \
72+
\
73+
template <int Dim, class DstT> struct InitSizesST##POSTFIX; \
74+
\
75+
template <class DstT> struct InitSizesST##POSTFIX<1, DstT> { \
76+
static DstT initSize() { return {get##POSTFIX<0>()}; } \
77+
}; \
78+
\
79+
template <class DstT> struct InitSizesST##POSTFIX<2, DstT> { \
80+
static DstT initSize() { return {get##POSTFIX<1>(), get##POSTFIX<0>()}; } \
81+
}; \
82+
\
83+
template <class DstT> struct InitSizesST##POSTFIX<3, DstT> { \
84+
static DstT initSize() { \
85+
return {get##POSTFIX<2>(), get##POSTFIX<1>(), get##POSTFIX<0>()}; \
86+
} \
87+
}; \
88+
\
89+
template <int Dims, class DstT> static DstT init##POSTFIX() { \
90+
return InitSizesST##POSTFIX<Dims, DstT>::initSize(); \
91+
}
92+
93+
namespace __spirv {
94+
95+
DEFINE_INIT_SIZES(GlobalInvocationId)
96+
DEFINE_INIT_SIZES(LocalInvocationId)
97+
98+
} // namespace __spirv
99+
100+
#undef DEFINE_INIT_SIZES
101+
102+
#endif // __SYCL_DEVICE_ONLY__

0 commit comments

Comments
 (0)