Skip to content

Commit 94815e5

Browse files
committed
Implement optimize(none)
1 parent ab1527f commit 94815e5

File tree

9 files changed

+54
-19
lines changed

9 files changed

+54
-19
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_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

+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/codegen/optimize-attr-1.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ revisions: NO-OPT SIZE-OPT SPEED-OPT
2-
//@[NO-OPT] compile-flags: -Copt-level=0 -Ccodegen-units=1
3-
//@[SIZE-OPT] compile-flags: -Copt-level=s -Ccodegen-units=1
4-
//@[SPEED-OPT] compile-flags: -Copt-level=3 -Ccodegen-units=1
2+
//@[NO-OPT] compile-flags: -Copt-level=0 -Zmir-opt-level=0 -Ccodegen-units=1
3+
//@[SIZE-OPT] compile-flags: -Copt-level=s -Zmir-opt-level=0 -Ccodegen-units=1
4+
//@[SPEED-OPT] compile-flags: -Copt-level=3 -Zmir-opt-level=0 -Ccodegen-units=1
55

66
#![feature(optimize_attribute)]
77
#![crate_type = "rlib"]
@@ -15,6 +15,18 @@ pub fn nothing() -> i32 {
1515
2 + 2
1616
}
1717

18+
// CHECK-LABEL: define{{.*}}i32 @none
19+
// CHECK-SAME: [[NONE_ATTRS:#[0-9]+]]
20+
// SIZE-OPT: call noundef i32 @callme
21+
// SPEED-OPT: call noundef i32 @callme
22+
#[no_mangle]
23+
#[optimize(none)]
24+
pub fn none() -> i32 {
25+
#[no_mangle]
26+
fn callme() -> i32 { 32 }
27+
callme()
28+
}
29+
1830
// CHECK-LABEL: define{{.*}}i32 @size
1931
// CHECK-SAME: [[SIZE_ATTRS:#[0-9]+]]
2032
// SIZE-OPT: ret i32 6
@@ -39,8 +51,10 @@ pub fn speed() -> i32 {
3951

4052
// NO-OPT-DAG: attributes [[SIZE_ATTRS]] = {{.*}}minsize{{.*}}optsize{{.*}}
4153
// SPEED-OPT-DAG: attributes [[SIZE_ATTRS]] = {{.*}}minsize{{.*}}optsize{{.*}}
54+
// SPEED-OPT-DAG: attributes [[NONE_ATTRS]] = {{.*}}optnone{{.*}}
4255
// SIZE-OPT-DAG: attributes [[NOTHING_ATTRS]] = {{.*}}optsize{{.*}}
4356
// SIZE-OPT-DAG: attributes [[SIZE_ATTRS]] = {{.*}}minsize{{.*}}optsize{{.*}}
57+
// SIZE-OPT-DAG: attributes [[NONE_ATTRS]] = {{.*}}optnone{{.*}}
4458

4559
// SIZE-OPT: attributes [[SPEED_ATTRS]]
4660
// SIZE-OPT-NOT: minsize

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)