Coverage for cuda/bindings/nvvm.pyx: 91.85%
135 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) 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.4.1. Do not modify it directly.
6# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=92a06aa55bccfabc1d5bb1216489bb147dfb71d6e628480128fee207b8f3794f
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 buf is None: 1aCDjklmnobcdefghiABuvwxyzprqst
24 ptr = 0 1CD
25 elif isinstance(buf, int): 1ajklmnobcdefghiABuvwxyzprqst
26 ptr = <intptr_t>buf
27 else:
28 try: 1ajklmnobcdefghiABuvwxyzprqst
29 status = _cyb_cpython.PyObject_GetBuffer(buf, &view, flags) 1ajklmnobcdefghiABuvwxyzprqst
30 if size != -1: 1ajklmnobcdefghiABuvwxyzprqst
31 assert view.len == size 1ajklmnobcdefghiuvwxyzprqst
32 assert view.ndim == 1 1ajklmnobcdefghiABuvwxyzprqst
33 except Exception as e:
34 adj = "writable " if not readonly else ""
35 raise ValueError(
36 "buf must be None, a Python int representing the pointer "
37 f"address to a valid buffer, or a 1D contiguous {adj}"
38 f"buffer, of size {size}"
39 ) from e
40 else:
41 ptr = <intptr_t>view.buf 1ajklmnobcdefghiABuvwxyzprqst
42 finally:
43 if status == 0: 1ajklmnobcdefghiABuvwxyzprqst
44 _cyb_cpython.PyBuffer_Release(&view) 1ajklmnobcdefghiABuvwxyzprqst
45 return ptr 1aCDjklmnobcdefghiABuvwxyzprqst
48# <<<< END OF PREAMBLE CONTENT >>>>
50cimport cython # NOQA
52from ._internal.utils cimport (get_nested_resource_ptr,
53 nested_resource)
56###############################################################################
57# Enum
58###############################################################################
60class Result(_cyb_FastEnum):
61 """
62 NVVM API call result code.
64 See `nvvmResult`.
65 """
66 SUCCESS = NVVM_SUCCESS
67 ERROR_OUT_OF_MEMORY = NVVM_ERROR_OUT_OF_MEMORY
68 ERROR_PROGRAM_CREATION_FAILURE = NVVM_ERROR_PROGRAM_CREATION_FAILURE
69 ERROR_IR_VERSION_MISMATCH = NVVM_ERROR_IR_VERSION_MISMATCH
70 ERROR_INVALID_INPUT = NVVM_ERROR_INVALID_INPUT
71 ERROR_INVALID_PROGRAM = NVVM_ERROR_INVALID_PROGRAM
72 ERROR_INVALID_IR = NVVM_ERROR_INVALID_IR
73 ERROR_INVALID_OPTION = NVVM_ERROR_INVALID_OPTION
74 ERROR_NO_MODULE_IN_PROGRAM = NVVM_ERROR_NO_MODULE_IN_PROGRAM
75 ERROR_COMPILATION = NVVM_ERROR_COMPILATION
76 ERROR_CANCELLED = NVVM_ERROR_CANCELLED
79###############################################################################
80# Error handling
81###############################################################################
83class nvvmError(Exception):
85 def __init__(self, status):
86 self.status = status 1CDjklmnoEF+pq
87 s = Result(status) 1CDjklmnoEF+pq
88 cdef str err = f"{s.name} ({s.value})" 1CDjklmnoEF+pq
89 super(nvvmError, self).__init__(err) 1CDjklmnoEF+pq
91 def __reduce__(self):
92 return (type(self), (self.status,))
95@cython.profile(False)
96cdef int check_status(int status) except 1 nogil:
97 if status != 0: 1aCDjSkTlUmVnoEFbWcXdYeZf0g1h2i3GAB4R5u6v7w8x9y!z#$%'()*HIJKLMNOPQprqst
98 with gil: 1CDjklmnoEFpq
99 raise nvvmError(status) 1CDjklmnoEFpq
100 return status 1aCDjSkTlUmVnoEFbWcXdYeZf0g1h2i3GAB4R5u6v7w8x9y!z#$%'()*HIJKLMNOPQprqst
103###############################################################################
104# Wrapper functions
105###############################################################################
107cpdef destroy_program(intptr_t prog):
108 """Destroy a program.
110 Args:
111 prog (intptr_t): nvvm prog.
113 .. seealso:: `nvvmDestroyProgram`
114 """
115 cdef Program p = <Program>prog 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
116 with nogil: 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
117 status = nvvmDestroyProgram(&p) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
118 check_status(status) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
121cpdef str get_error_string(int result):
122 """Get the message string for the given ``nvvmResult`` code.
125 Args:
126 result (Result): NVVM API result code.
128 .. seealso:: `nvvmGetErrorString`
129 """
130 cdef const char *_output_cstr_
131 cdef bytes _output_
132 with nogil: 1,
133 _output_cstr_ = nvvmGetErrorString(<_Result>result) 1a,
134 _output_ = _output_cstr_ 1,
135 return _output_.decode() 1,
138cpdef tuple version():
139 """Get the NVVM version.
142 Returns:
143 A 2-tuple containing:
144 - int: NVVM major version number.
145 - int: NVVM minor version number.
147 .. seealso:: `nvvmVersion`
148 """
149 cdef int major
150 cdef int minor
151 with nogil: 1a5HIJKLMNOPQ
152 __status__ = nvvmVersion(&major, &minor) 1a5HIJKLMNOPQ
153 check_status(__status__) 1a5HIJKLMNOPQ
154 return (major, minor) 1a5HIJKLMNOPQ
157cpdef tuple ir_version():
158 """Get the NVVM IR version.
161 Returns:
162 A 4-tuple containing:
163 - int: NVVM IR major version number.
164 - int: NVVM IR minor version number.
165 - int: NVVM IR debug metadata major version number.
166 - int: NVVM IR debug metadata minor version number.
168 .. seealso:: `nvvmIRVersion`
169 """
170 cdef int major_ir
171 cdef int minor_ir
172 cdef int major_dbg
173 cdef int minor_dbg
174 with nogil: 1aSTUVWXYZ012346789!#$%'()*HIJKLMNOPQprqst
175 __status__ = nvvmIRVersion(&major_ir, &minor_ir, &major_dbg, &minor_dbg) 1aSTUVWXYZ012346789!#$%'()*HIJKLMNOPQprqst
176 check_status(__status__) 1aSTUVWXYZ012346789!#$%'()*HIJKLMNOPQprqst
177 return (major_ir, minor_ir, major_dbg, minor_dbg) 1aSTUVWXYZ012346789!#$%'()*HIJKLMNOPQprqst
180cpdef intptr_t create_program() except? 0:
181 """Create a program, and set the value of its handle to ``*prog``.
184 Returns:
185 intptr_t: NVVM program.
187 .. seealso:: `nvvmCreateProgram`
188 """
189 cdef Program prog
190 with nogil: 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
191 __status__ = nvvmCreateProgram(&prog) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
192 check_status(__status__) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
193 return <intptr_t>prog 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
196cpdef add_module_to_program(intptr_t prog, buffer, size_t size, name):
197 """Add a module level NVVM IR to a program.
199 The ``buffer`` should contain an NVVM IR module. The module should have NVVM IR
200 either in the LLVM 7.0.1 bitcode representation or in the LLVM 7.0.1 text
201 representation. Support for reading the text representation of NVVM IR is
202 deprecated and may be removed in a later version.
204 Args:
205 prog (intptr_t): NVVM program.
206 buffer (bytes): NVVM IR module in the bitcode or text
207 representation.
208 size (size_t): Size of the NVVM IR module.
209 name (str): Name of the NVVM IR module. If NULL, "<unnamed>" is
210 used as the name.
212 .. seealso:: `nvvmAddModuleToProgram`
213 """
214 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, size, readonly=True) 1aCjklmnobcdefghiuvwxyzprqst
215 if not isinstance(name, str): 1aCjklmnobcdefghiuvwxyzprqst
216 raise TypeError("name must be a Python str")
217 cdef bytes _temp_name_ = (<str>name).encode() 1aCjklmnobcdefghiuvwxyzprqst
218 cdef char* _name_ = _temp_name_ 1aCjklmnobcdefghiuvwxyzprqst
219 with nogil: 1aCjklmnobcdefghiuvwxyzprqst
220 __status__ = nvvmAddModuleToProgram(<Program>prog, <const char*>_buffer_, size, <const char*>_name_) 1aCjklmnobcdefghiuvwxyzprqst
221 check_status(__status__) 1aCjklmnobcdefghiuvwxyzprqst
224cpdef lazy_add_module_to_program(intptr_t prog, buffer, size_t size, name):
225 """Add a module level NVVM IR to a program.
227 The ``buffer`` should contain an NVVM IR module. The module should have NVVM IR
228 in the LLVM 7.0.1 bitcode representation.
229 A module added using this API is lazily loaded - the only symbols loaded are
230 those that are required by module(s) loaded using nvvmAddModuleToProgram. It is
231 an error for a program to have all modules loaded using this API. Compiler may
232 also optimize entities in this module by making them internal to the linked
233 NVVM IR module, making them eligible for other optimizations. Due to these
234 optimizations, this API to load a module is more efficient and should be used
235 where possible.
237 Args:
238 prog (intptr_t): NVVM program.
239 buffer (bytes): NVVM IR module in the bitcode representation.
240 size (size_t): Size of the NVVM IR module.
241 name (str): Name of the NVVM IR module. If NULL, "<unnamed>" is
242 used as the name.
244 .. seealso:: `nvvmLazyAddModuleToProgram`
245 """
246 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, size, readonly=True) 1D
247 if not isinstance(name, str): 1D
248 raise TypeError("name must be a Python str")
249 cdef bytes _temp_name_ = (<str>name).encode() 1D
250 cdef char* _name_ = _temp_name_ 1D
251 with nogil: 1D
252 __status__ = nvvmLazyAddModuleToProgram(<Program>prog, <const char*>_buffer_, size, <const char*>_name_) 1D
253 check_status(__status__) 1D
256cpdef compile_program(intptr_t prog, int num_options, options):
257 """Compile the NVVM program.
259 The NVVM IR modules in the program will be linked at the IR level. The linked
260 IR program is compiled to PTX.
261 The target datalayout in the linked IR program is used to determine the address
262 size (32bit vs 64bit).
263 The valid compiler options are:.
265 - -g (enable generation of full debugging information). Full debug support is
266 only valid with '-opt=0'. Debug support requires the input module to utilize
267 NVVM IR Debug Metadata. Line number (line info) only generation is also
268 enabled via NVVM IR Debug Metadata, there is no specific libNVVM API flag for
269 that case.
270 - -opt=.
271 - 0 (disable optimizations).
272 - 3 (default, enable optimizations).
273 - -arch=.
274 - compute_75 (default).
275 - compute_80.
276 - compute_87.
277 - compute_89.
278 - compute_90.
279 - compute_90a.
280 - compute_100.
281 - compute_100a.
282 - compute_100f.
283 - compute_103.
284 - compute_103a.
285 - compute_103f.
286 - compute_110.
287 - compute_110a.
288 - compute_110f.
289 - compute_120.
290 - compute_120a.
291 - compute_120f.
292 - compute_121.
293 - compute_121a.
294 - compute_121f.
295 - -ftz=.
296 - 0 (default, preserve denormal values, when performing single-precision
297 floating-point operations).
298 - 1 (flush denormal values to zero, when performing single-precision
299 floating-point operations).
300 - -prec-sqrt=.
301 - 0 (use a faster approximation for single-precision floating-point square
302 root).
303 - 1 (default, use IEEE round-to-nearest mode for single-precision floating-
304 point square root).
305 - -prec-div=.
306 - 0 (use a faster approximation for single-precision floating-point division
307 and reciprocals).
308 - 1 (default, use IEEE round-to-nearest mode for single-precision floating-
309 point division and reciprocals).
310 - -fma=.
311 - 0 (disable FMA contraction).
312 - 1 (default, enable FMA contraction).
313 - -jump-table-density=[0-101] Specify the case density percentage in switch
314 statements, and use it as a minimal threshold to determine whether jump
315 table(brx.idx instruction) will be used to implement a switch statement.
316 Default value is 101. The percentage ranges from 0 to 101 inclusively.
317 - -gen-lto (Generate LTO IR instead of PTX).
318 - -ptx-version-target=[86-94] Specify the target PTX version as (MAJOR VERSION
319 \* 10 + MINOR VERSION). This is supported only for Blackwell and later
320 architectures (compute capability compute_100 or greater). Using this option
321 implies that the operations in incoming ``prog`` are compliant with targeted
322 PTX version. If not set, highest available PTX version is used.
324 Args:
325 prog (intptr_t): NVVM program.
326 num_options (int): Number of compiler ``options`` passed.
327 options (object): Compiler options in the form of C string array. It can be:
329 - an :class:`int` as the pointer address to the nested sequence, or
330 - a Python sequence of :class:`int`\s, each of which is a pointer address
331 to a valid sequence of 'char', or
332 - a nested Python sequence of ``str``.
334 .. seealso:: `nvvmCompileProgram`
335 """
336 cdef nested_resource[ char ] _options_
337 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1ajlnEbcdefghiprqst
338 with nogil: 1ajlnEbcdefghiprqst
339 __status__ = nvvmCompileProgram(<Program>prog, num_options, <const char**>(_options_.ptrs.data())) 1ajlnEbcdefghiprqst
340 check_status(__status__) 1ajlnEbcdefghiprqst
343cpdef verify_program(intptr_t prog, int num_options, options):
344 """Verify the NVVM program.
346 The valid compiler options are:.
347 Same as for :func:`compile_program`.
349 Args:
350 prog (intptr_t): NVVM program.
351 num_options (int): Number of compiler ``options`` passed.
352 options (object): Compiler options in the form of C string array. It can be:
354 - an :class:`int` as the pointer address to the nested sequence, or
355 - a Python sequence of :class:`int`\s, each of which is a pointer address
356 to a valid sequence of 'char', or
357 - a nested Python sequence of ``str``.
359 .. seealso:: `nvvmVerifyProgram`
360 """
361 cdef nested_resource[ char ] _options_
362 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1kmoFuvwxyz
363 with nogil: 1kmoFuvwxyz
364 __status__ = nvvmVerifyProgram(<Program>prog, num_options, <const char**>(_options_.ptrs.data())) 1kmoFuvwxyz
365 check_status(__status__) 1kmoFuvwxyz
368cpdef size_t get_compiled_result_size(intptr_t prog) except? 0:
369 """Get the size of the compiled result.
372 Args:
373 prog (intptr_t): NVVM program.
375 Returns:
376 size_t: Size of the compiled result (including the trailing NULL).
378 .. seealso:: `nvvmGetCompiledResultSize`
379 """
380 cdef size_t buffer_size_ret
381 with nogil: 1bcdefghiA
382 __status__ = nvvmGetCompiledResultSize(<Program>prog, &buffer_size_ret) 1bcdefghiA
383 check_status(__status__) 1bcdefghiA
384 return buffer_size_ret 1bcdefghiA
387cpdef get_compiled_result(intptr_t prog, buffer):
388 """Get the compiled result.
390 The result is stored in the memory pointed to by ``buffer``.
392 Args:
393 prog (intptr_t): NVVM program.
394 buffer (bytes): Compiled result.
396 .. seealso:: `nvvmGetCompiledResult`
397 """
398 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, -1, readonly=False) 1bcdefghiA
399 with nogil: 1bcdefghiA
400 __status__ = nvvmGetCompiledResult(<Program>prog, <char*>_buffer_) 1bcdefghiA
401 check_status(__status__) 1bcdefghiA
404cpdef size_t get_program_log_size(intptr_t prog) except? 0:
405 """Get the Size of Compiler/Verifier Message.
407 The size of the message string (including the trailing NULL) is stored into
408 ``buffer_size_ret`` when the return value is NVVM_SUCCESS.
410 Args:
411 prog (intptr_t): NVVM program.
413 Returns:
414 size_t: Size of the compilation/verification log (including the
415 trailing NULL).
417 .. seealso:: `nvvmGetProgramLogSize`
418 """
419 cdef size_t buffer_size_ret
420 with nogil: 1jklmnobcdefghiB
421 __status__ = nvvmGetProgramLogSize(<Program>prog, &buffer_size_ret) 1jklmnobcdefghiB
422 check_status(__status__) 1jklmnobcdefghiB
423 return buffer_size_ret 1jklmnobcdefghiB
426cpdef get_program_log(intptr_t prog, buffer):
427 """Get the Compiler/Verifier Message.
429 The NULL terminated message string is stored in the memory pointed to by
430 ``buffer`` when the return value is NVVM_SUCCESS.
432 Args:
433 prog (intptr_t): NVVM program.
434 buffer (bytes): Compilation/Verification log.
436 .. seealso:: `nvvmGetProgramLog`
437 """
438 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, -1, readonly=False) 1jklmnobcdefghiB
439 with nogil: 1jklmnobcdefghiB
440 __status__ = nvvmGetProgramLog(<Program>prog, <char*>_buffer_) 1jklmnobcdefghiB
441 check_status(__status__) 1jklmnobcdefghiB
444cpdef int llvm_version(arch) except? 0:
445 """Get the LLVM IR version guaranteed to be supported by NVVM.
447 The valid arch strings are the ones supported by :func:`compile_program`.
449 Args:
450 arch (str): Architecture string.
452 Returns:
453 int: IR version number.
455 .. seealso:: `nvvmLLVMVersion`
456 """
457 if not isinstance(arch, str): 1R
458 raise TypeError("arch must be a Python str")
459 cdef bytes _temp_arch_ = (<str>arch).encode() 1R
460 cdef char* _arch_ = _temp_arch_ 1R
461 cdef int major
462 with nogil: 1R
463 __status__ = nvvmLLVMVersion(<const char*>_arch_, &major) 1R
464 check_status(__status__) 1R
465 return major 1R
466del _cyb_FastEnum