Skip to content

Commit 208ec48

Browse files
authored
[SYCL] Add C++ linkage specification in SYCL assert headers (#15570)
Some SYCL applications are seeing compilation failures related to the SYCL assert headers. Specifically, they are wrapping the SYCL assert headers in C linkage specifications in their applications. These headers in turn include files that contain templates which emit errors under C linkage specifications so this PR explicitly adds C++ linkage specifications in the header contents to override any extra specification that the clients may add. Here is an example of faulty client code: ``` #ifdef __cplusplus extern "C" { #endif #include <assert.h> #ifdef __cplusplus } #endif int main() {} ``` Error fragments: ``` /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/CL/__spirv/spirv_types.hpp:140:1: error: templates must have C++ linkage 140 | template <typename T, std::size_t R, std::size_t C, MatrixLayout L, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 141 | Scope::Flag S = Scope::Flag::Subgroup, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 142 | MatrixUse U = MatrixUse::MatrixA> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../test.cpp:2:1: note: extern "C" language linkage specification begins here 2 | extern "C" { | ^ In file included from ../test.cpp:5: In file included from /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/sycl/stl_wrappers/assert.h:21: In file included from /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/CL/__spirv/spirv_vars.hpp:13: /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/CL/__spirv/spirv_types.hpp:156:1: error: templates must have C++ linkage 156 | template <typename dataT> | ^~~~~~~~~~~~~~~~~~~~~~~~~ ../test.cpp:2:1: note: extern "C" language linkage specification begins here 2 | extern "C" { | ^ In file included from ../test.cpp:5: In file included from /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/sycl/stl_wrappers/assert.h:21: In file included from /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/CL/__spirv/spirv_vars.hpp:13: /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/CL/__spirv/spirv_types.hpp:158:1: error: templates must have C++ linkage 158 | template <typename dataT> | ^~~~~~~~~~~~~~~~~~~~~~~~~ ../test.cpp:2:1: note: extern "C" language linkage specification begins here 2 | extern "C" { | ^ In file included from ../test.cpp:5: In file included from /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/sycl/stl_wrappers/assert.h:21: In file included from /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/CL/__spirv/spirv_vars.hpp:13: /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/CL/__spirv/spirv_types.hpp:162:1: error: templates must have C++ linkage 162 | template <typename dataT, int dims> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../test.cpp:2:1: note: extern "C" language linkage specification begins here 2 | extern "C" { | ^ In file included from ../test.cpp:5: In file included from /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/sycl/stl_wrappers/assert.h:21: In file included from /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/CL/__spirv/spirv_vars.hpp:13: /nfs/site/home/lbushi/sycl_workspace/llvm/build/bin/../include/CL/__spirv/spirv_types.hpp:177:1: error: templates must have C++ linkage 177 | template <int Bits> using ap_int = _BitInt(Bits); | ^~~~~~~~~~~~~~~~~~~ ../test.cpp:2:1: note: extern "C" language linkage specification begins here 2 | extern "C" { | ^ ```
1 parent d2359fd commit 208ec48

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

sycl/include/sycl/stl_wrappers/assert.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
// Must not be guarded. C++ standard says the macro assert is redefined
10-
// according to the current state of NDEBUG each time that <cassert> is
10+
// according to the current state of NDEBUG each time that <assert.h> is
1111
// included.
1212

1313
#if defined(__has_include_next)
@@ -16,6 +16,7 @@
1616
#include <../ucrt/assert.h>
1717
#endif
1818

19+
extern "C++" {
1920
#ifdef __SYCL_DEVICE_ONLY__
2021
#include <CL/__spirv/spirv_vars.hpp>
2122

@@ -42,3 +43,4 @@ __devicelib_assert_fail(const char *, const char *, int32_t, const char *,
4243
#endif
4344
#endif
4445
#endif
46+
}

sycl/include/sycl/stl_wrappers/cassert

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <../include/cassert>
1717
#endif
1818

19+
extern "C++" {
1920
#ifdef __SYCL_DEVICE_ONLY__
2021
#include <CL/__spirv/spirv_vars.hpp>
2122

@@ -42,3 +43,4 @@ __devicelib_assert_fail(const char *, const char *, int32_t, const char *,
4243
#endif
4344
#endif
4445
#endif
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clangxx -fsycl -DASSERT -fsyntax-only %s
2+
// RUN: %clangxx -fsycl -DCASSERT -fsyntax-only %s
3+
4+
// Verify that compilation works when assert.h/cassert is wrapped by a C linkage
5+
// specification.
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
#if defined(ASSERT)
12+
#include <assert.h>
13+
#elif defined(CASSERT)
14+
#include <cassert>
15+
#endif
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif

0 commit comments

Comments
 (0)