diff --git a/crates/rustc_codegen_spirv/build.rs b/crates/rustc_codegen_spirv/build.rs index b0cd95db81..c319e6ae90 100644 --- a/crates/rustc_codegen_spirv/build.rs +++ b/crates/rustc_codegen_spirv/build.rs @@ -10,9 +10,9 @@ use std::process::{Command, ExitCode}; /// `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-2024-04-24" +channel = "nightly-2024-06-20" components = ["rust-src", "rustc-dev", "llvm-tools"] -# commit_hash = 244da22fabd9fa677bbd0ac601a88e5ca6917526"#; +# commit_hash = d8a38b00024cd7156dea4ce8fd8ae113a2745e7f"#; fn get_rustc_commit_hash() -> Result> { let rustc = std::env::var("RUSTC").unwrap_or_else(|_| String::from("rustc")); diff --git a/crates/rustc_codegen_spirv/src/abi.rs b/crates/rustc_codegen_spirv/src/abi.rs index 7e8cf0a2e2..92cb406d37 100644 --- a/crates/rustc_codegen_spirv/src/abi.rs +++ b/crates/rustc_codegen_spirv/src/abi.rs @@ -12,7 +12,8 @@ use rustc_middle::query::Providers; use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, TyAndLayout}; use rustc_middle::ty::GenericArgsRef; use rustc_middle::ty::{ - self, Const, CoroutineArgs, FloatTy, IntTy, ParamEnv, PolyFnSig, Ty, TyCtxt, TyKind, UintTy, + self, Const, CoroutineArgs, CoroutineArgsExt, FloatTy, IntTy, ParamEnv, PolyFnSig, Ty, TyCtxt, + TyKind, UintTy, }; use rustc_middle::{bug, span_bug}; use rustc_span::def_id::DefId; @@ -20,7 +21,8 @@ use rustc_span::DUMMY_SP; use rustc_span::{Span, Symbol}; use rustc_target::abi::call::{ArgAbi, ArgAttributes, FnAbi, PassMode}; use rustc_target::abi::{ - Abi, Align, FieldsShape, LayoutS, Primitive, Scalar, Size, TagEncoding, VariantIdx, Variants, + Abi, Align, FieldsShape, Float, LayoutS, Primitive, Scalar, Size, TagEncoding, VariantIdx, + Variants, }; use rustc_target::spec::abi::Abi as SpecAbi; use std::cell::RefCell; @@ -504,10 +506,10 @@ fn trans_scalar<'tcx>( Primitive::Int(width, signedness) => { SpirvType::Integer(width.size().bits() as u32, signedness).def(span, cx) } - Primitive::F16 => SpirvType::Float(16).def(span, cx), - Primitive::F32 => SpirvType::Float(32).def(span, cx), - Primitive::F64 => SpirvType::Float(64).def(span, cx), - Primitive::F128 => SpirvType::Float(128).def(span, cx), + Primitive::Float(Float::F16) => SpirvType::Float(16).def(span, cx), + Primitive::Float(Float::F32) => SpirvType::Float(32).def(span, cx), + Primitive::Float(Float::F64) => SpirvType::Float(64).def(span, cx), + Primitive::Float(Float::F128) => SpirvType::Float(128).def(span, cx), Primitive::Pointer(_) => { let pointee_ty = dig_scalar_pointee(cx, ty, offset); // Pointers can be recursive. So, record what we're currently translating, and if we're already translating @@ -866,7 +868,10 @@ fn trans_intrinsic_type<'tcx>( cx: &CodegenCx<'tcx>, const_: Const<'tcx>, ) -> Result { - assert!(const_.ty().is_integral()); + assert!( + matches!(const_.kind(), ty::ConstKind::Value(ty, _) if ty.is_integral()), + "Expected an integral type" + ); let value = const_.eval_bits(cx.tcx, ParamEnv::reveal_all()); match P::from_u128(value) { Some(v) => Ok(v), diff --git a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs index b8841b45a8..f58e617a7d 100644 --- a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs +++ b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs @@ -337,7 +337,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let exit_bb = self.append_sibling_block("memset_exit"); let count = self.udiv(size_bytes, size_elem_const); - let index = self.alloca(count.ty, zero_align); + let index = self.alloca(Size::from_bits(32), zero_align); self.store(zero, index, zero_align); self.br(header_bb); @@ -1412,10 +1412,15 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> { fn to_immediate_scalar(&mut self, val: Self::Value, _scalar: Scalar) -> Self::Value { val } + fn alloca(&mut self, _size: Size, _align: Align) -> Self::Value { + // Define a u32 type for the allocation. + let u32_type = SpirvType::Integer(32, false).def(rustc_span::DUMMY_SP, self.cx); - fn alloca(&mut self, ty: Self::Type, _align: Align) -> Self::Value { - let ptr_ty = self.type_ptr_to(ty); - // "All OpVariable instructions in a function must be the first instructions in the first block." + // Define a pointer to the u32 type. + let ptr_ty = SpirvType::Pointer { pointee: u32_type }.def(rustc_span::DUMMY_SP, self.cx); + + // "All OpVariable instructions in a function must be the first instructions in + // the first block." let mut builder = self.emit(); builder.select_block(Some(0)).unwrap(); let index = { @@ -1446,7 +1451,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> { result_id.with_type(ptr_ty) } - fn byte_array_alloca(&mut self, _len: Self::Value, _align: Align) -> Self::Value { + fn dynamic_alloca(&mut self, _len: Self::Value, _align: Align) -> Self::Value { self.fatal("array alloca not supported yet") } @@ -3171,10 +3176,10 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> { let debug_printf_fmt = match (spec, scalar) { // FIXME(eddyb) support more of these, // potentially recursing to print ADTs. - (' ' | '?', Some(Int(I32, false))) => "%u", + (' ' | '?', Some(Int(_i32, false))) => "%u", ('x', Some(Int(I32, false))) => "%x", - (' ' | '?', Some(Int(I32, true))) => "%i", - (' ' | '?', Some(F32)) => "%f", + (' ' | '?', Some(Int(_i32, true))) => "%i", + (' ' | '?', Some(_f32)) => "%f", _ => "", }; diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs b/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs index 4ab26f317e..1078f3bfc1 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/constant.rs @@ -8,7 +8,7 @@ use rustc_middle::bug; use rustc_middle::mir::interpret::{alloc_range, ConstAllocation, GlobalAlloc, Scalar}; use rustc_middle::ty::layout::LayoutOf; use rustc_span::{Span, DUMMY_SP}; -use rustc_target::abi::{self, AddressSpace, HasDataLayout, Integer, Primitive, Size}; +use rustc_target::abi::{self, AddressSpace, Float, HasDataLayout, Integer, Primitive, Size}; impl<'tcx> CodegenCx<'tcx> { pub fn def_constant(&self, ty: Word, val: SpirvConst<'_, 'tcx>) -> SpirvValue { @@ -251,7 +251,7 @@ impl<'tcx> ConstMethods<'tcx> for CodegenCx<'tcx> { match scalar { Scalar::Int(int) => { assert_eq!(int.size(), layout.primitive().size(self)); - let data = int.assert_uint(int.size()); + let data = int.to_uint(int.size()); match layout.primitive() { Primitive::Int(int_size, int_signedness) => match self.lookup_type(ty) { @@ -273,21 +273,21 @@ impl<'tcx> ConstMethods<'tcx> for CodegenCx<'tcx> { other.debug(ty, self) )), }, - Primitive::F16 => self + Primitive::Float(Float::F16) => self .tcx .dcx() .fatal("scalar_to_backend Primitive::F16 not supported"), - Primitive::F32 => { + Primitive::Float(Float::F32) => { let res = self.constant_f32(DUMMY_SP, f32::from_bits(data as u32)); assert_eq!(res.ty, ty); res } - Primitive::F64 => { + Primitive::Float(Float::F64) => { let res = self.constant_f64(DUMMY_SP, f64::from_bits(data as u64)); assert_eq!(res.ty, ty); res } - Primitive::F128 => self + Primitive::Float(Float::F128) => self .tcx .dcx() .fatal("scalar_to_backend Primitive::F128 not supported"), @@ -488,8 +488,8 @@ impl<'tcx> CodegenCx<'tcx> { Primitive::Int(integer, int_signedness) } SpirvType::Float(float_size) => match float_size { - 32 => Primitive::F32, - 64 => Primitive::F64, + 32 => Primitive::Float(Float::F32), + 64 => Primitive::Float(Float::F64), other => { self.tcx .dcx() diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs index 2489e2ea79..715b4ebd21 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/entry.rs @@ -82,7 +82,7 @@ impl<'tcx> CodegenCx<'tcx> { let body = self .tcx .hir() - .body(self.tcx.hir().body_owned_by(fn_local_def_id)); + .body(self.tcx.hir().body_owned_by(fn_local_def_id).id()); body.params }; for (arg_abi, hir_param) in fn_abi.args.iter().zip(hir_params) { diff --git a/crates/rustc_codegen_spirv/src/lib.rs b/crates/rustc_codegen_spirv/src/lib.rs index 3d430d8db4..69c694d924 100644 --- a/crates/rustc_codegen_spirv/src/lib.rs +++ b/crates/rustc_codegen_spirv/src/lib.rs @@ -19,7 +19,6 @@ #![feature(assert_matches)] #![feature(result_flattening)] #![feature(lint_reasons)] -#![feature(lazy_cell)] // crate-specific exceptions: #![allow( unsafe_code, // rustc_codegen_ssa requires unsafe functions in traits to be impl'd @@ -98,13 +97,13 @@ use rustc_codegen_ssa::traits::{ }; use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen, ModuleKind}; use rustc_data_structures::fx::FxIndexMap; -use rustc_errors::{DiagCtxt, ErrorGuaranteed, FatalError}; +use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, FatalError}; use rustc_metadata::EncodedMetadata; use rustc_middle::dep_graph::{WorkProduct, WorkProductId}; use rustc_middle::mir::mono::{MonoItem, MonoItemData}; use rustc_middle::mir::pretty::write_mir_pretty; use rustc_middle::ty::print::with_no_trimmed_paths; -use rustc_middle::ty::{self, Instance, InstanceDef, TyCtxt}; +use rustc_middle::ty::{self, Instance, InstanceKind, TyCtxt}; use rustc_session::config::{self, OutputFilenames, OutputType}; use rustc_session::Session; use rustc_span::symbol::{sym, Symbol}; @@ -120,7 +119,7 @@ fn dump_mir(tcx: TyCtxt<'_>, mono_items: &[(MonoItem<'_>, MonoItemData)], path: let mut file = File::create(path).unwrap(); for &(mono_item, _) in mono_items { if let MonoItem::Fn(instance) = mono_item { - if matches!(instance.def, InstanceDef::Item(_)) { + if matches!(instance.def, InstanceKind::Item(_)) { let mut mir = Cursor::new(Vec::new()); if write_mir_pretty(tcx, Some(instance.def_id()), &mut mir).is_ok() { writeln!(file, "{}", String::from_utf8(mir.into_inner()).unwrap()).unwrap(); @@ -136,7 +135,7 @@ fn is_blocklisted_fn<'tcx>( instance: Instance<'tcx>, ) -> bool { // TODO: These sometimes have a constant value of an enum variant with a hole - if let InstanceDef::Item(def_id) = instance.def { + if let InstanceKind::Item(def_id) = instance.def { if let Some(debug_trait_def_id) = tcx.get_diagnostic_item(sym::Debug) { // Helper for detecting `<_ as core::fmt::Debug>::fmt` (in impls). let is_debug_fmt_method = |def_id| match tcx.opt_associated_item(def_id) { @@ -185,6 +184,9 @@ impl ThinBufferMethods for SpirvThinBuffer { fn data(&self) -> &[u8] { spirv_tools::binary::from_binary(&self.0) } + fn thin_link_data(&self) -> &[u8] { + unimplemented!(); + } } #[derive(Clone)] @@ -277,7 +279,7 @@ impl WriteBackendMethods for SpirvCodegenBackend { fn run_link( _cgcx: &CodegenContext, - _diag_handler: &DiagCtxt, + _diag_handler: DiagCtxtHandle<'_>, _modules: Vec>, ) -> Result, FatalError> { todo!() @@ -309,7 +311,7 @@ impl WriteBackendMethods for SpirvCodegenBackend { unsafe fn optimize( _: &CodegenContext, - _: &DiagCtxt, + _: DiagCtxtHandle<'_>, _: &ModuleCodegen, _: &ModuleConfig, ) -> Result<(), FatalError> { @@ -340,7 +342,7 @@ impl WriteBackendMethods for SpirvCodegenBackend { unsafe fn codegen( cgcx: &CodegenContext, - _diag_handler: &DiagCtxt, + _diag_handler: DiagCtxtHandle<'_>, module: ModuleCodegen, _config: &ModuleConfig, ) -> Result { @@ -364,7 +366,10 @@ impl WriteBackendMethods for SpirvCodegenBackend { }) } - fn prepare_thin(module: ModuleCodegen) -> (String, Self::ThinBuffer) { + fn prepare_thin( + module: ModuleCodegen, + _want_summary: bool, + ) -> (String, Self::ThinBuffer) { (module.name, SpirvThinBuffer(module.module_llvm)) } @@ -482,16 +487,13 @@ impl Drop for DumpModuleOnPanic<'_, '_, '_> { #[no_mangle] pub fn __rustc_codegen_backend() -> Box { // Tweak rustc's default ICE panic hook, to direct people to `rust-gpu`. - rustc_driver::install_ice_hook( - "https://github.com/rust-gpu/rust-gpu/issues/new", - |handler| { - handler.note(concat!( - "`rust-gpu` version `", - env!("CARGO_PKG_VERSION"), - "`" - )); - }, - ); + rustc_driver::install_ice_hook("https://github.com/rust-gpu/rust-gpu/issues/new", |dcx| { + dcx.handle().note(concat!( + "`rust-gpu` version `", + env!("CARGO_PKG_VERSION"), + "`" + )); + }); Box::new(SpirvCodegenBackend) } diff --git a/crates/rustc_codegen_spirv/src/linker/test.rs b/crates/rustc_codegen_spirv/src/linker/test.rs index 55750bf264..e6da8ed29c 100644 --- a/crates/rustc_codegen_spirv/src/linker/test.rs +++ b/crates/rustc_codegen_spirv/src/linker/test.rs @@ -2,6 +2,7 @@ use super::{link, LinkResult}; use rspirv::dr::{Loader, Module}; use rustc_errors::registry::Registry; use rustc_session::config::{Input, OutputFilenames, OutputTypes}; +use rustc_session::parse::ParseSess; use rustc_session::CompilerIO; use rustc_span::FileName; use std::io::Write; @@ -169,7 +170,11 @@ fn link_with_linker_opts( // HACK(eddyb) inject `write_diags` into `sess`, to work around // the removals in https://github.com/rust-lang/rust/pull/102992. - sess.psess.dcx = { + // HACK(legneato): This can be simplified to use `set_dcx()` when updating + // to a rustc version containing + // https://github.com/rust-lang/rust/commit/bde1f4dd57abd8e86dd7d7b325640558c4437d1f. + let source_map = sess.psess.clone_source_map(); + let dcx = { let fallback_bundle = { extern crate rustc_error_messages; rustc_error_messages::fallback_fluent_bundle( @@ -185,6 +190,8 @@ fn link_with_linker_opts( .with_flags(sess.opts.unstable_opts.dcx_flags(true)) }; + sess.psess = ParseSess::with_dcx(dcx, source_map); + let res = link( &sess, modules, diff --git a/examples/runners/ash/Cargo.toml b/examples/runners/ash/Cargo.toml index 014c0c7fc3..261fe1df8d 100644 --- a/examples/runners/ash/Cargo.toml +++ b/examples/runners/ash/Cargo.toml @@ -23,5 +23,8 @@ cfg-if = "1.0.0" shared = { path = "../../shaders/shared" } spirv-builder = { workspace = true, default-features = false } +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] } + [target.'cfg(target_os = "macos")'.dependencies] ash-molten = { version = "0.13.1", features = ["pre-built"] } diff --git a/examples/runners/wgpu/Cargo.toml b/examples/runners/wgpu/Cargo.toml index 0548bbeee5..ba0e6fa335 100644 --- a/examples/runners/wgpu/Cargo.toml +++ b/examples/runners/wgpu/Cargo.toml @@ -27,6 +27,9 @@ clap = { version = "4", features = ["derive"] } strum = { version = "0.25.0", default-features = false, features = ["std", "derive"] } bytemuck = "1.6.3" +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] } + [target.'cfg(not(any(target_os = "android", target_arch = "wasm32")))'.dependencies] env_logger = "0.11.0" spirv-builder = { workspace = true, features = ["watch"] } diff --git a/examples/shaders/compute-shader/Cargo.toml b/examples/shaders/compute-shader/Cargo.toml index 334e043793..a0402bd8e7 100644 --- a/examples/shaders/compute-shader/Cargo.toml +++ b/examples/shaders/compute-shader/Cargo.toml @@ -13,5 +13,8 @@ crate-type = ["dylib", "lib"] [dependencies] spirv-std = { workspace = true } +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] } + [target.'cfg(not(target_arch = "spirv"))'.dependencies] rayon = "1.5" diff --git a/examples/shaders/mouse-shader/Cargo.toml b/examples/shaders/mouse-shader/Cargo.toml index ea4144463a..163be30e8a 100644 --- a/examples/shaders/mouse-shader/Cargo.toml +++ b/examples/shaders/mouse-shader/Cargo.toml @@ -13,3 +13,6 @@ crate-type = ["dylib"] [dependencies] shared = { path = "../../shaders/shared" } spirv-std = { workspace = true } + +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] } diff --git a/examples/shaders/reduce/Cargo.toml b/examples/shaders/reduce/Cargo.toml index 870f110b00..5a963bc69d 100644 --- a/examples/shaders/reduce/Cargo.toml +++ b/examples/shaders/reduce/Cargo.toml @@ -12,3 +12,6 @@ crate-type = ["dylib", "lib"] [dependencies] spirv-std = { workspace = true } + +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] } diff --git a/examples/shaders/shared/Cargo.toml b/examples/shaders/shared/Cargo.toml index 66b69c4369..bdf018a87e 100644 --- a/examples/shaders/shared/Cargo.toml +++ b/examples/shaders/shared/Cargo.toml @@ -10,3 +10,6 @@ repository.workspace = true [dependencies] spirv-std = { workspace = true } bytemuck = { version = "1.18.0", features = ["derive"] } + +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] } diff --git a/examples/shaders/simplest-shader/Cargo.toml b/examples/shaders/simplest-shader/Cargo.toml index 975e93d484..0b85751a41 100644 --- a/examples/shaders/simplest-shader/Cargo.toml +++ b/examples/shaders/simplest-shader/Cargo.toml @@ -13,3 +13,6 @@ crate-type = ["dylib"] [dependencies] spirv-std = { workspace = true } shared = { path = "../shared" } + +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] } diff --git a/examples/shaders/sky-shader/Cargo.toml b/examples/shaders/sky-shader/Cargo.toml index dfd7b64238..caffa12077 100644 --- a/examples/shaders/sky-shader/Cargo.toml +++ b/examples/shaders/sky-shader/Cargo.toml @@ -13,3 +13,6 @@ crate-type = ["lib", "dylib"] [dependencies] shared = { path = "../../shaders/shared" } spirv-std = { workspace = true } + +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index e31153b5c2..8f648dac91 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,7 +1,7 @@ [toolchain] -channel = "nightly-2024-04-24" +channel = "nightly-2024-06-20" components = ["rust-src", "rustc-dev", "llvm-tools"] -# commit_hash = 244da22fabd9fa677bbd0ac601a88e5ca6917526 +# commit_hash = d8a38b00024cd7156dea4ce8fd8ae113a2745e7f # Whenever changing the nightly channel, update the commit hash above, and make # sure to change `REQUIRED_TOOLCHAIN` in `crates/rustc_codegen_spirv/build.rs` also.