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
57 changes: 57 additions & 0 deletions tests/codegen-llvm/optimize-closures-inheritance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Ensure that `#[optimize]` applied to an outer function is inherited by closures
//! and their coercion shims, unless overridden by an explicit `#[optimize]` on the closure.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I think you can do this as a -C no-prepopulate-passes test, right? Since we're the ones setting the attributes?

That would mean you'd have no need to handle all the attributes introduced today (or could be introduced in the future) in the CHECK lines, which would hopefully let it be a touch simpler.

(Might even be able to remove the side_effect functions because with no LLVM optimizations going on that shouldn't matter? Or at least only need one of them without also needing -Z merge-functions=disabled.)


#![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]]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly a silly question: what's that B from?

(I get worried about anything too specific in name checks because we've been bitten before by people doing things like %[[FOO]].0 and depending on the exact internal LLVM naming of SRoA results and such. This is probably not that bad, but I'm confused what it is, so...)

// CHECK-DAG: define{{.*}}void {{.*}}test_size{{.*}}call_once{{.*}}#[[ATTR_SIZE:[0-9]+]]
// CHECK-DAG: define{{.*}}void {{.*}}test_override_size{{.*}}call_once{{.*}}#[[ATTR_SIZE]]
// CHECK-DAG: define{{.*}}void {{.*}}test_override_none{{.*}}call_once{{.*}}#[[ATTR_NONE]]

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

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

#[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
Comment thread
veluca93 marked this conversation as resolved.
}

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

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