add codegen_instance_attrs query
and use it for naked functions
This commit is contained in:
@@ -530,12 +530,8 @@ fn codegen_cgu_content(
|
|||||||
for (mono_item, item_data) in mono_items {
|
for (mono_item, item_data) in mono_items {
|
||||||
match mono_item {
|
match mono_item {
|
||||||
MonoItem::Fn(instance) => {
|
MonoItem::Fn(instance) => {
|
||||||
// Other `InstanceKind`s (e.g. `ReifyShim` generated by indirect calls) should be
|
let flags = tcx.codegen_instance_attrs(instance.def).flags;
|
||||||
// codegened like a normal function.
|
if flags.contains(CodegenFnAttrFlags::NAKED) {
|
||||||
let is_item_instance = matches!(instance.def, InstanceKind::Item(_));
|
|
||||||
|
|
||||||
let flags = tcx.codegen_fn_attrs(instance.def_id()).flags;
|
|
||||||
if is_item_instance && flags.contains(CodegenFnAttrFlags::NAKED) {
|
|
||||||
rustc_codegen_ssa::mir::naked_asm::codegen_naked_asm(
|
rustc_codegen_ssa::mir::naked_asm::codegen_naked_asm(
|
||||||
&mut GlobalAsmContext { tcx, global_asm: &mut cx.global_asm },
|
&mut GlobalAsmContext { tcx, global_asm: &mut cx.global_asm },
|
||||||
instance,
|
instance,
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
|||||||
codegen_fn_attrs.export_name = Some(*name);
|
codegen_fn_attrs.export_name = Some(*name);
|
||||||
}
|
}
|
||||||
AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
|
AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
|
||||||
|
|
||||||
AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
|
AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
|
||||||
AttributeKind::LinkName { name, .. } => codegen_fn_attrs.link_name = Some(*name),
|
AttributeKind::LinkName { name, .. } => codegen_fn_attrs.link_name = Some(*name),
|
||||||
AttributeKind::LinkSection { name, .. } => {
|
AttributeKind::LinkSection { name, .. } => {
|
||||||
|
|||||||
@@ -356,7 +356,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||||||
LocalRef::Operand(operand) => {
|
LocalRef::Operand(operand) => {
|
||||||
// Don't spill operands onto the stack in naked functions.
|
// Don't spill operands onto the stack in naked functions.
|
||||||
// See: https://github.com/rust-lang/rust/issues/42779
|
// See: https://github.com/rust-lang/rust/issues/42779
|
||||||
let attrs = bx.tcx().codegen_fn_attrs(self.instance.def_id());
|
let attrs = bx.tcx().codegen_instance_attrs(self.instance.def);
|
||||||
if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
|
if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -390,9 +390,8 @@ fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
|
|||||||
|
|
||||||
let mut num_untupled = None;
|
let mut num_untupled = None;
|
||||||
|
|
||||||
let codegen_fn_attrs = bx.tcx().codegen_fn_attrs(fx.instance.def_id());
|
let codegen_fn_attrs = bx.tcx().codegen_instance_attrs(fx.instance.def);
|
||||||
let naked = codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED);
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
|
||||||
if naked {
|
|
||||||
return vec![];
|
return vec![];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
||||||
use rustc_middle::mir::mono::{Linkage, MonoItem, MonoItemData, Visibility};
|
use rustc_middle::mir::mono::{Linkage, MonoItem, MonoItemData, Visibility};
|
||||||
use rustc_middle::ty::InstanceKind;
|
|
||||||
use rustc_middle::ty::layout::HasTyCtxt;
|
use rustc_middle::ty::layout::HasTyCtxt;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
@@ -42,12 +41,8 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
|
|||||||
base::codegen_global_asm(cx, item_id);
|
base::codegen_global_asm(cx, item_id);
|
||||||
}
|
}
|
||||||
MonoItem::Fn(instance) => {
|
MonoItem::Fn(instance) => {
|
||||||
// Other `InstanceKind`s (e.g. `ReifyShim` generated by indirect calls) should be
|
let flags = cx.tcx().codegen_instance_attrs(instance.def).flags;
|
||||||
// codegened like a normal function.
|
if flags.contains(CodegenFnAttrFlags::NAKED) {
|
||||||
let is_item_instance = matches!(instance.def, InstanceKind::Item(_));
|
|
||||||
|
|
||||||
let flags = cx.tcx().codegen_fn_attrs(instance.def_id()).flags;
|
|
||||||
if is_item_instance && flags.contains(CodegenFnAttrFlags::NAKED) {
|
|
||||||
naked_asm::codegen_naked_asm::<Bx::CodegenCx>(cx, instance, item_data);
|
naked_asm::codegen_naked_asm::<Bx::CodegenCx>(cx, instance, item_data);
|
||||||
} else {
|
} else {
|
||||||
base::codegen_instance::<Bx>(cx, instance);
|
base::codegen_instance::<Bx>(cx, instance);
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use rustc_abi::Align;
|
use rustc_abi::Align;
|
||||||
use rustc_ast::expand::autodiff_attrs::AutoDiffAttrs;
|
use rustc_ast::expand::autodiff_attrs::AutoDiffAttrs;
|
||||||
use rustc_attr_data_structures::{InlineAttr, InstructionSetAttr, OptimizeAttr};
|
use rustc_attr_data_structures::{InlineAttr, InstructionSetAttr, OptimizeAttr};
|
||||||
@@ -6,6 +8,24 @@ use rustc_span::Symbol;
|
|||||||
use rustc_target::spec::SanitizerSet;
|
use rustc_target::spec::SanitizerSet;
|
||||||
|
|
||||||
use crate::mir::mono::Linkage;
|
use crate::mir::mono::Linkage;
|
||||||
|
use crate::ty::{InstanceKind, TyCtxt};
|
||||||
|
|
||||||
|
impl<'tcx> TyCtxt<'tcx> {
|
||||||
|
pub fn codegen_instance_attrs(
|
||||||
|
self,
|
||||||
|
instance_kind: InstanceKind<'_>,
|
||||||
|
) -> Cow<'tcx, CodegenFnAttrs> {
|
||||||
|
let mut attrs = Cow::Borrowed(self.codegen_fn_attrs(instance_kind.def_id()));
|
||||||
|
|
||||||
|
// Drop the `#[naked]` attribute on non-item `InstanceKind`s, like the shims that
|
||||||
|
// are generated for indirect function calls.
|
||||||
|
if !matches!(instance_kind, InstanceKind::Item(_)) {
|
||||||
|
attrs.to_mut().flags.remove(CodegenFnAttrFlags::NAKED);
|
||||||
|
}
|
||||||
|
|
||||||
|
attrs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, TyEncodable, TyDecodable, HashStable, Debug)]
|
#[derive(Clone, TyEncodable, TyDecodable, HashStable, Debug)]
|
||||||
pub struct CodegenFnAttrs {
|
pub struct CodegenFnAttrs {
|
||||||
|
|||||||
@@ -25,6 +25,11 @@ trait MyTrait {
|
|||||||
}
|
}
|
||||||
impl MyTrait for Thing {}
|
impl MyTrait for Thing {}
|
||||||
|
|
||||||
|
// the shim calls the real function
|
||||||
|
// CHECK-LABEL: define
|
||||||
|
// CHECK-SAME: my_naked_function
|
||||||
|
// CHECK-SAME: reify.shim.fnptr
|
||||||
|
|
||||||
// CHECK-LABEL: main
|
// CHECK-LABEL: main
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
@@ -32,16 +37,11 @@ pub fn main() {
|
|||||||
const F: extern "C" fn() = Thing::my_naked_function;
|
const F: extern "C" fn() = Thing::my_naked_function;
|
||||||
|
|
||||||
// main calls the shim function
|
// main calls the shim function
|
||||||
// CHECK: call
|
// CHECK: call void
|
||||||
// CHECK-SAME: my_naked_function
|
// CHECK-SAME: my_naked_function
|
||||||
// CHECK-SAME: reify.shim.fnptr
|
// CHECK-SAME: reify.shim.fnptr
|
||||||
(F)();
|
(F)();
|
||||||
}
|
}
|
||||||
|
|
||||||
// the shim calls the real function
|
|
||||||
// CHECK: define
|
|
||||||
// CHECK-SAME: my_naked_function
|
|
||||||
// CHECK-SAME: reify.shim.fnptr
|
|
||||||
|
|
||||||
// CHECK: declare !kcfi_type
|
// CHECK: declare !kcfi_type
|
||||||
// CHECK-SAME: my_naked_function
|
// CHECK-SAME: my_naked_function
|
||||||
|
|||||||
Reference in New Issue
Block a user