Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_abi::{Align, ExternAbi};
use rustc_hir::attrs::{
AttributeKind, EiiImplResolution, InlineAttr, InstrumentFnAttr as HirInstrumentFnAttr, Linkage,
RtsanSetting, UsedBy,
OptimizeAttr, RtsanSetting, UsedBy,
};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
Expand Down Expand Up @@ -354,6 +354,17 @@ fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut Code
}
}

// Closures inherit `#[optimize]` annotations.
if tcx.is_closure_like(did.to_def_id()) {
let owner_id = tcx.parent(did.to_def_id());
if tcx.def_kind(owner_id).has_codegen_attrs() {
let owner_attrs = tcx.codegen_fn_attrs(owner_id);
if codegen_fn_attrs.optimize == OptimizeAttr::Default {
codegen_fn_attrs.optimize = owner_attrs.optimize;
}
}
}

// When `no_builtins` is applied at the crate level, we should add the
// `no-builtins` attribute to each function to ensure it takes effect in LTO.
let no_builtins = find_attr!(tcx, crate, NoBuiltins);
Expand Down
33 changes: 33 additions & 0 deletions tests/codegen-llvm/optimize-closures-inheritance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Ensure that `#[optimize]` applied to an outer function is inherited by closures
//! and their coercion shims.

//@ compile-flags: -Copt-level=3

#![feature(optimize_attribute)]
#![crate_type = "lib"]

// CHECK-DAG: define{{.*}}void {{.*}}test_none{{.*}}call_once{{.*}}#[[ATTR_NONE:[0-9]+]]
// CHECK-DAG: define{{.*}}void {{.*}}test_none{{.*}}B{{[0-9]+}}_() {{.*}}#[[ATTR_NONE]]
// CHECK-DAG: define{{.*}}void {{.*}}test_size{{.*}}call_once{{.*}}#[[ATTR_SIZE:[0-9]+]]

// CHECK-DAG: attributes #[[ATTR_NONE]] = { {{.*}}noinline{{.*}}optnone{{.*}} }
// CHECK-DAG: attributes #[[ATTR_SIZE]] = { {{.*}}optsize{{.*}} }

extern "C" {
fn side_effect_1();
fn side_effect_2();
}

#[optimize(none)]
#[no_mangle]
pub fn test_none() -> fn() {
let closure = || unsafe { side_effect_1() };
closure
}

#[optimize(size)]
#[no_mangle]
pub fn test_size() -> fn() {
let closure = || unsafe { side_effect_2() };
closure
}
Loading