Coverage for cuda / bindings / nvvm.pyx: 90.65%

107 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-25 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.2.0, generator version 0.3.1.dev1406+gd8426ea19.d20260316. Do not modify it directly. 

6  

7cimport cython # NOQA 

8  

9from ._internal.utils cimport (get_buffer_pointer, get_nested_resource_ptr, 

10 nested_resource) 

11  

12from cuda.bindings._internal._fast_enum import FastEnum as _IntEnum 

13  

14  

15############################################################################### 

16# Enum 

17############################################################################### 

18  

19class Result(_IntEnum): 

20 """ 

21 NVVM API call result code. 

22  

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 

36  

37  

38############################################################################### 

39# Error handling 

40############################################################################### 

41  

42class nvvmError(Exception): 

43  

44 def __init__(self, status): 

45 self.status = status 1jklmnovw2

46 s = Result(status) 1jklmnovw2

47 cdef str err = f"{s.name} ({s.value})" 1jklmnovw2

48 super(nvvmError, self).__init__(err) 1jklmnovw2

49  

50 def __reduce__(self): 

51 return (type(self), (self.status,)) 

52  

53  

54@cython.profile(False) 

55cdef int check_status(int status) except 1 nogil: 

56 if status != 0: 1azAjDkElFmGnovwbHcIdJeKfLgMhNiOBxyPCQpRqSrTsUtVuWXYZ01

57 with gil: 1jklmnovw

58 raise nvvmError(status) 1jklmnovw

59 return status 1azAjDkElFmGnovwbHcIdJeKfLgMhNiOBxyPCQpRqSrTsUtVuWXYZ01

60  

61  

62############################################################################### 

63# Wrapper functions 

64############################################################################### 

65  

66cpdef destroy_program(intptr_t prog): 

67 """Destroy a program. 

68  

69 Args: 

70 prog (intptr_t): nvvm prog. 

71  

72 .. seealso:: `nvvmDestroyProgram` 

73 """ 

74 cdef Program p = <Program>prog 1azAjklmnovwbcdefghiBxypqrstu

75 with nogil: 1azAjklmnovwbcdefghiBxypqrstu

76 status = nvvmDestroyProgram(&p) 1azAjklmnovwbcdefghiBxypqrstu

77 check_status(status) 1azAjklmnovwbcdefghiBxypqrstu

78  

79  

80cpdef str get_error_string(int result): 

81 """Get the message string for the given ``nvvmResult`` code. 

82  

83 Args: 

84 result (Result): NVVM API result code. 

85  

86 .. seealso:: `nvvmGetErrorString` 

87 """ 

88 cdef bytes _output_ 

89 _output_ = nvvmGetErrorString(<_Result>result) 13

90 return _output_.decode() 13

91  

92  

93cpdef tuple version(): 

94 """Get the NVVM version. 

95  

96 Returns: 

97 A 2-tuple containing: 

98  

99 - int: NVVM major version number. 

100 - int: NVVM minor version number. 

101  

102 .. seealso:: `nvvmVersion` 

103 """ 

104 cdef int major 

105 cdef int minor 

106 with nogil: 1aQ

107 __status__ = nvvmVersion(&major, &minor) 1aQ

108 check_status(__status__) 1aQ

109 return (major, minor) 1aQ

110  

111  

112cpdef tuple ir_version(): 

113 """Get the NVVM IR version. 

114  

115 Returns: 

116 A 4-tuple containing: 

117  

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. 

122  

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: 1aDEFGHIJKLMNOPRSTUVWXYZ01

130 __status__ = nvvmIRVersion(&major_ir, &minor_ir, &major_dbg, &minor_dbg) 1aDEFGHIJKLMNOPRSTUVWXYZ01

131 check_status(__status__) 1aDEFGHIJKLMNOPRSTUVWXYZ01

132 return (major_ir, minor_ir, major_dbg, minor_dbg) 1aDEFGHIJKLMNOPRSTUVWXYZ01

133  

134  

135cpdef intptr_t create_program() except? 0: 

136 """Create a program, and set the value of its handle to ``*prog``. 

137  

138 Returns: 

139 intptr_t: NVVM program. 

140  

141 .. seealso:: `nvvmCreateProgram` 

142 """ 

143 cdef Program prog 

144 with nogil: 1azAjklmnovwbcdefghiBxypqrstu

145 __status__ = nvvmCreateProgram(&prog) 1azAjklmnovwbcdefghiBxypqrstu

146 check_status(__status__) 1azAjklmnovwbcdefghiBxypqrstu

147 return <intptr_t>prog 1azAjklmnovwbcdefghiBxypqrstu

148  

149  

150cpdef add_module_to_program(intptr_t prog, buffer, size_t size, name): 

151 """Add a module level NVVM IR to a program. 

152  

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. 

158  

159 .. seealso:: `nvvmAddModuleToProgram` 

160 """ 

161 cdef void* _buffer_ = get_buffer_pointer(buffer, size, readonly=True) 1azjklmnobcdefghipqrstu

162 if not isinstance(name, str): 1ajklmnobcdefghipqrstu

163 raise TypeError("name must be a Python str") 

164 cdef bytes _temp_name_ = (<str>name).encode() 1ajklmnobcdefghipqrstu

165 cdef char* _name_ = _temp_name_ 1ajklmnobcdefghipqrstu

166 with nogil: 1ajklmnobcdefghipqrstu

167 __status__ = nvvmAddModuleToProgram(<Program>prog, <const char*>_buffer_, size, <const char*>_name_) 1ajklmnobcdefghipqrstu

168 check_status(__status__) 1ajklmnobcdefghipqrstu

169  

170  

171cpdef lazy_add_module_to_program(intptr_t prog, buffer, size_t size, name): 

172 """Add a module level NVVM IR to a program. 

173  

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. 

179  

180 .. seealso:: `nvvmLazyAddModuleToProgram` 

181 """ 

182 cdef void* _buffer_ = get_buffer_pointer(buffer, size, readonly=True) 1A

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__) 

190  

191  

192cpdef compile_program(intptr_t prog, int num_options, options): 

193 """Compile the NVVM program. 

194  

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: 

199  

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``. 

204  

205  

206 .. seealso:: `nvvmCompileProgram` 

207 """ 

208 cdef nested_resource[ char ] _options_ 

209 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1ajlnvbcdefghi

210 with nogil: 1ajlnvbcdefghi

211 __status__ = nvvmCompileProgram(<Program>prog, num_options, <const char**>(_options_.ptrs.data())) 1ajlnvbcdefghi

212 check_status(__status__) 1ajlnvbcdefghi

213  

214  

215cpdef verify_program(intptr_t prog, int num_options, options): 

216 """Verify the NVVM program. 

217  

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: 

222  

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``. 

227  

228  

229 .. seealso:: `nvvmVerifyProgram` 

230 """ 

231 cdef nested_resource[ char ] _options_ 

232 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1akmowpqrstu

233 with nogil: 1akmowpqrstu

234 __status__ = nvvmVerifyProgram(<Program>prog, num_options, <const char**>(_options_.ptrs.data())) 1akmowpqrstu

235 check_status(__status__) 1akmowpqrstu

236  

237  

238cpdef size_t get_compiled_result_size(intptr_t prog) except? 0: 

239 """Get the size of the compiled result. 

240  

241 Args: 

242 prog (intptr_t): NVVM program. 

243  

244 Returns: 

245 size_t: Size of the compiled result (including the trailing NULL). 

246  

247 .. seealso:: `nvvmGetCompiledResultSize` 

248 """ 

249 cdef size_t buffer_size_ret 

250 with nogil: 1abcdefghix

251 __status__ = nvvmGetCompiledResultSize(<Program>prog, &buffer_size_ret) 1abcdefghix

252 check_status(__status__) 1abcdefghix

253 return buffer_size_ret 1abcdefghix

254  

255  

256cpdef get_compiled_result(intptr_t prog, buffer): 

257 """Get the compiled result. 

258  

259 Args: 

260 prog (intptr_t): NVVM program. 

261 buffer (bytes): Compiled result. 

262  

263 .. seealso:: `nvvmGetCompiledResult` 

264 """ 

265 cdef void* _buffer_ = get_buffer_pointer(buffer, -1, readonly=False) 1abcdefghix

266 with nogil: 1abcdefghix

267 __status__ = nvvmGetCompiledResult(<Program>prog, <char*>_buffer_) 1abcdefghix

268 check_status(__status__) 1abcdefghix

269  

270  

271cpdef size_t get_program_log_size(intptr_t prog) except? 0: 

272 """Get the Size of Compiler/Verifier Message. 

273  

274 Args: 

275 prog (intptr_t): NVVM program. 

276  

277 Returns: 

278 size_t: Size of the compilation/verification log (including the trailing NULL). 

279  

280 .. seealso:: `nvvmGetProgramLogSize` 

281 """ 

282 cdef size_t buffer_size_ret 

283 with nogil: 1jklmnobcdefghiy

284 __status__ = nvvmGetProgramLogSize(<Program>prog, &buffer_size_ret) 1jklmnobcdefghiy

285 check_status(__status__) 1jklmnobcdefghiy

286 return buffer_size_ret 1jklmnobcdefghiy

287  

288  

289cpdef get_program_log(intptr_t prog, buffer): 

290 """Get the Compiler/Verifier Message. 

291  

292 Args: 

293 prog (intptr_t): NVVM program. 

294 buffer (bytes): Compilation/Verification log. 

295  

296 .. seealso:: `nvvmGetProgramLog` 

297 """ 

298 cdef void* _buffer_ = get_buffer_pointer(buffer, -1, readonly=False) 1jklmnobcdefghiy

299 with nogil: 1jklmnobcdefghiy

300 __status__ = nvvmGetProgramLog(<Program>prog, <char*>_buffer_) 1jklmnobcdefghiy

301 check_status(__status__) 1jklmnobcdefghiy

302  

303  

304cpdef int llvm_version(arch) except? 0: 

305 """Get the LLVM IR version guaranteed to be supported by NVVM. 

306  

307 Args: 

308 arch (str): Architecture string. 

309  

310 Returns: 

311 int: IR version number. 

312  

313 .. seealso:: `nvvmLLVMVersion` 

314 """ 

315 if not isinstance(arch, str): 1C

316 raise TypeError("arch must be a Python str") 

317 cdef bytes _temp_arch_ = (<str>arch).encode() 1C

318 cdef char* _arch_ = _temp_arch_ 1C

319 cdef int major 

320 with nogil: 1C

321 __status__ = nvvmLLVMVersion(<const char*>_arch_, &major) 1C

322 check_status(__status__) 1C

323 return major 1C