Skip to content

Commit

Permalink
[SYCL] Add C++ linkage specification in SYCL assert headers (#15570)
Browse files Browse the repository at this point in the history
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" {
      | ^
```
  • Loading branch information
lbushi25 authored Oct 3, 2024
1 parent d2359fd commit 208ec48
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sycl/include/sycl/stl_wrappers/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

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

#if defined(__has_include_next)
Expand All @@ -16,6 +16,7 @@
#include <../ucrt/assert.h>
#endif

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

Expand All @@ -42,3 +43,4 @@ __devicelib_assert_fail(const char *, const char *, int32_t, const char *,
#endif
#endif
#endif
}
2 changes: 2 additions & 0 deletions sycl/include/sycl/stl_wrappers/cassert
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <../include/cassert>
#endif

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

Expand All @@ -42,3 +43,4 @@ __devicelib_assert_fail(const char *, const char *, int32_t, const char *,
#endif
#endif
#endif
}
19 changes: 19 additions & 0 deletions sycl/test/basic_tests/assert_header_with_c_linkage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clangxx -fsycl -DASSERT -fsyntax-only %s
// RUN: %clangxx -fsycl -DCASSERT -fsyntax-only %s

// Verify that compilation works when assert.h/cassert is wrapped by a C linkage
// specification.

#ifdef __cplusplus
extern "C" {
#endif

#if defined(ASSERT)
#include <assert.h>
#elif defined(CASSERT)
#include <cassert>
#endif

#ifdef __cplusplus
}
#endif

0 comments on commit 208ec48

Please sign in to comment.