Coverage for cuda/bindings/nvvm.pyx: 91.73%
133 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) 2025-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=a82258bb2654bea18f6bce657324bdbbee8b8b0b30d2a0021e792ba5f95fa9a4
9# <<<< PREAMBLE CONTENT >>>>
11cimport cpython as _cyb_cpython
12from libc.stdint cimport intptr_t
14from cuda.bindings._internal._fast_enum import FastEnum as _cyb_FastEnum
16cdef intptr_t _cyb_get_buffer_pointer(buf, Py_ssize_t size, readonly=True) except?-1:
17 cdef intptr_t ptr
18 cdef int flags = _cyb_cpython.PyBUF_ANY_CONTIGUOUS 1aCDjklmnobcdefghiABuvwxyzprqst
19 if not readonly: 1aCDjklmnobcdefghiABuvwxyzprqst
20 flags |= _cyb_cpython.PyBUF_WRITABLE 1ajklmnobcdefghiAB
21 cdef int status = -1 1aCDjklmnobcdefghiABuvwxyzprqst
22 cdef _cyb_cpython.Py_buffer view
23 if isinstance(buf, int): 1aCDjklmnobcdefghiABuvwxyzprqst
24 ptr = <intptr_t>buf
25 else:
26 try: 1aCDjklmnobcdefghiABuvwxyzprqst
27 status = _cyb_cpython.PyObject_GetBuffer(buf, &view, flags) 1aCDjklmnobcdefghiABuvwxyzprqst
28 if size != -1: 1ajklmnobcdefghiABuvwxyzprqst
29 assert view.len == size 1ajklmnobcdefghiuvwxyzprqst
30 assert view.ndim == 1 1ajklmnobcdefghiABuvwxyzprqst
31 except Exception as e: 1CD
32 adj = "writable " if not readonly else "" 1CD
33 raise ValueError( 1CD
34 "buf must be either a Python int representing the pointer " 1CD
35 f"address to a valid buffer, or a 1D contiguous {adj}" 1CD
36 f"buffer, of size {size}" 1CD
37 ) from e 1CD
38 else:
39 ptr = <intptr_t>view.buf 1ajklmnobcdefghiABuvwxyzprqst
40 finally:
41 if status == 0: 1ajklmnobcdefghiABuvwxyzprqst
42 _cyb_cpython.PyBuffer_Release(&view) 1ajklmnobcdefghiABuvwxyzprqst
43 return ptr 1ajklmnobcdefghiABuvwxyzprqst
46# <<<< END OF PREAMBLE CONTENT >>>>
48cimport cython # NOQA
50from ._internal.utils cimport (get_nested_resource_ptr,
51 nested_resource)
54###############################################################################
55# Enum
56###############################################################################
58class Result(_cyb_FastEnum):
59 """
60 NVVM API call result code.
62 See `nvvmResult`.
63 """
64 SUCCESS = NVVM_SUCCESS
65 ERROR_OUT_OF_MEMORY = NVVM_ERROR_OUT_OF_MEMORY
66 ERROR_PROGRAM_CREATION_FAILURE = NVVM_ERROR_PROGRAM_CREATION_FAILURE
67 ERROR_IR_VERSION_MISMATCH = NVVM_ERROR_IR_VERSION_MISMATCH
68 ERROR_INVALID_INPUT = NVVM_ERROR_INVALID_INPUT
69 ERROR_INVALID_PROGRAM = NVVM_ERROR_INVALID_PROGRAM
70 ERROR_INVALID_IR = NVVM_ERROR_INVALID_IR
71 ERROR_INVALID_OPTION = NVVM_ERROR_INVALID_OPTION
72 ERROR_NO_MODULE_IN_PROGRAM = NVVM_ERROR_NO_MODULE_IN_PROGRAM
73 ERROR_COMPILATION = NVVM_ERROR_COMPILATION
74 ERROR_CANCELLED = NVVM_ERROR_CANCELLED
77###############################################################################
78# Error handling
79###############################################################################
81class nvvmError(Exception):
83 def __init__(self, status):
84 self.status = status 1jklmnoEF)pq
85 s = Result(status) 1jklmnoEF)pq
86 cdef str err = f"{s.name} ({s.value})" 1jklmnoEF)pq
87 super(nvvmError, self).__init__(err) 1jklmnoEF)pq
89 def __reduce__(self):
90 return (type(self), (self.status,))
93@cython.profile(False)
94cdef int check_status(int status) except 1 nogil:
95 if status != 0: 1aCDjQkRlSmTnoEFbUcVdWeXfYgZh0i1GAB2P3u4v5w6x7y8z9!#$%'(HIJKLMNOprqst
96 with gil: 1jklmnoEFpq
97 raise nvvmError(status) 1jklmnoEFpq
98 return status 1aCDjQkRlSmTnoEFbUcVdWeXfYgZh0i1GAB2P3u4v5w6x7y8z9!#$%'(HIJKLMNOprqst
101###############################################################################
102# Wrapper functions
103###############################################################################
105cpdef destroy_program(intptr_t prog):
106 """Destroy a program.
108 Args:
109 prog (intptr_t): nvvm prog.
111 .. seealso:: `nvvmDestroyProgram`
112 """
113 cdef Program p = <Program>prog 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
114 with nogil: 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
115 status = nvvmDestroyProgram(&p) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
116 check_status(status) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
119cpdef str get_error_string(int result):
120 """Get the message string for the given ``nvvmResult`` code.
122 Args:
123 result (Result): NVVM API result code.
125 .. seealso:: `nvvmGetErrorString`
126 """
127 cdef const char *_output_cstr_
128 cdef bytes _output_
129 with nogil: 1*
130 _output_cstr_ = nvvmGetErrorString(<_Result>result) 1*
131 _output_ = _output_cstr_ 1*
132 return _output_.decode() 1*
135cpdef tuple version():
136 """Get the NVVM version.
138 Returns:
139 A 2-tuple containing:
141 - int: NVVM major version number.
142 - int: NVVM minor version number.
144 .. seealso:: `nvvmVersion`
145 """
146 cdef int major
147 cdef int minor
148 with nogil: 1a3HIJKLMNO
149 __status__ = nvvmVersion(&major, &minor) 1a3HIJKLMNO
150 check_status(__status__) 1a3HIJKLMNO
151 return (major, minor) 1a3HIJKLMNO
154cpdef tuple ir_version():
155 """Get the NVVM IR version.
157 Returns:
158 A 4-tuple containing:
160 - int: NVVM IR major version number.
161 - int: NVVM IR minor version number.
162 - int: NVVM IR debug metadata major version number.
163 - int: NVVM IR debug metadata minor version number.
165 .. seealso:: `nvvmIRVersion`
166 """
167 cdef int major_ir
168 cdef int minor_ir
169 cdef int major_dbg
170 cdef int minor_dbg
171 with nogil: 1aQRSTUVWXYZ012456789!#$%'(HIJKLMNOprqst
172 __status__ = nvvmIRVersion(&major_ir, &minor_ir, &major_dbg, &minor_dbg) 1aQRSTUVWXYZ012456789!#$%'(HIJKLMNOprqst
173 check_status(__status__) 1aQRSTUVWXYZ012456789!#$%'(HIJKLMNOprqst
174 return (major_ir, minor_ir, major_dbg, minor_dbg) 1aQRSTUVWXYZ012456789!#$%'(HIJKLMNOprqst
177cpdef intptr_t create_program() except? 0:
178 """Create a program, and set the value of its handle to ``*prog``.
180 Returns:
181 intptr_t: NVVM program.
183 .. seealso:: `nvvmCreateProgram`
184 """
185 cdef Program prog
186 with nogil: 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
187 __status__ = nvvmCreateProgram(&prog) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
188 check_status(__status__) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
189 return <intptr_t>prog 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
192cpdef add_module_to_program(intptr_t prog, buffer, size_t size, name):
193 """Add a module level NVVM IR to a program.
195 Args:
196 prog (intptr_t): NVVM program.
197 buffer (bytes): NVVM IR module in the bitcode or text
198 representation.
199 size (size_t): Size of the NVVM IR module.
200 name (str): Name of the NVVM IR module. If NULL, "<unnamed>"
201 is used as the name.
203 .. seealso:: `nvvmAddModuleToProgram`
204 """
205 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, size, readonly=True) 1aCjklmnobcdefghiuvwxyzprqst
206 if not isinstance(name, str): 1ajklmnobcdefghiuvwxyzprqst
207 raise TypeError("name must be a Python str")
208 cdef bytes _temp_name_ = (<str>name).encode() 1ajklmnobcdefghiuvwxyzprqst
209 cdef char* _name_ = _temp_name_ 1ajklmnobcdefghiuvwxyzprqst
210 with nogil: 1ajklmnobcdefghiuvwxyzprqst
211 __status__ = nvvmAddModuleToProgram(<Program>prog, <const char*>_buffer_, size, <const char*>_name_) 1ajklmnobcdefghiuvwxyzprqst
212 check_status(__status__) 1ajklmnobcdefghiuvwxyzprqst
215cpdef lazy_add_module_to_program(intptr_t prog, buffer, size_t size, name):
216 """Add a module level NVVM IR to a program.
218 Args:
219 prog (intptr_t): NVVM program.
220 buffer (bytes): NVVM IR module in the bitcode representation.
221 size (size_t): Size of the NVVM IR module.
222 name (str): Name of the NVVM IR module. If NULL, "<unnamed>"
223 is used as the name.
225 .. seealso:: `nvvmLazyAddModuleToProgram`
226 """
227 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, size, readonly=True) 1D
228 if not isinstance(name, str):
229 raise TypeError("name must be a Python str")
230 cdef bytes _temp_name_ = (<str>name).encode()
231 cdef char* _name_ = _temp_name_
232 with nogil:
233 __status__ = nvvmLazyAddModuleToProgram(<Program>prog, <const char*>_buffer_, size, <const char*>_name_)
234 check_status(__status__)
237cpdef compile_program(intptr_t prog, int num_options, options):
238 """Compile the NVVM program.
240 Args:
241 prog (intptr_t): NVVM program.
242 num_options (int): Number of compiler ``options`` passed.
243 options (object): Compiler options in the form of C string
244 array. It can be:
246 - an :class:`int` as the pointer address to the nested sequence, or
247 - a Python sequence of :class:`int`\s, each of which is a pointer address
248 to a valid sequence of 'char', or
249 - a nested Python sequence of ``str``.
252 .. seealso:: `nvvmCompileProgram`
253 """
254 cdef nested_resource[ char ] _options_
255 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1ajlnEbcdefghiprqst
256 with nogil: 1ajlnEbcdefghiprqst
257 __status__ = nvvmCompileProgram(<Program>prog, num_options, <const char**>(_options_.ptrs.data())) 1ajlnEbcdefghiprqst
258 check_status(__status__) 1ajlnEbcdefghiprqst
261cpdef verify_program(intptr_t prog, int num_options, options):
262 """Verify the NVVM program.
264 Args:
265 prog (intptr_t): NVVM program.
266 num_options (int): Number of compiler ``options`` passed.
267 options (object): Compiler options in the form of C string
268 array. It can be:
270 - an :class:`int` as the pointer address to the nested sequence, or
271 - a Python sequence of :class:`int`\s, each of which is a pointer address
272 to a valid sequence of 'char', or
273 - a nested Python sequence of ``str``.
276 .. seealso:: `nvvmVerifyProgram`
277 """
278 cdef nested_resource[ char ] _options_
279 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1kmoFuvwxyz
280 with nogil: 1kmoFuvwxyz
281 __status__ = nvvmVerifyProgram(<Program>prog, num_options, <const char**>(_options_.ptrs.data())) 1kmoFuvwxyz
282 check_status(__status__) 1kmoFuvwxyz
285cpdef size_t get_compiled_result_size(intptr_t prog) except? 0:
286 """Get the size of the compiled result.
288 Args:
289 prog (intptr_t): NVVM program.
291 Returns:
292 size_t: Size of the compiled result (including the trailing
293 NULL).
295 .. seealso:: `nvvmGetCompiledResultSize`
296 """
297 cdef size_t buffer_size_ret
298 with nogil: 1bcdefghiA
299 __status__ = nvvmGetCompiledResultSize(<Program>prog, &buffer_size_ret) 1bcdefghiA
300 check_status(__status__) 1bcdefghiA
301 return buffer_size_ret 1bcdefghiA
304cpdef get_compiled_result(intptr_t prog, buffer):
305 """Get the compiled result.
307 Args:
308 prog (intptr_t): NVVM program.
309 buffer (bytes): Compiled result.
311 .. seealso:: `nvvmGetCompiledResult`
312 """
313 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, -1, readonly=False) 1bcdefghiA
314 with nogil: 1bcdefghiA
315 __status__ = nvvmGetCompiledResult(<Program>prog, <char*>_buffer_) 1bcdefghiA
316 check_status(__status__) 1bcdefghiA
319cpdef size_t get_program_log_size(intptr_t prog) except? 0:
320 """Get the Size of Compiler/Verifier Message.
322 Args:
323 prog (intptr_t): NVVM program.
325 Returns:
326 size_t: Size of the compilation/verification log (including
327 the trailing NULL).
329 .. seealso:: `nvvmGetProgramLogSize`
330 """
331 cdef size_t buffer_size_ret
332 with nogil: 1jklmnobcdefghiB
333 __status__ = nvvmGetProgramLogSize(<Program>prog, &buffer_size_ret) 1jklmnobcdefghiB
334 check_status(__status__) 1jklmnobcdefghiB
335 return buffer_size_ret 1jklmnobcdefghiB
338cpdef get_program_log(intptr_t prog, buffer):
339 """Get the Compiler/Verifier Message.
341 Args:
342 prog (intptr_t): NVVM program.
343 buffer (bytes): Compilation/Verification log.
345 .. seealso:: `nvvmGetProgramLog`
346 """
347 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, -1, readonly=False) 1jklmnobcdefghiB
348 with nogil: 1jklmnobcdefghiB
349 __status__ = nvvmGetProgramLog(<Program>prog, <char*>_buffer_) 1jklmnobcdefghiB
350 check_status(__status__) 1jklmnobcdefghiB
353cpdef int llvm_version(arch) except? 0:
354 """Get the LLVM IR version guaranteed to be supported by NVVM.
356 Args:
357 arch (str): Architecture string.
359 Returns:
360 int: IR version number.
362 .. seealso:: `nvvmLLVMVersion`
363 """
364 if not isinstance(arch, str): 1P
365 raise TypeError("arch must be a Python str")
366 cdef bytes _temp_arch_ = (<str>arch).encode() 1P
367 cdef char* _arch_ = _temp_arch_ 1P
368 cdef int major
369 with nogil: 1P
370 __status__ = nvvmLLVMVersion(<const char*>_arch_, &major) 1P
371 check_status(__status__) 1P
372 return major 1P
373del _cyb_FastEnum