Coverage for cuda/bindings/nvvm.pyx: 91.04%
134 statements
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-03 01:06 +0000
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-03 01:06 +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.5.0. Do not modify it directly.
6# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=404e6696c1e11fe6b1854c8b341bfba192fb90689ae0a69b3110c86d7c51ac5f
9# <<<< PREAMBLE CONTENT >>>>
11cimport cpython as _cyb_cpython
12from cuda.bindings._internal.utils cimport (
13 get_nested_resource_ptr as _cyb_get_nested_resource_ptr,
14 nested_resource as _cyb_nested_resource,
15)
16from libc.stdint cimport intptr_t
18from cuda.bindings._internal._fast_enum import FastEnum as _cyb_FastEnum
20cdef intptr_t _cyb_get_buffer_pointer(buf, Py_ssize_t size, readonly=True) except?-1:
21 cdef intptr_t ptr
22 cdef int flags = _cyb_cpython.PyBUF_ANY_CONTIGUOUS 1aCDjklmnobcdefghiABuvwxyzprqst
23 if not readonly: 1aCDjklmnobcdefghiABuvwxyzprqst
24 flags |= _cyb_cpython.PyBUF_WRITABLE 1jklmnobcdefghiAB
25 cdef int status = -1 1aCDjklmnobcdefghiABuvwxyzprqst
26 cdef _cyb_cpython.Py_buffer view
27 if buf is None: 1aCDjklmnobcdefghiABuvwxyzprqst
28 ptr = 0 1CD
29 elif isinstance(buf, int): 1ajklmnobcdefghiABuvwxyzprqst
30 ptr = <intptr_t>buf
31 else:
32 try: 1ajklmnobcdefghiABuvwxyzprqst
33 status = _cyb_cpython.PyObject_GetBuffer(buf, &view, flags) 1ajklmnobcdefghiABuvwxyzprqst
34 if size != -1: 1ajklmnobcdefghiABuvwxyzprqst
35 assert view.len == size 1ajklmnobcdefghiuvwxyzprqst
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 contiguous {adj}"
41 f"buffer (a ctypes object, NumPy array, bytes, ...), of size {size}"
42 ) from e
43 else:
44 ptr = <intptr_t>view.buf 1ajklmnobcdefghiABuvwxyzprqst
45 finally:
46 if status == 0: 1ajklmnobcdefghiABuvwxyzprqst
47 _cyb_cpython.PyBuffer_Release(&view) 1ajklmnobcdefghiABuvwxyzprqst
48 return ptr 1aCDjklmnobcdefghiABuvwxyzprqst
51# <<<< END OF PREAMBLE CONTENT >>>>
53cimport cython # NOQA
55from ._internal.utils cimport (get_nested_resource_ptr,
56 nested_resource)
59###############################################################################
60# Enum
61###############################################################################
63class Result(_cyb_FastEnum):
64 """
65 NVVM API call result code.
67 See `nvvmResult`.
68 """
69 SUCCESS = NVVM_SUCCESS
70 ERROR_OUT_OF_MEMORY = NVVM_ERROR_OUT_OF_MEMORY
71 ERROR_PROGRAM_CREATION_FAILURE = NVVM_ERROR_PROGRAM_CREATION_FAILURE
72 ERROR_IR_VERSION_MISMATCH = NVVM_ERROR_IR_VERSION_MISMATCH
73 ERROR_INVALID_INPUT = NVVM_ERROR_INVALID_INPUT
74 ERROR_INVALID_PROGRAM = NVVM_ERROR_INVALID_PROGRAM
75 ERROR_INVALID_IR = NVVM_ERROR_INVALID_IR
76 ERROR_INVALID_OPTION = NVVM_ERROR_INVALID_OPTION
77 ERROR_NO_MODULE_IN_PROGRAM = NVVM_ERROR_NO_MODULE_IN_PROGRAM
78 ERROR_COMPILATION = NVVM_ERROR_COMPILATION
79 ERROR_CANCELLED = NVVM_ERROR_CANCELLED
82###############################################################################
83# Error handling
84###############################################################################
86class nvvmError(Exception):
88 def __init__(self, status):
89 self.status = status 1CDjklmnoEF+pq
90 s = Result(status) 1CDjklmnoEF+pq
91 cdef str err = f"{s.name} ({s.value})" 1CDjklmnoEF+pq
92 super(nvvmError, self).__init__(err) 1CDjklmnoEF+pq
94 def __reduce__(self):
95 return (type(self), (self.status,))
98@cython.profile(False)
99cdef int check_status(int status) except 1 nogil:
100 if status != 0: 1aCDjSkTlUmVnoEFbWcXdYeZf0g1h2i3GAB4R5u6v7w8x9y!z#$%'()*HIJKLMNOPQprqst
101 with gil: 1CDjklmnoEFpq
102 raise nvvmError(status) 1CDjklmnoEFpq
103 return status 1aCDjSkTlUmVnoEFbWcXdYeZf0g1h2i3GAB4R5u6v7w8x9y!z#$%'()*HIJKLMNOPQprqst
106###############################################################################
107# Wrapper functions
108###############################################################################
110cpdef destroy_program(intptr_t prog):
111 """Destroy a program.
113 Args:
114 prog (intptr_t): nvvm prog.
116 .. seealso:: `nvvmDestroyProgram`
117 """
118 cdef Program p = <Program>prog 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
119 with nogil: 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
120 status = nvvmDestroyProgram(&p) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
121 check_status(status) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
124cpdef str get_error_string(int result):
125 """Get the message string for the given ``nvvmResult`` code.
128 Args:
129 result (Result): NVVM API result code.
131 .. seealso:: `nvvmGetErrorString`
132 """
133 cdef const char *_output_cstr_
134 cdef bytes _output_
135 with nogil: 1,
136 _output_cstr_ = nvvmGetErrorString(<_Result>result) 1,
137 _output_ = _output_cstr_ 1,
138 return _output_.decode() 1,
141cpdef tuple version():
142 """Get the NVVM version.
145 Returns:
146 A 2-tuple containing:
147 - int: NVVM major version number.
148 - int: NVVM minor version number.
150 .. seealso:: `nvvmVersion`
151 """
152 cdef int major
153 cdef int minor
154 with nogil: 1a5HIJKLMNOPQ
155 __status__ = nvvmVersion(&major, &minor) 1a5HIJKLMNOPQ
156 check_status(__status__) 1a5HIJKLMNOPQ
157 return (major, minor) 1a5HIJKLMNOPQ
160cpdef tuple ir_version():
161 """Get the NVVM IR version.
164 Returns:
165 A 4-tuple containing:
166 - int: NVVM IR major version number.
167 - int: NVVM IR minor version number.
168 - int: NVVM IR debug metadata major version number.
169 - int: NVVM IR debug metadata minor version number.
171 .. seealso:: `nvvmIRVersion`
172 """
173 cdef int major_ir
174 cdef int minor_ir
175 cdef int major_dbg
176 cdef int minor_dbg
177 with nogil: 1aSTUVWXYZ012346789!#$%'()*HIJKLMNOPQprqst
178 __status__ = nvvmIRVersion(&major_ir, &minor_ir, &major_dbg, &minor_dbg) 1aSTUVWXYZ012346789!#$%'()*HIJKLMNOPQprqst
179 check_status(__status__) 1aSTUVWXYZ012346789!#$%'()*HIJKLMNOPQprqst
180 return (major_ir, minor_ir, major_dbg, minor_dbg) 1aSTUVWXYZ012346789!#$%'()*HIJKLMNOPQprqst
183cpdef intptr_t create_program() except? 0:
184 """Create a program, and set the value of its handle to ``*prog``.
187 Returns:
188 intptr_t: NVVM program.
190 .. seealso:: `nvvmCreateProgram`
191 """
192 cdef Program prog
193 with nogil: 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
194 __status__ = nvvmCreateProgram(&prog) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
195 check_status(__status__) 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
196 return <intptr_t>prog 1aCDjklmnoEFbcdefghiGABuvwxyzprqst
199cpdef add_module_to_program(intptr_t prog, buffer, size_t size, name):
200 """Add a module level NVVM IR to a program.
202 The ``buffer`` should contain an NVVM IR module. The module should have NVVM IR
203 either in the LLVM 7.0.1 bitcode representation or in the LLVM 7.0.1 text
204 representation. Support for reading the text representation of NVVM IR is
205 deprecated and may be removed in a later version.
207 Args:
208 prog (intptr_t): NVVM program.
209 buffer (bytes): NVVM IR module in the bitcode or text
210 representation.
211 size (size_t): Size of the NVVM IR module.
212 name (str): Name of the NVVM IR module. If NULL, "<unnamed>" is
213 used as the name.
215 .. seealso:: `nvvmAddModuleToProgram`
216 """
217 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, size, readonly=True) 1aCjklmnobcdefghiuvwxyzprqst
218 if not isinstance(name, str): 1aCjklmnobcdefghiuvwxyzprqst
219 raise TypeError("name must be a Python str")
220 cdef bytes _temp_name_ = (<str>name).encode() 1aCjklmnobcdefghiuvwxyzprqst
221 cdef char* _name_ = _temp_name_ 1aCjklmnobcdefghiuvwxyzprqst
222 with nogil: 1aCjklmnobcdefghiuvwxyzprqst
223 __status__ = nvvmAddModuleToProgram(<Program>prog, <const char*>_buffer_, size, <const char*>_name_) 1aCjklmnobcdefghiuvwxyzprqst
224 check_status(__status__) 1aCjklmnobcdefghiuvwxyzprqst
227cpdef lazy_add_module_to_program(intptr_t prog, buffer, size_t size, name):
228 """Add a module level NVVM IR to a program.
230 The ``buffer`` should contain an NVVM IR module. The module should have NVVM IR
231 in the LLVM 7.0.1 bitcode representation.
232 A module added using this API is lazily loaded - the only symbols loaded are
233 those that are required by module(s) loaded using nvvmAddModuleToProgram. It is
234 an error for a program to have all modules loaded using this API. Compiler may
235 also optimize entities in this module by making them internal to the linked
236 NVVM IR module, making them eligible for other optimizations. Due to these
237 optimizations, this API to load a module is more efficient and should be used
238 where possible.
240 Args:
241 prog (intptr_t): NVVM program.
242 buffer (bytes): NVVM IR module in the bitcode representation.
243 size (size_t): Size of the NVVM IR module.
244 name (str): Name of the NVVM IR module. If NULL, "<unnamed>" is
245 used as the name.
247 .. seealso:: `nvvmLazyAddModuleToProgram`
248 """
249 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, size, readonly=True) 1D
250 if not isinstance(name, str): 1D
251 raise TypeError("name must be a Python str")
252 cdef bytes _temp_name_ = (<str>name).encode() 1D
253 cdef char* _name_ = _temp_name_ 1D
254 with nogil: 1D
255 __status__ = nvvmLazyAddModuleToProgram(<Program>prog, <const char*>_buffer_, size, <const char*>_name_) 1D
256 check_status(__status__) 1D
259cpdef compile_program(intptr_t prog, int num_options, options):
260 """Compile the NVVM program.
262 The NVVM IR modules in the program will be linked at the IR level. The linked
263 IR program is compiled to PTX.
264 The target datalayout in the linked IR program is used to determine the address
265 size (32bit vs 64bit).
266 The valid compiler options are:.
268 - -g (enable generation of full debugging information). Full debug support is
269 only valid with '-opt=0'. Debug support requires the input module to utilize
270 NVVM IR Debug Metadata. Line number (line info) only generation is also
271 enabled via NVVM IR Debug Metadata, there is no specific libNVVM API flag for
272 that case.
273 - -opt=.
274 - 0 (disable optimizations).
275 - 3 (default, enable optimizations).
276 - -arch=.
277 - compute_75 (default).
278 - compute_80.
279 - compute_87.
280 - compute_89.
281 - compute_90.
282 - compute_90a.
283 - compute_100.
284 - compute_100a.
285 - compute_100f.
286 - compute_103.
287 - compute_103a.
288 - compute_103f.
289 - compute_110.
290 - compute_110a.
291 - compute_110f.
292 - compute_120.
293 - compute_120a.
294 - compute_120f.
295 - compute_121.
296 - compute_121a.
297 - compute_121f.
298 - -ftz=.
299 - 0 (default, preserve denormal values, when performing single-precision
300 floating-point operations).
301 - 1 (flush denormal values to zero, when performing single-precision
302 floating-point operations).
303 - -prec-sqrt=.
304 - 0 (use a faster approximation for single-precision floating-point square
305 root).
306 - 1 (default, use IEEE round-to-nearest mode for single-precision
307 floating-point square root).
308 - -prec-div=.
309 - 0 (use a faster approximation for single-precision floating-point division
310 and reciprocals).
311 - 1 (default, use IEEE round-to-nearest mode for single-precision
312 floating-point division and reciprocals).
313 - -fma=.
314 - 0 (disable FMA contraction).
315 - 1 (default, enable FMA contraction).
316 - -jump-table-density=[0-101] Specify the case density percentage in switch
317 statements, and use it as a minimal threshold to determine whether jump
318 table(brx.idx instruction) will be used to implement a switch statement.
319 Default value is 101. The percentage ranges from 0 to 101 inclusively.
320 - -gen-lto (Generate LTO IR instead of PTX).
321 - -ptx-version-target=[86-94] Specify the target PTX version as (MAJOR VERSION
322 \* 10 + MINOR VERSION). This is supported only for Blackwell and later
323 architectures (compute capability compute_100 or greater). Using this option
324 implies that the operations in incoming ``prog`` are compliant with targeted
325 PTX version. If not set, highest available PTX version is used.
326 - -nvvm-latest Use the most recent LLVM-based NVVM release. Previews this newer
327 NVVM on all GPU architectures. No-op for Blackwell and later where it is
328 already the default.
330 Args:
331 prog (intptr_t): NVVM program.
332 num_options (int): Number of compiler ``options`` passed.
333 options (object): Compiler options in the form of C string array. It can be:
335 - an :class:`int` as the pointer address to the nested sequence, or
336 - a Python sequence of :class:`int`\s, each of which is a pointer address
337 to a valid sequence of 'char', or
338 - a nested Python sequence of ``str``.
340 .. seealso:: `nvvmCompileProgram`
341 """
342 cdef _cyb_nested_resource[ char ] _options_
343 _cyb_get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1ajlnEbcdefghiprqst
344 with nogil: 1ajlnEbcdefghiprqst
345 __status__ = nvvmCompileProgram(<Program>prog, num_options, <const char**>(_options_.ptrs.data())) 1ajlnEbcdefghiprqst
346 check_status(__status__) 1ajlnEbcdefghiprqst
349cpdef verify_program(intptr_t prog, int num_options, options):
350 """Verify the NVVM program.
352 The valid compiler options are:.
353 Same as for :func:`compile_program`.
355 Args:
356 prog (intptr_t): NVVM program.
357 num_options (int): Number of compiler ``options`` passed.
358 options (object): Compiler options in the form of C string array. It can be:
360 - an :class:`int` as the pointer address to the nested sequence, or
361 - a Python sequence of :class:`int`\s, each of which is a pointer address
362 to a valid sequence of 'char', or
363 - a nested Python sequence of ``str``.
365 .. seealso:: `nvvmVerifyProgram`
366 """
367 cdef _cyb_nested_resource[ char ] _options_
368 _cyb_get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1kmoFuvwxyz
369 with nogil: 1kmoFuvwxyz
370 __status__ = nvvmVerifyProgram(<Program>prog, num_options, <const char**>(_options_.ptrs.data())) 1kmoFuvwxyz
371 check_status(__status__) 1kmoFuvwxyz
374cpdef size_t get_compiled_result_size(intptr_t prog) except? 0:
375 """Get the size of the compiled result.
378 Args:
379 prog (intptr_t): NVVM program.
381 Returns:
382 size_t: Size of the compiled result (including the trailing NULL).
384 .. seealso:: `nvvmGetCompiledResultSize`
385 """
386 cdef size_t buffer_size_ret
387 with nogil: 1bcdefghiA
388 __status__ = nvvmGetCompiledResultSize(<Program>prog, &buffer_size_ret) 1bcdefghiA
389 check_status(__status__) 1bcdefghiA
390 return buffer_size_ret 1bcdefghiA
393cpdef get_compiled_result(intptr_t prog, buffer):
394 """Get the compiled result.
396 The result is stored in the memory pointed to by ``buffer``.
398 Args:
399 prog (intptr_t): NVVM program.
400 buffer (bytes): Compiled result.
402 .. seealso:: `nvvmGetCompiledResult`
403 """
404 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, -1, readonly=False) 1bcdefghiA
405 with nogil: 1bcdefghiA
406 __status__ = nvvmGetCompiledResult(<Program>prog, <char*>_buffer_) 1bcdefghiA
407 check_status(__status__) 1bcdefghiA
410cpdef size_t get_program_log_size(intptr_t prog) except? 0:
411 """Get the Size of Compiler/Verifier Message.
413 The size of the message string (including the trailing NULL) is stored into
414 ``buffer_size_ret`` when the return value is NVVM_SUCCESS.
416 Args:
417 prog (intptr_t): NVVM program.
419 Returns:
420 size_t: Size of the compilation/verification log (including the
421 trailing NULL).
423 .. seealso:: `nvvmGetProgramLogSize`
424 """
425 cdef size_t buffer_size_ret
426 with nogil: 1jklmnobcdefghiB
427 __status__ = nvvmGetProgramLogSize(<Program>prog, &buffer_size_ret) 1jklmnobcdefghiB
428 check_status(__status__) 1jklmnobcdefghiB
429 return buffer_size_ret 1jklmnobcdefghiB
432cpdef get_program_log(intptr_t prog, buffer):
433 """Get the Compiler/Verifier Message.
435 The NULL terminated message string is stored in the memory pointed to by
436 ``buffer`` when the return value is NVVM_SUCCESS.
438 Args:
439 prog (intptr_t): NVVM program.
440 buffer (bytes): Compilation/Verification log.
442 .. seealso:: `nvvmGetProgramLog`
443 """
444 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, -1, readonly=False) 1jklmnobcdefghiB
445 with nogil: 1jklmnobcdefghiB
446 __status__ = nvvmGetProgramLog(<Program>prog, <char*>_buffer_) 1jklmnobcdefghiB
447 check_status(__status__) 1jklmnobcdefghiB
450cpdef int llvm_version(arch) except? 0:
451 """Get the LLVM IR version guaranteed to be supported by NVVM.
453 The valid arch strings are the ones supported by :func:`compile_program`.
455 Args:
456 arch (str): Architecture string.
458 Returns:
459 int: LLVM IR version number.
461 .. seealso:: `nvvmLLVMVersion`
462 """
463 if not isinstance(arch, str): 1R
464 raise TypeError("arch must be a Python str")
465 cdef bytes _temp_arch_ = (<str>arch).encode() 1R
466 cdef char* _arch_ = _temp_arch_ 1R
467 cdef int major
468 with nogil: 1R
469 __status__ = nvvmLLVMVersion(<const char*>_arch_, &major) 1R
470 check_status(__status__) 1R
471 return major 1R
472del _cyb_FastEnum