Skip to content

[SYCL] Add C++ linkage specification in SYCL assert headers #15570

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 8 commits into from
Oct 3, 2024

Conversation

lbushi25
Copy link
Contributor

@lbushi25 lbushi25 commented Oct 1, 2024

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" {
      | ^

@lbushi25 lbushi25 marked this pull request as ready for review October 1, 2024 17:06
@lbushi25 lbushi25 requested a review from a team as a code owner October 1, 2024 17:06
Copy link
Contributor

@AlexeySachkov AlexeySachkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example from the commit message should be moved into sycl/test

@againull againull merged commit 208ec48 into intel:sycl Oct 3, 2024
12 checks passed
@@ -16,6 +16,7 @@
#include <../include/cassert>
#endif

extern "C++" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this? <cassert> is a C++ header, not C.

Copy link
Contributor Author

@lbushi25 lbushi25 Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something like

#ifdef __cplusplus
extern "C" {
#endif

#include <cassert>

#ifdef __cplusplus
}
#endif

int main() {}

is well-defined code and without the changes, it fails to compile.
Also note that the external linkage specification is after the actual C++ STL header <cassert> is included so we're not actually changing anything in the STL header contents.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tahonermann , @AaronBallman , is the above code really well-defined?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think including a C++ standard library header in a linkage specification block is well-formed.

What problem is the extern "C++" declaration intended to solve? If the problem is that code somewhere else is including the <cassert> header inside of an extern "C" declaration then that is the bug that should be fixed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that, per [support.c.headers.general]p1, there is no need to include C library headers within a extern "C" block when compiling as C++.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong, but I think libstdc++ itself might be doing something like that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since libstdc++ is an implementation of the C++ standard library, it is allowed to do things that user code is not. If it "knows" that the header it is including is a C header that doesn't itself place its contents in an extern "C" block, then it makes since for it to do so as part of wrapping that header (e.g., inside the <cassert> header).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that, per [support.c.headers.general]p1, there is no need to include C library headers within a extern "C" block when compiling as C++.

"there is no need" doesn't mean "it's prohibited", right?

It also isn't right to include C headers in an extern "C" block when compiling as C++.
Can the clients that are including C headers be fixed to not do so within a extern "C" block?

Do you have a link to the C++ draft section which says this? I initially wanted to push back on the bug report to make customer rewrite their code, but I haven't been able to find a C++ spec section which would pronounce this ill-formed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of wording in the C++ standard that explicitly makes inclusion of a C header within an extern "C" block ill-formed. My interpretation of such inclusion being ill-formed comes from the absence of constraints that would prohibit use of C++ features in the C++ standard library implementation of the C headers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an example where C++ features are used in the implementation of C headers provided by a C++ standard library implementation, see the source code for libstdc++. It provides a set of C compatibility headers that are wrappers around the C++ equivalents. Specifically, see its implementation of the stdlib.h header; it unconditionally uses C++ using declarations. Whether these compatibility headers are used is a decision made when gcc is configured and built and depends on the availability of a C library implementation.

sarnex pushed a commit that referenced this pull request Oct 8, 2024
This [PR](#15570) added C++ linkage
specifications for the SYCL wrappers around the STL assert headers
`assert.h` and `cassert`.
For `cassert`, this is actually superfluous since it is purely a C++
header. This PR removes it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants