use std::sync::atomic::{AtomicUsize, Ordering};
pub const COMPILE_OUTPUT_CAP_ENV: &str = "RLX_COMPILE_OUTPUT_CAP";
pub const COMPILE_OUTPUT_CAP_ENV_MLX: &str = "RLX_MLX_COMPILE_OUTPUT_CAP";
pub const DEFAULT_COMPILE_OUTPUT_CAP: usize = 1024;
static EXPLICIT_COMPILE_OUTPUT_CAP: AtomicUsize = AtomicUsize::new(0);
fn cap_from_env() -> usize {
for key in [COMPILE_OUTPUT_CAP_ENV, COMPILE_OUTPUT_CAP_ENV_MLX] {
if let Ok(v) = std::env::var(key) {
if let Ok(n) = v.parse::<usize>() {
if n > 0 {
return n;
}
}
}
}
DEFAULT_COMPILE_OUTPUT_CAP
}
pub fn compile_output_cap() -> usize {
let explicit = EXPLICIT_COMPILE_OUTPUT_CAP.load(Ordering::Relaxed);
if explicit != 0 {
return explicit;
}
cap_from_env()
}
pub fn set_compile_output_cap(cap: usize) {
EXPLICIT_COMPILE_OUTPUT_CAP.store(cap, Ordering::Relaxed);
sync_mlx_compile_output_cap(cap);
}
pub fn reset_compile_output_cap() {
EXPLICIT_COMPILE_OUTPUT_CAP.store(0, Ordering::Relaxed);
sync_mlx_compile_output_cap(0);
}
pub fn device_has_compile_output_cap(device: rlx_driver::Device) -> bool {
use rlx_driver::Device;
matches!(device, Device::Mlx)
}
#[cfg(all(feature = "mlx", rlx_mlx_host))]
fn sync_mlx_compile_output_cap(cap: usize) {
if cap == 0 {
rlx_mlx::reset_compile_output_cap();
} else {
rlx_mlx::set_compile_output_cap(cap);
}
}
#[cfg(not(all(feature = "mlx", rlx_mlx_host)))]
fn sync_mlx_compile_output_cap(_cap: usize) {}