Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b364460

Browse files
committed
Add a scheme for moving away from extern "rust-intrinsic" entirely
1 parent 65f01d8 commit b364460

File tree

17 files changed

+133
-9
lines changed

17 files changed

+133
-9
lines changed

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,17 @@ fn codegen_regular_intrinsic_call<'tcx>(
12551255

12561256
// Unimplemented intrinsics must have a fallback body. The fallback body is obtained
12571257
// by converting the `InstanceDef::Intrinsic` to an `InstanceDef::Item`.
1258-
_ => return Err(Instance::new(instance.def_id(), instance.args)),
1258+
_ => {
1259+
let intrinsic = fx.tcx.intrinsic(instance.def_id()).unwrap();
1260+
if intrinsic.must_be_overridden {
1261+
span_bug!(
1262+
source_info.span,
1263+
"intrinsic {} must be overridden by codegen_cranelift, but isn't",
1264+
intrinsic.name,
1265+
);
1266+
}
1267+
return Err(Instance::new(instance.def_id(), instance.args));
1268+
}
12591269
}
12601270

12611271
let ret_block = fx.get_block(destination.unwrap());

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_middle::ty::{self, SymbolName, TyCtxt};
1616
use rustc_middle::ty::{GenericArgKind, GenericArgsRef};
1717
use rustc_middle::util::Providers;
1818
use rustc_session::config::{CrateType, OomStrategy};
19+
use rustc_span::sym;
1920
use rustc_target::spec::{SanitizerSet, TlsModel};
2021

2122
pub fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
@@ -81,6 +82,10 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
8182
return library.kind.is_statically_included().then_some(def_id);
8283
}
8384

85+
if tcx.has_attr(def_id, sym::rustc_intrinsic_must_be_overridden) {
86+
return None;
87+
}
88+
8489
// Only consider nodes that actually have exported symbols.
8590
match tcx.def_kind(def_id) {
8691
DefKind::Fn | DefKind::Static(_) => {}

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
896896
MergingSucc::False
897897
};
898898
}
899-
Err(instance) => Some(instance),
899+
Err(instance) => {
900+
if intrinsic.must_be_overridden {
901+
span_bug!(
902+
span,
903+
"intrinsic {} must be overridden by codegen backend, but isn't",
904+
intrinsic.name,
905+
);
906+
}
907+
Some(instance)
908+
}
900909
}
901910
}
902911
};

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
792792
rustc_intrinsic, Normal, template!(Word), ErrorFollowing,
793793
"the `#[rustc_intrinsic]` attribute is used to declare intrinsics with function bodies",
794794
),
795+
rustc_attr!(
796+
rustc_intrinsic_must_be_overridden, Normal, template!(Word), ErrorFollowing,
797+
"the `#[rustc_intrinsic_must_be_overridden]` attribute is used to declare intrinsics without real bodies",
798+
),
795799

796800
// ==========================================================================
797801
// Internal attributes, Testing:

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,13 +1051,18 @@ fn should_encode_mir(
10511051
// Coroutines require optimized MIR to compute layout.
10521052
DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true),
10531053
// Full-fledged functions + closures
1054-
DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
1054+
def_kind @ (DefKind::AssocFn | DefKind::Fn | DefKind::Closure) => {
10551055
let generics = tcx.generics_of(def_id);
1056-
let opt = tcx.sess.opts.unstable_opts.always_encode_mir
1056+
let mut opt = tcx.sess.opts.unstable_opts.always_encode_mir
10571057
|| (tcx.sess.opts.output_types.should_codegen()
10581058
&& reachable_set.contains(&def_id)
10591059
&& (generics.requires_monomorphization(tcx)
10601060
|| tcx.cross_crate_inlinable(def_id)));
1061+
if matches!(def_kind, DefKind::AssocFn | DefKind::Fn) {
1062+
if let Some(intrinsic) = tcx.intrinsic(def_id) {
1063+
opt &= !intrinsic.must_be_overridden;
1064+
}
1065+
}
10611066
// The function has a `const` modifier or is in a `#[const_trait]`.
10621067
let is_const_fn = tcx.is_const_fn_raw(def_id.to_def_id())
10631068
|| tcx.is_const_default_method(def_id.to_def_id());

compiler/rustc_middle/src/ty/intrinsic.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use super::TyCtxt;
55
#[derive(Copy, Clone, Debug, Decodable, Encodable, HashStable)]
66
pub struct IntrinsicDef {
77
pub name: Symbol,
8+
/// Whether the intrinsic has no meaningful body and all backends need to shim all calls to it.
9+
pub must_be_overridden: bool,
810
}
911

1012
impl TyCtxt<'_> {

compiler/rustc_middle/src/ty/util.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,10 @@ pub fn intrinsic(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::IntrinsicDef
16461646
if matches!(tcx.fn_sig(def_id).skip_binder().abi(), Abi::RustIntrinsic | Abi::PlatformIntrinsic)
16471647
|| tcx.has_attr(def_id, sym::rustc_intrinsic)
16481648
{
1649-
Some(ty::IntrinsicDef { name: tcx.item_name(def_id.into()) })
1649+
Some(ty::IntrinsicDef {
1650+
name: tcx.item_name(def_id.into()),
1651+
must_be_overridden: tcx.has_attr(def_id, sym::rustc_intrinsic_must_be_overridden),
1652+
})
16501653
} else {
16511654
None
16521655
}

compiler/rustc_mir_transform/src/cross_crate_inline.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_middle::query::Providers;
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_session::config::InliningThreshold;
1111
use rustc_session::config::OptLevel;
12+
use rustc_span::sym;
1213

1314
pub fn provide(providers: &mut Providers) {
1415
providers.cross_crate_inlinable = cross_crate_inlinable;
@@ -22,6 +23,10 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
2223
return false;
2324
}
2425

26+
if tcx.has_attr(def_id, sym::rustc_intrinsic_must_be_overridden) {
27+
return false;
28+
}
29+
2530
// This just reproduces the logic from Instance::requires_inline.
2631
match tcx.def_kind(def_id) {
2732
DefKind::Ctor(..) | DefKind::Closure => return true,

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,12 @@ fn optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> &Body<'_> {
632632
}
633633

634634
fn inner_optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> Body<'_> {
635+
if let Some(attr) = tcx.get_attr(did, sym::rustc_intrinsic_must_be_overridden) {
636+
span_bug!(
637+
attr.span,
638+
"this intrinsic must be overridden by the codegen backend, it has no meaningful body",
639+
)
640+
}
635641
if tcx.is_constructor(did.to_def_id()) {
636642
// There's no reason to run all of the MIR passes on constructors when
637643
// we can just output the MIR we want directly. This also saves const

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,11 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) ->
10191019
return false;
10201020
}
10211021

1022+
if tcx.has_attr(def_id, sym::rustc_intrinsic_must_be_overridden) {
1023+
// These are implemented by backends directly and have no meaningful body.
1024+
return false;
1025+
}
1026+
10221027
if def_id.is_local() {
10231028
// Local items cannot be referred to locally without monomorphizing them locally.
10241029
return true;

0 commit comments

Comments
 (0)