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
4 changes: 2 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-2026-04-02"
channel = "nightly-2026-04-08"
components = ["rust-src", "rustc-dev", "llvm-tools"]
# commit_hash = 7e46c5f6fb87f8cf4353e058479cef15d1d952b4"#;
# commit_hash = c756124775121dea0e640652c5ee3c89e3dd0eb4"#;

fn rustc_output(arg: &str) -> Result<String, Box<dyn Error>> {
let rustc = env::var("RUSTC").unwrap_or_else(|_| "rustc".into());
Expand Down
16 changes: 6 additions & 10 deletions crates/rustc_codegen_spirv/src/builder/builder_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::maybe_pqp_cg_ssa as rustc_codegen_ssa;

use super::Builder;
use crate::abi::ConvSpirvType;
use crate::builder::format_args_decompiler::{CodegenPanic, DecodedFormatArgs};
use crate::builder_spirv::{
SpirvBlockCursor, SpirvConst, SpirvValue, SpirvValueExt, SpirvValueKind,
};
Expand All @@ -26,14 +27,13 @@ use rustc_codegen_ssa::traits::{
};
use rustc_middle::bug;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::{self, AtomicOrdering, Ty};
use rustc_span::Span;
use rustc_target::callconv::FnAbi;
use smallvec::SmallVec;
use std::iter::{self, empty};
use std::ops::{BitAnd, BitOr, BitXor, Not, RangeInclusive};

use crate::builder::format_args_decompiler::{CodegenPanic, DecodedFormatArgs};
use tracing::{Level, instrument, span};
use tracing::{trace, warn};

Expand Down Expand Up @@ -1825,10 +1825,6 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
self.declare_func_local_var(self.type_array(self.type_i8(), size.bytes()), align)
}

fn scalable_alloca(&mut self, _elt: u64, _align: Align, _element_ty: Ty<'_>) -> Self::Value {
bug!("scalable alloca is not supported in SPIR-V backend")
}

fn load(&mut self, ty: Self::Type, ptr: Self::Value, _align: Align) -> Self::Value {
let (ptr, access_ty) = self.adjust_pointer_for_typed_access(ptr, ty);
let loaded_val = ptr.const_fold_load(self).unwrap_or_else(|| {
Expand Down Expand Up @@ -3081,10 +3077,6 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
bug!("Funclets are not supported")
}

fn get_funclet_cleanuppad(&self, _funclet: &Self::Funclet) -> Self::Value {
bug!("Funclets are not supported")
}

fn atomic_cmpxchg(
&mut self,
dst: Self::Value,
Expand Down Expand Up @@ -3466,4 +3458,8 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
fn apply_attrs_to_cleanup_callsite(&mut self, _llret: Self::Value) {
// Ignore
}

fn alloca_with_ty(&mut self, _layout: TyAndLayout<'tcx>) -> Self::Value {
bug!("scalable alloca is not supported in SPIR-V backend")
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the replacement for scalable_alloca, otherwise this is never called, although judging by the name who knows what'll be used for in the future

}
9 changes: 6 additions & 3 deletions crates/rustc_codegen_spirv/src/codegen_cx/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ impl ConstCodegenMethods for CodegenCx<'_> {
fn const_i32(&self, i: i32) -> Self::Value {
self.constant_i32(DUMMY_SP, i)
}
fn const_i64(&self, i: i64) -> Self::Value {
self.constant_i64(DUMMY_SP, i)
}
fn const_u8(&self, i: u8) -> Self::Value {
self.constant_u8(DUMMY_SP, i)
}
fn const_u32(&self, i: u32) -> Self::Value {
self.constant_u32(DUMMY_SP, i)
}
Expand All @@ -156,9 +162,6 @@ impl ConstCodegenMethods for CodegenCx<'_> {
let t = SpirvType::Integer(ptr_size, false).def(DUMMY_SP, self);
self.constant_int(t, i.into())
}
fn const_u8(&self, i: u8) -> Self::Value {
self.constant_u8(DUMMY_SP, i)
}
fn const_real(&self, t: Self::Type, val: f64) -> Self::Value {
self.constant_float(t, val)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rustc_codegen_spirv/src/codegen_cx/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_hir::attrs::{InlineAttr, Linkage};
use rustc_middle::bug;
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc_middle::mir::interpret::ConstAllocation;
use rustc_middle::mir::mono::{MonoItem, Visibility};
use rustc_middle::mono::{MonoItem, Visibility};
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
use rustc_middle::ty::{self, Instance, TypeVisitableExt, TypingEnv};
use rustc_span::Span;
Expand Down
2 changes: 1 addition & 1 deletion crates/rustc_codegen_spirv/src/codegen_cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rustc_codegen_ssa::traits::{
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::DefId;
use rustc_middle::mir;
use rustc_middle::mir::mono::CodegenUnit;
use rustc_middle::mono::CodegenUnit;
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv};
use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypingEnv};
use rustc_session::Session;
Expand Down
19 changes: 10 additions & 9 deletions crates/rustc_codegen_spirv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ mod symbols;
mod target;
mod target_feature;

use crate::maybe_pqp_cg_ssa::back::write::ThinLtoInput;
use builder::Builder;
use codegen_cx::CodegenCx;
use maybe_pqp_cg_ssa::back::lto::{SerializedModule, ThinModule};
use maybe_pqp_cg_ssa::back::lto::ThinModule;
use maybe_pqp_cg_ssa::back::write::{
CodegenContext, FatLtoInput, ModuleConfig, OngoingCodegen, SharedEmitter,
TargetMachineFactoryFn,
Expand All @@ -152,10 +153,11 @@ use rspirv::binary::Assemble;
use rustc_ast::expand::allocator::AllocatorMethod;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::DiagCtxtHandle;
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::mono::{MonoItem, MonoItemData};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{InstanceKind, TyCtxt};
use rustc_session::Session;
Expand Down Expand Up @@ -338,16 +340,15 @@ impl WriteBackendMethods for SpirvCodegenBackend {
}

fn run_thin_lto(
cgcx: &CodegenContext,
_cgcx: &CodegenContext,
_prof: &SelfProfilerRef,
_dcx: rustc_errors::DiagCtxtHandle<'_>,
// FIXME(bjorn3): Limit LTO exports to these symbols
_dcx: DiagCtxtHandle<'_>,
_exported_symbols_for_lto: &[String],
_each_linked_rlib_for_lto: &[PathBuf], // njn: ?
modules: Vec<(String, Self::ModuleBuffer)>,
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
_each_linked_rlib_for_lto: &[PathBuf],
_modules: Vec<ThinLtoInput<Self>>,
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
link::run_thin(cgcx, modules, cached_modules)
// Note(@firestar99): gcc impl this as unreachable as well
unreachable!()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this is unreachable? What if someone sets LTO to thin?

https://doc.rust-lang.org/cargo/reference/profiles.html#lto

Wouldn't what we did before, just pass-through, not be better?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gcc impl is just unreachable!(), as gcc doesn't seem to have ThinLTO: rust-lang

I personally feel like we've got quite a few things implemented "in case they are used" without actually knowing when they're used. So we can't test them either, so there's no way to know whether our "mock" implementations actually work. To me, removing these dead code paths by a hard error and seeing it never trigger is a good thing, as it means less code to maintain.

With the ThinLTO interface having gotten a refactor the past week, I don't exactly know how to actually implement this pass-through correctly, and even worse, how to even test it.

}

fn optimize(
Expand Down
59 changes: 3 additions & 56 deletions crates/rustc_codegen_spirv/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,33 @@
use crate::maybe_pqp_cg_ssa as rustc_codegen_ssa;

use crate::codegen_cx::{CodegenArgs, SpirvMetadata};
use crate::{SpirvCodegenBackend, SpirvModuleBuffer, linker};
use crate::linker;
use ar::{Archive, GnuBuilder, Header};
use rspirv::binary::Assemble;
use rspirv::dr::Module;
use rustc_attr_parsing::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::{CompiledModules, CrateInfo, NativeLib};
use rustc_data_structures::fx::FxHashSet;
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;
use rustc_middle::middle::dependency_format::Linkage;
use rustc_session::Session;
use rustc_session::config::{
CrateType, DebugInfo, Lto, OptLevel, OutFileName, OutputFilenames, OutputType,
CrateType, DebugInfo, OptLevel, OutFileName, OutputFilenames, OutputType,
};
use rustc_session::output::{check_file_is_writeable, invalid_output_for_target, out_filename};
use rustc_span::Symbol;
use spirv_tools::TargetEnv;
use std::collections::BTreeMap;
use std::ffi::{CString, OsStr};
use std::ffi::OsStr;
use std::fs::File;
use std::io::{BufWriter, Read};
use std::iter;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;

pub fn link(
sess: &Session,
Expand Down Expand Up @@ -624,52 +620,3 @@ fn do_link(
bug!("Linker errored, but no error reported");
}
}

/// As of right now, this is essentially a no-op, just plumbing through all the files.
// TODO: WorkProduct impl
pub(crate) fn run_thin(
cgcx: &CodegenContext,
modules: Vec<(String, SpirvModuleBuffer)>,
cached_modules: Vec<(SerializedModule<SpirvModuleBuffer>, WorkProduct)>,
) -> (Vec<ThinModule<SpirvCodegenBackend>>, Vec<WorkProduct>) {
if cgcx.use_linker_plugin_lto {
unreachable!("We should never reach this case if the LTO step is deferred to the linker");
}
assert!(
cgcx.lto == Lto::ThinLocal,
"no actual LTO implemented in Rust-GPU"
);
let mut thin_buffers = Vec::with_capacity(modules.len());
let mut module_names = Vec::with_capacity(modules.len() + cached_modules.len());

for (name, buffer) in modules {
let cname = CString::new(name.clone()).unwrap();
thin_buffers.push(buffer);
module_names.push(cname);
}

let mut serialized_modules = Vec::with_capacity(cached_modules.len());

for (sm, wp) in cached_modules {
let _slice_u8 = sm.data();
serialized_modules.push(sm);
module_names.push(CString::new(wp.cgu_name).unwrap());
}

let shared = Arc::new(ThinShared {
data: (),
thin_buffers,
serialized_modules,
module_names,
});

let mut opt_jobs = vec![];
for (module_index, _) in shared.module_names.iter().enumerate() {
opt_jobs.push(ThinModule {
shared: shared.clone(),
idx: module_index,
});
}

(opt_jobs, vec![])
}
4 changes: 2 additions & 2 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[toolchain]
channel = "nightly-2026-04-02"
channel = "nightly-2026-04-08"
components = ["rust-src", "rustc-dev", "llvm-tools"]
# commit_hash = 7e46c5f6fb87f8cf4353e058479cef15d1d952b4
# commit_hash = c756124775121dea0e640652c5ee3c89e3dd0eb4

# Whenever changing the nightly channel, update the commit hash above, and
# change `REQUIRED_RUST_TOOLCHAIN` in `crates/rustc_codegen_spirv/build.rs` too.
Loading