Skip to content

Commit b06cc7c

Browse files
committed
cuda.core: read linked LTOIR through cynvjitlink instead of probing the Python bindings (NVIDIA#2783)
NVIDIA#2867 fetched linked LTOIR through the Python-level cuda.bindings.nvjitlink module, probing it with hasattr for get_linked_ltoir_size/get_linked_ltoir because the cuda-bindings in use might predate them, and left a TODO(NVIDIA#2783) to switch once a floor existed. Both floors (12.9.8, 13.4.1) have the cynvjitlink getters, so Linker.link("ltoir") now calls nvJitLinkGetLinkedLTOIRSize/nvJitLinkGetLinkedLTOIR directly, like the cubin and ptx branches. The nvJitLink library-version gate (13.3) stays: that is the library, not the bindings. The cached module object is gone; only the cached library version remains. Tests that faked the module probe or asserted the cached module follow.
1 parent b000b54 commit b06cc7c

4 files changed

Lines changed: 13 additions & 66 deletions

File tree

‎cuda_core/cuda/core/_linker.pyi‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const_char_ptr: TypeAlias = bytes
2121
__all__ = ['Linker', 'LinkerOptions']
2222
LinkerHandleT = Union['cuda.bindings.nvjitlink.nvJitLinkHandle', 'cuda.bindings.driver.CUlinkState']
2323
_driver = None
24-
_nvjitlink = None
2524
_nvjitlink_version = None
2625
_inited = False
2726
_use_nvjitlink_backend = None
@@ -263,8 +262,6 @@ class LinkerOptions:
263262

264263
def _require_nvjitlink_version(minimum_version: tuple[int, int], feature: str) -> None:
265264
"""Check that the cached nvJitLink runtime meets a feature's requirement."""
266-
def _linked_ltoir_output_module():
267-
"""Return bindings that can retrieve linked LTOIR without a Cython dependency."""
268265
def _nvjitlink_has_version_symbol(nvjitlink) -> bool: ...
269266
def _decide_nvjitlink_or_driver() -> bool:
270267
"""Return True if falling back to the cuLink* driver APIs."""

‎cuda_core/cuda/core/_linker.pyx‎

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ from cuda.bindings cimport cynvjitlink
1818

1919
from ._rt cimport (
2020
as_cu,
21-
as_intptr,
2221
as_py,
2322
create_culink_handle,
2423
create_nvjitlink_handle,
@@ -687,14 +686,13 @@ cdef inline object Linker_link(Linker self, str target_type):
687686
'LTOIR output is not supported with "ptx" or "cubin" inputs; '
688687
"they carry no LTOIR and would be omitted from the output"
689688
)
690-
nvjitlink_module = _linked_ltoir_output_module()
689+
_require_nvjitlink_version((13, 3), "LTOIR output")
691690

692691
cdef cynvjitlink.nvJitLinkHandle c_nvjitlink_h
693692
cdef cydriver.CUlinkState c_culink_state
694693
cdef size_t c_output_size = 0
695694
cdef char* c_code_ptr
696695
cdef void* c_cubin_out = NULL
697-
cdef intptr_t c_handle
698696

699697
if self._use_nvjitlink:
700698
c_nvjitlink_h = as_cu(self._nvjitlink_handle)
@@ -717,10 +715,13 @@ cdef inline object Linker_link(Linker self, str target_type):
717715
HANDLE_RETURN_NVJITLINK(c_nvjitlink_h,
718716
cynvjitlink.nvJitLinkGetLinkedPtx(c_nvjitlink_h, c_code_ptr))
719717
else:
720-
c_handle = as_intptr(self._nvjitlink_handle)
721-
output_size = nvjitlink_module.get_linked_ltoir_size(c_handle)
722-
code = bytearray(output_size)
723-
nvjitlink_module.get_linked_ltoir(c_handle, code)
718+
HANDLE_RETURN_NVJITLINK(c_nvjitlink_h,
719+
cynvjitlink.nvJitLinkGetLinkedLTOIRSize(c_nvjitlink_h, &c_output_size))
720+
code = bytearray(c_output_size)
721+
c_code_ptr = <char*>(<bytearray>code)
722+
with nogil:
723+
HANDLE_RETURN_NVJITLINK(c_nvjitlink_h,
724+
cynvjitlink.nvJitLinkGetLinkedLTOIR(c_nvjitlink_h, c_code_ptr))
724725
else:
725726
c_culink_state = as_cu(self._culink_handle)
726727
try:
@@ -752,7 +753,6 @@ cdef inline void Linker_annotate_error_log(Linker self, object e):
752753

753754
# TODO: revisit this treatment for py313t builds
754755
_driver = None # populated if nvJitLink cannot be used
755-
_nvjitlink = None # populated if nvJitLink can be used
756756
_nvjitlink_version = None
757757
_inited = False
758758
_use_nvjitlink_backend = None # set by _decide_nvjitlink_or_driver()
@@ -770,23 +770,6 @@ def _require_nvjitlink_version(minimum_version: tuple[int, int], feature: str) -
770770
raise RuntimeError(f"{feature} requires nvJitLink {required} or newer; found {detected}")
771771

772772

773-
# TODO(#2783): Replace this Python-level dispatch with direct cimports once
774-
# the cuda-bindings runtime floor includes the linked-LTOIR getters.
775-
def _linked_ltoir_output_module():
776-
"""Return bindings that can retrieve linked LTOIR without a Cython dependency."""
777-
_require_nvjitlink_version((13, 3), "LTOIR output")
778-
missing = [
779-
name
780-
for name in ("get_linked_ltoir_size", "get_linked_ltoir")
781-
if not hasattr(_nvjitlink, name)
782-
]
783-
if missing:
784-
raise RuntimeError(
785-
"LTOIR output requires cuda-bindings with " + " and ".join(missing)
786-
)
787-
return _nvjitlink
788-
789-
790773
def _nvjitlink_has_version_symbol(nvjitlink) -> bool:
791774
# This condition is equivalent to testing for version >= 12.3
792775
return bool(nvjitlink._inspect_function_pointer("__nvJitLinkVersion"))
@@ -795,11 +778,10 @@ def _nvjitlink_has_version_symbol(nvjitlink) -> bool:
795778
# Note: this function is reused in the tests
796779
def _decide_nvjitlink_or_driver() -> bool:
797780
"""Return True if falling back to the cuLink* driver APIs."""
798-
global _driver, _nvjitlink, _nvjitlink_version, _use_nvjitlink_backend
781+
global _driver, _nvjitlink_version, _use_nvjitlink_backend
799782
if _use_nvjitlink_backend is not None:
800783
return not _use_nvjitlink_backend
801784

802-
_nvjitlink = None
803785
_nvjitlink_version = None
804786

805787
warn_txt_common = (
@@ -820,7 +802,6 @@ def _decide_nvjitlink_or_driver() -> bool:
820802
)
821803
else:
822804
if has_version_symbol:
823-
_nvjitlink = _nvjitlink_bindings
824805
_nvjitlink_version = _nvjitlink_bindings.version()
825806
_use_nvjitlink_backend = True
826807
return False # Use nvjitlink

‎cuda_core/tests/test_linker.py‎

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@
3939

4040
nvJitLinkError = nvjitlink.nvJitLinkError
4141
nvjitlink_version = nvjitlink.version()
42-
has_linked_ltoir_bindings = all(hasattr(nvjitlink, name) for name in ("get_linked_ltoir_size", "get_linked_ltoir"))
4342
else:
4443
nvjitlink_version = (0, 0)
45-
has_linked_ltoir_bindings = False
4644

4745
class nvJitLinkError(Exception):
4846
pass
@@ -343,7 +341,7 @@ def fake_decide():
343341
assert called, "_decide_nvjitlink_or_driver was not called"
344342

345343
@pytest.mark.agent_authored(model="gpt-5.6")
346-
def test_which_backend_caches_nvjitlink_module_and_version(self, monkeypatch):
344+
def test_which_backend_caches_nvjitlink_version(self, monkeypatch):
347345
class NvJitLink:
348346
version_calls = 0
349347

@@ -353,13 +351,11 @@ def version(cls):
353351
return (13, 4)
354352

355353
monkeypatch.setattr(_linker, "_use_nvjitlink_backend", None)
356-
monkeypatch.setattr(_linker, "_nvjitlink", None)
357354
monkeypatch.setattr(_linker, "_nvjitlink_version", None)
358355
monkeypatch.setattr(_linker, "_nvjitlink_bindings", NvJitLink)
359356
monkeypatch.setattr(_linker, "_nvjitlink_has_version_symbol", lambda _nvjitlink: True)
360357

361358
assert Linker.which_backend() == "nvJitLink"
362-
assert _linker._nvjitlink is NvJitLink
363359
assert _linker._nvjitlink_version == (13, 4)
364360
assert NvJitLink.version_calls == 1
365361

@@ -371,7 +367,6 @@ def test_which_backend_falls_back_when_nvjitlink_too_old(self, monkeypatch):
371367
"""Regression test for #2408: old nvJitLink must not crash which_backend()."""
372368
monkeypatch.setattr(_linker, "_use_nvjitlink_backend", None)
373369
monkeypatch.setattr(_linker, "_driver", None)
374-
monkeypatch.setattr(_linker, "_nvjitlink", None)
375370
monkeypatch.setattr(_linker, "_nvjitlink_version", None)
376371

377372
monkeypatch.setattr(_linker, "_nvjitlink_has_version_symbol", lambda _nvjitlink: False)
@@ -388,7 +383,6 @@ def test_which_backend_falls_back_when_dylib_missing(self, monkeypatch):
388383

389384
monkeypatch.setattr(_linker, "_use_nvjitlink_backend", None)
390385
monkeypatch.setattr(_linker, "_driver", None)
391-
monkeypatch.setattr(_linker, "_nvjitlink", None)
392386
monkeypatch.setattr(_linker, "_nvjitlink_version", None)
393387

394388
def raise_missing(_nvjitlink):
@@ -562,26 +556,6 @@ def test_require_nvjitlink_version_accepts_boundary_version(monkeypatch):
562556
_linker._require_nvjitlink_version((13, 2), "incremental linking")
563557

564558

565-
@pytest.mark.agent_authored(model="gpt-5.6")
566-
def test_linked_ltoir_output_requires_new_enough_runtime(monkeypatch):
567-
monkeypatch.setattr(_linker, "_nvjitlink_version", (13, 2))
568-
569-
with pytest.raises(RuntimeError, match=r"LTOIR output requires nvJitLink 13\.3 or newer; found 13\.2"):
570-
_linker._linked_ltoir_output_module()
571-
572-
573-
@pytest.mark.agent_authored(model="gpt-5.6")
574-
def test_linked_ltoir_output_requires_new_enough_bindings(monkeypatch):
575-
class NvJitLinkWithoutLinkedLtoir:
576-
pass
577-
578-
monkeypatch.setattr(_linker, "_nvjitlink", NvJitLinkWithoutLinkedLtoir)
579-
monkeypatch.setattr(_linker, "_nvjitlink_version", (13, 3))
580-
581-
with pytest.raises(RuntimeError, match="cuda-bindings with get_linked_ltoir_size and get_linked_ltoir"):
582-
_linker._linked_ltoir_output_module()
583-
584-
585559
incremental_caller = r"""
586560
extern "C" __device__ int incremental_helper();
587561
extern "C" __global__ void incremental_kernel(int* result) {
@@ -657,7 +631,7 @@ def test_incremental_cubin_round_trip(init_cuda):
657631

658632
@pytest.mark.agent_authored(model="gpt-5.6")
659633
@pytest.mark.skipif(
660-
is_culink_backend or nvjitlink_version < (13, 3) or not has_linked_ltoir_bindings,
634+
is_culink_backend or nvjitlink_version < (13, 3),
661635
reason="linked LTOIR output requires nvJitLink 13.3 or newer and matching cuda-bindings",
662636
)
663637
def test_incremental_ltoir_round_trip(init_cuda):
@@ -683,7 +657,7 @@ def test_incremental_ltoir_round_trip(init_cuda):
683657

684658
@pytest.mark.agent_authored(model="gpt-5.6")
685659
@pytest.mark.skipif(
686-
is_culink_backend or nvjitlink_version < (13, 3) or not has_linked_ltoir_bindings,
660+
is_culink_backend or nvjitlink_version < (13, 3),
687661
reason="linked LTOIR output requires nvJitLink 13.3 or newer and matching cuda-bindings",
688662
)
689663
def test_complete_ltoir_round_trip_matches_direct_cubin(init_cuda):
@@ -729,7 +703,7 @@ def test_incremental_lto_cubin_round_trip(init_cuda):
729703

730704
@pytest.mark.agent_authored(model="gpt-5.6")
731705
@pytest.mark.skipif(
732-
is_culink_backend or nvjitlink_version < (13, 3) or not has_linked_ltoir_bindings,
706+
is_culink_backend or nvjitlink_version < (13, 3),
733707
reason="linked LTOIR output requires nvJitLink 13.3 or newer and matching cuda-bindings",
734708
)
735709
@pytest.mark.parametrize("non_ltoir_type", ("ptx", "cubin"))

‎cuda_core/tests/test_optional_dependency_imports.py‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ def restore_optional_import_state():
1818
saved_nvvm_attempted = _program._nvvm_import_attempted
1919
saved_driver = _linker._driver
2020
saved_inited = _linker._inited
21-
saved_nvjitlink = _linker._nvjitlink
2221
saved_nvjitlink_version = _linker._nvjitlink_version
2322
saved_use_nvjitlink = _linker._use_nvjitlink_backend
2423

2524
_program._nvvm_module = None
2625
_program._nvvm_import_attempted = False
2726
_linker._driver = None
2827
_linker._inited = False
29-
_linker._nvjitlink = None
3028
_linker._nvjitlink_version = None
3129
_linker._use_nvjitlink_backend = None
3230

@@ -36,7 +34,6 @@ def restore_optional_import_state():
3634
_program._nvvm_import_attempted = saved_nvvm_attempted
3735
_linker._driver = saved_driver
3836
_linker._inited = saved_inited
39-
_linker._nvjitlink = saved_nvjitlink
4037
_linker._nvjitlink_version = saved_nvjitlink_version
4138
_linker._use_nvjitlink_backend = saved_use_nvjitlink
4239

@@ -125,7 +122,6 @@ def version(self):
125122

126123
assert use_driver_backend is False
127124
assert _linker._use_nvjitlink_backend is True
128-
assert _linker._nvjitlink is nvjitlink_module
129125
assert _linker._nvjitlink_version == (13, 4)
130126
assert version_calls == 1
131127

@@ -151,5 +147,4 @@ def fake_has_version(_nvjitlink):
151147
assert _linker._decide_nvjitlink_or_driver() is True
152148
assert called["inspect"] is True
153149
assert called["version"] is False
154-
assert _linker._nvjitlink is None
155150
assert _linker._nvjitlink_version is None

0 commit comments

Comments
 (0)