Skip to content

Commit

Permalink
Auto merge of rust-lang#128657 - clubby789:optimize-none, r=fee1-dead…
Browse files Browse the repository at this point in the history
…,WaffleLapkin

Add `#[optimize(none)]`

cc rust-lang#54882

This extends the `optimize` attribute to add `none`, which corresponds to the LLVM `OptimizeNone` attribute.

Not sure if an MCP is required for this, happy to file one if so.
  • Loading branch information
bors committed Jan 25, 2025
2 parents 1fc3491 + 5ac95a5 commit 6365178
Show file tree
Hide file tree
Showing 65 changed files with 374 additions and 32 deletions.
16 changes: 14 additions & 2 deletions compiler/rustc_attr_data_structures/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,25 @@ pub enum InstructionSetAttr {
ArmT32,
}

#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq, HashStable_Generic, Default)]
pub enum OptimizeAttr {
None,
/// No `#[optimize(..)]` attribute
#[default]
Default,
/// `#[optimize(none)]`
DoNotOptimize,
/// `#[optimize(speed)]`
Speed,
/// `#[optimize(size)]`
Size,
}

impl OptimizeAttr {
pub fn do_not_optimize(&self) -> bool {
matches!(self, Self::DoNotOptimize)
}
}

#[derive(Clone, Debug, Encodable, Decodable)]
pub enum DiagnosticAttribute {
// tidy-alphabetical-start
Expand Down
17 changes: 10 additions & 7 deletions compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,22 +333,25 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
let mut to_add = SmallVec::<[_; 16]>::new();

match codegen_fn_attrs.optimize {
OptimizeAttr::None => {
OptimizeAttr::Default => {
to_add.extend(default_optimisation_attrs(cx));
}
OptimizeAttr::DoNotOptimize => {
to_add.push(llvm::AttributeKind::OptimizeNone.create_attr(cx.llcx));
}
OptimizeAttr::Size => {
to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
}
OptimizeAttr::Speed => {}
}

let inline =
if codegen_fn_attrs.inline == InlineAttr::None && instance.def.requires_inline(cx.tcx) {
InlineAttr::Hint
} else {
codegen_fn_attrs.inline
};
// `optnone` requires `noinline`
let inline = match (codegen_fn_attrs.inline, &codegen_fn_attrs.optimize) {
(_, OptimizeAttr::DoNotOptimize) => InlineAttr::Never,
(InlineAttr::None, _) if instance.def.requires_inline(cx.tcx) => InlineAttr::Hint,
(inline, _) => inline,
};
to_add.extend(inline_attr(cx, inline));

// The `uwtable` attribute according to LLVM is:
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,10 +1053,7 @@ pub(crate) fn provide(providers: &mut Providers) {

let any_for_speed = defids.items().any(|id| {
let CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);
match optimize {
attr::OptimizeAttr::None | attr::OptimizeAttr::Size => false,
attr::OptimizeAttr::Speed => true,
}
matches!(optimize, attr::OptimizeAttr::Speed)
});

if any_for_speed {
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
codegen_fn_attrs.inline = InlineAttr::Never;
}

codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::None, |ia, attr| {
codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::Default, |ia, attr| {
if !attr.has_name(sym::optimize) {
return ia;
}
Expand All @@ -587,17 +587,19 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
inline_span = Some(attr.span);
if items.len() != 1 {
err(attr.span, "expected one argument");
OptimizeAttr::None
OptimizeAttr::Default
} else if list_contains_name(items, sym::size) {
OptimizeAttr::Size
} else if list_contains_name(items, sym::speed) {
OptimizeAttr::Speed
} else if list_contains_name(items, sym::none) {
OptimizeAttr::DoNotOptimize
} else {
err(items[0].span(), "invalid argument");
OptimizeAttr::None
OptimizeAttr::Default
}
} else {
OptimizeAttr::None
OptimizeAttr::Default
}
});

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
),
// RFC 2412
gated!(
optimize, Normal, template!(List: "size|speed"), ErrorPreceding,
optimize, Normal, template!(List: "none|size|speed"), ErrorPreceding,
EncodeCrossCrate::No, optimize_attribute, experimental!(optimize)
),

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/middle/codegen_fn_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl CodegenFnAttrs {
CodegenFnAttrs {
flags: CodegenFnAttrFlags::empty(),
inline: InlineAttr::None,
optimize: OptimizeAttr::None,
optimize: OptimizeAttr::Default,
export_name: None,
link_name: None,
link_ordinal: None,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/abort_unwinding_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,8 @@ impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
// We may have invalidated some `cleanup` blocks so clean those up now.
super::simplify::remove_dead_blocks(body);
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/add_call_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {

body.basic_blocks_mut().extend(new_blocks);
}

fn is_required(&self) -> bool {
true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl<'tcx> crate::MirPass<'tcx> for AddMovesForPackedDrops {

patch.apply(body);
}

fn is_required(&self) -> bool {
true
}
}

fn add_move_for_packed_drop<'tcx>(
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/add_retag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ impl<'tcx> crate::MirPass<'tcx> for AddRetag {
}
}
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/add_subtyping_projections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ impl<'tcx> crate::MirPass<'tcx> for Subtyper {
}
checker.patcher.apply(body);
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/check_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
}
}
}

fn is_required(&self) -> bool {
true
}
}

struct PointerFinder<'a, 'tcx> {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
decl.user_ty = None;
}
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/copy_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
crate::simplify::remove_unused_definitions(body);
}
}

fn is_required(&self) -> bool {
false
}
}

/// `SsaLocals` computed equivalence classes between locals considering copy/move assignments.
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,10 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
// Run derefer to fix Derefs that are not in the first place
deref_finder(tcx, body);
}

fn is_required(&self) -> bool {
true
}
}

/// Looks for any assignments between locals (e.g., `_4 = _5`) that will both be converted to fields
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ impl<'tcx> crate::MirPass<'tcx> for InstrumentCoverage {

instrument_function_for_coverage(tcx, mir_body);
}

fn is_required(&self) -> bool {
false
}
}

fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/cross_crate_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
// Don't do any inference if codegen optimizations are disabled and also MIR inlining is not
// enabled. This ensures that we do inference even if someone only passes -Zinline-mir,
// which is less confusing than having to also enable -Copt-level=1.
let inliner_will_run = pm::should_run_pass(tcx, &inline::Inline)
let inliner_will_run = pm::should_run_pass(tcx, &inline::Inline, pm::Optimizations::Allowed)
|| inline::ForceInline::should_run_pass_for_callee(tcx, def_id.to_def_id());
if matches!(tcx.sess.opts.optimize, OptLevel::No) && !inliner_will_run {
return false;
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/ctfe_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ impl<'tcx> crate::MirPass<'tcx> for CtfeLimit {
);
}
}

fn is_required(&self) -> bool {
true
}
}

fn has_back_edge(
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/dataflow_const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl<'tcx> crate::MirPass<'tcx> for DataflowConstProp {
let mut patch = visitor.patch;
debug_span!("patch").in_scope(|| patch.visit_body_preserves_cfg(body));
}

fn is_required(&self) -> bool {
false
}
}

// Note: Currently, places that have their reference taken cannot be tracked. Although this would
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/dead_store_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ impl<'tcx> crate::MirPass<'tcx> for DeadStoreElimination {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
eliminate(tcx, body);
}

fn is_required(&self) -> bool {
false
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/deduplicate_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ impl<'tcx> crate::MirPass<'tcx> for DeduplicateBlocks {
simplify_cfg(body);
}
}

fn is_required(&self) -> bool {
false
}
}

struct OptApplier<'tcx> {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/deref_separator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ impl<'tcx> crate::MirPass<'tcx> for Derefer {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
deref_finder(tcx, body);
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/dest_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {

trace!(round_count);
}

fn is_required(&self) -> bool {
false
}
}

#[derive(Debug, Default)]
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/dump_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ impl<'tcx> crate::MirPass<'tcx> for Marker {
}

fn run_pass(&self, _tcx: TyCtxt<'tcx>, _body: &mut Body<'tcx>) {}

fn is_required(&self) -> bool {
false
}
}

pub fn emit_mir(tcx: TyCtxt<'_>) -> io::Result<()> {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/early_otherwise_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
simplify_cfg(body);
}
}

fn is_required(&self) -> bool {
false
}
}

#[derive(Debug)]
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/elaborate_box_derefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,8 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateBoxDerefs {
}
}
}

fn is_required(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateDrops {
elaborate_patch.apply(body);
deref_finder(tcx, body);
}

fn is_required(&self) -> bool {
true
}
}

/// Records unwind edges which are known to be unreachable, because they are in `drop` terminators
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
// statements.
StorageRemover { tcx, reused_locals: state.reused_locals }.visit_body_preserves_cfg(body);
}

fn is_required(&self) -> bool {
false
}
}

newtype_index! {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/impossible_predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ impl<'tcx> MirPass<'tcx> for ImpossiblePredicates {
body.local_decls.raw.truncate(body.arg_count + 1);
}
}

fn is_required(&self) -> bool {
true
}
}
8 changes: 8 additions & 0 deletions compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl<'tcx> crate::MirPass<'tcx> for Inline {
deref_finder(tcx, body);
}
}

fn is_required(&self) -> bool {
false
}
}

pub struct ForceInline;
Expand All @@ -85,6 +89,10 @@ impl<'tcx> crate::MirPass<'tcx> for ForceInline {
false
}

fn is_required(&self) -> bool {
true
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let span = trace_span!("force_inline", body = %tcx.def_path_str(body.source.def_id()));
let _guard = span.enter();
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/instsimplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
simplify_duplicate_switch_targets(block.terminator.as_mut().unwrap());
}
}

fn is_required(&self) -> bool {
false
}
}

struct InstSimplifyContext<'a, 'tcx> {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/jump_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ impl<'tcx> crate::MirPass<'tcx> for JumpThreading {
}
OpportunitySet::new(body, opportunities).apply(body);
}

fn is_required(&self) -> bool {
false
}
}

#[derive(Debug)]
Expand Down
Loading

0 comments on commit 6365178

Please sign in to comment.