Skip to content

Guard libdevice code with __SPIR__. #1672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/SPIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 11 additions & 1 deletion clang/test/Preprocessor/predefined-macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions libdevice/cmake/modules/SYCLLibdevice.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions libdevice/cmath_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }

Expand Down Expand Up @@ -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__
6 changes: 4 additions & 2 deletions libdevice/cmath_wrapper_fp64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__
4 changes: 3 additions & 1 deletion libdevice/complex_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }

Expand Down Expand Up @@ -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__
4 changes: 3 additions & 1 deletion libdevice/complex_wrapper_fp64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }

Expand Down Expand Up @@ -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__
18 changes: 3 additions & 15 deletions libdevice/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
3 changes: 3 additions & 0 deletions libdevice/device_complex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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_
3 changes: 3 additions & 0 deletions libdevice/device_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "device.h"

#ifdef __SPIR__

DEVICE_EXTERN_C
double __devicelib_log(double x);

Expand Down Expand Up @@ -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__
2 changes: 2 additions & 0 deletions libdevice/fallback-cassert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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__
3 changes: 3 additions & 0 deletions libdevice/fallback-cmath-fp64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "device_math.h"

#ifdef __SPIR__

DEVICE_EXTERN_C
double __devicelib_log(double x) { return __spirv_ocl_log(x); }

Expand Down Expand Up @@ -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__
4 changes: 4 additions & 0 deletions libdevice/fallback-cmath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }

Expand Down Expand Up @@ -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__
3 changes: 3 additions & 0 deletions libdevice/fallback-complex-fp64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "device_complex.h"
#include "device_math.h"

#ifdef __SPIR__
#include <cmath>

DEVICE_EXTERN_C
Expand Down Expand Up @@ -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__
3 changes: 3 additions & 0 deletions libdevice/fallback-complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "device_complex.h"
#include "device_math.h"

#ifdef __SPIR__
#include <cmath>

DEVICE_EXTERN_C
Expand Down Expand Up @@ -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__
3 changes: 2 additions & 1 deletion libdevice/glibc_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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__
3 changes: 2 additions & 1 deletion libdevice/msvc_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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__
44 changes: 44 additions & 0 deletions libdevice/spirv_vars.h
Original file line number Diff line number Diff line change
@@ -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 <cstddef>
#include <cstdint>

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
Loading