Skip to content

Commit 0cbdf03

Browse files
committed
add codegen_instance_attrs query
and use it for naked functions
1 parent 41dfac0 commit 0cbdf03

File tree

10 files changed

+47
-27
lines changed

10 files changed

+47
-27
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,8 @@ fn codegen_cgu_content(
530530
for (mono_item, item_data) in mono_items {
531531
match mono_item {
532532
MonoItem::Fn(instance) => {
533-
// Other `InstanceKind`s (e.g. `ReifyShim` generated by indirect calls) should be
534-
// codegened like a normal function.
535-
let is_item_instance = matches!(instance.def, InstanceKind::Item(_));
536-
537-
let flags = tcx.codegen_fn_attrs(instance.def_id()).flags;
538-
if is_item_instance && flags.contains(CodegenFnAttrFlags::NAKED) {
533+
let flags = tcx.codegen_instance_attrs(instance.def).flags;
534+
if flags.contains(CodegenFnAttrFlags::NAKED) {
539535
rustc_codegen_ssa::mir::naked_asm::codegen_naked_asm(
540536
&mut GlobalAsmContext { tcx, global_asm: &mut cx.global_asm },
541537
instance,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_middle::middle::codegen_fn_attrs::{
1616
use rustc_middle::mir::mono::Linkage;
1717
use rustc_middle::query::Providers;
1818
use rustc_middle::span_bug;
19-
use rustc_middle::ty::{self as ty, TyCtxt};
19+
use rustc_middle::ty::{self as ty, InstanceKind, TyCtxt};
2020
use rustc_session::lint;
2121
use rustc_session::parse::feature_err;
2222
use rustc_span::{Ident, Span, sym};
@@ -54,6 +54,12 @@ fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
5454
}
5555

5656
fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
57+
codegen_instance_attrs(tcx, InstanceKind::Item(did.to_def_id()))
58+
}
59+
60+
fn codegen_instance_attrs(tcx: TyCtxt<'_>, instance_kind: InstanceKind<'_>) -> CodegenFnAttrs {
61+
let did = instance_kind.def_id().expect_local();
62+
5763
if cfg!(debug_assertions) {
5864
let def_kind = tcx.def_kind(did);
5965
assert!(
@@ -121,7 +127,14 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
121127
AttributeKind::ExportName { name, .. } => {
122128
codegen_fn_attrs.export_name = Some(*name);
123129
}
124-
AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
130+
AttributeKind::Naked(_) => {
131+
// Drop the `#[naked]` attribute on other `InstanceKind`s, like the shims that
132+
// are generated for indirect function calls.
133+
if let InstanceKind::Item(_) = instance_kind {
134+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED
135+
}
136+
}
137+
125138
AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
126139
AttributeKind::LinkName { name, .. } => codegen_fn_attrs.link_name = Some(*name),
127140
AttributeKind::LinkSection { name, .. } => {
@@ -739,6 +752,11 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> Option<AutoDiffAttrs> {
739752
}
740753

741754
pub(crate) fn provide(providers: &mut Providers) {
742-
*providers =
743-
Providers { codegen_fn_attrs, should_inherit_track_caller, inherited_align, ..*providers };
755+
*providers = Providers {
756+
codegen_fn_attrs,
757+
codegen_instance_attrs,
758+
should_inherit_track_caller,
759+
inherited_align,
760+
..*providers
761+
};
744762
}

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
356356
LocalRef::Operand(operand) => {
357357
// Don't spill operands onto the stack in naked functions.
358358
// See: https://github.com/rust-lang/rust/issues/42779
359-
let attrs = bx.tcx().codegen_fn_attrs(self.instance.def_id());
359+
let attrs = bx.tcx().codegen_instance_attrs(self.instance.def);
360360
if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
361361
return;
362362
}

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,8 @@ fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
385385

386386
let mut num_untupled = None;
387387

388-
let codegen_fn_attrs = bx.tcx().codegen_fn_attrs(fx.instance.def_id());
389-
let naked = codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED);
390-
if naked {
388+
let codegen_fn_attrs = bx.tcx().codegen_instance_attrs(fx.instance.def);
389+
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
391390
return vec![];
392391
}
393392

compiler/rustc_codegen_ssa/src/mono_item.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
22
use rustc_middle::mir::mono::{Linkage, MonoItem, MonoItemData, Visibility};
3-
use rustc_middle::ty::InstanceKind;
43
use rustc_middle::ty::layout::HasTyCtxt;
54
use tracing::debug;
65

@@ -42,12 +41,8 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
4241
base::codegen_global_asm(cx, item_id);
4342
}
4443
MonoItem::Fn(instance) => {
45-
// Other `InstanceKind`s (e.g. `ReifyShim` generated by indirect calls) should be
46-
// codegened like a normal function.
47-
let is_item_instance = matches!(instance.def, InstanceKind::Item(_));
48-
49-
let flags = cx.tcx().codegen_fn_attrs(instance.def_id()).flags;
50-
if is_item_instance && flags.contains(CodegenFnAttrFlags::NAKED) {
44+
let flags = cx.tcx().codegen_instance_attrs(instance.def).flags;
45+
if flags.contains(CodegenFnAttrFlags::NAKED) {
5146
naked_asm::codegen_naked_asm::<Bx::CodegenCx>(cx, instance, item_data);
5247
} else {
5348
base::codegen_instance::<Bx>(cx, instance);

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ provide! { tcx, def_id, other, cdata,
252252
variances_of => { table }
253253
fn_sig => { table }
254254
codegen_fn_attrs => { table }
255+
codegen_instance_attrs => { table }
255256
impl_trait_header => { table }
256257
const_param_default => { table }
257258
object_lifetime_default => { table }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14661466
}
14671467
if def_kind.has_codegen_attrs() {
14681468
record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
1469+
record!(self.tables.codegen_instance_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
14691470
}
14701471
if should_encode_visibility(def_kind) {
14711472
let vis =

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ define_tables! {
432432
variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
433433
fn_sig: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::PolyFnSig<'static>>>>,
434434
codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
435+
codegen_instance_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
435436
impl_trait_header: Table<DefIndex, LazyValue<ty::ImplTraitHeader<'static>>>,
436437
const_param_default: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, rustc_middle::ty::Const<'static>>>>,
437438
object_lifetime_default: Table<DefIndex, LazyValue<ObjectLifetimeDefault>>,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ use crate::traits::{
133133
CodegenObligationError, DynCompatibilityViolation, EvaluationResult, ImplSource,
134134
ObligationCause, OverflowError, WellFormedLoc, specialization_graph,
135135
};
136+
use crate::ty::InstanceKind;
136137
use crate::ty::fast_reject::SimplifiedType;
137138
use crate::ty::layout::ValidityRequirement;
138139
use crate::ty::print::{PrintTraitRefExt, describe_as_module};
@@ -1511,6 +1512,14 @@ rustc_queries! {
15111512
separate_provide_extern
15121513
}
15131514

1515+
query codegen_instance_attrs(instance_kind: InstanceKind<'tcx>) -> &'tcx CodegenFnAttrs {
1516+
desc { |tcx| "computing instance attributes of `{}`", tcx.def_path_str(instance_kind.def_id()) }
1517+
arena_cache
1518+
cache_on_disk_if { instance_kind.def_id().is_local() }
1519+
separate_provide_extern
1520+
feedable
1521+
}
1522+
15141523
query codegen_fn_attrs(def_id: DefId) -> &'tcx CodegenFnAttrs {
15151524
desc { |tcx| "computing codegen attributes of `{}`", tcx.def_path_str(def_id) }
15161525
arena_cache

tests/codegen/sanitizer/kcfi/naked-function.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ trait MyTrait {
2525
}
2626
impl MyTrait for Thing {}
2727

28+
// the shim calls the real function
29+
// CHECK-LABEL: define
30+
// CHECK-SAME: my_naked_function
31+
// CHECK-SAME: reify.shim.fnptr
32+
2833
// CHECK-LABEL: main
2934
#[unsafe(no_mangle)]
3035
pub fn main() {
3136
// Trick the compiler into generating an indirect call.
3237
const F: extern "C" fn() = Thing::my_naked_function;
3338

3439
// main calls the shim function
35-
// CHECK: call
40+
// CHECK: call void
3641
// CHECK-SAME: my_naked_function
3742
// CHECK-SAME: reify.shim.fnptr
3843
(F)();
3944
}
4045

41-
// the shim calls the real function
42-
// CHECK: define
43-
// CHECK-SAME: my_naked_function
44-
// CHECK-SAME: reify.shim.fnptr
45-
4646
// CHECK: declare !kcfi_type
4747
// CHECK-SAME: my_naked_function

0 commit comments

Comments
 (0)