Skip to content

Commit 69df389

Browse files
committed
fix(core): reject use_libdevice on the NVRTC and PTX backends
`ProgramOptions.use_libdevice` is documented "Only supported for the NVVM backend", but the guard enforcing that was written in `Program_init`'s final `else` -- the branch reached only when `code_type` is not a backend at all: else: supported_code_types = tuple(x.value for x in SourceCodeType) if options.use_libdevice: raise ValueError("use_libdevice is only supported by the NVVM backend") raise RuntimeError(f"Unsupported {code_type=} ({supported_code_types=})") So it never fires for the two real non-NVVM backends, and it fires for the wrong reason on a typo: * `Program(src, "c++", ProgramOptions(use_libdevice=True))` and the "ptx" equivalent are accepted silently. `self._use_libdevice` is initialised to False and only set inside the `nvvm` branch, so libdevice is never loaded and the caller learns about it from undefined-symbol errors at link time instead of from the documented up-front ValueError. * `Program(src, "bogus", ProgramOptions(use_libdevice=True))` raises "use_libdevice is only supported by the NVVM backend", which says nothing about the code_type that is the actual problem. Move the check next to the per-branch `extra_sources` guards, which are the sibling option with the same "NVVM only" contract and are already duplicated into the "c++" and "ptx" branches, and drop it from the `else` so an unrecognised code_type reports itself.
1 parent 3bd069a commit 69df389

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎cuda_core/cuda/core/_program.pyx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ cdef inline int Program_init(Program self, object code, str code_type, object op
771771
assert_type(code, str)
772772
if options.extra_sources is not None:
773773
raise ValueError("extra_sources is not supported by the NVRTC backend (C++ code_type)")
774+
if options.use_libdevice:
775+
raise ValueError("use_libdevice is not supported by the NVRTC backend (C++ code_type)")
774776

775777
# TODO: support pre-loaded headers & include names
776778
code_bytes = code.encode()
@@ -789,6 +791,8 @@ cdef inline int Program_init(Program self, object code, str code_type, object op
789791
assert_type(code, str)
790792
if options.extra_sources is not None:
791793
raise ValueError("extra_sources is not supported by the PTX backend.")
794+
if options.use_libdevice:
795+
raise ValueError("use_libdevice is not supported by the PTX backend.")
792796
code_bytes = code.encode()
793797
self._code = code_bytes
794798
self._linker = Linker(
@@ -835,9 +839,12 @@ cdef inline int Program_init(Program self, object code, str code_type, object op
835839
self._linker = None
836840

837841
else:
842+
# No use_libdevice check here: this branch is only reached for a
843+
# code_type that is not a backend at all, and an unrecognised
844+
# code_type is the error worth reporting. The per-backend guards
845+
# above mirror the extra_sources ones and are what actually
846+
# enforce "NVVM only", which is what ProgramOptions documents.
838847
supported_code_types = tuple(x.value for x in SourceCodeType)
839-
if options.use_libdevice:
840-
raise ValueError("use_libdevice is only supported by the NVVM backend")
841848
raise RuntimeError(f"Unsupported {code_type=} ({supported_code_types=})")
842849

843850
return 0

‎cuda_core/docs/source/release/1.2.0-notes.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ Fixes and enhancements
7373
Windows, both ``ctypes.CFUNCTYPE`` and ``ctypes.WINFUNCTYPE`` are accepted.
7474
(`#2439 <https://github.com/NVIDIA/cuda-python/issues/2439>`__)
7575

76+
- :class:`Program` now rejects ``use_libdevice=True`` for ``code_type="c++"``
77+
and ``code_type="ptx"``, as :class:`ProgramOptions` documents. The guard was
78+
written in ``Program_init``'s unrecognised-``code_type`` branch, so the two
79+
real non-NVVM backends accepted the option silently and never linked
80+
libdevice, leaving the caller with undefined-symbol errors at link time.
81+
Conversely, an unrecognised ``code_type`` combined with ``use_libdevice=True``
82+
now reports the ``code_type``, not libdevice.
83+
7684
Deprecation Notices
7785
-------------------
7886

‎cuda_core/tests/test_program.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,20 @@ def test_program_init_invalid_code_type():
345345
Program(code, "FORTRAN")
346346

347347

348+
@pytest.mark.agent_authored(model="claude-opus-5")
349+
def test_program_init_invalid_code_type_reports_the_code_type():
350+
"""An unrecognised code_type is the error worth reporting, even when
351+
use_libdevice is set.
352+
353+
The use_libdevice guard used to live in this branch, so a typo'd
354+
code_type combined with use_libdevice=True reported
355+
"use_libdevice is only supported by the NVVM backend" and never
356+
mentioned that the code_type was the actual problem.
357+
"""
358+
with pytest.raises(RuntimeError, match=r"^Unsupported code_type='fortran'"):
359+
Program("goto 100", "FORTRAN", ProgramOptions(arch="sm_80", use_libdevice=True))
360+
361+
348362
def test_program_init_invalid_code_format():
349363
code = 12345
350364
with pytest.raises(TypeError):
@@ -718,6 +732,22 @@ def test_cpp_program_with_extra_sources():
718732
Program(code, "c++", options)
719733

720734

735+
@pytest.mark.agent_authored(model="claude-opus-5")
736+
def test_cpp_program_with_use_libdevice():
737+
"""NVRTC has no libdevice loading path.
738+
739+
The guard used to sit in Program_init's unrecognised-code_type branch, so
740+
"c++" accepted use_libdevice=True silently: Program._use_libdevice stays
741+
False (it is only set inside the nvvm branch), libdevice is never linked,
742+
and the caller finds out from undefined-symbol errors at link time instead
743+
of from the up-front ValueError ProgramOptions documents.
744+
"""
745+
code = 'extern "C" __global__ void my_kernel(){}'
746+
options = ProgramOptions(use_libdevice=True)
747+
with pytest.raises(ValueError, match="use_libdevice is not supported by the NVRTC backend"):
748+
Program(code, "c++", options)
749+
750+
721751
def test_program_options_as_bytes_nvrtc():
722752
"""Test ProgramOptions.as_bytes() for NVRTC backend"""
723753
options = ProgramOptions(arch="sm_80", debug=True, lineinfo=True, ftz=True)
@@ -831,6 +861,14 @@ def test_ptx_program_extra_sources_unsupported(ptx_code_object):
831861
Program(ptx_code_object.code.decode(), "ptx", options)
832862

833863

864+
@pytest.mark.agent_authored(model="claude-opus-5")
865+
def test_ptx_program_use_libdevice_unsupported(ptx_code_object):
866+
"""PTX goes through the Linker, which has no libdevice loading path."""
867+
options = ProgramOptions(use_libdevice=True)
868+
with pytest.raises(ValueError, match="use_libdevice is not supported by the PTX backend"):
869+
Program(ptx_code_object.code.decode(), "ptx", options)
870+
871+
834872
def test_ptx_program_handle_is_linker_handle(init_cuda, ptx_code_object):
835873
"""Program.handle for the PTX backend delegates to the linker handle."""
836874
program = Program(ptx_code_object.code.decode(), "ptx")

0 commit comments

Comments
 (0)