Skip to content
Draft
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
31 changes: 29 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-05-22"
channel = "nightly-2026-06-09"
components = ["rust-src", "rustc-dev", "llvm-tools"]
# commit_hash = e96c36b6f76833388c519561d145492d2c08db4e"#;
# commit_hash = cb46fbb8c6ea799c6fba9188ed889275c35a8c28"#;

fn rustc_output(arg: &str) -> Result<String, Box<dyn Error>> {
let rustc = env::var("RUSTC").unwrap_or_else(|_| "rustc".into());
Expand Down Expand Up @@ -256,6 +256,33 @@ pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {",
);
}

// HACK(firestar99): Undo code cleanup that prevents passing ScalarPairs as `PassMode::Direct`
// https://github.com/rust-lang/rust/commit/dfc475d018c780475ea962f15d86cfa05a50a148
if relative_path == Path::new("src/mir/mod.rs") {
src = src.replace(
"
debug_assert!(bx.is_backend_immediate(arg.layout));
return local(OperandRef {
val: OperandValue::Immediate(llarg),
layout: arg.layout,
move_annotation: None,
});",
"
return local(OperandRef::from_immediate_or_packed_pair(
bx, llarg, arg.layout,
));",
);
}
if relative_path == Path::new("src/mir/block.rs") {
src = src.replace(
r#"
PassMode::Direct(_) => (op.immediate(), arg.layout.align.abi, false),
PassMode::Ignore | PassMode::Pair(..) => unreachable!("handled above"),"#,
"\
_ => (op.immediate_or_packed_pair(bx), arg.layout.align.abi, false),",
);
}

fs::write(out_path, src)?;
}
}
Expand Down
36 changes: 19 additions & 17 deletions crates/rustc_codegen_spirv/src/builder/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use crate::spirv_type::SpirvType;
use rspirv::dr::Operand;
use rspirv::spirv::GLOp;
use rustc_codegen_ssa::RetagInfo;
use rustc_codegen_ssa::mir::IntrinsicResult;
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
use rustc_codegen_ssa::mir::place::PlaceRef;
use rustc_codegen_ssa::mir::place::{PlaceRef, PlaceValue};
use rustc_codegen_ssa::traits::{BuilderMethods, IntrinsicCallBuilderMethods};
use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::{FnDef, Instance, Ty, TyKind, TypingEnv};
Expand Down Expand Up @@ -64,9 +65,14 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
&mut self,
instance: Instance<'tcx>,
args: &[OperandRef<'tcx, Self::Value>],
result: PlaceRef<'tcx, Self::Value>,
result_layout: ty::layout::TyAndLayout<'tcx>,
result_place: Option<PlaceValue<Self::Value>>,
_span: Span,
) -> Result<(), ty::Instance<'tcx>> {
) -> IntrinsicResult<'tcx, Self::Value> {
let result = PlaceRef {
val: result_place.unwrap(),
layout: result_layout,
};
let callee_ty = instance.ty(self.tcx, TypingEnv::fully_monomorphized());

let (def_id, fn_args) = match *callee_ty.kind() {
Expand Down Expand Up @@ -95,7 +101,7 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
sym::breakpoint => {
self.abort();
assert!(result.layout.ty.is_unit());
return Ok(());
return IntrinsicResult::WroteIntoPlace;
}

sym::volatile_load | sym::unaligned_volatile_load => {
Expand All @@ -105,7 +111,7 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
if !result.layout.is_zst() {
self.store(load, result.val.llval, result.val.align);
}
return Ok(());
return IntrinsicResult::WroteIntoPlace;
}

sym::prefetch_read_data
Expand All @@ -114,7 +120,7 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
| sym::prefetch_write_instruction => {
// ignore
assert!(result.layout.ty.is_unit());
return Ok(());
return IntrinsicResult::WroteIntoPlace;
}

sym::saturating_add => {
Expand Down Expand Up @@ -352,7 +358,10 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {

_ => {
// Call the fallback body instead of generating the intrinsic code
return Err(ty::Instance::new_raw(instance.def_id(), instance.args));
return IntrinsicResult::Fallback(Instance::new_raw(
instance.def_id(),
instance.args,
));
}
};

Expand All @@ -368,7 +377,7 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
.val
.store(self, result);
}
Ok(())
IntrinsicResult::WroteIntoPlace
}

fn codegen_llvm_intrinsic_call(
Expand Down Expand Up @@ -402,16 +411,9 @@ impl<'a, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'tcx> {
todo!()
}

fn va_start(&mut self, val: Self::Value) -> Self::Value {
// SPIR-V backend has no variadic ABI support; keep the placeholder
// operand unchanged so MIR lowering can proceed without crashing.
val
}
fn va_start(&mut self, _val: Self::Value) {}

fn va_end(&mut self, val: Self::Value) -> Self::Value {
// See `va_start` above.
val
}
fn va_end(&mut self, _val: Self::Value) {}

fn retag_mem(&mut self, _place: Self::Value, _info: &RetagInfo<Self::Value>) {
bug!("retag not supported")
Expand Down
4 changes: 4 additions & 0 deletions crates/rustc_codegen_spirv/src/codegen_cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,10 @@ impl<'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'tcx> {
fn declare_c_main(&self, _fn_type: Self::FunctionSignature) -> Option<Self::Function> {
todo!()
}

fn intrinsic_call_expects_place_always(&self, _: Symbol) -> bool {
true
}
}

impl<'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'tcx> {
Expand Down
8 changes: 5 additions & 3 deletions crates/rustc_codegen_spirv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,10 @@ use maybe_pqp_cg_ssa::{
};
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::dep_graph::{WorkProduct, WorkProductMap};
use rustc_middle::mono::{MonoItem, MonoItemData};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{InstanceKind, TyCtxt};
Expand Down Expand Up @@ -258,7 +257,7 @@ impl CodegenBackend for SpirvCodegenBackend {
sess: &Session,
_outputs: &OutputFilenames,
crate_info: &CrateInfo,
) -> (CompiledModules, FxIndexMap<WorkProductId, WorkProduct>) {
) -> (CompiledModules, WorkProductMap) {
ongoing_codegen
.downcast::<OngoingCodegen<Self>>()
.expect("Expected OngoingCodegen, found Box<Any>")
Expand Down Expand Up @@ -411,6 +410,7 @@ impl WriteBackendMethods for SpirvCodegenBackend {
name,
kind,
object: Some(path),
global_asm_object: None,
dwarf_object: None,
bytecode: None,
assembly: None,
Expand All @@ -434,6 +434,8 @@ impl WriteBackendMethods for SpirvCodegenBackend {
}

impl ExtraBackendMethods for SpirvCodegenBackend {
type Module = rspirv::dr::Module;

fn codegen_allocator(&self, _: TyCtxt<'_>, _: &str, _: &[AllocatorMethod]) -> Self::Module {
todo!()
}
Expand Down
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-05-22"
channel = "nightly-2026-06-09"
components = ["rust-src", "rustc-dev", "llvm-tools"]
# commit_hash = e96c36b6f76833388c519561d145492d2c08db4e
# commit_hash = cb46fbb8c6ea799c6fba9188ed889275c35a8c28

# Whenever changing the nightly channel, update the commit hash above, and
# change `REQUIRED_RUST_TOOLCHAIN` in `crates/rustc_codegen_spirv/build.rs` too.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ OpLine %5 41 19
%10 = OpIAdd %7 %11 %12
OpLine %5 47 8
%13 = OpBitcast %7 %14
OpLine %15 1244 17
OpLine %15 1245 17
%16 = OpBitcast %7 %17
OpLine %5 46 4
%18 = OpCompositeConstruct %2 %13 %16 %19 %20 %21 %22 %6 %23 %10 %24 %24 %24
Expand Down
Loading