diff --git a/naga/build.rs b/naga/build.rs index e647f1fdd..38b1a28a8 100644 --- a/naga/build.rs +++ b/naga/build.rs @@ -6,5 +6,7 @@ fn main() { msl_out: { any(feature = "msl-out", all(target_vendor = "apple", feature = "msl-out-if-target-apple")) }, spv_out: { feature = "spv-out" }, wgsl_out: { feature = "wgsl-out" }, + std: { any(test, spv_out, feature = "spv-in", feature = "wgsl-in", feature = "stderr") }, + no_std: { not(std) }, } } diff --git a/naga/src/back/glsl/keywords.rs b/naga/src/back/glsl/keywords.rs index b8f620dae..ed217d9e6 100644 --- a/naga/src/back/glsl/keywords.rs +++ b/naga/src/back/glsl/keywords.rs @@ -1,4 +1,4 @@ -use std::sync::LazyLock; +use crate::racy_lock::RacyLock; use hashbrown::HashSet; @@ -499,7 +499,7 @@ pub const RESERVED_KEYWORDS: &[&str] = &[ /// significant time during [`Namer::reset`](crate::proc::Namer::reset). /// /// See for benchmarks. -pub static RESERVED_KEYWORD_SET: LazyLock> = LazyLock::new(|| { +pub static RESERVED_KEYWORD_SET: RacyLock> = RacyLock::new(|| { let mut set = HashSet::default(); set.reserve(RESERVED_KEYWORDS.len()); for &word in RESERVED_KEYWORDS { diff --git a/naga/src/back/hlsl/keywords.rs b/naga/src/back/hlsl/keywords.rs index d3b9ad8cf..2d0d71282 100644 --- a/naga/src/back/hlsl/keywords.rs +++ b/naga/src/back/hlsl/keywords.rs @@ -1,4 +1,4 @@ -use std::sync::LazyLock; +use crate::racy_lock::RacyLock; use hashbrown::HashSet; @@ -924,7 +924,7 @@ pub const TYPES: &[&str] = &{ /// significant time during [`Namer::reset`](crate::proc::Namer::reset). /// /// See for benchmarks. -pub static RESERVED_SET: LazyLock> = LazyLock::new(|| { +pub static RESERVED_SET: RacyLock> = RacyLock::new(|| { let mut set = HashSet::default(); set.reserve(RESERVED.len() + TYPES.len()); for &word in RESERVED { diff --git a/naga/src/back/msl/keywords.rs b/naga/src/back/msl/keywords.rs index 4feb6004f..21113f5a0 100644 --- a/naga/src/back/msl/keywords.rs +++ b/naga/src/back/msl/keywords.rs @@ -1,4 +1,4 @@ -use std::sync::LazyLock; +use crate::racy_lock::RacyLock; use hashbrown::HashSet; @@ -360,7 +360,7 @@ pub const RESERVED: &[&str] = &[ /// significant time during [`Namer::reset`](crate::proc::Namer::reset). /// /// See for benchmarks. -pub static RESERVED_SET: LazyLock> = LazyLock::new(|| { +pub static RESERVED_SET: RacyLock> = RacyLock::new(|| { let mut set = HashSet::default(); set.reserve(RESERVED.len()); for &word in RESERVED { diff --git a/naga/src/back/pipeline_constants.rs b/naga/src/back/pipeline_constants.rs index 6c5be5852..1cf1c8052 100644 --- a/naga/src/back/pipeline_constants.rs +++ b/naga/src/back/pipeline_constants.rs @@ -16,6 +16,9 @@ use crate::{ Span, Statement, TypeInner, WithSpan, }; +#[cfg(no_std)] +use num_traits::float::FloatCore as _; + #[derive(Error, Debug, Clone)] #[cfg_attr(test, derive(PartialEq))] pub enum PipelineConstantError { diff --git a/naga/src/common/wgsl/to_wgsl.rs b/naga/src/common/wgsl/to_wgsl.rs index 426d15c5c..fcc7307f4 100644 --- a/naga/src/common/wgsl/to_wgsl.rs +++ b/naga/src/common/wgsl/to_wgsl.rs @@ -14,7 +14,7 @@ use alloc::string::{String, ToString}; /// /// - If a type's WGSL form requires dynamic formatting, so that /// returning a `&'static str` isn't feasible, consider implementing -/// [`std::fmt::Display`] on some wrapper type instead. +/// [`core::fmt::Display`] on some wrapper type instead. pub trait ToWgsl: Sized { /// Return WGSL source code representation of `self`. fn to_wgsl(self) -> &'static str; @@ -32,7 +32,7 @@ pub trait ToWgsl: Sized { /// /// - If a type's WGSL form requires dynamic formatting, so that /// returning a `&'static str` isn't feasible, consider implementing -/// [`std::fmt::Display`] on some wrapper type instead. +/// [`core::fmt::Display`] on some wrapper type instead. pub trait TryToWgsl: Sized { /// Return the WGSL form of `self` as a `'static` string. /// diff --git a/naga/src/keywords/wgsl.rs b/naga/src/keywords/wgsl.rs index 82be25ed4..df464a10f 100644 --- a/naga/src/keywords/wgsl.rs +++ b/naga/src/keywords/wgsl.rs @@ -4,7 +4,7 @@ Keywords for [WGSL][wgsl] (WebGPU Shading Language). [wgsl]: https://gpuweb.github.io/gpuweb/wgsl.html */ -use std::sync::LazyLock; +use crate::racy_lock::RacyLock; use hashbrown::HashSet; @@ -238,7 +238,7 @@ pub const RESERVED: &[&str] = &[ /// significant time during [`Namer::reset`](crate::proc::Namer::reset). /// /// See for benchmarks. -pub static RESERVED_SET: LazyLock> = LazyLock::new(|| { +pub static RESERVED_SET: RacyLock> = RacyLock::new(|| { let mut set = HashSet::default(); set.reserve(RESERVED.len()); for &word in RESERVED { diff --git a/naga/src/lib.rs b/naga/src/lib.rs index d440a61af..d6229742b 100644 --- a/naga/src/lib.rs +++ b/naga/src/lib.rs @@ -100,20 +100,7 @@ void main() { )] #![no_std] -#[cfg(any( - test, - spv_out, - - // Need OnceLock - hlsl_out, - msl_out, - wgsl_out, - - feature = "spv-in", - feature = "wgsl-in", - - feature = "stderr", -))] +#[cfg(std)] extern crate std; extern crate alloc; @@ -130,6 +117,7 @@ pub mod ir; pub mod keywords; mod non_max_u32; pub mod proc; +mod racy_lock; mod span; pub mod valid; diff --git a/naga/src/racy_lock.rs b/naga/src/racy_lock.rs new file mode 100644 index 000000000..116808055 --- /dev/null +++ b/naga/src/racy_lock.rs @@ -0,0 +1,41 @@ +#![cfg_attr( + not(any(glsl_out, hlsl_out, msl_out, feature = "wgsl-in", wgsl_out)), + expect( + dead_code, + reason = "RacyLock is only required for the above configurations" + ) +)] + +use alloc::boxed::Box; +use once_cell::race::OnceBox; + +/// An alternative to [`LazyLock`] based on [`OnceBox`]. +/// +/// [`LazyLock`]: https://doc.rust-lang.org/stable/std/sync/struct.LazyLock.html +pub struct RacyLock { + inner: OnceBox, + init: fn() -> T, +} + +impl RacyLock { + /// Creates a new [`RacyLock`], which will initialize using the provided `init` function. + pub const fn new(init: fn() -> T) -> Self { + Self { + inner: OnceBox::new(), + init, + } + } + + /// Loads the internal value, initializing it if required. + pub fn get(&self) -> &T { + self.inner.get_or_init(|| Box::new((self.init)())) + } +} + +impl core::ops::Deref for RacyLock { + type Target = T; + + fn deref(&self) -> &Self::Target { + self.get() + } +}