-
Notifications
You must be signed in to change notification settings - Fork 769
[SYCL] Move devicelib assert wrapper to header files #1398
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1218,9 +1218,14 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, | |
getToolChain().AddCudaIncludeArgs(Args, CmdArgs); | ||
|
||
if (JA.isOffloading(Action::OFK_SYCL) || | ||
Args.hasArg(options::OPT_fsycl_device_only)) | ||
Args.hasArg(options::OPT_fsycl_device_only)) { | ||
toolchains::SYCLToolChain::AddSYCLIncludeArgs(D, Args, CmdArgs); | ||
|
||
if (getToolChain().getTriple().isSPIR()) { | ||
toolchains::SYCLToolChain::AddDevicelibIncludeArgs(CmdArgs); | ||
} | ||
Comment on lines
+1224
to
+1226
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A minor suggestion would be to make this logic part of |
||
} | ||
|
||
// If we are offloading to a target via OpenMP we need to include the | ||
// openmp_wrappers folder which contains alternative system headers. | ||
if (JA.isDeviceOffloading(Action::OFK_OpenMP) && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -543,6 +543,12 @@ void SYCLToolChain::AddSYCLIncludeArgs(const clang::driver::Driver &Driver, | |
CC1Args.push_back(DriverArgs.MakeArgString(P)); | ||
} | ||
|
||
void SYCLToolChain::AddDevicelibIncludeArgs(ArgStringList &CC1Args) { | ||
// Assume that clang system include path is added elsewhere | ||
CC1Args.push_back("-include"); | ||
CC1Args.push_back("devicelib/__devicelib_assert.h"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be included somewhere in SYCL headers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point is to support compilation when SYCL headers are not included, e.g. when SYCL_EXTERNAL is used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Compilation of what? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Compilation of source code that do not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The last patch includes the header when SYCL language is compiled for SPIR target. Non-SPIR targets should not be affected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Andrew, can we make this include agnostic to compilation mode/language and include it for all compilations for SPIR target? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it makes sense - sure. I'm concerned that this feature will be enabled for languages where it doesn't make sense (for OpenCL, in particular). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you think it doesn't make sense for OpenCL? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is ultimately for OpenCL folks to decide. Right now this extension is not documented to work for OpenCL. |
||
} | ||
|
||
void SYCLToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, | ||
ArgStringList &CC1Args) const { | ||
HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//==------------ devicelib.h - macros for devicelib wrappers ---------------==// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This path (i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what do you mean by a "conflict". When installed on a system, this directory resides in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, then. |
||
// | ||
// 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 __DEVICELIB_H__ | ||
#define __DEVICELIB_H__ | ||
|
||
#ifndef _WIN32 | ||
#include <features.h> // for GLIBC macro | ||
#endif | ||
|
||
// Devicelib wraps a system C or C++ library | ||
#ifdef __GLIBC__ | ||
#define __DEVICELIB_GLIBC__ 1 | ||
#elif defined(_WIN32) | ||
#define __DEVICELIB_MSLIBC__ 1 | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
#define EXTERN_C extern "C" | ||
#else // __cplusplus | ||
#define EXTERN_C | ||
#endif // __cplusplus | ||
|
||
#ifdef CL_SYCL_LANGUAGE_VERSION | ||
#ifndef SYCL_EXTERNAL | ||
#define SYCL_EXTERNAL | ||
#endif // SYCL_EXTERNAL | ||
|
||
#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 | ||
#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 | ||
|
||
#ifdef __SYCL_DEVICE_ONLY__ | ||
#define __DEVICELIB_DEVICE_ONLY__ 1 | ||
#endif | ||
|
||
#endif // __DEVICELIB_H__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//==--------- devicelib-assert.h - wrapper definition for C assert ---------==// | ||
// | ||
// 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 __DEVICELIB_ASSERT_H__ | ||
#define __DEVICELIB_ASSERT_H__ | ||
|
||
#include "__devicelib.h" | ||
|
||
#ifdef __DEVICELIB_DEVICE_ONLY__ | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
#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); | ||
|
||
#ifdef __DEVICELIB_GLIBC__ | ||
EXTERN_C inline void | ||
__assert_fail(const char *expr, const char *file, | ||
unsigned int line, const char *func) __THROW __attribute__ ((__noreturn__)) { | ||
__devicelib_assert_fail( | ||
expr, file, line, func, __spirv_GlobalInvocationId_x(), | ||
__spirv_GlobalInvocationId_y(), __spirv_GlobalInvocationId_z(), | ||
__spirv_LocalInvocationId_x(), __spirv_LocalInvocationId_y(), | ||
__spirv_LocalInvocationId_z()); | ||
} | ||
#elif defined(__DEVICELIB_MSLIBC__) | ||
// Truncates a wide (16 or 32 bit) string (wstr) into an ASCII string (str). | ||
// Any non-ASCII characters are replaced by question mark '?'. | ||
static inline void __truncate_wchar_char_str(const wchar_t *wstr, char *str, | ||
size_t str_size) { | ||
str_size -= 1; // reserve for NULL terminator | ||
while (str_size > 0 && *wstr != L'\0') { | ||
wchar_t w = *wstr++; | ||
*str++ = (w > 0 && w < 127) ? (char)w : '?'; | ||
str_size--; | ||
} | ||
*str = '\0'; | ||
} | ||
|
||
EXTERN_C inline void _wassert(const wchar_t *wexpr, const wchar_t *wfile, | ||
unsigned line) { | ||
// Paths and expressions that are longer than 256 characters are going to be | ||
// truncated. | ||
char file[256]; | ||
__truncate_wchar_char_str(wfile, file, sizeof(file)); | ||
char expr[256]; | ||
__truncate_wchar_char_str(wexpr, expr, sizeof(expr)); | ||
|
||
__devicelib_assert_fail( | ||
expr, file, line, /*func=*/nullptr, __spirv_GlobalInvocationId_x(), | ||
__spirv_GlobalInvocationId_y(), __spirv_GlobalInvocationId_z(), | ||
__spirv_LocalInvocationId_x(), __spirv_LocalInvocationId_y(), | ||
__spirv_LocalInvocationId_z()); | ||
} | ||
|
||
#endif | ||
|
||
#endif // __DEVICELIB_DEVICE_ONLY__ | ||
#endif // __DEVICELIB_ASSERT_H__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
//==---------- 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 | ||
// | ||
// ===-------------------------------------------------------------------=== // | ||
|
||
#ifndef __SPIRV_VARS_H__ | ||
#define __SPIRV_VARS_H__ | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
#ifdef __cplusplus | ||
#define __EXTERN_C extern "C" | ||
#else | ||
#define __EXTERN_C | ||
#endif | ||
|
||
#ifdef __SYCL_DEVICE_ONLY__ | ||
|
||
#ifdef __SYCL_NVPTX__ | ||
|
||
SYCL_EXTERNAL size_t __spirv_GlobalInvocationId_x(); | ||
SYCL_EXTERNAL size_t __spirv_GlobalInvocationId_y(); | ||
SYCL_EXTERNAL size_t __spirv_GlobalInvocationId_z(); | ||
|
||
SYCL_EXTERNAL size_t __spirv_GlobalSize_x(); | ||
SYCL_EXTERNAL size_t __spirv_GlobalSize_y(); | ||
SYCL_EXTERNAL size_t __spirv_GlobalSize_z(); | ||
|
||
SYCL_EXTERNAL size_t __spirv_GlobalOffset_x(); | ||
SYCL_EXTERNAL size_t __spirv_GlobalOffset_y(); | ||
SYCL_EXTERNAL size_t __spirv_GlobalOffset_z(); | ||
|
||
SYCL_EXTERNAL size_t __spirv_NumWorkgroups_x(); | ||
SYCL_EXTERNAL size_t __spirv_NumWorkgroups_y(); | ||
SYCL_EXTERNAL size_t __spirv_NumWorkgroups_z(); | ||
|
||
SYCL_EXTERNAL size_t __spirv_WorkgroupSize_x(); | ||
SYCL_EXTERNAL size_t __spirv_WorkgroupSize_y(); | ||
SYCL_EXTERNAL size_t __spirv_WorkgroupSize_z(); | ||
|
||
SYCL_EXTERNAL size_t __spirv_WorkgroupId_x(); | ||
SYCL_EXTERNAL size_t __spirv_WorkgroupId_y(); | ||
SYCL_EXTERNAL size_t __spirv_WorkgroupId_z(); | ||
|
||
SYCL_EXTERNAL size_t __spirv_LocalInvocationId_x(); | ||
SYCL_EXTERNAL size_t __spirv_LocalInvocationId_y(); | ||
SYCL_EXTERNAL size_t __spirv_LocalInvocationId_z(); | ||
|
||
#else // __SYCL_NVPTX__ | ||
|
||
typedef size_t size_t_vec __attribute__((ext_vector_type(3))); | ||
__EXTERN_C const __attribute__((opencl_constant)) | ||
size_t_vec __spirv_BuiltInGlobalInvocationId; | ||
__EXTERN_C const __attribute__((opencl_constant)) | ||
size_t_vec __spirv_BuiltInGlobalSize; | ||
__EXTERN_C const __attribute__((opencl_constant)) | ||
size_t_vec __spirv_BuiltInGlobalOffset; | ||
__EXTERN_C const __attribute__((opencl_constant)) | ||
size_t_vec __spirv_BuiltInNumWorkgroups; | ||
__EXTERN_C const __attribute__((opencl_constant)) | ||
size_t_vec __spirv_BuiltInWorkgroupSize; | ||
__EXTERN_C const __attribute__((opencl_constant)) | ||
size_t_vec __spirv_BuiltInWorkgroupId; | ||
__EXTERN_C const __attribute__((opencl_constant)) | ||
size_t_vec __spirv_BuiltInLocalInvocationId; | ||
|
||
SYCL_EXTERNAL inline size_t __spirv_GlobalInvocationId_x() { | ||
return __spirv_BuiltInGlobalInvocationId.x; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_GlobalInvocationId_y() { | ||
return __spirv_BuiltInGlobalInvocationId.y; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_GlobalInvocationId_z() { | ||
return __spirv_BuiltInGlobalInvocationId.z; | ||
} | ||
|
||
SYCL_EXTERNAL inline size_t __spirv_GlobalSize_x() { | ||
return __spirv_BuiltInGlobalSize.x; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_GlobalSize_y() { | ||
return __spirv_BuiltInGlobalSize.y; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_GlobalSize_z() { | ||
return __spirv_BuiltInGlobalSize.z; | ||
} | ||
|
||
SYCL_EXTERNAL inline size_t __spirv_GlobalOffset_x() { | ||
return __spirv_BuiltInGlobalOffset.x; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_GlobalOffset_y() { | ||
return __spirv_BuiltInGlobalOffset.y; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_GlobalOffset_z() { | ||
return __spirv_BuiltInGlobalOffset.z; | ||
} | ||
|
||
SYCL_EXTERNAL inline size_t __spirv_NumWorkgroups_x() { | ||
return __spirv_BuiltInNumWorkgroups.x; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_NumWorkgroups_y() { | ||
return __spirv_BuiltInNumWorkgroups.y; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_NumWorkgroups_z() { | ||
return __spirv_BuiltInNumWorkgroups.z; | ||
} | ||
|
||
SYCL_EXTERNAL inline size_t __spirv_WorkgroupSize_x() { | ||
return __spirv_BuiltInWorkgroupSize.x; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_WorkgroupSize_y() { | ||
return __spirv_BuiltInWorkgroupSize.y; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_WorkgroupSize_z() { | ||
return __spirv_BuiltInWorkgroupSize.z; | ||
} | ||
|
||
SYCL_EXTERNAL inline size_t __spirv_WorkgroupId_x() { | ||
return __spirv_BuiltInWorkgroupId.x; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_WorkgroupId_y() { | ||
return __spirv_BuiltInWorkgroupId.y; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_WorkgroupId_z() { | ||
return __spirv_BuiltInWorkgroupId.z; | ||
} | ||
|
||
SYCL_EXTERNAL inline size_t __spirv_LocalInvocationId_x() { | ||
return __spirv_BuiltInLocalInvocationId.x; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_LocalInvocationId_y() { | ||
return __spirv_BuiltInLocalInvocationId.y; | ||
} | ||
SYCL_EXTERNAL inline size_t __spirv_LocalInvocationId_z() { | ||
return __spirv_BuiltInLocalInvocationId.z; | ||
} | ||
|
||
#endif // __SYCL_NVPTX__ | ||
|
||
#endif // __SYCL_DEVICE_ONLY__ | ||
|
||
#undef __EXTERN_C | ||
|
||
#endif // __SPIRV_VARS_H__ |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,9 @@ | |||||
// RUN: | FileCheck -check-prefix=CHECK-SYCL-DEV %s | ||||||
// CHECK-SYCL-DEV: "-fsycl-is-device"{{.*}} "-internal-isystem" "{{.*}}bin{{[/\\]+}}..{{[/\\]+}}include{{[/\\]+}}sycl" | ||||||
|
||||||
/// Check that devicelib include path is added | ||||||
// CHECK-SYCL-DEV: "-include" "devicelib{{[/\\]}}__devicelib_assert.h" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this have separate |
||||||
/// Check that "-Wno-sycl-strict" is set on compiler invocation with "-fsycl" | ||||||
/// or "-fsycl-device-only" or both: | ||||||
// RUN: %clang -### -fsycl %s 2>&1 \ | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure, would:
be unacceptable because DPC++ NVPTX compilations would also pass the condition?