Skip to content

Conversation

abhilash1910
Copy link

@abhilash1910 abhilash1910 commented Aug 26, 2025

Description

Abstract : The cuda/bindings backend of Cuda python has NVVM support through libnvvm api . However the frontend of cuda python does not support nvvm ir as input source. Since cuda python allows users to leverage a "pythonic dsl" format for writing the host code (taking care of launch parameters etc), it makes sense to also allow NVVM IR as an alternative input to the already included list of inputs {ptx, c++, lto ir} etc.

Discussion Link: #906

Fix #452

Changes made {to be made} in this PR:

  • Added cuda core linkage to cuda bindings nvvm counterpart
  • Cosmetic changes in user interface to use existing nvvm backend of cuda bindings.

Checklist

  • [ TBD ] New tests needed to be added to cover these changes.
  • [ TBD ] The documentation needs to be updated with these changes.

cc @leofang

Copy link
Contributor

copy-pr-bot bot commented Aug 26, 2025

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@abhilash1910 abhilash1910 marked this pull request as draft August 26, 2025 13:51
Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low-level review: Apart from the bare except, this looks good to me.

I defer to @leofang for the high-level take.

@leofang leofang self-requested a review August 26, 2025 17:48
@leofang leofang added P0 High priority - Must do! feature New feature or request cuda.core Everything related to the cuda.core module labels Aug 26, 2025
@leofang leofang added this to the cuda.core beta 7 milestone Aug 26, 2025
Copy link
Member

@leofang leofang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @abhilash1910, left some quick comments, will circle back later.

@@ -544,6 +544,24 @@ def from_ltoir(module: Union[bytes, str], *, name: str = "", symbol_mapping: Opt
them (default to no mappings).
"""
return ObjectCode._init(module, "ltoir", name=name, symbol_mapping=symbol_mapping)

@staticmethod
def from_nvvm(module: Union[bytes, str], *, name: str = "", symbol_mapping: Optional[dict] = None) -> "ObjectCode":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: How do we plan to use NVVM IR wrapped as a ObjectCode in this case? It's not a valid input for any linker (nvjitlink/culink).

Comment on lines -12 to +16
from cuda.bindings import driver, nvrtc, runtime
from cuda.bindings import driver, nvrtc, nvvm, runtime
except ImportError:
from cuda import cuda as driver
from cuda import cudart as runtime
from cuda import nvrtc
from cuda import nvrtc, nvvm
Copy link
Member

@leofang leofang Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should import nvvm separately from all other modules. The NVVM bindings was added very recently (12.9.0), so it is not available for users on cuda.bindings 12.8 and below, but we don't want to force them to update unnecessarily for now. Today cuda.core works with any CUDA 12.x and 13.x.

Another note is that from cuda import nvvm is invalid. Always import it from cuda.bindings because it's where it is since it was added.

We should probably defer the import until it is actually needed, in Program when compiling NVVM IRs to PTX, and if NVVM is not available because of either reason

  1. cuda.bindings is not new enough
  2. libnvvm is not found, which can happen in Python environments (this can be checked by calling cuda.bindings._internal.nvvm._inspect_function_pointers())

then we raise an error.

We can drop this defer import by CUDA 14.0 (when we drop the support for 12.x).

@@ -27,6 +27,10 @@ class NVRTCError(CUDAError):
pass


class NVVMError(CUDAError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here's a bit of project history 😅 For the driver/runtime/nvrtc bindings, we did not raise exceptions if the C API returns a nonzero error code, and instead we return the error code as the first element of the returned tuple. So we had to cover these weird situations in cuda.core.

This is no longer the case for all other newer bindings such as nvJitLink and NVVM, which are auto-generated by another internal codegen. We already have the exception inspection done at the binding level, for example cuda.bindings.nvvm.NVVMError is a thing already. So we don't need to make any of the changes in this file, starting this line (you can see there's no special handling for nvJitLink in this file either).

@@ -103,13 +108,31 @@ cpdef inline int _check_nvrtc_error(error, handle=None) except?-1:
raise NVRTCError(err)


cpdef inline int _check_nvvm_error(error, handle=None) except?-1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, drop

cdef inline int _check_error(error, handle=None) except?-1:
if isinstance(error, driver.CUresult):
return _check_driver_error(error)
elif isinstance(error, runtime.cudaError_t):
return _check_runtime_error(error)
elif isinstance(error, nvrtc.nvrtcResult):
return _check_nvrtc_error(error, handle=handle)
elif isinstance(error, nvvm.Result):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, drop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cuda.core Everything related to the cuda.core module feature New feature or request P0 High priority - Must do!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support NVVM IRs as input to Program
3 participants