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

98 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-10 01:19 +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.0. 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 enum import IntEnum as _IntEnum 

13  

14  

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

16# Enum 

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

18  

19class Result(_IntEnum): 

20 """See `nvvmResult`.""" 

21 SUCCESS = NVVM_SUCCESS 

22 ERROR_OUT_OF_MEMORY = NVVM_ERROR_OUT_OF_MEMORY 

23 ERROR_PROGRAM_CREATION_FAILURE = NVVM_ERROR_PROGRAM_CREATION_FAILURE 

24 ERROR_IR_VERSION_MISMATCH = NVVM_ERROR_IR_VERSION_MISMATCH 

25 ERROR_INVALID_INPUT = NVVM_ERROR_INVALID_INPUT 

26 ERROR_INVALID_PROGRAM = NVVM_ERROR_INVALID_PROGRAM 

27 ERROR_INVALID_IR = NVVM_ERROR_INVALID_IR 

28 ERROR_INVALID_OPTION = NVVM_ERROR_INVALID_OPTION 

29 ERROR_NO_MODULE_IN_PROGRAM = NVVM_ERROR_NO_MODULE_IN_PROGRAM 

30 ERROR_COMPILATION = NVVM_ERROR_COMPILATION 

31 ERROR_CANCELLED = NVVM_ERROR_CANCELLED 

32  

33  

34############################################################################### 

35# Error handling 

36############################################################################### 

37  

38class nvvmError(Exception): 

39  

40 def __init__(self, status): 

41 self.status = status 

42 s = Result(status) 

43 cdef str err = f"{s.name} ({s.value})" 

44 super(nvvmError, self).__init__(err) 

45  

46 def __reduce__(self): 

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

48  

49  

50@cython.profile(False) 

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

52 if status != 0: 

53 with gil: 

54 raise nvvmError(status) 

55 return status 

56  

57  

58############################################################################### 

59# Wrapper functions 

60############################################################################### 

61  

62cpdef destroy_program(intptr_t prog): 

63 """Destroy a program. 

64  

65 Args: 

66 prog (intptr_t): nvvm prog. 

67  

68 .. seealso:: `nvvmDestroyProgram` 

69 """ 

70 cdef Program p = <Program>prog 

71 with nogil: 

72 status = nvvmDestroyProgram(&p) 

73 check_status(status) 

74  

75  

76cpdef str get_error_string(int result): 

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

78  

79 Args: 

80 result (Result): NVVM API result code. 

81  

82 .. seealso:: `nvvmGetErrorString` 

83 """ 

84 cdef bytes _output_ 

85 _output_ = nvvmGetErrorString(<_Result>result) 

86 return _output_.decode() 

87  

88  

89cpdef tuple version(): 

90 """Get the NVVM version. 

91  

92 Returns: 

93 A 2-tuple containing: 

94  

95 - int: NVVM major version number. 

96 - int: NVVM minor version number. 

97  

98 .. seealso:: `nvvmVersion` 

99 """ 

100 cdef int major 

101 cdef int minor 

102 with nogil: 

103 __status__ = nvvmVersion(&major, &minor) 

104 check_status(__status__) 

105 return (major, minor) 

106  

107  

108cpdef tuple ir_version(): 

109 """Get the NVVM IR version. 

110  

111 Returns: 

112 A 4-tuple containing: 

113  

114 - int: NVVM IR major version number. 

115 - int: NVVM IR minor version number. 

116 - int: NVVM IR debug metadata major version number. 

117 - int: NVVM IR debug metadata minor version number. 

118  

119 .. seealso:: `nvvmIRVersion` 

120 """ 

121 cdef int major_ir 

122 cdef int minor_ir 

123 cdef int major_dbg 

124 cdef int minor_dbg 

125 with nogil: 

126 __status__ = nvvmIRVersion(&major_ir, &minor_ir, &major_dbg, &minor_dbg) 

127 check_status(__status__) 

128 return (major_ir, minor_ir, major_dbg, minor_dbg) 

129  

130  

131cpdef intptr_t create_program() except? 0: 

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

133  

134 Returns: 

135 intptr_t: NVVM program. 

136  

137 .. seealso:: `nvvmCreateProgram` 

138 """ 

139 cdef Program prog 

140 with nogil: 

141 __status__ = nvvmCreateProgram(&prog) 

142 check_status(__status__) 

143 return <intptr_t>prog 

144  

145  

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

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

148  

149 Args: 

150 prog (intptr_t): NVVM program. 

151 buffer (bytes): NVVM IR module in the bitcode or text representation. 

152 size (size_t): Size of the NVVM IR module. 

153 name (str): Name of the NVVM IR module. If NULL, "<unnamed>" is used as the name. 

154  

155 .. seealso:: `nvvmAddModuleToProgram` 

156 """ 

157 cdef void* _buffer_ = get_buffer_pointer(buffer, size, readonly=True) 

158 if not isinstance(name, str): 

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

160 cdef bytes _temp_name_ = (<str>name).encode() 

161 cdef char* _name_ = _temp_name_ 

162 with nogil: 

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

164 check_status(__status__) 

165  

166  

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

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

169  

170 Args: 

171 prog (intptr_t): NVVM program. 

172 buffer (bytes): NVVM IR module in the bitcode representation. 

173 size (size_t): Size of the NVVM IR module. 

174 name (str): Name of the NVVM IR module. If NULL, "<unnamed>" is used as the name. 

175  

176 .. seealso:: `nvvmLazyAddModuleToProgram` 

177 """ 

178 cdef void* _buffer_ = get_buffer_pointer(buffer, size, readonly=True) 

179 if not isinstance(name, str): 

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

181 cdef bytes _temp_name_ = (<str>name).encode() 

182 cdef char* _name_ = _temp_name_ 

183 with nogil: 

184 __status__ = nvvmLazyAddModuleToProgram(<Program>prog, <const char*>_buffer_, size, <const char*>_name_) 

185 check_status(__status__) 

186  

187  

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

189 """Compile the NVVM program. 

190  

191 Args: 

192 prog (intptr_t): NVVM program. 

193 num_options (int): Number of compiler ``options`` passed. 

194 options (object): Compiler options in the form of C string array. It can be: 

195  

196 - an :class:`int` as the pointer address to the nested sequence, or 

197 - a Python sequence of :class:`int`\s, each of which is a pointer address 

198 to a valid sequence of 'char', or 

199 - a nested Python sequence of ``str``. 

200  

201  

202 .. seealso:: `nvvmCompileProgram` 

203 """ 

204 cdef nested_resource[ char ] _options_ 

205 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 

206 with nogil: 

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

208 check_status(__status__) 

209  

210  

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

212 """Verify the NVVM program. 

213  

214 Args: 

215 prog (intptr_t): NVVM program. 

216 num_options (int): Number of compiler ``options`` passed. 

217 options (object): Compiler options in the form of C string array. It can be: 

218  

219 - an :class:`int` as the pointer address to the nested sequence, or 

220 - a Python sequence of :class:`int`\s, each of which is a pointer address 

221 to a valid sequence of 'char', or 

222 - a nested Python sequence of ``str``. 

223  

224  

225 .. seealso:: `nvvmVerifyProgram` 

226 """ 

227 cdef nested_resource[ char ] _options_ 

228 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 

229 with nogil: 

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

231 check_status(__status__) 

232  

233  

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

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

236  

237 Args: 

238 prog (intptr_t): NVVM program. 

239  

240 Returns: 

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

242  

243 .. seealso:: `nvvmGetCompiledResultSize` 

244 """ 

245 cdef size_t buffer_size_ret 

246 with nogil: 

247 __status__ = nvvmGetCompiledResultSize(<Program>prog, &buffer_size_ret) 

248 check_status(__status__) 

249 return buffer_size_ret 

250  

251  

252cpdef get_compiled_result(intptr_t prog, buffer): 

253 """Get the compiled result. 

254  

255 Args: 

256 prog (intptr_t): NVVM program. 

257 buffer (bytes): Compiled result. 

258  

259 .. seealso:: `nvvmGetCompiledResult` 

260 """ 

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

262 with nogil: 

263 __status__ = nvvmGetCompiledResult(<Program>prog, <char*>_buffer_) 

264 check_status(__status__) 

265  

266  

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

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

269  

270 Args: 

271 prog (intptr_t): NVVM program. 

272  

273 Returns: 

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

275  

276 .. seealso:: `nvvmGetProgramLogSize` 

277 """ 

278 cdef size_t buffer_size_ret 

279 with nogil: 

280 __status__ = nvvmGetProgramLogSize(<Program>prog, &buffer_size_ret) 

281 check_status(__status__) 

282 return buffer_size_ret 

283  

284  

285cpdef get_program_log(intptr_t prog, buffer): 

286 """Get the Compiler/Verifier Message. 

287  

288 Args: 

289 prog (intptr_t): NVVM program. 

290 buffer (bytes): Compilation/Verification log. 

291  

292 .. seealso:: `nvvmGetProgramLog` 

293 """ 

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

295 with nogil: 

296 __status__ = nvvmGetProgramLog(<Program>prog, <char*>_buffer_) 

297 check_status(__status__)