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

157 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-05 01:11 +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=fa69db9162a1215e162a49f33d17f368a9a3a6ac8bcd89b30f4069ba2919e3f3 

7  

8  

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

10  

11cimport cpython as _cyb_cpython 

12from cuda.bindings._internal.utils cimport ( 

13 get_nested_resource_ptr as _cyb_get_nested_resource_ptr, 

14 nested_resource as _cyb_nested_resource, 

15) 

16from libc.stdint cimport ( 

17 intptr_t, 

18 uint32_t, 

19) 

20  

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

22  

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

24 cdef intptr_t ptr 

25 cdef int flags = _cyb_cpython.PyBUF_ANY_CONTIGUOUS 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

26 if not readonly: 1aPQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

27 flags |= _cyb_cpython.PyBUF_WRITABLE 1HIJKbcdefghijklmnopqLMNOrstuvwxyzABCDEFG

28 cdef int status = -1 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

29 cdef _cyb_cpython.Py_buffer view 

30 if buf is None: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

31 ptr = 0 

32 elif isinstance(buf, int): 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

33 ptr = <intptr_t>buf 

34 else: 

35 try: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

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

37 if size != -1: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

38 assert view.len == size 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG

39 except Exception as e: 

40 adj = "writable " if not readonly else "" 

41 raise ValueError( 

42 "buf must be None, a Python int representing the pointer " 

43 f"address to a valid buffer, or a contiguous {adj}" 

44 f"buffer (a ctypes object, NumPy array, bytes, ...), of size {size}" 

45 ) from e 

46 else: 

47 ptr = <intptr_t>view.buf 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

48 finally: 

49 if status == 0: 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

50 _cyb_cpython.PyBuffer_Release(&view) 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

51 return ptr 1PQRSHIJKbcdefghijklmnopqTUVWLMNOrstuvwxyzABCDEFG

52  

53  

54# <<<< END OF PREAMBLE CONTENT >>>> 

55  

56cimport cython # NOQA 

57  

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

59 get_resource_ptrs) 

60  

61from libcpp.vector cimport vector 

62  

63  

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

65# Enum 

66############################################################################### 

67  

68class Result(_cyb_FastEnum): 

69 """ 

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

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

72  

73 See `nvJitLinkResult`. 

74 """ 

75 SUCCESS = NVJITLINK_SUCCESS 

76 ERROR_UNRECOGNIZED_OPTION = (NVJITLINK_ERROR_UNRECOGNIZED_OPTION, 'Unrecognized Option') 

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

78 ERROR_INVALID_INPUT = (NVJITLINK_ERROR_INVALID_INPUT, 'Invalid Input') 

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

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

81 ERROR_INTERNAL = (NVJITLINK_ERROR_INTERNAL, 'Internal Error') 

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

83 ERROR_UNRECOGNIZED_INPUT = (NVJITLINK_ERROR_UNRECOGNIZED_INPUT, 'Unrecognized Input') 

84 ERROR_FINALIZE = (NVJITLINK_ERROR_FINALIZE, 'Finalizer Error') 

85 ERROR_NULL_INPUT = (NVJITLINK_ERROR_NULL_INPUT, 'Null Input') 

86 ERROR_INCOMPATIBLE_OPTIONS = (NVJITLINK_ERROR_INCOMPATIBLE_OPTIONS, 'Incompatible Options') 

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

88 ERROR_ARCH_MISMATCH = (NVJITLINK_ERROR_ARCH_MISMATCH, 'Arch Mismatch') 

89 ERROR_OUTDATED_LIBRARY = (NVJITLINK_ERROR_OUTDATED_LIBRARY, 'Outdated Library') 

90 ERROR_MISSING_FATBIN = (NVJITLINK_ERROR_MISSING_FATBIN, 'Missing Fatbin') 

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

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

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

94  

95class InputType(_cyb_FastEnum): 

96 """ 

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

98 that can be passed to nvJitLinkAdd* APIs. 

99  

100 See `nvJitLinkInputType`. 

101 """ 

102 NONE = (NVJITLINK_INPUT_NONE, 'Error Type') 

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

104 PTX = (NVJITLINK_INPUT_PTX, 'For PTX') 

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

106 FATBIN = (NVJITLINK_INPUT_FATBIN, 'For Fatbin') 

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

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

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

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

111  

112  

113############################################################################### 

114# Error handling 

115############################################################################### 

116  

117class nvJitLinkError(Exception): 

118  

119 def __init__(self, status): 

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

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

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

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

124  

125 def __reduce__(self): 

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

127  

128  

129@cython.profile(False) 

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

131 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

132 with gil: 1%'()

133 raise nvJitLinkError(status) 1a%'()

134 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

135  

136  

137############################################################################### 

138# Wrapper functions 

139############################################################################### 

140  

141cpdef destroy(intptr_t handle): 

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

143  

144 Args: 

145 handle (intptr_t): nvJitLink handle. 

146  

147 .. seealso:: `nvJitLinkDestroy` 

148 """ 

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

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

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

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

153  

154  

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

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

157  

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

159  

160 Args: 

161 num_options (uint32_t): Number of options passed. 

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

163  

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

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

166 to a valid sequence of 'char', or 

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

168  

169 Returns: 

170 intptr_t: Address of nvJitLink handle. 

171  

172 .. seealso:: `nvJitLinkCreate` 

173 """ 

174 cdef _cyb_nested_resource[ char ] _options_ 

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

176 cdef Handle handle 

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

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

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

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

181  

182  

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

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

185  

186  

187 Args: 

188 handle (intptr_t): nvJitLink handle. 

189 input_type (InputType): kind of input. 

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

191 size (size_t): size of the data. 

192 name (str): name of input object. 

193  

194 .. seealso:: `nvJitLinkAddData` 

195 """ 

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

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

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

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

200 cdef char* _name_ = _temp_name_ 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG

201 with nogil: 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG

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

203 check_status(__status__) 1PQRSbcdefghijklmnopqTUVWrstuvwxyzABCDEFG

204  

205  

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

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

208  

209  

210 Args: 

211 handle (intptr_t): nvJitLink handle. 

212 input_type (InputType): kind of input. 

213 file_name (str): name of file. 

214  

215 .. seealso:: `nvJitLinkAddFile` 

216 """ 

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

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

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

220 cdef char* _file_name_ = _temp_file_name_ 1XYZ01234

221 with nogil: 1XYZ01234

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

223 check_status(__status__) 1XYZ01234

224  

225  

226cpdef complete(intptr_t handle): 

227 """nvJitLinkComplete does the actual link. 

228  

229  

230 Args: 

231 handle (intptr_t): nvJitLink handle. 

232  

233 .. seealso:: `nvJitLinkComplete` 

234 """ 

235 with nogil: 1PQRSXYZ05678HIJKbcdefghijklmnopqTUVW12349!#$LMNOrstuvwxyzABCDEFG

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

237 check_status(__status__) 1PQRSXYZ05678HIJKbcdefghijklmnopqTUVW12349!#$LMNOrstuvwxyzABCDEFG

238  

239  

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

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

242  

243  

244 Args: 

245 handle (intptr_t): nvJitLink handle. 

246  

247 Returns: 

248 size_t: Size of the linked cubin. 

249  

250 .. seealso:: `nvJitLinkGetLinkedCubinSize` 

251 """ 

252 cdef size_t size 

253 with nogil: 1fghivwxy

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

255 check_status(__status__) 1fghivwxy

256 return size 1fghivwxy

257  

258  

259cpdef get_linked_cubin(intptr_t handle, cubin): 

260 """nvJitLinkGetLinkedCubin gets the linked cubin. 

261  

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

263  

264 Args: 

265 handle (intptr_t): nvJitLink handle. 

266 cubin (bytes): The linked cubin. 

267  

268 .. seealso:: `nvJitLinkGetLinkedCubin` 

269 """ 

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

271 with nogil: 1fghivwxy

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

273 check_status(__status__) 1fghivwxy

274  

275  

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

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

278  

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

280  

281 Args: 

282 handle (intptr_t): nvJitLink handle. 

283  

284 Returns: 

285 size_t: Size of the linked PTX. 

286  

287 .. seealso:: `nvJitLinkGetLinkedPtxSize` 

288 """ 

289 cdef size_t size 

290 with nogil: 1nopqDEFG

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

292 check_status(__status__) 1nopqDEFG

293 return size 1nopqDEFG

294  

295  

296cpdef get_linked_ptx(intptr_t handle, ptx): 

297 """nvJitLinkGetLinkedPtx gets the linked ptx. 

298  

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

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

301  

302 Args: 

303 handle (intptr_t): nvJitLink handle. 

304 ptx (bytes): The linked PTX. 

305  

306 .. seealso:: `nvJitLinkGetLinkedPtx` 

307 """ 

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

309 with nogil: 1nopqDEFG

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

311 check_status(__status__) 1nopqDEFG

312  

313  

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

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

316  

317  

318 Args: 

319 handle (intptr_t): nvJitLink handle. 

320  

321 Returns: 

322 size_t: Size of the error log. 

323  

324 .. seealso:: `nvJitLinkGetErrorLogSize` 

325 """ 

326 cdef size_t size 

327 with nogil: 1HIJKLMNO

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

329 check_status(__status__) 1HIJKLMNO

330 return size 1HIJKLMNO

331  

332  

333cpdef get_error_log(intptr_t handle, log): 

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

335  

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

337  

338 Args: 

339 handle (intptr_t): nvJitLink handle. 

340 log (bytes): The error log. 

341  

342 .. seealso:: `nvJitLinkGetErrorLog` 

343 """ 

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

345 with nogil: 1HIJKLMNO

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

347 check_status(__status__) 1HIJKLMNO

348  

349  

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

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

352  

353  

354 Args: 

355 handle (intptr_t): nvJitLink handle. 

356  

357 Returns: 

358 size_t: Size of the info log. 

359  

360 .. seealso:: `nvJitLinkGetInfoLogSize` 

361 """ 

362 cdef size_t size 

363 with nogil: 1bcderstu

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

365 check_status(__status__) 1bcderstu

366 return size 1bcderstu

367  

368  

369cpdef get_info_log(intptr_t handle, log): 

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

371  

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

373  

374 Args: 

375 handle (intptr_t): nvJitLink handle. 

376 log (bytes): The info log. 

377  

378 .. seealso:: `nvJitLinkGetInfoLog` 

379 """ 

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

381 with nogil: 1bcderstu

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

383 check_status(__status__) 1bcderstu

384  

385  

386cpdef tuple version(): 

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

388  

389  

390 Returns: 

391 A 2-tuple containing: 

392 - unsigned int: The major version. 

393 - unsigned int: The minor version. 

394  

395 .. seealso:: `nvJitLinkVersion` 

396 """ 

397 cdef unsigned int major 

398 cdef unsigned int minor 

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

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

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

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

403  

404  

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

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

407  

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

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

410  

411 Args: 

412 handle (intptr_t): nvJitLink handle. 

413  

414 Returns: 

415 size_t: Size of the linked LTOIR. 

416  

417 .. seealso:: `nvJitLinkGetLinkedLTOIRSize` 

418 """ 

419 cdef size_t size 

420 with nogil: 1jklmzABC

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

422 check_status(__status__) 1jklmzABC

423 return size 1jklmzABC

424  

425  

426cpdef get_linked_ltoir(intptr_t handle, ltoir): 

427 """nvJitLinkGetLinkedLTOIR gets the linked LTOIR. 

428  

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

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

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

432  

433 Args: 

434 handle (intptr_t): nvJitLink handle. 

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

436  

437 .. seealso:: `nvJitLinkGetLinkedLTOIR` 

438 """ 

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

440 with nogil: 1jklmzABC

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

442 check_status(__status__) 1jklmzABC

443del _cyb_FastEnum