Skip to content

Fix kernel launches on CPU devices - #61

Merged
szymon-zadworny merged 6 commits into
oneapi-src:mainfrom
ChihweiLHBird:cpu-pointer-args
Nov 12, 2026
Merged

szymon-zadworny merged 6 commits into
oneapi-src:mainfrom
ChihweiLHBird:cpu-pointer-args

Conversation

@ChihweiLHBird

@ChihweiLHBird ChihweiLHBird commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #62

Change

USM pointers are marked as pointers and passed with handler::set_arg. Plain values still use raw_kernel_arg. The spec's newer pointer_arg form isn't in oneAPI 2026.1.

  1. Carry a kind for each kernel argument through the launch bridge. No behaviour change.
  2. Return RawKernelArg from KernelArgument. No behaviour change.
  3. Mark UsmBox arguments as pointers, and add a regression test that prefers the CPU device.

@ChihweiLHBird
ChihweiLHBird marked this pull request as draft November 1, 2026 01:19
@ChihweiLHBird
ChihweiLHBird force-pushed the cpu-pointer-args branch 2 times, most recently from cdbc52a to 93339da Compare November 1, 2026 01:41
@ChihweiLHBird
ChihweiLHBird marked this pull request as ready for review November 1, 2026 04:09
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>
@bratpiorka

Copy link
Copy Markdown
Contributor

Hi @ChihweiLHBird, it's great to see you contributing to oneAPI-rs. Are you interested in using this crate in your projects?
Give us a little time to look over your feedback and changes as we've got a busy queue right now - we'll start from the OpenCL support first

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 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 RawKernelArg and carries argument kinds through traits, derives, and launch interfaces.
  • Passes USM pointers through typed handler::set_arg, while retaining raw_kernel_arg for 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

@ChihweiLHBird

Copy link
Copy Markdown
Contributor Author

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 szymon-zadworny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 :)

Comment thread sycl/sycl-rs-sys/src/types-sys.rs Resolved
Comment thread sycl/sycl-rs/src/usmbox.rs Resolved
Comment thread sycl/sycl-rs-sys/src/queue.cpp Resolved
Comment thread sycl/sycl-rs-sys/src/queue.cpp Resolved
Comment thread sycl/sycl-rs-sys/src/queue.cpp Resolved
Comment thread sycl/sycl-rs-sys/src/queue.cpp Resolved
Comment thread sycl/sycl-rs-sys/src/queue.cpp Resolved
Comment thread sycl/sycl-rs/src/kernel.rs Resolved
Comment thread sycl/sycl-rs-sys/src/queue.cpp Resolved
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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()));

@ChihweiLHBird ChihweiLHBird Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 szymon-zadworny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Awesome, let's merge this.

@szymon-zadworny
szymon-zadworny merged commit 74b8bed into oneapi-src:main Nov 12, 2026
@ChihweiLHBird
ChihweiLHBird deleted the cpu-pointer-args branch November 12, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kernels with USM pointer arguments fail on the OpenCL CPU device

4 participants