rusticl/platform: extract env variable parsing from Platform::init
authorKarol Herbst <kherbst@redhat.com>
Mon, 24 Apr 2023 11:06:01 +0000 (13:06 +0200)
committerMarge Bot <emma+marge@anholt.net>
Tue, 25 Apr 2023 04:27:57 +0000 (04:27 +0000)
In our platform initialization code we might want to access the parsed env
variables already. So do this in separate steps.

Signed-off-by: Karol Herbst <kherbst@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22649>

src/gallium/frontends/rusticl/api/program.rs
src/gallium/frontends/rusticl/core/platform.rs
src/gallium/frontends/rusticl/core/program.rs

index 6bc5319..d987a9e 100644 (file)
@@ -291,7 +291,7 @@ pub fn build_program(
     if res {
         Ok(())
     } else {
-        if Platform::get().debug.program {
+        if Platform::dbg().program {
             for dev in &devs {
                 eprintln!("{}", p.log(dev));
             }
@@ -370,7 +370,7 @@ pub fn compile_program(
     if res {
         Ok(())
     } else {
-        if Platform::get().debug.program {
+        if Platform::dbg().program {
             for dev in &devs {
                 eprintln!("{}", p.log(dev));
             }
index 0a3e22e..e5c9005 100644 (file)
@@ -15,14 +15,15 @@ pub struct Platform {
     dispatch: &'static cl_icd_dispatch,
     pub extensions: [cl_name_version; 2],
     pub devs: Vec<Arc<Device>>,
-    pub debug: PlatformDebug,
 }
 
 pub struct PlatformDebug {
     pub program: bool,
 }
 
+static PLATFORM_ENV_ONCE: Once = Once::new();
 static PLATFORM_ONCE: Once = Once::new();
+
 static mut PLATFORM: Platform = Platform {
     dispatch: &DISPATCH,
     extensions: [
@@ -30,8 +31,20 @@ static mut PLATFORM: Platform = Platform {
         mk_cl_version_ext(1, 0, 0, "cl_khr_il_program"),
     ],
     devs: Vec::new(),
-    debug: PlatformDebug { program: false },
 };
+static mut PLATFORM_DBG: PlatformDebug = PlatformDebug { program: false };
+
+fn load_env() {
+    let debug = unsafe { &mut PLATFORM_DBG };
+    if let Ok(debug_flags) = env::var("RUSTICL_DEBUG") {
+        for flag in debug_flags.split(',') {
+            match flag {
+                "program" => debug.program = true,
+                _ => eprintln!("Unknown RUSTICL_DEBUG flag found: {}", flag),
+            }
+        }
+    }
+}
 
 impl Platform {
     pub fn as_ptr(&self) -> cl_platform_id {
@@ -44,23 +57,21 @@ impl Platform {
         unsafe { &PLATFORM }
     }
 
+    pub fn dbg() -> &'static PlatformDebug {
+        debug_assert!(PLATFORM_ENV_ONCE.is_completed());
+        unsafe { &PLATFORM_DBG }
+    }
+
     fn init(&mut self) {
         unsafe {
             glsl_type_singleton_init_or_ref();
         }
 
         self.devs.extend(Device::all());
-        if let Ok(debug_flags) = env::var("RUSTICL_DEBUG") {
-            for flag in debug_flags.split(',') {
-                match flag {
-                    "program" => self.debug.program = true,
-                    _ => eprintln!("Unknown RUSTICL_DEBUG flag found: {}", flag),
-                }
-            }
-        }
     }
 
     pub fn init_once() {
+        PLATFORM_ENV_ONCE.call_once(load_env);
         // SAFETY: no concurrent static mut access due to std::Once
         PLATFORM_ONCE.call_once(|| unsafe { PLATFORM.init() });
     }
index 530b49e..afed3c8 100644 (file)
@@ -569,7 +569,7 @@ impl Program {
         let info = Self::dev_build_info(&mut lock, d);
         assert_eq!(info.status, CL_BUILD_SUCCESS as cl_build_status);
 
-        let mut log = Platform::get().debug.program.then(Vec::new);
+        let mut log = Platform::dbg().program.then(Vec::new);
         let nir = info.spirv.as_ref().unwrap().to_nir(
             kernel,
             d.screen