Skip to content

Commit

Permalink
do not add prolog for variadic naked functions
Browse files Browse the repository at this point in the history
fixes #99858
  • Loading branch information
goolmoos committed Apr 12, 2024
1 parent 8df7e72 commit 9139d72
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
7 changes: 7 additions & 0 deletions compiler/rustc_codegen_ssa/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::base;
use crate::traits::*;
use rustc_index::bit_set::BitSet;
use rustc_index::IndexVec;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mir;
use rustc_middle::mir::traversal;
use rustc_middle::mir::UnwindTerminateReason;
Expand Down Expand Up @@ -290,6 +291,12 @@ fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(

let mut num_untupled = None;

let codegen_fn_attrs = bx.tcx().codegen_fn_attrs(fx.instance.def_id());
let naked = codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED);
if naked {
return vec![];
}

let args = mir
.args_iter()
.enumerate()
Expand Down
19 changes: 19 additions & 0 deletions tests/codegen/cffi/c-variadic-naked.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@ needs-asm-support
//@ only-x86_64

// tests that `va_start` is not injected into naked functions

#![crate_type = "lib"]
#![feature(c_variadic)]
#![feature(naked_functions)]
#![no_std]

#[naked]
pub unsafe extern "C" fn c_variadic(_: usize, _: ...) {
// CHECK-NOT: va_start
// CHECK-NOT: alloca
core::arch::asm! {
"ret",
options(noreturn),
}
}
2 changes: 1 addition & 1 deletion tests/codegen/naked-fn/naked-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub unsafe extern "C" fn naked_empty() {
}

// CHECK: Function Attrs: naked
// CHECK-NEXT: define{{.*}}i{{[0-9]+}} @naked_with_args_and_return(i64 %a, i64 %b)
// CHECK-NEXT: define{{.*}}i{{[0-9]+}} @naked_with_args_and_return(i64 %0, i64 %1)
#[no_mangle]
#[naked]
pub unsafe extern "C" fn naked_with_args_and_return(a: isize, b: isize) -> isize {
Expand Down

0 comments on commit 9139d72

Please sign in to comment.