Move target machine factory error reporting into codegen backend

This commit is contained in:
bjorn3 2026-02-11 10:53:38 +00:00
parent 70587ce07c
commit 2d07e81a5c
6 changed files with 12 additions and 24 deletions

View file

@ -374,7 +374,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
_features: &[String],
) -> TargetMachineFactoryFn<Self> {
// TODO(antoyo): set opt level.
Arc::new(|_| Ok(()))
Arc::new(|_, _| ())
}
}
@ -421,7 +421,6 @@ unsafe impl Sync for SyncContext {}
impl WriteBackendMethods for GccCodegenBackend {
type Module = GccContext;
type TargetMachine = ();
type TargetMachineError = ();
type ModuleBuffer = ModuleBuffer;
type ThinData = ThinData;
type ThinBuffer = ThinBuffer;

View file

@ -38,8 +38,8 @@ use crate::builder::SBuilder;
use crate::builder::gpu_offload::scalar_width;
use crate::common::AsCCharPtr;
use crate::errors::{
CopyBitcode, FromLlvmDiag, FromLlvmOptimizationDiag, LlvmError, UnknownCompression,
WithLlvmError, WriteBytecode,
CopyBitcode, FromLlvmDiag, FromLlvmOptimizationDiag, LlvmError, ParseTargetMachineConfig,
UnknownCompression, WithLlvmError, WriteBytecode,
};
use crate::llvm::diagnostic::OptimizationDiagnosticKind::*;
use crate::llvm::{self, DiagnosticInfo};
@ -111,8 +111,7 @@ pub(crate) fn create_informational_target_machine(
// Can't use query system here quite yet because this function is invoked before the query
// system/tcx is set up.
let features = llvm_util::global_llvm_features(sess, only_base_features);
target_machine_factory(sess, config::OptLevel::No, &features)(config)
.unwrap_or_else(|err| llvm_err(sess.dcx(), err))
target_machine_factory(sess, config::OptLevel::No, &features)(sess.dcx(), config)
}
pub(crate) fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> OwnedTargetMachine {
@ -138,8 +137,7 @@ pub(crate) fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> OwnedTar
tcx.sess,
tcx.backend_optimization_level(()),
tcx.global_backend_features(()),
)(config)
.unwrap_or_else(|err| llvm_err(tcx.dcx(), err))
)(tcx.dcx(), config)
}
fn to_llvm_opt_settings(cfg: config::OptLevel) -> (llvm::CodeGenOptLevel, llvm::CodeGenOptSize) {
@ -278,7 +276,7 @@ pub(crate) fn target_machine_factory(
let large_data_threshold = sess.opts.unstable_opts.large_data_threshold.unwrap_or(0);
let prof = SelfProfilerRef::clone(&sess.prof);
Arc::new(move |config: TargetMachineFactoryConfig| {
Arc::new(move |dcx: DiagCtxtHandle<'_>, config: TargetMachineFactoryConfig| {
// Self-profile timer for invoking a factory to create a target machine.
let _prof_timer = prof.generic_activity("target_machine_factory_inner");
@ -320,6 +318,7 @@ pub(crate) fn target_machine_factory(
use_wasm_eh,
large_data_threshold,
)
.unwrap_or_else(|err| dcx.emit_fatal(ParseTargetMachineConfig(err)))
})
}

View file

@ -94,7 +94,7 @@ pub(crate) struct LtoBitcodeFromRlib {
}
#[derive(Diagnostic)]
pub enum LlvmError<'a> {
pub(crate) enum LlvmError<'a> {
#[diag("could not write output to {$path}")]
WriteOutput { path: &'a Path },
#[diag("could not create LLVM TargetMachine for triple: {$triple}")]

View file

@ -25,7 +25,6 @@ use std::path::PathBuf;
use back::owned_target_machine::OwnedTargetMachine;
use back::write::{create_informational_target_machine, create_target_machine};
use context::SimpleCx;
use errors::ParseTargetMachineConfig;
use llvm_util::target_config;
use rustc_ast::expand::allocator::AllocatorMethod;
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule};
@ -152,7 +151,6 @@ impl WriteBackendMethods for LlvmCodegenBackend {
type Module = ModuleLlvm;
type ModuleBuffer = back::lto::ModuleBuffer;
type TargetMachine = OwnedTargetMachine;
type TargetMachineError = crate::errors::LlvmError<'static>;
type ThinData = back::lto::ThinData;
type ThinBuffer = back::lto::ThinBuffer;
fn print_pass_timings(&self) {
@ -445,13 +443,7 @@ impl ModuleLlvm {
name: &str,
dcx: DiagCtxtHandle<'_>,
) -> OwnedTargetMachine {
let tm_factory_config = TargetMachineFactoryConfig::new(cgcx, name);
match (cgcx.tm_factory)(tm_factory_config) {
Ok(m) => m,
Err(e) => {
dcx.emit_fatal(ParseTargetMachineConfig(e));
}
}
(cgcx.tm_factory)(dcx, TargetMachineFactoryConfig::new(cgcx, name))
}
fn parse(

View file

@ -313,11 +313,10 @@ impl TargetMachineFactoryConfig {
pub type TargetMachineFactoryFn<B> = Arc<
dyn Fn(
DiagCtxtHandle<'_>,
TargetMachineFactoryConfig,
) -> Result<
<B as WriteBackendMethods>::TargetMachine,
<B as WriteBackendMethods>::TargetMachineError,
> + Send
) -> <B as WriteBackendMethods>::TargetMachine
+ Send
+ Sync,
>;

View file

@ -10,7 +10,6 @@ use crate::{CompiledModule, ModuleCodegen};
pub trait WriteBackendMethods: Clone + 'static {
type Module: Send + Sync;
type TargetMachine;
type TargetMachineError;
type ModuleBuffer: ModuleBufferMethods;
type ThinData: Send + Sync;
type ThinBuffer: ThinBufferMethods;