Coverage for cuda / bindings / nvjitlink.pyx: 98%

122 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-10 01:19 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2024-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_resource_ptr, get_nested_resource_ptr, nested_resource, nullable_unique_ptr, 

10 get_buffer_pointer, get_resource_ptrs) 

11  

12from enum import IntEnum as _IntEnum 

13from libcpp.vector cimport vector 

14  

15  

16############################################################################### 

17# Enum 

18############################################################################### 

19  

20class Result(_IntEnum): 

21 """See `nvJitLinkResult`.""" 

22 SUCCESS = NVJITLINK_SUCCESS 

23 ERROR_UNRECOGNIZED_OPTION = NVJITLINK_ERROR_UNRECOGNIZED_OPTION 

24 ERROR_MISSING_ARCH = NVJITLINK_ERROR_MISSING_ARCH 

25 ERROR_INVALID_INPUT = NVJITLINK_ERROR_INVALID_INPUT 

26 ERROR_PTX_COMPILE = NVJITLINK_ERROR_PTX_COMPILE 

27 ERROR_NVVM_COMPILE = NVJITLINK_ERROR_NVVM_COMPILE 

28 ERROR_INTERNAL = NVJITLINK_ERROR_INTERNAL 

29 ERROR_THREADPOOL = NVJITLINK_ERROR_THREADPOOL 

30 ERROR_UNRECOGNIZED_INPUT = NVJITLINK_ERROR_UNRECOGNIZED_INPUT 

31 ERROR_FINALIZE = NVJITLINK_ERROR_FINALIZE 

32 ERROR_NULL_INPUT = NVJITLINK_ERROR_NULL_INPUT 

33 ERROR_INCOMPATIBLE_OPTIONS = NVJITLINK_ERROR_INCOMPATIBLE_OPTIONS 

34 ERROR_INCORRECT_INPUT_TYPE = NVJITLINK_ERROR_INCORRECT_INPUT_TYPE 

35 ERROR_ARCH_MISMATCH = NVJITLINK_ERROR_ARCH_MISMATCH 

36 ERROR_OUTDATED_LIBRARY = NVJITLINK_ERROR_OUTDATED_LIBRARY 

37 ERROR_MISSING_FATBIN = NVJITLINK_ERROR_MISSING_FATBIN 

38 ERROR_UNRECOGNIZED_ARCH = NVJITLINK_ERROR_UNRECOGNIZED_ARCH 

39 ERROR_UNSUPPORTED_ARCH = NVJITLINK_ERROR_UNSUPPORTED_ARCH 

40 ERROR_LTO_NOT_ENABLED = NVJITLINK_ERROR_LTO_NOT_ENABLED 

41  

42class InputType(_IntEnum): 

43 """See `nvJitLinkInputType`.""" 

44 NONE = NVJITLINK_INPUT_NONE 

45 CUBIN = NVJITLINK_INPUT_CUBIN 

46 PTX = NVJITLINK_INPUT_PTX 

47 LTOIR = NVJITLINK_INPUT_LTOIR 

48 FATBIN = NVJITLINK_INPUT_FATBIN 

49 OBJECT = NVJITLINK_INPUT_OBJECT 

50 LIBRARY = NVJITLINK_INPUT_LIBRARY 

51 INDEX = NVJITLINK_INPUT_INDEX 

52 ANY = NVJITLINK_INPUT_ANY 

53  

54  

55############################################################################### 

56# Error handling 

57############################################################################### 

58  

59class nvJitLinkError(Exception): 

60  

61 def __init__(self, status): 

62 self.status = status 

63 s = Result(status) 

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

65 super(nvJitLinkError, self).__init__(err) 

66  

67 def __reduce__(self): 

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

69  

70  

71@cython.profile(False) 

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

73 if status != 0: 

74 with gil: 

75 raise nvJitLinkError(status) 

76 return status 

77  

78  

79############################################################################### 

80# Wrapper functions 

81############################################################################### 

82  

83cpdef destroy(intptr_t handle): 

84 """nvJitLinkDestroy frees the memory associated with the given handle. 

85  

86 Args: 

87 handle (intptr_t): nvJitLink handle. 

88  

89 .. seealso:: `nvJitLinkDestroy` 

90 """ 

91 cdef Handle h = <Handle>handle 

92 with nogil: 

93 status = nvJitLinkDestroy(&h) 

94 check_status(status) 

95  

96  

97cpdef intptr_t create(uint32_t num_options, options) except -1: 

98 """nvJitLinkCreate creates an instance of nvJitLinkHandle with the given input options, and sets the output parameter ``handle``. 

99  

100 Args: 

101 num_options (uint32_t): Number of options passed. 

102 options (object): Array of size ``num_options`` of option strings. It can be: 

103  

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

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

106 to a valid sequence of 'char', or 

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

108  

109  

110 Returns: 

111 intptr_t: Address of nvJitLink handle. 

112  

113 .. seealso:: `nvJitLinkCreate` 

114 """ 

115 cdef nested_resource[ char ] _options_ 

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

117 cdef Handle handle 

118 with nogil: 

119 __status__ = nvJitLinkCreate(&handle, num_options, <const char**>(_options_.ptrs.data())) 

120 check_status(__status__) 

121 return <intptr_t>handle 

122  

123  

124cpdef add_data(intptr_t handle, int input_type, data, size_t size, name): 

125 """nvJitLinkAddData adds data image to the link. 

126  

127 Args: 

128 handle (intptr_t): nvJitLink handle. 

129 input_type (InputType): kind of input. 

130 data (bytes): pointer to data image in memory. 

131 size (size_t): size of the data. 

132 name (str): name of input object. 

133  

134 .. seealso:: `nvJitLinkAddData` 

135 """ 

136 cdef void* _data_ = get_buffer_pointer(data, size, readonly=True) 

137 if not isinstance(name, str): 

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

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

140 cdef char* _name_ = _temp_name_ 

141 with nogil: 

142 __status__ = nvJitLinkAddData(<Handle>handle, <_InputType>input_type, <const void*>_data_, size, <const char*>_name_) 

143 check_status(__status__) 

144  

145  

146cpdef add_file(intptr_t handle, int input_type, file_name): 

147 """nvJitLinkAddFile reads data from file and links it in. 

148  

149 Args: 

150 handle (intptr_t): nvJitLink handle. 

151 input_type (InputType): kind of input. 

152 file_name (str): name of file. 

153  

154 .. seealso:: `nvJitLinkAddFile` 

155 """ 

156 if not isinstance(file_name, str): 

157 raise TypeError("file_name must be a Python str") 

158 cdef bytes _temp_file_name_ = (<str>file_name).encode() 

159 cdef char* _file_name_ = _temp_file_name_ 

160 with nogil: 

161 __status__ = nvJitLinkAddFile(<Handle>handle, <_InputType>input_type, <const char*>_file_name_) 

162 check_status(__status__) 

163  

164  

165cpdef complete(intptr_t handle): 

166 """nvJitLinkComplete does the actual link. 

167  

168 Args: 

169 handle (intptr_t): nvJitLink handle. 

170  

171 .. seealso:: `nvJitLinkComplete` 

172 """ 

173 with nogil: 

174 __status__ = nvJitLinkComplete(<Handle>handle) 

175 check_status(__status__) 

176  

177  

178cpdef size_t get_linked_cubin_size(intptr_t handle) except? 0: 

179 """nvJitLinkGetLinkedCubinSize gets the size of the linked cubin. 

180  

181 Args: 

182 handle (intptr_t): nvJitLink handle. 

183  

184 Returns: 

185 size_t: Size of the linked cubin. 

186  

187 .. seealso:: `nvJitLinkGetLinkedCubinSize` 

188 """ 

189 cdef size_t size 

190 with nogil: 

191 __status__ = nvJitLinkGetLinkedCubinSize(<Handle>handle, &size) 

192 check_status(__status__) 

193 return size 

194  

195  

196cpdef get_linked_cubin(intptr_t handle, cubin): 

197 """nvJitLinkGetLinkedCubin gets the linked cubin. 

198  

199 Args: 

200 handle (intptr_t): nvJitLink handle. 

201 cubin (bytes): The linked cubin. 

202  

203 .. seealso:: `nvJitLinkGetLinkedCubin` 

204 """ 

205 cdef void* _cubin_ = get_buffer_pointer(cubin, -1, readonly=False) 

206 with nogil: 

207 __status__ = nvJitLinkGetLinkedCubin(<Handle>handle, <void*>_cubin_) 

208 check_status(__status__) 

209  

210  

211cpdef size_t get_linked_ptx_size(intptr_t handle) except? 0: 

212 """nvJitLinkGetLinkedPtxSize gets the size of the linked ptx. 

213  

214 Args: 

215 handle (intptr_t): nvJitLink handle. 

216  

217 Returns: 

218 size_t: Size of the linked PTX. 

219  

220 .. seealso:: `nvJitLinkGetLinkedPtxSize` 

221 """ 

222 cdef size_t size 

223 with nogil: 

224 __status__ = nvJitLinkGetLinkedPtxSize(<Handle>handle, &size) 

225 check_status(__status__) 

226 return size 

227  

228  

229cpdef get_linked_ptx(intptr_t handle, ptx): 

230 """nvJitLinkGetLinkedPtx gets the linked ptx. 

231  

232 Args: 

233 handle (intptr_t): nvJitLink handle. 

234 ptx (bytes): The linked PTX. 

235  

236 .. seealso:: `nvJitLinkGetLinkedPtx` 

237 """ 

238 cdef void* _ptx_ = get_buffer_pointer(ptx, -1, readonly=False) 

239 with nogil: 

240 __status__ = nvJitLinkGetLinkedPtx(<Handle>handle, <char*>_ptx_) 

241 check_status(__status__) 

242  

243  

244cpdef size_t get_error_log_size(intptr_t handle) except? 0: 

245 """nvJitLinkGetErrorLogSize gets the size of the error log. 

246  

247 Args: 

248 handle (intptr_t): nvJitLink handle. 

249  

250 Returns: 

251 size_t: Size of the error log. 

252  

253 .. seealso:: `nvJitLinkGetErrorLogSize` 

254 """ 

255 cdef size_t size 

256 with nogil: 

257 __status__ = nvJitLinkGetErrorLogSize(<Handle>handle, &size) 

258 check_status(__status__) 

259 return size 

260  

261  

262cpdef get_error_log(intptr_t handle, log): 

263 """nvJitLinkGetErrorLog puts any error messages in the log. 

264  

265 Args: 

266 handle (intptr_t): nvJitLink handle. 

267 log (bytes): The error log. 

268  

269 .. seealso:: `nvJitLinkGetErrorLog` 

270 """ 

271 cdef void* _log_ = get_buffer_pointer(log, -1, readonly=False) 

272 with nogil: 

273 __status__ = nvJitLinkGetErrorLog(<Handle>handle, <char*>_log_) 

274 check_status(__status__) 

275  

276  

277cpdef size_t get_info_log_size(intptr_t handle) except? 0: 

278 """nvJitLinkGetInfoLogSize gets the size of the info log. 

279  

280 Args: 

281 handle (intptr_t): nvJitLink handle. 

282  

283 Returns: 

284 size_t: Size of the info log. 

285  

286 .. seealso:: `nvJitLinkGetInfoLogSize` 

287 """ 

288 cdef size_t size 

289 with nogil: 

290 __status__ = nvJitLinkGetInfoLogSize(<Handle>handle, &size) 

291 check_status(__status__) 

292 return size 

293  

294  

295cpdef get_info_log(intptr_t handle, log): 

296 """nvJitLinkGetInfoLog puts any info messages in the log. 

297  

298 Args: 

299 handle (intptr_t): nvJitLink handle. 

300 log (bytes): The info log. 

301  

302 .. seealso:: `nvJitLinkGetInfoLog` 

303 """ 

304 cdef void* _log_ = get_buffer_pointer(log, -1, readonly=False) 

305 with nogil: 

306 __status__ = nvJitLinkGetInfoLog(<Handle>handle, <char*>_log_) 

307 check_status(__status__) 

308  

309  

310cpdef tuple version(): 

311 """nvJitLinkVersion returns the current version of nvJitLink. 

312  

313 Returns: 

314 A 2-tuple containing: 

315  

316 - unsigned int: The major version. 

317 - unsigned int: The minor version. 

318  

319 .. seealso:: `nvJitLinkVersion` 

320 """ 

321 cdef unsigned int major 

322 cdef unsigned int minor 

323 with nogil: 

324 __status__ = nvJitLinkVersion(&major, &minor) 

325 check_status(__status__) 

326 return (major, minor)