From c440beb1717ce5bc0c2c4058d714179b4e8c1174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=C3=A1n=20de=20B=C3=BArca?= Date: Sat, 13 Sep 2025 07:43:24 -0700 Subject: [PATCH] rusticl/kernel: add Kernel::mut_ref_from_raw() The OpenCL spec indicates that functions which modify `cl_kernel` are not thread-safe, allowing us to handle those functions with standard mutability. Reviewed-by: Karol Herbst Part-of: --- src/gallium/frontends/rusticl/core/kernel.rs | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/gallium/frontends/rusticl/core/kernel.rs b/src/gallium/frontends/rusticl/core/kernel.rs index 30c6c3fb2c6..03fa0d29a26 100644 --- a/src/gallium/frontends/rusticl/core/kernel.rs +++ b/src/gallium/frontends/rusticl/core/kernel.rs @@ -1407,6 +1407,34 @@ impl Kernel { }) } + /// Returns a mutable reference to the `Kernel` corresponding to the + /// provided pointer. + /// + /// # Safety + /// + /// The provided pointer must be valid for a read of `ptr` size. + /// Additionally, no other threads must hold a reference to this + /// `cl_kernel`. + pub unsafe fn mut_ref_from_raw<'a>(kernel: cl_kernel) -> CLResult<&'a mut Self> { + let ty = crate::api::icd::CLObjectBase::::check_ptr(kernel.cast())?; + if ty != RusticlTypes::Kernel { + return Err(CL_INVALID_KERNEL); + } + + let offset = ::std::mem::offset_of!(Kernel, base); + + // SAFETY: We offset the pointer back from the ICD specified base type to our + // internal type. + let obj_ptr: *mut Self = unsafe { kernel.byte_sub(offset) }.cast(); + + // Check at compile-time that we indeed got the right path + unsafe { + let _: &crate::api::icd::CLObjectBase = &(*obj_ptr).base; + } + + Ok(unsafe { &mut *obj_ptr }) + } + pub fn suggest_local_size( &self, d: &Device,