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

158 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-16 01:23 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2024-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.4.1. Do not modify it directly. 

6# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=d783bc46a513ae0becbc9385c3be40ba64809b0fd834e5ed3189b94bd287436d 

7  

8  

9# <<<< PREAMBLE CONTENT >>>> 

10  

11cimport cpython as _cyb_cpython 

12from libc.stdint cimport ( 

13 intptr_t, 

14 uint32_t, 

15) 

16  

17from cuda.bindings._internal._fast_enum import FastEnum as _cyb_FastEnum 

18  

19cdef intptr_t _cyb_get_buffer_pointer(buf, Py_ssize_t size, readonly=True) except?-1: 

20 cdef intptr_t ptr 

21 cdef int flags = _cyb_cpython.PyBUF_ANY_CONTIGUOUS 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

22 if not readonly: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

23 flags |= _cyb_cpython.PyBUF_WRITABLE 1HIJKbcdefghijklmnopqLMNOrstuvwxyzABCDEFG

24 cdef int status = -1 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

25 cdef _cyb_cpython.Py_buffer view 

26 if buf is None: 1aPQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

27 ptr = 0 

28 elif isinstance(buf, int): 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

29 ptr = <intptr_t>buf 

30 else: 

31 try: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

32 status = _cyb_cpython.PyObject_GetBuffer(buf, &view, flags) 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

33 if size != -1: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

34 assert view.len == size 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG

35 assert view.ndim == 1 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

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 1D contiguous {adj}" 

41 f"buffer, of size {size}" 

42 ) from e 

43 else: 

44 ptr = <intptr_t>view.buf 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

45 finally: 

46 if status == 0: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

47 _cyb_cpython.PyBuffer_Release(&view) 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

48 return ptr 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

49  

50  

51# <<<< END OF PREAMBLE CONTENT >>>> 

52  

53cimport cython # NOQA 

54  

55from ._internal.utils cimport (get_resource_ptr, get_nested_resource_ptr, nested_resource, nullable_unique_ptr, 

56 get_resource_ptrs) 

57  

58from libcpp.vector cimport vector 

59  

60  

61############################################################################### 

62# Enum 

63############################################################################### 

64  

65class Result(_cyb_FastEnum): 

66 """ 

67 The enumerated type `nvJitLinkResult` defines API call result codes. 

68 nvJitLink APIs return `nvJitLinkResult` codes to indicate the result. 

69  

70 See `nvJitLinkResult`. 

71 """ 

72 SUCCESS = NVJITLINK_SUCCESS 

73 ERROR_UNRECOGNIZED_OPTION = (NVJITLINK_ERROR_UNRECOGNIZED_OPTION, 'Unrecognized Option') 

74 ERROR_MISSING_ARCH = (NVJITLINK_ERROR_MISSING_ARCH, 'Option `-arch=sm_NN` not specified') 

75 ERROR_INVALID_INPUT = (NVJITLINK_ERROR_INVALID_INPUT, 'Invalid Input') 

76 ERROR_PTX_COMPILE = (NVJITLINK_ERROR_PTX_COMPILE, 'Issue during PTX Compilation') 

77 ERROR_NVVM_COMPILE = (NVJITLINK_ERROR_NVVM_COMPILE, 'Issue during NVVM Compilation') 

78 ERROR_INTERNAL = (NVJITLINK_ERROR_INTERNAL, 'Internal Error') 

79 ERROR_THREADPOOL = (NVJITLINK_ERROR_THREADPOOL, 'Issue with Thread Pool') 

80 ERROR_UNRECOGNIZED_INPUT = (NVJITLINK_ERROR_UNRECOGNIZED_INPUT, 'Unrecognized Input') 

81 ERROR_FINALIZE = (NVJITLINK_ERROR_FINALIZE, 'Finalizer Error') 

82 ERROR_NULL_INPUT = (NVJITLINK_ERROR_NULL_INPUT, 'Null Input') 

83 ERROR_INCOMPATIBLE_OPTIONS = (NVJITLINK_ERROR_INCOMPATIBLE_OPTIONS, 'Incompatible Options') 

84 ERROR_INCORRECT_INPUT_TYPE = (NVJITLINK_ERROR_INCORRECT_INPUT_TYPE, 'Incorrect Input Type') 

85 ERROR_ARCH_MISMATCH = (NVJITLINK_ERROR_ARCH_MISMATCH, 'Arch Mismatch') 

86 ERROR_OUTDATED_LIBRARY = (NVJITLINK_ERROR_OUTDATED_LIBRARY, 'Outdated Library') 

87 ERROR_MISSING_FATBIN = (NVJITLINK_ERROR_MISSING_FATBIN, 'Missing Fatbin') 

88 ERROR_UNRECOGNIZED_ARCH = (NVJITLINK_ERROR_UNRECOGNIZED_ARCH, 'Unrecognized -arch value') 

89 ERROR_UNSUPPORTED_ARCH = (NVJITLINK_ERROR_UNSUPPORTED_ARCH, 'Unsupported -arch value') 

90 ERROR_LTO_NOT_ENABLED = (NVJITLINK_ERROR_LTO_NOT_ENABLED, 'Requires -lto') 

91  

92class InputType(_cyb_FastEnum): 

93 """ 

94 The enumerated type `nvJitLinkInputType` defines the kind of inputs 

95 that can be passed to nvJitLinkAdd* APIs. 

96  

97 See `nvJitLinkInputType`. 

98 """ 

99 NONE = (NVJITLINK_INPUT_NONE, 'Error Type') 

100 CUBIN = (NVJITLINK_INPUT_CUBIN, 'For CUDA Binaries') 

101 PTX = (NVJITLINK_INPUT_PTX, 'For PTX') 

102 LTOIR = (NVJITLINK_INPUT_LTOIR, 'For LTO-IR') 

103 FATBIN = (NVJITLINK_INPUT_FATBIN, 'For Fatbin') 

104 OBJECT = (NVJITLINK_INPUT_OBJECT, 'For Host Object') 

105 LIBRARY = (NVJITLINK_INPUT_LIBRARY, 'For Host Library') 

106 INDEX = (NVJITLINK_INPUT_INDEX, 'For Index File') 

107 ANY = (NVJITLINK_INPUT_ANY, 'Dynamically chooses from the valid types') 

108  

109  

110############################################################################### 

111# Error handling 

112############################################################################### 

113  

114class nvJitLinkError(Exception): 

115  

116 def __init__(self, status): 

117 self.status = status 2% ' sbtb( )

118 s = Result(status) 2% ' sbtb( )

119 cdef str err = f"{s.name} ({s.value})" 2% ' sbtb( )

120 super(nvJitLinkError, self).__init__(err) 2% ' sbtb( )

121  

122 def __reduce__(self): 

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

124  

125  

126@cython.profile(False) 

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

128 if status != 0: 2a P Q R S X Y Z 0 5 6 7 8 * + , - . H I J K b c d e f g h i j k l m n o p q % @ ' T U V W 1 2 3 4 9 ! # $ / : ; = ? L M N O r s t u v w x y z A B C D E F G ( [ ) ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb

129 with gil: 1%'()

130 raise nvJitLinkError(status) 1%'()

131 return status 2a P Q R S X Y Z 0 5 6 7 8 * + , - . H I J K b c d e f g h i j k l m n o p q @ T U V W 1 2 3 4 9 ! # $ / : ; = ? L M N O r s t u v w x y z A B C D E F G [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb

132  

133  

134############################################################################### 

135# Wrapper functions 

136############################################################################### 

137  

138cpdef destroy(intptr_t handle): 

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

140  

141 Args: 

142 handle (intptr_t): nvJitLink handle. 

143  

144 .. seealso:: `nvJitLinkDestroy` 

145 """ 

146 cdef Handle h = <Handle>handle 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG

147 with nogil: 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG

148 status = nvJitLinkDestroy(&h) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG

149 check_status(status) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG

150  

151  

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

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

154  

155 It supports options listed in ``Supported Link Options``. 

156  

157 Args: 

158 num_options (uint32_t): Number of options passed. 

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

160  

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

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

163 to a valid sequence of 'char', or 

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

165  

166 Returns: 

167 intptr_t: Address of nvJitLink handle. 

168  

169 .. seealso:: `nvJitLinkCreate` 

170 """ 

171 cdef nested_resource[ char ] _options_ 

172 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()

173 cdef Handle handle 

174 with nogil: 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()

175 __status__ = nvJitLinkCreate(&handle, num_options, <const char**>(_options_.ptrs.data())) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()

176 check_status(__status__) 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopq%'TUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG()

177 return <intptr_t>handle 1PQRSXYZ05678*+,-.HIJKbcdefghijklmnopqTUVW12349!#$/:;=?LMNOrstuvwxyzABCDEFG

178  

179  

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

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

182  

183  

184 Args: 

185 handle (intptr_t): nvJitLink handle. 

186 input_type (InputType): kind of input. 

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

188 size (size_t): size of the data. 

189 name (str): name of input object. 

190  

191 .. seealso:: `nvJitLinkAddData` 

192 """ 

193 cdef void* _data_ = <void *>_cyb_get_buffer_pointer(data, size, readonly=True) 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG

194 if not isinstance(name, str): 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG

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

196 cdef bytes _temp_name_ = (<str>name).encode() 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG

197 cdef char* _name_ = _temp_name_ 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG

198 with nogil: 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG

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

200 check_status(__status__) 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG

201  

202  

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

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

205  

206  

207 Args: 

208 handle (intptr_t): nvJitLink handle. 

209 input_type (InputType): kind of input. 

210 file_name (str): name of file. 

211  

212 .. seealso:: `nvJitLinkAddFile` 

213 """ 

214 if not isinstance(file_name, str): 1XYZ01234

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

216 cdef bytes _temp_file_name_ = (<str>file_name).encode() 1XYZ01234

217 cdef char* _file_name_ = _temp_file_name_ 1XYZ01234

218 with nogil: 1XYZ01234

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

220 check_status(__status__) 1XYZ01234

221  

222  

223cpdef complete(intptr_t handle): 

224 """nvJitLinkComplete does the actual link. 

225  

226  

227 Args: 

228 handle (intptr_t): nvJitLink handle. 

229  

230 .. seealso:: `nvJitLinkComplete` 

231 """ 

232 with nogil: 1PQRSXYZ05678HIJKbcdefghijklmnopqTUVW12349!#$LMNOrstuvwxyzABCDEFG

233 __status__ = nvJitLinkComplete(<Handle>handle) 1PQRSXYZ05678HIJKbcdefghijklmnopqTUVW12349!#$LMNOrstuvwxyzABCDEFG

234 check_status(__status__) 1PQRSXYZ05678HIJKbcdefghijklmnopqTUVW12349!#$LMNOrstuvwxyzABCDEFG

235  

236  

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

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

239  

240  

241 Args: 

242 handle (intptr_t): nvJitLink handle. 

243  

244 Returns: 

245 size_t: Size of the linked cubin. 

246  

247 .. seealso:: `nvJitLinkGetLinkedCubinSize` 

248 """ 

249 cdef size_t size 

250 with nogil: 1fghivwxy

251 __status__ = nvJitLinkGetLinkedCubinSize(<Handle>handle, &size) 1fghivwxy

252 check_status(__status__) 1fghivwxy

253 return size 1fghivwxy

254  

255  

256cpdef get_linked_cubin(intptr_t handle, cubin): 

257 """nvJitLinkGetLinkedCubin gets the linked cubin. 

258  

259 User is responsible for allocating enough space to hold the ``cubin``. 

260  

261 Args: 

262 handle (intptr_t): nvJitLink handle. 

263 cubin (bytes): The linked cubin. 

264  

265 .. seealso:: `nvJitLinkGetLinkedCubin` 

266 """ 

267 cdef void* _cubin_ = <void *>_cyb_get_buffer_pointer(cubin, -1, readonly=False) 1fghivwxy

268 with nogil: 1fghivwxy

269 __status__ = nvJitLinkGetLinkedCubin(<Handle>handle, <void*>_cubin_) 1fghivwxy

270 check_status(__status__) 1fghivwxy

271  

272  

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

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

275  

276 Linked PTX is only available when using the ``-lto`` option. 

277  

278 Args: 

279 handle (intptr_t): nvJitLink handle. 

280  

281 Returns: 

282 size_t: Size of the linked PTX. 

283  

284 .. seealso:: `nvJitLinkGetLinkedPtxSize` 

285 """ 

286 cdef size_t size 

287 with nogil: 1nopqDEFG

288 __status__ = nvJitLinkGetLinkedPtxSize(<Handle>handle, &size) 1nopqDEFG

289 check_status(__status__) 1nopqDEFG

290 return size 1nopqDEFG

291  

292  

293cpdef get_linked_ptx(intptr_t handle, ptx): 

294 """nvJitLinkGetLinkedPtx gets the linked ptx. 

295  

296 Linked PTX is only available when using the ``-lto`` option. User is 

297 responsible for allocating enough space to hold the ``ptx``. 

298  

299 Args: 

300 handle (intptr_t): nvJitLink handle. 

301 ptx (bytes): The linked PTX. 

302  

303 .. seealso:: `nvJitLinkGetLinkedPtx` 

304 """ 

305 cdef void* _ptx_ = <void *>_cyb_get_buffer_pointer(ptx, -1, readonly=False) 1nopqDEFG

306 with nogil: 1nopqDEFG

307 __status__ = nvJitLinkGetLinkedPtx(<Handle>handle, <char*>_ptx_) 1nopqDEFG

308 check_status(__status__) 1nopqDEFG

309  

310  

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

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

313  

314  

315 Args: 

316 handle (intptr_t): nvJitLink handle. 

317  

318 Returns: 

319 size_t: Size of the error log. 

320  

321 .. seealso:: `nvJitLinkGetErrorLogSize` 

322 """ 

323 cdef size_t size 

324 with nogil: 1HIJKLMNO

325 __status__ = nvJitLinkGetErrorLogSize(<Handle>handle, &size) 1HIJKLMNO

326 check_status(__status__) 1HIJKLMNO

327 return size 1HIJKLMNO

328  

329  

330cpdef get_error_log(intptr_t handle, log): 

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

332  

333 User is responsible for allocating enough space to hold the ``log``. 

334  

335 Args: 

336 handle (intptr_t): nvJitLink handle. 

337 log (bytes): The error log. 

338  

339 .. seealso:: `nvJitLinkGetErrorLog` 

340 """ 

341 cdef void* _log_ = <void *>_cyb_get_buffer_pointer(log, -1, readonly=False) 1HIJKLMNO

342 with nogil: 1HIJKLMNO

343 __status__ = nvJitLinkGetErrorLog(<Handle>handle, <char*>_log_) 1HIJKLMNO

344 check_status(__status__) 1HIJKLMNO

345  

346  

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

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

349  

350  

351 Args: 

352 handle (intptr_t): nvJitLink handle. 

353  

354 Returns: 

355 size_t: Size of the info log. 

356  

357 .. seealso:: `nvJitLinkGetInfoLogSize` 

358 """ 

359 cdef size_t size 

360 with nogil: 1bcderstu

361 __status__ = nvJitLinkGetInfoLogSize(<Handle>handle, &size) 1bcderstu

362 check_status(__status__) 1bcderstu

363 return size 1bcderstu

364  

365  

366cpdef get_info_log(intptr_t handle, log): 

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

368  

369 User is responsible for allocating enough space to hold the ``log``. 

370  

371 Args: 

372 handle (intptr_t): nvJitLink handle. 

373 log (bytes): The info log. 

374  

375 .. seealso:: `nvJitLinkGetInfoLog` 

376 """ 

377 cdef void* _log_ = <void *>_cyb_get_buffer_pointer(log, -1, readonly=False) 1bcderstu

378 with nogil: 1bcderstu

379 __status__ = nvJitLinkGetInfoLog(<Handle>handle, <char*>_log_) 1bcderstu

380 check_status(__status__) 1bcderstu

381  

382  

383cpdef tuple version(): 

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

385  

386  

387 Returns: 

388 A 2-tuple containing: 

389 - unsigned int: The major version. 

390 - unsigned int: The minor version. 

391  

392 .. seealso:: `nvJitLinkVersion` 

393 """ 

394 cdef unsigned int major 

395 cdef unsigned int minor 

396 with nogil: 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb

397 __status__ = nvJitLinkVersion(&major, &minor) 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb

398 check_status(__status__) 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb

399 return (major, minor) 2a @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrb

400  

401  

402cpdef size_t get_linked_ltoir_size(intptr_t handle) except? 0: 

403 """nvJitLinkGetLinkedLTOIRSize gets the size of the linked LTOIR. 

404  

405 Linked LTOIR is only available when using the ``-lto`` option. The returned 

406 data is in LTOIR Container format (not raw bitcode). 

407  

408 Args: 

409 handle (intptr_t): nvJitLink handle. 

410  

411 Returns: 

412 size_t: Size of the linked LTOIR. 

413  

414 .. seealso:: `nvJitLinkGetLinkedLTOIRSize` 

415 """ 

416 cdef size_t size 

417 with nogil: 1jklmzABC

418 __status__ = nvJitLinkGetLinkedLTOIRSize(<Handle>handle, &size) 1jklmzABC

419 check_status(__status__) 1jklmzABC

420 return size 1jklmzABC

421  

422  

423cpdef get_linked_ltoir(intptr_t handle, ltoir): 

424 """nvJitLinkGetLinkedLTOIR gets the linked LTOIR. 

425  

426 Linked LTOIR is only available when using the ``-lto`` option. User is 

427 responsible for allocating enough space to hold the ``ltoir``. The returned 

428 data is in LTOIR Container format (not raw bitcode). 

429  

430 Args: 

431 handle (intptr_t): nvJitLink handle. 

432 ltoir (bytes): The linked LTOIR in Container format. 

433  

434 .. seealso:: `nvJitLinkGetLinkedLTOIR` 

435 """ 

436 cdef void* _ltoir_ = <void *>_cyb_get_buffer_pointer(ltoir, -1, readonly=False) 1jklmzABC

437 with nogil: 1jklmzABC

438 __status__ = nvJitLinkGetLinkedLTOIR(<Handle>handle, <void*>_ltoir_) 1jklmzABC

439 check_status(__status__) 1jklmzABC

440del _cyb_FastEnum