Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 334c324

Browse files
committed
Auto merge of rust-lang#128657 - clubby789:optimize-none, r=<try>
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.
2 parents 7347f8e + 4408092 commit 334c324

32 files changed

+235
-88
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,22 @@ pub enum InstructionSetAttr {
5353
ArmT32,
5454
}
5555

56-
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
56+
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic, PartialEq, Eq)]
5757
pub enum OptimizeAttr {
58+
/// `#[optimize(none)]`. This implies `#[inline(never)]`, required by LLVM
5859
None,
60+
/// `#[optimize(speed)]`
5961
Speed,
62+
/// `#[optimize(size)]`
6063
Size,
6164
}
6265

66+
impl OptimizeAttr {
67+
pub fn do_not_optimize(&self) -> bool {
68+
matches!(*self, OptimizeAttr::None)
69+
}
70+
}
71+
6372
/// Represents the following attributes:
6473
///
6574
/// - `#[stable]`

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,22 +337,27 @@ pub fn llfn_attrs_from_instance<'ll, 'tcx>(
337337
let mut to_add = SmallVec::<[_; 16]>::new();
338338

339339
match codegen_fn_attrs.optimize {
340-
OptimizeAttr::None => {
340+
None => {
341341
to_add.extend(default_optimisation_attrs(cx));
342342
}
343-
OptimizeAttr::Size => {
343+
Some(OptimizeAttr::None) => {
344+
to_add.push(llvm::AttributeKind::OptimizeNone.create_attr(cx.llcx));
345+
}
346+
Some(OptimizeAttr::Size) => {
344347
to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
345348
to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
346349
}
347-
OptimizeAttr::Speed => {}
350+
Some(OptimizeAttr::Speed) => {}
348351
}
349352

350-
let inline =
351-
if codegen_fn_attrs.inline == InlineAttr::None && instance.def.requires_inline(cx.tcx) {
352-
InlineAttr::Hint
353-
} else {
354-
codegen_fn_attrs.inline
355-
};
353+
// `optnone` requires `noinline`
354+
let inline = if codegen_fn_attrs.optimize == Some(OptimizeAttr::None) {
355+
InlineAttr::Never
356+
} else if codegen_fn_attrs.inline == InlineAttr::None && instance.def.requires_inline(cx.tcx) {
357+
InlineAttr::Hint
358+
} else {
359+
codegen_fn_attrs.inline
360+
};
356361
to_add.extend(inline_attr(cx, inline));
357362

358363
// The `uwtable` attribute according to LLVM is:

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,8 +1024,8 @@ pub fn provide(providers: &mut Providers) {
10241024
let any_for_speed = defids.items().any(|id| {
10251025
let CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);
10261026
match optimize {
1027-
attr::OptimizeAttr::None | attr::OptimizeAttr::Size => false,
1028-
attr::OptimizeAttr::Speed => true,
1027+
None | Some(attr::OptimizeAttr::None | attr::OptimizeAttr::Size) => false,
1028+
Some(attr::OptimizeAttr::Speed) => true,
10291029
}
10301030
});
10311031

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
559559
}
560560
});
561561

562-
codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::None, |ia, attr| {
562+
codegen_fn_attrs.optimize = attrs.iter().fold(None, |ia, attr| {
563563
if !attr.has_name(sym::optimize) {
564564
return ia;
565565
}
@@ -573,14 +573,16 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
573573
inline_span = Some(attr.span);
574574
if items.len() != 1 {
575575
err(attr.span, "expected one argument");
576-
OptimizeAttr::None
576+
None
577577
} else if list_contains_name(items, sym::size) {
578-
OptimizeAttr::Size
578+
Some(OptimizeAttr::Size)
579579
} else if list_contains_name(items, sym::speed) {
580-
OptimizeAttr::Speed
580+
Some(OptimizeAttr::Speed)
581+
} else if list_contains_name(items, sym::none) {
582+
Some(OptimizeAttr::None)
581583
} else {
582584
err(items[0].span(), "invalid argument");
583-
OptimizeAttr::None
585+
None
584586
}
585587
}
586588
Some(MetaItemKind::NameValue(_)) => ia,

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
532532
),
533533
// RFC 2412
534534
gated!(
535-
optimize, Normal, template!(List: "size|speed"), ErrorPreceding,
535+
optimize, Normal, template!(List: "none|size|speed"), ErrorPreceding,
536536
EncodeCrossCrate::No, optimize_attribute, experimental!(optimize)
537537
),
538538

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub struct CodegenFnAttrs {
1111
pub flags: CodegenFnAttrFlags,
1212
/// Parsed representation of the `#[inline]` attribute
1313
pub inline: InlineAttr,
14-
/// Parsed representation of the `#[optimize]` attribute
15-
pub optimize: OptimizeAttr,
14+
/// Parsed representation of the `#[optimize]` attribute if present
15+
pub optimize: Option<OptimizeAttr>,
1616
/// The `#[export_name = "..."]` attribute, indicating a custom symbol a
1717
/// function should be exported under
1818
pub export_name: Option<Symbol>,
@@ -146,7 +146,7 @@ impl CodegenFnAttrs {
146146
CodegenFnAttrs {
147147
flags: CodegenFnAttrFlags::empty(),
148148
inline: InlineAttr::None,
149-
optimize: OptimizeAttr::None,
149+
optimize: None,
150150
export_name: None,
151151
link_name: None,
152152
link_ordinal: None,

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ pub trait MirPass<'tcx> {
153153
to_profiler_name(self.name())
154154
}
155155

156+
fn min_mir_opt_level(&self) -> usize {
157+
0
158+
}
159+
156160
/// Returns `true` if this pass is enabled with the current combination of compiler flags.
157161
fn is_enabled(&self, _sess: &Session) -> bool {
158162
true
@@ -443,6 +447,8 @@ pub struct Body<'tcx> {
443447
/// If `-Cinstrument-coverage` is not active, or if an individual function
444448
/// is not eligible for coverage, then this should always be `None`.
445449
pub function_coverage_info: Option<Box<coverage::FunctionCoverageInfo>>,
450+
// /// Whether optimization is disabled by `#[optimize(none)]`
451+
// pub optimization_disabled: bool,
446452
}
447453

448454
impl<'tcx> Body<'tcx> {

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ use crate::ssa::SsaLocals;
1919
pub struct CopyProp;
2020

2121
impl<'tcx> MirPass<'tcx> for CopyProp {
22+
fn min_mir_opt_level(&self) -> usize {
23+
1
24+
}
25+
2226
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
2327
sess.mir_opt_level() >= 1
2428
}

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const PLACE_LIMIT: usize = 100;
2828
pub struct DataflowConstProp;
2929

3030
impl<'tcx> MirPass<'tcx> for DataflowConstProp {
31+
fn min_mir_opt_level(&self) -> usize {
32+
3
33+
}
34+
3135
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
3236
sess.mir_opt_level() >= 3
3337
}

compiler/rustc_mir_transform/src/dead_store_elimination.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ impl<'tcx> MirPass<'tcx> for DeadStoreElimination {
140140
}
141141
}
142142

143+
fn min_mir_opt_level(&self) -> usize {
144+
2
145+
}
146+
143147
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
144148
sess.mir_opt_level() >= 2
145149
}

0 commit comments

Comments
 (0)