Skip to content

Add nvvm bindings #421

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

Merged
merged 52 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
487e437
Add nvvm to setup.py
rwgk Jan 23, 2025
20ef456
Add test_nvvm.py
rwgk Jan 25, 2025
4c53023
test_nvvm.py version(), ir_version()
rwgk Jan 26, 2025
33e52ce
Snapshot of generated files.
rwgk Jan 26, 2025
49e26a4
Add in `nvvm.create_program()`
rwgk Jan 26, 2025
b8a1bf0
Add in `nvvm.destroy_program()`
rwgk Jan 26, 2025
508cbcd
Add in `nvvm.compile_program()`
rwgk Jan 26, 2025
15aa46a
Add in add_module_to_program()
rwgk Jan 26, 2025
9ecc390
Add in verify_program()
rwgk Jan 26, 2025
27fb042
Add in lazy_add_module_to_program()
rwgk Jan 26, 2025
3dfea4b
Add in get_compiled_result_size(), get_program_log_size()
rwgk Jan 27, 2025
760518f
Add in get_compiled_result(), get_program_log()
rwgk Jan 27, 2025
7c015b0
Merge branch 'main' into nvvm_bindings
rwgk Jan 27, 2025
f62e75a
Change Copyright dates to 2025
rwgk Jan 28, 2025
181f089
Use cybind results "automatically generated across versions from 12.0…
rwgk Jan 28, 2025
216026c
update to use NVKS runners
leofang Jan 30, 2025
7d3c86c
Add tests/run_simple.py
rwgk Jan 30, 2025
b8a9800
update fetch_ctk to find nvvm shared lib
leofang Jan 30, 2025
1ab9f63
fix wheel rel path
leofang Jan 30, 2025
164375e
add nvcc wheel to [all]
leofang Jan 30, 2025
86deeab
Fix cybind bindings for add_module_to_program(), lazy_add_module_to_p…
rwgk Jan 30, 2025
a2e2a2f
Add test_with_minimal_nnvm_ir()
rwgk Jan 30, 2025
fbf3a64
Remove tests/run_simple.py
rwgk Jan 30, 2025
44ae137
Merge branch 'nvvm_bindings' of https://github.com/rwgk/cuda-python i…
rwgk Jan 30, 2025
7265f48
Update cuda_bindings/cuda/bindings/_internal/nvvm_windows.pyx
rwgk Jan 30, 2025
9eb2211
Update cuda_bindings/cuda/bindings/_internal/nvvm_windows.pyx
rwgk Jan 30, 2025
d34a061
Merge branch 'nvvm_bindings' of https://github.com/rwgk/cuda-python i…
rwgk Jan 30, 2025
f0bdbbb
Remove stray `f` (it is now a plain string, not an f-string anymore)
rwgk Jan 30, 2025
3a129bc
Merge branch 'main' into nvvm_bindings
rwgk Feb 1, 2025
2c7d602
Merge branch 'main' into nvvm_bindings
rwgk Feb 4, 2025
1fc5d8e
Add bootstrap_local_dev.sh script.
rwgk Feb 4, 2025
cb991d7
Merge branch 'main' into nvvm_bindings
rwgk Feb 4, 2025
ee149f0
Fix nvvm.compile_program() failure for CUDA version 12.0
rwgk Feb 4, 2025
7833268
Add test_verify_program_with_minimal_nnvm_ir() and rename some tests …
rwgk Feb 4, 2025
5773d17
Complete test coverage.
rwgk Feb 4, 2025
0a603bf
Introduce noregex() to reduce backslash clutter.
rwgk Feb 4, 2025
a6890ea
Use a contextmanager to replace repeated try-finally.
rwgk Feb 4, 2025
4b83b20
Rename noregex to match_exact
rwgk Feb 4, 2025
acbe245
Introduce get_program_log() helper.
rwgk Feb 4, 2025
38f3729
Improve nvvm_program() Context Manager
rwgk Feb 4, 2025
8851d79
Remove redundant "utf-8"
rwgk Feb 4, 2025
81e023c
Also test with NVVM Bitcode (using a new pytest fixture).
rwgk Feb 4, 2025
e6eb2a0
Introduce compile_or_verify fixture.
rwgk Feb 5, 2025
9bfcf4d
Remove bootstrap_local_dev.sh, to be moved to a separate PR.
rwgk Feb 5, 2025
9ac1a9b
Update from codegen after config fix.
rwgk Feb 5, 2025
9d833f3
Update from codegen after config fix.
rwgk Feb 6, 2025
8e04fa7
Update from codegen after adding CTK 11.x nvvm.h headers. Functional …
rwgk Feb 6, 2025
b21f094
Fix get_nvvm_dso_version_suffix() to match actual version numbers:
rwgk Feb 6, 2025
b0f9a16
Merge branch 'main' into nvvm_bindings
rwgk Feb 7, 2025
b45bac2
find_libnvvm_so_via_proc_self_maps() Proof Of Concept
rwgk Feb 7, 2025
b17c363
Revert "find_libnvvm_so_via_proc_self_maps() Proof Of Concept"
rwgk Feb 7, 2025
be55676
Add another rpath for finding libnvvm.so
rwgk Feb 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/fetch_ctk/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,4 @@ runs:
echo "CUDA_PATH=${CUDA_PATH}" >> $GITHUB_ENV
echo "CUDA_HOME=${CUDA_PATH}" >> $GITHUB_ENV
echo "${CUDA_PATH}/bin" >> $GITHUB_PATH
echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-}:${CUDA_PATH}/lib" >> $GITHUB_ENV
echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-}:${CUDA_PATH}/lib:${CUDA_PATH}/nvvm/lib64" >> $GITHUB_ENV
25 changes: 25 additions & 0 deletions cuda_bindings/cuda/bindings/_internal/nvvm.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
#
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
#
# This code was automatically generated across versions from 11.0.3 to 12.8.0. Do not modify it directly.

from ..cynvvm cimport *


###############################################################################
# Wrapper functions
###############################################################################

cdef nvvmResult _nvvmVersion(int* major, int* minor) except* nogil
cdef nvvmResult _nvvmIRVersion(int* majorIR, int* minorIR, int* majorDbg, int* minorDbg) except* nogil
cdef nvvmResult _nvvmCreateProgram(nvvmProgram* prog) except* nogil
cdef nvvmResult _nvvmDestroyProgram(nvvmProgram* prog) except* nogil
cdef nvvmResult _nvvmAddModuleToProgram(nvvmProgram prog, const char* buffer, size_t size, const char* name) except* nogil
cdef nvvmResult _nvvmLazyAddModuleToProgram(nvvmProgram prog, const char* buffer, size_t size, const char* name) except* nogil
cdef nvvmResult _nvvmCompileProgram(nvvmProgram prog, int numOptions, const char** options) except* nogil
cdef nvvmResult _nvvmVerifyProgram(nvvmProgram prog, int numOptions, const char** options) except* nogil
cdef nvvmResult _nvvmGetCompiledResultSize(nvvmProgram prog, size_t* bufferSizeRet) except* nogil
cdef nvvmResult _nvvmGetCompiledResult(nvvmProgram prog, char* buffer) except* nogil
cdef nvvmResult _nvvmGetProgramLogSize(nvvmProgram prog, size_t* bufferSizeRet) except* nogil
cdef nvvmResult _nvvmGetProgramLog(nvvmProgram prog, char* buffer) except* nogil
360 changes: 360 additions & 0 deletions cuda_bindings/cuda/bindings/_internal/nvvm_linux.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,360 @@
# Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
#
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
#
# This code was automatically generated across versions from 11.0.3 to 12.8.0. Do not modify it directly.

from libc.stdint cimport intptr_t

from .utils cimport get_nvvm_dso_version_suffix

from .utils import FunctionNotFoundError, NotSupportedError

###############################################################################
# Extern
###############################################################################

cdef extern from "<dlfcn.h>" nogil:
void* dlopen(const char*, int)
char* dlerror()
void* dlsym(void*, const char*)
int dlclose(void*)

enum:
RTLD_LAZY
RTLD_NOW
RTLD_GLOBAL
RTLD_LOCAL

const void* RTLD_DEFAULT 'RTLD_DEFAULT'


###############################################################################
# Wrapper init
###############################################################################

cdef bint __py_nvvm_init = False
cdef void* __cuDriverGetVersion = NULL

cdef void* __nvvmVersion = NULL
cdef void* __nvvmIRVersion = NULL
cdef void* __nvvmCreateProgram = NULL
cdef void* __nvvmDestroyProgram = NULL
cdef void* __nvvmAddModuleToProgram = NULL
cdef void* __nvvmLazyAddModuleToProgram = NULL
cdef void* __nvvmCompileProgram = NULL
cdef void* __nvvmVerifyProgram = NULL
cdef void* __nvvmGetCompiledResultSize = NULL
cdef void* __nvvmGetCompiledResult = NULL
cdef void* __nvvmGetProgramLogSize = NULL
cdef void* __nvvmGetProgramLog = NULL


cdef void* load_library(const int driver_ver) except* with gil:
cdef void* handle
for suffix in get_nvvm_dso_version_suffix(driver_ver):
so_name = "libnvvm.so" + (f".{suffix}" if suffix else suffix)
handle = dlopen(so_name.encode(), RTLD_NOW | RTLD_GLOBAL)
if handle != NULL:
break
else:
err_msg = dlerror()
raise RuntimeError(f'Failed to dlopen libnvvm ({err_msg.decode()})')
return handle


cdef int _check_or_init_nvvm() except -1 nogil:
global __py_nvvm_init
if __py_nvvm_init:
return 0

# Load driver to check version
cdef void* handle = NULL
handle = dlopen('libcuda.so.1', RTLD_NOW | RTLD_GLOBAL)
if handle == NULL:
with gil:
err_msg = dlerror()
raise NotSupportedError(f'CUDA driver is not found ({err_msg.decode()})')
global __cuDriverGetVersion
if __cuDriverGetVersion == NULL:
__cuDriverGetVersion = dlsym(handle, "cuDriverGetVersion")
if __cuDriverGetVersion == NULL:
with gil:
raise RuntimeError('something went wrong')
cdef int err, driver_ver
err = (<int (*)(int*) nogil>__cuDriverGetVersion)(&driver_ver)
if err != 0:
with gil:
raise RuntimeError('something went wrong')
#dlclose(handle)
handle = NULL

# Load function
global __nvvmVersion
__nvvmVersion = dlsym(RTLD_DEFAULT, 'nvvmVersion')
if __nvvmVersion == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmVersion = dlsym(handle, 'nvvmVersion')

global __nvvmIRVersion
__nvvmIRVersion = dlsym(RTLD_DEFAULT, 'nvvmIRVersion')
if __nvvmIRVersion == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmIRVersion = dlsym(handle, 'nvvmIRVersion')

global __nvvmCreateProgram
__nvvmCreateProgram = dlsym(RTLD_DEFAULT, 'nvvmCreateProgram')
if __nvvmCreateProgram == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmCreateProgram = dlsym(handle, 'nvvmCreateProgram')

global __nvvmDestroyProgram
__nvvmDestroyProgram = dlsym(RTLD_DEFAULT, 'nvvmDestroyProgram')
if __nvvmDestroyProgram == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmDestroyProgram = dlsym(handle, 'nvvmDestroyProgram')

global __nvvmAddModuleToProgram
__nvvmAddModuleToProgram = dlsym(RTLD_DEFAULT, 'nvvmAddModuleToProgram')
if __nvvmAddModuleToProgram == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmAddModuleToProgram = dlsym(handle, 'nvvmAddModuleToProgram')

global __nvvmLazyAddModuleToProgram
__nvvmLazyAddModuleToProgram = dlsym(RTLD_DEFAULT, 'nvvmLazyAddModuleToProgram')
if __nvvmLazyAddModuleToProgram == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmLazyAddModuleToProgram = dlsym(handle, 'nvvmLazyAddModuleToProgram')

global __nvvmCompileProgram
__nvvmCompileProgram = dlsym(RTLD_DEFAULT, 'nvvmCompileProgram')
if __nvvmCompileProgram == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmCompileProgram = dlsym(handle, 'nvvmCompileProgram')

global __nvvmVerifyProgram
__nvvmVerifyProgram = dlsym(RTLD_DEFAULT, 'nvvmVerifyProgram')
if __nvvmVerifyProgram == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmVerifyProgram = dlsym(handle, 'nvvmVerifyProgram')

global __nvvmGetCompiledResultSize
__nvvmGetCompiledResultSize = dlsym(RTLD_DEFAULT, 'nvvmGetCompiledResultSize')
if __nvvmGetCompiledResultSize == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmGetCompiledResultSize = dlsym(handle, 'nvvmGetCompiledResultSize')

global __nvvmGetCompiledResult
__nvvmGetCompiledResult = dlsym(RTLD_DEFAULT, 'nvvmGetCompiledResult')
if __nvvmGetCompiledResult == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmGetCompiledResult = dlsym(handle, 'nvvmGetCompiledResult')

global __nvvmGetProgramLogSize
__nvvmGetProgramLogSize = dlsym(RTLD_DEFAULT, 'nvvmGetProgramLogSize')
if __nvvmGetProgramLogSize == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmGetProgramLogSize = dlsym(handle, 'nvvmGetProgramLogSize')

global __nvvmGetProgramLog
__nvvmGetProgramLog = dlsym(RTLD_DEFAULT, 'nvvmGetProgramLog')
if __nvvmGetProgramLog == NULL:
if handle == NULL:
handle = load_library(driver_ver)
__nvvmGetProgramLog = dlsym(handle, 'nvvmGetProgramLog')

__py_nvvm_init = True
return 0


cdef dict func_ptrs = None


cpdef dict _inspect_function_pointers():
global func_ptrs
if func_ptrs is not None:
return func_ptrs

_check_or_init_nvvm()
cdef dict data = {}

global __nvvmVersion
data["__nvvmVersion"] = <intptr_t>__nvvmVersion

global __nvvmIRVersion
data["__nvvmIRVersion"] = <intptr_t>__nvvmIRVersion

global __nvvmCreateProgram
data["__nvvmCreateProgram"] = <intptr_t>__nvvmCreateProgram

global __nvvmDestroyProgram
data["__nvvmDestroyProgram"] = <intptr_t>__nvvmDestroyProgram

global __nvvmAddModuleToProgram
data["__nvvmAddModuleToProgram"] = <intptr_t>__nvvmAddModuleToProgram

global __nvvmLazyAddModuleToProgram
data["__nvvmLazyAddModuleToProgram"] = <intptr_t>__nvvmLazyAddModuleToProgram

global __nvvmCompileProgram
data["__nvvmCompileProgram"] = <intptr_t>__nvvmCompileProgram

global __nvvmVerifyProgram
data["__nvvmVerifyProgram"] = <intptr_t>__nvvmVerifyProgram

global __nvvmGetCompiledResultSize
data["__nvvmGetCompiledResultSize"] = <intptr_t>__nvvmGetCompiledResultSize

global __nvvmGetCompiledResult
data["__nvvmGetCompiledResult"] = <intptr_t>__nvvmGetCompiledResult

global __nvvmGetProgramLogSize
data["__nvvmGetProgramLogSize"] = <intptr_t>__nvvmGetProgramLogSize

global __nvvmGetProgramLog
data["__nvvmGetProgramLog"] = <intptr_t>__nvvmGetProgramLog

func_ptrs = data
return data


cpdef _inspect_function_pointer(str name):
global func_ptrs
if func_ptrs is None:
func_ptrs = _inspect_function_pointers()
return func_ptrs[name]


###############################################################################
# Wrapper functions
###############################################################################

cdef nvvmResult _nvvmVersion(int* major, int* minor) except* nogil:
global __nvvmVersion
_check_or_init_nvvm()
if __nvvmVersion == NULL:
with gil:
raise FunctionNotFoundError("function nvvmVersion is not found")
return (<nvvmResult (*)(int*, int*) nogil>__nvvmVersion)(
major, minor)


cdef nvvmResult _nvvmIRVersion(int* majorIR, int* minorIR, int* majorDbg, int* minorDbg) except* nogil:
global __nvvmIRVersion
_check_or_init_nvvm()
if __nvvmIRVersion == NULL:
with gil:
raise FunctionNotFoundError("function nvvmIRVersion is not found")
return (<nvvmResult (*)(int*, int*, int*, int*) nogil>__nvvmIRVersion)(
majorIR, minorIR, majorDbg, minorDbg)


cdef nvvmResult _nvvmCreateProgram(nvvmProgram* prog) except* nogil:
global __nvvmCreateProgram
_check_or_init_nvvm()
if __nvvmCreateProgram == NULL:
with gil:
raise FunctionNotFoundError("function nvvmCreateProgram is not found")
return (<nvvmResult (*)(nvvmProgram*) nogil>__nvvmCreateProgram)(
prog)


cdef nvvmResult _nvvmDestroyProgram(nvvmProgram* prog) except* nogil:
global __nvvmDestroyProgram
_check_or_init_nvvm()
if __nvvmDestroyProgram == NULL:
with gil:
raise FunctionNotFoundError("function nvvmDestroyProgram is not found")
return (<nvvmResult (*)(nvvmProgram*) nogil>__nvvmDestroyProgram)(
prog)


cdef nvvmResult _nvvmAddModuleToProgram(nvvmProgram prog, const char* buffer, size_t size, const char* name) except* nogil:
global __nvvmAddModuleToProgram
_check_or_init_nvvm()
if __nvvmAddModuleToProgram == NULL:
with gil:
raise FunctionNotFoundError("function nvvmAddModuleToProgram is not found")
return (<nvvmResult (*)(nvvmProgram, const char*, size_t, const char*) nogil>__nvvmAddModuleToProgram)(
prog, buffer, size, name)


cdef nvvmResult _nvvmLazyAddModuleToProgram(nvvmProgram prog, const char* buffer, size_t size, const char* name) except* nogil:
global __nvvmLazyAddModuleToProgram
_check_or_init_nvvm()
if __nvvmLazyAddModuleToProgram == NULL:
with gil:
raise FunctionNotFoundError("function nvvmLazyAddModuleToProgram is not found")
return (<nvvmResult (*)(nvvmProgram, const char*, size_t, const char*) nogil>__nvvmLazyAddModuleToProgram)(
prog, buffer, size, name)


cdef nvvmResult _nvvmCompileProgram(nvvmProgram prog, int numOptions, const char** options) except* nogil:
global __nvvmCompileProgram
_check_or_init_nvvm()
if __nvvmCompileProgram == NULL:
with gil:
raise FunctionNotFoundError("function nvvmCompileProgram is not found")
return (<nvvmResult (*)(nvvmProgram, int, const char**) nogil>__nvvmCompileProgram)(
prog, numOptions, options)


cdef nvvmResult _nvvmVerifyProgram(nvvmProgram prog, int numOptions, const char** options) except* nogil:
global __nvvmVerifyProgram
_check_or_init_nvvm()
if __nvvmVerifyProgram == NULL:
with gil:
raise FunctionNotFoundError("function nvvmVerifyProgram is not found")
return (<nvvmResult (*)(nvvmProgram, int, const char**) nogil>__nvvmVerifyProgram)(
prog, numOptions, options)


cdef nvvmResult _nvvmGetCompiledResultSize(nvvmProgram prog, size_t* bufferSizeRet) except* nogil:
global __nvvmGetCompiledResultSize
_check_or_init_nvvm()
if __nvvmGetCompiledResultSize == NULL:
with gil:
raise FunctionNotFoundError("function nvvmGetCompiledResultSize is not found")
return (<nvvmResult (*)(nvvmProgram, size_t*) nogil>__nvvmGetCompiledResultSize)(
prog, bufferSizeRet)


cdef nvvmResult _nvvmGetCompiledResult(nvvmProgram prog, char* buffer) except* nogil:
global __nvvmGetCompiledResult
_check_or_init_nvvm()
if __nvvmGetCompiledResult == NULL:
with gil:
raise FunctionNotFoundError("function nvvmGetCompiledResult is not found")
return (<nvvmResult (*)(nvvmProgram, char*) nogil>__nvvmGetCompiledResult)(
prog, buffer)


cdef nvvmResult _nvvmGetProgramLogSize(nvvmProgram prog, size_t* bufferSizeRet) except* nogil:
global __nvvmGetProgramLogSize
_check_or_init_nvvm()
if __nvvmGetProgramLogSize == NULL:
with gil:
raise FunctionNotFoundError("function nvvmGetProgramLogSize is not found")
return (<nvvmResult (*)(nvvmProgram, size_t*) nogil>__nvvmGetProgramLogSize)(
prog, bufferSizeRet)


cdef nvvmResult _nvvmGetProgramLog(nvvmProgram prog, char* buffer) except* nogil:
global __nvvmGetProgramLog
_check_or_init_nvvm()
if __nvvmGetProgramLog == NULL:
with gil:
raise FunctionNotFoundError("function nvvmGetProgramLog is not found")
return (<nvvmResult (*)(nvvmProgram, char*) nogil>__nvvmGetProgramLog)(
prog, buffer)
Loading
Loading