Skip to content

Commit 6365178

Browse files
committed
Auto merge of #128657 - clubby789:optimize-none, r=fee1-dead,WaffleLapkin
Add `#[optimize(none)]` cc #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 1fc3491 + 5ac95a5 commit 6365178

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+374
-32
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,25 @@ pub enum InstructionSetAttr {
3535
ArmT32,
3636
}
3737

38-
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
38+
#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq, HashStable_Generic, Default)]
3939
pub enum OptimizeAttr {
40-
None,
40+
/// No `#[optimize(..)]` attribute
41+
#[default]
42+
Default,
43+
/// `#[optimize(none)]`
44+
DoNotOptimize,
45+
/// `#[optimize(speed)]`
4146
Speed,
47+
/// `#[optimize(size)]`
4248
Size,
4349
}
4450

51+
impl OptimizeAttr {
52+
pub fn do_not_optimize(&self) -> bool {
53+
matches!(self, Self::DoNotOptimize)
54+
}
55+
}
56+
4557
#[derive(Clone, Debug, Encodable, Decodable)]
4658
pub enum DiagnosticAttribute {
4759
// tidy-alphabetical-start

compiler/rustc_codegen_llvm/src/attributes.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -333,22 +333,25 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
333333
let mut to_add = SmallVec::<[_; 16]>::new();
334334

335335
match codegen_fn_attrs.optimize {
336-
OptimizeAttr::None => {
336+
OptimizeAttr::Default => {
337337
to_add.extend(default_optimisation_attrs(cx));
338338
}
339+
OptimizeAttr::DoNotOptimize => {
340+
to_add.push(llvm::AttributeKind::OptimizeNone.create_attr(cx.llcx));
341+
}
339342
OptimizeAttr::Size => {
340343
to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
341344
to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
342345
}
343346
OptimizeAttr::Speed => {}
344347
}
345348

346-
let inline =
347-
if codegen_fn_attrs.inline == InlineAttr::None && instance.def.requires_inline(cx.tcx) {
348-
InlineAttr::Hint
349-
} else {
350-
codegen_fn_attrs.inline
351-
};
349+
// `optnone` requires `noinline`
350+
let inline = match (codegen_fn_attrs.inline, &codegen_fn_attrs.optimize) {
351+
(_, OptimizeAttr::DoNotOptimize) => InlineAttr::Never,
352+
(InlineAttr::None, _) if instance.def.requires_inline(cx.tcx) => InlineAttr::Hint,
353+
(inline, _) => inline,
354+
};
352355
to_add.extend(inline_attr(cx, inline));
353356

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

compiler/rustc_codegen_ssa/src/base.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1053,10 +1053,7 @@ pub(crate) fn provide(providers: &mut Providers) {
10531053

10541054
let any_for_speed = defids.items().any(|id| {
10551055
let CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);
1056-
match optimize {
1057-
attr::OptimizeAttr::None | attr::OptimizeAttr::Size => false,
1058-
attr::OptimizeAttr::Speed => true,
1059-
}
1056+
matches!(optimize, attr::OptimizeAttr::Speed)
10601057
});
10611058

10621059
if any_for_speed {

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
575575
codegen_fn_attrs.inline = InlineAttr::Never;
576576
}
577577

578-
codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::None, |ia, attr| {
578+
codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::Default, |ia, attr| {
579579
if !attr.has_name(sym::optimize) {
580580
return ia;
581581
}
@@ -587,17 +587,19 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
587587
inline_span = Some(attr.span);
588588
if items.len() != 1 {
589589
err(attr.span, "expected one argument");
590-
OptimizeAttr::None
590+
OptimizeAttr::Default
591591
} else if list_contains_name(items, sym::size) {
592592
OptimizeAttr::Size
593593
} else if list_contains_name(items, sym::speed) {
594594
OptimizeAttr::Speed
595+
} else if list_contains_name(items, sym::none) {
596+
OptimizeAttr::DoNotOptimize
595597
} else {
596598
err(items[0].span(), "invalid argument");
597-
OptimizeAttr::None
599+
OptimizeAttr::Default
598600
}
599601
} else {
600-
OptimizeAttr::None
602+
OptimizeAttr::Default
601603
}
602604
});
603605

compiler/rustc_feature/src/builtin_attrs.rs

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl CodegenFnAttrs {
147147
CodegenFnAttrs {
148148
flags: CodegenFnAttrFlags::empty(),
149149
inline: InlineAttr::None,
150-
optimize: OptimizeAttr::None,
150+
optimize: OptimizeAttr::Default,
151151
export_name: None,
152152
link_name: None,
153153
link_ordinal: None,

compiler/rustc_mir_transform/src/abort_unwinding_calls.rs

+4
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,8 @@ impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
116116
// We may have invalidated some `cleanup` blocks so clean those up now.
117117
super::simplify::remove_dead_blocks(body);
118118
}
119+
120+
fn is_required(&self) -> bool {
121+
true
122+
}
119123
}

compiler/rustc_mir_transform/src/add_call_guards.rs

+4
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,8 @@ impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {
7575

7676
body.basic_blocks_mut().extend(new_blocks);
7777
}
78+
79+
fn is_required(&self) -> bool {
80+
true
81+
}
7882
}

compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ impl<'tcx> crate::MirPass<'tcx> for AddMovesForPackedDrops {
6868

6969
patch.apply(body);
7070
}
71+
72+
fn is_required(&self) -> bool {
73+
true
74+
}
7175
}
7276

7377
fn add_move_for_packed_drop<'tcx>(

compiler/rustc_mir_transform/src/add_retag.rs

+4
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,8 @@ impl<'tcx> crate::MirPass<'tcx> for AddRetag {
176176
}
177177
}
178178
}
179+
180+
fn is_required(&self) -> bool {
181+
true
182+
}
179183
}

compiler/rustc_mir_transform/src/add_subtyping_projections.rs

+4
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,8 @@ impl<'tcx> crate::MirPass<'tcx> for Subtyper {
6161
}
6262
checker.patcher.apply(body);
6363
}
64+
65+
fn is_required(&self) -> bool {
66+
true
67+
}
6468
}

compiler/rustc_mir_transform/src/check_alignment.rs

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
6060
}
6161
}
6262
}
63+
64+
fn is_required(&self) -> bool {
65+
true
66+
}
6367
}
6468

6569
struct PointerFinder<'a, 'tcx> {

compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs

+4
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,8 @@ impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
7272
decl.user_ty = None;
7373
}
7474
}
75+
76+
fn is_required(&self) -> bool {
77+
true
78+
}
7579
}

compiler/rustc_mir_transform/src/copy_prop.rs

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
5656
crate::simplify::remove_unused_definitions(body);
5757
}
5858
}
59+
60+
fn is_required(&self) -> bool {
61+
false
62+
}
5963
}
6064

6165
/// `SsaLocals` computed equivalence classes between locals considering copy/move assignments.

compiler/rustc_mir_transform/src/coroutine.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,10 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
16881688
// Run derefer to fix Derefs that are not in the first place
16891689
deref_finder(tcx, body);
16901690
}
1691+
1692+
fn is_required(&self) -> bool {
1693+
true
1694+
}
16911695
}
16921696

16931697
/// Looks for any assignments between locals (e.g., `_4 = _5`) that will both be converted to fields

compiler/rustc_mir_transform/src/coverage/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ impl<'tcx> crate::MirPass<'tcx> for InstrumentCoverage {
6161

6262
instrument_function_for_coverage(tcx, mir_body);
6363
}
64+
65+
fn is_required(&self) -> bool {
66+
false
67+
}
6468
}
6569

6670
fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {

compiler/rustc_mir_transform/src/cross_crate_inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
6969
// Don't do any inference if codegen optimizations are disabled and also MIR inlining is not
7070
// enabled. This ensures that we do inference even if someone only passes -Zinline-mir,
7171
// which is less confusing than having to also enable -Copt-level=1.
72-
let inliner_will_run = pm::should_run_pass(tcx, &inline::Inline)
72+
let inliner_will_run = pm::should_run_pass(tcx, &inline::Inline, pm::Optimizations::Allowed)
7373
|| inline::ForceInline::should_run_pass_for_callee(tcx, def_id.to_def_id());
7474
if matches!(tcx.sess.opts.optimize, OptLevel::No) && !inliner_will_run {
7575
return false;

compiler/rustc_mir_transform/src/ctfe_limit.rs

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ impl<'tcx> crate::MirPass<'tcx> for CtfeLimit {
3636
);
3737
}
3838
}
39+
40+
fn is_required(&self) -> bool {
41+
true
42+
}
3943
}
4044

4145
fn has_back_edge(

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ impl<'tcx> crate::MirPass<'tcx> for DataflowConstProp {
7171
let mut patch = visitor.patch;
7272
debug_span!("patch").in_scope(|| patch.visit_body_preserves_cfg(body));
7373
}
74+
75+
fn is_required(&self) -> bool {
76+
false
77+
}
7478
}
7579

7680
// Note: Currently, places that have their reference taken cannot be tracked. Although this would

compiler/rustc_mir_transform/src/dead_store_elimination.rs

+4
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,8 @@ impl<'tcx> crate::MirPass<'tcx> for DeadStoreElimination {
147147
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
148148
eliminate(tcx, body);
149149
}
150+
151+
fn is_required(&self) -> bool {
152+
false
153+
}
150154
}

compiler/rustc_mir_transform/src/deduplicate_blocks.rs

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ impl<'tcx> crate::MirPass<'tcx> for DeduplicateBlocks {
3131
simplify_cfg(body);
3232
}
3333
}
34+
35+
fn is_required(&self) -> bool {
36+
false
37+
}
3438
}
3539

3640
struct OptApplier<'tcx> {

compiler/rustc_mir_transform/src/deref_separator.rs

+4
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@ impl<'tcx> crate::MirPass<'tcx> for Derefer {
8181
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
8282
deref_finder(tcx, body);
8383
}
84+
85+
fn is_required(&self) -> bool {
86+
true
87+
}
8488
}

compiler/rustc_mir_transform/src/dest_prop.rs

+4
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {
240240

241241
trace!(round_count);
242242
}
243+
244+
fn is_required(&self) -> bool {
245+
false
246+
}
243247
}
244248

245249
#[derive(Debug, Default)]

compiler/rustc_mir_transform/src/dump_mir.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ impl<'tcx> crate::MirPass<'tcx> for Marker {
1515
}
1616

1717
fn run_pass(&self, _tcx: TyCtxt<'tcx>, _body: &mut Body<'tcx>) {}
18+
19+
fn is_required(&self) -> bool {
20+
false
21+
}
1822
}
1923

2024
pub fn emit_mir(tcx: TyCtxt<'_>) -> io::Result<()> {

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

+4
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
227227
simplify_cfg(body);
228228
}
229229
}
230+
231+
fn is_required(&self) -> bool {
232+
false
233+
}
230234
}
231235

232236
#[derive(Debug)]

compiler/rustc_mir_transform/src/elaborate_box_derefs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,8 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateBoxDerefs {
150150
}
151151
}
152152
}
153+
154+
fn is_required(&self) -> bool {
155+
true
156+
}
153157
}

compiler/rustc_mir_transform/src/elaborate_drops.rs

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateDrops {
8888
elaborate_patch.apply(body);
8989
deref_finder(tcx, body);
9090
}
91+
92+
fn is_required(&self) -> bool {
93+
true
94+
}
9195
}
9296

9397
/// Records unwind edges which are known to be unreachable, because they are in `drop` terminators

compiler/rustc_mir_transform/src/gvn.rs

+4
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
163163
// statements.
164164
StorageRemover { tcx, reused_locals: state.reused_locals }.visit_body_preserves_cfg(body);
165165
}
166+
167+
fn is_required(&self) -> bool {
168+
false
169+
}
166170
}
167171

168172
newtype_index! {

compiler/rustc_mir_transform/src/impossible_predicates.rs

+4
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@ impl<'tcx> MirPass<'tcx> for ImpossiblePredicates {
5353
body.local_decls.raw.truncate(body.arg_count + 1);
5454
}
5555
}
56+
57+
fn is_required(&self) -> bool {
58+
true
59+
}
5660
}

compiler/rustc_mir_transform/src/inline.rs

+8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ impl<'tcx> crate::MirPass<'tcx> for Inline {
6666
deref_finder(tcx, body);
6767
}
6868
}
69+
70+
fn is_required(&self) -> bool {
71+
false
72+
}
6973
}
7074

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

92+
fn is_required(&self) -> bool {
93+
true
94+
}
95+
8896
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
8997
let span = trace_span!("force_inline", body = %tcx.def_path_str(body.source.def_id()));
9098
let _guard = span.enter();

compiler/rustc_mir_transform/src/instsimplify.rs

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
6262
simplify_duplicate_switch_targets(block.terminator.as_mut().unwrap());
6363
}
6464
}
65+
66+
fn is_required(&self) -> bool {
67+
false
68+
}
6569
}
6670

6771
struct InstSimplifyContext<'a, 'tcx> {

compiler/rustc_mir_transform/src/jump_threading.rs

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ impl<'tcx> crate::MirPass<'tcx> for JumpThreading {
105105
}
106106
OpportunitySet::new(body, opportunities).apply(body);
107107
}
108+
109+
fn is_required(&self) -> bool {
110+
false
111+
}
108112
}
109113

110114
#[derive(Debug)]

0 commit comments

Comments
 (0)