1use std::path::PathBuf;
2
3use cubecl::config::cache::CacheConfig;
4use cubecl::config::{CubeClRuntimeConfig, RuntimeConfig};
5
6pub const COMPILATION_CACHE_ENV: &str = "AV_DENOISE_COMPILATION_CACHE";
9
10#[derive(Debug, thiserror::Error)]
13#[error(
14 "CubeCL global config already initialized. Call apply_compilation_cache_env() before any Denoiser::create"
15)]
16pub struct CacheAlreadyInitialisedError;
17
18pub fn apply_compilation_cache_env() -> Result<Option<PathBuf>, CacheAlreadyInitialisedError> {
26 let Some(raw) = std::env::var_os(COMPILATION_CACHE_ENV) else {
27 return Ok(None);
28 };
29 let path = PathBuf::from(raw);
30
31 let mut cfg = CubeClRuntimeConfig::from_current_dir().override_from_env();
32 cfg.compilation.cache = Some(CacheConfig::File(path.clone()));
33 cfg.autotune.cache = CacheConfig::File(path.clone());
34
35 std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
42 CubeClRuntimeConfig::set(cfg);
43 }))
44 .map_err(|_| CacheAlreadyInitialisedError)?;
45
46 Ok(Some(path))
47}