Skip to content

Commit 857ee51

Browse files
authored
[LIBDEVICE] Guard libdevice code with __SPIR__. (#1672)
Signed-off-by: Vyacheslav Zakharin <[email protected]>
1 parent a0b0f67 commit 857ee51

20 files changed

+109
-139
lines changed

clang/lib/Basic/Targets/SPIR.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ void SPIRTargetInfo::getTargetDefines(const LangOptions &Opts,
2323

2424
void SPIR32TargetInfo::getTargetDefines(const LangOptions &Opts,
2525
MacroBuilder &Builder) const {
26+
SPIRTargetInfo::getTargetDefines(Opts, Builder);
2627
DefineStd(Builder, "SPIR32", Opts);
2728
}
2829

2930
void SPIR64TargetInfo::getTargetDefines(const LangOptions &Opts,
3031
MacroBuilder &Builder) const {
32+
SPIRTargetInfo::getTargetDefines(Opts, Builder);
3133
DefineStd(Builder, "SPIR64", Opts);
3234
}

clang/test/Preprocessor/predefined-macros.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,17 @@
173173

174174
// RUN: %clang_cc1 %s -E -dM -o - -x cl -triple spir-unknown-unknown \
175175
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-SPIR
176-
// CHECK-SPIR: #define __IMAGE_SUPPORT__ 1
176+
// CHECK-SPIR-DAG: #define __IMAGE_SUPPORT__ 1
177+
// CHECK-SPIR-DAG: #define __SPIR__ 1
178+
// CHECK-SPIR-DAG: #define __SPIR32__ 1
179+
// CHECK-SPIR-NOT: #define __SPIR64__ 1
180+
181+
// RUN: %clang_cc1 %s -E -dM -o - -x cl -triple spir64-unknown-unknown \
182+
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-SPIR64
183+
// CHECK-SPIR64-DAG: #define __IMAGE_SUPPORT__ 1
184+
// CHECK-SPIR64-DAG: #define __SPIR__ 1
185+
// CHECK-SPIR64-DAG: #define __SPIR64__ 1
186+
// CHECK-SPIR32-NOT: #define __SPIR32__ 1
177187

178188
// RUN: %clang_cc1 %s -E -dM -o - -x hip -triple amdgcn-amd-amdhsa \
179189
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-HIP

libdevice/cmake/modules/SYCLLibdevice.cmake

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if (WIN32)
2626
${CMAKE_CURRENT_SOURCE_DIR}/msvc_wrapper.cpp
2727
-o ${devicelib-obj-file}
2828
MAIN_DEPENDENCY msvc_wrapper.cpp
29-
DEPENDS wrapper.h device.h clang
29+
DEPENDS wrapper.h device.h spirv_vars.h clang
3030
VERBATIM)
3131
else()
3232
set(devicelib-obj-file ${binary_dir}/libsycl-glibc.o)
@@ -36,7 +36,7 @@ else()
3636
${CMAKE_CURRENT_SOURCE_DIR}/glibc_wrapper.cpp
3737
-o ${devicelib-obj-file}
3838
MAIN_DEPENDENCY glibc_wrapper.cpp
39-
DEPENDS wrapper.h device.h clang
39+
DEPENDS wrapper.h device.h spirv_vars.h clang
4040
VERBATIM)
4141
endif()
4242

@@ -86,7 +86,7 @@ add_custom_command(OUTPUT ${binary_dir}/libsycl-fallback-cassert.spv
8686
${CMAKE_CURRENT_SOURCE_DIR}/fallback-cassert.cpp
8787
-o ${binary_dir}/libsycl-fallback-cassert.spv
8888
MAIN_DEPENDENCY fallback-cassert.cpp
89-
DEPENDS wrapper.h device.h clang llvm-spirv
89+
DEPENDS wrapper.h device.h clang spirv_vars.h llvm-spirv
9090
VERBATIM)
9191

9292
add_custom_command(OUTPUT ${binary_dir}/libsycl-fallback-complex.spv

libdevice/cmath_wrapper.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "device.h"
109
#include "device_math.h"
1110

11+
#ifdef __SPIR__
12+
1213
DEVICE_EXTERN_C
1314
float scalbnf(float x, int n) { return __devicelib_scalbnf(x, n); }
1415

@@ -367,4 +368,5 @@ float _FSinh(float x, float y) { // compute y * sinh(x), |y| <= 1
367368
return neg ? -x : x;
368369
}
369370
}
370-
#endif
371+
#endif // defined(_WIN32)
372+
#endif // __SPIR__

libdevice/cmath_wrapper_fp64.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "device.h"
109
#include "device_math.h"
1110

11+
#ifdef __SPIR__
12+
1213
// All exported functions in math and complex device libraries are weak
1314
// reference. If users provide their own math or complex functions(with
1415
// the prototype), functions in device libraries will be ignored and
@@ -397,4 +398,5 @@ double _Sinh(double x, double y) { // compute y * sinh(x), |y| <= 1
397398
return neg ? -x : x;
398399
}
399400
}
400-
#endif
401+
#endif // defined(_WIN32)
402+
#endif // __SPIR__

libdevice/complex_wrapper.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "device.h"
109
#include "device_complex.h"
1110

11+
#ifdef __SPIR__
12+
1213
DEVICE_EXTERN_C
1314
float cimagf(float __complex__ z) { return __devicelib_cimagf(z); }
1415

@@ -98,3 +99,4 @@ DEVICE_EXTERN_C
9899
float __complex__ __divsc3(float __a, float __b, float __c, float __d) {
99100
return __devicelib___divsc3(__a, __b, __c, __d);
100101
}
102+
#endif // __SPIR__

libdevice/complex_wrapper_fp64.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "device.h"
109
#include "device_complex.h"
1110

11+
#ifdef __SPIR__
12+
1213
DEVICE_EXTERN_C
1314
double cimag(double __complex__ z) { return __devicelib_cimag(z); }
1415

@@ -98,3 +99,4 @@ DEVICE_EXTERN_C
9899
double __complex__ __divdc3(double __a, double __b, double __c, double __d) {
99100
return __devicelib___divdc3(__a, __b, __c, __d);
100101
}
102+
#endif // __SPIR__

libdevice/device.h

+3-15
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,14 @@
1515
#define EXTERN_C
1616
#endif // __cplusplus
1717

18-
#ifdef CL_SYCL_LANGUAGE_VERSION
19-
#ifndef SYCL_EXTERNAL
20-
#define SYCL_EXTERNAL
21-
#endif // SYCL_EXTERNAL
22-
18+
#ifdef __SPIR__
2319
#ifdef __SYCL_DEVICE_ONLY__
2420
#define DEVICE_EXTERNAL SYCL_EXTERNAL __attribute__((weak))
2521
#else // __SYCL_DEVICE_ONLY__
26-
#define DEVICE_EXTERNAL static
27-
#undef EXTERN_C
28-
#define EXTERN_C
22+
#define DEVICE_EXTERNAL __attribute__((weak))
2923
#endif // __SYCL_DEVICE_ONLY__
30-
#else // CL_SYCL_LANGUAGE_VERSION
31-
#define DEVICE_EXTERNAL
32-
#endif // CL_SYCL_LANGUAGE_VERSION
3324

3425
#define DEVICE_EXTERN_C DEVICE_EXTERNAL EXTERN_C
35-
36-
// We need the following header to ensure the definition of all spirv variables
37-
// required by the wrapper libraries.
38-
#include "spirv_vars.hpp"
26+
#endif // __SPIR__
3927

4028
#endif // __LIBDEVICE_DEVICE_H__

libdevice/device_complex.h

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include "device.h"
1212

13+
#ifdef __SPIR__
14+
1315
// TODO: This needs to be more robust.
1416
// clang doesn't recognize the c11 CMPLX macro, but it does have
1517
// its own syntax extension for initializing a complex as a struct.
@@ -163,4 +165,5 @@ double __complex__ __devicelib___divdc3(double a, double b, double c, double d);
163165

164166
DEVICE_EXTERN_C
165167
float __complex__ __devicelib___divsc3(float a, float b, float c, float d);
168+
#endif // __SPIR__
166169
#endif // __LIBDEVICE_DEVICE_COMPLEX_H_

libdevice/device_math.h

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "device.h"
1313

14+
#ifdef __SPIR__
15+
1416
DEVICE_EXTERN_C
1517
double __devicelib_log(double x);
1618

@@ -247,4 +249,5 @@ float __devicelib_logbf(float x);
247249

248250
DEVICE_EXTERN_C
249251
float __devicelib_scalbnf(float x, int n);
252+
#endif // __SPIR__
250253
#endif // __LIBDEVICE_DEVICE_MATH_H__

libdevice/fallback-cassert.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "wrapper.h"
1010

11+
#ifdef __SPIR__
1112
static const __attribute__((opencl_constant)) char assert_fmt[] =
1213
"%s:%d: %s: global id: [%lu,%lu,%lu], local id: [%lu,%lu,%lu] "
1314
"Assertion `%s` failed.\n";
@@ -30,3 +31,4 @@ DEVICE_EXTERN_C void __devicelib_assert_fail(const char *expr, const char *file,
3031
// volatile int *die = (int *)0x0;
3132
// *die = 0xdead;
3233
}
34+
#endif // __SPIR__

libdevice/fallback-cmath-fp64.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "device_math.h"
1010

11+
#ifdef __SPIR__
12+
1113
DEVICE_EXTERN_C
1214
double __devicelib_log(double x) { return __spirv_ocl_log(x); }
1315

@@ -138,3 +140,4 @@ double __devicelib_asinh(double x) { return __spirv_ocl_asinh(x); }
138140

139141
DEVICE_EXTERN_C
140142
double __devicelib_atanh(double x) { return __spirv_ocl_atanh(x); }
143+
#endif // __SPIR__

libdevice/fallback-cmath.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "device_math.h"
1010

11+
#ifdef __SPIR__
12+
1113
DEVICE_EXTERN_C
1214
float __devicelib_scalbnf(float x, int n) { return __spirv_ocl_ldexp(x, n); }
1315

@@ -139,3 +141,5 @@ float __devicelib_asinhf(float x) { return __spirv_ocl_asinh(x); }
139141

140142
DEVICE_EXTERN_C
141143
float __devicelib_atanhf(float x) { return __spirv_ocl_atanh(x); }
144+
145+
#endif // __SPIR__

libdevice/fallback-complex-fp64.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "device_complex.h"
1010
#include "device_math.h"
11+
12+
#ifdef __SPIR__
1113
#include <cmath>
1214

1315
DEVICE_EXTERN_C
@@ -423,3 +425,4 @@ double __complex__ __devicelib_catan(double __complex__ z) {
423425
__devicelib_catanh(CMPLX(-__devicelib_cimag(z), __devicelib_creal(z)));
424426
return CMPLX(__devicelib_cimag(w), -__devicelib_creal(w));
425427
}
428+
#endif // __SPIR__

libdevice/fallback-complex.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "device_complex.h"
1010
#include "device_math.h"
11+
12+
#ifdef __SPIR__
1113
#include <cmath>
1214

1315
DEVICE_EXTERN_C
@@ -427,3 +429,4 @@ float __complex__ __devicelib_catanf(float __complex__ z) {
427429
CMPLXF(-__devicelib_cimagf(z), __devicelib_crealf(z)));
428430
return CMPLXF(__devicelib_cimagf(w), -__devicelib_crealf(w));
429431
}
432+
#endif // __SPIR__

libdevice/glibc_wrapper.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "device.h"
109
#include "wrapper.h"
1110

11+
#ifdef __SPIR__
1212
DEVICE_EXTERN_C
1313
void __assert_fail(const char *expr, const char *file, unsigned int line,
1414
const char *func) {
@@ -18,3 +18,4 @@ void __assert_fail(const char *expr, const char *file, unsigned int line,
1818
__spirv_LocalInvocationId_x(), __spirv_LocalInvocationId_y(),
1919
__spirv_LocalInvocationId_z());
2020
}
21+
#endif // __SPIR__

libdevice/msvc_wrapper.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "device.h"
109
#include "wrapper.h"
1110

11+
#ifdef __SPIR__
1212
// Truncates a wide (16 or 32 bit) string (wstr) into an ASCII string (str).
1313
// Any non-ASCII characters are replaced by question mark '?'.
1414
static void __truncate_wchar_char_str(const wchar_t *wstr, char *str,
@@ -37,3 +37,4 @@ void _wassert(const wchar_t *wexpr, const wchar_t *wfile, unsigned line) {
3737
__spirv_LocalInvocationId_x(), __spirv_LocalInvocationId_y(),
3838
__spirv_LocalInvocationId_z());
3939
}
40+
#endif // __SPIR__

libdevice/spirv_vars.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//==---------- spirv_vars.h --- SPIRV variables --------------*- C++ -*-----==//
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 __LIBDEVICE_SPIRV_VARS_H
10+
#define __LIBDEVICE_SPIRV_VARS_H
11+
12+
#include "device.h"
13+
14+
#ifdef __SPIR__
15+
16+
#include <cstddef>
17+
#include <cstdint>
18+
19+
typedef size_t size_t_vec __attribute__((ext_vector_type(3)));
20+
extern "C" const size_t_vec __spirv_BuiltInGlobalInvocationId;
21+
extern "C" const size_t_vec __spirv_BuiltInLocalInvocationId;
22+
23+
DEVICE_EXTERNAL inline size_t __spirv_GlobalInvocationId_x() {
24+
return __spirv_BuiltInGlobalInvocationId.x;
25+
}
26+
DEVICE_EXTERNAL inline size_t __spirv_GlobalInvocationId_y() {
27+
return __spirv_BuiltInGlobalInvocationId.y;
28+
}
29+
DEVICE_EXTERNAL inline size_t __spirv_GlobalInvocationId_z() {
30+
return __spirv_BuiltInGlobalInvocationId.z;
31+
}
32+
33+
DEVICE_EXTERNAL inline size_t __spirv_LocalInvocationId_x() {
34+
return __spirv_BuiltInLocalInvocationId.x;
35+
}
36+
DEVICE_EXTERNAL inline size_t __spirv_LocalInvocationId_y() {
37+
return __spirv_BuiltInLocalInvocationId.y;
38+
}
39+
DEVICE_EXTERNAL inline size_t __spirv_LocalInvocationId_z() {
40+
return __spirv_BuiltInLocalInvocationId.z;
41+
}
42+
43+
#endif // __SPIR__
44+
#endif // __LIBDEVICE_SPIRV_VARS_H

0 commit comments

Comments
 (0)