-
Notifications
You must be signed in to change notification settings - Fork 201
[NVVM IR] NVVM IR Integration #907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
There was a problem hiding this 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": |
There was a problem hiding this comment.
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).
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 |
There was a problem hiding this comment.
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
- cuda.bindings is not new enough
- 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): |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto, drop
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:
Checklist
cc @leofang