Skip to content

Commit

Permalink
Rollup merge of rust-lang#79192 - tmiasko:naked-noinline, r=oli-obk
Browse files Browse the repository at this point in the history
Never inline naked functions

The `#[naked]` attribute disabled prologue / epilogue emission for the
function and it is responsibility of a developer to provide them. The
compiler is no position to inline such functions correctly.

Disable inlining of naked functions at LLVM and MIR level.

Closes rust-lang#60919.
  • Loading branch information
Dylan-DPC authored Nov 19, 2020
2 parents cf82316 + 8afa22d commit b152b83
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
}
}

#[derive(Clone, PartialEq, Encodable, Decodable)]
#[derive(Copy, Clone, PartialEq, Encodable, Decodable)]
pub enum InlineAttr {
None,
Hint,
Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::value::Value;

/// Mark LLVM function to use provided inline heuristic.
#[inline]
fn inline(cx: &CodegenCx<'ll, '_>, val: &'ll Value, inline: InlineAttr, requires_inline: bool) {
fn inline(cx: &CodegenCx<'ll, '_>, val: &'ll Value, inline: InlineAttr) {
use self::InlineAttr::*;
match inline {
Hint => Attribute::InlineHint.apply_llfn(Function, val),
Expand All @@ -35,7 +35,6 @@ fn inline(cx: &CodegenCx<'ll, '_>, val: &'ll Value, inline: InlineAttr, requires
Attribute::NoInline.apply_llfn(Function, val);
}
}
None if requires_inline => Attribute::InlineHint.apply_llfn(Function, val),
None => {}
};
}
Expand Down Expand Up @@ -226,7 +225,14 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
}
}

inline(cx, llfn, codegen_fn_attrs.inline.clone(), instance.def.requires_inline(cx.tcx));
let inline_attr = if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
InlineAttr::Never
} else if codegen_fn_attrs.inline == InlineAttr::None && instance.def.requires_inline(cx.tcx) {
InlineAttr::Hint
} else {
codegen_fn_attrs.inline
};
inline(cx, llfn, inline_attr);

// The `uwtable` attribute according to LLVM is:
//
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_mir/src/transform/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ impl Inliner<'tcx> {
self.tcx.sess.opts.debugging_opts.inline_mir_threshold
};

if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
debug!("#[naked] present - not inlining");
return false;
}

if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
debug!("#[cold] present - not inlining");
return false;
Expand Down
29 changes: 29 additions & 0 deletions src/test/codegen/naked-inline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Checks that naked functions are never inlined.
// compile-flags: -O -Zmir-opt-level=2
#![crate_type = "lib"]
#![feature(asm)]
#![feature(naked_functions)]

#[inline(always)]
#[naked]
#[no_mangle]
pub unsafe fn f() {
// Check that f has naked and noinline attributes.
//
// CHECK: define void @f() unnamed_addr [[ATTR:#[0-9]+]]
// CHECK-NEXT: start:
// CHECK-NEXT: call void asm
asm!("", options(noreturn));
}

#[no_mangle]
pub unsafe fn g() {
// Check that call to f is not inlined.
//
// CHECK-LABEL: define void @g()
// CHECK-NEXT: start:
// CHECK-NEXT: call void @f()
f();
}

// CHECK: attributes [[ATTR]] = { naked noinline{{.*}} }

0 comments on commit b152b83

Please sign in to comment.