diff --git a/clang/lib/Basic/Targets/SPIR.cpp b/clang/lib/Basic/Targets/SPIR.cpp index a9b815d13bc15..9b7aab85314a9 100644 --- a/clang/lib/Basic/Targets/SPIR.cpp +++ b/clang/lib/Basic/Targets/SPIR.cpp @@ -23,10 +23,12 @@ void SPIRTargetInfo::getTargetDefines(const LangOptions &Opts, void SPIR32TargetInfo::getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const { + SPIRTargetInfo::getTargetDefines(Opts, Builder); DefineStd(Builder, "SPIR32", Opts); } void SPIR64TargetInfo::getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const { + SPIRTargetInfo::getTargetDefines(Opts, Builder); DefineStd(Builder, "SPIR64", Opts); } diff --git a/clang/test/Preprocessor/predefined-macros.c b/clang/test/Preprocessor/predefined-macros.c index def105f4c52e2..c7d33c54901c8 100644 --- a/clang/test/Preprocessor/predefined-macros.c +++ b/clang/test/Preprocessor/predefined-macros.c @@ -173,7 +173,17 @@ // RUN: %clang_cc1 %s -E -dM -o - -x cl -triple spir-unknown-unknown \ // RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-SPIR -// CHECK-SPIR: #define __IMAGE_SUPPORT__ 1 +// CHECK-SPIR-DAG: #define __IMAGE_SUPPORT__ 1 +// CHECK-SPIR-DAG: #define __SPIR__ 1 +// CHECK-SPIR-DAG: #define __SPIR32__ 1 +// CHECK-SPIR-NOT: #define __SPIR64__ 1 + +// RUN: %clang_cc1 %s -E -dM -o - -x cl -triple spir64-unknown-unknown \ +// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-SPIR64 +// CHECK-SPIR64-DAG: #define __IMAGE_SUPPORT__ 1 +// CHECK-SPIR64-DAG: #define __SPIR__ 1 +// CHECK-SPIR64-DAG: #define __SPIR64__ 1 +// CHECK-SPIR32-NOT: #define __SPIR32__ 1 // RUN: %clang_cc1 %s -E -dM -o - -x hip -triple amdgcn-amd-amdhsa \ // RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-HIP diff --git a/libdevice/cmake/modules/SYCLLibdevice.cmake b/libdevice/cmake/modules/SYCLLibdevice.cmake index 847cac46f4a42..ac088a8313d2d 100644 --- a/libdevice/cmake/modules/SYCLLibdevice.cmake +++ b/libdevice/cmake/modules/SYCLLibdevice.cmake @@ -26,7 +26,7 @@ if (WIN32) ${CMAKE_CURRENT_SOURCE_DIR}/msvc_wrapper.cpp -o ${devicelib-obj-file} MAIN_DEPENDENCY msvc_wrapper.cpp - DEPENDS wrapper.h device.h clang + DEPENDS wrapper.h device.h spirv_vars.h clang VERBATIM) else() set(devicelib-obj-file ${binary_dir}/libsycl-glibc.o) @@ -36,7 +36,7 @@ else() ${CMAKE_CURRENT_SOURCE_DIR}/glibc_wrapper.cpp -o ${devicelib-obj-file} MAIN_DEPENDENCY glibc_wrapper.cpp - DEPENDS wrapper.h device.h clang + DEPENDS wrapper.h device.h spirv_vars.h clang VERBATIM) endif() @@ -86,7 +86,7 @@ add_custom_command(OUTPUT ${binary_dir}/libsycl-fallback-cassert.spv ${CMAKE_CURRENT_SOURCE_DIR}/fallback-cassert.cpp -o ${binary_dir}/libsycl-fallback-cassert.spv MAIN_DEPENDENCY fallback-cassert.cpp - DEPENDS wrapper.h device.h clang llvm-spirv + DEPENDS wrapper.h device.h clang spirv_vars.h llvm-spirv VERBATIM) add_custom_command(OUTPUT ${binary_dir}/libsycl-fallback-complex.spv diff --git a/libdevice/cmath_wrapper.cpp b/libdevice/cmath_wrapper.cpp index 1d45fe081d03c..321092a3720b0 100644 --- a/libdevice/cmath_wrapper.cpp +++ b/libdevice/cmath_wrapper.cpp @@ -6,9 +6,10 @@ // //===----------------------------------------------------------------------===// -#include "device.h" #include "device_math.h" +#ifdef __SPIR__ + DEVICE_EXTERN_C float scalbnf(float x, int n) { return __devicelib_scalbnf(x, n); } @@ -367,4 +368,5 @@ float _FSinh(float x, float y) { // compute y * sinh(x), |y| <= 1 return neg ? -x : x; } } -#endif +#endif // defined(_WIN32) +#endif // __SPIR__ diff --git a/libdevice/cmath_wrapper_fp64.cpp b/libdevice/cmath_wrapper_fp64.cpp index 84e6e8ab035ac..f6e55a6e30a4d 100644 --- a/libdevice/cmath_wrapper_fp64.cpp +++ b/libdevice/cmath_wrapper_fp64.cpp @@ -6,9 +6,10 @@ // //===----------------------------------------------------------------------===// -#include "device.h" #include "device_math.h" +#ifdef __SPIR__ + // All exported functions in math and complex device libraries are weak // reference. If users provide their own math or complex functions(with // 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 return neg ? -x : x; } } -#endif +#endif // defined(_WIN32) +#endif // __SPIR__ diff --git a/libdevice/complex_wrapper.cpp b/libdevice/complex_wrapper.cpp index 845aad2f5ecb7..9e930859393d7 100644 --- a/libdevice/complex_wrapper.cpp +++ b/libdevice/complex_wrapper.cpp @@ -6,9 +6,10 @@ // //===----------------------------------------------------------------------===// -#include "device.h" #include "device_complex.h" +#ifdef __SPIR__ + DEVICE_EXTERN_C float cimagf(float __complex__ z) { return __devicelib_cimagf(z); } @@ -98,3 +99,4 @@ DEVICE_EXTERN_C float __complex__ __divsc3(float __a, float __b, float __c, float __d) { return __devicelib___divsc3(__a, __b, __c, __d); } +#endif // __SPIR__ diff --git a/libdevice/complex_wrapper_fp64.cpp b/libdevice/complex_wrapper_fp64.cpp index d4dfe3733c2ed..b1adb09f90a73 100644 --- a/libdevice/complex_wrapper_fp64.cpp +++ b/libdevice/complex_wrapper_fp64.cpp @@ -6,9 +6,10 @@ // //===----------------------------------------------------------------------===// -#include "device.h" #include "device_complex.h" +#ifdef __SPIR__ + DEVICE_EXTERN_C double cimag(double __complex__ z) { return __devicelib_cimag(z); } @@ -98,3 +99,4 @@ DEVICE_EXTERN_C double __complex__ __divdc3(double __a, double __b, double __c, double __d) { return __devicelib___divdc3(__a, __b, __c, __d); } +#endif // __SPIR__ diff --git a/libdevice/device.h b/libdevice/device.h index 4ddeb99f41a59..1e37da3052a78 100644 --- a/libdevice/device.h +++ b/libdevice/device.h @@ -15,26 +15,14 @@ #define EXTERN_C #endif // __cplusplus -#ifdef CL_SYCL_LANGUAGE_VERSION -#ifndef SYCL_EXTERNAL -#define SYCL_EXTERNAL -#endif // SYCL_EXTERNAL - +#ifdef __SPIR__ #ifdef __SYCL_DEVICE_ONLY__ #define DEVICE_EXTERNAL SYCL_EXTERNAL __attribute__((weak)) #else // __SYCL_DEVICE_ONLY__ -#define DEVICE_EXTERNAL static -#undef EXTERN_C -#define EXTERN_C +#define DEVICE_EXTERNAL __attribute__((weak)) #endif // __SYCL_DEVICE_ONLY__ -#else // CL_SYCL_LANGUAGE_VERSION -#define DEVICE_EXTERNAL -#endif // CL_SYCL_LANGUAGE_VERSION #define DEVICE_EXTERN_C DEVICE_EXTERNAL EXTERN_C - -// We need the following header to ensure the definition of all spirv variables -// required by the wrapper libraries. -#include "spirv_vars.hpp" +#endif // __SPIR__ #endif // __LIBDEVICE_DEVICE_H__ diff --git a/libdevice/device_complex.h b/libdevice/device_complex.h index ff8b4f0730d5d..317b631f85ac1 100644 --- a/libdevice/device_complex.h +++ b/libdevice/device_complex.h @@ -10,6 +10,8 @@ #include "device.h" +#ifdef __SPIR__ + // TODO: This needs to be more robust. // clang doesn't recognize the c11 CMPLX macro, but it does have // 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); DEVICE_EXTERN_C float __complex__ __devicelib___divsc3(float a, float b, float c, float d); +#endif // __SPIR__ #endif // __LIBDEVICE_DEVICE_COMPLEX_H_ diff --git a/libdevice/device_math.h b/libdevice/device_math.h index 8d753099d92b7..5d15a80d73da3 100644 --- a/libdevice/device_math.h +++ b/libdevice/device_math.h @@ -11,6 +11,8 @@ #include "device.h" +#ifdef __SPIR__ + DEVICE_EXTERN_C double __devicelib_log(double x); @@ -247,4 +249,5 @@ float __devicelib_logbf(float x); DEVICE_EXTERN_C float __devicelib_scalbnf(float x, int n); +#endif // __SPIR__ #endif // __LIBDEVICE_DEVICE_MATH_H__ diff --git a/libdevice/fallback-cassert.cpp b/libdevice/fallback-cassert.cpp index 9225189b1f992..724d4635fb0b5 100644 --- a/libdevice/fallback-cassert.cpp +++ b/libdevice/fallback-cassert.cpp @@ -8,6 +8,7 @@ #include "wrapper.h" +#ifdef __SPIR__ static const __attribute__((opencl_constant)) char assert_fmt[] = "%s:%d: %s: global id: [%lu,%lu,%lu], local id: [%lu,%lu,%lu] " "Assertion `%s` failed.\n"; @@ -30,3 +31,4 @@ DEVICE_EXTERN_C void __devicelib_assert_fail(const char *expr, const char *file, // volatile int *die = (int *)0x0; // *die = 0xdead; } +#endif // __SPIR__ diff --git a/libdevice/fallback-cmath-fp64.cpp b/libdevice/fallback-cmath-fp64.cpp index a0693d6b44c8a..c3fdad6ce756f 100644 --- a/libdevice/fallback-cmath-fp64.cpp +++ b/libdevice/fallback-cmath-fp64.cpp @@ -8,6 +8,8 @@ #include "device_math.h" +#ifdef __SPIR__ + DEVICE_EXTERN_C double __devicelib_log(double x) { return __spirv_ocl_log(x); } @@ -138,3 +140,4 @@ double __devicelib_asinh(double x) { return __spirv_ocl_asinh(x); } DEVICE_EXTERN_C double __devicelib_atanh(double x) { return __spirv_ocl_atanh(x); } +#endif // __SPIR__ diff --git a/libdevice/fallback-cmath.cpp b/libdevice/fallback-cmath.cpp index 61904143a72f2..50a98d01c363c 100644 --- a/libdevice/fallback-cmath.cpp +++ b/libdevice/fallback-cmath.cpp @@ -8,6 +8,8 @@ #include "device_math.h" +#ifdef __SPIR__ + DEVICE_EXTERN_C float __devicelib_scalbnf(float x, int n) { return __spirv_ocl_ldexp(x, n); } @@ -139,3 +141,5 @@ float __devicelib_asinhf(float x) { return __spirv_ocl_asinh(x); } DEVICE_EXTERN_C float __devicelib_atanhf(float x) { return __spirv_ocl_atanh(x); } + +#endif // __SPIR__ diff --git a/libdevice/fallback-complex-fp64.cpp b/libdevice/fallback-complex-fp64.cpp index 8a24566c8ed30..cc9d80f27180a 100644 --- a/libdevice/fallback-complex-fp64.cpp +++ b/libdevice/fallback-complex-fp64.cpp @@ -8,6 +8,8 @@ #include "device_complex.h" #include "device_math.h" + +#ifdef __SPIR__ #include DEVICE_EXTERN_C @@ -423,3 +425,4 @@ double __complex__ __devicelib_catan(double __complex__ z) { __devicelib_catanh(CMPLX(-__devicelib_cimag(z), __devicelib_creal(z))); return CMPLX(__devicelib_cimag(w), -__devicelib_creal(w)); } +#endif // __SPIR__ diff --git a/libdevice/fallback-complex.cpp b/libdevice/fallback-complex.cpp index b60dbfa8723ad..91094eab30f27 100644 --- a/libdevice/fallback-complex.cpp +++ b/libdevice/fallback-complex.cpp @@ -8,6 +8,8 @@ #include "device_complex.h" #include "device_math.h" + +#ifdef __SPIR__ #include DEVICE_EXTERN_C @@ -427,3 +429,4 @@ float __complex__ __devicelib_catanf(float __complex__ z) { CMPLXF(-__devicelib_cimagf(z), __devicelib_crealf(z))); return CMPLXF(__devicelib_cimagf(w), -__devicelib_crealf(w)); } +#endif // __SPIR__ diff --git a/libdevice/glibc_wrapper.cpp b/libdevice/glibc_wrapper.cpp index 1d0d1e4fa694d..6003c520cf0ff 100644 --- a/libdevice/glibc_wrapper.cpp +++ b/libdevice/glibc_wrapper.cpp @@ -6,9 +6,9 @@ // //===----------------------------------------------------------------------===// -#include "device.h" #include "wrapper.h" +#ifdef __SPIR__ DEVICE_EXTERN_C void __assert_fail(const char *expr, const char *file, unsigned int line, const char *func) { @@ -18,3 +18,4 @@ void __assert_fail(const char *expr, const char *file, unsigned int line, __spirv_LocalInvocationId_x(), __spirv_LocalInvocationId_y(), __spirv_LocalInvocationId_z()); } +#endif // __SPIR__ diff --git a/libdevice/msvc_wrapper.cpp b/libdevice/msvc_wrapper.cpp index 61f357d8899fd..d96bc973dffc4 100644 --- a/libdevice/msvc_wrapper.cpp +++ b/libdevice/msvc_wrapper.cpp @@ -6,9 +6,9 @@ // //===----------------------------------------------------------------------===// -#include "device.h" #include "wrapper.h" +#ifdef __SPIR__ // Truncates a wide (16 or 32 bit) string (wstr) into an ASCII string (str). // Any non-ASCII characters are replaced by question mark '?'. 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) { __spirv_LocalInvocationId_x(), __spirv_LocalInvocationId_y(), __spirv_LocalInvocationId_z()); } +#endif // __SPIR__ diff --git a/libdevice/spirv_vars.h b/libdevice/spirv_vars.h new file mode 100644 index 0000000000000..5a1a1d0d96705 --- /dev/null +++ b/libdevice/spirv_vars.h @@ -0,0 +1,44 @@ +//==---------- spirv_vars.h --- SPIRV variables --------------*- C++ -*-----==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef __LIBDEVICE_SPIRV_VARS_H +#define __LIBDEVICE_SPIRV_VARS_H + +#include "device.h" + +#ifdef __SPIR__ + +#include +#include + +typedef size_t size_t_vec __attribute__((ext_vector_type(3))); +extern "C" const size_t_vec __spirv_BuiltInGlobalInvocationId; +extern "C" const size_t_vec __spirv_BuiltInLocalInvocationId; + +DEVICE_EXTERNAL inline size_t __spirv_GlobalInvocationId_x() { + return __spirv_BuiltInGlobalInvocationId.x; +} +DEVICE_EXTERNAL inline size_t __spirv_GlobalInvocationId_y() { + return __spirv_BuiltInGlobalInvocationId.y; +} +DEVICE_EXTERNAL inline size_t __spirv_GlobalInvocationId_z() { + return __spirv_BuiltInGlobalInvocationId.z; +} + +DEVICE_EXTERNAL inline size_t __spirv_LocalInvocationId_x() { + return __spirv_BuiltInLocalInvocationId.x; +} +DEVICE_EXTERNAL inline size_t __spirv_LocalInvocationId_y() { + return __spirv_BuiltInLocalInvocationId.y; +} +DEVICE_EXTERNAL inline size_t __spirv_LocalInvocationId_z() { + return __spirv_BuiltInLocalInvocationId.z; +} + +#endif // __SPIR__ +#endif // __LIBDEVICE_SPIRV_VARS_H diff --git a/libdevice/spirv_vars.hpp b/libdevice/spirv_vars.hpp deleted file mode 100644 index ebc7a99842782..0000000000000 --- a/libdevice/spirv_vars.hpp +++ /dev/null @@ -1,100 +0,0 @@ -//==---------- spirv_vars.hpp --- SPIRV variables -------------------------==// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -// ===-------------------------------------------------------------------=== // - -#pragma once - -#include -#include - -#ifdef __SYCL_DEVICE_ONLY__ - -#ifdef __SYCL_NVPTX__ - -DEVICE_EXTERNAL size_t __spirv_GlobalInvocationId_x(); -DEVICE_EXTERNAL size_t __spirv_GlobalInvocationId_y(); -DEVICE_EXTERNAL size_t __spirv_GlobalInvocationId_z(); - -DEVICE_EXTERNAL size_t __spirv_LocalInvocationId_x(); -DEVICE_EXTERNAL size_t __spirv_LocalInvocationId_y(); -DEVICE_EXTERNAL size_t __spirv_LocalInvocationId_z(); - -#else // __SYCL_NVPTX__ - -typedef size_t size_t_vec __attribute__((ext_vector_type(3))); -extern "C" const size_t_vec __spirv_BuiltInGlobalInvocationId; -extern "C" const size_t_vec __spirv_BuiltInLocalInvocationId; - -DEVICE_EXTERNAL inline size_t __spirv_GlobalInvocationId_x() { - return __spirv_BuiltInGlobalInvocationId.x; -} -DEVICE_EXTERNAL inline size_t __spirv_GlobalInvocationId_y() { - return __spirv_BuiltInGlobalInvocationId.y; -} -DEVICE_EXTERNAL inline size_t __spirv_GlobalInvocationId_z() { - return __spirv_BuiltInGlobalInvocationId.z; -} - -DEVICE_EXTERNAL inline size_t __spirv_LocalInvocationId_x() { - return __spirv_BuiltInLocalInvocationId.x; -} -DEVICE_EXTERNAL inline size_t __spirv_LocalInvocationId_y() { - return __spirv_BuiltInLocalInvocationId.y; -} -DEVICE_EXTERNAL inline size_t __spirv_LocalInvocationId_z() { - return __spirv_BuiltInLocalInvocationId.z; -} - -#endif // __SYCL_NVPTX__ - -#define DEFINE_FUNC_ID_TO_XYZ_CONVERTER(POSTFIX) \ - template static inline size_t get##POSTFIX(); \ - template <> size_t get##POSTFIX<0>() { return __spirv_##POSTFIX##_x(); } \ - template <> size_t get##POSTFIX<1>() { return __spirv_##POSTFIX##_y(); } \ - template <> size_t get##POSTFIX<2>() { return __spirv_##POSTFIX##_z(); } - -namespace __spirv { - -DEFINE_FUNC_ID_TO_XYZ_CONVERTER(GlobalInvocationId); -DEFINE_FUNC_ID_TO_XYZ_CONVERTER(LocalInvocationId); - -} // namespace __spirv - -#undef DEFINE_FUNC_ID_TO_XYZ_CONVERTER - -#define DEFINE_INIT_SIZES(POSTFIX) \ - \ - template struct InitSizesST##POSTFIX; \ - \ - template struct InitSizesST##POSTFIX<1, DstT> { \ - static DstT initSize() { return {get##POSTFIX<0>()}; } \ - }; \ - \ - template struct InitSizesST##POSTFIX<2, DstT> { \ - static DstT initSize() { return {get##POSTFIX<1>(), get##POSTFIX<0>()}; } \ - }; \ - \ - template struct InitSizesST##POSTFIX<3, DstT> { \ - static DstT initSize() { \ - return {get##POSTFIX<2>(), get##POSTFIX<1>(), get##POSTFIX<0>()}; \ - } \ - }; \ - \ - template static DstT init##POSTFIX() { \ - return InitSizesST##POSTFIX::initSize(); \ - } - -namespace __spirv { - -DEFINE_INIT_SIZES(GlobalInvocationId) -DEFINE_INIT_SIZES(LocalInvocationId) - -} // namespace __spirv - -#undef DEFINE_INIT_SIZES - -#endif // __SYCL_DEVICE_ONLY__ diff --git a/libdevice/wrapper.h b/libdevice/wrapper.h index e91a47d5fd3c3..2116d1e55a226 100644 --- a/libdevice/wrapper.h +++ b/libdevice/wrapper.h @@ -10,24 +10,19 @@ #define __LIBDEVICE_WRAPPER_H__ #include "device.h" -#include -#include - -DEVICE_EXTERNAL size_t __spirv_GlobalInvocationId_x(); -DEVICE_EXTERNAL size_t __spirv_GlobalInvocationId_y(); -DEVICE_EXTERNAL size_t __spirv_GlobalInvocationId_z(); -DEVICE_EXTERNAL size_t __spirv_LocalInvocationId_x(); -DEVICE_EXTERNAL size_t __spirv_LocalInvocationId_y(); -DEVICE_EXTERNAL size_t __spirv_LocalInvocationId_z(); +#ifdef __SPIR__ -DEVICE_EXTERNAL int -__spirv_ocl_printf(const __attribute__((opencl_constant)) char *fmt, ...); +#include +#include +// We need the following header to ensure the definition of all spirv variables +// required by the wrapper libraries. +#include "spirv_vars.h" DEVICE_EXTERN_C void __devicelib_assert_fail(const char *expr, const char *file, int32_t line, const char *func, uint64_t gid0, uint64_t gid1, uint64_t gid2, uint64_t lid0, uint64_t lid1, uint64_t lid2); - +#endif // __SPIR__ #endif // __LIBDEVICE_WRAPPER_H__