use std::path::PathBuf;
use cubecl::config::cache::CacheConfig;
use cubecl::config::{CubeClRuntimeConfig, RuntimeConfig};
pub const COMPILATION_CACHE_ENV: &str = "AV_DENOISE_COMPILATION_CACHE";
#[derive(Debug, thiserror::Error)]
#[error(
"CubeCL global config already initialized. Call apply_compilation_cache_env() before any Denoiser::create"
)]
pub struct CacheAlreadyInitialisedError;
pub fn apply_compilation_cache_env() -> Result<Option<PathBuf>, CacheAlreadyInitialisedError> {
let Some(raw) = std::env::var_os(COMPILATION_CACHE_ENV) else {
return Ok(None);
};
let path = PathBuf::from(raw);
let mut cfg = CubeClRuntimeConfig::from_current_dir().override_from_env();
cfg.compilation.cache = Some(CacheConfig::File(path.clone()));
cfg.autotune.cache = CacheConfig::File(path.clone());
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
CubeClRuntimeConfig::set(cfg);
}))
.map_err(|_| CacheAlreadyInitialisedError)?;
Ok(Some(path))
}