Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 10 additions & 2 deletions cuda_bindings/cuda/bindings/_internal/utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE

cimport cpython
from cpython.bytes cimport PyBytes_AsString
from libc.stdint cimport intptr_t
from libcpp.utility cimport move
from cython.operator cimport dereference as deref
Expand Down Expand Up @@ -120,10 +121,17 @@ cdef int get_nested_resource_ptr(nested_resource[ResT] &in_out_ptr, object obj,
nested_ptr.reset(nested_vec, True)
for i, obj_i in enumerate(obj):
if ResT is char:
obj_i_bytes = (<str?>(obj_i)).encode()
obj_i_type = type(obj_i)
if obj_i_type is str:
obj_i_bytes = obj_i.encode("utf-8")
elif obj_i_type is bytes:
obj_i_bytes = obj_i
else:
raise TypeError(
f"Expected str or bytes, got {obj_i_type.__name__}")
str_len = <size_t>(len(obj_i_bytes)) + 1 # including null termination
deref(nested_res_vec)[i].resize(str_len)
obj_i_ptr = <char*>(obj_i_bytes)
obj_i_ptr = PyBytes_AsString(obj_i_bytes)
Comment on lines -126 to +134
Copy link
Member

Choose a reason for hiding this comment

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

In general we should let Cython figure out the correct C API to use, and not intervene with manual C API calls unless we can prove

  1. Cython emits wrong code, or
  2. We can do it with an equally safe but more performant way

# cast to size_t explicitly to work around a potentially Cython bug
deref(nested_res_vec)[i].assign(obj_i_ptr, obj_i_ptr + <size_t>str_len)
else:
Expand Down
7 changes: 7 additions & 0 deletions cuda_bindings/tests/test_nvjitlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ def test_create_and_destroy(option):
nvjitlink.destroy(handle)


@pytest.mark.parametrize("option", ARCHITECTURES)
Copy link
Member

Choose a reason for hiding this comment

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

no need to parametrize it, just one bytes instance is fine

def test_create_and_destroy_bytes_options(option):
handle = nvjitlink.create(1, [f"-arch={option}".encode()])
assert handle != 0
nvjitlink.destroy(handle)


@pytest.mark.parametrize("option", ARCHITECTURES)
def test_complete_empty(option):
handle = nvjitlink.create(1, [f"-arch={option}"])
Expand Down
8 changes: 6 additions & 2 deletions cuda_bindings/tests/test_nvvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ def test_get_buffer_empty(get_size, get_buffer):
assert buffer == b"\x00"


@pytest.mark.parametrize("options", [[], ["-opt=0"], ["-opt=3", "-g"]])
@pytest.mark.parametrize(
"options", [[], ["-opt=0"], ["-opt=3", "-g"], [b"-opt=0"], [b"-opt=3", b"-g"], ["-opt=3", b"-g"]]
)
Comment on lines +118 to +120
Copy link
Member

Choose a reason for hiding this comment

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

ditto

def test_compile_program_with_minimal_nvvm_ir(minimal_nvvmir, options): # noqa: F401, F811
with nvvm_program() as prog:
nvvm.add_module_to_program(prog, minimal_nvvmir, len(minimal_nvvmir), "FileNameHere.ll")
Expand All @@ -135,7 +137,9 @@ def test_compile_program_with_minimal_nvvm_ir(minimal_nvvmir, options): # noqa:
assert ".visible .entry kernel()" in buffer.decode()


@pytest.mark.parametrize("options", [[], ["-opt=0"], ["-opt=3", "-g"]])
@pytest.mark.parametrize(
"options", [[], ["-opt=0"], ["-opt=3", "-g"], [b"-opt=0"], [b"-opt=3", b"-g"], ["-opt=3", b"-g"]]
)
Comment on lines +140 to +142
Copy link
Member

Choose a reason for hiding this comment

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

ditto; also, if this is already tested above, no need to test again

def test_verify_program_with_minimal_nvvm_ir(minimal_nvvmir, options): # noqa: F401, F811
with nvvm_program() as prog:
nvvm.add_module_to_program(prog, minimal_nvvmir, len(minimal_nvvmir), "FileNameHere.ll")
Expand Down
Loading