Coverage for cuda / bindings / nvvm.pyx: 90.82%
98 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-08 01:07 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-08 01:07 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2#
3# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
4#
5# This code was automatically generated across versions from 12.0.1 to 13.1.1, generator version 0.3.1.dev1322+g646ce84ec. Do not modify it directly.
7cimport cython # NOQA
9from ._internal.utils cimport (get_buffer_pointer, get_nested_resource_ptr,
10 nested_resource)
12from cuda.bindings._internal._fast_enum import FastEnum as _IntEnum
15###############################################################################
16# Enum
17###############################################################################
19class Result(_IntEnum):
20 """
21 NVVM API call result code.
23 See `nvvmResult`.
24 """
25 SUCCESS = NVVM_SUCCESS
26 ERROR_OUT_OF_MEMORY = NVVM_ERROR_OUT_OF_MEMORY
27 ERROR_PROGRAM_CREATION_FAILURE = NVVM_ERROR_PROGRAM_CREATION_FAILURE
28 ERROR_IR_VERSION_MISMATCH = NVVM_ERROR_IR_VERSION_MISMATCH
29 ERROR_INVALID_INPUT = NVVM_ERROR_INVALID_INPUT
30 ERROR_INVALID_PROGRAM = NVVM_ERROR_INVALID_PROGRAM
31 ERROR_INVALID_IR = NVVM_ERROR_INVALID_IR
32 ERROR_INVALID_OPTION = NVVM_ERROR_INVALID_OPTION
33 ERROR_NO_MODULE_IN_PROGRAM = NVVM_ERROR_NO_MODULE_IN_PROGRAM
34 ERROR_COMPILATION = NVVM_ERROR_COMPILATION
35 ERROR_CANCELLED = NVVM_ERROR_CANCELLED
38###############################################################################
39# Error handling
40###############################################################################
42class nvvmError(Exception):
44 def __init__(self, status):
45 self.status = status 1hijklmtu
46 s = Result(status) 1hijklmtu
47 cdef str err = f"{s.name} ({s.value})" 1hijklmtu
48 super(nvvmError, self).__init__(err) 1hijklmtu
50 def __reduce__(self):
51 return (type(self), (self.status,))
54@cython.profile(False)
55cdef int check_status(int status) except 1 nogil:
56 if status != 0: 1axyhAiBjCkDlmtubEcFdGeHfIgJzvwKLnMoNpOqPrQsRSTUVWX
57 with gil: 1hijklmtu
58 raise nvvmError(status) 1hijklmtu
59 return status 1axyhAiBjCkDlmtubEcFdGeHfIgJzvwKLnMoNpOqPrQsRSTUVWX
62###############################################################################
63# Wrapper functions
64###############################################################################
66cpdef destroy_program(intptr_t prog):
67 """Destroy a program.
69 Args:
70 prog (intptr_t): nvvm prog.
72 .. seealso:: `nvvmDestroyProgram`
73 """
74 cdef Program p = <Program>prog 1axyhijklmtubcdefgzvwnopqrs
75 with nogil: 1axyhijklmtubcdefgzvwnopqrs
76 status = nvvmDestroyProgram(&p) 1axyhijklmtubcdefgzvwnopqrs
77 check_status(status) 1axyhijklmtubcdefgzvwnopqrs
80cpdef str get_error_string(int result):
81 """Get the message string for the given ``nvvmResult`` code.
83 Args:
84 result (Result): NVVM API result code.
86 .. seealso:: `nvvmGetErrorString`
87 """
88 cdef bytes _output_
89 _output_ = nvvmGetErrorString(<_Result>result) 1Y
90 return _output_.decode() 1Y
93cpdef tuple version():
94 """Get the NVVM version.
96 Returns:
97 A 2-tuple containing:
99 - int: NVVM major version number.
100 - int: NVVM minor version number.
102 .. seealso:: `nvvmVersion`
103 """
104 cdef int major
105 cdef int minor
106 with nogil: 1aL
107 __status__ = nvvmVersion(&major, &minor) 1aL
108 check_status(__status__) 1aL
109 return (major, minor) 1aL
112cpdef tuple ir_version():
113 """Get the NVVM IR version.
115 Returns:
116 A 4-tuple containing:
118 - int: NVVM IR major version number.
119 - int: NVVM IR minor version number.
120 - int: NVVM IR debug metadata major version number.
121 - int: NVVM IR debug metadata minor version number.
123 .. seealso:: `nvvmIRVersion`
124 """
125 cdef int major_ir
126 cdef int minor_ir
127 cdef int major_dbg
128 cdef int minor_dbg
129 with nogil: 1aABCDEFGHIJKMNOPQRSTUVWX
130 __status__ = nvvmIRVersion(&major_ir, &minor_ir, &major_dbg, &minor_dbg) 1aABCDEFGHIJKMNOPQRSTUVWX
131 check_status(__status__) 1aABCDEFGHIJKMNOPQRSTUVWX
132 return (major_ir, minor_ir, major_dbg, minor_dbg) 1aABCDEFGHIJKMNOPQRSTUVWX
135cpdef intptr_t create_program() except? 0:
136 """Create a program, and set the value of its handle to ``*prog``.
138 Returns:
139 intptr_t: NVVM program.
141 .. seealso:: `nvvmCreateProgram`
142 """
143 cdef Program prog
144 with nogil: 1axyhijklmtubcdefgzvwnopqrs
145 __status__ = nvvmCreateProgram(&prog) 1axyhijklmtubcdefgzvwnopqrs
146 check_status(__status__) 1axyhijklmtubcdefgzvwnopqrs
147 return <intptr_t>prog 1axyhijklmtubcdefgzvwnopqrs
150cpdef add_module_to_program(intptr_t prog, buffer, size_t size, name):
151 """Add a module level NVVM IR to a program.
153 Args:
154 prog (intptr_t): NVVM program.
155 buffer (bytes): NVVM IR module in the bitcode or text representation.
156 size (size_t): Size of the NVVM IR module.
157 name (str): Name of the NVVM IR module. If NULL, "<unnamed>" is used as the name.
159 .. seealso:: `nvvmAddModuleToProgram`
160 """
161 cdef void* _buffer_ = get_buffer_pointer(buffer, size, readonly=True) 1axhijklmbcdefgnopqrs
162 if not isinstance(name, str): 1ahijklmbcdefgnopqrs
163 raise TypeError("name must be a Python str")
164 cdef bytes _temp_name_ = (<str>name).encode() 1ahijklmbcdefgnopqrs
165 cdef char* _name_ = _temp_name_ 1ahijklmbcdefgnopqrs
166 with nogil: 1ahijklmbcdefgnopqrs
167 __status__ = nvvmAddModuleToProgram(<Program>prog, <const char*>_buffer_, size, <const char*>_name_) 1ahijklmbcdefgnopqrs
168 check_status(__status__) 1ahijklmbcdefgnopqrs
171cpdef lazy_add_module_to_program(intptr_t prog, buffer, size_t size, name):
172 """Add a module level NVVM IR to a program.
174 Args:
175 prog (intptr_t): NVVM program.
176 buffer (bytes): NVVM IR module in the bitcode representation.
177 size (size_t): Size of the NVVM IR module.
178 name (str): Name of the NVVM IR module. If NULL, "<unnamed>" is used as the name.
180 .. seealso:: `nvvmLazyAddModuleToProgram`
181 """
182 cdef void* _buffer_ = get_buffer_pointer(buffer, size, readonly=True) 1y
183 if not isinstance(name, str):
184 raise TypeError("name must be a Python str")
185 cdef bytes _temp_name_ = (<str>name).encode()
186 cdef char* _name_ = _temp_name_
187 with nogil:
188 __status__ = nvvmLazyAddModuleToProgram(<Program>prog, <const char*>_buffer_, size, <const char*>_name_)
189 check_status(__status__)
192cpdef compile_program(intptr_t prog, int num_options, options):
193 """Compile the NVVM program.
195 Args:
196 prog (intptr_t): NVVM program.
197 num_options (int): Number of compiler ``options`` passed.
198 options (object): Compiler options in the form of C string array. It can be:
200 - an :class:`int` as the pointer address to the nested sequence, or
201 - a Python sequence of :class:`int`\s, each of which is a pointer address
202 to a valid sequence of 'char', or
203 - a nested Python sequence of ``str``.
206 .. seealso:: `nvvmCompileProgram`
207 """
208 cdef nested_resource[ char ] _options_
209 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1ahjltbcdefg
210 with nogil: 1ahjltbcdefg
211 __status__ = nvvmCompileProgram(<Program>prog, num_options, <const char**>(_options_.ptrs.data())) 1ahjltbcdefg
212 check_status(__status__) 1ahjltbcdefg
215cpdef verify_program(intptr_t prog, int num_options, options):
216 """Verify the NVVM program.
218 Args:
219 prog (intptr_t): NVVM program.
220 num_options (int): Number of compiler ``options`` passed.
221 options (object): Compiler options in the form of C string array. It can be:
223 - an :class:`int` as the pointer address to the nested sequence, or
224 - a Python sequence of :class:`int`\s, each of which is a pointer address
225 to a valid sequence of 'char', or
226 - a nested Python sequence of ``str``.
229 .. seealso:: `nvvmVerifyProgram`
230 """
231 cdef nested_resource[ char ] _options_
232 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1aikmunopqrs
233 with nogil: 1aikmunopqrs
234 __status__ = nvvmVerifyProgram(<Program>prog, num_options, <const char**>(_options_.ptrs.data())) 1aikmunopqrs
235 check_status(__status__) 1aikmunopqrs
238cpdef size_t get_compiled_result_size(intptr_t prog) except? 0:
239 """Get the size of the compiled result.
241 Args:
242 prog (intptr_t): NVVM program.
244 Returns:
245 size_t: Size of the compiled result (including the trailing NULL).
247 .. seealso:: `nvvmGetCompiledResultSize`
248 """
249 cdef size_t buffer_size_ret
250 with nogil: 1abcdefgv
251 __status__ = nvvmGetCompiledResultSize(<Program>prog, &buffer_size_ret) 1abcdefgv
252 check_status(__status__) 1abcdefgv
253 return buffer_size_ret 1abcdefgv
256cpdef get_compiled_result(intptr_t prog, buffer):
257 """Get the compiled result.
259 Args:
260 prog (intptr_t): NVVM program.
261 buffer (bytes): Compiled result.
263 .. seealso:: `nvvmGetCompiledResult`
264 """
265 cdef void* _buffer_ = get_buffer_pointer(buffer, -1, readonly=False) 1abcdefgv
266 with nogil: 1abcdefgv
267 __status__ = nvvmGetCompiledResult(<Program>prog, <char*>_buffer_) 1abcdefgv
268 check_status(__status__) 1abcdefgv
271cpdef size_t get_program_log_size(intptr_t prog) except? 0:
272 """Get the Size of Compiler/Verifier Message.
274 Args:
275 prog (intptr_t): NVVM program.
277 Returns:
278 size_t: Size of the compilation/verification log (including the trailing NULL).
280 .. seealso:: `nvvmGetProgramLogSize`
281 """
282 cdef size_t buffer_size_ret
283 with nogil: 1hijklmbcdefgw
284 __status__ = nvvmGetProgramLogSize(<Program>prog, &buffer_size_ret) 1hijklmbcdefgw
285 check_status(__status__) 1hijklmbcdefgw
286 return buffer_size_ret 1hijklmbcdefgw
289cpdef get_program_log(intptr_t prog, buffer):
290 """Get the Compiler/Verifier Message.
292 Args:
293 prog (intptr_t): NVVM program.
294 buffer (bytes): Compilation/Verification log.
296 .. seealso:: `nvvmGetProgramLog`
297 """
298 cdef void* _buffer_ = get_buffer_pointer(buffer, -1, readonly=False) 1hijklmbcdefgw
299 with nogil: 1hijklmbcdefgw
300 __status__ = nvvmGetProgramLog(<Program>prog, <char*>_buffer_) 1hijklmbcdefgw
301 check_status(__status__) 1hijklmbcdefgw