rustc_mir_transform/
cross_crate_inline.rs

1use rustc_hir::attrs::InlineAttr;
2use rustc_hir::def::DefKind;
3use rustc_hir::def_id::LocalDefId;
4use rustc_middle::bug;
5use rustc_middle::mir::visit::Visitor;
6use rustc_middle::mir::*;
7use rustc_middle::query::Providers;
8use rustc_middle::ty::TyCtxt;
9use rustc_session::config::{InliningThreshold, OptLevel};
10use rustc_span::sym;
11
12use crate::{inline, pass_manager as pm};
13
14pub(super) fn provide(providers: &mut Providers) {
15    providers.cross_crate_inlinable = cross_crate_inlinable;
16}
17
18fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
19    let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id);
20    // If this has an extern indicator, then this function is globally shared and thus will not
21    // generate cgu-internal copies which would make it cross-crate inlinable.
22    if codegen_fn_attrs.contains_extern_indicator() {
23        return false;
24    }
25
26    // This just reproduces the logic from Instance::requires_inline.
27    match tcx.def_kind(def_id) {
28        DefKind::Ctor(..) | DefKind::Closure | DefKind::SyntheticCoroutineBody => return true,
29        DefKind::Fn | DefKind::AssocFn => {}
30        _ => return false,
31    }
32
33    // From this point on, it is valid to return true or false.
34    if tcx.sess.opts.unstable_opts.cross_crate_inline_threshold == InliningThreshold::Always {
35        return true;
36    }
37
38    // FIXME(autodiff): replace this as per discussion in https://github.com/rust-lang/rust/pull/149033#discussion_r2535465880
39    if tcx.has_attr(def_id, sym::autodiff_forward)
40        || tcx.has_attr(def_id, sym::autodiff_reverse)
41        || tcx.has_attr(def_id, sym::rustc_autodiff)
42    {
43        return true;
44    }
45
46    if tcx.has_attr(def_id, sym::rustc_intrinsic) {
47        // Intrinsic fallback bodies are always cross-crate inlineable.
48        // To ensure that the MIR inliner doesn't cluelessly try to inline fallback
49        // bodies even when the backend would implement something better, we stop
50        // the MIR inliner from ever inlining an intrinsic.
51        return true;
52    }
53
54    // Obey source annotations first; this is important because it means we can use
55    // #[inline(never)] to force code generation.
56    match codegen_fn_attrs.inline {
57        InlineAttr::Never => return false,
58        InlineAttr::Hint | InlineAttr::Always | InlineAttr::Force { .. } => return true,
59        _ => {}
60    }
61
62    // If the crate is likely to be mostly unused, use cross-crate inlining to defer codegen until
63    // the function is referenced, in order to skip codegen for unused functions. This is
64    // intentionally after the check for `inline(never)`, so that `inline(never)` wins.
65    if tcx.sess.opts.unstable_opts.hint_mostly_unused {
66        return true;
67    }
68
69    let sig = tcx.fn_sig(def_id).instantiate_identity();
70    for ty in sig.inputs().skip_binder().iter().chain(std::iter::once(&sig.output().skip_binder()))
71    {
72        // FIXME(f16_f128): in order to avoid crashes building `core`, always inline to skip
73        // codegen if the function is not used.
74        if ty == &tcx.types.f16 || ty == &tcx.types.f128 {
75            return true;
76        }
77    }
78
79    // Don't do any inference when incremental compilation is enabled; the additional inlining that
80    // inference permits also creates more work for small edits.
81    if tcx.sess.opts.incremental.is_some() {
82        return false;
83    }
84
85    // Don't do any inference if codegen optimizations are disabled and also MIR inlining is not
86    // enabled. This ensures that we do inference even if someone only passes -Zinline-mir,
87    // which is less confusing than having to also enable -Copt-level=1.
88    let inliner_will_run = pm::should_run_pass(tcx, &inline::Inline, pm::Optimizations::Allowed)
89        || inline::ForceInline::should_run_pass_for_callee(tcx, def_id.to_def_id());
90    if matches!(tcx.sess.opts.optimize, OptLevel::No) && !inliner_will_run {
91        return false;
92    }
93
94    if !tcx.is_mir_available(def_id) {
95        return false;
96    }
97
98    let threshold = match tcx.sess.opts.unstable_opts.cross_crate_inline_threshold {
99        InliningThreshold::Always => return true,
100        InliningThreshold::Sometimes(threshold) => threshold,
101        InliningThreshold::Never => return false,
102    };
103
104    let mir = tcx.optimized_mir(def_id);
105    let mut checker =
106        CostChecker { tcx, callee_body: mir, calls: 0, statements: 0, landing_pads: 0, resumes: 0 };
107    checker.visit_body(mir);
108    checker.calls == 0
109        && checker.resumes == 0
110        && checker.landing_pads == 0
111        && checker.statements <= threshold
112}
113
114// The threshold that CostChecker computes is balancing the desire to make more things
115// inlinable cross crates against the growth in incremental CGU size that happens when too many
116// things in the sysroot are made inlinable.
117// Permitting calls causes the size of some incremental CGUs to grow, because more functions are
118// made inlinable out of the sysroot or dependencies.
119// Assert terminators are similar to calls, but do not have the same impact on compile time, so
120// those are just treated as statements.
121// A threshold exists at all because we don't want to blindly mark a huge function as inlinable.
122
123struct CostChecker<'b, 'tcx> {
124    tcx: TyCtxt<'tcx>,
125    callee_body: &'b Body<'tcx>,
126    calls: usize,
127    statements: usize,
128    landing_pads: usize,
129    resumes: usize,
130}
131
132impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
133    fn visit_statement(&mut self, statement: &Statement<'tcx>, _: Location) {
134        // Don't count StorageLive/StorageDead in the inlining cost.
135        match statement.kind {
136            StatementKind::StorageLive(_) | StatementKind::StorageDead(_) | StatementKind::Nop => {}
137            _ => self.statements += 1,
138        }
139    }
140
141    fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, _: Location) {
142        self.statements += 1;
143        let tcx = self.tcx;
144        match &terminator.kind {
145            TerminatorKind::Drop { place, unwind, .. } => {
146                let ty = place.ty(self.callee_body, tcx).ty;
147                if !ty.is_trivially_pure_clone_copy() {
148                    self.calls += 1;
149                    if let UnwindAction::Cleanup(_) = unwind {
150                        self.landing_pads += 1;
151                    }
152                }
153            }
154            TerminatorKind::Call { func, unwind, .. } => {
155                // We track calls because they make our function not a leaf (and in theory, the
156                // number of calls indicates how likely this function is to perturb other CGUs).
157                // But intrinsics don't have a body that gets assigned to a CGU, so they are
158                // ignored.
159                if let Some((fn_def_id, _)) = func.const_fn_def()
160                    && self.tcx.has_attr(fn_def_id, sym::rustc_intrinsic)
161                {
162                    return;
163                }
164                self.calls += 1;
165                if let UnwindAction::Cleanup(_) = unwind {
166                    self.landing_pads += 1;
167                }
168            }
169            TerminatorKind::TailCall { .. } => {
170                self.calls += 1;
171            }
172            TerminatorKind::Assert { unwind, .. } => {
173                if let UnwindAction::Cleanup(_) = unwind {
174                    self.landing_pads += 1;
175                }
176            }
177            TerminatorKind::UnwindResume => self.resumes += 1,
178            TerminatorKind::InlineAsm { unwind, .. } => {
179                if let UnwindAction::Cleanup(_) = unwind {
180                    self.landing_pads += 1;
181                }
182            }
183            TerminatorKind::Return
184            | TerminatorKind::Goto { .. }
185            | TerminatorKind::SwitchInt { .. }
186            | TerminatorKind::Unreachable
187            | TerminatorKind::UnwindTerminate(_) => {}
188            kind @ (TerminatorKind::FalseUnwind { .. }
189            | TerminatorKind::FalseEdge { .. }
190            | TerminatorKind::Yield { .. }
191            | TerminatorKind::CoroutineDrop) => {
192                bug!("{kind:?} should not be in runtime MIR");
193            }
194        }
195    }
196}