From 0125e865a124077e57f318ad08e00eb5166a3b28 Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Mon, 29 Jan 2024 11:45:06 +0100 Subject: [PATCH] rusticl/icd: actually allow dispatching CL types Part-of: --- src/gallium/frontends/rusticl/api/icd.rs | 47 +++++++++++++++----- src/gallium/frontends/rusticl/core/device.rs | 2 +- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/gallium/frontends/rusticl/api/icd.rs b/src/gallium/frontends/rusticl/api/icd.rs index 15337c68d15..9d71eec8099 100644 --- a/src/gallium/frontends/rusticl/api/icd.rs +++ b/src/gallium/frontends/rusticl/api/icd.rs @@ -173,7 +173,7 @@ pub static DISPATCH: cl_icd_dispatch = cl_icd_dispatch { pub type CLError = cl_int; pub type CLResult = Result; -#[derive(Clone, Copy)] +#[derive(Clone, Copy, PartialEq)] #[repr(u32)] pub enum RusticlTypes { // random number @@ -191,6 +191,22 @@ impl RusticlTypes { pub const fn u32(&self) -> u32 { *self as u32 } + + pub const fn from_u32(val: u32) -> Option { + let result = match val { + 0xec4cf9a9 => Self::Context, + 0xec4cf9aa => Self::Device, + 0xec4cf9ab => Self::Event, + 0xec4cf9ac => Self::Kernel, + 0xec4cf9ad => Self::Mem, + 0xec4cf9ae => Self::Program, + 0xec4cf9af => Self::Queue, + 0xec4cf9b0 => Self::Sampler, + _ => return None, + }; + debug_assert!(result.u32() == val); + Some(result) + } } #[repr(C)] @@ -207,7 +223,7 @@ impl CLObjectBase { } } - pub fn check_ptr(ptr: *const Self) -> CLResult<()> { + pub fn check_ptr(ptr: *const Self) -> CLResult { if ptr.is_null() { return Err(ERR); } @@ -217,13 +233,17 @@ impl CLObjectBase { return Err(ERR); } - if (*ptr).rusticl_type != RUSTICL_TYPE { + let Some(ty) = RusticlTypes::from_u32((*ptr).rusticl_type) else { return Err(ERR); - } + }; - Ok(()) + Ok(ty) } } + + pub fn get_type(&self) -> CLResult { + RusticlTypes::from_u32(self.rusticl_type).ok_or(ERR) + } } pub trait ReferenceCountedAPIPointer { @@ -340,11 +360,14 @@ pub trait ArcedCLObject<'a, const ERR: i32, CL: ReferenceCountedAPIPointer { + (@BASE $cl: ident, $t: ident, [$($types: ident),+], $err: ident, $($field:ident).+) => { impl $crate::api::icd::ReferenceCountedAPIPointer<$t, $err> for $cl { fn get_ptr(&self) -> CLResult<*const $t> { type Base = $crate::api::icd::CLObjectBase<$err>; - Base::check_ptr::<{ RusticlTypes::$t.u32() }>(self.cast())?; + let t = Base::check_ptr(self.cast())?; + if ![$($crate::api::icd::RusticlTypes::$types),+].contains(&t) { + return Err($err); + } let offset = ::mesa_rust_util::offset_of!($t, $($field).+); let mut obj_ptr: *const u8 = self.cast(); @@ -391,20 +414,20 @@ macro_rules! impl_cl_type_trait_base { } }; - ($cl: ident, $t: ident, $err: ident, $($field:ident).+) => { - $crate::impl_cl_type_trait_base!(@BASE $cl, $t, $err, $($field).+); + ($cl: ident, $t: ident, [$($types: ident),+], $err: ident, $($field:ident).+) => { + $crate::impl_cl_type_trait_base!(@BASE $cl, $t, [$($types),+], $err, $($field).+); impl $crate::api::icd::CLObject<'_, $err, $cl> for $t {} }; - ($cl: ident, $t: ident, $err: ident) => { - $crate::impl_cl_type_trait_base!($cl, $t, $err, base); + ($cl: ident, $t: ident, [$($types: ident),+], $err: ident) => { + $crate::impl_cl_type_trait_base!($cl, $t, [$($types),+], $err, base); }; } #[macro_export] macro_rules! impl_cl_type_trait { ($cl: ident, $t: ident, $err: ident, $($field:ident).+) => { - $crate::impl_cl_type_trait_base!(@BASE $cl, $t, $err, $($field).+); + $crate::impl_cl_type_trait_base!(@BASE $cl, $t, [$t], $err, $($field).+); impl $crate::api::icd::ArcedCLObject<'_, $err, $cl> for $t {} }; diff --git a/src/gallium/frontends/rusticl/core/device.rs b/src/gallium/frontends/rusticl/core/device.rs index af3dbf7099b..da422cde928 100644 --- a/src/gallium/frontends/rusticl/core/device.rs +++ b/src/gallium/frontends/rusticl/core/device.rs @@ -212,7 +212,7 @@ impl<'a> HelperContextWrapper for HelperContext<'a> { } } -impl_cl_type_trait_base!(cl_device_id, Device, CL_INVALID_DEVICE); +impl_cl_type_trait_base!(cl_device_id, Device, [Device], CL_INVALID_DEVICE); impl Device { fn new(screen: PipeScreen) -> Option {