Coverage for cuda/bindings/nvjitlink.pyx: 92.95%
156 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-03 02:41 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-03 02:41 +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.3.0. Do not modify it directly.
6# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=4f142d6dd069dd459052ff17e4e585b764e7a8b4298051df3c6c0d39e1c67ded
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 isinstance(buf, int): 1aPQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
27 ptr = <intptr_t>buf
28 else:
29 try: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
30 status = _cyb_cpython.PyObject_GetBuffer(buf, &view, flags) 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
31 if size != -1: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
32 assert view.len == size 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
33 assert view.ndim == 1 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
34 except Exception as e:
35 adj = "writable " if not readonly else ""
36 raise ValueError(
37 "buf must be either a Python int representing the pointer "
38 f"address to a valid buffer, or a 1D contiguous {adj}"
39 f"buffer, of size {size}"
40 ) from e
41 else:
42 ptr = <intptr_t>view.buf 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
43 finally:
44 if status == 0: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
45 _cyb_cpython.PyBuffer_Release(&view) 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
46 return ptr 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG
49# <<<< END OF PREAMBLE CONTENT >>>>
51cimport cython # NOQA
53from ._internal.utils cimport (get_resource_ptr, get_nested_resource_ptr, nested_resource, nullable_unique_ptr,
54 get_resource_ptrs)
56from libcpp.vector cimport vector
59###############################################################################
60# Enum
61###############################################################################
63class Result(_cyb_FastEnum):
64 """
65 The enumerated type `nvJitLinkResult` defines API call result codes.
66 nvJitLink APIs return `nvJitLinkResult` codes to indicate the result.
68 See `nvJitLinkResult`.
69 """
70 SUCCESS = NVJITLINK_SUCCESS
71 ERROR_UNRECOGNIZED_OPTION = (NVJITLINK_ERROR_UNRECOGNIZED_OPTION, 'Unrecognized Option')
72 ERROR_MISSING_ARCH = (NVJITLINK_ERROR_MISSING_ARCH, 'Option `-arch=sm_NN` not specified')
73 ERROR_INVALID_INPUT = (NVJITLINK_ERROR_INVALID_INPUT, 'Invalid Input')
74 ERROR_PTX_COMPILE = (NVJITLINK_ERROR_PTX_COMPILE, 'Issue during PTX Compilation')
75 ERROR_NVVM_COMPILE = (NVJITLINK_ERROR_NVVM_COMPILE, 'Issue during NVVM Compilation')
76 ERROR_INTERNAL = (NVJITLINK_ERROR_INTERNAL, 'Internal Error')
77 ERROR_THREADPOOL = (NVJITLINK_ERROR_THREADPOOL, 'Issue with Thread Pool')
78 ERROR_UNRECOGNIZED_INPUT = (NVJITLINK_ERROR_UNRECOGNIZED_INPUT, 'Unrecognized Input')
79 ERROR_FINALIZE = (NVJITLINK_ERROR_FINALIZE, 'Finalizer Error')
80 ERROR_NULL_INPUT = (NVJITLINK_ERROR_NULL_INPUT, 'Null Input')
81 ERROR_INCOMPATIBLE_OPTIONS = (NVJITLINK_ERROR_INCOMPATIBLE_OPTIONS, 'Incompatible Options')
82 ERROR_INCORRECT_INPUT_TYPE = (NVJITLINK_ERROR_INCORRECT_INPUT_TYPE, 'Incorrect Input Type')
83 ERROR_ARCH_MISMATCH = (NVJITLINK_ERROR_ARCH_MISMATCH, 'Arch Mismatch')
84 ERROR_OUTDATED_LIBRARY = (NVJITLINK_ERROR_OUTDATED_LIBRARY, 'Outdated Library')
85 ERROR_MISSING_FATBIN = (NVJITLINK_ERROR_MISSING_FATBIN, 'Missing Fatbin')
86 ERROR_UNRECOGNIZED_ARCH = (NVJITLINK_ERROR_UNRECOGNIZED_ARCH, 'Unrecognized -arch value')
87 ERROR_UNSUPPORTED_ARCH = (NVJITLINK_ERROR_UNSUPPORTED_ARCH, 'Unsupported -arch value')
88 ERROR_LTO_NOT_ENABLED = (NVJITLINK_ERROR_LTO_NOT_ENABLED, 'Requires -lto')
90class InputType(_cyb_FastEnum):
91 """
92 The enumerated type `nvJitLinkInputType` defines the kind of inputs
93 that can be passed to nvJitLinkAdd* APIs.
95 See `nvJitLinkInputType`.
96 """
97 NONE = (NVJITLINK_INPUT_NONE, 'Error Type')
98 CUBIN = (NVJITLINK_INPUT_CUBIN, 'For CUDA Binaries')
99 PTX = (NVJITLINK_INPUT_PTX, 'For PTX')
100 LTOIR = (NVJITLINK_INPUT_LTOIR, 'For LTO-IR')
101 FATBIN = (NVJITLINK_INPUT_FATBIN, 'For Fatbin')
102 OBJECT = (NVJITLINK_INPUT_OBJECT, 'For Host Object')
103 LIBRARY = (NVJITLINK_INPUT_LIBRARY, 'For Host Library')
104 INDEX = (NVJITLINK_INPUT_INDEX, 'For Index File')
105 ANY = (NVJITLINK_INPUT_ANY, 'Dynamically chooses from the valid types')
108###############################################################################
109# Error handling
110###############################################################################
112class nvJitLinkError(Exception):
114 def __init__(self, status):
115 self.status = status 2a % ' sbtb( )
116 s = Result(status) 2% ' sbtb( )
117 cdef str err = f"{s.name} ({s.value})" 2% ' sbtb( )
118 super(nvJitLinkError, self).__init__(err) 2% ' sbtb( )
120 def __reduce__(self):
121 return (type(self), (self.status,))
124@cython.profile(False)
125cdef int check_status(int status) except 1 nogil:
126 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
127 with gil: 1%'()
128 raise nvJitLinkError(status) 1%'()
129 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
132###############################################################################
133# Wrapper functions
134###############################################################################
136cpdef destroy(intptr_t handle):
137 """nvJitLinkDestroy frees the memory associated with the given handle.
139 Args:
140 handle (intptr_t): nvJitLink handle.
142 .. seealso:: `nvJitLinkDestroy`
143 """
144 cdef Handle h = <Handle>handle 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG
145 with nogil: 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG
146 status = nvJitLinkDestroy(&h) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG
147 check_status(status) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG
150cpdef intptr_t create(uint32_t num_options, options) except -1:
151 """nvJitLinkCreate creates an instance of ``nvJitLinkHandle`` with the given input options, and sets the output parameter ``handle``.
153 Args:
154 num_options (uint32_t): Number of options passed.
155 options (object): Array of size ``num_options`` of option
156 strings. It can be:
158 - an :class:`int` as the pointer address to the nested sequence, or
159 - a Python sequence of :class:`int`\s, each of which is a pointer address
160 to a valid sequence of 'char', or
161 - a nested Python sequence of ``str``.
164 Returns:
165 intptr_t: Address of nvJitLink handle.
167 .. seealso:: `nvJitLinkCreate`
168 """
169 cdef nested_resource[ char ] _options_
170 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()
171 cdef Handle handle
172 with nogil: 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()
173 __status__ = nvJitLinkCreate(&handle, num_options, <const char**>(_options_.ptrs.data())) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()
174 check_status(__status__) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()
175 return <intptr_t>handle 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG
178cpdef add_data(intptr_t handle, int input_type, data, size_t size, name):
179 """nvJitLinkAddData adds data image to the link.
181 Args:
182 handle (intptr_t): nvJitLink handle.
183 input_type (InputType): kind of input.
184 data (bytes): pointer to data image in memory.
185 size (size_t): size of the data.
186 name (str): name of input object.
188 .. seealso:: `nvJitLinkAddData`
189 """
190 cdef void* _data_ = <void *>_cyb_get_buffer_pointer(data, size, readonly=True) 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
191 if not isinstance(name, str): 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
192 raise TypeError("name must be a Python str")
193 cdef bytes _temp_name_ = (<str>name).encode() 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
194 cdef char* _name_ = _temp_name_ 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
195 with nogil: 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
196 __status__ = nvJitLinkAddData(<Handle>handle, <_InputType>input_type, <const void*>_data_, size, <const char*>_name_) 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
197 check_status(__status__) 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG
200cpdef add_file(intptr_t handle, int input_type, file_name):
201 """nvJitLinkAddFile reads data from file and links it in.
203 Args:
204 handle (intptr_t): nvJitLink handle.
205 input_type (InputType): kind of input.
206 file_name (str): name of file.
208 .. seealso:: `nvJitLinkAddFile`
209 """
210 if not isinstance(file_name, str): 1XYZ01234
211 raise TypeError("file_name must be a Python str")
212 cdef bytes _temp_file_name_ = (<str>file_name).encode() 1XYZ01234
213 cdef char* _file_name_ = _temp_file_name_ 1XYZ01234
214 with nogil: 1XYZ01234
215 __status__ = nvJitLinkAddFile(<Handle>handle, <_InputType>input_type, <const char*>_file_name_) 1XYZ01234
216 check_status(__status__) 1XYZ01234
219cpdef complete(intptr_t handle):
220 """nvJitLinkComplete does the actual link.
222 Args:
223 handle (intptr_t): nvJitLink handle.
225 .. seealso:: `nvJitLinkComplete`
226 """
227 with nogil: 1PQRSXYZ05678HIJKbcdefghijklmnopqTUVW12349!#$LMNOrstuvwxyzABCDEFG
228 __status__ = nvJitLinkComplete(<Handle>handle) 1PQRSXYZ05678HIJKbcdefghijklmnopqTUVW12349!#$LMNOrstuvwxyzABCDEFG
229 check_status(__status__) 1PQRSXYZ05678HIJKbcdefghijklmnopqTUVW12349!#$LMNOrstuvwxyzABCDEFG
232cpdef size_t get_linked_cubin_size(intptr_t handle) except? 0:
233 """nvJitLinkGetLinkedCubinSize gets the size of the linked cubin.
235 Args:
236 handle (intptr_t): nvJitLink handle.
238 Returns:
239 size_t: Size of the linked cubin.
241 .. seealso:: `nvJitLinkGetLinkedCubinSize`
242 """
243 cdef size_t size
244 with nogil: 1fghivwxy
245 __status__ = nvJitLinkGetLinkedCubinSize(<Handle>handle, &size) 1fghivwxy
246 check_status(__status__) 1fghivwxy
247 return size 1fghivwxy
250cpdef get_linked_cubin(intptr_t handle, cubin):
251 """nvJitLinkGetLinkedCubin gets the linked cubin.
253 Args:
254 handle (intptr_t): nvJitLink handle.
255 cubin (bytes): The linked cubin.
257 .. seealso:: `nvJitLinkGetLinkedCubin`
258 """
259 cdef void* _cubin_ = <void *>_cyb_get_buffer_pointer(cubin, -1, readonly=False) 1fghivwxy
260 with nogil: 1fghivwxy
261 __status__ = nvJitLinkGetLinkedCubin(<Handle>handle, <void*>_cubin_) 1fghivwxy
262 check_status(__status__) 1fghivwxy
265cpdef size_t get_linked_ptx_size(intptr_t handle) except? 0:
266 """nvJitLinkGetLinkedPtxSize gets the size of the linked ptx.
268 Args:
269 handle (intptr_t): nvJitLink handle.
271 Returns:
272 size_t: Size of the linked PTX.
274 .. seealso:: `nvJitLinkGetLinkedPtxSize`
275 """
276 cdef size_t size
277 with nogil: 1nopqDEFG
278 __status__ = nvJitLinkGetLinkedPtxSize(<Handle>handle, &size) 1nopqDEFG
279 check_status(__status__) 1nopqDEFG
280 return size 1nopqDEFG
283cpdef get_linked_ptx(intptr_t handle, ptx):
284 """nvJitLinkGetLinkedPtx gets the linked ptx.
286 Args:
287 handle (intptr_t): nvJitLink handle.
288 ptx (bytes): The linked PTX.
290 .. seealso:: `nvJitLinkGetLinkedPtx`
291 """
292 cdef void* _ptx_ = <void *>_cyb_get_buffer_pointer(ptx, -1, readonly=False) 1nopqDEFG
293 with nogil: 1nopqDEFG
294 __status__ = nvJitLinkGetLinkedPtx(<Handle>handle, <char*>_ptx_) 1nopqDEFG
295 check_status(__status__) 1nopqDEFG
298cpdef size_t get_error_log_size(intptr_t handle) except? 0:
299 """nvJitLinkGetErrorLogSize gets the size of the error log.
301 Args:
302 handle (intptr_t): nvJitLink handle.
304 Returns:
305 size_t: Size of the error log.
307 .. seealso:: `nvJitLinkGetErrorLogSize`
308 """
309 cdef size_t size
310 with nogil: 1HIJKLMNO
311 __status__ = nvJitLinkGetErrorLogSize(<Handle>handle, &size) 1HIJKLMNO
312 check_status(__status__) 1HIJKLMNO
313 return size 1HIJKLMNO
316cpdef get_error_log(intptr_t handle, log):
317 """nvJitLinkGetErrorLog puts any error messages in the log.
319 Args:
320 handle (intptr_t): nvJitLink handle.
321 log (bytes): The error log.
323 .. seealso:: `nvJitLinkGetErrorLog`
324 """
325 cdef void* _log_ = <void *>_cyb_get_buffer_pointer(log, -1, readonly=False) 1HIJKLMNO
326 with nogil: 1HIJKLMNO
327 __status__ = nvJitLinkGetErrorLog(<Handle>handle, <char*>_log_) 1HIJKLMNO
328 check_status(__status__) 1HIJKLMNO
331cpdef size_t get_info_log_size(intptr_t handle) except? 0:
332 """nvJitLinkGetInfoLogSize gets the size of the info log.
334 Args:
335 handle (intptr_t): nvJitLink handle.
337 Returns:
338 size_t: Size of the info log.
340 .. seealso:: `nvJitLinkGetInfoLogSize`
341 """
342 cdef size_t size
343 with nogil: 1bcderstu
344 __status__ = nvJitLinkGetInfoLogSize(<Handle>handle, &size) 1bcderstu
345 check_status(__status__) 1bcderstu
346 return size 1bcderstu
349cpdef get_info_log(intptr_t handle, log):
350 """nvJitLinkGetInfoLog puts any info messages in the log.
352 Args:
353 handle (intptr_t): nvJitLink handle.
354 log (bytes): The info log.
356 .. seealso:: `nvJitLinkGetInfoLog`
357 """
358 cdef void* _log_ = <void *>_cyb_get_buffer_pointer(log, -1, readonly=False) 1bcderstu
359 with nogil: 1bcderstu
360 __status__ = nvJitLinkGetInfoLog(<Handle>handle, <char*>_log_) 1bcderstu
361 check_status(__status__) 1bcderstu
364cpdef tuple version():
365 """nvJitLinkVersion returns the current version of nvJitLink.
367 Returns:
368 A 2-tuple containing:
370 - unsigned int: The major version.
371 - unsigned int: The minor version.
373 .. seealso:: `nvJitLinkVersion`
374 """
375 cdef unsigned int major
376 cdef unsigned int minor
377 with nogil: 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb
378 __status__ = nvJitLinkVersion(&major, &minor) 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb
379 check_status(__status__) 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb
380 return (major, minor) 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb
383cpdef size_t get_linked_ltoir_size(intptr_t handle) except? 0:
384 """nvJitLinkGetLinkedLTOIRSize gets the size of the linked LTOIR.
386 Args:
387 handle (intptr_t): nvJitLink handle.
389 Returns:
390 size_t: Size of the linked LTOIR.
392 .. seealso:: `nvJitLinkGetLinkedLTOIRSize`
393 """
394 cdef size_t size
395 with nogil: 1jklmzABC
396 __status__ = nvJitLinkGetLinkedLTOIRSize(<Handle>handle, &size) 1jklmzABC
397 check_status(__status__) 1jklmzABC
398 return size 1jklmzABC
401cpdef get_linked_ltoir(intptr_t handle, ltoir):
402 """nvJitLinkGetLinkedLTOIR gets the linked LTOIR.
404 Args:
405 handle (intptr_t): nvJitLink handle.
406 ltoir (bytes): The linked LTOIR in Container format.
408 .. seealso:: `nvJitLinkGetLinkedLTOIR`
409 """
410 cdef void* _ltoir_ = <void *>_cyb_get_buffer_pointer(ltoir, -1, readonly=False) 1jklmzABC
411 with nogil: 1jklmzABC
412 __status__ = nvJitLinkGetLinkedLTOIR(<Handle>handle, <void*>_ltoir_) 1jklmzABC
413 check_status(__status__) 1jklmzABC
414del _cyb_FastEnum