Skip to content

Commit 49be3fc

Browse files
committed
build: unify compiler flag sets across cuda-bindings and cuda-core
Close the audit from NVIDIA#1882. The two packages' Linux and MSVC flag sets had drifted for legacy reasons; both now go through the same structure. Changes in cuda-bindings: - Drop -fpermissive and -fno-var-tracking-assignments (gcc-only; not needed by current Cython-generated C++; confirmed by a rebuild). - Change -O3 to -O2 (consistent with cuda-core; -O3 was not the source of the measured launch_{256,512}_args latency difference vs c++17). - Add /std:c++14 and /O2 on MSVC (was missing entirely). - Split '-std=c++14 -Wno-deprecated-declarations' onto separate lines with an explanatory comment on each. Changes in cuda-core: - Add /O2 on MSVC (modern setuptools no longer forces /Ox). - Add comments at both the MSVC and Linux flag sites explaining why c++17 is required (structured bindings / if constexpr in _cpp/). Tests: - Update the bindings TestResolveToolchain: replace test_gnu_keeps_gcc_only_flags with test_linux_opt_flag_set (asserts -O2, not -O3; no gnu-only flags); add test_msvc_opt_flag_set; update test_gnu_sets_env_and_flags assertions. - Add test_linux_opt_flag_set and test_msvc_opt_flag_set to the core TestResolveToolchain; update the comment in test_gnu_sets_env_and_flags.
1 parent 8c03e61 commit 49be3fc

4 files changed

Lines changed: 67 additions & 18 deletions

File tree

‎cuda_bindings/build_hooks.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,21 +437,28 @@ def _resolve_toolchain(debug=False, compile_for_coverage=False):
437437
extra_link_args = []
438438

439439
if name == "msvc":
440+
# c++14: raising to c++17 costs a measured ~15% on launch_{256,512}_args
441+
# from gcc's c++17 variadic-template expansion.
442+
extra_compile_args += ["/std:c++14", "/O2"]
440443
if debug:
441444
raise RuntimeError("Debuggable builds are not supported on Windows.")
442445
else:
443446
# Common Linux compile flags.
444-
extra_compile_args += ["-std=c++14", "-Wno-deprecated-declarations"]
447+
# c++14: raising to c++17 costs a measured ~15% on launch_{256,512}_args
448+
# from gcc's c++17 variadic-template expansion.
449+
extra_compile_args += ["-std=c++14"]
450+
# cudaMemcpy*Array* and cudaGetDriverEntryPoint are deprecated but still
451+
# supported; suppress the resulting warnings so a future -Werror build
452+
# is not broken by Cython-generated calls we cannot control.
453+
extra_compile_args += ["-Wno-deprecated-declarations"]
445454
# Compiler-specific flags.
446-
if name == "gnu":
447-
extra_compile_args += ["-fpermissive", "-fno-var-tracking-assignments"]
448-
elif name == "llvm":
455+
if name == "llvm":
449456
extra_link_args += ["-fuse-ld=lld"]
450457
# Common Linux debug/opt flags.
451458
if debug:
452459
extra_compile_args += ["-g", "-O0", "-D _GLIBCXX_ASSERTIONS"]
453460
else:
454-
extra_compile_args += ["-g0", "-O3"]
461+
extra_compile_args += ["-g0", "-O2"]
455462
extra_link_args += ["-Wl,--strip-all"]
456463

457464
if compile_for_coverage:

‎cuda_bindings/tests/test_build_hooks.py‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@ def test_gnu_sets_env_and_flags(self, monkeypatch):
148148
assert os.environ["CXX"] == "g++"
149149
assert os.environ["LDCXXSHARED"] == "g++ -shared -Wl,-O1"
150150
assert "LDSHARED" not in os.environ
151-
# gcc-only flags are present (this is the point of P2: explicit gnu must use gcc, not generic cc)
152-
assert "-fpermissive" in cargs
153-
assert "-fno-var-tracking-assignments" in cargs
151+
assert "-fpermissive" not in cargs
152+
assert "-fno-var-tracking-assignments" not in cargs
154153

155154
@pytest.mark.agent_authored(model="grok-4.6")
156155
def test_llvm_keeps_sccache_prefix(self, monkeypatch):
@@ -167,14 +166,31 @@ def test_llvm_keeps_sccache_prefix(self, monkeypatch):
167166
# The launcher prefixes CC/CXX only; the shared linker command is the bare compiler.
168167
assert os.environ["LDCXXSHARED"] == "clang++ -shared -Wl,-O1"
169168

170-
@pytest.mark.agent_authored(model="glm-5.2")
171-
def test_gnu_keeps_gcc_only_flags(self, monkeypatch):
169+
@pytest.mark.agent_authored(model="claude-sonnet-4-6")
170+
def test_linux_opt_flag_set(self, monkeypatch):
171+
"""Linux opt build: -std=c++14, -O2, -Wno-deprecated-declarations; no -O3 or gnu-only flags."""
172172
if sys.platform == "win32":
173-
pytest.skip("gnu only valid on Linux")
174-
monkeypatch.setenv("CUDA_PYTHON_TOOLCHAIN", "gnu")
175-
_name, _cc, _cxx, cargs, _largs = build_hooks._resolve_toolchain()
176-
assert "-fpermissive" in cargs
177-
assert "-fno-var-tracking-assignments" in cargs
173+
pytest.skip("Linux flags only")
174+
monkeypatch.delenv("CUDA_PYTHON_TOOLCHAIN", raising=False)
175+
_name, _cc, _cxx, cargs, largs = build_hooks._resolve_toolchain(debug=False)
176+
assert "-std=c++14" in cargs
177+
assert "-Wno-deprecated-declarations" in cargs
178+
assert "-g0" in cargs
179+
assert "-O2" in cargs
180+
assert "-O3" not in cargs
181+
assert "-fpermissive" not in cargs
182+
assert "-fno-var-tracking-assignments" not in cargs
183+
assert "-Wl,--strip-all" in largs
184+
185+
@pytest.mark.agent_authored(model="claude-sonnet-4-6")
186+
def test_msvc_opt_flag_set(self, monkeypatch):
187+
"""MSVC opt build emits /std:c++14 and /O2."""
188+
if sys.platform != "win32":
189+
pytest.skip("MSVC flags only on Windows")
190+
monkeypatch.delenv("CUDA_PYTHON_TOOLCHAIN", raising=False)
191+
_name, _cc, _cxx, cargs, _largs = build_hooks._resolve_toolchain(debug=False)
192+
assert "/std:c++14" in cargs
193+
assert "/O2" in cargs
178194

179195
@pytest.mark.agent_authored(model="claude-sonnet-5.5")
180196
def test_explicit_toolchain_prefers_env_ldcxxshared(self, monkeypatch):

‎cuda_core/build_hooks.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,13 @@ def _resolve_toolchain(debug=False, compile_for_coverage=False):
484484
extra_link_args = []
485485

486486
if name == "msvc":
487-
extra_compile_args += ["/std:c++17"]
487+
# c++17: required by structured bindings and if constexpr in cuda/core/_cpp/.
488+
extra_compile_args += ["/std:c++17", "/O2"]
488489
if debug:
489490
raise RuntimeError("Debuggable builds are not supported on Windows.")
490491
else:
491492
# Common Linux compile flags.
493+
# c++17: required by structured bindings and if constexpr in cuda/core/_cpp/.
492494
extra_compile_args += ["-std=c++17"]
493495
# Compiler-specific flags.
494496
if name == "llvm":

‎cuda_core/tests/test_build_hooks.py‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,8 @@ def test_gnu_sets_env_and_flags(self, monkeypatch):
976976
assert os.environ["CXX"] == "g++"
977977
assert os.environ["LDCXXSHARED"] == "g++ -shared -Wl,-O1"
978978
assert "LDSHARED" not in os.environ
979-
# gcc-only flags are present (this is the point of P2: explicit gnu must use gcc, not generic cc)
980-
assert "-fpermissive" not in cargs # cuda.core gnu flags don't include it; bindings do
979+
# Neither cuda.core nor cuda.bindings sets these gcc-only flags.
980+
assert "-fpermissive" not in cargs
981981
assert "-fno-var-tracking-assignments" not in cargs
982982

983983
@pytest.mark.agent_authored(model="grok-4.6")
@@ -1018,6 +1018,30 @@ def test_explicit_toolchain_falls_back_to_ldshared_then_shared_flag(self, monkey
10181018
build_hooks._resolve_toolchain()
10191019
assert os.environ["LDCXXSHARED"] == "clang++ -shared"
10201020

1021+
@pytest.mark.agent_authored(model="claude-sonnet-4-6")
1022+
def test_linux_opt_flag_set(self, monkeypatch):
1023+
"""Linux opt build: -std=c++17, -O2; no gnu-only flags."""
1024+
if sys.platform == "win32":
1025+
pytest.skip("Linux flags only")
1026+
monkeypatch.delenv("CUDA_PYTHON_TOOLCHAIN", raising=False)
1027+
_name, _cc, _cxx, cargs, largs = build_hooks._resolve_toolchain(debug=False)
1028+
assert "-std=c++17" in cargs
1029+
assert "-g0" in cargs
1030+
assert "-O2" in cargs
1031+
assert "-fpermissive" not in cargs
1032+
assert "-fno-var-tracking-assignments" not in cargs
1033+
assert "-Wl,--strip-all" in largs
1034+
1035+
@pytest.mark.agent_authored(model="claude-sonnet-4-6")
1036+
def test_msvc_opt_flag_set(self, monkeypatch):
1037+
"""MSVC opt build emits /std:c++17 and /O2."""
1038+
if sys.platform != "win32":
1039+
pytest.skip("MSVC flags only on Windows")
1040+
monkeypatch.delenv("CUDA_PYTHON_TOOLCHAIN", raising=False)
1041+
_name, _cc, _cxx, cargs, _largs = build_hooks._resolve_toolchain(debug=False)
1042+
assert "/std:c++17" in cargs
1043+
assert "/O2" in cargs
1044+
10211045

10221046
class TestWithSccache:
10231047
"""_with_sccache: keep a leading sccache token, swap the compiler."""

0 commit comments

Comments
 (0)