Skip to content

Commit cd848c9

Browse files
committed
Implement optimize(none) attribute
1 parent dee7d0e commit cd848c9

File tree

8 files changed

+45
-14
lines changed

8 files changed

+45
-14
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

+4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ pub enum InstructionSetAttr {
3838
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
3939
pub enum OptimizeAttr {
4040
None,
41+
/// `#[optimize(none)]`
42+
DoNotOptimize,
43+
/// `#[optimize(speed)]`
4144
Speed,
45+
/// `#[optimize(size)]`
4246
Size,
4347
}
4448

compiler/rustc_codegen_llvm/src/attributes.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -336,19 +336,22 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
336336
OptimizeAttr::None => {
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

+2
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
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");
597599
OptimizeAttr::None

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

tests/codegen/optimize-attr-1.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,23 @@ pub fn speed() -> i32 {
3737
4 + 4
3838
}
3939

40+
// CHECK-LABEL: define{{.*}}i32 @none
41+
// CHECK-SAME: [[NONE_ATTRS:#[0-9]+]]
42+
// SIZE-OPT: alloca
43+
// SPEED-OPT: alloca
44+
#[no_mangle]
45+
#[optimize(none)]
46+
pub fn none() -> i32 {
47+
let arr = [0, 1, 2, 3, 4];
48+
arr[4]
49+
}
50+
4051
// NO-OPT-DAG: attributes [[SIZE_ATTRS]] = {{.*}}minsize{{.*}}optsize{{.*}}
4152
// SPEED-OPT-DAG: attributes [[SIZE_ATTRS]] = {{.*}}minsize{{.*}}optsize{{.*}}
4253
// SIZE-OPT-DAG: attributes [[NOTHING_ATTRS]] = {{.*}}optsize{{.*}}
4354
// SIZE-OPT-DAG: attributes [[SIZE_ATTRS]] = {{.*}}minsize{{.*}}optsize{{.*}}
55+
// CHECK-DAG: attributes [[NONE_ATTRS]] = {{.*}}noinline{{.*}}optnone{{.*}}
4456

45-
// SIZE-OPT: attributes [[SPEED_ATTRS]]
57+
// SIZE-OPT-DAG: attributes [[SPEED_ATTRS]]
4658
// SIZE-OPT-NOT: minsize
4759
// SIZE-OPT-NOT: optsize

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

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

9+
#[optimize(none)] //~ ERROR the `#[optimize]` attribute is an experimental feature
10+
fn none() {}
11+
912
#[optimize(banana)]
1013
//~^ ERROR the `#[optimize]` attribute is an experimental feature
1114
//~| ERROR E0722

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ LL | #[optimize(speed)]
2121
error[E0658]: the `#[optimize]` attribute is an experimental feature
2222
--> $DIR/feature-gate-optimize_attribute.rs:9:1
2323
|
24+
LL | #[optimize(none)]
25+
| ^^^^^^^^^^^^^^^^^
26+
|
27+
= note: see issue #54882 <https://github.com/rust-lang/rust/issues/54882> for more information
28+
= help: add `#![feature(optimize_attribute)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
31+
error[E0658]: the `#[optimize]` attribute is an experimental feature
32+
--> $DIR/feature-gate-optimize_attribute.rs:12:1
33+
|
2434
LL | #[optimize(banana)]
2535
| ^^^^^^^^^^^^^^^^^^^
2636
|
@@ -29,12 +39,12 @@ LL | #[optimize(banana)]
2939
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3040

3141
error[E0722]: invalid argument
32-
--> $DIR/feature-gate-optimize_attribute.rs:9:12
42+
--> $DIR/feature-gate-optimize_attribute.rs:12:12
3343
|
3444
LL | #[optimize(banana)]
3545
| ^^^^^^
3646

37-
error: aborting due to 4 previous errors
47+
error: aborting due to 5 previous errors
3848

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

0 commit comments

Comments
 (0)