Skip to content

Commit 5cddc15

Browse files
authored
build: unify compiler flags (#2998)
build: unify compiler flags across cuda-bindings and cuda-core (#2998) Close the flag audit in #1882. Both packages' Linux and MSVC flag sets had drifted for legacy reasons and now follow the same structure. cuda-bindings: drop the gcc-only -fpermissive and -fno-var-tracking-assignments, use -O2 instead of -O3, and add /std:c++14 and /O2 on MSVC. cuda-core: add /O2 on MSVC and document why C++17 is required. Also let the Cython test-extension builders reuse the wheel Cython cache.
1 parent c3c2678 commit 5cddc15

6 files changed

Lines changed: 182 additions & 35 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/cython/build_tests.py‎

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,39 @@
88
so `cimport cuda.bindings.*` fails to locate the .pxd files. We resolve
99
the namespace package's source root from `cuda.bindings.__file__` and pass
1010
it via `include_path=` so cythonize finds the .pxd tree on every platform.
11+
12+
When CUDA_PYTHON_CYTHON_CACHE_DIR is set, cythonize uses the same cache
13+
namespacing and include-path aliasing as cuda_bindings/build_hooks.py.
1114
"""
1215

1316
from __future__ import annotations
1417

18+
import importlib.util
1519
import os
1620
import sys
1721
from pathlib import Path
1822

23+
import Cython
1924
from Cython.Build import cythonize
2025
from setuptools import setup
2126

2227
import cuda.bindings
2328

29+
_COMPILER_DIRECTIVES = {"freethreading_compatible": True}
30+
31+
32+
def _load_build_hooks():
33+
# PEP 517 backend, not an installed module. Load by path so we do not put
34+
# cuda_bindings/ on sys.path (that would shadow the installed package).
35+
build_hooks_path = Path(__file__).resolve().parents[2] / "build_hooks.py"
36+
spec = importlib.util.spec_from_file_location("cuda_bindings_build_hooks", build_hooks_path)
37+
module = importlib.util.module_from_spec(spec)
38+
spec.loader.exec_module(module)
39+
return module
40+
41+
42+
build_hooks = _load_build_hooks()
43+
2444

2545
def _bindings_source_root() -> Path:
2646
# cuda.bindings.__file__ -> .../<root>/cuda/bindings/__init__.py
@@ -32,6 +52,41 @@ def _bindings_source_root() -> Path:
3252
return root
3353

3454

55+
def _cythonize_tests(pyx_files):
56+
cache_path = build_hooks._cython_cache_path(
57+
"cuda-bindings-cython-tests",
58+
compiler_directives=_COMPILER_DIRECTIVES,
59+
language_level=3,
60+
cplus=True,
61+
)
62+
cythonize_kwargs = {
63+
"language_level": 3,
64+
"nthreads": 1,
65+
"compiler_directives": _COMPILER_DIRECTIVES,
66+
"cache": cache_path,
67+
}
68+
if cache_path is None:
69+
return cythonize(
70+
pyx_files,
71+
include_path=[str(_bindings_source_root())],
72+
**cythonize_kwargs,
73+
)
74+
75+
# Distinct alias names so a concurrent package build's .cython-stdlib /
76+
# .cython-bindings symlinks are not replaced. Relative aliases resolve
77+
# next to build_hooks.py (package root).
78+
stdlib_target = Path(Cython.__file__).parent / "Includes"
79+
with (
80+
build_hooks._stable_cython_alias(stdlib_target, Path(".cython-stdlib-tests")) as rel_stdlib,
81+
build_hooks._stable_cython_alias(_bindings_source_root(), Path(".cython-bindings-tests")) as rel_bindings,
82+
):
83+
return cythonize(
84+
pyx_files,
85+
include_path=[".", rel_bindings, rel_stdlib],
86+
**cythonize_kwargs,
87+
)
88+
89+
3590
def main() -> None:
3691
script_dir = Path(__file__).resolve().parent
3792
# Avoid appending the absolute checkout path under build/temp: the
@@ -41,13 +96,7 @@ def main() -> None:
4196
if not pyx_files:
4297
raise SystemExit(f"no test_*.pyx files under {script_dir}")
4398

44-
ext_modules = cythonize(
45-
pyx_files,
46-
language_level=3,
47-
nthreads=1,
48-
include_path=[str(_bindings_source_root())],
49-
compiler_directives={"freethreading_compatible": True},
50-
)
99+
ext_modules = _cythonize_tests(pyx_files)
51100

52101
# pytest imports each extension by bare module name (see test_cython.py),
53102
# so build in-place next to its .pyx regardless of the invoking cwd.

‎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/cython/build_tests.py‎

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,39 @@
88
so `cimport cuda.bindings.*` fails to locate the .pxd files. We resolve
99
the namespace package's source root from `cuda.bindings.__file__` and pass
1010
it via `include_path=` so cythonize finds the .pxd tree on every platform.
11+
12+
When CUDA_PYTHON_CYTHON_CACHE_DIR is set, cythonize uses the same cache
13+
namespacing and include-path aliasing as cuda_core/build_hooks.py.
1114
"""
1215

1316
from __future__ import annotations
1417

18+
import importlib.util
1519
import os
1620
import sys
1721
from pathlib import Path
1822

23+
import Cython
1924
from Cython.Build import cythonize
2025
from setuptools import setup
2126

2227
import cuda.bindings
2328

29+
_COMPILER_DIRECTIVES = {"freethreading_compatible": True}
30+
31+
32+
def _load_build_hooks():
33+
# PEP 517 backend, not an installed module. Load by path so we do not put
34+
# cuda_core/ on sys.path (that would shadow the installed package).
35+
build_hooks_path = Path(__file__).resolve().parents[2] / "build_hooks.py"
36+
spec = importlib.util.spec_from_file_location("cuda_core_build_hooks", build_hooks_path)
37+
module = importlib.util.module_from_spec(spec)
38+
spec.loader.exec_module(module)
39+
return module
40+
41+
42+
build_hooks = _load_build_hooks()
43+
2444

2545
def _bindings_source_root() -> Path:
2646
# cuda.bindings.__file__ -> .../<root>/cuda/bindings/__init__.py
@@ -32,26 +52,55 @@ def _bindings_source_root() -> Path:
3252
return root
3353

3454

55+
def _cythonize_tests(pyx_files):
56+
cache_path = build_hooks._cython_cache_path(
57+
"cuda-core-cython-tests",
58+
compiler_directives=_COMPILER_DIRECTIVES,
59+
language_level=3,
60+
cplus=True,
61+
)
62+
cythonize_kwargs = {
63+
"language_level": 3,
64+
"compiler_directives": _COMPILER_DIRECTIVES,
65+
"cache": cache_path,
66+
}
67+
if cache_path is None:
68+
return cythonize(
69+
pyx_files,
70+
include_path=[str(_bindings_source_root())],
71+
**cythonize_kwargs,
72+
)
73+
74+
# Distinct alias names so a concurrent package build's .cython-stdlib /
75+
# .cython-bindings symlinks are not replaced. Relative aliases resolve
76+
# next to build_hooks.py (package root).
77+
stdlib_target = Path(Cython.__file__).parent / "Includes"
78+
with (
79+
build_hooks._stable_cython_alias(stdlib_target, Path(".cython-stdlib-tests")) as rel_stdlib,
80+
build_hooks._stable_cython_alias(_bindings_source_root(), Path(".cython-bindings-tests")) as rel_bindings,
81+
):
82+
return cythonize(
83+
pyx_files,
84+
include_path=[".", rel_bindings, rel_stdlib],
85+
**cythonize_kwargs,
86+
)
87+
88+
3589
def main() -> None:
3690
script_dir = Path(__file__).resolve().parent
37-
pyx_files = sorted(str(p) for p in script_dir.glob("test_*.pyx"))
91+
pyx_files = sorted(p.name for p in script_dir.glob("test_*.pyx"))
3892
if not pyx_files:
3993
raise SystemExit(f"no test_*.pyx files under {script_dir}")
4094

41-
ext_modules = cythonize(
42-
pyx_files,
43-
language_level=3,
44-
include_path=[str(_bindings_source_root())],
45-
compiler_directives={"freethreading_compatible": True},
46-
)
47-
4895
# `build_ext --inplace` places the compiled .so relative to the current
4996
# working directory, but pixi runs this task from the project root. pytest
5097
# imports each extension by bare module name (see test_cython.py), which
5198
# only resolves when the .so sits in tests/cython (the dir pytest puts on
52-
# sys.path). chdir here so the .so lands next to its .pyx regardless of the
53-
# invoking cwd.
99+
# sys.path). chdir before cythonize so alias relpaths and the .so location
100+
# are stable regardless of the invoking cwd.
54101
os.chdir(script_dir)
102+
ext_modules = _cythonize_tests(pyx_files)
103+
55104
sys.argv = [sys.argv[0], "build_ext", "--inplace"]
56105
setup(name="cuda_core_cython_tests", ext_modules=ext_modules)
57106

‎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)