66
77#include < Python.h>
88#include < cuda.h>
9+ #include < nvrtc.h>
910#include < cstdint>
1011#include < memory>
1112
13+ // Forward declaration for NVVM - avoids nvvm.h dependency
14+ // Use void* to match cuda.bindings.cynvvm's typedef
15+ using nvvmProgram = void *;
16+
1217namespace cuda_core {
1318
1419// ============================================================================
@@ -67,6 +72,28 @@ extern decltype(&cuLibraryLoadData) p_cuLibraryLoadData;
6772extern decltype (&cuLibraryUnload) p_cuLibraryUnload;
6873extern decltype (&cuLibraryGetKernel) p_cuLibraryGetKernel;
6974
75+ // ============================================================================
76+ // NVRTC function pointers
77+ //
78+ // These are populated by _resource_handles.pyx at module import time using
79+ // function pointers extracted from cuda.bindings.cynvrtc.__pyx_capi__.
80+ // ============================================================================
81+
82+ extern decltype (&nvrtcDestroyProgram) p_nvrtcDestroyProgram;
83+
84+ // ============================================================================
85+ // NVVM function pointers
86+ //
87+ // These are populated by _resource_handles.pyx at module import time using
88+ // function pointers extracted from cuda.bindings.cynvvm.__pyx_capi__.
89+ // Note: May be null if NVVM is not available at runtime.
90+ // ============================================================================
91+
92+ // Function pointer type for nvvmDestroyProgram (avoids nvvm.h dependency)
93+ // Signature: nvvmResult nvvmDestroyProgram(nvvmProgram *prog)
94+ using NvvmDestroyProgramFn = int (*)(nvvmProgram*);
95+ extern NvvmDestroyProgramFn p_nvvmDestroyProgram;
96+
7097// ============================================================================
7198// Handle type aliases - expose only the raw CUDA resource
7299// ============================================================================
@@ -77,6 +104,8 @@ using EventHandle = std::shared_ptr<const CUevent>;
77104using MemoryPoolHandle = std::shared_ptr<const CUmemoryPool>;
78105using LibraryHandle = std::shared_ptr<const CUlibrary>;
79106using KernelHandle = std::shared_ptr<const CUkernel>;
107+ using NvrtcProgramHandle = std::shared_ptr<const nvrtcProgram>;
108+ using NvvmProgramHandle = std::shared_ptr<const nvvmProgram>;
80109
81110// ============================================================================
82111// Context handle functions
@@ -260,6 +289,33 @@ KernelHandle create_kernel_handle(const LibraryHandle& h_library, const char* na
260289// Use for borrowed kernels. The library handle keeps the library alive.
261290KernelHandle create_kernel_handle_ref (CUkernel kernel, const LibraryHandle& h_library);
262291
292+ // ============================================================================
293+ // NVRTC Program handle functions
294+ // ============================================================================
295+
296+ // Create an owning NVRTC program handle.
297+ // When the last reference is released, nvrtcDestroyProgram is called.
298+ // Use this to wrap a program created via nvrtcCreateProgram.
299+ NvrtcProgramHandle create_nvrtc_program_handle (nvrtcProgram prog);
300+
301+ // Create a non-owning NVRTC program handle (references existing program).
302+ // The program will NOT be destroyed when the handle is released.
303+ NvrtcProgramHandle create_nvrtc_program_handle_ref (nvrtcProgram prog);
304+
305+ // ============================================================================
306+ // NVVM Program handle functions
307+ // ============================================================================
308+
309+ // Create an owning NVVM program handle.
310+ // When the last reference is released, nvvmDestroyProgram is called.
311+ // Use this to wrap a program created via nvvmCreateProgram.
312+ // Note: If NVVM is not available (p_nvvmDestroyProgram is null), the deleter is a no-op.
313+ NvvmProgramHandle create_nvvm_program_handle (nvvmProgram prog);
314+
315+ // Create a non-owning NVVM program handle (references existing program).
316+ // The program will NOT be destroyed when the handle is released.
317+ NvvmProgramHandle create_nvvm_program_handle_ref (nvvmProgram prog);
318+
263319// ============================================================================
264320// Overloaded helper functions to extract raw resources from handles
265321// ============================================================================
@@ -293,6 +349,14 @@ inline CUkernel as_cu(const KernelHandle& h) noexcept {
293349 return h ? *h : nullptr ;
294350}
295351
352+ inline nvrtcProgram as_cu (const NvrtcProgramHandle& h) noexcept {
353+ return h ? *h : nullptr ;
354+ }
355+
356+ inline nvvmProgram as_cu (const NvvmProgramHandle& h) noexcept {
357+ return h ? *h : nullptr ;
358+ }
359+
296360// as_intptr() - extract handle as intptr_t for Python interop
297361// Using signed intptr_t per C standard convention and issue #1342
298362inline std::intptr_t as_intptr (const ContextHandle& h) noexcept {
@@ -323,11 +387,19 @@ inline std::intptr_t as_intptr(const KernelHandle& h) noexcept {
323387 return reinterpret_cast <std::intptr_t >(as_cu (h));
324388}
325389
326- // as_py() - convert handle to Python driver wrapper object (returns new reference)
390+ inline std::intptr_t as_intptr (const NvrtcProgramHandle& h) noexcept {
391+ return reinterpret_cast <std::intptr_t >(as_cu (h));
392+ }
393+
394+ inline std::intptr_t as_intptr (const NvvmProgramHandle& h) noexcept {
395+ return reinterpret_cast <std::intptr_t >(as_cu (h));
396+ }
397+
398+ // as_py() - convert handle to Python wrapper object (returns new reference)
327399namespace detail {
328400// n.b. class lookup is not cached to avoid deadlock hazard, see DESIGN.md
329- inline PyObject* make_py (const char * class_name, std::intptr_t value) noexcept {
330- PyObject* mod = PyImport_ImportModule (" cuda.bindings.driver " );
401+ inline PyObject* make_py (const char * module_name, const char * class_name, std::intptr_t value) noexcept {
402+ PyObject* mod = PyImport_ImportModule (module_name );
331403 if (!mod) return nullptr ;
332404 PyObject* cls = PyObject_GetAttrString (mod, class_name);
333405 Py_DECREF (mod);
@@ -339,31 +411,40 @@ inline PyObject* make_py(const char* class_name, std::intptr_t value) noexcept {
339411} // namespace detail
340412
341413inline PyObject* as_py (const ContextHandle& h) noexcept {
342- return detail::make_py (" CUcontext" , as_intptr (h));
414+ return detail::make_py (" cuda.bindings.driver " , " CUcontext" , as_intptr (h));
343415}
344416
345417inline PyObject* as_py (const StreamHandle& h) noexcept {
346- return detail::make_py (" CUstream" , as_intptr (h));
418+ return detail::make_py (" cuda.bindings.driver " , " CUstream" , as_intptr (h));
347419}
348420
349421inline PyObject* as_py (const EventHandle& h) noexcept {
350- return detail::make_py (" CUevent" , as_intptr (h));
422+ return detail::make_py (" cuda.bindings.driver " , " CUevent" , as_intptr (h));
351423}
352424
353425inline PyObject* as_py (const MemoryPoolHandle& h) noexcept {
354- return detail::make_py (" CUmemoryPool" , as_intptr (h));
426+ return detail::make_py (" cuda.bindings.driver " , " CUmemoryPool" , as_intptr (h));
355427}
356428
357429inline PyObject* as_py (const DevicePtrHandle& h) noexcept {
358- return detail::make_py (" CUdeviceptr" , as_intptr (h));
430+ return detail::make_py (" cuda.bindings.driver " , " CUdeviceptr" , as_intptr (h));
359431}
360432
361433inline PyObject* as_py (const LibraryHandle& h) noexcept {
362- return detail::make_py (" CUlibrary" , as_intptr (h));
434+ return detail::make_py (" cuda.bindings.driver " , " CUlibrary" , as_intptr (h));
363435}
364436
365437inline PyObject* as_py (const KernelHandle& h) noexcept {
366- return detail::make_py (" CUkernel" , as_intptr (h));
438+ return detail::make_py (" cuda.bindings.driver" , " CUkernel" , as_intptr (h));
439+ }
440+
441+ inline PyObject* as_py (const NvrtcProgramHandle& h) noexcept {
442+ return detail::make_py (" cuda.bindings.nvrtc" , " nvrtcProgram" , as_intptr (h));
443+ }
444+
445+ inline PyObject* as_py (const NvvmProgramHandle& h) noexcept {
446+ // NVVM bindings use raw integers, not wrapper classes
447+ return PyLong_FromSsize_t (as_intptr (h));
367448}
368449
369450} // namespace cuda_core
0 commit comments