rusticl: add a safe abstraction to execute a ProgramCB
authorLingMan <18294-LingMan@users.noreply.gitlab.freedesktop.org>
Fri, 13 Oct 2023 22:29:08 +0000 (00:29 +0200)
committerMarge Bot <emma+marge@anholt.net>
Sun, 15 Oct 2023 00:17:10 +0000 (00:17 +0000)
Reviewed-by: Karol Herbst <kherbst@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25669>

src/gallium/frontends/rusticl/api/program.rs
src/gallium/frontends/rusticl/api/types.rs

index 1cef458..fc1040f 100644 (file)
@@ -289,7 +289,7 @@ fn build_program(
     }
 
     if let Some(cb) = cb_opt {
-        unsafe { (cb.func)(program, cb.data) };
+        cb.call(p);
     }
 
     //• CL_INVALID_BINARY if program is created with clCreateProgramWithBinary and devices listed in device_list do not have a valid program binary loaded.
@@ -375,7 +375,7 @@ fn compile_program(
     }
 
     if let Some(cb) = cb_opt {
-        unsafe { (cb.func)(program, cb.data) };
+        cb.call(p);
     }
 
     // • CL_INVALID_COMPILER_OPTIONS if the compiler options specified by options are invalid.
@@ -447,13 +447,11 @@ pub fn link_program(
         CL_LINK_PROGRAM_FAILURE
     };
 
-    let res = cl_program::from_arc(res);
-
     if let Some(cb) = cb_opt {
-        unsafe { (cb.func)(res, cb.data) };
+        cb.call(&res);
     }
 
-    Ok((res, code))
+    Ok((cl_program::from_arc(res), code))
 
     //• CL_INVALID_LINKER_OPTIONS if the linker options specified by options are invalid.
     //• CL_INVALID_OPERATION if the rules for devices containing compiled binaries or libraries as described in input_programs argument above are not followed.
index cdca8f3..39a9a02 100644 (file)
@@ -3,6 +3,7 @@ use crate::api::icd::ReferenceCountedAPIPointer;
 use crate::core::context::Context;
 use crate::core::event::Event;
 use crate::core::memory::Mem;
+use crate::core::program::Program;
 use crate::core::queue::Queue;
 
 use rusticl_opencl_gen::*;
@@ -178,6 +179,15 @@ cl_callback!(
     }
 );
 
+impl ProgramCB {
+    pub fn call(self, program: &Program) {
+        let cl = cl_program::from_ptr(program);
+        // SAFETY: `cl` must have pointed to an OpenCL program, which is where we just got it from.
+        // All other requirements are covered by this callback's type invariants.
+        unsafe { (self.func)(cl, self.data) };
+    }
+}
+
 cl_callback!(
     SVMFreeCb(FuncSVMFreeCb) {
         queue: cl_command_queue,