rustc_mir_transform/
cross_crate_inline.rs1use 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 codegen_fn_attrs.contains_extern_indicator() {
23 return false;
24 }
25
26 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 if tcx.sess.opts.unstable_opts.cross_crate_inline_threshold == InliningThreshold::Always {
35 return true;
36 }
37
38 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 return true;
52 }
53
54 match codegen_fn_attrs.inline {
57 InlineAttr::Never => return false,
58 InlineAttr::Hint | InlineAttr::Always | InlineAttr::Force { .. } => return true,
59 _ => {}
60 }
61
62 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 if ty == &tcx.types.f16 || ty == &tcx.types.f128 {
75 return true;
76 }
77 }
78
79 if tcx.sess.opts.incremental.is_some() {
82 return false;
83 }
84
85 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
114struct 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 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 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}