Skip to main content

rustc_mir_transform/
inline.rs

1//! Inlining pass for MIR functions.
2
3use std::ops::{Range, RangeFrom};
4use std::{debug_assert_matches, iter};
5
6use rustc_abi::{ExternAbi, FieldIdx};
7use rustc_data_structures::thin_vec::ThinVec;
8use rustc_hir::attrs::{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::{
18    self, Instance, InstanceKind, Ty, TyCtxt, TypeFlags, TypeVisitableExt, Unnormalized,
19};
20use rustc_session::config::{DebugInfo, OptLevel};
21use rustc_span::Spanned;
22use tracing::{debug, instrument, trace, trace_span};
23
24use crate::cost_checker::{CostChecker, is_call_like};
25use crate::simplify::{UsedInStmtLocals, simplify_cfg};
26use crate::validate::validate_types;
27use crate::{check_inline, util};
28
29pub(crate) mod cycle;
30
31const HISTORY_DEPTH_LIMIT: usize = 20;
32const TOP_DOWN_DEPTH_LIMIT: usize = 5;
33
34#[derive(Clone, Debug)]
35struct CallSite<'tcx> {
36    callee: Instance<'tcx>,
37    fn_sig: ty::PolyFnSig<'tcx>,
38    block: BasicBlock,
39    source_info: SourceInfo,
40}
41
42// Made public so that `mir_drops_elaborated_and_const_checked` can be overridden
43// by custom rustc drivers, running all the steps by themselves. See #114628.
44pub struct Inline;
45
46impl<'tcx> crate::MirPass<'tcx> for Inline {
47    fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
48        if let Some(enabled) = sess.opts.unstable_opts.inline_mir {
49            return enabled;
50        }
51
52        match sess.mir_opt_level() {
53            0 | 1 => false,
54            2 => {
55                (sess.opts.optimize == OptLevel::More || sess.opts.optimize == OptLevel::Aggressive)
56                    && sess.opts.incremental == None
57            }
58            _ => true,
59        }
60    }
61
62    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
63        let span = trace_span!("inline", body = %tcx.def_path_str(body.source.def_id()));
64        let _guard = span.enter();
65        if inline::<NormalInliner<'tcx>>(tcx, body) {
66            debug!("running simplify cfg on {:?}", body.source);
67            simplify_cfg(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(tcx, body);
103        }
104    }
105}
106
107trait Inliner<'tcx> {
108    fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> Self;
109
110    fn tcx(&self) -> TyCtxt<'tcx>;
111    fn typing_env(&self) -> ty::TypingEnv<'tcx>;
112    fn history(&self) -> &[DefId];
113    fn caller_def_id(&self) -> DefId;
114
115    /// Has the caller body been changed?
116    fn changed(self) -> bool;
117
118    /// Should inlining happen for a given callee?
119    fn should_inline_for_callee(&self, def_id: DefId) -> bool;
120
121    fn check_codegen_attributes_extra(
122        &self,
123        callee_attrs: &CodegenFnAttrs,
124    ) -> Result<(), &'static str>;
125
126    fn check_caller_mir_body(&self, body: &Body<'tcx>) -> bool;
127
128    /// Returns inlining decision that is based on the examination of callee MIR body.
129    /// Assumes that codegen attributes have been checked for compatibility already.
130    fn check_callee_mir_body(
131        &self,
132        callsite: &CallSite<'tcx>,
133        callee_body: &Body<'tcx>,
134        callee_attrs: &CodegenFnAttrs,
135    ) -> Result<(), &'static str>;
136
137    /// Called when inlining succeeds.
138    fn on_inline_success(
139        &mut self,
140        callsite: &CallSite<'tcx>,
141        caller_body: &mut Body<'tcx>,
142        new_blocks: std::ops::Range<BasicBlock>,
143    );
144
145    /// Called when inlining failed or was not performed.
146    fn on_inline_failure(&self, callsite: &CallSite<'tcx>, reason: &'static str);
147}
148
149struct ForceInliner<'tcx> {
150    tcx: TyCtxt<'tcx>,
151    typing_env: ty::TypingEnv<'tcx>,
152    /// `DefId` of caller.
153    def_id: DefId,
154    /// Stack of inlined instances.
155    /// We only check the `DefId` and not the args because we want to
156    /// avoid inlining cases of polymorphic recursion.
157    /// The number of `DefId`s is finite, so checking history is enough
158    /// to ensure that we do not loop endlessly while inlining.
159    history: Vec<DefId>,
160    /// Indicates that the caller body has been modified.
161    changed: bool,
162}
163
164impl<'tcx> Inliner<'tcx> for ForceInliner<'tcx> {
165    fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> Self {
166        Self { tcx, typing_env: body.typing_env(tcx), def_id, history: Vec::new(), changed: false }
167    }
168
169    fn tcx(&self) -> TyCtxt<'tcx> {
170        self.tcx
171    }
172
173    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
174        self.typing_env
175    }
176
177    fn history(&self) -> &[DefId] {
178        &self.history
179    }
180
181    fn caller_def_id(&self) -> DefId {
182        self.def_id
183    }
184
185    fn changed(self) -> bool {
186        self.changed
187    }
188
189    fn should_inline_for_callee(&self, def_id: DefId) -> bool {
190        ForceInline::should_run_pass_for_callee(self.tcx(), def_id)
191    }
192
193    fn check_codegen_attributes_extra(
194        &self,
195        callee_attrs: &CodegenFnAttrs,
196    ) -> Result<(), &'static str> {
197        debug_assert_matches!(callee_attrs.inline, InlineAttr::Force { .. });
198        Ok(())
199    }
200
201    fn check_caller_mir_body(&self, _: &Body<'tcx>) -> bool {
202        true
203    }
204
205    #[instrument(level = "debug", skip(self, callee_body))]
206    fn check_callee_mir_body(
207        &self,
208        _: &CallSite<'tcx>,
209        callee_body: &Body<'tcx>,
210        callee_attrs: &CodegenFnAttrs,
211    ) -> Result<(), &'static str> {
212        if callee_body.tainted_by_errors.is_some() {
213            return Err("body has errors");
214        }
215
216        let caller_attrs = self.tcx().codegen_fn_attrs(self.caller_def_id());
217        if callee_attrs.instruction_set != caller_attrs.instruction_set
218            && callee_body
219                .basic_blocks
220                .iter()
221                .any(|bb| matches!(bb.terminator().kind, TerminatorKind::InlineAsm { .. }))
222        {
223            // During the attribute checking stage we allow a callee with no
224            // instruction_set assigned to count as compatible with a function that does
225            // assign one. However, during this stage we require an exact match when any
226            // inline-asm is detected. LLVM will still possibly do an inline later on
227            // if the no-attribute function ends up with the same instruction set anyway.
228            Err("cannot move inline-asm across instruction sets")
229        } else {
230            Ok(())
231        }
232    }
233
234    fn on_inline_success(
235        &mut self,
236        callsite: &CallSite<'tcx>,
237        caller_body: &mut Body<'tcx>,
238        new_blocks: std::ops::Range<BasicBlock>,
239    ) {
240        self.changed = true;
241
242        self.history.push(callsite.callee.def_id());
243        process_blocks(self, caller_body, new_blocks);
244        self.history.pop();
245    }
246
247    fn on_inline_failure(&self, callsite: &CallSite<'tcx>, reason: &'static str) {
248        let tcx = self.tcx();
249        let InlineAttr::Force { attr_span, reason: justification } =
250            tcx.codegen_instance_attrs(callsite.callee.def).inline
251        else {
252            bug!("called on item without required inlining");
253        };
254
255        let call_span = callsite.source_info.span;
256        let callee = tcx.def_path_str(callsite.callee.def_id());
257        tcx.dcx().emit_err(crate::diagnostics::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: callee.clone(),
264            reason,
265            justification: justification
266                .map(|sym| crate::diagnostics::ForceInlineJustification { sym, callee }),
267        });
268    }
269}
270
271struct NormalInliner<'tcx> {
272    tcx: TyCtxt<'tcx>,
273    typing_env: ty::TypingEnv<'tcx>,
274    /// `DefId` of caller.
275    def_id: DefId,
276    /// Stack of inlined instances.
277    /// We only check the `DefId` and not the args because we want to
278    /// avoid inlining cases of polymorphic recursion.
279    /// The number of `DefId`s is finite, so checking history is enough
280    /// to ensure that we do not loop endlessly while inlining.
281    history: Vec<DefId>,
282    /// How many (multi-call) callsites have we inlined for the top-level call?
283    ///
284    /// We need to limit this in order to prevent super-linear growth in MIR size.
285    top_down_counter: usize,
286    /// Indicates that the caller body has been modified.
287    changed: bool,
288    /// Indicates that the caller is #[inline] and just calls another function,
289    /// and thus we can inline less into it as it'll be inlined itself.
290    caller_is_inline_forwarder: bool,
291}
292
293impl<'tcx> NormalInliner<'tcx> {
294    fn past_depth_limit(&self) -> bool {
295        self.history.len() > HISTORY_DEPTH_LIMIT || self.top_down_counter > TOP_DOWN_DEPTH_LIMIT
296    }
297}
298
299impl<'tcx> Inliner<'tcx> for NormalInliner<'tcx> {
300    fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> Self {
301        let typing_env = body.typing_env(tcx);
302        let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id);
303
304        Self {
305            tcx,
306            typing_env,
307            def_id,
308            history: Vec::new(),
309            top_down_counter: 0,
310            changed: false,
311            caller_is_inline_forwarder: matches!(
312                codegen_fn_attrs.inline,
313                InlineAttr::Hint | InlineAttr::Always | InlineAttr::Force { .. }
314            ) && body_is_forwarder(body),
315        }
316    }
317
318    fn tcx(&self) -> TyCtxt<'tcx> {
319        self.tcx
320    }
321
322    fn caller_def_id(&self) -> DefId {
323        self.def_id
324    }
325
326    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
327        self.typing_env
328    }
329
330    fn history(&self) -> &[DefId] {
331        &self.history
332    }
333
334    fn changed(self) -> bool {
335        self.changed
336    }
337
338    fn should_inline_for_callee(&self, _: DefId) -> bool {
339        true
340    }
341
342    fn check_codegen_attributes_extra(
343        &self,
344        callee_attrs: &CodegenFnAttrs,
345    ) -> Result<(), &'static str> {
346        if self.past_depth_limit() && matches!(callee_attrs.inline, InlineAttr::None) {
347            Err("Past depth limit so not inspecting unmarked callee")
348        } else {
349            Ok(())
350        }
351    }
352
353    fn check_caller_mir_body(&self, body: &Body<'tcx>) -> bool {
354        // Avoid inlining into coroutines, since their `optimized_mir` is used for layout computation,
355        // which can create a cycle, even when no attempt is made to inline the function in the other
356        // direction.
357        if body.coroutine.is_some() {
358            return false;
359        }
360
361        true
362    }
363
364    #[instrument(level = "debug", skip(self, callee_body))]
365    fn check_callee_mir_body(
366        &self,
367        callsite: &CallSite<'tcx>,
368        callee_body: &Body<'tcx>,
369        callee_attrs: &CodegenFnAttrs,
370    ) -> Result<(), &'static str> {
371        let tcx = self.tcx();
372
373        if let Some(_) = callee_body.tainted_by_errors {
374            return Err("body has errors");
375        }
376
377        if self.past_depth_limit() && callee_body.basic_blocks.len() > 1 {
378            return Err("Not inlining multi-block body as we're past a depth limit");
379        }
380
381        let mut threshold = if self.caller_is_inline_forwarder || self.past_depth_limit() {
382            tcx.sess.opts.unstable_opts.inline_mir_forwarder_threshold.unwrap_or(30)
383        } else if tcx.cross_crate_inlinable(callsite.callee.def_id()) {
384            tcx.sess.opts.unstable_opts.inline_mir_hint_threshold.unwrap_or(100)
385        } else {
386            tcx.sess.opts.unstable_opts.inline_mir_threshold.unwrap_or(50)
387        };
388
389        // Give a bonus functions with a small number of blocks,
390        // We normally have two or three blocks for even
391        // very small functions.
392        if callee_body.basic_blocks.len() <= 3 {
393            threshold += threshold / 4;
394        }
395        debug!("    final inline threshold = {}", threshold);
396
397        // FIXME: Give a bonus to functions with only a single caller
398
399        let mut checker =
400            CostChecker::new(tcx, self.typing_env(), Some(callsite.callee), callee_body);
401
402        checker.add_function_level_costs();
403
404        // Traverse the MIR manually so we can account for the effects of inlining on the CFG.
405        let mut work_list = vec![START_BLOCK];
406        let mut visited = DenseBitSet::new_empty(callee_body.basic_blocks.len());
407        while let Some(bb) = work_list.pop() {
408            if !visited.insert(bb.index()) {
409                continue;
410            }
411
412            let blk = &callee_body.basic_blocks[bb];
413            checker.visit_basic_block_data(bb, blk);
414
415            let term = blk.terminator();
416            let caller_attrs = tcx.codegen_fn_attrs(self.caller_def_id());
417            if let TerminatorKind::Drop { ref place, target, unwind, replace: _, drop: _ } =
418                term.kind
419            {
420                work_list.push(target);
421
422                // If the place doesn't actually need dropping, treat it like a regular goto.
423                let ty = callsite
424                    .callee
425                    .instantiate_mir(tcx, ty::EarlyBinder::bind(&place.ty(callee_body, tcx).ty));
426                if ty.needs_drop(tcx, self.typing_env())
427                    && let UnwindAction::Cleanup(unwind) = unwind
428                {
429                    work_list.push(unwind);
430                }
431            } else if callee_attrs.instruction_set != caller_attrs.instruction_set
432                && matches!(term.kind, TerminatorKind::InlineAsm { .. })
433            {
434                // During the attribute checking stage we allow a callee with no
435                // instruction_set assigned to count as compatible with a function that does
436                // assign one. However, during this stage we require an exact match when any
437                // inline-asm is detected. LLVM will still possibly do an inline later on
438                // if the no-attribute function ends up with the same instruction set anyway.
439                return Err("cannot move inline-asm across instruction sets");
440            } else if let TerminatorKind::TailCall { .. } = term.kind {
441                // FIXME(explicit_tail_calls): figure out how exactly functions containing tail
442                // calls can be inlined (and if they even should)
443                return Err("can't inline functions with tail calls");
444            } else {
445                work_list.extend(term.successors())
446            }
447        }
448
449        // N.B. We still apply our cost threshold to #[inline(always)] functions.
450        // That attribute is often applied to very large functions that exceed LLVM's (very
451        // generous) inlining threshold. Such functions are very poor MIR inlining candidates.
452        // Always inlining #[inline(always)] functions in MIR, on net, slows down the compiler.
453        let cost = checker.cost();
454        if cost <= threshold {
455            debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
456            Ok(())
457        } else {
458            debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
459            Err("cost above threshold")
460        }
461    }
462
463    fn on_inline_success(
464        &mut self,
465        callsite: &CallSite<'tcx>,
466        caller_body: &mut Body<'tcx>,
467        new_blocks: std::ops::Range<BasicBlock>,
468    ) {
469        self.changed = true;
470
471        let new_calls_count = new_blocks
472            .clone()
473            .filter(|&bb| is_call_like(caller_body.basic_blocks[bb].terminator()))
474            .count();
475        if new_calls_count > 1 {
476            self.top_down_counter += 1;
477        }
478
479        self.history.push(callsite.callee.def_id());
480        process_blocks(self, caller_body, new_blocks);
481        self.history.pop();
482
483        if self.history.is_empty() {
484            self.top_down_counter = 0;
485        }
486    }
487
488    fn on_inline_failure(&self, _: &CallSite<'tcx>, _: &'static str) {}
489}
490
491fn inline<'tcx, T: Inliner<'tcx>>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
492    let def_id = body.source.def_id();
493
494    // Only do inlining into fn bodies.
495    if !tcx.hir_body_owner_kind(def_id).is_fn_or_closure() {
496        return false;
497    }
498
499    let mut inliner = T::new(tcx, def_id, body);
500    if !inliner.check_caller_mir_body(body) {
501        return false;
502    }
503
504    let blocks = START_BLOCK..body.basic_blocks.next_index();
505    process_blocks(&mut inliner, body, blocks);
506    inliner.changed()
507}
508
509fn process_blocks<'tcx, I: Inliner<'tcx>>(
510    inliner: &mut I,
511    caller_body: &mut Body<'tcx>,
512    blocks: Range<BasicBlock>,
513) {
514    for bb in blocks {
515        let bb_data = &caller_body[bb];
516        if bb_data.is_cleanup {
517            continue;
518        }
519
520        let Some(callsite) = resolve_callsite(inliner, caller_body, bb, bb_data) else {
521            continue;
522        };
523
524        let span = trace_span!("process_blocks", %callsite.callee, ?bb);
525        let _guard = span.enter();
526
527        match try_inlining(inliner, caller_body, &callsite) {
528            Err(reason) => {
529                debug!("not-inlined {} [{}]", callsite.callee, reason);
530                inliner.on_inline_failure(&callsite, reason);
531            }
532            Ok(new_blocks) => {
533                debug!("inlined {}", callsite.callee);
534                inliner.on_inline_success(&callsite, caller_body, new_blocks);
535            }
536        }
537    }
538}
539
540fn resolve_callsite<'tcx, I: Inliner<'tcx>>(
541    inliner: &I,
542    caller_body: &Body<'tcx>,
543    bb: BasicBlock,
544    bb_data: &BasicBlockData<'tcx>,
545) -> Option<CallSite<'tcx>> {
546    let tcx = inliner.tcx();
547    // Only consider direct calls to functions
548    let terminator = bb_data.terminator();
549
550    // FIXME(explicit_tail_calls): figure out if we can inline tail calls
551    if let TerminatorKind::Call { ref func, fn_span, .. } = terminator.kind {
552        let func_ty = func.ty(caller_body, tcx);
553        if let ty::FnDef(def_id, args) = *func_ty.kind() {
554            if !inliner.should_inline_for_callee(def_id) {
555                debug!("not enabled");
556                return None;
557            }
558
559            // To resolve an instance its args have to be fully normalized.
560            let args = tcx
561                .try_normalize_erasing_regions(inliner.typing_env(), Unnormalized::new_wip(args))
562                .ok()?;
563            let mut callee =
564                Instance::try_resolve(tcx, inliner.typing_env(), def_id, args).ok().flatten()?;
565
566            if let InstanceKind::Virtual(..) = callee.def {
567                return None;
568            }
569            if let InstanceKind::Intrinsic(..) = callee.def {
570                let intrinsic = tcx.intrinsic(def_id).unwrap();
571                if intrinsic.must_be_overridden {
572                    return None; // intrinsic without fallback body
573                }
574                if !tcx.sess.fallback_intrinsics.contains(&intrinsic.name) {
575                    return None; // intrinsic that the backend may want to overwrite
576                }
577                // The callee is the fallback body.
578                debug!("callsite is fallback body: {def_id:?}");
579                callee = ty::Instance { def: ty::InstanceKind::Item(def_id), args: callee.args };
580            }
581
582            if inliner.history().contains(&callee.def_id()) {
583                return None;
584            }
585
586            let fn_sig = tcx.fn_sig(def_id).instantiate(tcx, args).skip_norm_wip();
587
588            // Additionally, check that the body that we're inlining actually agrees
589            // with the ABI of the trait that the item comes from.
590            if let InstanceKind::Item(instance_def_id) = callee.def
591                && tcx.def_kind(instance_def_id) == DefKind::AssocFn
592                && let instance_fn_sig = tcx.fn_sig(instance_def_id).skip_binder()
593                && instance_fn_sig.abi() != fn_sig.abi()
594            {
595                return None;
596            }
597
598            let source_info = SourceInfo { span: fn_span, ..terminator.source_info };
599
600            return Some(CallSite { callee, fn_sig, block: bb, source_info });
601        }
602    }
603
604    None
605}
606
607/// Attempts to inline a callsite into the caller body. When successful returns basic blocks
608/// containing the inlined body. Otherwise returns an error describing why inlining didn't take
609/// place.
610fn try_inlining<'tcx, I: Inliner<'tcx>>(
611    inliner: &I,
612    caller_body: &mut Body<'tcx>,
613    callsite: &CallSite<'tcx>,
614) -> Result<std::ops::Range<BasicBlock>, &'static str> {
615    let tcx = inliner.tcx();
616    check_mir_is_available(inliner, caller_body, callsite.callee)?;
617
618    let callee_attrs = tcx.codegen_instance_attrs(callsite.callee.def);
619    let callee_attrs = callee_attrs.as_ref();
620    check_inline::is_inline_valid_on_fn(tcx, callsite.callee.def_id())?;
621    check_codegen_attributes(inliner, callsite, callee_attrs)?;
622
623    let terminator = caller_body[callsite.block].terminator.as_ref().unwrap();
624    let TerminatorKind::Call { args, destination, .. } = &terminator.kind else { bug!() };
625    let destination_ty = destination.ty(&caller_body.local_decls, tcx).ty;
626    for arg in args {
627        if !arg.node.ty(&caller_body.local_decls, tcx).is_sized(tcx, inliner.typing_env()) {
628            // We do not allow inlining functions with unsized params. Inlining these functions
629            // could create unsized locals, which are unsound and being phased out.
630            return Err("call has unsized argument");
631        }
632    }
633
634    let callee_body = try_instance_mir(tcx, callsite.callee.def)?;
635    check_inline::is_inline_valid_on_body(tcx, callee_body)?;
636    inliner.check_callee_mir_body(callsite, callee_body, callee_attrs)?;
637
638    let Ok(callee_body) = callsite.callee.try_instantiate_mir_and_normalize_erasing_regions(
639        tcx,
640        inliner.typing_env(),
641        ty::EarlyBinder::bind(callee_body.clone()),
642    ) else {
643        debug!("failed to normalize callee body");
644        return Err("implementation limitation -- could not normalize callee body");
645    };
646
647    // Normally, this shouldn't be required, but trait normalization failure can create a
648    // validation ICE.
649    if !validate_types(tcx, inliner.typing_env(), &callee_body, caller_body).is_empty() {
650        debug!("failed to validate callee body");
651        return Err("implementation limitation -- callee body failed validation");
652    }
653
654    // Check call signature compatibility.
655    // Normally, this shouldn't be required, but trait normalization failure can create a
656    // validation ICE.
657    let output_type = callee_body.return_ty();
658    if !util::sub_types(tcx, inliner.typing_env(), output_type, destination_ty) {
659        trace!(?output_type, ?destination_ty);
660        return Err("implementation limitation -- return type mismatch");
661    }
662    if callsite.fn_sig.abi() == ExternAbi::RustCall {
663        let (self_arg, arg_tuple) = match &args[..] {
664            [arg_tuple] => (None, arg_tuple),
665            [self_arg, arg_tuple] => (Some(self_arg), arg_tuple),
666            _ => bug!("Expected `rust-call` to have 1 or 2 args"),
667        };
668
669        let self_arg_ty = self_arg.map(|self_arg| self_arg.node.ty(&caller_body.local_decls, tcx));
670
671        let arg_tuple_ty = arg_tuple.node.ty(&caller_body.local_decls, tcx);
672        let arg_tys = if callee_body.spread_arg.is_some() {
673            std::slice::from_ref(&arg_tuple_ty)
674        } else {
675            let ty::Tuple(arg_tuple_tys) = *arg_tuple_ty.kind() else {
676                bug!("Closure arguments are not passed as a tuple");
677            };
678            arg_tuple_tys.as_slice()
679        };
680
681        for (arg_ty, input) in
682            self_arg_ty.into_iter().chain(arg_tys.iter().copied()).zip(callee_body.args_iter())
683        {
684            let input_type = callee_body.local_decls[input].ty;
685            if !util::sub_types(tcx, inliner.typing_env(), input_type, arg_ty) {
686                trace!(?arg_ty, ?input_type);
687                debug!("failed to normalize tuple argument type");
688                return Err("implementation limitation");
689            }
690        }
691    } else {
692        for (arg, input) in args.iter().zip(callee_body.args_iter()) {
693            let input_type = callee_body.local_decls[input].ty;
694            let arg_ty = arg.node.ty(&caller_body.local_decls, tcx);
695            if !util::sub_types(tcx, inliner.typing_env(), input_type, arg_ty) {
696                trace!(?arg_ty, ?input_type);
697                debug!("failed to normalize argument type");
698                return Err("implementation limitation -- arg mismatch");
699            }
700        }
701    }
702
703    let old_blocks = caller_body.basic_blocks.next_index();
704    inline_call(inliner, caller_body, callsite, callee_body);
705    let new_blocks = old_blocks..caller_body.basic_blocks.next_index();
706
707    Ok(new_blocks)
708}
709
710fn check_mir_is_available<'tcx, I: Inliner<'tcx>>(
711    inliner: &I,
712    caller_body: &Body<'tcx>,
713    callee: Instance<'tcx>,
714) -> Result<(), &'static str> {
715    let caller_def_id = caller_body.source.def_id();
716    let callee_def_id = callee.def_id();
717    if callee_def_id == caller_def_id {
718        return Err("self-recursion");
719    }
720
721    match callee.def {
722        InstanceKind::Item(_) => {
723            // If there is no MIR available (either because it was not in metadata or
724            // because it has no MIR because it's an extern function), then the inliner
725            // won't cause cycles on this.
726            if !inliner.tcx().is_mir_available(callee_def_id) {
727                debug!("item MIR unavailable");
728                return Err("implementation limitation -- MIR unavailable");
729            }
730        }
731        // These have no own callable MIR.
732        InstanceKind::Intrinsic(_) | InstanceKind::Virtual(..) => {
733            debug!("instance without MIR (intrinsic / virtual)");
734            return Err("implementation limitation -- cannot inline intrinsic");
735        }
736
737        // FIXME(#127030): `ConstParamHasTy` has bad interactions with
738        // the drop shim builder, which does not evaluate predicates in
739        // the correct param-env for types being dropped. Stall resolving
740        // the MIR for this instance until all of its const params are
741        // substituted.
742        InstanceKind::DropGlue(_, Some(ty)) if ty.has_type_flags(TypeFlags::HAS_CT_PARAM) => {
743            debug!("still needs substitution");
744            return Err("implementation limitation -- HACK for dropping polymorphic type");
745        }
746        InstanceKind::AsyncDropGlue(_, ty) | InstanceKind::AsyncDropGlueCtorShim(_, ty) => {
747            return if ty.still_further_specializable() {
748                Err("still needs substitution")
749            } else {
750                Ok(())
751            };
752        }
753        InstanceKind::FutureDropPollShim(_, ty, ty2) => {
754            return if ty.still_further_specializable() || ty2.still_further_specializable() {
755                Err("still needs substitution")
756            } else {
757                Ok(())
758            };
759        }
760
761        // This cannot result in an immediate cycle since the callee MIR is a shim, which does
762        // not get any optimizations run on it. Any subsequent inlining may cause cycles, but we
763        // do not need to catch this here, we can wait until the inliner decides to continue
764        // inlining a second time.
765        InstanceKind::VTableShim(_)
766        | InstanceKind::ReifyShim(..)
767        | InstanceKind::FnPtrShim(..)
768        | InstanceKind::ClosureOnceShim { .. }
769        | InstanceKind::ConstructCoroutineInClosureShim { .. }
770        | InstanceKind::DropGlue(..)
771        | InstanceKind::CloneShim(..)
772        | InstanceKind::ThreadLocalShim(..)
773        | InstanceKind::FnPtrAddrShim(..) => return Ok(()),
774    }
775
776    if inliner.tcx().is_constructor(callee_def_id) {
777        trace!("constructors always have MIR");
778        // Constructor functions cannot cause a query cycle.
779        return Ok(());
780    }
781
782    if let Some(callee_def_id) = callee_def_id.as_local()
783        && !inliner
784            .tcx()
785            .is_lang_item(inliner.tcx().parent(caller_def_id), rustc_hir::LangItem::FnOnce)
786    {
787        // If we know for sure that the function we're calling will itself try to
788        // call us, then we avoid inlining that function.
789        let Some(cyclic_callees) = inliner.tcx().mir_callgraph_cyclic(caller_def_id.expect_local())
790        else {
791            return Err("call graph cycle detection bailed due to recursion limit");
792        };
793        if cyclic_callees.contains(&callee_def_id) {
794            debug!("query cycle avoidance");
795            return Err("caller might be reachable from callee");
796        }
797
798        Ok(())
799    } else {
800        // This cannot result in an immediate cycle since the callee MIR is from another crate
801        // and is already optimized. Any subsequent inlining may cause cycles, but we do
802        // not need to catch this here, we can wait until the inliner decides to continue
803        // inlining a second time.
804        trace!("functions from other crates always have MIR");
805        Ok(())
806    }
807}
808
809/// Returns an error if inlining is not possible based on codegen attributes alone. A success
810/// indicates that inlining decision should be based on other criteria.
811fn check_codegen_attributes<'tcx, I: Inliner<'tcx>>(
812    inliner: &I,
813    callsite: &CallSite<'tcx>,
814    callee_attrs: &CodegenFnAttrs,
815) -> Result<(), &'static str> {
816    let tcx = inliner.tcx();
817    if let InlineAttr::Never = callee_attrs.inline {
818        return Err("never inline attribute");
819    }
820
821    if let OptimizeAttr::DoNotOptimize = callee_attrs.optimize {
822        return Err("has DoNotOptimize attribute");
823    }
824
825    inliner.check_codegen_attributes_extra(callee_attrs)?;
826
827    // Reachability pass defines which functions are eligible for inlining. Generally inlining
828    // other functions is incorrect because they could reference symbols that aren't exported.
829    let is_generic = callsite.callee.args.non_erasable_generics().next().is_some();
830    if !is_generic && !tcx.cross_crate_inlinable(callsite.callee.def_id()) {
831        return Err("not exported");
832    }
833
834    let codegen_fn_attrs = tcx.codegen_fn_attrs(inliner.caller_def_id());
835    if callee_attrs.sanitizers != codegen_fn_attrs.sanitizers {
836        return Err("incompatible sanitizer set");
837    }
838
839    // Two functions are compatible if the callee has no attribute (meaning
840    // that it's codegen agnostic), or sets an attribute that is identical
841    // to this function's attribute.
842    if callee_attrs.instruction_set.is_some()
843        && callee_attrs.instruction_set != codegen_fn_attrs.instruction_set
844    {
845        return Err("incompatible instruction set");
846    }
847
848    let callee_feature_names = callee_attrs.target_features.iter().map(|f| f.name);
849    let this_feature_names = codegen_fn_attrs.target_features.iter().map(|f| f.name);
850    if callee_feature_names.ne(this_feature_names) {
851        // In general it is not correct to inline a callee with target features that are a
852        // subset of the caller. This is because the callee might contain calls, and the ABI of
853        // those calls depends on the target features of the surrounding function. By moving a
854        // `Call` terminator from one MIR body to another with more target features, we might
855        // change the ABI of that call!
856        return Err("incompatible target features");
857    }
858
859    Ok(())
860}
861
862fn inline_call<'tcx, I: Inliner<'tcx>>(
863    inliner: &I,
864    caller_body: &mut Body<'tcx>,
865    callsite: &CallSite<'tcx>,
866    mut callee_body: Body<'tcx>,
867) {
868    let tcx = inliner.tcx();
869    let terminator = caller_body[callsite.block].terminator.take().unwrap();
870    let TerminatorKind::Call { func, args, destination, unwind, target, .. } = terminator.kind
871    else {
872        bug!("unexpected terminator kind {:?}", terminator.kind);
873    };
874
875    let return_block = if let Some(block) = target {
876        // Prepare a new block for code that should execute when call returns. We don't use
877        // target block directly since it might have other predecessors.
878        let data = BasicBlockData::new(
879            Some(Terminator {
880                source_info: terminator.source_info,
881                kind: TerminatorKind::Goto { target: block },
882                attributes: ThinVec::new(),
883            }),
884            caller_body[block].is_cleanup,
885        );
886        Some(caller_body.basic_blocks_mut().push(data))
887    } else {
888        None
889    };
890
891    // If the call is something like `a[*i] = f(i)`, where
892    // `i : &mut usize`, then just duplicating the `a[*i]`
893    // Place could result in two different locations if `f`
894    // writes to `i`. To prevent this we need to create a temporary
895    // borrow of the place and pass the destination as `*temp` instead.
896    fn dest_needs_borrow(place: Place<'_>) -> bool {
897        for elem in place.projection.iter() {
898            match elem {
899                ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
900                _ => {}
901            }
902        }
903
904        false
905    }
906
907    let dest = if dest_needs_borrow(destination) {
908        trace!("creating temp for return destination");
909        let dest = Rvalue::Ref(
910            tcx.lifetimes.re_erased,
911            BorrowKind::Mut { kind: MutBorrowKind::Default },
912            destination,
913        );
914        let dest_ty = dest.ty(caller_body, tcx);
915        let temp = Place::from(new_call_temp(caller_body, callsite, dest_ty, return_block));
916        caller_body[callsite.block].statements.push(Statement::new(
917            callsite.source_info,
918            StatementKind::Assign(Box::new((temp, dest))),
919        ));
920        tcx.mk_place_deref(temp)
921    } else {
922        destination
923    };
924
925    // Always create a local to hold the destination, as `RETURN_PLACE` may appear
926    // where a full `Place` is not allowed.
927    let (remap_destination, destination_local) = if let Some(d) = dest.as_local() {
928        (false, d)
929    } else {
930        (
931            true,
932            new_call_temp(caller_body, callsite, destination.ty(caller_body, tcx).ty, return_block),
933        )
934    };
935
936    // Copy the arguments if needed.
937    let args = make_call_args(inliner, args, callsite, caller_body, &callee_body, return_block);
938
939    let mut integrator = Integrator {
940        args: &args,
941        new_locals: caller_body.local_decls.next_index()..,
942        new_scopes: caller_body.source_scopes.next_index()..,
943        new_blocks: caller_body.basic_blocks.next_index()..,
944        destination: destination_local,
945        callsite_scope: caller_body.source_scopes[callsite.source_info.scope].clone(),
946        callsite,
947        cleanup_block: unwind,
948        in_cleanup_block: false,
949        return_block,
950        tcx,
951        always_live_locals: UsedInStmtLocals::new(&callee_body).locals,
952    };
953
954    // Map all `Local`s, `SourceScope`s and `BasicBlock`s to new ones
955    // (or existing ones, in a few special cases) in the caller.
956    integrator.visit_body(&mut callee_body);
957
958    // If there are any locals without storage markers, give them storage only for the
959    // duration of the call.
960    for local in callee_body.vars_and_temps_iter() {
961        if integrator.always_live_locals.contains(local) {
962            let new_local = integrator.map_local(local);
963            caller_body[callsite.block]
964                .statements
965                .push(Statement::new(callsite.source_info, StatementKind::StorageLive(new_local)));
966        }
967    }
968    if let Some(block) = return_block {
969        // To avoid repeated O(n) insert, push any new statements to the end and rotate
970        // the slice once.
971        let mut n = 0;
972        if remap_destination {
973            caller_body[block].statements.push(Statement::new(
974                callsite.source_info,
975                StatementKind::Assign(Box::new((
976                    dest,
977                    Rvalue::Use(Operand::Move(destination_local.into()), WithRetag::Yes),
978                ))),
979            ));
980            n += 1;
981        }
982        for local in callee_body.vars_and_temps_iter().rev() {
983            if integrator.always_live_locals.contains(local) {
984                let new_local = integrator.map_local(local);
985                caller_body[block].statements.push(Statement::new(
986                    callsite.source_info,
987                    StatementKind::StorageDead(new_local),
988                ));
989                n += 1;
990            }
991        }
992        caller_body[block].statements.rotate_right(n);
993    }
994
995    // Insert all of the (mapped) parts of the callee body into the caller.
996    caller_body.local_decls.extend(callee_body.drain_vars_and_temps());
997    caller_body.source_scopes.append(&mut callee_body.source_scopes);
998
999    // only "full" debug promises any variable-level information
1000    if tcx
1001        .sess
1002        .opts
1003        .unstable_opts
1004        .inline_mir_preserve_debug
1005        .unwrap_or(tcx.sess.opts.debuginfo == DebugInfo::Full)
1006    {
1007        // -Zinline-mir-preserve-debug is enabled when building the standard library, so that
1008        // people working on rust can build with or without debuginfo while
1009        // still getting consistent results from the mir-opt tests.
1010        caller_body.var_debug_info.append(&mut callee_body.var_debug_info);
1011    } else {
1012        for bb in callee_body.basic_blocks_mut() {
1013            bb.drop_debuginfo();
1014        }
1015    }
1016    caller_body.basic_blocks_mut().append(callee_body.basic_blocks_mut());
1017
1018    caller_body[callsite.block].terminator = Some(Terminator {
1019        source_info: callsite.source_info,
1020        kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
1021        attributes: ThinVec::new(),
1022    });
1023
1024    // Copy required constants from the callee_body into the caller_body. Although we are only
1025    // pushing unevaluated consts to `required_consts`, here they may have been evaluated
1026    // because we are calling `instantiate_and_normalize_erasing_regions` -- so we filter again.
1027    caller_body.required_consts.as_mut().unwrap().extend(
1028        callee_body.required_consts().into_iter().filter(|ct| ct.const_.is_required_const()),
1029    );
1030    // Now that we incorporated the callee's `required_consts`, we can remove the callee from
1031    // `mentioned_items` -- but we have to take their `mentioned_items` in return. This does
1032    // some extra work here to save the monomorphization collector work later. It helps a lot,
1033    // since monomorphization can avoid a lot of work when the "mentioned items" are similar to
1034    // the actually used items. By doing this we can entirely avoid visiting the callee!
1035    // We need to reconstruct the `required_item` for the callee so that we can find and
1036    // remove it.
1037    let callee_item = MentionedItem::Fn(func.ty(caller_body, tcx));
1038    let caller_mentioned_items = caller_body.mentioned_items.as_mut().unwrap();
1039    if let Some(idx) = caller_mentioned_items.iter().position(|item| item.node == callee_item) {
1040        // We found the callee, so remove it and add its items instead.
1041        caller_mentioned_items.remove(idx);
1042        caller_mentioned_items.extend(callee_body.mentioned_items());
1043    } else {
1044        // If we can't find the callee, there's no point in adding its items. Probably it
1045        // already got removed by being inlined elsewhere in the same function, so we already
1046        // took its items.
1047    }
1048}
1049
1050fn make_call_args<'tcx, I: Inliner<'tcx>>(
1051    inliner: &I,
1052    args: Box<[Spanned<Operand<'tcx>>]>,
1053    callsite: &CallSite<'tcx>,
1054    caller_body: &mut Body<'tcx>,
1055    callee_body: &Body<'tcx>,
1056    return_block: Option<BasicBlock>,
1057) -> Box<[Local]> {
1058    let tcx = inliner.tcx();
1059
1060    // There is a bit of a mismatch between the *caller* of a closure and the *callee*.
1061    // The caller provides the arguments wrapped up in a tuple:
1062    //
1063    //     tuple_tmp = (a, b, c)
1064    //     Fn::call(closure_ref, tuple_tmp)
1065    //
1066    // meanwhile the closure body expects the arguments (here, `a`, `b`, and `c`)
1067    // as distinct arguments. (This is the "rust-call" ABI hack.) Normally, codegen has
1068    // the job of unpacking this tuple. But here, we are codegen. =) So we want to create
1069    // a vector like
1070    //
1071    //     [closure_ref, tuple_tmp.0, tuple_tmp.1, tuple_tmp.2]
1072    //
1073    // Except for one tiny wrinkle: we don't actually want `tuple_tmp.0`. It's more convenient
1074    // if we "spill" that into *another* temporary, so that we can map the argument
1075    // variable in the callee MIR directly to an argument variable on our side.
1076    // So we introduce temporaries like:
1077    //
1078    //     tmp0 = tuple_tmp.0
1079    //     tmp1 = tuple_tmp.1
1080    //     tmp2 = tuple_tmp.2
1081    //
1082    // and the vector is `[closure_ref, tmp0, tmp1, tmp2]`.
1083    if callsite.fn_sig.abi() == ExternAbi::RustCall && callee_body.spread_arg.is_none() {
1084        let mut args = args.into_iter();
1085        let self_ = create_temp_if_necessary(
1086            inliner,
1087            args.next().unwrap().node,
1088            callsite,
1089            caller_body,
1090            return_block,
1091        );
1092        let tuple = create_temp_if_necessary(
1093            inliner,
1094            args.next().unwrap().node,
1095            callsite,
1096            caller_body,
1097            return_block,
1098        );
1099        assert!(args.next().is_none());
1100
1101        let tuple = Place::from(tuple);
1102        let ty::Tuple(tuple_tys) = tuple.ty(caller_body, tcx).ty.kind() else {
1103            bug!("Closure arguments are not passed as a tuple");
1104        };
1105
1106        // The `closure_ref` in our example above.
1107        let closure_ref_arg = iter::once(self_);
1108
1109        // The `tmp0`, `tmp1`, and `tmp2` in our example above.
1110        let tuple_tmp_args = tuple_tys.iter().enumerate().map(|(i, ty)| {
1111            // This is e.g., `tuple_tmp.0` in our example above.
1112            let tuple_field = Operand::Move(tcx.mk_place_field(tuple, FieldIdx::new(i), ty));
1113
1114            // Spill to a local to make e.g., `tmp0`.
1115            create_temp_if_necessary(inliner, tuple_field, callsite, caller_body, return_block)
1116        });
1117
1118        closure_ref_arg.chain(tuple_tmp_args).collect()
1119    } else {
1120        args.into_iter()
1121            .map(|a| create_temp_if_necessary(inliner, a.node, callsite, caller_body, return_block))
1122            .collect()
1123    }
1124}
1125
1126/// If `arg` is already a temporary, returns it. Otherwise, introduces a fresh temporary `T` and an
1127/// instruction `T = arg`, and returns `T`.
1128fn create_temp_if_necessary<'tcx, I: Inliner<'tcx>>(
1129    inliner: &I,
1130    arg: Operand<'tcx>,
1131    callsite: &CallSite<'tcx>,
1132    caller_body: &mut Body<'tcx>,
1133    return_block: Option<BasicBlock>,
1134) -> Local {
1135    // Reuse the operand if it is a moved temporary.
1136    if let Operand::Move(place) = &arg
1137        && let Some(local) = place.as_local()
1138        && caller_body.local_kind(local) == LocalKind::Temp
1139    {
1140        return local;
1141    }
1142
1143    // Otherwise, create a temporary for the argument.
1144    trace!("creating temp for argument {:?}", arg);
1145    let arg_ty = arg.ty(caller_body, inliner.tcx());
1146    let local = new_call_temp(caller_body, callsite, arg_ty, return_block);
1147    caller_body[callsite.block].statements.push(Statement::new(
1148        callsite.source_info,
1149        StatementKind::Assign(Box::new((Place::from(local), Rvalue::Use(arg, WithRetag::Yes)))),
1150    ));
1151    local
1152}
1153
1154/// Introduces a new temporary into the caller body that is live for the duration of the call.
1155fn new_call_temp<'tcx>(
1156    caller_body: &mut Body<'tcx>,
1157    callsite: &CallSite<'tcx>,
1158    ty: Ty<'tcx>,
1159    return_block: Option<BasicBlock>,
1160) -> Local {
1161    let local = caller_body.local_decls.push(LocalDecl::new(ty, callsite.source_info.span));
1162
1163    caller_body[callsite.block]
1164        .statements
1165        .push(Statement::new(callsite.source_info, StatementKind::StorageLive(local)));
1166
1167    if let Some(block) = return_block {
1168        caller_body[block]
1169            .statements
1170            .insert(0, Statement::new(callsite.source_info, StatementKind::StorageDead(local)));
1171    }
1172
1173    local
1174}
1175
1176/**
1177 * Integrator.
1178 *
1179 * Integrates blocks from the callee function into the calling function.
1180 * Updates block indices, references to locals and other control flow
1181 * stuff.
1182*/
1183struct Integrator<'a, 'tcx> {
1184    args: &'a [Local],
1185    new_locals: RangeFrom<Local>,
1186    new_scopes: RangeFrom<SourceScope>,
1187    new_blocks: RangeFrom<BasicBlock>,
1188    destination: Local,
1189    callsite_scope: SourceScopeData<'tcx>,
1190    callsite: &'a CallSite<'tcx>,
1191    cleanup_block: UnwindAction,
1192    in_cleanup_block: bool,
1193    return_block: Option<BasicBlock>,
1194    tcx: TyCtxt<'tcx>,
1195    always_live_locals: DenseBitSet<Local>,
1196}
1197
1198impl Integrator<'_, '_> {
1199    fn map_local(&self, local: Local) -> Local {
1200        let new = if local == RETURN_PLACE {
1201            self.destination
1202        } else {
1203            let idx = local.index() - 1;
1204            if idx < self.args.len() {
1205                self.args[idx]
1206            } else {
1207                self.new_locals.start + (idx - self.args.len())
1208            }
1209        };
1210        trace!("mapping local `{:?}` to `{:?}`", local, new);
1211        new
1212    }
1213
1214    fn map_scope(&self, scope: SourceScope) -> SourceScope {
1215        let new = self.new_scopes.start + scope.index();
1216        trace!("mapping scope `{:?}` to `{:?}`", scope, new);
1217        new
1218    }
1219
1220    fn map_block(&self, block: BasicBlock) -> BasicBlock {
1221        let new = self.new_blocks.start + block.index();
1222        trace!("mapping block `{:?}` to `{:?}`", block, new);
1223        new
1224    }
1225
1226    fn map_unwind(&self, unwind: UnwindAction) -> UnwindAction {
1227        if self.in_cleanup_block {
1228            match unwind {
1229                UnwindAction::Cleanup(_) | UnwindAction::Continue => {
1230                    bug!("cleanup on cleanup block");
1231                }
1232                UnwindAction::Unreachable | UnwindAction::Terminate(_) => return unwind,
1233            }
1234        }
1235
1236        match unwind {
1237            UnwindAction::Unreachable | UnwindAction::Terminate(_) => unwind,
1238            UnwindAction::Cleanup(target) => UnwindAction::Cleanup(self.map_block(target)),
1239            // Add an unwind edge to the original call's cleanup block
1240            UnwindAction::Continue => self.cleanup_block,
1241        }
1242    }
1243}
1244
1245impl<'tcx> MutVisitor<'tcx> for Integrator<'_, 'tcx> {
1246    fn tcx(&self) -> TyCtxt<'tcx> {
1247        self.tcx
1248    }
1249
1250    fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) {
1251        *local = self.map_local(*local);
1252    }
1253
1254    fn visit_source_scope_data(&mut self, scope_data: &mut SourceScopeData<'tcx>) {
1255        self.super_source_scope_data(scope_data);
1256        if scope_data.parent_scope.is_none() {
1257            // Attach the outermost callee scope as a child of the callsite
1258            // scope, via the `parent_scope` and `inlined_parent_scope` chains.
1259            scope_data.parent_scope = Some(self.callsite.source_info.scope);
1260            assert_eq!(scope_data.inlined_parent_scope, None);
1261            scope_data.inlined_parent_scope = if self.callsite_scope.inlined.is_some() {
1262                Some(self.callsite.source_info.scope)
1263            } else {
1264                self.callsite_scope.inlined_parent_scope
1265            };
1266
1267            // Mark the outermost callee scope as an inlined one.
1268            assert_eq!(scope_data.inlined, None);
1269            scope_data.inlined = Some((self.callsite.callee, self.callsite.source_info.span));
1270        } else if scope_data.inlined_parent_scope.is_none() {
1271            // Make it easy to find the scope with `inlined` set above.
1272            scope_data.inlined_parent_scope = Some(self.map_scope(OUTERMOST_SOURCE_SCOPE));
1273        }
1274    }
1275
1276    fn visit_source_scope(&mut self, scope: &mut SourceScope) {
1277        *scope = self.map_scope(*scope);
1278    }
1279
1280    fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
1281        self.in_cleanup_block = data.is_cleanup;
1282        self.super_basic_block_data(block, data);
1283        self.in_cleanup_block = false;
1284    }
1285
1286    fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
1287        if let StatementKind::StorageLive(local) | StatementKind::StorageDead(local) =
1288            statement.kind
1289        {
1290            self.always_live_locals.remove(local);
1291        }
1292        self.super_statement(statement, location);
1293    }
1294
1295    fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, loc: Location) {
1296        // Don't try to modify the implicit `_0` access on return (`return` terminators are
1297        // replaced down below anyways).
1298        if !matches!(terminator.kind, TerminatorKind::Return) {
1299            self.super_terminator(terminator, loc);
1300        } else {
1301            self.visit_source_info(&mut terminator.source_info);
1302        }
1303
1304        match terminator.kind {
1305            TerminatorKind::CoroutineDrop | TerminatorKind::Yield { .. } => bug!(),
1306            TerminatorKind::Goto { ref mut target } => {
1307                *target = self.map_block(*target);
1308            }
1309            TerminatorKind::SwitchInt { ref mut targets, .. } => {
1310                for tgt in targets.all_targets_mut() {
1311                    *tgt = self.map_block(*tgt);
1312                }
1313            }
1314            TerminatorKind::Drop { ref mut target, ref mut unwind, .. } => {
1315                *target = self.map_block(*target);
1316                *unwind = self.map_unwind(*unwind);
1317            }
1318            TerminatorKind::TailCall { .. } => {
1319                // check_mir_body forbids tail calls
1320                unreachable!()
1321            }
1322            TerminatorKind::Call { ref mut target, ref mut unwind, .. } => {
1323                if let Some(ref mut tgt) = *target {
1324                    *tgt = self.map_block(*tgt);
1325                }
1326                *unwind = self.map_unwind(*unwind);
1327            }
1328            TerminatorKind::Assert { ref mut target, ref mut unwind, .. } => {
1329                *target = self.map_block(*target);
1330                *unwind = self.map_unwind(*unwind);
1331            }
1332            TerminatorKind::Return => {
1333                terminator.kind = if let Some(tgt) = self.return_block {
1334                    TerminatorKind::Goto { target: tgt }
1335                } else {
1336                    TerminatorKind::Unreachable
1337                }
1338            }
1339            TerminatorKind::UnwindResume => {
1340                terminator.kind = match self.cleanup_block {
1341                    UnwindAction::Cleanup(tgt) => TerminatorKind::Goto { target: tgt },
1342                    UnwindAction::Continue => TerminatorKind::UnwindResume,
1343                    UnwindAction::Unreachable => TerminatorKind::Unreachable,
1344                    UnwindAction::Terminate(reason) => TerminatorKind::UnwindTerminate(reason),
1345                };
1346            }
1347            TerminatorKind::UnwindTerminate(_) => {}
1348            TerminatorKind::Unreachable => {}
1349            TerminatorKind::FalseEdge { ref mut real_target, ref mut imaginary_target } => {
1350                *real_target = self.map_block(*real_target);
1351                *imaginary_target = self.map_block(*imaginary_target);
1352            }
1353            TerminatorKind::FalseUnwind { real_target: _, unwind: _ } =>
1354            // see the ordering of passes in the optimized_mir query.
1355            {
1356                bug!("False unwinds should have been removed before inlining")
1357            }
1358            TerminatorKind::InlineAsm { ref mut targets, ref mut unwind, .. } => {
1359                for tgt in targets.iter_mut() {
1360                    *tgt = self.map_block(*tgt);
1361                }
1362                *unwind = self.map_unwind(*unwind);
1363            }
1364        }
1365    }
1366}
1367
1368#[instrument(skip(tcx), level = "debug")]
1369fn try_instance_mir<'tcx>(
1370    tcx: TyCtxt<'tcx>,
1371    instance: InstanceKind<'tcx>,
1372) -> Result<&'tcx Body<'tcx>, &'static str> {
1373    if let ty::InstanceKind::DropGlue(_, Some(ty)) | ty::InstanceKind::AsyncDropGlueCtorShim(_, ty) =
1374        instance
1375        && let ty::Adt(def, args) = ty.kind()
1376    {
1377        let fields = def.all_fields();
1378        for field in fields {
1379            let field_ty = field.ty(tcx, args);
1380            if field_ty.has_param() && field_ty.has_aliases() {
1381                return Err("cannot build drop shim for polymorphic type");
1382            }
1383        }
1384    }
1385    Ok(tcx.instance_mir(instance))
1386}
1387
1388fn body_is_forwarder(body: &Body<'_>) -> bool {
1389    let TerminatorKind::Call { target, .. } = body.basic_blocks[START_BLOCK].terminator().kind
1390    else {
1391        return false;
1392    };
1393    if let Some(target) = target {
1394        let TerminatorKind::Return = body.basic_blocks[target].terminator().kind else {
1395            return false;
1396        };
1397    }
1398
1399    let max_blocks = if !body.is_polymorphic {
1400        2
1401    } else if target.is_none() {
1402        3
1403    } else {
1404        4
1405    };
1406    if body.basic_blocks.len() > max_blocks {
1407        return false;
1408    }
1409
1410    body.basic_blocks.iter_enumerated().all(|(bb, bb_data)| {
1411        bb == START_BLOCK
1412            || matches!(
1413                bb_data.terminator().kind,
1414                TerminatorKind::Return
1415                    | TerminatorKind::Drop { .. }
1416                    | TerminatorKind::UnwindResume
1417                    | TerminatorKind::UnwindTerminate(_)
1418            )
1419    })
1420}