1use std::assert_matches::debug_assert_matches;
4use std::iter;
5use std::ops::{Range, RangeFrom};
6
7use rustc_abi::{ExternAbi, FieldIdx};
8use rustc_attr_parsing::{InlineAttr, OptimizeAttr};
9use rustc_hir::def::DefKind;
10use rustc_hir::def_id::DefId;
11use rustc_index::Idx;
12use rustc_index::bit_set::DenseBitSet;
13use rustc_middle::bug;
14use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
15use rustc_middle::mir::visit::*;
16use rustc_middle::mir::*;
17use rustc_middle::ty::{self, Instance, InstanceKind, Ty, TyCtxt, TypeFlags, TypeVisitableExt};
18use rustc_session::config::{DebugInfo, OptLevel};
19use rustc_span::source_map::Spanned;
20use tracing::{debug, instrument, trace, trace_span};
21
22use crate::cost_checker::{CostChecker, is_call_like};
23use crate::deref_separator::deref_finder;
24use crate::simplify::simplify_cfg;
25use crate::validate::validate_types;
26use crate::{check_inline, util};
27
28pub(crate) mod cycle;
29
30const HISTORY_DEPTH_LIMIT: usize = 20;
31const TOP_DOWN_DEPTH_LIMIT: usize = 5;
32
33#[derive(Clone, Debug)]
34struct CallSite<'tcx> {
35 callee: Instance<'tcx>,
36 fn_sig: ty::PolyFnSig<'tcx>,
37 block: BasicBlock,
38 source_info: SourceInfo,
39}
40
41pub struct Inline;
44
45impl<'tcx> crate::MirPass<'tcx> for Inline {
46 fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
47 if let Some(enabled) = sess.opts.unstable_opts.inline_mir {
48 return enabled;
49 }
50
51 match sess.mir_opt_level() {
52 0 | 1 => false,
53 2 => {
54 (sess.opts.optimize == OptLevel::More || sess.opts.optimize == OptLevel::Aggressive)
55 && sess.opts.incremental == None
56 }
57 _ => true,
58 }
59 }
60
61 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
62 let span = trace_span!("inline", body = %tcx.def_path_str(body.source.def_id()));
63 let _guard = span.enter();
64 if inline::<NormalInliner<'tcx>>(tcx, body) {
65 debug!("running simplify cfg on {:?}", body.source);
66 simplify_cfg(body);
67 deref_finder(tcx, body);
68 }
69 }
70
71 fn is_required(&self) -> bool {
72 false
73 }
74}
75
76pub struct ForceInline;
77
78impl ForceInline {
79 pub fn should_run_pass_for_callee<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
80 matches!(tcx.codegen_fn_attrs(def_id).inline, InlineAttr::Force { .. })
81 }
82}
83
84impl<'tcx> crate::MirPass<'tcx> for ForceInline {
85 fn is_enabled(&self, _: &rustc_session::Session) -> bool {
86 true
87 }
88
89 fn can_be_overridden(&self) -> bool {
90 false
91 }
92
93 fn is_required(&self) -> bool {
94 true
95 }
96
97 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
98 let span = trace_span!("force_inline", body = %tcx.def_path_str(body.source.def_id()));
99 let _guard = span.enter();
100 if inline::<ForceInliner<'tcx>>(tcx, body) {
101 debug!("running simplify cfg on {:?}", body.source);
102 simplify_cfg(body);
103 deref_finder(tcx, body);
104 }
105 }
106}
107
108trait Inliner<'tcx> {
109 fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> Self;
110
111 fn tcx(&self) -> TyCtxt<'tcx>;
112 fn typing_env(&self) -> ty::TypingEnv<'tcx>;
113 fn history(&self) -> &[DefId];
114 fn caller_def_id(&self) -> DefId;
115
116 fn changed(self) -> bool;
118
119 fn should_inline_for_callee(&self, def_id: DefId) -> bool;
121
122 fn check_codegen_attributes_extra(
123 &self,
124 callee_attrs: &CodegenFnAttrs,
125 ) -> Result<(), &'static str>;
126
127 fn check_caller_mir_body(&self, body: &Body<'tcx>) -> bool;
128
129 fn check_callee_mir_body(
132 &self,
133 callsite: &CallSite<'tcx>,
134 callee_body: &Body<'tcx>,
135 callee_attrs: &CodegenFnAttrs,
136 ) -> Result<(), &'static str>;
137
138 fn on_inline_success(
140 &mut self,
141 callsite: &CallSite<'tcx>,
142 caller_body: &mut Body<'tcx>,
143 new_blocks: std::ops::Range<BasicBlock>,
144 );
145
146 fn on_inline_failure(&self, callsite: &CallSite<'tcx>, reason: &'static str);
148}
149
150struct ForceInliner<'tcx> {
151 tcx: TyCtxt<'tcx>,
152 typing_env: ty::TypingEnv<'tcx>,
153 def_id: DefId,
155 history: Vec<DefId>,
161 changed: bool,
163}
164
165impl<'tcx> Inliner<'tcx> for ForceInliner<'tcx> {
166 fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> Self {
167 Self { tcx, typing_env: body.typing_env(tcx), def_id, history: Vec::new(), changed: false }
168 }
169
170 fn tcx(&self) -> TyCtxt<'tcx> {
171 self.tcx
172 }
173
174 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
175 self.typing_env
176 }
177
178 fn history(&self) -> &[DefId] {
179 &self.history
180 }
181
182 fn caller_def_id(&self) -> DefId {
183 self.def_id
184 }
185
186 fn changed(self) -> bool {
187 self.changed
188 }
189
190 fn should_inline_for_callee(&self, def_id: DefId) -> bool {
191 ForceInline::should_run_pass_for_callee(self.tcx(), def_id)
192 }
193
194 fn check_codegen_attributes_extra(
195 &self,
196 callee_attrs: &CodegenFnAttrs,
197 ) -> Result<(), &'static str> {
198 debug_assert_matches!(callee_attrs.inline, InlineAttr::Force { .. });
199 Ok(())
200 }
201
202 fn check_caller_mir_body(&self, _: &Body<'tcx>) -> bool {
203 true
204 }
205
206 #[instrument(level = "debug", skip(self, callee_body))]
207 fn check_callee_mir_body(
208 &self,
209 _: &CallSite<'tcx>,
210 callee_body: &Body<'tcx>,
211 callee_attrs: &CodegenFnAttrs,
212 ) -> Result<(), &'static str> {
213 if callee_body.tainted_by_errors.is_some() {
214 return Err("body has errors");
215 }
216
217 let caller_attrs = self.tcx().codegen_fn_attrs(self.caller_def_id());
218 if callee_attrs.instruction_set != caller_attrs.instruction_set
219 && callee_body
220 .basic_blocks
221 .iter()
222 .any(|bb| matches!(bb.terminator().kind, TerminatorKind::InlineAsm { .. }))
223 {
224 Err("cannot move inline-asm across instruction sets")
230 } else {
231 Ok(())
232 }
233 }
234
235 fn on_inline_success(
236 &mut self,
237 callsite: &CallSite<'tcx>,
238 caller_body: &mut Body<'tcx>,
239 new_blocks: std::ops::Range<BasicBlock>,
240 ) {
241 self.changed = true;
242
243 self.history.push(callsite.callee.def_id());
244 process_blocks(self, caller_body, new_blocks);
245 self.history.pop();
246 }
247
248 fn on_inline_failure(&self, callsite: &CallSite<'tcx>, reason: &'static str) {
249 let tcx = self.tcx();
250 let InlineAttr::Force { attr_span, reason: justification } =
251 tcx.codegen_fn_attrs(callsite.callee.def_id()).inline
252 else {
253 bug!("called on item without required inlining");
254 };
255
256 let call_span = callsite.source_info.span;
257 tcx.dcx().emit_err(crate::errors::ForceInlineFailure {
258 call_span,
259 attr_span,
260 caller_span: tcx.def_span(self.def_id),
261 caller: tcx.def_path_str(self.def_id),
262 callee_span: tcx.def_span(callsite.callee.def_id()),
263 callee: tcx.def_path_str(callsite.callee.def_id()),
264 reason,
265 justification: justification.map(|sym| crate::errors::ForceInlineJustification { sym }),
266 });
267 }
268}
269
270struct NormalInliner<'tcx> {
271 tcx: TyCtxt<'tcx>,
272 typing_env: ty::TypingEnv<'tcx>,
273 def_id: DefId,
275 history: Vec<DefId>,
281 top_down_counter: usize,
285 changed: bool,
287 caller_is_inline_forwarder: bool,
290}
291
292impl<'tcx> NormalInliner<'tcx> {
293 fn past_depth_limit(&self) -> bool {
294 self.history.len() > HISTORY_DEPTH_LIMIT || self.top_down_counter > TOP_DOWN_DEPTH_LIMIT
295 }
296}
297
298impl<'tcx> Inliner<'tcx> for NormalInliner<'tcx> {
299 fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> Self {
300 let typing_env = body.typing_env(tcx);
301 let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id);
302
303 Self {
304 tcx,
305 typing_env,
306 def_id,
307 history: Vec::new(),
308 top_down_counter: 0,
309 changed: false,
310 caller_is_inline_forwarder: matches!(
311 codegen_fn_attrs.inline,
312 InlineAttr::Hint | InlineAttr::Always | InlineAttr::Force { .. }
313 ) && body_is_forwarder(body),
314 }
315 }
316
317 fn tcx(&self) -> TyCtxt<'tcx> {
318 self.tcx
319 }
320
321 fn caller_def_id(&self) -> DefId {
322 self.def_id
323 }
324
325 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
326 self.typing_env
327 }
328
329 fn history(&self) -> &[DefId] {
330 &self.history
331 }
332
333 fn changed(self) -> bool {
334 self.changed
335 }
336
337 fn should_inline_for_callee(&self, _: DefId) -> bool {
338 true
339 }
340
341 fn check_codegen_attributes_extra(
342 &self,
343 callee_attrs: &CodegenFnAttrs,
344 ) -> Result<(), &'static str> {
345 if self.past_depth_limit() && matches!(callee_attrs.inline, InlineAttr::None) {
346 Err("Past depth limit so not inspecting unmarked callee")
347 } else {
348 Ok(())
349 }
350 }
351
352 fn check_caller_mir_body(&self, body: &Body<'tcx>) -> bool {
353 if body.coroutine.is_some() {
357 return false;
358 }
359
360 true
361 }
362
363 #[instrument(level = "debug", skip(self, callee_body))]
364 fn check_callee_mir_body(
365 &self,
366 callsite: &CallSite<'tcx>,
367 callee_body: &Body<'tcx>,
368 callee_attrs: &CodegenFnAttrs,
369 ) -> Result<(), &'static str> {
370 let tcx = self.tcx();
371
372 if let Some(_) = callee_body.tainted_by_errors {
373 return Err("body has errors");
374 }
375
376 if self.past_depth_limit() && callee_body.basic_blocks.len() > 1 {
377 return Err("Not inlining multi-block body as we're past a depth limit");
378 }
379
380 let mut threshold = if self.caller_is_inline_forwarder || self.past_depth_limit() {
381 tcx.sess.opts.unstable_opts.inline_mir_forwarder_threshold.unwrap_or(30)
382 } else if tcx.cross_crate_inlinable(callsite.callee.def_id()) {
383 tcx.sess.opts.unstable_opts.inline_mir_hint_threshold.unwrap_or(100)
384 } else {
385 tcx.sess.opts.unstable_opts.inline_mir_threshold.unwrap_or(50)
386 };
387
388 if callee_body.basic_blocks.len() <= 3 {
392 threshold += threshold / 4;
393 }
394 debug!(" final inline threshold = {}", threshold);
395
396 let mut checker =
399 CostChecker::new(tcx, self.typing_env(), Some(callsite.callee), callee_body);
400
401 checker.add_function_level_costs();
402
403 let mut work_list = vec![START_BLOCK];
405 let mut visited = DenseBitSet::new_empty(callee_body.basic_blocks.len());
406 while let Some(bb) = work_list.pop() {
407 if !visited.insert(bb.index()) {
408 continue;
409 }
410
411 let blk = &callee_body.basic_blocks[bb];
412 checker.visit_basic_block_data(bb, blk);
413
414 let term = blk.terminator();
415 let caller_attrs = tcx.codegen_fn_attrs(self.caller_def_id());
416 if let TerminatorKind::Drop { ref place, target, unwind, replace: _ } = term.kind {
417 work_list.push(target);
418
419 let ty = callsite
421 .callee
422 .instantiate_mir(tcx, ty::EarlyBinder::bind(&place.ty(callee_body, tcx).ty));
423 if ty.needs_drop(tcx, self.typing_env())
424 && let UnwindAction::Cleanup(unwind) = unwind
425 {
426 work_list.push(unwind);
427 }
428 } else if callee_attrs.instruction_set != caller_attrs.instruction_set
429 && matches!(term.kind, TerminatorKind::InlineAsm { .. })
430 {
431 return Err("cannot move inline-asm across instruction sets");
437 } else if let TerminatorKind::TailCall { .. } = term.kind {
438 return Err("can't inline functions with tail calls");
441 } else {
442 work_list.extend(term.successors())
443 }
444 }
445
446 let cost = checker.cost();
451 if cost <= threshold {
452 debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
453 Ok(())
454 } else {
455 debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
456 Err("cost above threshold")
457 }
458 }
459
460 fn on_inline_success(
461 &mut self,
462 callsite: &CallSite<'tcx>,
463 caller_body: &mut Body<'tcx>,
464 new_blocks: std::ops::Range<BasicBlock>,
465 ) {
466 self.changed = true;
467
468 let new_calls_count = new_blocks
469 .clone()
470 .filter(|&bb| is_call_like(caller_body.basic_blocks[bb].terminator()))
471 .count();
472 if new_calls_count > 1 {
473 self.top_down_counter += 1;
474 }
475
476 self.history.push(callsite.callee.def_id());
477 process_blocks(self, caller_body, new_blocks);
478 self.history.pop();
479
480 if self.history.is_empty() {
481 self.top_down_counter = 0;
482 }
483 }
484
485 fn on_inline_failure(&self, _: &CallSite<'tcx>, _: &'static str) {}
486}
487
488fn inline<'tcx, T: Inliner<'tcx>>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
489 let def_id = body.source.def_id();
490
491 if !tcx.hir_body_owner_kind(def_id).is_fn_or_closure() {
493 return false;
494 }
495
496 let mut inliner = T::new(tcx, def_id, body);
497 if !inliner.check_caller_mir_body(body) {
498 return false;
499 }
500
501 let blocks = START_BLOCK..body.basic_blocks.next_index();
502 process_blocks(&mut inliner, body, blocks);
503 inliner.changed()
504}
505
506fn process_blocks<'tcx, I: Inliner<'tcx>>(
507 inliner: &mut I,
508 caller_body: &mut Body<'tcx>,
509 blocks: Range<BasicBlock>,
510) {
511 for bb in blocks {
512 let bb_data = &caller_body[bb];
513 if bb_data.is_cleanup {
514 continue;
515 }
516
517 let Some(callsite) = resolve_callsite(inliner, caller_body, bb, bb_data) else {
518 continue;
519 };
520
521 let span = trace_span!("process_blocks", %callsite.callee, ?bb);
522 let _guard = span.enter();
523
524 match try_inlining(inliner, caller_body, &callsite) {
525 Err(reason) => {
526 debug!("not-inlined {} [{}]", callsite.callee, reason);
527 inliner.on_inline_failure(&callsite, reason);
528 }
529 Ok(new_blocks) => {
530 debug!("inlined {}", callsite.callee);
531 inliner.on_inline_success(&callsite, caller_body, new_blocks);
532 }
533 }
534 }
535}
536
537fn resolve_callsite<'tcx, I: Inliner<'tcx>>(
538 inliner: &I,
539 caller_body: &Body<'tcx>,
540 bb: BasicBlock,
541 bb_data: &BasicBlockData<'tcx>,
542) -> Option<CallSite<'tcx>> {
543 let tcx = inliner.tcx();
544 let terminator = bb_data.terminator();
546
547 if let TerminatorKind::Call { ref func, fn_span, .. } = terminator.kind {
549 let func_ty = func.ty(caller_body, tcx);
550 if let ty::FnDef(def_id, args) = *func_ty.kind() {
551 if !inliner.should_inline_for_callee(def_id) {
552 debug!("not enabled");
553 return None;
554 }
555
556 let args = tcx.try_normalize_erasing_regions(inliner.typing_env(), args).ok()?;
558 let callee =
559 Instance::try_resolve(tcx, inliner.typing_env(), def_id, args).ok().flatten()?;
560
561 if let InstanceKind::Virtual(..) | InstanceKind::Intrinsic(_) = callee.def {
562 return None;
563 }
564
565 if inliner.history().contains(&callee.def_id()) {
566 return None;
567 }
568
569 let fn_sig = tcx.fn_sig(def_id).instantiate(tcx, args);
570
571 if let InstanceKind::Item(instance_def_id) = callee.def
574 && tcx.def_kind(instance_def_id) == DefKind::AssocFn
575 && let instance_fn_sig = tcx.fn_sig(instance_def_id).skip_binder()
576 && instance_fn_sig.abi() != fn_sig.abi()
577 {
578 return None;
579 }
580
581 let source_info = SourceInfo { span: fn_span, ..terminator.source_info };
582
583 return Some(CallSite { callee, fn_sig, block: bb, source_info });
584 }
585 }
586
587 None
588}
589
590fn try_inlining<'tcx, I: Inliner<'tcx>>(
594 inliner: &I,
595 caller_body: &mut Body<'tcx>,
596 callsite: &CallSite<'tcx>,
597) -> Result<std::ops::Range<BasicBlock>, &'static str> {
598 let tcx = inliner.tcx();
599 check_mir_is_available(inliner, caller_body, callsite.callee)?;
600
601 let callee_attrs = tcx.codegen_fn_attrs(callsite.callee.def_id());
602 check_inline::is_inline_valid_on_fn(tcx, callsite.callee.def_id())?;
603 check_codegen_attributes(inliner, callsite, callee_attrs)?;
604 inliner.check_codegen_attributes_extra(callee_attrs)?;
605
606 let terminator = caller_body[callsite.block].terminator.as_ref().unwrap();
607 let TerminatorKind::Call { args, destination, .. } = &terminator.kind else { bug!() };
608 let destination_ty = destination.ty(&caller_body.local_decls, tcx).ty;
609 for arg in args {
610 if !arg.node.ty(&caller_body.local_decls, tcx).is_sized(tcx, inliner.typing_env()) {
611 return Err("call has unsized argument");
614 }
615 }
616
617 let callee_body = try_instance_mir(tcx, callsite.callee.def)?;
618 check_inline::is_inline_valid_on_body(tcx, callee_body)?;
619 inliner.check_callee_mir_body(callsite, callee_body, callee_attrs)?;
620
621 let Ok(callee_body) = callsite.callee.try_instantiate_mir_and_normalize_erasing_regions(
622 tcx,
623 inliner.typing_env(),
624 ty::EarlyBinder::bind(callee_body.clone()),
625 ) else {
626 debug!("failed to normalize callee body");
627 return Err("implementation limitation -- could not normalize callee body");
628 };
629
630 if !validate_types(tcx, inliner.typing_env(), &callee_body, &caller_body).is_empty() {
633 debug!("failed to validate callee body");
634 return Err("implementation limitation -- callee body failed validation");
635 }
636
637 let output_type = callee_body.return_ty();
641 if !util::sub_types(tcx, inliner.typing_env(), output_type, destination_ty) {
642 trace!(?output_type, ?destination_ty);
643 return Err("implementation limitation -- return type mismatch");
644 }
645 if callsite.fn_sig.abi() == ExternAbi::RustCall {
646 let (self_arg, arg_tuple) = match &args[..] {
647 [arg_tuple] => (None, arg_tuple),
648 [self_arg, arg_tuple] => (Some(self_arg), arg_tuple),
649 _ => bug!("Expected `rust-call` to have 1 or 2 args"),
650 };
651
652 let self_arg_ty = self_arg.map(|self_arg| self_arg.node.ty(&caller_body.local_decls, tcx));
653
654 let arg_tuple_ty = arg_tuple.node.ty(&caller_body.local_decls, tcx);
655 let arg_tys = if callee_body.spread_arg.is_some() {
656 std::slice::from_ref(&arg_tuple_ty)
657 } else {
658 let ty::Tuple(arg_tuple_tys) = *arg_tuple_ty.kind() else {
659 bug!("Closure arguments are not passed as a tuple");
660 };
661 arg_tuple_tys.as_slice()
662 };
663
664 for (arg_ty, input) in
665 self_arg_ty.into_iter().chain(arg_tys.iter().copied()).zip(callee_body.args_iter())
666 {
667 let input_type = callee_body.local_decls[input].ty;
668 if !util::sub_types(tcx, inliner.typing_env(), input_type, arg_ty) {
669 trace!(?arg_ty, ?input_type);
670 debug!("failed to normalize tuple argument type");
671 return Err("implementation limitation");
672 }
673 }
674 } else {
675 for (arg, input) in args.iter().zip(callee_body.args_iter()) {
676 let input_type = callee_body.local_decls[input].ty;
677 let arg_ty = arg.node.ty(&caller_body.local_decls, tcx);
678 if !util::sub_types(tcx, inliner.typing_env(), input_type, arg_ty) {
679 trace!(?arg_ty, ?input_type);
680 debug!("failed to normalize argument type");
681 return Err("implementation limitation -- arg mismatch");
682 }
683 }
684 }
685
686 let old_blocks = caller_body.basic_blocks.next_index();
687 inline_call(inliner, caller_body, callsite, callee_body);
688 let new_blocks = old_blocks..caller_body.basic_blocks.next_index();
689
690 Ok(new_blocks)
691}
692
693fn check_mir_is_available<'tcx, I: Inliner<'tcx>>(
694 inliner: &I,
695 caller_body: &Body<'tcx>,
696 callee: Instance<'tcx>,
697) -> Result<(), &'static str> {
698 let caller_def_id = caller_body.source.def_id();
699 let callee_def_id = callee.def_id();
700 if callee_def_id == caller_def_id {
701 return Err("self-recursion");
702 }
703
704 match callee.def {
705 InstanceKind::Item(_) => {
706 if !inliner.tcx().is_mir_available(callee_def_id) {
710 debug!("item MIR unavailable");
711 return Err("implementation limitation -- MIR unavailable");
712 }
713 }
714 InstanceKind::Intrinsic(_) | InstanceKind::Virtual(..) => {
716 debug!("instance without MIR (intrinsic / virtual)");
717 return Err("implementation limitation -- cannot inline intrinsic");
718 }
719
720 InstanceKind::DropGlue(_, Some(ty)) if ty.has_type_flags(TypeFlags::HAS_CT_PARAM) => {
726 debug!("still needs substitution");
727 return Err("implementation limitation -- HACK for dropping polymorphic type");
728 }
729
730 InstanceKind::VTableShim(_)
735 | InstanceKind::ReifyShim(..)
736 | InstanceKind::FnPtrShim(..)
737 | InstanceKind::ClosureOnceShim { .. }
738 | InstanceKind::ConstructCoroutineInClosureShim { .. }
739 | InstanceKind::DropGlue(..)
740 | InstanceKind::CloneShim(..)
741 | InstanceKind::ThreadLocalShim(..)
742 | InstanceKind::FnPtrAddrShim(..)
743 | InstanceKind::AsyncDropGlueCtorShim(..) => return Ok(()),
744 }
745
746 if inliner.tcx().is_constructor(callee_def_id) {
747 trace!("constructors always have MIR");
748 return Ok(());
750 }
751
752 if callee_def_id.is_local()
753 && !inliner
754 .tcx()
755 .is_lang_item(inliner.tcx().parent(caller_def_id), rustc_hir::LangItem::FnOnce)
756 {
757 if inliner.tcx().mir_callgraph_reachable((callee, caller_def_id.expect_local())) {
760 debug!("query cycle avoidance");
761 return Err("caller might be reachable from callee");
762 }
763
764 Ok(())
765 } else {
766 trace!("functions from other crates always have MIR");
771 Ok(())
772 }
773}
774
775fn check_codegen_attributes<'tcx, I: Inliner<'tcx>>(
778 inliner: &I,
779 callsite: &CallSite<'tcx>,
780 callee_attrs: &CodegenFnAttrs,
781) -> Result<(), &'static str> {
782 let tcx = inliner.tcx();
783 if let InlineAttr::Never = callee_attrs.inline {
784 return Err("never inline attribute");
785 }
786
787 if let OptimizeAttr::DoNotOptimize = callee_attrs.optimize {
788 return Err("has DoNotOptimize attribute");
789 }
790
791 inliner.check_codegen_attributes_extra(callee_attrs)?;
792
793 let is_generic = callsite.callee.args.non_erasable_generics().next().is_some();
796 if !is_generic && !tcx.cross_crate_inlinable(callsite.callee.def_id()) {
797 return Err("not exported");
798 }
799
800 let codegen_fn_attrs = tcx.codegen_fn_attrs(inliner.caller_def_id());
801 if callee_attrs.no_sanitize != codegen_fn_attrs.no_sanitize {
802 return Err("incompatible sanitizer set");
803 }
804
805 if callee_attrs.instruction_set.is_some()
809 && callee_attrs.instruction_set != codegen_fn_attrs.instruction_set
810 {
811 return Err("incompatible instruction set");
812 }
813
814 let callee_feature_names = callee_attrs.target_features.iter().map(|f| f.name);
815 let this_feature_names = codegen_fn_attrs.target_features.iter().map(|f| f.name);
816 if callee_feature_names.ne(this_feature_names) {
817 return Err("incompatible target features");
823 }
824
825 Ok(())
826}
827
828fn inline_call<'tcx, I: Inliner<'tcx>>(
829 inliner: &I,
830 caller_body: &mut Body<'tcx>,
831 callsite: &CallSite<'tcx>,
832 mut callee_body: Body<'tcx>,
833) {
834 let tcx = inliner.tcx();
835 let terminator = caller_body[callsite.block].terminator.take().unwrap();
836 let TerminatorKind::Call { func, args, destination, unwind, target, .. } = terminator.kind
837 else {
838 bug!("unexpected terminator kind {:?}", terminator.kind);
839 };
840
841 let return_block = if let Some(block) = target {
842 let data = BasicBlockData::new(
845 Some(Terminator {
846 source_info: terminator.source_info,
847 kind: TerminatorKind::Goto { target: block },
848 }),
849 caller_body[block].is_cleanup,
850 );
851 Some(caller_body.basic_blocks_mut().push(data))
852 } else {
853 None
854 };
855
856 fn dest_needs_borrow(place: Place<'_>) -> bool {
862 for elem in place.projection.iter() {
863 match elem {
864 ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
865 _ => {}
866 }
867 }
868
869 false
870 }
871
872 let dest = if dest_needs_borrow(destination) {
873 trace!("creating temp for return destination");
874 let dest = Rvalue::Ref(
875 tcx.lifetimes.re_erased,
876 BorrowKind::Mut { kind: MutBorrowKind::Default },
877 destination,
878 );
879 let dest_ty = dest.ty(caller_body, tcx);
880 let temp = Place::from(new_call_temp(caller_body, callsite, dest_ty, return_block));
881 caller_body[callsite.block].statements.push(Statement {
882 source_info: callsite.source_info,
883 kind: StatementKind::Assign(Box::new((temp, dest))),
884 });
885 tcx.mk_place_deref(temp)
886 } else {
887 destination
888 };
889
890 let (remap_destination, destination_local) = if let Some(d) = dest.as_local() {
893 (false, d)
894 } else {
895 (
896 true,
897 new_call_temp(caller_body, callsite, destination.ty(caller_body, tcx).ty, return_block),
898 )
899 };
900
901 let args = make_call_args(inliner, args, callsite, caller_body, &callee_body, return_block);
903
904 let mut integrator = Integrator {
905 args: &args,
906 new_locals: Local::new(caller_body.local_decls.len())..,
907 new_scopes: SourceScope::new(caller_body.source_scopes.len())..,
908 new_blocks: BasicBlock::new(caller_body.basic_blocks.len())..,
909 destination: destination_local,
910 callsite_scope: caller_body.source_scopes[callsite.source_info.scope].clone(),
911 callsite,
912 cleanup_block: unwind,
913 in_cleanup_block: false,
914 return_block,
915 tcx,
916 always_live_locals: DenseBitSet::new_filled(callee_body.local_decls.len()),
917 };
918
919 integrator.visit_body(&mut callee_body);
922
923 for local in callee_body.vars_and_temps_iter() {
926 if integrator.always_live_locals.contains(local) {
927 let new_local = integrator.map_local(local);
928 caller_body[callsite.block].statements.push(Statement {
929 source_info: callsite.source_info,
930 kind: StatementKind::StorageLive(new_local),
931 });
932 }
933 }
934 if let Some(block) = return_block {
935 let mut n = 0;
938 if remap_destination {
939 caller_body[block].statements.push(Statement {
940 source_info: callsite.source_info,
941 kind: StatementKind::Assign(Box::new((
942 dest,
943 Rvalue::Use(Operand::Move(destination_local.into())),
944 ))),
945 });
946 n += 1;
947 }
948 for local in callee_body.vars_and_temps_iter().rev() {
949 if integrator.always_live_locals.contains(local) {
950 let new_local = integrator.map_local(local);
951 caller_body[block].statements.push(Statement {
952 source_info: callsite.source_info,
953 kind: StatementKind::StorageDead(new_local),
954 });
955 n += 1;
956 }
957 }
958 caller_body[block].statements.rotate_right(n);
959 }
960
961 caller_body.local_decls.extend(callee_body.drain_vars_and_temps());
963 caller_body.source_scopes.append(&mut callee_body.source_scopes);
964 if tcx
965 .sess
966 .opts
967 .unstable_opts
968 .inline_mir_preserve_debug
969 .unwrap_or(tcx.sess.opts.debuginfo != DebugInfo::None)
970 {
971 caller_body.var_debug_info.append(&mut callee_body.var_debug_info);
975 }
976 caller_body.basic_blocks_mut().append(callee_body.basic_blocks_mut());
977
978 caller_body[callsite.block].terminator = Some(Terminator {
979 source_info: callsite.source_info,
980 kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
981 });
982
983 caller_body.required_consts.as_mut().unwrap().extend(
987 callee_body.required_consts().into_iter().filter(|ct| ct.const_.is_required_const()),
988 );
989 let callee_item = MentionedItem::Fn(func.ty(caller_body, tcx));
997 let caller_mentioned_items = caller_body.mentioned_items.as_mut().unwrap();
998 if let Some(idx) = caller_mentioned_items.iter().position(|item| item.node == callee_item) {
999 caller_mentioned_items.remove(idx);
1001 caller_mentioned_items.extend(callee_body.mentioned_items());
1002 } else {
1003 }
1007}
1008
1009fn make_call_args<'tcx, I: Inliner<'tcx>>(
1010 inliner: &I,
1011 args: Box<[Spanned<Operand<'tcx>>]>,
1012 callsite: &CallSite<'tcx>,
1013 caller_body: &mut Body<'tcx>,
1014 callee_body: &Body<'tcx>,
1015 return_block: Option<BasicBlock>,
1016) -> Box<[Local]> {
1017 let tcx = inliner.tcx();
1018
1019 if callsite.fn_sig.abi() == ExternAbi::RustCall && callee_body.spread_arg.is_none() {
1043 let mut args = <_>::into_iter(args);
1045 let self_ = create_temp_if_necessary(
1046 inliner,
1047 args.next().unwrap().node,
1048 callsite,
1049 caller_body,
1050 return_block,
1051 );
1052 let tuple = create_temp_if_necessary(
1053 inliner,
1054 args.next().unwrap().node,
1055 callsite,
1056 caller_body,
1057 return_block,
1058 );
1059 assert!(args.next().is_none());
1060
1061 let tuple = Place::from(tuple);
1062 let ty::Tuple(tuple_tys) = tuple.ty(caller_body, tcx).ty.kind() else {
1063 bug!("Closure arguments are not passed as a tuple");
1064 };
1065
1066 let closure_ref_arg = iter::once(self_);
1068
1069 let tuple_tmp_args = tuple_tys.iter().enumerate().map(|(i, ty)| {
1071 let tuple_field = Operand::Move(tcx.mk_place_field(tuple, FieldIdx::new(i), ty));
1073
1074 create_temp_if_necessary(inliner, tuple_field, callsite, caller_body, return_block)
1076 });
1077
1078 closure_ref_arg.chain(tuple_tmp_args).collect()
1079 } else {
1080 args.into_iter()
1081 .map(|a| create_temp_if_necessary(inliner, a.node, callsite, caller_body, return_block))
1082 .collect()
1083 }
1084}
1085
1086fn create_temp_if_necessary<'tcx, I: Inliner<'tcx>>(
1089 inliner: &I,
1090 arg: Operand<'tcx>,
1091 callsite: &CallSite<'tcx>,
1092 caller_body: &mut Body<'tcx>,
1093 return_block: Option<BasicBlock>,
1094) -> Local {
1095 if let Operand::Move(place) = &arg
1097 && let Some(local) = place.as_local()
1098 && caller_body.local_kind(local) == LocalKind::Temp
1099 {
1100 return local;
1101 }
1102
1103 trace!("creating temp for argument {:?}", arg);
1105 let arg_ty = arg.ty(caller_body, inliner.tcx());
1106 let local = new_call_temp(caller_body, callsite, arg_ty, return_block);
1107 caller_body[callsite.block].statements.push(Statement {
1108 source_info: callsite.source_info,
1109 kind: StatementKind::Assign(Box::new((Place::from(local), Rvalue::Use(arg)))),
1110 });
1111 local
1112}
1113
1114fn new_call_temp<'tcx>(
1116 caller_body: &mut Body<'tcx>,
1117 callsite: &CallSite<'tcx>,
1118 ty: Ty<'tcx>,
1119 return_block: Option<BasicBlock>,
1120) -> Local {
1121 let local = caller_body.local_decls.push(LocalDecl::new(ty, callsite.source_info.span));
1122
1123 caller_body[callsite.block].statements.push(Statement {
1124 source_info: callsite.source_info,
1125 kind: StatementKind::StorageLive(local),
1126 });
1127
1128 if let Some(block) = return_block {
1129 caller_body[block].statements.insert(
1130 0,
1131 Statement {
1132 source_info: callsite.source_info,
1133 kind: StatementKind::StorageDead(local),
1134 },
1135 );
1136 }
1137
1138 local
1139}
1140
1141struct Integrator<'a, 'tcx> {
1149 args: &'a [Local],
1150 new_locals: RangeFrom<Local>,
1151 new_scopes: RangeFrom<SourceScope>,
1152 new_blocks: RangeFrom<BasicBlock>,
1153 destination: Local,
1154 callsite_scope: SourceScopeData<'tcx>,
1155 callsite: &'a CallSite<'tcx>,
1156 cleanup_block: UnwindAction,
1157 in_cleanup_block: bool,
1158 return_block: Option<BasicBlock>,
1159 tcx: TyCtxt<'tcx>,
1160 always_live_locals: DenseBitSet<Local>,
1161}
1162
1163impl Integrator<'_, '_> {
1164 fn map_local(&self, local: Local) -> Local {
1165 let new = if local == RETURN_PLACE {
1166 self.destination
1167 } else {
1168 let idx = local.index() - 1;
1169 if idx < self.args.len() {
1170 self.args[idx]
1171 } else {
1172 Local::new(self.new_locals.start.index() + (idx - self.args.len()))
1173 }
1174 };
1175 trace!("mapping local `{:?}` to `{:?}`", local, new);
1176 new
1177 }
1178
1179 fn map_scope(&self, scope: SourceScope) -> SourceScope {
1180 let new = SourceScope::new(self.new_scopes.start.index() + scope.index());
1181 trace!("mapping scope `{:?}` to `{:?}`", scope, new);
1182 new
1183 }
1184
1185 fn map_block(&self, block: BasicBlock) -> BasicBlock {
1186 let new = BasicBlock::new(self.new_blocks.start.index() + block.index());
1187 trace!("mapping block `{:?}` to `{:?}`", block, new);
1188 new
1189 }
1190
1191 fn map_unwind(&self, unwind: UnwindAction) -> UnwindAction {
1192 if self.in_cleanup_block {
1193 match unwind {
1194 UnwindAction::Cleanup(_) | UnwindAction::Continue => {
1195 bug!("cleanup on cleanup block");
1196 }
1197 UnwindAction::Unreachable | UnwindAction::Terminate(_) => return unwind,
1198 }
1199 }
1200
1201 match unwind {
1202 UnwindAction::Unreachable | UnwindAction::Terminate(_) => unwind,
1203 UnwindAction::Cleanup(target) => UnwindAction::Cleanup(self.map_block(target)),
1204 UnwindAction::Continue => self.cleanup_block,
1206 }
1207 }
1208}
1209
1210impl<'tcx> MutVisitor<'tcx> for Integrator<'_, 'tcx> {
1211 fn tcx(&self) -> TyCtxt<'tcx> {
1212 self.tcx
1213 }
1214
1215 fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) {
1216 *local = self.map_local(*local);
1217 }
1218
1219 fn visit_source_scope_data(&mut self, scope_data: &mut SourceScopeData<'tcx>) {
1220 self.super_source_scope_data(scope_data);
1221 if scope_data.parent_scope.is_none() {
1222 scope_data.parent_scope = Some(self.callsite.source_info.scope);
1225 assert_eq!(scope_data.inlined_parent_scope, None);
1226 scope_data.inlined_parent_scope = if self.callsite_scope.inlined.is_some() {
1227 Some(self.callsite.source_info.scope)
1228 } else {
1229 self.callsite_scope.inlined_parent_scope
1230 };
1231
1232 assert_eq!(scope_data.inlined, None);
1234 scope_data.inlined = Some((self.callsite.callee, self.callsite.source_info.span));
1235 } else if scope_data.inlined_parent_scope.is_none() {
1236 scope_data.inlined_parent_scope = Some(self.map_scope(OUTERMOST_SOURCE_SCOPE));
1238 }
1239 }
1240
1241 fn visit_source_scope(&mut self, scope: &mut SourceScope) {
1242 *scope = self.map_scope(*scope);
1243 }
1244
1245 fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
1246 self.in_cleanup_block = data.is_cleanup;
1247 self.super_basic_block_data(block, data);
1248 self.in_cleanup_block = false;
1249 }
1250
1251 fn visit_retag(&mut self, kind: &mut RetagKind, place: &mut Place<'tcx>, loc: Location) {
1252 self.super_retag(kind, place, loc);
1253
1254 if *kind == RetagKind::FnEntry {
1257 *kind = RetagKind::Default;
1258 }
1259 }
1260
1261 fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
1262 if let StatementKind::StorageLive(local) | StatementKind::StorageDead(local) =
1263 statement.kind
1264 {
1265 self.always_live_locals.remove(local);
1266 }
1267 self.super_statement(statement, location);
1268 }
1269
1270 fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, loc: Location) {
1271 if !matches!(terminator.kind, TerminatorKind::Return) {
1274 self.super_terminator(terminator, loc);
1275 } else {
1276 self.visit_source_info(&mut terminator.source_info);
1277 }
1278
1279 match terminator.kind {
1280 TerminatorKind::CoroutineDrop | TerminatorKind::Yield { .. } => bug!(),
1281 TerminatorKind::Goto { ref mut target } => {
1282 *target = self.map_block(*target);
1283 }
1284 TerminatorKind::SwitchInt { ref mut targets, .. } => {
1285 for tgt in targets.all_targets_mut() {
1286 *tgt = self.map_block(*tgt);
1287 }
1288 }
1289 TerminatorKind::Drop { ref mut target, ref mut unwind, .. } => {
1290 *target = self.map_block(*target);
1291 *unwind = self.map_unwind(*unwind);
1292 }
1293 TerminatorKind::TailCall { .. } => {
1294 unreachable!()
1296 }
1297 TerminatorKind::Call { ref mut target, ref mut unwind, .. } => {
1298 if let Some(ref mut tgt) = *target {
1299 *tgt = self.map_block(*tgt);
1300 }
1301 *unwind = self.map_unwind(*unwind);
1302 }
1303 TerminatorKind::Assert { ref mut target, ref mut unwind, .. } => {
1304 *target = self.map_block(*target);
1305 *unwind = self.map_unwind(*unwind);
1306 }
1307 TerminatorKind::Return => {
1308 terminator.kind = if let Some(tgt) = self.return_block {
1309 TerminatorKind::Goto { target: tgt }
1310 } else {
1311 TerminatorKind::Unreachable
1312 }
1313 }
1314 TerminatorKind::UnwindResume => {
1315 terminator.kind = match self.cleanup_block {
1316 UnwindAction::Cleanup(tgt) => TerminatorKind::Goto { target: tgt },
1317 UnwindAction::Continue => TerminatorKind::UnwindResume,
1318 UnwindAction::Unreachable => TerminatorKind::Unreachable,
1319 UnwindAction::Terminate(reason) => TerminatorKind::UnwindTerminate(reason),
1320 };
1321 }
1322 TerminatorKind::UnwindTerminate(_) => {}
1323 TerminatorKind::Unreachable => {}
1324 TerminatorKind::FalseEdge { ref mut real_target, ref mut imaginary_target } => {
1325 *real_target = self.map_block(*real_target);
1326 *imaginary_target = self.map_block(*imaginary_target);
1327 }
1328 TerminatorKind::FalseUnwind { real_target: _, unwind: _ } =>
1329 {
1331 bug!("False unwinds should have been removed before inlining")
1332 }
1333 TerminatorKind::InlineAsm { ref mut targets, ref mut unwind, .. } => {
1334 for tgt in targets.iter_mut() {
1335 *tgt = self.map_block(*tgt);
1336 }
1337 *unwind = self.map_unwind(*unwind);
1338 }
1339 }
1340 }
1341}
1342
1343#[instrument(skip(tcx), level = "debug")]
1344fn try_instance_mir<'tcx>(
1345 tcx: TyCtxt<'tcx>,
1346 instance: InstanceKind<'tcx>,
1347) -> Result<&'tcx Body<'tcx>, &'static str> {
1348 if let ty::InstanceKind::DropGlue(_, Some(ty))
1349 | ty::InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)) = instance
1350 && let ty::Adt(def, args) = ty.kind()
1351 {
1352 let fields = def.all_fields();
1353 for field in fields {
1354 let field_ty = field.ty(tcx, args);
1355 if field_ty.has_param() && field_ty.has_aliases() {
1356 return Err("cannot build drop shim for polymorphic type");
1357 }
1358 }
1359 }
1360 Ok(tcx.instance_mir(instance))
1361}
1362
1363fn body_is_forwarder(body: &Body<'_>) -> bool {
1364 let TerminatorKind::Call { target, .. } = body.basic_blocks[START_BLOCK].terminator().kind
1365 else {
1366 return false;
1367 };
1368 if let Some(target) = target {
1369 let TerminatorKind::Return = body.basic_blocks[target].terminator().kind else {
1370 return false;
1371 };
1372 }
1373
1374 let max_blocks = if !body.is_polymorphic {
1375 2
1376 } else if target.is_none() {
1377 3
1378 } else {
1379 4
1380 };
1381 if body.basic_blocks.len() > max_blocks {
1382 return false;
1383 }
1384
1385 body.basic_blocks.iter_enumerated().all(|(bb, bb_data)| {
1386 bb == START_BLOCK
1387 || matches!(
1388 bb_data.terminator().kind,
1389 TerminatorKind::Return
1390 | TerminatorKind::Drop { .. }
1391 | TerminatorKind::UnwindResume
1392 | TerminatorKind::UnwindTerminate(_)
1393 )
1394 })
1395}