Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 11 additions & 66 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/rustc_codegen_spirv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ itertools = "0.14.0"
tracing.workspace = true
tracing-subscriber.workspace = true
tracing-tree = "0.4.0"
naga = { version = "29.0.1", features = ["spv-in", "wgsl-out"], optional = true }
strum = { version = "0.27.2", features = ["derive"] }
naga = { version = "30.0.1", features = ["spv-in", "wgsl-out"], optional = true }
strum = { version = "0.28.0", features = ["derive"] }
rspirv2.workspace = true

[dev-dependencies]
Expand Down
17 changes: 10 additions & 7 deletions crates/rustc_codegen_spirv/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::maybe_pqp_cg_ssa as rustc_codegen_ssa;

use crate::codegen_cx::{CodegenArgs, SpirvMetadata};
use crate::linker;
use crate::naga_transpile::should_transpile;
use crate::naga_transpile::transpile;
use crate::target::{SpirvTarget, SpirvTargetVariant};
use ar::{Archive, GnuBuilder, Header};
use rspirv::binary::Assemble;
Expand Down Expand Up @@ -302,10 +302,17 @@ fn post_link_single_module(
do_spirv_val(sess, &spv_binary, out_filename, val_options);
}

let binary = {
let _transpile_timer = sess.timer("link_transpile");
match transpile(sess, cg_args, &spv_binary) {
Ok(e) => e,
Err(_) => return,
}
};

{
let save_modules_timer = sess.timer("link_save_modules");
if let Err(e) = std::fs::write(out_filename, spirv_tools::binary::from_binary(&spv_binary))
{
if let Err(e) = std::fs::write(out_filename, binary) {
let mut err = sess
.dcx()
.struct_err("failed to serialize spirv-binary to disk");
Expand All @@ -316,10 +323,6 @@ fn post_link_single_module(

drop(save_modules_timer);
}

if let Ok(Some(transpile)) = should_transpile(sess) {
transpile(sess, cg_args, &spv_binary, out_filename).ok();
}
}

fn do_spirv_opt(
Expand Down
44 changes: 17 additions & 27 deletions crates/rustc_codegen_spirv/src/naga_transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,26 @@ use crate::codegen_cx::CodegenArgs;
use crate::target::{NagaTarget, SpirvTarget};
use rustc_session::Session;
use rustc_span::ErrorGuaranteed;
use std::path::Path;

pub type NagaTranspile = fn(
pub fn transpile(
sess: &Session,
cg_args: &CodegenArgs,
spv_binary: &[u32],
out_filename: &Path,
) -> Result<(), ErrorGuaranteed>;

pub fn should_transpile(sess: &Session) -> Result<Option<NagaTranspile>, ErrorGuaranteed> {
) -> Result<Vec<u8>, ErrorGuaranteed> {
let target = SpirvTarget::parse_target(sess.opts.target_triple.tuple())
.expect("parsing should fail earlier");
let result: Result<Option<NagaTranspile>, ()> = match target {
match target {
#[cfg(feature = "naga")]
SpirvTarget::Naga(NagaTarget::NAGA_WGSL) => Ok(Some(transpile::wgsl_transpile)),
SpirvTarget::Naga(NagaTarget::NAGA_WGSL) => {
transpile::wgsl_transpile(sess, cg_args, spv_binary)
}
#[cfg(not(feature = "naga"))]
SpirvTarget::Naga(NagaTarget::NAGA_WGSL) => Err(()),
_ => Ok(None),
};
result.map_err(|_e| {
sess.dcx().err(format!(
SpirvTarget::Naga(_) => Err(sess.dcx().err(format!(
"Target `{}` requires feature \"naga\" on rustc_codegen_spirv",
target.target()
))
})
))),
_ => Ok(bytemuck::cast_slice::<_, u8>(spv_binary).to_vec()),
}
}

#[cfg(feature = "naga")]
Expand All @@ -36,16 +31,18 @@ mod transpile {
use naga::valid::Capabilities;
use rustc_session::Session;
use rustc_span::ErrorGuaranteed;
use std::path::Path;

pub fn wgsl_transpile(
sess: &Session,
_cg_args: &CodegenArgs,
spv_binary: &[u32],
out_filename: &Path,
) -> Result<(), ErrorGuaranteed> {
) -> Result<Vec<u8>, ErrorGuaranteed> {
// these should be params via spirv-builder
let opts = naga::front::spv::Options::default();
let opts = naga::front::spv::Options {
adjust_coordinate_space: false,
strict_capabilities: false,
..Default::default()
};
let capabilities = Capabilities::all();
let writer_flags = naga::back::wgsl::WriterFlags::empty();

Expand Down Expand Up @@ -73,17 +70,10 @@ mod transpile {
))
})?;

let wgsl_dst = out_filename.with_extension("wgsl");
let wgsl = naga::back::wgsl::write_string(&module, &info, writer_flags).map_err(|err| {
sess.dcx()
.err(format!("Naga failed to write wgsl : \n{err}"))
})?;

std::fs::write(&wgsl_dst, wgsl).map_err(|err| {
sess.dcx()
.err(format!("failed to write wgsl to file: {err}"))
})?;

Ok(())
Ok(wgsl.into_bytes())
}
}
Loading
Loading