Coverage for cuda/bindings/nvjitlink.pyx: 92.41%
158 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-16 01:23 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-16 01:23 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
4#
5# This code was automatically generated across versions from 12.0.1 to 13.4.1. Do not modify it directly.
6# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=d783bc46a513ae0becbc9385c3be40ba64809b0fd834e5ed3189b94bd287436d
9# <<<< PREAMBLE CONTENT >>>>
11cimport cpython as _cyb_cpython
12from libc.stdint cimport (
13 intptr_t,
14 uint32_t,
15)
17from cuda.bindings._internal._fast_enum import FastEnum as _cyb_FastEnum
19cdef intptr_t _cyb_get_buffer_pointer(buf, Py_ssize_t size, readonly=True) except?-1:
20 cdef intptr_t ptr
21 cdef int flags = _cyb_cpython.PyBUF_ANY_CONTIGUOUS 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
22 if not readonly: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
23 flags |= _cyb_cpython.PyBUF_WRITABLE 1HIJKbcdefghijklmnopqLMNOrstuvwxyzABCDEFG
24 cdef int status = -1 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
25 cdef _cyb_cpython.Py_buffer view
26 if buf is None: 1aPQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
27 ptr = 0
28 elif isinstance(buf, int): 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
29 ptr = <intptr_t>buf
30 else:
31 try: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
32 status = _cyb_cpython.PyObject_GetBuffer(buf, &view, flags) 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
33 if size != -1: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
34 assert view.len == size 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
35 assert view.ndim == 1 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
36 except Exception as e:
37 adj = "writable " if not readonly else ""
38 raise ValueError(
39 "buf must be None, a Python int representing the pointer "
40 f"address to a valid buffer, or a 1D contiguous {adj}"
41 f"buffer, of size {size}"
42 ) from e
43 else:
44 ptr = <intptr_t>view.buf 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
45 finally:
46 if status == 0: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
47 _cyb_cpython.PyBuffer_Release(&view) 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
48 return ptr 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
51# <<<< END OF PREAMBLE CONTENT >>>>
53cimport cython # NOQA
55from ._internal.utils cimport (get_resource_ptr, get_nested_resource_ptr, nested_resource, nullable_unique_ptr,
56 get_resource_ptrs)
58from libcpp.vector cimport vector
61###############################################################################
62# Enum
63###############################################################################
65class Result(_cyb_FastEnum):
66 """
67 The enumerated type `nvJitLinkResult` defines API call result codes.
68 nvJitLink APIs return `nvJitLinkResult` codes to indicate the result.
70 See `nvJitLinkResult`.
71 """
72 SUCCESS = NVJITLINK_SUCCESS
73 ERROR_UNRECOGNIZED_OPTION = (NVJITLINK_ERROR_UNRECOGNIZED_OPTION, 'Unrecognized Option')
74 ERROR_MISSING_ARCH = (NVJITLINK_ERROR_MISSING_ARCH, 'Option `-arch=sm_NN` not specified')
75 ERROR_INVALID_INPUT = (NVJITLINK_ERROR_INVALID_INPUT, 'Invalid Input')
76 ERROR_PTX_COMPILE = (NVJITLINK_ERROR_PTX_COMPILE, 'Issue during PTX Compilation')
77 ERROR_NVVM_COMPILE = (NVJITLINK_ERROR_NVVM_COMPILE, 'Issue during NVVM Compilation')
78 ERROR_INTERNAL = (NVJITLINK_ERROR_INTERNAL, 'Internal Error')
79 ERROR_THREADPOOL = (NVJITLINK_ERROR_THREADPOOL, 'Issue with Thread Pool')
80 ERROR_UNRECOGNIZED_INPUT = (NVJITLINK_ERROR_UNRECOGNIZED_INPUT, 'Unrecognized Input')
81 ERROR_FINALIZE = (NVJITLINK_ERROR_FINALIZE, 'Finalizer Error')
82 ERROR_NULL_INPUT = (NVJITLINK_ERROR_NULL_INPUT, 'Null Input')
83 ERROR_INCOMPATIBLE_OPTIONS = (NVJITLINK_ERROR_INCOMPATIBLE_OPTIONS, 'Incompatible Options')
84 ERROR_INCORRECT_INPUT_TYPE = (NVJITLINK_ERROR_INCORRECT_INPUT_TYPE, 'Incorrect Input Type')
85 ERROR_ARCH_MISMATCH = (NVJITLINK_ERROR_ARCH_MISMATCH, 'Arch Mismatch')
86 ERROR_OUTDATED_LIBRARY = (NVJITLINK_ERROR_OUTDATED_LIBRARY, 'Outdated Library')
87 ERROR_MISSING_FATBIN = (NVJITLINK_ERROR_MISSING_FATBIN, 'Missing Fatbin')
88 ERROR_UNRECOGNIZED_ARCH = (NVJITLINK_ERROR_UNRECOGNIZED_ARCH, 'Unrecognized -arch value')
89 ERROR_UNSUPPORTED_ARCH = (NVJITLINK_ERROR_UNSUPPORTED_ARCH, 'Unsupported -arch value')
90 ERROR_LTO_NOT_ENABLED = (NVJITLINK_ERROR_LTO_NOT_ENABLED, 'Requires -lto')
92class InputType(_cyb_FastEnum):
93 """
94 The enumerated type `nvJitLinkInputType` defines the kind of inputs
95 that can be passed to nvJitLinkAdd* APIs.
97 See `nvJitLinkInputType`.
98 """
99 NONE = (NVJITLINK_INPUT_NONE, 'Error Type')
100 CUBIN = (NVJITLINK_INPUT_CUBIN, 'For CUDA Binaries')
101 PTX = (NVJITLINK_INPUT_PTX, 'For PTX')
102 LTOIR = (NVJITLINK_INPUT_LTOIR, 'For LTO-IR')
103 FATBIN = (NVJITLINK_INPUT_FATBIN, 'For Fatbin')
104 OBJECT = (NVJITLINK_INPUT_OBJECT, 'For Host Object')
105 LIBRARY = (NVJITLINK_INPUT_LIBRARY, 'For Host Library')
106 INDEX = (NVJITLINK_INPUT_INDEX, 'For Index File')
107 ANY = (NVJITLINK_INPUT_ANY, 'Dynamically chooses from the valid types')
110###############################################################################
111# Error handling
112###############################################################################
114class nvJitLinkError(Exception):
116 def __init__(self, status):
117 self.status = status 2% ' sbtb( )
118 s = Result(status) 2% ' sbtb( )
119 cdef str err = f"{s.name} ({s.value})" 2% ' sbtb( )
120 super(nvJitLinkError, self).__init__(err) 2% ' sbtb( )
122 def __reduce__(self):
123 return (type(self), (self.status,))
126@cython.profile(False)
127cdef int check_status(int status) except 1 nogil:
128 if status != 0: 2a P Q R S X Y Z 0 5 6 7 8 * + , - . H I J K b c d e f g h i j k l m n o p q % @ ' T U V W 1 2 3 4 9 ! # $ / : ; = ? L M N O r s t u v w x y z A B C D E F G ( [ ) ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb
129 with gil: 1%'()
130 raise nvJitLinkError(status) 1%'()
131 return status 2a P Q R S X Y Z 0 5 6 7 8 * + , - . H I J K b c d e f g h i j k l m n o p q @ T U V W 1 2 3 4 9 ! # $ / : ; = ? L M N O r s t u v w x y z A B C D E F G [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb
134###############################################################################
135# Wrapper functions
136###############################################################################
138cpdef destroy(intptr_t handle):
139 """nvJitLinkDestroy frees the memory associated with the given handle.
141 Args:
142 handle (intptr_t): nvJitLink handle.
144 .. seealso:: `nvJitLinkDestroy`
145 """
146 cdef Handle h = <Handle>handle 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG
147 with nogil: 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG
148 status = nvJitLinkDestroy(&h) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG
149 check_status(status) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG
152cpdef intptr_t create(uint32_t num_options, options) except -1:
153 """nvJitLinkCreate creates an instance of ``nvJitLinkHandle`` with the given input options, and sets the output parameter ``handle``.
155 It supports options listed in ``Supported Link Options``.
157 Args:
158 num_options (uint32_t): Number of options passed.
159 options (object): Array of size ``num_options`` of option strings. It can be:
161 - an :class:`int` as the pointer address to the nested sequence, or
162 - a Python sequence of :class:`int`\s, each of which is a pointer address
163 to a valid sequence of 'char', or
164 - a nested Python sequence of ``str``.
166 Returns:
167 intptr_t: Address of nvJitLink handle.
169 .. seealso:: `nvJitLinkCreate`
170 """
171 cdef nested_resource[ char ] _options_
172 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()
173 cdef Handle handle
174 with nogil: 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()
175 __status__ = nvJitLinkCreate(&handle, num_options, <const char**>(_options_.ptrs.data())) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()
176 check_status(__status__) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()
177 return <intptr_t>handle 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG
180cpdef add_data(intptr_t handle, int input_type, data, size_t size, name):
181 """nvJitLinkAddData adds data image to the link.
184 Args:
185 handle (intptr_t): nvJitLink handle.
186 input_type (InputType): kind of input.
187 data (bytes): pointer to data image in memory.
188 size (size_t): size of the data.
189 name (str): name of input object.
191 .. seealso:: `nvJitLinkAddData`
192 """
193 cdef void* _data_ = <void *>_cyb_get_buffer_pointer(data, size, readonly=True) 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
194 if not isinstance(name, str): 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
195 raise TypeError("name must be a Python str")
196 cdef bytes _temp_name_ = (<str>name).encode() 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
197 cdef char* _name_ = _temp_name_ 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
198 with nogil: 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
199 __status__ = nvJitLinkAddData(<Handle>handle, <_InputType>input_type, <const void*>_data_, size, <const char*>_name_) 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
200 check_status(__status__) 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
203cpdef add_file(intptr_t handle, int input_type, file_name):
204 """nvJitLinkAddFile reads data from file and links it in.
207 Args:
208 handle (intptr_t): nvJitLink handle.
209 input_type (InputType): kind of input.
210 file_name (str): name of file.
212 .. seealso:: `nvJitLinkAddFile`
213 """
214 if not isinstance(file_name, str): 1XYZ01234
215 raise TypeError("file_name must be a Python str")
216 cdef bytes _temp_file_name_ = (<str>file_name).encode() 1XYZ01234
217 cdef char* _file_name_ = _temp_file_name_ 1XYZ01234
218 with nogil: 1XYZ01234
219 __status__ = nvJitLinkAddFile(<Handle>handle, <_InputType>input_type, <const char*>_file_name_) 1XYZ01234
220 check_status(__status__) 1XYZ01234
223cpdef complete(intptr_t handle):
224 """nvJitLinkComplete does the actual link.
227 Args:
228 handle (intptr_t): nvJitLink handle.
230 .. seealso:: `nvJitLinkComplete`
231 """
232 with nogil: 1PQRSXYZ05678HIJKbcdefghijklmnopqTUVW12349!#$LMNOrstuvwxyzABCDEFG
233 __status__ = nvJitLinkComplete(<Handle>handle) 1PQRSXYZ05678HIJKbcdefghijklmnopqTUVW12349!#$LMNOrstuvwxyzABCDEFG
234 check_status(__status__) 1PQRSXYZ05678HIJKbcdefghijklmnopqTUVW12349!#$LMNOrstuvwxyzABCDEFG
237cpdef size_t get_linked_cubin_size(intptr_t handle) except? 0:
238 """nvJitLinkGetLinkedCubinSize gets the size of the linked cubin.
241 Args:
242 handle (intptr_t): nvJitLink handle.
244 Returns:
245 size_t: Size of the linked cubin.
247 .. seealso:: `nvJitLinkGetLinkedCubinSize`
248 """
249 cdef size_t size
250 with nogil: 1fghivwxy
251 __status__ = nvJitLinkGetLinkedCubinSize(<Handle>handle, &size) 1fghivwxy
252 check_status(__status__) 1fghivwxy
253 return size 1fghivwxy
256cpdef get_linked_cubin(intptr_t handle, cubin):
257 """nvJitLinkGetLinkedCubin gets the linked cubin.
259 User is responsible for allocating enough space to hold the ``cubin``.
261 Args:
262 handle (intptr_t): nvJitLink handle.
263 cubin (bytes): The linked cubin.
265 .. seealso:: `nvJitLinkGetLinkedCubin`
266 """
267 cdef void* _cubin_ = <void *>_cyb_get_buffer_pointer(cubin, -1, readonly=False) 1fghivwxy
268 with nogil: 1fghivwxy
269 __status__ = nvJitLinkGetLinkedCubin(<Handle>handle, <void*>_cubin_) 1fghivwxy
270 check_status(__status__) 1fghivwxy
273cpdef size_t get_linked_ptx_size(intptr_t handle) except? 0:
274 """nvJitLinkGetLinkedPtxSize gets the size of the linked ptx.
276 Linked PTX is only available when using the ``-lto`` option.
278 Args:
279 handle (intptr_t): nvJitLink handle.
281 Returns:
282 size_t: Size of the linked PTX.
284 .. seealso:: `nvJitLinkGetLinkedPtxSize`
285 """
286 cdef size_t size
287 with nogil: 1nopqDEFG
288 __status__ = nvJitLinkGetLinkedPtxSize(<Handle>handle, &size) 1nopqDEFG
289 check_status(__status__) 1nopqDEFG
290 return size 1nopqDEFG
293cpdef get_linked_ptx(intptr_t handle, ptx):
294 """nvJitLinkGetLinkedPtx gets the linked ptx.
296 Linked PTX is only available when using the ``-lto`` option. User is
297 responsible for allocating enough space to hold the ``ptx``.
299 Args:
300 handle (intptr_t): nvJitLink handle.
301 ptx (bytes): The linked PTX.
303 .. seealso:: `nvJitLinkGetLinkedPtx`
304 """
305 cdef void* _ptx_ = <void *>_cyb_get_buffer_pointer(ptx, -1, readonly=False) 1nopqDEFG
306 with nogil: 1nopqDEFG
307 __status__ = nvJitLinkGetLinkedPtx(<Handle>handle, <char*>_ptx_) 1nopqDEFG
308 check_status(__status__) 1nopqDEFG
311cpdef size_t get_error_log_size(intptr_t handle) except? 0:
312 """nvJitLinkGetErrorLogSize gets the size of the error log.
315 Args:
316 handle (intptr_t): nvJitLink handle.
318 Returns:
319 size_t: Size of the error log.
321 .. seealso:: `nvJitLinkGetErrorLogSize`
322 """
323 cdef size_t size
324 with nogil: 1HIJKLMNO
325 __status__ = nvJitLinkGetErrorLogSize(<Handle>handle, &size) 1HIJKLMNO
326 check_status(__status__) 1HIJKLMNO
327 return size 1HIJKLMNO
330cpdef get_error_log(intptr_t handle, log):
331 """nvJitLinkGetErrorLog puts any error messages in the log.
333 User is responsible for allocating enough space to hold the ``log``.
335 Args:
336 handle (intptr_t): nvJitLink handle.
337 log (bytes): The error log.
339 .. seealso:: `nvJitLinkGetErrorLog`
340 """
341 cdef void* _log_ = <void *>_cyb_get_buffer_pointer(log, -1, readonly=False) 1HIJKLMNO
342 with nogil: 1HIJKLMNO
343 __status__ = nvJitLinkGetErrorLog(<Handle>handle, <char*>_log_) 1HIJKLMNO
344 check_status(__status__) 1HIJKLMNO
347cpdef size_t get_info_log_size(intptr_t handle) except? 0:
348 """nvJitLinkGetInfoLogSize gets the size of the info log.
351 Args:
352 handle (intptr_t): nvJitLink handle.
354 Returns:
355 size_t: Size of the info log.
357 .. seealso:: `nvJitLinkGetInfoLogSize`
358 """
359 cdef size_t size
360 with nogil: 1bcderstu
361 __status__ = nvJitLinkGetInfoLogSize(<Handle>handle, &size) 1bcderstu
362 check_status(__status__) 1bcderstu
363 return size 1bcderstu
366cpdef get_info_log(intptr_t handle, log):
367 """nvJitLinkGetInfoLog puts any info messages in the log.
369 User is responsible for allocating enough space to hold the ``log``.
371 Args:
372 handle (intptr_t): nvJitLink handle.
373 log (bytes): The info log.
375 .. seealso:: `nvJitLinkGetInfoLog`
376 """
377 cdef void* _log_ = <void *>_cyb_get_buffer_pointer(log, -1, readonly=False) 1bcderstu
378 with nogil: 1bcderstu
379 __status__ = nvJitLinkGetInfoLog(<Handle>handle, <char*>_log_) 1bcderstu
380 check_status(__status__) 1bcderstu
383cpdef tuple version():
384 """nvJitLinkVersion returns the current version of nvJitLink.
387 Returns:
388 A 2-tuple containing:
389 - unsigned int: The major version.
390 - unsigned int: The minor version.
392 .. seealso:: `nvJitLinkVersion`
393 """
394 cdef unsigned int major
395 cdef unsigned int minor
396 with nogil: 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb
397 __status__ = nvJitLinkVersion(&major, &minor) 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb
398 check_status(__status__) 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb
399 return (major, minor) 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb
402cpdef size_t get_linked_ltoir_size(intptr_t handle) except? 0:
403 """nvJitLinkGetLinkedLTOIRSize gets the size of the linked LTOIR.
405 Linked LTOIR is only available when using the ``-lto`` option. The returned
406 data is in LTOIR Container format (not raw bitcode).
408 Args:
409 handle (intptr_t): nvJitLink handle.
411 Returns:
412 size_t: Size of the linked LTOIR.
414 .. seealso:: `nvJitLinkGetLinkedLTOIRSize`
415 """
416 cdef size_t size
417 with nogil: 1jklmzABC
418 __status__ = nvJitLinkGetLinkedLTOIRSize(<Handle>handle, &size) 1jklmzABC
419 check_status(__status__) 1jklmzABC
420 return size 1jklmzABC
423cpdef get_linked_ltoir(intptr_t handle, ltoir):
424 """nvJitLinkGetLinkedLTOIR gets the linked LTOIR.
426 Linked LTOIR is only available when using the ``-lto`` option. User is
427 responsible for allocating enough space to hold the ``ltoir``. The returned
428 data is in LTOIR Container format (not raw bitcode).
430 Args:
431 handle (intptr_t): nvJitLink handle.
432 ltoir (bytes): The linked LTOIR in Container format.
434 .. seealso:: `nvJitLinkGetLinkedLTOIR`
435 """
436 cdef void* _ltoir_ = <void *>_cyb_get_buffer_pointer(ltoir, -1, readonly=False) 1jklmzABC
437 with nogil: 1jklmzABC
438 __status__ = nvJitLinkGetLinkedLTOIR(<Handle>handle, <void*>_ltoir_) 1jklmzABC
439 check_status(__status__) 1jklmzABC
440del _cyb_FastEnum