Skip to content

Commit 42ac3f0

Browse files
committed
Implement optimize(none)
1 parent ab1527f commit 42ac3f0

File tree

7 files changed

+36
-15
lines changed

7 files changed

+36
-15
lines changed

compiler/rustc_attr/src/builtin.rs

+3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ pub enum InstructionSetAttr {
5555

5656
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
5757
pub enum OptimizeAttr {
58+
/// `#[optimize(none)]`
5859
None,
60+
/// `#[optimize(speed)]`
5961
Speed,
62+
/// `#[optimize(size)]`
6063
Size,
6164
}
6265

compiler/rustc_codegen_llvm/src/attributes.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,17 @@ pub fn from_fn_attrs<'ll, 'tcx>(
336336
let mut to_add = SmallVec::<[_; 16]>::new();
337337

338338
match codegen_fn_attrs.optimize {
339-
OptimizeAttr::None => {
339+
None => {
340340
to_add.extend(default_optimisation_attrs(cx));
341341
}
342-
OptimizeAttr::Size => {
342+
Some(OptimizeAttr::None) => {
343+
to_add.push(llvm::AttributeKind::OptimizeNone.create_attr(cx.llcx));
344+
}
345+
Some(OptimizeAttr::Size) => {
343346
to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
344347
to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
345348
}
346-
OptimizeAttr::Speed => {}
349+
Some(OptimizeAttr::Speed) => {}
347350
}
348351

349352
let inline =

compiler/rustc_codegen_ssa/src/base.rs

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

+7-5
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_middle/src/middle/codegen_fn_attrs.rs

+3-3
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>,
@@ -137,7 +137,7 @@ impl CodegenFnAttrs {
137137
CodegenFnAttrs {
138138
flags: CodegenFnAttrFlags::empty(),
139139
inline: InlineAttr::None,
140-
optimize: OptimizeAttr::None,
140+
optimize: None,
141141
export_name: None,
142142
link_name: None,
143143
link_ordinal: None,

tests/ui/feature-gates/feature-gate-optimize_attribute.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ fn size() {}
1010
#[optimize(speed)] //~ ERROR the `#[optimize]` attribute is an experimental feature
1111
fn speed() {}
1212

13+
#[optimize(none)] //~ ERROR the `#[optimize]` attribute is an experimental feature
14+
fn none() {}
15+
1316
#[optimize(banana)]
1417
//~^ ERROR the `#[optimize]` attribute is an experimental feature
1518
//~| ERROR E0722

tests/ui/feature-gates/feature-gate-optimize_attribute.stderr

+12-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ LL | #[optimize(speed)]
4141
error[E0658]: the `#[optimize]` attribute is an experimental feature
4242
--> $DIR/feature-gate-optimize_attribute.rs:13:1
4343
|
44+
LL | #[optimize(none)]
45+
| ^^^^^^^^^^^^^^^^^
46+
|
47+
= note: see issue #54882 <https://github.com/rust-lang/rust/issues/54882> for more information
48+
= help: add `#![feature(optimize_attribute)]` to the crate attributes to enable
49+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
50+
51+
error[E0658]: the `#[optimize]` attribute is an experimental feature
52+
--> $DIR/feature-gate-optimize_attribute.rs:16:1
53+
|
4454
LL | #[optimize(banana)]
4555
| ^^^^^^^^^^^^^^^^^^^
4656
|
@@ -49,12 +59,12 @@ LL | #[optimize(banana)]
4959
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
5060

5161
error[E0722]: invalid argument
52-
--> $DIR/feature-gate-optimize_attribute.rs:13:12
62+
--> $DIR/feature-gate-optimize_attribute.rs:16:12
5363
|
5464
LL | #[optimize(banana)]
5565
| ^^^^^^
5666

57-
error: aborting due to 6 previous errors
67+
error: aborting due to 7 previous errors
5868

5969
Some errors have detailed explanations: E0658, E0722.
6070
For more information about an error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)