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

133 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-19 01:12 +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.3.0. Do not modify it directly. 

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

7  

8  

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

10  

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

12  

13  

14# <<<< END OF PREAMBLE CONTENT >>>> 

15  

16cimport cython # NOQA 

17  

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

19 get_buffer_pointer, get_resource_ptrs) 

20  

21from libcpp.vector cimport vector 

22  

23  

24############################################################################### 

25# Enum 

26############################################################################### 

27  

28class Result(_cyb_FastEnum): 

29 """ 

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

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

32  

33 See `nvJitLinkResult`. 

34 """ 

35 SUCCESS = NVJITLINK_SUCCESS 

36 ERROR_UNRECOGNIZED_OPTION = (NVJITLINK_ERROR_UNRECOGNIZED_OPTION, 'Unrecognized Option') 

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

38 ERROR_INVALID_INPUT = (NVJITLINK_ERROR_INVALID_INPUT, 'Invalid Input') 

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

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

41 ERROR_INTERNAL = (NVJITLINK_ERROR_INTERNAL, 'Internal Error') 

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

43 ERROR_UNRECOGNIZED_INPUT = (NVJITLINK_ERROR_UNRECOGNIZED_INPUT, 'Unrecognized Input') 

44 ERROR_FINALIZE = (NVJITLINK_ERROR_FINALIZE, 'Finalizer Error') 

45 ERROR_NULL_INPUT = (NVJITLINK_ERROR_NULL_INPUT, 'Null Input') 

46 ERROR_INCOMPATIBLE_OPTIONS = (NVJITLINK_ERROR_INCOMPATIBLE_OPTIONS, 'Incompatible Options') 

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

48 ERROR_ARCH_MISMATCH = (NVJITLINK_ERROR_ARCH_MISMATCH, 'Arch Mismatch') 

49 ERROR_OUTDATED_LIBRARY = (NVJITLINK_ERROR_OUTDATED_LIBRARY, 'Outdated Library') 

50 ERROR_MISSING_FATBIN = (NVJITLINK_ERROR_MISSING_FATBIN, 'Missing Fatbin') 

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

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

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

54  

55class InputType(_cyb_FastEnum): 

56 """ 

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

58 that can be passed to nvJitLinkAdd* APIs. 

59  

60 See `nvJitLinkInputType`. 

61 """ 

62 NONE = (NVJITLINK_INPUT_NONE, 'Error Type') 

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

64 PTX = (NVJITLINK_INPUT_PTX, 'For PTX') 

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

66 FATBIN = (NVJITLINK_INPUT_FATBIN, 'For Fatbin') 

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

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

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

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

71  

72  

73############################################################################### 

74# Error handling 

75############################################################################### 

76  

77class nvJitLinkError(Exception): 

78  

79 def __init__(self, status): 

80 self.status = status 1%'HI

81 s = Result(status) 1%'HI

82 cdef str err = f"{s.name} ({s.value})" 1%'HI

83 super(nvJitLinkError, self).__init__(err) 1%'HI

84  

85 def __reduce__(self): 

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

87  

88  

89@cython.profile(False) 

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

91 if status != 0: 1avwxyzABCDEFGJKLMNrstubcdefghijklmnopqHOIPQRSTUVWXYZ0123456789!#$

92 with gil: 1HI

93 raise nvJitLinkError(status) 1HI

94 return status 1avwxyzABCDEFGJKLMNrstubcdefghijklmnopqOPQRSTUVWXYZ0123456789!#$

95  

96  

97############################################################################### 

98# Wrapper functions 

99############################################################################### 

100  

101cpdef destroy(intptr_t handle): 

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

103  

104 Args: 

105 handle (intptr_t): nvJitLink handle. 

106  

107 .. seealso:: `nvJitLinkDestroy` 

108 """ 

109 cdef Handle h = <Handle>handle 1vwxyzABCDEFGJKLMNrstubcdefghijklmnopq

110 with nogil: 1vwxyzABCDEFGJKLMNrstubcdefghijklmnopq

111 status = nvJitLinkDestroy(&h) 1vwxyzABCDEFGJKLMNrstubcdefghijklmnopq

112 check_status(status) 1vwxyzABCDEFGJKLMNrstubcdefghijklmnopq

113  

114  

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

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

117  

118 Args: 

119 num_options (uint32_t): Number of options passed. 

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

121  

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

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

124 to a valid sequence of 'char', or 

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

126  

127  

128 Returns: 

129 intptr_t: Address of nvJitLink handle. 

130  

131 .. seealso:: `nvJitLinkCreate` 

132 """ 

133 cdef nested_resource[ char ] _options_ 

134 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1vwxyzABCDEFGJKLMNrstubcdefghijklmnopqHI

135 cdef Handle handle 

136 with nogil: 1vwxyzABCDEFGJKLMNrstubcdefghijklmnopqHI

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

138 check_status(__status__) 1vwxyzABCDEFGJKLMNrstubcdefghijklmnopqHI

139 return <intptr_t>handle 1vwxyzABCDEFGJKLMNrstubcdefghijklmnopq

140  

141  

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

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

144  

145 Args: 

146 handle (intptr_t): nvJitLink handle. 

147 input_type (InputType): kind of input. 

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

149 size (size_t): size of the data. 

150 name (str): name of input object. 

151  

152 .. seealso:: `nvJitLinkAddData` 

153 """ 

154 cdef void* _data_ = get_buffer_pointer(data, size, readonly=True) 1vwxybcdefghijklmnopq

155 if not isinstance(name, str): 1vwxybcdefghijklmnopq

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

157 cdef bytes _temp_name_ = (<str>name).encode() 1vwxybcdefghijklmnopq

158 cdef char* _name_ = _temp_name_ 1vwxybcdefghijklmnopq

159 with nogil: 1vwxybcdefghijklmnopq

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

161 check_status(__status__) 1vwxybcdefghijklmnopq

162  

163  

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

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

166  

167 Args: 

168 handle (intptr_t): nvJitLink handle. 

169 input_type (InputType): kind of input. 

170 file_name (str): name of file. 

171  

172 .. seealso:: `nvJitLinkAddFile` 

173 """ 

174 if not isinstance(file_name, str): 1zABC

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

176 cdef bytes _temp_file_name_ = (<str>file_name).encode() 1zABC

177 cdef char* _file_name_ = _temp_file_name_ 1zABC

178 with nogil: 1zABC

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

180 check_status(__status__) 1zABC

181  

182  

183cpdef complete(intptr_t handle): 

184 """nvJitLinkComplete does the actual link. 

185  

186 Args: 

187 handle (intptr_t): nvJitLink handle. 

188  

189 .. seealso:: `nvJitLinkComplete` 

190 """ 

191 with nogil: 1vwxyzABCDEFGrstubcdefghijklmnopq

192 __status__ = nvJitLinkComplete(<Handle>handle) 1vwxyzABCDEFGrstubcdefghijklmnopq

193 check_status(__status__) 1vwxyzABCDEFGrstubcdefghijklmnopq

194  

195  

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

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

198  

199 Args: 

200 handle (intptr_t): nvJitLink handle. 

201  

202 Returns: 

203 size_t: Size of the linked cubin. 

204  

205 .. seealso:: `nvJitLinkGetLinkedCubinSize` 

206 """ 

207 cdef size_t size 

208 with nogil: 1fghi

209 __status__ = nvJitLinkGetLinkedCubinSize(<Handle>handle, &size) 1fghi

210 check_status(__status__) 1fghi

211 return size 1fghi

212  

213  

214cpdef get_linked_cubin(intptr_t handle, cubin): 

215 """nvJitLinkGetLinkedCubin gets the linked cubin. 

216  

217 Args: 

218 handle (intptr_t): nvJitLink handle. 

219 cubin (bytes): The linked cubin. 

220  

221 .. seealso:: `nvJitLinkGetLinkedCubin` 

222 """ 

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

224 with nogil: 1fghi

225 __status__ = nvJitLinkGetLinkedCubin(<Handle>handle, <void*>_cubin_) 1fghi

226 check_status(__status__) 1fghi

227  

228  

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

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

231  

232 Args: 

233 handle (intptr_t): nvJitLink handle. 

234  

235 Returns: 

236 size_t: Size of the linked PTX. 

237  

238 .. seealso:: `nvJitLinkGetLinkedPtxSize` 

239 """ 

240 cdef size_t size 

241 with nogil: 1nopq

242 __status__ = nvJitLinkGetLinkedPtxSize(<Handle>handle, &size) 1nopq

243 check_status(__status__) 1nopq

244 return size 1nopq

245  

246  

247cpdef get_linked_ptx(intptr_t handle, ptx): 

248 """nvJitLinkGetLinkedPtx gets the linked ptx. 

249  

250 Args: 

251 handle (intptr_t): nvJitLink handle. 

252 ptx (bytes): The linked PTX. 

253  

254 .. seealso:: `nvJitLinkGetLinkedPtx` 

255 """ 

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

257 with nogil: 1nopq

258 __status__ = nvJitLinkGetLinkedPtx(<Handle>handle, <char*>_ptx_) 1nopq

259 check_status(__status__) 1nopq

260  

261  

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

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

264  

265 Args: 

266 handle (intptr_t): nvJitLink handle. 

267  

268 Returns: 

269 size_t: Size of the error log. 

270  

271 .. seealso:: `nvJitLinkGetErrorLogSize` 

272 """ 

273 cdef size_t size 

274 with nogil: 1rstu

275 __status__ = nvJitLinkGetErrorLogSize(<Handle>handle, &size) 1rstu

276 check_status(__status__) 1rstu

277 return size 1rstu

278  

279  

280cpdef get_error_log(intptr_t handle, log): 

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

282  

283 Args: 

284 handle (intptr_t): nvJitLink handle. 

285 log (bytes): The error log. 

286  

287 .. seealso:: `nvJitLinkGetErrorLog` 

288 """ 

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

290 with nogil: 1rstu

291 __status__ = nvJitLinkGetErrorLog(<Handle>handle, <char*>_log_) 1rstu

292 check_status(__status__) 1rstu

293  

294  

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

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

297  

298 Args: 

299 handle (intptr_t): nvJitLink handle. 

300  

301 Returns: 

302 size_t: Size of the info log. 

303  

304 .. seealso:: `nvJitLinkGetInfoLogSize` 

305 """ 

306 cdef size_t size 

307 with nogil: 1bcde

308 __status__ = nvJitLinkGetInfoLogSize(<Handle>handle, &size) 1bcde

309 check_status(__status__) 1bcde

310 return size 1bcde

311  

312  

313cpdef get_info_log(intptr_t handle, log): 

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

315  

316 Args: 

317 handle (intptr_t): nvJitLink handle. 

318 log (bytes): The info log. 

319  

320 .. seealso:: `nvJitLinkGetInfoLog` 

321 """ 

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

323 with nogil: 1bcde

324 __status__ = nvJitLinkGetInfoLog(<Handle>handle, <char*>_log_) 1bcde

325 check_status(__status__) 1bcde

326  

327  

328cpdef tuple version(): 

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

330  

331 Returns: 

332 A 2-tuple containing: 

333  

334 - unsigned int: The major version. 

335 - unsigned int: The minor version. 

336  

337 .. seealso:: `nvJitLinkVersion` 

338 """ 

339 cdef unsigned int major 

340 cdef unsigned int minor 

341 with nogil: 1aOPQRSTUVWXYZ0123456789!#$

342 __status__ = nvJitLinkVersion(&major, &minor) 1aOPQRSTUVWXYZ0123456789!#$

343 check_status(__status__) 1aOPQRSTUVWXYZ0123456789!#$

344 return (major, minor) 1aOPQRSTUVWXYZ0123456789!#$

345  

346  

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

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

349  

350 Args: 

351 handle (intptr_t): nvJitLink handle. 

352  

353 Returns: 

354 size_t: Size of the linked LTOIR. 

355  

356 .. seealso:: `nvJitLinkGetLinkedLTOIRSize` 

357 """ 

358 cdef size_t size 

359 with nogil: 1jklm

360 __status__ = nvJitLinkGetLinkedLTOIRSize(<Handle>handle, &size) 1jklm

361 check_status(__status__) 1jklm

362 return size 1jklm

363  

364  

365cpdef get_linked_ltoir(intptr_t handle, ltoir): 

366 """nvJitLinkGetLinkedLTOIR gets the linked LTOIR. 

367  

368 Args: 

369 handle (intptr_t): nvJitLink handle. 

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

371  

372 .. seealso:: `nvJitLinkGetLinkedLTOIR` 

373 """ 

374 cdef void* _ltoir_ = get_buffer_pointer(ltoir, -1, readonly=False) 1jklm

375 with nogil: 1jklm

376 __status__ = nvJitLinkGetLinkedLTOIR(<Handle>handle, <void*>_ltoir_) 1jklm

377 check_status(__status__) 1jklm

378del _cyb_FastEnum