Skip to content

Add kernel bundle states, kernel ids and SYCL library loading - #2440

Open
ndgrigorian wants to merge 10 commits into
masterfrom
feature/kernel-bundle-api
Open

ndgrigorian wants to merge 10 commits into
masterfrom
feature/kernel-bundle-api

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

This PR proposes expanding dpctl.compiler to include kernel bundles in the input, object and executable states, get_kernel_bundle and has_kernel_bundle, compile, link, build and join, the SyclKernelId and SyclDeviceImage classes, get_kernel_ids, is_compatible, and new SyclKernelBundle queries (state, backend, sycl_context, get_devices, is_empty, get_kernel_ids, has_kernel, get_kernel, get_device_images, specialization constant queries)

It also adds dpctl.compiler.load_kernel_library, which loads a shared library with SYCL device code, e.g. compiled for CUDA or HIP devices, and returns the ids of its kernels for use with get_kernel_bundle, and dpctl4pybind11.hpp type casters for sycl::kernel_bundle<sycl::bundle_state::input>, sycl::kernel_bundle<sycl::bundle_state::object> and sycl::kernel_id, with a new examples/pybind11/use_kernel_bundle example that sets a SYCL specialization constant on a kernel bundle obtained in Python

This includes a breaking change for dpctl.compiler.SyclKernel: the device-specific queries become methods taking an optional device, which defaults to the first device of the kernel bundle, and the matching DPCTLKernel_Get* C-API functions take a nullable DPCTLSyclDeviceRef. These queries now return 0 instead of terminating the process when the SYCL runtime throws

The PR also fixes a number of bugs in the existing kernel bundle C API:

  • DPCTLKernelBundle_GetKernel, DPCTLKernelBundle_HasKernel and DPCTLKernelBundle_CreateFromOCLSource let SYCL exceptions escape an extern "C" function, terminating the process
  • the OpenCL context, program and kernel references obtained through get_native and make_kernel_bundle/make_kernel were leaked
  • the Level Zero module and kernels were created with ownership::keep and never destroyed
  • the OpenCL and Level Zero build logs are now included in the error reported when compilation fails
  • DynamicLibHelper no longer closes a NULL handle when a library fails to open
  • create_kernel_bundle_from_spirv rejects non-contiguous buffers which were passed to the C API as contiguous
  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?
  • If this PR is a work in progress, are you opening the PR as a draft?

Wrap sycl::kernel_bundle in input, object and executable states, kernel_id
and device images, with get_kernel_bundle, compile, link, build and join in
the C API and dpctl.compiler. DPCTLKernel_GetNumArgs no longer terminates
for kernels without a number of arguments.
SyclKernel device-specific queries become methods with an optional device,
and DPCTLKernel_Get* take a nullable device (breaking). get_kernel_bundle
accepts a device image selector, and dpctl4pybind11.hpp casts input and
object bundles and kernel ids, used by the new use_kernel_bundle example.
Load a shared library with SYCL device code, e.g. built for CUDA or HIP,
and return the ids of the kernels it registered, for get_kernel_bundle.
Exposed as dpctl.compiler.load_kernel_library and DPCTLKernelId_LoadLibrary.
The IL memoryview was typed as strided but passed to the C API as a
contiguous pointer and length. Declare it as const unsigned char[::1] so
non-contiguous buffers are rejected.
The destructor called dlclose/FreeLibrary even when the library failed to
open.
DPCTLKernelBundle_GetKernel, DPCTLKernelBundle_HasKernel and
DPCTLKernelBundle_CreateFromOCLSource could let SYCL exceptions escape an
extern "C" function, which terminates the process. Report them through
error_handler and return the function's failure value.
get_native and make_kernel_bundle/make_kernel retain the OpenCL context,
program and kernel, so the references obtained by dpctl were leaked.
Release them with a scoped guard, and release the kernels created by
_HasKernel_ocl_impl.
The module and kernels were created with ownership::keep and never
destroyed. Use ownership::transfer, destroy them if interop fails, and
destroy the kernel created by _HasKernel_ze_impl.
Append the clBuildProgram or zeModuleCreate build log to the error
reported through error_handler.
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

A Debug build defines _DEBUG, which makes Windows pyconfig.h enable
Py_DEBUG and reference symbols missing from the release python lib.

This branch has not been deployed

No deployments
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.

1 participant