Allow Naga spv-in and spv-out in no_std (#7760)

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
This commit is contained in:
Zachary Harrold 2025-06-27 06:45:47 +10:00 committed by GitHub
parent 0d61648eeb
commit 4c39227510
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 273 additions and 105 deletions

View File

@ -296,7 +296,7 @@ jobs:
# Check with all compatible features
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu-types --no-default-features --features strict_asserts,fragile-send-sync-non-atomic-wasm,serde,counters
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p naga --no-default-features --features dot-out
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p naga --no-default-features --features dot-out,spv-in,spv-out
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu-hal --no-default-features --features fragile-send-sync-non-atomic-wasm
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu --no-default-features --features serde

View File

@ -38,6 +38,7 @@ naga = { workspace = true, features = [
"deserialize",
"termcolor",
"stderr",
"fs",
] }
bincode.workspace = true

View File

@ -424,7 +424,7 @@ fn run() -> anyhow::Result<()> {
params.spv_in = naga::front::spv::Options {
adjust_coordinate_space: !args.keep_coordinate_space,
strict_capabilities: false,
block_ctx_dump_prefix: args.block_ctx_dir.clone().map(std::path::PathBuf::from),
block_ctx_dump_prefix: args.block_ctx_dir.clone().map(Into::into),
};
params.entry_point.clone_from(&args.entry_point);
@ -486,7 +486,7 @@ fn run() -> anyhow::Result<()> {
.set(naga::back::spv::WriterFlags::DEBUG, true);
params.spv_out.debug_info = Some(naga::back::spv::DebugInfo {
source_code: input_text,
file_name: input_path,
file_name: input_path.into(),
language,
})
} else {

View File

@ -23,7 +23,6 @@ default = []
dot-out = []
glsl-in = ["dep:pp-rs"]
glsl-out = []
std = []
## Enables outputting to the Metal Shading Language (MSL).
##
@ -79,6 +78,9 @@ termcolor = ["codespan-reporting/termcolor"]
## Enables writing output to stderr.
stderr = ["codespan-reporting/std"]
## Enables integration with the underlying filesystem.
fs = []
[dependencies]
arbitrary = { workspace = true, features = ["derive"], optional = true }
arrayvec.workspace = true

View File

@ -6,7 +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") },
std: { any(test, feature = "wgsl-in", feature = "stderr", feature = "fs") },
no_std: { not(std) },
}
}

View File

@ -1,79 +0,0 @@
//! [`AsDiagnosticFilePath`] and its supporting items.
use alloc::borrow::Cow;
#[cfg(feature = "std")]
use std::path::Path;
#[cfg(not(feature = "std"))]
use alloc::string::String;
mod sealed {
pub trait Sealed {}
}
/// A trait that abstracts over types accepted for conversion to the most
/// featureful path representation possible; that is:
///
/// - When `no_std` is active, this is implemented for [`String`], [`str`], and [`Cow`] (i.e.,
/// `Cow<'_, str>`).
/// - Otherwise, types that implement `AsRef<Path>` (to extract a `&Path`).
///
/// This type is used as the type bounds for various diagnostic rendering methods, i.e.,
/// [`WithSpan::emit_to_string_with_path`](crate::span::WithSpan::emit_to_string_with_path).
///
/// [`String`]: alloc::string::String
pub trait AsDiagnosticFilePath: sealed::Sealed {
fn to_string_lossy(&self) -> Cow<'_, str>;
}
#[cfg(feature = "std")]
impl<T: AsRef<Path> + ?Sized> AsDiagnosticFilePath for T {
fn to_string_lossy(&self) -> Cow<'_, str> {
self.as_ref().to_string_lossy()
}
}
#[cfg(feature = "std")]
impl<T: AsRef<Path> + ?Sized> sealed::Sealed for T {}
#[cfg(not(feature = "std"))]
impl AsDiagnosticFilePath for String {
fn to_string_lossy(&self) -> Cow<'_, str> {
Cow::Borrowed(self.as_str())
}
}
#[cfg(not(feature = "std"))]
impl sealed::Sealed for String {}
#[cfg(not(feature = "std"))]
impl AsDiagnosticFilePath for str {
fn to_string_lossy(&self) -> Cow<'_, str> {
Cow::Borrowed(self)
}
}
#[cfg(not(feature = "std"))]
impl sealed::Sealed for str {}
#[cfg(not(feature = "std"))]
impl AsDiagnosticFilePath for Cow<'_, str> {
fn to_string_lossy(&self) -> Cow<'_, str> {
use core::borrow::Borrow;
Cow::Borrowed(self.borrow())
}
}
#[cfg(not(feature = "std"))]
impl sealed::Sealed for Cow<'_, str> {}
#[cfg(not(feature = "std"))]
impl<T: AsDiagnosticFilePath + ?Sized> AsDiagnosticFilePath for &T {
fn to_string_lossy(&self) -> Cow<'_, str> {
(*self).to_string_lossy()
}
}
#[cfg(not(feature = "std"))]
impl<T: AsDiagnosticFilePath + ?Sized> sealed::Sealed for &T {}

View File

@ -25,6 +25,7 @@ use spirv::Word;
use thiserror::Error;
use crate::arena::{Handle, HandleVec};
use crate::path_like::PathLikeRef;
use crate::proc::{BoundsCheckPolicies, TypeResolution};
#[derive(Clone)]
@ -92,7 +93,7 @@ impl IdGenerator {
#[derive(Debug, Clone)]
pub struct DebugInfo<'a> {
pub source_code: &'a str,
pub file_name: &'a std::path::Path,
pub file_name: PathLikeRef<'a>,
pub language: SourceLanguage,
}

View File

@ -1,8 +1,4 @@
use alloc::{
string::{String, ToString},
vec,
vec::Vec,
};
use alloc::{string::String, vec, vec::Vec};
use hashbrown::hash_map::Entry;
use spirv::Word;
@ -18,6 +14,7 @@ use super::{
use crate::{
arena::{Handle, HandleVec, UniqueArena},
back::spv::{BindingInfo, WrappedFunction},
path_like::PathLike,
proc::{Alignment, TypeResolution},
valid::{FunctionInfo, ModuleInfo},
};
@ -2415,7 +2412,7 @@ impl Writer {
if let Some(debug_info) = debug_info.as_ref() {
let source_file_id = self.id_gen.next();
self.debugs.push(Instruction::string(
&debug_info.file_name.display().to_string(),
&debug_info.file_name.to_string_lossy(),
source_file_id,
));

View File

@ -169,10 +169,18 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
Some(ep) => format!("block_ctx.{:?}-{}.txt", ep.stage, ep.name),
None => format!("block_ctx.Fun-{}.txt", function_index),
};
let dest = prefix.join(dump_suffix);
let dump = format!("{block_ctx:#?}");
if let Err(e) = std::fs::write(&dest, dump) {
log::error!("Unable to dump the block context into {:?}: {}", dest, e);
cfg_if::cfg_if! {
if #[cfg(feature = "fs")] {
let prefix: &std::path::Path = prefix.as_ref();
let dest = prefix.join(dump_suffix);
let dump = format!("{block_ctx:#?}");
if let Err(e) = std::fs::write(&dest, dump) {
log::error!("Unable to dump the block context into {:?}: {}", dest, e);
}
} else {
log::error!("Unable to dump the block context into {:?}/{}: file system integration was not enabled with the `fs` feature", prefix, dump_suffix);
}
}
}

View File

@ -37,7 +37,6 @@ pub use error::Error;
use alloc::{borrow::ToOwned, format, string::String, vec, vec::Vec};
use core::{convert::TryInto, mem, num::NonZeroU32};
use std::path::PathBuf;
use half::f16;
use petgraph::graphmap::GraphMap;
@ -45,6 +44,7 @@ use petgraph::graphmap::GraphMap;
use super::atomic_upgrade::Upgrades;
use crate::{
arena::{Arena, Handle, UniqueArena},
path_like::PathLikeOwned,
proc::{Alignment, Layouter},
FastHashMap, FastHashSet, FastIndexMap,
};
@ -381,7 +381,7 @@ pub struct Options {
pub adjust_coordinate_space: bool,
/// Only allow shaders with the known set of capabilities.
pub strict_capabilities: bool,
pub block_ctx_dump_prefix: Option<PathBuf>,
pub block_ctx_dump_prefix: Option<PathLikeOwned>,
}
impl Default for Options {

View File

@ -106,7 +106,6 @@ extern crate std;
extern crate alloc;
mod arena;
mod as_diagnostic_file_path;
pub mod back;
pub mod common;
pub mod compact;
@ -116,6 +115,7 @@ pub mod front;
pub mod ir;
pub mod keywords;
mod non_max_u32;
mod path_like;
pub mod proc;
mod racy_lock;
mod span;

238
naga/src/path_like.rs Normal file
View File

@ -0,0 +1,238 @@
//! [`PathLike`] and its supporting items, such as [`PathLikeRef`] and [`PathLikeOwned`].
//! This trait and these types provide a common denominator API for `Path`-like
//! types and operations in `std` and `no_std` contexts.
//!
//! # Usage
//!
//! - Store a [`PathLikeRef<'a>`] instead of a `&'a Path` in structs and enums.
//! - Store a [`PathLikeOwned`] instead of a `PathBuf` in structs and enums.
//! - Accept `impl PathLike` instead of `impl AsRef<Path>` for methods which directly
//! work with `Path`-like values.
//! - Accept `Into<PathLikeRef<'_>>` and/or `Into<PathLikeOwned>` in methods which
//! will store a `Path`-like value.
use alloc::{borrow::Cow, string::String};
use core::fmt;
mod sealed {
/// Seal for [`PathLike`](super::PathLike).
pub trait Sealed {}
}
/// A trait that abstracts over types accepted for conversion to the most
/// featureful path representation possible; that is:
///
/// - When `no_std` is active, this is implemented for:
/// - [`str`],
/// - [`String`],
/// - [`Cow<'_, str>`], and
/// - [`PathLikeRef`]
/// - Otherwise, types that implement `AsRef<Path>` (to extract a `&Path`).
///
/// This type is used as the type bounds for various diagnostic rendering methods, i.e.,
/// [`WithSpan::emit_to_string_with_path`](crate::span::WithSpan::emit_to_string_with_path).
pub trait PathLike: sealed::Sealed {
fn to_string_lossy(&self) -> Cow<'_, str>;
}
/// Abstraction over `Path` which falls back to [`str`] for `no_std` compatibility.
///
/// This type should be used for _storing_ a reference to a [`PathLike`].
/// Functions which accept a `Path` should prefer to use `impl PathLike`
/// or `impl Into<PathLikeRef<'_>>`.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PathLikeRef<'a>(&'a path_like_impls::PathInner);
impl fmt::Debug for PathLikeRef<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.0, f)
}
}
impl<'a> From<&'a str> for PathLikeRef<'a> {
fn from(value: &'a str) -> Self {
cfg_if::cfg_if! {
if #[cfg(std)] {
Self(std::path::Path::new(value))
} else {
Self(value)
}
}
}
}
/// Abstraction over `PathBuf` which falls back to [`String`] for `no_std` compatibility.
///
/// This type should be used for _storing_ an owned [`PathLike`].
/// Functions which accept a `PathBuf` should prefer to use `impl PathLike`
/// or `impl Into<PathLikeOwned>`.
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PathLikeOwned(<path_like_impls::PathInner as alloc::borrow::ToOwned>::Owned);
impl fmt::Debug for PathLikeOwned {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl From<String> for PathLikeOwned {
fn from(value: String) -> Self {
cfg_if::cfg_if! {
if #[cfg(std)] {
Self(value.into())
} else {
Self(value)
}
}
}
}
#[cfg(std)]
mod path_like_impls {
//! Implementations of [`PathLike`] within an `std` context.
//!
//! Since `std` is available, we blanket implement [`PathLike`] for all types
//! implementing [`AsRef<Path>`].
use alloc::borrow::Cow;
use std::path::Path;
use super::{sealed, PathLike};
pub(super) type PathInner = Path;
impl<T: AsRef<Path> + ?Sized> PathLike for T {
fn to_string_lossy(&self) -> Cow<'_, str> {
self.as_ref().to_string_lossy()
}
}
impl<T: AsRef<Path> + ?Sized> sealed::Sealed for T {}
}
#[cfg(no_std)]
mod path_like_impls {
//! Implementations of [`PathLike`] within a `no_std` context.
//!
//! Without `std`, we cannot blanket implement on [`AsRef<Path>`].
//! Instead, we manually implement for a subset of types which are known
//! to implement [`AsRef<Path>`] when `std` is available.
//!
//! Implementing [`PathLike`] for a type which does _not_ implement [`AsRef<Path>`]
//! with `std` enabled breaks the additive requirement of Cargo features.
use alloc::{borrow::Cow, string::String};
use core::borrow::Borrow;
use super::{sealed, PathLike, PathLikeOwned, PathLikeRef};
pub(super) type PathInner = str;
impl PathLike for String {
fn to_string_lossy(&self) -> Cow<'_, str> {
Cow::Borrowed(self.as_str())
}
}
impl sealed::Sealed for String {}
impl PathLike for str {
fn to_string_lossy(&self) -> Cow<'_, str> {
Cow::Borrowed(self)
}
}
impl sealed::Sealed for str {}
impl PathLike for Cow<'_, str> {
fn to_string_lossy(&self) -> Cow<'_, str> {
Cow::Borrowed(self.borrow())
}
}
impl sealed::Sealed for Cow<'_, str> {}
impl<T: PathLike + ?Sized> PathLike for &T {
fn to_string_lossy(&self) -> Cow<'_, str> {
(*self).to_string_lossy()
}
}
impl<T: PathLike + ?Sized> sealed::Sealed for &T {}
impl PathLike for PathLikeRef<'_> {
fn to_string_lossy(&self) -> Cow<'_, str> {
Cow::Borrowed(self.0)
}
}
impl sealed::Sealed for PathLikeRef<'_> {}
impl PathLike for PathLikeOwned {
fn to_string_lossy(&self) -> Cow<'_, str> {
Cow::Borrowed(self.0.borrow())
}
}
impl sealed::Sealed for PathLikeOwned {}
}
#[cfg(std)]
mod path_like_owned_std_impls {
//! Traits which can only be implemented for [`PathLikeOwned`] with `std`.
use std::path::{Path, PathBuf};
use super::PathLikeOwned;
impl AsRef<Path> for PathLikeOwned {
fn as_ref(&self) -> &Path {
self.0.as_ref()
}
}
impl From<PathBuf> for PathLikeOwned {
fn from(value: PathBuf) -> Self {
Self(value)
}
}
impl From<PathLikeOwned> for PathBuf {
fn from(value: PathLikeOwned) -> Self {
value.0
}
}
impl AsRef<PathBuf> for PathLikeOwned {
fn as_ref(&self) -> &PathBuf {
&self.0
}
}
}
#[cfg(std)]
mod path_like_ref_std_impls {
//! Traits which can only be implemented for [`PathLikeRef`] with `std`.
use std::path::Path;
use super::PathLikeRef;
impl AsRef<Path> for PathLikeRef<'_> {
fn as_ref(&self) -> &Path {
self.0
}
}
impl<'a> From<&'a Path> for PathLikeRef<'a> {
fn from(value: &'a Path) -> Self {
Self(value)
}
}
impl<'a> From<PathLikeRef<'a>> for &'a Path {
fn from(value: PathLikeRef<'a>) -> Self {
value.0
}
}
}

View File

@ -6,7 +6,7 @@ use alloc::{
};
use core::{error::Error, fmt, ops::Range};
use crate::{as_diagnostic_file_path::AsDiagnosticFilePath, Arena, Handle, UniqueArena};
use crate::{path_like::PathLike, Arena, Handle, UniqueArena};
/// A source code span, used for error reporting.
#[derive(Clone, Copy, Debug, PartialEq, Default)]
@ -286,7 +286,7 @@ impl<E> WithSpan<E> {
pub fn emit_to_stderr_with_path<P>(&self, source: &str, path: P)
where
E: Error,
P: AsDiagnosticFilePath,
P: PathLike,
{
use codespan_reporting::{files, term};
@ -318,7 +318,7 @@ impl<E> WithSpan<E> {
pub fn emit_to_string_with_path<P>(&self, source: &str, path: P) -> String
where
E: Error,
P: AsDiagnosticFilePath,
P: PathLike,
{
use codespan_reporting::{files, term};

View File

@ -476,7 +476,7 @@ fn check_targets(input: &Input, module: &mut naga::Module, source_code: Option<&
if let Some(source_code) = source_code {
debug_info = Some(naga::back::spv::DebugInfo {
source_code,
file_name: name,
file_name: name.as_path().into(),
// wgpu#6266: we technically know all the information here to
// produce the valid language but it's not too important for
// validation purposes
@ -883,7 +883,7 @@ fn convert_snapshots_spv() {
&naga::front::spv::Options {
adjust_coordinate_space,
strict_capabilities: true,
block_ctx_dump_prefix: None,
..Default::default()
},
)
.unwrap();

View File

@ -885,7 +885,7 @@ impl super::Device {
if let Some(ref debug) = naga_shader.debug_source {
temp_options.debug_info = Some(naga::back::spv::DebugInfo {
source_code: &debug.source_code,
file_name: debug.file_name.as_ref().as_ref(),
file_name: debug.file_name.as_ref().into(),
language: naga::back::spv::SourceLanguage::WGSL,
})
}
@ -1884,7 +1884,7 @@ impl crate::Device for super::Device {
.as_ref()
.map(|d| naga::back::spv::DebugInfo {
source_code: d.source_code.as_ref(),
file_name: d.file_name.as_ref().as_ref(),
file_name: d.file_name.as_ref().into(),
language: naga::back::spv::SourceLanguage::WGSL,
});
if !desc.runtime_checks.bounds_checks {