Skip to content

Commit

Permalink
[SYCL] Disable clang pedantic diagnostics in header/footer (#15171)
Browse files Browse the repository at this point in the history
When -Wreserved-identifer or -Wold-style-cast is enabled, some of the
constructs used in
integration headers and footers for device_globals and host_pipes
trigger a diagnostic.
This change adds a pragma to the generated files that suppress the
diagnostic prior to
the construct and reenabled the diagnostic after.
  • Loading branch information
premanandrao authored Aug 23, 2024
1 parent e8fcff1 commit 4140240
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
33 changes: 33 additions & 0 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6029,6 +6029,23 @@ static void OutputStableNameInChars(raw_ostream &O, StringRef Name) {
}
}

static void EmitPragmaDiagnosticPush(raw_ostream &O, StringRef DiagName) {
O << "\n";
O << "#ifdef __clang__\n";
O << "#pragma clang diagnostic push\n";
O << "#pragma clang diagnostic ignored \"" << DiagName.str() << "\"\n";
O << "#endif // defined(__clang__)\n";
O << "\n";
}

static void EmitPragmaDiagnosticPop(raw_ostream &O) {
O << "\n";
O << "#ifdef __clang__\n";
O << "#pragma clang diagnostic pop\n";
O << "#endif // defined(__clang__)\n";
O << "\n";
}

void SYCLIntegrationHeader::emit(raw_ostream &O) {
O << "// This is auto-generated SYCL integration header.\n";
O << "\n";
Expand Down Expand Up @@ -6127,6 +6144,9 @@ void SYCLIntegrationHeader::emit(raw_ostream &O) {
// main() function.

if (NeedToEmitDeviceGlobalRegistration) {
// Supress the reserved identifier diagnostic that clang generates
// for the construct below.
EmitPragmaDiagnosticPush(O, "-Wreserved-identifier");
O << "namespace {\n";

O << "class __sycl_device_global_registration {\n";
Expand All @@ -6138,12 +6158,16 @@ void SYCLIntegrationHeader::emit(raw_ostream &O) {
O << "} // namespace\n";

O << "\n";
EmitPragmaDiagnosticPop(O);
}

// Generate declaration of variable of type __sycl_host_pipe_registration
// whose sole purpose is to run its constructor before the application's
// main() function.
if (NeedToEmitHostPipeRegistration) {
// Supress the reserved identifier diagnostic that clang generates
// for the construct below.
EmitPragmaDiagnosticPush(O, "-Wreserved-identifier");
O << "namespace {\n";

O << "class __sycl_host_pipe_registration {\n";
Expand All @@ -6155,6 +6179,7 @@ void SYCLIntegrationHeader::emit(raw_ostream &O) {
O << "} // namespace\n";

O << "\n";
EmitPragmaDiagnosticPop(O);
}


Expand Down Expand Up @@ -6723,12 +6748,16 @@ bool SYCLIntegrationFooter::emit(raw_ostream &OS) {
OS << "#include <sycl/detail/device_global_map.hpp>\n";
DeviceGlobOS.flush();
OS << "namespace sycl::detail {\n";
// Supress the old-style case diagnostic that clang generates
// for the construct below in DeviceGlobalsBuf.
EmitPragmaDiagnosticPush(OS, "-Wold-style-cast");
OS << "namespace {\n";
OS << "__sycl_device_global_registration::__sycl_device_global_"
"registration() noexcept {\n";
OS << DeviceGlobalsBuf;
OS << "}\n";
OS << "} // namespace (unnamed)\n";
EmitPragmaDiagnosticPop(OS);
OS << "} // namespace sycl::detail\n";

S.getSyclIntegrationHeader().addDeviceGlobalRegistration();
Expand All @@ -6738,12 +6767,16 @@ bool SYCLIntegrationFooter::emit(raw_ostream &OS) {
OS << "#include <sycl/detail/host_pipe_map.hpp>\n";
HostPipesOS.flush();
OS << "namespace sycl::detail {\n";
// Supress the old-style case diagnostic that clang generates
// for the construct below in HostPipesBuf.
EmitPragmaDiagnosticPush(OS, "-Wold-style-cast");
OS << "namespace {\n";
OS << "__sycl_host_pipe_registration::__sycl_host_pipe_"
"registration() noexcept {\n";
OS << HostPipesBuf;
OS << "}\n";
OS << "} // namespace (unnamed)\n";
EmitPragmaDiagnosticPop(OS);
OS << "} // namespace sycl::detail\n";

S.getSyclIntegrationHeader().addHostPipeRegistration();
Expand Down
18 changes: 16 additions & 2 deletions clang/test/CodeGenSYCL/device_global_int_footer_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ int main() {
// CHECK-HEADER: namespace sycl {
// CHECK-HEADER-NEXT: inline namespace _V1 {
// CHECK-HEADER-NEXT: namespace detail {
// CHECK-HEADER-NEXT: namespace {
// CHECK-HEADER: #ifdef __clang__
// CHECK-HEADER-NEXT: #pragma clang diagnostic push
// CHECK-HEADER-NEXT: #pragma clang diagnostic ignored "-Wreserved-identifier"
// CHECK-HEADER-NEXT: #endif // defined(__clang__)
// CHECK-HEADER: namespace {
// CHECK-HEADER-NEXT: class __sycl_device_global_registration {
// CHECK-HEADER-NEXT: public:
// CHECK-HEADER-NEXT: __sycl_device_global_registration() noexcept;
// CHECK-HEADER-NEXT: };
// CHECK-HEADER-NEXT: __sycl_device_global_registration __sycl_device_global_registrar;
// CHECK-HEADER-NEXT: } // namespace
// CHECK-HEADER: #ifdef __clang__
// CHECK-HEADER-NEXT: #pragma clang diagnostic pop
// CHECK-HEADER-NEXT: #endif // defined(__clang__)
// CHECK-HEADER: } // namespace detail
// CHECK-HEADER: } // namespace _V1
// CHECK-HEADER: } // namespace sycl
Expand All @@ -49,7 +56,11 @@ int main() {

// CHECK-FOOTER: #include <sycl/detail/device_global_map.hpp>
// CHECK-FOOTER: namespace sycl::detail {
// CHECK-FOOTER-NEXT: namespace {
// CHECK-FOOTER: #ifdef __clang__
// CHECK-FOOTER-NEXT: #pragma clang diagnostic push
// CHECK-FOOTER-NEXT: #pragma clang diagnostic ignored "-Wold-style-cast"
// CHECK-FOOTER-NEXT: #endif // defined(__clang__)
// CHECK-FOOTER: namespace {
// CHECK-FOOTER-NEXT: __sycl_device_global_registration::__sycl_device_global_registration() noexcept {

extern device_global<int> Basic;
Expand Down Expand Up @@ -111,3 +122,6 @@ struct HasVarTemplate {
} // namespace
const auto x = HasVarTemplate::VarTempl<int>.get();
// CHECK-FOOTER-NEXT: device_global_map::add((void *)&::__sycl_detail::__shim_[[SHIM1]](), "THE_PREFIX____ZN12_GLOBAL__N_114HasVarTemplate8VarTemplIiEE");
// CHECK-FOOTER: #ifdef __clang__
// CHECK-FOOTER-NEXT: #pragma clang diagnostic pop
// CHECK-FOOTER-NEXT: #endif // defined(__clang__)
4 changes: 2 additions & 2 deletions clang/test/CodeGenSYCL/device_globals_with_spec_ids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ struct Wrapper {
// CHECK: #include <sycl/detail/spec_const_integration.hpp>
// CHECK-NEXT: #include <sycl/detail/device_global_map.hpp>
// CHECK-NEXT: namespace sycl::detail {
// CHECK-NEXT: namespace {
// CHECK: namespace {
// CHECK-NEXT: __sycl_device_global_registration::__sycl_device_global_registration() noexcept {
// CHECK-NEXT: device_global_map::add((void *)&::b, "_Z1b");
// CHECK-NEXT: device_global_map::add((void *)&::Wrapper::b, "_ZN7Wrapper1bE");
Expand All @@ -260,4 +260,4 @@ struct Wrapper {
// CHECK-NEXT: device_global_map::add((void *)&::outer::__sycl_detail::__shim_[[SHIM11]](), "____ZN5outer12_GLOBAL__N_15inner12_GLOBAL__N_17Wrapper1cE");
// CHECK-NEXT: }
// CHECK-NEXT: } // namespace (unnamed)
// CHECK-NEXT: } // namespace sycl::detail
// CHECK: } // namespace sycl::detail
4 changes: 2 additions & 2 deletions clang/test/CodeGenSYCL/host_pipe_int_footer_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void foo() {
// CHECK-HEADER: namespace sycl {
// CHECK-HEADER-NEXT: inline namespace _V1 {
// CHECK-HEADER-NEXT: namespace detail {
// CHECK-HEADER-NEXT: namespace {
// CHECK-HEADER: namespace {
// CHECK-HEADER-NEXT: class __sycl_host_pipe_registration {
// CHECK-HEADER-NEXT: public:
// CHECK-HEADER-NEXT: __sycl_host_pipe_registration() noexcept;
Expand All @@ -37,7 +37,7 @@ void foo() {
// CHECK-FOOTER: #include <sycl/detail/defines_elementary.hpp>
// CHECK-FOOTER: #include <sycl/detail/host_pipe_map.hpp>
// CHECK-FOOTER-NEXT: namespace sycl::detail {
// CHECK-FOOTER-NEXT: namespace {
// CHECK-FOOTER: namespace {
// CHECK-FOOTER-NEXT: __sycl_host_pipe_registration::__sycl_host_pipe_registration() noexcept {

// CHECK-FOOTER: host_pipe_map::add((void *)&::sycl::ext::intel::experimental::host_pipe<HPInt, int>::__pipe, "THE_PREFIX____ZN4sycl3_V13ext5intel12experimental9host_pipeIZZZ3foovENKUlRNS0_7handlerEE_clES6_ENKUlvE_clEvE5HPIntiE6__pipeE");
Expand Down
6 changes: 3 additions & 3 deletions clang/test/CodeGenSYCL/int_header_without_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using namespace sycl::ext::oneapi;
// CHECK-HEADER: namespace sycl {
// CHECK-HEADER-NEXT: inline namespace _V1 {
// CHECK-HEADER-NEXT: namespace detail {
// CHECK-HEADER-NEXT: namespace {
// CHECK-HEADER: namespace {
// CHECK-HEADER-NEXT: class __sycl_device_global_registration {
// CHECK-HEADER-NEXT: public:
// CHECK-HEADER-NEXT: __sycl_device_global_registration() noexcept;
Expand All @@ -27,12 +27,12 @@ using namespace sycl::ext::oneapi;

// CHECK-FOOTER: #include <sycl/detail/device_global_map.hpp>
// CHECK-FOOTER: namespace sycl::detail {
// CHECK-FOOTER-NEXT: namespace {
// CHECK-FOOTER: namespace {
// CHECK-FOOTER-NEXT: __sycl_device_global_registration::__sycl_device_global_registration() noexcept {

device_global<int> Basic;
// CHECK-FOOTER-NEXT: device_global_map::add((void *)&::Basic, "_Z5Basic");

// CHECK-FOOTER-NEXT: }
// CHECK-FOOTER-NEXT: }
// CHECK-FOOTER-NEXT: }
// CHECK-FOOTER: }
8 changes: 8 additions & 0 deletions sycl/test/basic_tests/header_footer_diagnostics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %clangxx -fsycl -Werror=reserved-identifier -Werror=old-style-cast %s

// Check that the generated header and footer files do not generate
// errors when pedantic warnings are enabled.

#include <sycl/sycl.hpp>
sycl::ext::oneapi::experimental::device_global<int> devGlobVar;
int main(int argc, char **argv) { return 0; }

0 comments on commit 4140240

Please sign in to comment.