Skip to content
Merged
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
8 changes: 2 additions & 6 deletions compiler/rustc_codegen_cranelift/src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_middle::dep_graph::WorkProduct;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mono::{MonoItem, MonoItemData, Visibility};
use rustc_session::config::{OptLevel, OutputFilenames, OutputType};
use rustc_session::config::{OutputFilenames, OutputType};
use rustc_session::{BorrowedIncrCompSession, Session};
use rustc_span::Symbol;

Expand Down Expand Up @@ -318,11 +318,7 @@ impl WriteBackendMethods for AotDriver {
type TargetMachine = ();
type ThinData = Infallible;

fn target_machine_factory(
&self,
_sess: &Session,
_opt_level: OptLevel,
) -> TargetMachineFactoryFn<Self> {
fn target_machine_factory(&self, _sess: &Session) -> TargetMachineFactoryFn<Self> {
Arc::new(|_, _| ())
}

Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_codegen_gcc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,7 @@ impl WriteBackendMethods for GccCodegenBackend {
false
}

fn target_machine_factory(
&self,
_sess: &Session,
_opt_level: OptLevel,
) -> TargetMachineFactoryFn<Self> {
fn target_machine_factory(&self, _sess: &Session) -> TargetMachineFactoryFn<Self> {
// FIXME(antoyo): set opt level.
Arc::new(|_, _| ())
}
Expand Down
10 changes: 3 additions & 7 deletions compiler/rustc_codegen_llvm/src/back/llvm_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductMap};
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{OptLevel, OutputFilenames, PrintKind, PrintRequest};
use rustc_session::config::{OutputFilenames, PrintKind, PrintRequest};
use rustc_session::{
BorrowedIncrCompSession, CodegenBackendInit, EarlySession, IncrCompSession, Session,
};
Expand Down Expand Up @@ -89,12 +89,8 @@ impl WriteBackendMethods for LlvmCodegenBackend {
fn thread_profiler() -> Box<dyn Any> {
Box::new(TimeTraceProfiler::new())
}
fn target_machine_factory(
&self,
sess: &Session,
optlvl: OptLevel,
) -> TargetMachineFactoryFn<Self> {
back::write::target_machine_factory(sess, optlvl)
fn target_machine_factory(&self, sess: &Session) -> TargetMachineFactoryFn<Self> {
back::write::target_machine_factory(sess, sess.opts.optimize)

@bjorn3 bjorn3 Nov 7, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this be changed to only accept sess?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was considering that, but there is a "create_informational_machine" (or thereabouts) function that passes OptLevel::No, and I was unsure whether passing the opt level there would cause perf regressions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That function is only used for fetching information from LLVM, not for any actual compilation, so I think it would't affect performance at all, but I don't know if LLVM does some work on creating the optimization pipeline in advance when creating a target machine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, that's exactly my doubt too :-)

}
fn optimize_and_codegen_fat_lto(
sess: &Session,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub(crate) fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> OwnedTar
Some(tcx.output_filenames(()).temp_path_for_cgu(OutputType::Object, mod_name));
let config = TargetMachineFactoryConfig { split_dwarf_file, output_obj_file };

target_machine_factory(tcx.sess, tcx.backend_optimization_level(()))(tcx.dcx(), config)
target_machine_factory(tcx.sess, tcx.sess.opts.optimize)(tcx.dcx(), config)
}

// `s` and `z` are the same codegen level; what tells LLVM that they are size builds is the
Expand Down
13 changes: 5 additions & 8 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use rustc_metadata::fs::copy_to_stdout;
use rustc_middle::dep_graph::{WorkProduct, WorkProductMap};
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{
self, JobsMode, Lto, OptLevel, OptimizationObjective, OutFileName, OutputFilenames, OutputType,
Passes, SwitchWithOptPath,
self, JobsMode, Lto, OptimizationObjective, OutFileName, OutputFilenames, OutputType, Passes,
SwitchWithOptPath,
};
use rustc_session::{BorrowedIncrCompSession, IncrCompSession, Session};
use rustc_span::source_map::SourceMap;
Expand Down Expand Up @@ -328,7 +328,6 @@ pub struct CodegenContext {
pub crate_types: Vec<CrateType>,
pub output_filenames: Arc<OutputFilenames>,
pub module_config: Arc<ModuleConfig>,
pub opt_level: OptLevel,
pub msvc_imps_needed: bool,
pub is_pe_coff: bool,
pub target_can_use_split_dwarf: bool,
Expand Down Expand Up @@ -1243,8 +1242,7 @@ fn start_executing_work<B: WriteBackendMethods>(
.expect("failed to spawn helper thread")
});

let opt_level = tcx.backend_optimization_level(());
let tm_factory = backend.target_machine_factory(tcx.sess, opt_level);
let tm_factory = backend.target_machine_factory(tcx.sess);

let remark_dir = if let Some(ref dir) = sess.opts.unstable_opts.remark_dir {
let result = fs::create_dir_all(dir).and_then(|_| dir.canonicalize());
Expand All @@ -1271,7 +1269,6 @@ fn start_executing_work<B: WriteBackendMethods>(
remark_dir,
output_filenames: Arc::clone(tcx.output_filenames(())),
module_config: regular_config,
opt_level,
msvc_imps_needed: msvc_imps_needed(tcx),
is_pe_coff: tcx.sess.target.is_like_windows,
target_can_use_split_dwarf: tcx.sess.target_can_use_split_dwarf(),
Expand Down Expand Up @@ -2147,7 +2144,7 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
(compiled_modules, work_products)
}
MaybeLtoModules::FatLto { cgcx, needs_fat_lto } => {
let tm_factory = self.backend.target_machine_factory(sess, cgcx.opt_level);
let tm_factory = self.backend.target_machine_factory(sess);

// FIXME cache local pre-LTO bitcode
let work_products = WorkProductMap::default();
Expand All @@ -2168,7 +2165,7 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
(compiled_modules, work_products)
}
MaybeLtoModules::ThinLto { cgcx, needs_thin_lto } => {
let tm_factory = self.backend.target_machine_factory(sess, cgcx.opt_level);
let tm_factory = self.backend.target_machine_factory(sess);

let compiled_modules = CompiledModules {
modules: do_thin_lto::<B>(
Expand Down
28 changes: 1 addition & 27 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ use rustc_ast::expand::allocator::{
};
use rustc_attr_ir::lang_items::LangItem;
use rustc_attr_ir::target::Target;
use rustc_attr_ir::{DebuggerVisualizerType, EiiDecl, EiiImpl, OptimizeAttr, find_attr};
use rustc_attr_ir::{DebuggerVisualizerType, EiiDecl, EiiImpl, find_attr};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
use rustc_data_structures::sync::{IntoDynSyncSend, par_map};
use rustc_data_structures::unord::UnordMap;
use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
use rustc_middle::middle::dependency_format::{Dependencies, Linkage};
use rustc_middle::middle::exported_symbols::{self, SymbolExportKind};
use rustc_middle::middle::lang_items;
use rustc_middle::mir::interpret::{CTFE_ALLOC_SALT, ErrorHandled, Scalar};
use rustc_middle::mir::{BinOp, ConstValue};
use rustc_middle::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem, MonoItemPartitions};
use rustc_middle::query::Providers;
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
use rustc_middle::ty::{self, Instance, PatternKind, Ty, TyCtxt, UintTy, Unnormalized};
use rustc_session::config::{self, EntryFnType, JobsMode};
Expand Down Expand Up @@ -1217,30 +1215,6 @@ impl CrateInfo {
}
}

pub(crate) fn provide(providers: &mut Providers) {
providers.backend_optimization_level = |tcx, cratenum| {
let global = tcx.sess.opts.optimize;

// Nothing to promote outside a size build. At `-Copt-level=0` the upgrade would populate
// the pass manager, and its module-wide passes ignore the `optnone` annotation.
if global.objective() != config::OptimizationObjective::Size {
return global;
}

// A speed-marked body in a size build needs a speed level, and `-O2` is the lowest one.
let for_speed = config::OptLevel::More;

let defids = tcx.collect_and_partition_mono_items(cratenum).all_mono_items;

let any_for_speed = defids.items().any(|id| {
let CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);
matches!(optimize, OptimizeAttr::Speed)
});

if any_for_speed { for_speed } else { global }
};
}

pub fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguReuse {
if !tcx.dep_graph.is_fully_enabled()
|| tcx.sess.opts.unstable_opts.disable_incr_comp_backend_caching
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ pub enum CodegenError {

pub fn provide(providers: &mut Providers) {
crate::back::symbol_export::provide(providers);
crate::base::provide(&mut providers.queries);
crate::target_features::provide(&mut providers.queries);
crate::codegen_attrs::provide(&mut providers.queries);
}
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_codegen_ssa/src/traits/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::PathBuf;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::DiagCtxtHandle;
use rustc_middle::dep_graph::WorkProduct;
use rustc_session::{BorrowedIncrCompSession, Session, config};
use rustc_session::{BorrowedIncrCompSession, Session};

use crate::back::lto::ThinModule;
use crate::back::write::{
Expand All @@ -28,11 +28,7 @@ pub trait WriteBackendMethods: Clone + 'static {
fn thread_profiler() -> Box<dyn Any> {
Box::new(())
}
fn target_machine_factory(
&self,
sess: &Session,
opt_level: config::OptLevel,
) -> TargetMachineFactoryFn<Self>;
fn target_machine_factory(&self, sess: &Session) -> TargetMachineFactoryFn<Self>;
/// Performs fat LTO by merging all modules into a single one, running autodiff
/// if necessary and running any further optimizations
fn optimize_and_codegen_fat_lto(
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_middle/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ use rustc_lint_defs::{LintId, StableLintExpectationId};
use rustc_macros::rustc_queries;
use rustc_middle::middle::resolve::TypeRelativeDelegationRes;
use rustc_session::Limits;
use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};
use rustc_session::config::{EntryFnType, OutputFilenames, SymbolManglingVersion};
use rustc_span::def_id::{LOCAL_CRATE, ModId};
use rustc_span::{DUMMY_SP, LocalExpnId, Span, Spanned, Symbol};
use rustc_target::spec::PanicStrategy;
Expand Down Expand Up @@ -2488,10 +2488,6 @@ rustc_queries! {
desc { "getting codegen unit `{sym}`" }
}

query backend_optimization_level(_: ()) -> OptLevel {
desc { "optimization level used by backend" }
}

/// Return the filenames where output artefacts shall be stored.
///
/// This query returns an `&Arc` because codegen backends need the value even after the `TyCtxt`
Expand Down
Loading