Skip to content

Commit 1fe25ed

Browse files
alliepiperericnieblermiscco
authored
Share common compiler flags in new CCCL-level targets. (NVIDIA#2386)
* Share common compiler flags in new CCCL-level targets. * fix up some warnings from cudax/ * Avoid deprecation warning from allocator_traits * Silence conversion warning in tuple_leaf * Suppress conversion warning in complex test * Fix more conversion warnings * Silence warning about signedness of unary minus * Let doxygen ignore warning suppression * Suppress a variety of warnings on MSVC. * Fix unused parameter warning. * Suppress reordering warnings on ICC, too. * More msvc taming. * More warnings. * More MSVC narrowing warning fixes. * Re-enable global suppression of MSVC warnings. There are hundreds of these, and most of them appear to be in the unit tests, not the actual Thrust headers. It's turning into a rabbit hole and dragging beyond the scope of the current PR. Filed NVIDIA#2409 to track the removal of these suppressions. --------- Co-authored-by: Eric Niebler <[email protected]> Co-authored-by: Michael Schellenberger Costa <[email protected]>
1 parent 1c422f2 commit 1fe25ed

35 files changed

+362
-469
lines changed

CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ include(CTest)
5555
enable_testing()
5656

5757
if (CCCL_TOPLEVEL_PROJECT)
58-
include(cmake/CCCLUtilities.cmake) # include this first
58+
include(cmake/AppendOptionIfAvailable.cmake)
59+
include(cmake/CCCLUtilities.cmake) # include this before other CCCL helpers
60+
include(cmake/CCCLBuildCompilerTargets.cmake)
5961
include(cmake/CCCLClangdCompileInfo.cmake)
62+
63+
cccl_build_compiler_targets()
6064
endif()
6165

6266
if (CCCL_ENABLE_LIBCUDACXX)
File renamed without changes.

cmake/CCCLBuildCompilerTargets.cmake

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# This file defines the `cccl_build_compiler_targets()` function, which
2+
# creates the following interface targets:
3+
#
4+
# cccl.compiler_interface
5+
# - Interface target providing compiler-specific options needed to build
6+
# CCCL's tests, examples, etc. This includes warning flags and the like.
7+
#
8+
# cccl.compiler_interface_cppXX
9+
# - Interface targets providing compiler-specific options that should only be
10+
# applied to certain dialects of C++. Includes `compiler_interface`, and will
11+
# be defined for each supported dialect.
12+
#
13+
# cccl.silence_unreachable_code_warnings
14+
# - Interface target that silences unreachable code warnings.
15+
# - Used to selectively disable such warnings in unit tests caused by
16+
# unconditionally thrown exceptions.
17+
18+
set(CCCL_KNOWN_CXX_DIALECTS 11 14 17 20)
19+
20+
# sccache cannot handle the -Fd option generating pdb files
21+
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT Embedded)
22+
23+
function(cccl_build_compiler_interface interface_target cuda_compile_options cxx_compile_options compile_defs)
24+
add_library(${interface_target} INTERFACE)
25+
26+
foreach (cuda_option IN LISTS cuda_compile_options)
27+
target_compile_options(${interface_target} INTERFACE
28+
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:${cuda_option}>
29+
)
30+
endforeach()
31+
32+
foreach (cxx_option IN LISTS cxx_compile_options)
33+
target_compile_options(${interface_target} INTERFACE
34+
$<$<COMPILE_LANGUAGE:CXX>:${cxx_option}>
35+
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcompiler=${cxx_option}>
36+
)
37+
endforeach()
38+
39+
target_compile_definitions(${interface_target} INTERFACE
40+
${compile_defs}
41+
)
42+
endfunction()
43+
44+
function(cccl_build_compiler_targets)
45+
set(cuda_compile_options)
46+
set(cxx_compile_options)
47+
set(cxx_compile_definitions)
48+
49+
list(APPEND cuda_compile_definitions "-Xcudafe=--display_error_number")
50+
list(APPEND cuda_compile_definitions "-Xcudafe=--promote_warnings")
51+
list(APPEND cuda_compile_definitions "-Wno-deprecated-gpu-targets")
52+
53+
# Ensure that we build our tests without treating ourself as system header
54+
list(APPEND cxx_compile_definitions "_CCCL_NO_SYSTEM_HEADER")
55+
56+
if ("MSVC" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
57+
list(APPEND cuda_compile_options "--use-local-env")
58+
list(APPEND cxx_compile_options "/bigobj")
59+
list(APPEND cxx_compile_definitions "_ENABLE_EXTENDED_ALIGNED_STORAGE")
60+
list(APPEND cxx_compile_definitions "NOMINMAX")
61+
62+
append_option_if_available("/W4" cxx_compile_options)
63+
# Treat all warnings as errors. This is only supported on Release builds,
64+
# as `nv_exec_check_disable` doesn't seem to work with MSVC debug iterators
65+
# and spurious warnings are emitted.
66+
# See NVIDIA/thrust#1273, NVBug 3129879.
67+
if (CMAKE_BUILD_TYPE STREQUAL "Release")
68+
append_option_if_available("/WX" cxx_compile_options)
69+
endif()
70+
71+
# Suppress overly-pedantic/unavoidable warnings brought in with /W4:
72+
# C4324: structure was padded due to alignment specifier
73+
append_option_if_available("/wd4324" cxx_compile_options)
74+
# C4505: unreferenced local function has been removed
75+
# The CUDA `host_runtime.h` header emits this for
76+
# `__cudaUnregisterBinaryUtil`.
77+
append_option_if_available("/wd4505" cxx_compile_options)
78+
# C4706: assignment within conditional expression
79+
# MSVC doesn't provide an opt-out for this warning when the assignment is
80+
# intentional. Clang will warn for these, but suppresses the warning when
81+
# double-parentheses are used around the assignment. We'll let Clang catch
82+
# unintentional assignments and suppress all such warnings on MSVC.
83+
append_option_if_available("/wd4706" cxx_compile_options)
84+
85+
# Disabled loss-of-data conversion warnings.
86+
# append_option_if_available("/wd4244" cxx_compile_options)
87+
88+
# Disable warning about applying unary operator- to unsigned type.
89+
# append_option_if_available("/wd4146" cxx_compile_options)
90+
91+
# MSVC STL assumes that `allocator_traits`'s allocator will use raw pointers,
92+
# and the `__DECLSPEC_ALLOCATOR` macro causes issues with thrust's universal
93+
# allocators:
94+
# warning C4494: 'std::allocator_traits<_Alloc>::allocate' :
95+
# Ignoring __declspec(allocator) because the function return type is not
96+
# a pointer or reference
97+
# See https://github.com/microsoft/STL/issues/696
98+
append_option_if_available("/wd4494" cxx_compile_options)
99+
100+
else()
101+
list(APPEND cuda_compile_options "-Wreorder")
102+
103+
append_option_if_available("-Werror" cxx_compile_options)
104+
append_option_if_available("-Wall" cxx_compile_options)
105+
append_option_if_available("-Wextra" cxx_compile_options)
106+
append_option_if_available("-Wreorder" cxx_compile_options)
107+
append_option_if_available("-Winit-self" cxx_compile_options)
108+
append_option_if_available("-Woverloaded-virtual" cxx_compile_options)
109+
append_option_if_available("-Wcast-qual" cxx_compile_options)
110+
append_option_if_available("-Wpointer-arith" cxx_compile_options)
111+
append_option_if_available("-Wunused-local-typedef" cxx_compile_options)
112+
append_option_if_available("-Wvla" cxx_compile_options)
113+
114+
# Disable GNU extensions (flag is clang only)
115+
append_option_if_available("-Wgnu" cxx_compile_options)
116+
append_option_if_available("-Wno-gnu-line-marker" cxx_compile_options) # WAR 3916341
117+
# Calling a variadic macro with zero args is a GNU extension until C++20,
118+
# but the THRUST_PP_ARITY macro is used with zero args. Need to see if this
119+
# is a real problem worth fixing.
120+
append_option_if_available("-Wno-gnu-zero-variadic-macro-arguments" cxx_compile_options)
121+
122+
# This complains about functions in CUDA system headers when used with nvcc.
123+
append_option_if_available("-Wno-unused-function" cxx_compile_options)
124+
endif()
125+
126+
if ("GNU" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
127+
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 7.3)
128+
# GCC 7.3 complains about name mangling changes due to `noexcept`
129+
# becoming part of the type system; we don't care.
130+
append_option_if_available("-Wno-noexcept-type" cxx_compile_options)
131+
endif()
132+
endif()
133+
134+
if ("Intel" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
135+
# Do not flush denormal floats to zero
136+
append_option_if_available("-no-ftz" cxx_compile_options)
137+
# Disable warning that inlining is inhibited by compiler thresholds.
138+
append_option_if_available("-diag-disable=11074" cxx_compile_options)
139+
append_option_if_available("-diag-disable=11076" cxx_compile_options)
140+
# Disable warning about deprecated classic compiler
141+
append_option_if_available("-diag-disable=10441" cxx_compile_options)
142+
endif()
143+
144+
cccl_build_compiler_interface(cccl.compiler_interface
145+
"${cuda_compile_options}"
146+
"${cxx_compile_options}"
147+
"${cxx_compile_definitions}"
148+
)
149+
150+
# These targets are used for dialect-specific options:
151+
foreach (dialect IN LISTS CCCL_KNOWN_CXX_DIALECTS)
152+
add_library(cccl.compiler_interface_cpp${dialect} INTERFACE)
153+
target_link_libraries(cccl.compiler_interface_cpp${dialect} INTERFACE cccl.compiler_interface)
154+
endforeach()
155+
156+
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
157+
# C4127: conditional expression is constant
158+
# Disable this MSVC warning for C++11/C++14. In C++17+, we can use
159+
# _CCCL_IF_CONSTEXPR to address these warnings.
160+
target_compile_options(cccl.compiler_interface_cpp11 INTERFACE
161+
$<$<COMPILE_LANGUAGE:CXX>:/wd4127>
162+
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcompiler=/wd4127>
163+
)
164+
target_compile_options(cccl.compiler_interface_cpp14 INTERFACE
165+
$<$<COMPILE_LANGUAGE:CXX>:/wd4127>
166+
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcompiler=/wd4127>
167+
)
168+
endif()
169+
170+
# Some of our unit tests unconditionally throw exceptions, and compilers will
171+
# detect that the following instructions are unreachable. This is intentional
172+
# and unavoidable in these cases. This target can be used to silence
173+
# unreachable code warnings.
174+
add_library(cccl.silence_unreachable_code_warnings INTERFACE)
175+
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
176+
target_compile_options(cccl.silence_unreachable_code_warnings INTERFACE
177+
$<$<COMPILE_LANGUAGE:CXX>:/wd4702>
178+
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcompiler=/wd4702>
179+
)
180+
endif()
181+
endfunction()

cub/CMakeLists.txt

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# 3.27.5 is the minimum for MSVC build with RDC=true.
44
cmake_minimum_required(VERSION 3.15)
55

6-
# CXX is only needed for AppendOptionIfAvailable.
76
project(CUB LANGUAGES NONE)
87

98
# Determine whether CUB is the top-level project or included into
@@ -17,9 +16,7 @@ if (CUB_TOPLEVEL_PROJECT)
1716
cmake_minimum_required(VERSION 3.21)
1817
endif()
1918

20-
# This must appear after our Compiler Hacks or else CMake will delete the cache
21-
# and reconfigure from scratch.
22-
# This must also appear before the installation rules, as it is required by the
19+
# This must appear before the installation rules, as it is required by the
2320
# GNUInstallDirs CMake module.
2421
enable_language(CXX)
2522

@@ -65,7 +62,6 @@ if (NOT (CUB_ENABLE_HEADER_TESTING OR
6562
return()
6663
endif()
6764

68-
include(cmake/AppendOptionIfAvailable.cmake)
6965
include(cmake/CubBuildCompilerTargets.cmake)
7066
include(cmake/CubBuildTargetList.cmake)
7167
include(cmake/CubCudaConfig.cmake)
@@ -87,6 +83,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
8783
set(CUB_LIBRARY_OUTPUT_DIR "${CMAKE_BINARY_DIR}/lib")
8884
set(CUB_EXECUTABLE_OUTPUT_DIR "${CMAKE_BINARY_DIR}/bin")
8985

86+
cub_build_compiler_targets()
9087
cub_build_target_list()
9188

9289
if (CUB_ENABLE_HEADER_TESTING)
+21-133
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,33 @@
1-
#
21
# This file defines the `cub_build_compiler_targets()` function, which
32
# creates the following interface targets:
43
#
54
# cub.compiler_interface
6-
# - Interface target providing compiler-specific options needed to build
7-
# CUB's tests, examples, etc.
5+
# - Interface target linked into all targets in the CUB developer build.
6+
# This should not be directly used; it is only used to construct the
7+
# per-dialect targets below.
8+
#
9+
# cub.compiler_interface_cppXX
10+
# - Interface targets providing dialect-specific compiler flags. These should
11+
# be linked into the developer build targets, as they include both
12+
# cub.compiler_interface and cccl.compiler_interface_cppXX.
813

914
function(cub_build_compiler_targets)
10-
set(cxx_compile_definitions)
11-
set(cxx_compile_options)
1215
set(cuda_compile_options)
16+
set(cxx_compile_options)
17+
set(cxx_compile_definitions)
1318

14-
# Ensure that we build our tests without treating ourself as system header
15-
list(APPEND cxx_compile_definitions "_CCCL_NO_SYSTEM_HEADER")
16-
17-
if ("MSVC" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
18-
list(APPEND cxx_compile_definitions _ENABLE_EXTENDED_ALIGNED_STORAGE)
19-
list(APPEND cuda_compile_options "--use-local-env")
20-
21-
# sccache cannot handle the -Fd option generationg pdb files
22-
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT Embedded)
23-
24-
append_option_if_available("/W4" cxx_compile_options)
25-
26-
append_option_if_available("/WX" cxx_compile_options)
27-
28-
# Suppress overly-pedantic/unavoidable warnings brought in with /W4:
29-
# C4324: structure was padded due to alignment specifier
30-
append_option_if_available("/wd4324" cxx_compile_options)
31-
# C4505: unreferenced local function has been removed
32-
# The CUDA `host_runtime.h` header emits this for
33-
# `__cudaUnregisterBinaryUtil`.
34-
append_option_if_available("/wd4505" cxx_compile_options)
35-
# C4706: assignment within conditional expression
36-
# MSVC doesn't provide an opt-out for this warning when the assignment is
37-
# intentional. Clang will warn for these, but suppresses the warning when
38-
# double-parentheses are used around the assignment. We'll let Clang catch
39-
# unintentional assignments and suppress all such warnings on MSVC.
40-
append_option_if_available("/wd4706" cxx_compile_options)
41-
42-
# Some tests require /bigobj to fit everything into their object files:
43-
append_option_if_available("/bigobj" cxx_compile_options)
44-
else()
45-
append_option_if_available("-Wreorder" cuda_compile_options)
46-
47-
append_option_if_available("-Werror" cxx_compile_options)
48-
append_option_if_available("-Wall" cxx_compile_options)
49-
append_option_if_available("-Wextra" cxx_compile_options)
50-
append_option_if_available("-Winit-self" cxx_compile_options)
51-
append_option_if_available("-Woverloaded-virtual" cxx_compile_options)
52-
append_option_if_available("-Wcast-qual" cxx_compile_options)
53-
append_option_if_available("-Wpointer-arith" cxx_compile_options)
54-
append_option_if_available("-Wunused-local-typedef" cxx_compile_options)
55-
append_option_if_available("-Wvla" cxx_compile_options)
56-
57-
# Disable GNU extensions (flag is clang only)
58-
append_option_if_available("-Wgnu" cxx_compile_options)
59-
append_option_if_available("-Wno-gnu-line-marker" cxx_compile_options) # WAR 3916341
60-
# Calling a variadic macro with zero args is a GNU extension until C++20,
61-
# but the THRUST_PP_ARITY macro is used with zero args. Need to see if this
62-
# is a real problem worth fixing.
63-
append_option_if_available("-Wno-gnu-zero-variadic-macro-arguments" cxx_compile_options)
64-
65-
# This complains about functions in CUDA system headers when used with nvcc.
66-
append_option_if_available("-Wno-unused-function" cxx_compile_options)
67-
endif()
68-
69-
if ("GNU" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
70-
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 7.3)
71-
# GCC 7.3 complains about name mangling changes due to `noexcept`
72-
# becoming part of the type system; we don't care.
73-
append_option_if_available("-Wno-noexcept-type" cxx_compile_options)
74-
endif()
75-
endif()
76-
77-
if ("Intel" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
78-
# Do not flush denormal floats to zero
79-
append_option_if_available("-no-ftz" cxx_compile_options)
80-
# Disable warning that inlining is inhibited by compiler thresholds.
81-
append_option_if_available("-diag-disable=11074" cxx_compile_options)
82-
append_option_if_available("-diag-disable=11076" cxx_compile_options)
83-
# Disable warning about deprecated classic compiler
84-
append_option_if_available("-diag-disable=10441" cxx_compile_options)
85-
endif()
86-
87-
if ("Clang" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
88-
option(CUB_ENABLE_CT_PROFILING "Enable compilation time profiling" OFF)
89-
if (CUB_ENABLE_CT_PROFILING)
90-
append_option_if_available("-ftime-trace" cxx_compile_options)
91-
endif()
92-
endif()
93-
94-
if ("NVHPC" STREQUAL "${CMAKE_CUDA_COMPILER_ID}")
95-
# Do not flush denormal floats to zero
96-
list(APPEND cxx_compile_options -Mnodaz)
97-
# TODO: Managed memory is currently not supported on windows with WSL
98-
list(APPEND cxx_compile_options -gpu=nomanaged)
99-
endif()
100-
101-
add_library(cub.compiler_interface INTERFACE)
102-
103-
foreach (cxx_option IN LISTS cxx_compile_options)
104-
target_compile_options(cub.compiler_interface INTERFACE
105-
$<$<COMPILE_LANGUAGE:CXX>:${cxx_option}>
106-
$<$<COMPILE_LANG_AND_ID:CUDA,NVHPC>:${cxx_option}>
107-
# Only use -Xcompiler with NVCC, not NVC++.
108-
#
109-
# CMake can't split genexs, so this can't be formatted better :(
110-
# This is:
111-
# if (using CUDA and CUDA_COMPILER is NVCC) add -Xcompiler=opt:
112-
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcompiler=${cxx_option}>
113-
)
114-
endforeach()
115-
116-
foreach (cuda_option IN LISTS cuda_compile_options)
117-
target_compile_options(cub.compiler_interface INTERFACE
118-
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:${cuda_option}>
119-
)
120-
endforeach()
121-
122-
# Add these for both CUDA and CXX targets:
123-
target_compile_definitions(cub.compiler_interface INTERFACE
124-
${cxx_compile_definitions}
125-
)
126-
127-
# Promote warnings and display diagnostic numbers for nvcc:
128-
target_compile_options(cub.compiler_interface INTERFACE
129-
# If using CUDA w/ NVCC...
130-
# Display diagnostic numbers.
131-
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcudafe=--display_error_number>
132-
# Promote warnings.
133-
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcudafe=--promote_warnings>
134-
# Don't complain about deprecated GPU targets.
135-
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Wno-deprecated-gpu-targets>
19+
cccl_build_compiler_interface(cub.compiler_interface
20+
"${cuda_compile_options}"
21+
"${cxx_compile_options}"
22+
"${cxx_compile_definitions}"
13623
)
13724

138-
if ("MSVC" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
139-
# Use the local env instead of rebuilding it all the time
140-
target_compile_options(cub.compiler_interface INTERFACE
141-
# If using CUDA w/ NVCC...
142-
$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:--use-local-env>
25+
foreach (dialect IN LISTS CCCL_KNOWN_CXX_DIALECTS)
26+
add_library(cub.compiler_interface_cpp${dialect} INTERFACE)
27+
target_link_libraries(cub.compiler_interface_cpp${dialect} INTERFACE
28+
# order matters here, we need the project options to override the cccl options.
29+
cccl.compiler_interface_cpp${dialect}
30+
cub.compiler_interface
14331
)
144-
endif()
32+
endforeach()
14533
endfunction()

0 commit comments

Comments
 (0)