Repository navigation
Fix kernel launches on CPU devices - #61
Conversation
cdbc52a to
93339da
Compare
The launch functions in sycl-rs-sys took each kernel argument as plain bytes and always handed them to raw_kernel_arg, so the C++ shim could not tell a USM pointer from a value. A backend may take pointers through a different entry point than plain bytes: the OpenCL CPU device rejects USM pointers passed through raw_kernel_arg. Pass each argument as a KernelArg with a kind. Bytes still go through raw_kernel_arg, and pointers go through the typed handler::set_arg overload. sycl-rs passes every argument as Bytes for now, so behaviour is unchanged. Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
KernelArgument::as_raw_arg and KernelArgumentList::as_raw_arg_list returned plain byte slices. Return a RawKernelArg instead, so that a later change can mark USM pointers. Every argument is still passed as bytes, so behaviour is unchanged. RawKernelArg is non-exhaustive so that more kinds can be added without a breaking change. Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
UsmBox kernel arguments were passed as plain bytes. On the OpenCL CPU device, kernel launches with them failed with UR_RESULT_ERROR_INVALID_MEM_OBJECT, so no kernel could run on a CPU. Add RawKernelArg::Pointer, return it for UsmBox arguments, and require KernelArgument and KernelArgumentList implementations to mark USM pointers this way. The launch bridge then passes them through handler::set_arg. Since the pointer bytes now matter, take their size from the NonNull<T> field instead of an unrelated *const u8. Add a regression test, which fails without this change on the OpenCL CPU device. It prefers the CPU device, because the default device is a GPU when there is one, and GPUs accepted pointers passed as plain bytes. Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
93339da to
c8c5b58
Compare
|
Hi @ChihweiLHBird, it's great to see you contributing to oneAPI-rs. Are you interested in using this crate in your projects? |
There was a problem hiding this comment.
🔵 Needs a closer look
CPU use of raw_kernel_arg, documented as GPU-only, requires regression testing on the affected oneAPI 2026.1 Intel OpenCL CPU runtime and confirmation of the existing GPU path beyond source review.
No open issues 🎉
What changed in this PR
Fixes #62 by distinguishing USM pointers from plain-value kernel arguments throughout the Rust-to-SYCL launch bridge, allowing launches on OpenCL CPU devices.
Changes:
- Introduces
RawKernelArgand carries argument kinds through traits, derives, and launch interfaces. - Passes USM pointers through typed
handler::set_arg, while retainingraw_kernel_argfor plain values. - Adds a CPU-preferred regression test mixing scalar and pointer arguments.
| File | Description |
|---|---|
sycl/sycl-rs/tests/launch.rs |
Adds a CPU-preferred SAXPY launch regression test. |
sycl/sycl-rs/src/usmbox.rs |
Marks owned and borrowed USM arguments as pointers. |
sycl/sycl-rs/src/range.rs |
Converts typed arguments for all launch dimensions. |
sycl/sycl-rs/src/prelude.rs |
Exports RawKernelArg through the prelude. |
sycl/sycl-rs/src/kernel.rs |
Defines argument kinds and updates argument traits. |
sycl/sycl-rs-sys/src/types-sys.rs |
Adds argument-kind and argument-record bridge types. |
sycl/sycl-rs-sys/src/queue.cpp |
Dispatches pointer and value arguments separately. |
sycl/sycl-rs-sys/src/queue-sys.rs |
Updates launch bindings and safety contracts. |
sycl/sycl-rs-sys/include/queue.hpp |
Updates C++ launch declarations for typed arguments. |
sycl/sycl-rs-derive/src/lib.rs |
Updates generated argument lists to preserve argument kinds. |
CHANGELOG.md |
Documents pointer-aware kernel argument handling. |
Review effort: Balanced
|
Hi @bratpiorka, yes I plan to use this crate in a prototype/learning project. No rush, and let me know if you need anything from my side. Thank you so much! |
szymon-zadworny
left a comment
There was a problem hiding this comment.
Good job! The PR works as expected and it fixes a valid issue. I only have some minor concerns regarding comments and the error handling. The fixes are simple and shouldn't take too long to implement :)
Replace the into_ffi method with a From implementation, the idiomatic way to express this conversion. Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
The shim checked that a pointer argument was pointer-sized and threw otherwise. Move the check to the From conversion, which every kernel argument goes through, and make it a panic: no kernel can run with a malformed pointer argument, so there is nothing to recover from. The check also covers alignment, so the shim can read the pointer in place instead of copying it, which keeps the shim minimal. The shim's throw for an unknown argument kind goes too, since sycl-rs only builds the two declared kinds. The launch functions' safety docs now state both requirements for anyone calling them directly. Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
KernelArg pairs a kind with bytes because cxx shared enums cannot hold data, and the shim passes pointers through the typed set_arg overload until the pointer_arg form of raw_kernel_arg reaches a stable Intel LLVM release. Say so where each workaround lives. Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
There was a problem hiding this comment.
🟡 Changes recommended
The pointer-argument path in queue.cpp dereferences byte storage as a C++ pointer object, causing undefined behavior despite size and alignment checks; it must instead copy the representation into a local pointer with std::memcpy.
1 open issue
Review effort: Balanced
| // typed set_arg overload until the pointer_arg form of raw_kernel_arg | ||
| // (intel/llvm#23479) lands in a stable Intel LLVM release, where | ||
| // SYCL_EXT_ONEAPI_RAW_KERNEL_ARG is at least 2. | ||
| cgh.set_arg(index, *reinterpret_cast<void *const *>(arg.bytes.data())); |
There was a problem hiding this comment.
This is actualy mostly safe here. The bytes are a pointer value written by Rust, the Rust side asserts their size and alignment before the call, and the handler copies them immediately. That been said, I am also open to picking memcpy back if needed. @szymon-zadworny what do you think?
There was a problem hiding this comment.
Yes, this is safe. What Copilot mentions is related to C++ pointer aliasing compiler optimizations. Those cannot happen on the FFI boundary, since arg.bytes.data() is stored by Rust.
szymon-zadworny
left a comment
There was a problem hiding this comment.
Awesome, let's merge this.

Fixes #62
Change
USM pointers are marked as pointers and passed with
handler::set_arg. Plain values still useraw_kernel_arg. The spec's newerpointer_argform isn't in oneAPI 2026.1.RawKernelArgfromKernelArgument. No behaviour change.UsmBoxarguments as pointers, and add a regression test that prefers the CPU device.