Skip to content

Commit b739645

Browse files
committed
Implement optimize(none) attribute
1 parent cd7abb4 commit b739645

File tree

8 files changed

+43
-11
lines changed

8 files changed

+43
-11
lines changed

compiler/rustc_attr/src/builtin.rs

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub enum InstructionSetAttr {
5757

5858
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
5959
pub enum OptimizeAttr {
60+
/// `#[optimize(none)]` - NOT the absence of an `#[optimize(..)]` attribute
61+
None,
6062
Speed,
6163
Size,
6264
}

compiler/rustc_codegen_llvm/src/attributes.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -334,19 +334,22 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
334334
None => {
335335
to_add.extend(default_optimisation_attrs(cx));
336336
}
337+
Some(OptimizeAttr::None) => {
338+
to_add.push(llvm::AttributeKind::OptimizeNone.create_attr(cx.llcx));
339+
}
337340
Some(OptimizeAttr::Size) => {
338341
to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
339342
to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
340343
}
341344
Some(OptimizeAttr::Speed) => {}
342345
}
343346

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

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

compiler/rustc_codegen_ssa/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ pub(crate) fn provide(providers: &mut Providers) {
10571057
let CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);
10581058
match optimize {
10591059
None => false,
1060-
Some(attr::OptimizeAttr::Size) => false,
1060+
Some(attr::OptimizeAttr::None | attr::OptimizeAttr::Size) => false,
10611061
Some(attr::OptimizeAttr::Speed) => true,
10621062
}
10631063
});

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
561561
Some(OptimizeAttr::Size)
562562
} else if list_contains_name(items, sym::speed) {
563563
Some(OptimizeAttr::Speed)
564+
} else if list_contains_name(items, sym::none) {
565+
Some(OptimizeAttr::None)
564566
} else {
565567
err(items[0].span(), "invalid argument");
566568
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)