rustc_mir_transform/
cross_crate_inline.rs

1use rustc_attr_parsing::InlineAttr;
2use rustc_hir::def::DefKind;
3use rustc_hir::def_id::LocalDefId;
4use rustc_middle::mir::visit::Visitor;
5use rustc_middle::mir::*;
6use rustc_middle::query::Providers;
7use rustc_middle::ty::TyCtxt;
8use rustc_session::config::{InliningThreshold, OptLevel};
9use rustc_span::sym;
10
11use crate::{inline, pass_manager as pm};
12
13pub(super) fn provide(providers: &mut Providers) {
14    providers.cross_crate_inlinable = cross_crate_inlinable;
15}
16
17fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
18    let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id);
19    // If this has an extern indicator, then this function is globally shared and thus will not
20    // generate cgu-internal copies which would make it cross-crate inlinable.
21    if codegen_fn_attrs.contains_extern_indicator() {
22        return false;
23    }
24
25    // This just reproduces the logic from Instance::requires_inline.
26    match tcx.def_kind(def_id) {
27        DefKind::Ctor(..) | DefKind::Closure | DefKind::SyntheticCoroutineBody => return true,
28        DefKind::Fn | DefKind::AssocFn => {}
29        _ => return false,
30    }
31
32    // From this point on, it is valid to return true or false.
33    if tcx.sess.opts.unstable_opts.cross_crate_inline_threshold == InliningThreshold::Always {
34        return true;
35    }
36
37    if tcx.has_attr(def_id, sym::rustc_intrinsic) {
38        // Intrinsic fallback bodies are always cross-crate inlineable.
39        // To ensure that the MIR inliner doesn't cluelessly try to inline fallback
40        // bodies even when the backend would implement something better, we stop
41        // the MIR inliner from ever inlining an intrinsic.
42        return true;
43    }
44
45    // Obey source annotations first; this is important because it means we can use
46    // #[inline(never)] to force code generation.
47    match codegen_fn_attrs.inline {
48        InlineAttr::Never => return false,
49        InlineAttr::Hint | InlineAttr::Always | InlineAttr::Force { .. } => return true,
50        _ => {}
51    }
52
53    let sig = tcx.fn_sig(def_id).instantiate_identity();
54    for ty in sig.inputs().skip_binder().iter().chain(std::iter::once(&sig.output().skip_binder()))
55    {
56        // FIXME(f16_f128): in order to avoid crashes building `core`, always inline to skip
57        // codegen if the function is not used.
58        if ty == &tcx.types.f16 || ty == &tcx.types.f128 {
59            return true;
60        }
61    }
62
63    // Don't do any inference when incremental compilation is enabled; the additional inlining that
64    // inference permits also creates more work for small edits.
65    if tcx.sess.opts.incremental.is_some() {
66        return false;
67    }
68
69    // Don't do any inference if codegen optimizations are disabled and also MIR inlining is not
70    // enabled. This ensures that we do inference even if someone only passes -Zinline-mir,
71    // which is less confusing than having to also enable -Copt-level=1.
72    let inliner_will_run = pm::should_run_pass(tcx, &inline::Inline, pm::Optimizations::Allowed)
73        || inline::ForceInline::should_run_pass_for_callee(tcx, def_id.to_def_id());
74    if matches!(tcx.sess.opts.optimize, OptLevel::No) && !inliner_will_run {
75        return false;
76    }
77
78    if !tcx.is_mir_available(def_id) {
79        return false;
80    }
81
82    let threshold = match tcx.sess.opts.unstable_opts.cross_crate_inline_threshold {
83        InliningThreshold::Always => return true,
84        InliningThreshold::Sometimes(threshold) => threshold,
85        InliningThreshold::Never => return false,
86    };
87
88    let mir = tcx.optimized_mir(def_id);
89    let mut checker =
90        CostChecker { tcx, callee_body: mir, calls: 0, statements: 0, landing_pads: 0, resumes: 0 };
91    checker.visit_body(mir);
92    checker.calls == 0
93        && checker.resumes == 0
94        && checker.landing_pads == 0
95        && checker.statements <= threshold
96}
97
98struct CostChecker<'b, 'tcx> {
99    tcx: TyCtxt<'tcx>,
100    callee_body: &'b Body<'tcx>,
101    calls: usize,
102    statements: usize,
103    landing_pads: usize,
104    resumes: usize,
105}
106
107impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
108    fn visit_statement(&mut self, statement: &Statement<'tcx>, _: Location) {
109        // Don't count StorageLive/StorageDead in the inlining cost.
110        match statement.kind {
111            StatementKind::StorageLive(_)
112            | StatementKind::StorageDead(_)
113            | StatementKind::Deinit(_)
114            | StatementKind::Nop => {}
115            _ => self.statements += 1,
116        }
117    }
118
119    fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, _: Location) {
120        let tcx = self.tcx;
121        match terminator.kind {
122            TerminatorKind::Drop { ref place, unwind, .. } => {
123                let ty = place.ty(self.callee_body, tcx).ty;
124                if !ty.is_trivially_pure_clone_copy() {
125                    self.calls += 1;
126                    if let UnwindAction::Cleanup(_) = unwind {
127                        self.landing_pads += 1;
128                    }
129                }
130            }
131            TerminatorKind::Call { unwind, .. } => {
132                self.calls += 1;
133                if let UnwindAction::Cleanup(_) = unwind {
134                    self.landing_pads += 1;
135                }
136            }
137            TerminatorKind::Assert { unwind, .. } => {
138                self.calls += 1;
139                if let UnwindAction::Cleanup(_) = unwind {
140                    self.landing_pads += 1;
141                }
142            }
143            TerminatorKind::UnwindResume => self.resumes += 1,
144            TerminatorKind::InlineAsm { unwind, .. } => {
145                self.statements += 1;
146                if let UnwindAction::Cleanup(_) = unwind {
147                    self.landing_pads += 1;
148                }
149            }
150            TerminatorKind::Return => {}
151            _ => self.statements += 1,
152        }
153    }
154}