Module: Mesa
Branch: main
Commit: 389a199993e0c20306a3fb343724bfbf00a278cb
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=389a199993e0c20306a3fb343724bfbf00a278cb

Author: Karol Herbst <[email protected]>
Date:   Mon Apr 24 13:06:01 2023 +0200

rusticl/platform: extract env variable parsing from Platform::init

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 <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22649>

---

 src/gallium/frontends/rusticl/api/program.rs   |  4 ++--
 src/gallium/frontends/rusticl/core/platform.rs | 31 +++++++++++++++++---------
 src/gallium/frontends/rusticl/core/program.rs  |  2 +-
 3 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/src/gallium/frontends/rusticl/api/program.rs 
b/src/gallium/frontends/rusticl/api/program.rs
index 6bc53193c39..d987a9eb868 100644
--- a/src/gallium/frontends/rusticl/api/program.rs
+++ b/src/gallium/frontends/rusticl/api/program.rs
@@ -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));
             }
diff --git a/src/gallium/frontends/rusticl/core/platform.rs 
b/src/gallium/frontends/rusticl/core/platform.rs
index 0a3e22e8af5..e5c9005f69d 100644
--- a/src/gallium/frontends/rusticl/core/platform.rs
+++ b/src/gallium/frontends/rusticl/core/platform.rs
@@ -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() });
     }
diff --git a/src/gallium/frontends/rusticl/core/program.rs 
b/src/gallium/frontends/rusticl/core/program.rs
index 530b49e2577..afed3c8dea4 100644
--- a/src/gallium/frontends/rusticl/core/program.rs
+++ b/src/gallium/frontends/rusticl/core/program.rs
@@ -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

Reply via email to