Skip to content

Commit 79d1e54

Browse files
[SYCL][NFC] Throw warning when sycl.hpp is included without -fsycl flag. (#19279)
To improve user experience. Fixes #12052
1 parent 4080129 commit 79d1e54

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

sycl/doc/PreprocessorMacros.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ This file describes macros that have effect on SYCL compiler and run-time.
3434
Disables warning diagnostic issued when calling `device::has(aspect::image)`
3535
and `platform::has(aspect::image)`.
3636

37+
- **SYCL_DISABLE_FSYCL_SYCLHPP_WARNING**
38+
39+
Disable warning diagnostic issued when including `<sycl/sycl.hpp>` without
40+
`-fsycl` compiler flag.
41+
3742
- **SYCL_FALLBACK_ASSERT**
3843

3944
Defining as non-zero enables the fallback assert feature even on devices

sycl/include/sycl/sycl.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@
88

99
#pragma once
1010

11+
// Throw warning when including sycl.hpp without using -fsycl flag.
12+
// Warning can be disabled by defining SYCL_DISABLE_FSYCL_SYCLHPP_WARNING macro.
13+
#define STRINGIFY(x) #x
14+
#define TOSTRING(x) STRINGIFY(x)
15+
16+
#ifdef _MSC_VER
17+
#define WARNING(msg) \
18+
__pragma(message(__FILE__ "(" TOSTRING(__LINE__) "): warning: " msg))
19+
#elif defined(__GNUC__) || defined(__clang__)
20+
#define WARNING(msg) _Pragma(TOSTRING(GCC warning msg))
21+
#else
22+
#define WARNING(msg) // Unsupported compiler
23+
#endif
24+
25+
#if !defined(SYCL_LANGUAGE_VERSION) && \
26+
!defined(SYCL_DISABLE_FSYCL_SYCLHPP_WARNING)
27+
WARNING("You are including <sycl/sycl.hpp> without -fsycl flag, \
28+
which is errorenous for device code compilation. This warning \
29+
can be disabled by setting SYCL_DISABLE_FSYCL_SYCLHPP_WARNING macro.")
30+
#endif
31+
#undef WARNING
32+
#undef TOSTRING
33+
#undef STRINGIFY
34+
1135
#include <sycl/detail/core.hpp>
1236

1337
#include <sycl/accessor_image.hpp>

sycl/test/warnings/no_fsycl_flag.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Test to verify that a warning is thrown when the -fsycl flag is not used and
2+
// <sycl/sycl.hpp> file is included.
3+
// RUN: %clangxx -std=c++17 -I %sycl_include -fsyntax-only %s -Xclang -verify -Xclang -verify-ignore-unexpected=note,warning
4+
5+
// expected-warning@sycl/sycl.hpp:27 {{You are including <sycl/sycl.hpp> without -fsycl flag, which is errorenous for device code compilation. This warning can be disabled by setting SYCL_DISABLE_FSYCL_SYCLHPP_WARNING macro.}}
6+
#include <sycl/sycl.hpp>

sycl/tools/sycl-ls/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if (WIN32 AND "${build_type_lower}" MATCHES "debug")
99
endif()
1010

1111
# Disable aspect::image & deprecation warnings.
12-
target_compile_definitions(sycl-ls PRIVATE SYCL_DISABLE_IMAGE_ASPECT_WARNING SYCL2020_DISABLE_DEPRECATION_WARNINGS)
12+
target_compile_definitions(sycl-ls PRIVATE SYCL_DISABLE_IMAGE_ASPECT_WARNING SYCL2020_DISABLE_DEPRECATION_WARNINGS SYCL_DISABLE_FSYCL_SYCLHPP_WARNING)
1313

1414
target_link_libraries(sycl-ls
1515
PRIVATE

0 commit comments

Comments
 (0)