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
3 changes: 1 addition & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ rustflags = [
"-Wclippy::dbg_macro",
"-Wclippy::debug_assert_with_mut_call",
"-Wclippy::doc_markdown",
"-Wclippy::empty_enum",
"-Wclippy::empty_enums",
"-Wclippy::enum_glob_use",
"-Wclippy::exit",
"-Wclippy::expl_impl_clone_on_copy",
Expand Down Expand Up @@ -82,7 +82,6 @@ rustflags = [
"-Wclippy::string_add_assign",
"-Wclippy::string_add",
"-Wclippy::string_lit_as_bytes",
"-Wclippy::string_to_string",
"-Wclippy::todo",
"-Wclippy::trait_duplication_in_bounds",
"-Wclippy::unimplemented",
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

14 changes: 11 additions & 3 deletions crates/rustc_codegen_spirv-types/src/target_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum TargetSpecVersion {
/// Some later version requires them.
/// Some earlier version fails with them (notably our 0.9.0 release).
Rustc_1_76_0,
/// rustc 1.93 requires that the value of "target-pointer-width" is no longer a string but u16
Rustc_1_93_0,
}

impl TargetSpecVersion {
Expand All @@ -39,7 +41,9 @@ impl TargetSpecVersion {
/// Returns the version of the target spec required for a certain rustc version. May return `None` if the version
/// is old enough to not need target specs.
pub fn from_rustc_version(rustc_version: Version) -> Option<Self> {
if rustc_version >= Version::new(1, 85, 0) {
if rustc_version >= Version::new(1, 93, 0) {
Some(Self::Rustc_1_93_0)
} else if rustc_version >= Version::new(1, 85, 0) {
Some(Self::Rustc_1_85_0)
} else if rustc_version >= Version::new(1, 76, 0) {
Some(Self::Rustc_1_76_0)
Expand All @@ -52,8 +56,12 @@ impl TargetSpecVersion {
pub fn format_spec(&self, target: &SpirvTarget) -> String {
let target_env = target.env();
let extra = match self {
TargetSpecVersion::Rustc_1_85_0 => r#""crt-static-respected": true,"#,
TargetSpecVersion::Rustc_1_76_0 => r#""os": "unknown","#,
_ => r#""crt-static-respected": true,"#,
};
let target_pointer_width = match self {
TargetSpecVersion::Rustc_1_76_0 | TargetSpecVersion::Rustc_1_85_0 => "\"32\"",
TargetSpecVersion::Rustc_1_93_0 => "32",
};
format!(
r#"{{
Expand All @@ -80,7 +88,7 @@ impl TargetSpecVersion {
{extra}
"panic-strategy": "abort",
"simd-types-indirect": false,
"target-pointer-width": "32"
"target-pointer-width": {target_pointer_width}
}}"#
)
}
Expand Down
1 change: 1 addition & 0 deletions crates/rustc_codegen_spirv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ tracing-tree = "0.4.0"

[dev-dependencies]
pretty_assertions = "1.0"
termcolor = "1.1.3"

# HACK(eddyb) can't re-introduce deps of `rustc_codegen_ssa`, for `pqp_cg_ssa`
# (see `build.rs`).
Expand Down
11 changes: 9 additions & 2 deletions crates/rustc_codegen_spirv/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use std::{env, fs, mem};
/// `cargo publish`. We need to figure out a way to do this properly, but let's hardcode it for now :/
//const REQUIRED_RUST_TOOLCHAIN: &str = include_str!("../../rust-toolchain.toml");
const REQUIRED_RUST_TOOLCHAIN: &str = r#"[toolchain]
channel = "nightly-2025-08-04"
channel = "nightly-2025-11-02"
components = ["rust-src", "rustc-dev", "llvm-tools"]
# commit_hash = f34ba774c78ea32b7c40598b8ad23e75cdac42a6"#;
# commit_hash = bd3ac0330018c23b111bbee176f32c377be7b319"#;

fn rustc_output(arg: &str) -> Result<String, Box<dyn Error>> {
let rustc = env::var("RUSTC").unwrap_or_else(|_| "rustc".into());
Expand Down Expand Up @@ -198,6 +198,13 @@ mod win {",
for link_path in raw_dylib::",
);
}
src = src.replace(
"
for (link_path, as_needed) in raw_dylib::",
"
#[cfg(any())]
for (link_path, as_needed) in raw_dylib::",
);
if relative_path == Path::new("src/back/metadata.rs") {
// HACK(eddyb) remove `object` dependency.
src = src.replace(
Expand Down
4 changes: 3 additions & 1 deletion crates/rustc_codegen_spirv/src/builder/builder_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
_src_align: Align,
size: Self::Value,
flags: MemFlags,
_tt: Option<rustc_ast::expand::typetree::FncTree>,
) {
if flags != MemFlags::empty() {
self.err(format!(
Expand Down Expand Up @@ -2878,7 +2879,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
size: Self::Value,
flags: MemFlags,
) {
self.memcpy(dst, dst_align, src, src_align, size, flags);
self.memcpy(dst, dst_align, src, src_align, size, flags, None);
}

fn memset(
Expand Down Expand Up @@ -3124,6 +3125,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
dst: Self::Value,
src: Self::Value,
order: AtomicOrdering,
_ret_ptr: bool,
) -> Self::Value {
let ty = src.ty;

Expand Down
17 changes: 16 additions & 1 deletion crates/rustc_codegen_spirv/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<'a, 'tcx> DebugInfoBuilderMethods for Builder<'a, 'tcx> {
_direct_offset: Size,
// NB: each offset implies a deref (i.e. they're steps in a pointer chain).
_indirect_offsets: &[Size],
_fragment: Option<Range<Size>>,
_fragment: &Option<Range<Size>>,
) {
todo!()
}
Expand All @@ -203,6 +203,21 @@ impl<'a, 'tcx> DebugInfoBuilderMethods for Builder<'a, 'tcx> {
fn set_var_name(&mut self, _value: Self::Value, _name: &str) {
todo!()
}

fn dbg_var_value(
&mut self,
_dbg_var: Self::DIVariable,
_dbg_loc: Self::DILocation,
_value: Self::Value,
_direct_offset: Size,
// NB: each offset implies a deref (i.e. they're steps in a pointer chain).
_indirect_offsets: &[Size],
// Byte range in the `dbg_var` covered by this fragment,
// if this is a fragment of a composite `DIVariable`.
_fragment: &Option<Range<Size>>,
) {
todo!()
}
}

impl<'a, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'a, 'tcx> {
Expand Down
3 changes: 1 addition & 2 deletions crates/rustc_codegen_spirv/src/codegen_cx/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,7 @@ impl<'tcx> CodegenCx<'tcx> {

// HACK(eddyb) these should never happen when using
// `read_scalar`, but better not outright crash.
AllocError::ScalarSizeMismatch(_)
| AllocError::OverwritePartialPointer(_) => {
AllocError::ScalarSizeMismatch(_) => {
Err(format!("unrecognized `AllocError::{err:?}`"))
}
},
Expand Down
6 changes: 3 additions & 3 deletions crates/rustc_codegen_spirv/src/codegen_cx/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use itertools::Itertools;
use rspirv::spirv::{FunctionControl, LinkageType, StorageClass, Word};
use rustc_abi::Align;
use rustc_codegen_ssa::traits::{PreDefineCodegenMethods, StaticCodegenMethods};
use rustc_hir::attrs::InlineAttr;
use rustc_hir::attrs::{InlineAttr, Linkage};
use rustc_middle::bug;
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc_middle::mir::mono::{Linkage, MonoItem, Visibility};
use rustc_middle::mir::mono::{MonoItem, Visibility};
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
use rustc_middle::ty::{self, Instance, TypeVisitableExt, TypingEnv};
use rustc_span::Span;
Expand Down Expand Up @@ -178,7 +178,7 @@ impl<'tcx> CodegenCx<'tcx> {

// Check if this is a From trait implementation
if let Some(impl_def_id) = self.tcx.impl_of_assoc(def_id)
&& let Some(trait_ref) = self.tcx.impl_trait_ref(impl_def_id)
&& let Some(trait_ref) = self.tcx.impl_opt_trait_ref(impl_def_id)
{
let trait_def_id = trait_ref.skip_binder().def_id;

Expand Down
50 changes: 19 additions & 31 deletions crates/rustc_codegen_spirv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,9 @@ use maybe_pqp_cg_ssa::traits::{
};
use maybe_pqp_cg_ssa::{CodegenResults, CompiledModule, ModuleCodegen, ModuleKind, TargetConfig};
use rspirv::binary::Assemble;
use rustc_ast::expand::allocator::AllocatorKind;
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
use rustc_ast::expand::allocator::AllocatorMethod;
use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::{DiagCtxtHandle, FatalError};
use rustc_errors::DiagCtxtHandle;
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::mir::mono::{MonoItem, MonoItemData};
Expand Down Expand Up @@ -281,6 +280,10 @@ impl CodegenBackend for SpirvCodegenBackend {
);
drop(timer);
}

fn name(&self) -> &'static str {
"SpirvCodegenBackend"
}
}

struct SpirvModuleBuffer(Vec<u32>);
Expand All @@ -299,16 +302,13 @@ impl ThinBufferMethods for SpirvModuleBuffer {
fn data(&self) -> &[u8] {
self.as_bytes()
}
fn thin_link_data(&self) -> &[u8] {
&[]
}
}

impl SpirvCodegenBackend {
fn optimize_common(
_cgcx: &CodegenContext<Self>,
module: &mut ModuleCodegen<<Self as WriteBackendMethods>::Module>,
) -> Result<(), FatalError> {
) {
// Apply DCE ("dead code elimination") to modules before ever serializing
// them as `.spv` files (technically, `.rcgu.o` files inside `.rlib`s),
// that will later get linked (potentially many times, esp. if this is
Expand All @@ -317,8 +317,6 @@ impl SpirvCodegenBackend {
linker::dce::dce(&mut module.module_llvm);

// FIXME(eddyb) run as many optimization passes as possible, not just DCE.

Ok(())
}
}

Expand All @@ -339,8 +337,7 @@ impl WriteBackendMethods for SpirvCodegenBackend {
_exported_symbols_for_lto: &[String],
_each_linked_rlib_for_lto: &[PathBuf],
_modules: Vec<FatLtoInput<Self>>,
_diff_fncs: Vec<AutoDiffItem>,
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
) -> ModuleCodegen<Self::Module> {
assert!(
cgcx.lto == rustc_session::config::Lto::Fat,
"`run_and_optimize_fat_lto` (for `WorkItemResult::NeedsFatLto`) should \
Expand All @@ -356,7 +353,7 @@ impl WriteBackendMethods for SpirvCodegenBackend {
_each_linked_rlib_for_lto: &[PathBuf], // njn: ?
modules: Vec<(String, Self::ThinBuffer)>,
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
) -> Result<(Vec<ThinModule<Self>>, Vec<WorkProduct>), FatalError> {
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
link::run_thin(cgcx, modules, cached_modules)
}

Expand All @@ -373,14 +370,14 @@ impl WriteBackendMethods for SpirvCodegenBackend {
_dcx: DiagCtxtHandle<'_>,
module: &mut ModuleCodegen<Self::Module>,
_config: &ModuleConfig,
) -> Result<(), FatalError> {
Self::optimize_common(cgcx, module)
) {
Self::optimize_common(cgcx, module);
}

fn optimize_thin(
cgcx: &CodegenContext<Self>,
thin_module: ThinModule<Self>,
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
) -> ModuleCodegen<Self::Module> {
// FIXME(eddyb) the inefficiency of Module -> [u8] -> Module roundtrips
// comes from upstream and it applies to `rustc_codegen_llvm` as well,
// eventually it should be properly addressed (for `ThinLocal` at least).
Expand All @@ -393,15 +390,15 @@ impl WriteBackendMethods for SpirvCodegenBackend {
kind: ModuleKind::Regular,
thin_lto_buffer: None,
};
Self::optimize_common(cgcx, &mut module)?;
Ok(module)
Self::optimize_common(cgcx, &mut module);
module
}

fn codegen(
cgcx: &CodegenContext<Self>,
module: ModuleCodegen<Self::Module>,
_config: &ModuleConfig,
) -> Result<CompiledModule, FatalError> {
) -> CompiledModule {
let kind = module.kind;
let (name, module_buffer) = Self::serialize_module(module);

Expand All @@ -412,7 +409,7 @@ impl WriteBackendMethods for SpirvCodegenBackend {
);
fs::write(&path, module_buffer.as_bytes()).unwrap();

Ok(CompiledModule {
CompiledModule {
name,
kind,
object: Some(path),
Expand All @@ -421,13 +418,10 @@ impl WriteBackendMethods for SpirvCodegenBackend {
assembly: None,
llvm_ir: None,
links_from_incr_cache: vec![],
})
}
}

fn prepare_thin(
module: ModuleCodegen<Self::Module>,
_want_summary: bool,
) -> (String, Self::ThinBuffer) {
fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
Self::serialize_module(module)
}

Expand All @@ -440,13 +434,7 @@ impl WriteBackendMethods for SpirvCodegenBackend {
}

impl ExtraBackendMethods for SpirvCodegenBackend {
fn codegen_allocator(
&self,
_: TyCtxt<'_>,
_: &str,
_: AllocatorKind,
_: AllocatorKind,
) -> Self::Module {
fn codegen_allocator(&self, _: TyCtxt<'_>, _: &str, _: &[AllocatorMethod]) -> Self::Module {
todo!()
}

Expand Down
13 changes: 8 additions & 5 deletions crates/rustc_codegen_spirv/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use ar::{Archive, GnuBuilder, Header};
use rspirv::binary::Assemble;
use rspirv::dr::Module;
use rustc_ast::CRATE_NODE_ID;
use rustc_attr_parsing::{ShouldEmit, eval_config_entry};
use rustc_codegen_spirv_types::{CompileResult, ModuleResult};
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule, ThinShared};
use rustc_codegen_ssa::back::write::CodegenContext;
use rustc_codegen_ssa::{CodegenResults, NativeLib};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{Diag, FatalError};
use rustc_errors::Diag;
use rustc_hir::attrs::NativeLibKind;
use rustc_metadata::{EncodedMetadata, fs::METADATA_FILENAME};
use rustc_middle::bug;
use rustc_middle::dep_graph::WorkProduct;
Expand All @@ -22,7 +24,6 @@ use rustc_session::config::{
CrateType, DebugInfo, Lto, OptLevel, OutFileName, OutputFilenames, OutputType,
};
use rustc_session::output::{check_file_is_writeable, invalid_output_for_target, out_filename};
use rustc_session::utils::NativeLibKind;
use rustc_span::Symbol;
use std::collections::BTreeMap;
use std::ffi::{CString, OsStr};
Expand Down Expand Up @@ -496,7 +497,9 @@ fn add_upstream_native_libraries(
// (see `compiler/rustc_codegen_ssa/src/back/link.rs`)
fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {
match lib.cfg {
Some(ref cfg) => rustc_attr_parsing::cfg_matches(cfg, sess, CRATE_NODE_ID, None),
Some(ref cfg) => {
eval_config_entry(sess, cfg, CRATE_NODE_ID, None, ShouldEmit::ErrorsAndLints).as_bool()
}
None => true,
}
}
Expand Down Expand Up @@ -634,7 +637,7 @@ pub(crate) fn run_thin(
cgcx: &CodegenContext<SpirvCodegenBackend>,
modules: Vec<(String, SpirvModuleBuffer)>,
cached_modules: Vec<(SerializedModule<SpirvModuleBuffer>, WorkProduct)>,
) -> Result<(Vec<ThinModule<SpirvCodegenBackend>>, Vec<WorkProduct>), FatalError> {
) -> (Vec<ThinModule<SpirvCodegenBackend>>, Vec<WorkProduct>) {
if cgcx.opts.cg.linker_plugin_lto.enabled() {
unreachable!("We should never reach this case if the LTO step is deferred to the linker");
}
Expand Down Expand Up @@ -674,5 +677,5 @@ pub(crate) fn run_thin(
});
}

Ok((opt_jobs, vec![]))
(opt_jobs, vec![])
}
Loading
Loading