Skip to content

Commit

Permalink
#[optimize(none)] implies #[inline(never)]
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Jan 31, 2025
1 parent 7f36543 commit 2c35bd0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::iter;
use std::ops::{Range, RangeFrom};

use rustc_abi::{ExternAbi, FieldIdx};
use rustc_attr_parsing::InlineAttr;
use rustc_attr_parsing::{InlineAttr, OptimizeAttr};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_index::Idx;
Expand Down Expand Up @@ -770,6 +770,10 @@ fn check_codegen_attributes<'tcx, I: Inliner<'tcx>>(
return Err("never inline attribute");
}

if let OptimizeAttr::DoNotOptimize = callee_attrs.optimize {
return Err("has DoNotOptimize attribute");
}

// Reachability pass defines which functions are eligible for inlining. Generally inlining
// other functions is incorrect because they could reference symbols that aren't exported.
let is_generic = callsite.callee.args.non_erasable_generics().next().is_some();
Expand Down
21 changes: 21 additions & 0 deletions tests/codegen/issues/issue-136329-optnone-noinline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Ensure that `#[optimize(none)]` functions are never inlined
//@ compile-flags: -Copt-level=3

#![feature(optimize_attribute)]

#[optimize(none)]
pub fn foo() {
let _x = 123;
}

// CHECK-LABEL: define{{.*}}void @bar
// CHECK: start:
// CHECK: {{.*}}call {{.*}}void
// CHECK: ret void
#[no_mangle]
pub fn bar() {
foo();
}

fn main() {}

0 comments on commit 2c35bd0

Please sign in to comment.