1use std::ops::{Range, RangeFrom};
4use std::{debug_assert_matches, iter};
5
6use rustc_abi::{ExternAbi, FieldIdx};
7use rustc_hir::attrs::{InlineAttr, OptimizeAttr};
8use rustc_hir::def::DefKind;
9use rustc_hir::def_id::DefId;
10use rustc_index::Idx;
11use rustc_index::bit_set::DenseBitSet;
12use rustc_middle::bug;
13use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
14use rustc_middle::mir::visit::*;
15use rustc_middle::mir::*;
16use rustc_middle::ty::{
17 self, Instance, InstanceKind, Ty, TyCtxt, TypeFlags, TypeVisitableExt, Unnormalized,
18};
19use rustc_session::config::{DebugInfo, OptLevel};
20use rustc_span::Spanned;
21use tracing::{debug, instrument, trace, trace_span};
22
23use crate::cost_checker::{CostChecker, is_call_like};
24use crate::simplify::{UsedInStmtLocals, 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(tcx, body);
67 }
68 }
69
70 fn is_required(&self) -> bool {
71 false
72 }
73}
74
75pub struct ForceInline;
76
77impl ForceInline {
78 pub fn should_run_pass_for_callee<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
79 matches!(tcx.codegen_fn_attrs(def_id).inline, InlineAttr::Force { .. })
80 }
81}
82
83impl<'tcx> crate::MirPass<'tcx> for ForceInline {
84 fn is_enabled(&self, _: &rustc_session::Session) -> bool {
85 true
86 }
87
88 fn can_be_overridden(&self) -> bool {
89 false
90 }
91
92 fn is_required(&self) -> bool {
93 true
94 }
95
96 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
97 let span = trace_span!("force_inline", body = %tcx.def_path_str(body.source.def_id()));
98 let _guard = span.enter();
99 if inline::<ForceInliner<'tcx>>(tcx, body) {
100 debug!("running simplify cfg on {:?}", body.source);
101 simplify_cfg(tcx, body);
102 }
103 }
104}
105
106trait Inliner<'tcx> {
107 fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> Self;
108
109 fn tcx(&self) -> TyCtxt<'tcx>;
110 fn typing_env(&self) -> ty::TypingEnv<'tcx>;
111 fn history(&self) -> &[DefId];
112 fn caller_def_id(&self) -> DefId;
113
114 fn changed(self) -> bool;
116
117 fn should_inline_for_callee(&self, def_id: DefId) -> bool;
119
120 fn check_codegen_attributes_extra(
121 &self,
122 callee_attrs: &CodegenFnAttrs,
123 ) -> Result<(), &'static str>;
124
125 fn check_caller_mir_body(&self, body: &Body<'tcx>) -> bool;
126
127 fn check_callee_mir_body(
130 &self,
131 callsite: &CallSite<'tcx>,
132 callee_body: &Body<'tcx>,
133 callee_attrs: &CodegenFnAttrs,
134 ) -> Result<(), &'static str>;
135
136 fn on_inline_success(
138 &mut self,
139 callsite: &CallSite<'tcx>,
140 caller_body: &mut Body<'tcx>,
141 new_blocks: std::ops::Range<BasicBlock>,
142 );
143
144 fn on_inline_failure(&self, callsite: &CallSite<'tcx>, reason: &'static str);
146}
147
148struct ForceInliner<'tcx> {
149 tcx: TyCtxt<'tcx>,
150 typing_env: ty::TypingEnv<'tcx>,
151 def_id: DefId,
153 history: Vec<DefId>,
159 changed: bool,
161}
162
163impl<'tcx> Inliner<'tcx> for ForceInliner<'tcx> {
164 fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> Self {
165 Self { tcx, typing_env: body.typing_env(tcx), def_id, history: Vec::new(), changed: false }
166 }
167
168 fn tcx(&self) -> TyCtxt<'tcx> {
169 self.tcx
170 }
171
172 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
173 self.typing_env
174 }
175
176 fn history(&self) -> &[DefId] {
177 &self.history
178 }
179
180 fn caller_def_id(&self) -> DefId {
181 self.def_id
182 }
183
184 fn changed(self) -> bool {
185 self.changed
186 }
187
188 fn should_inline_for_callee(&self, def_id: DefId) -> bool {
189 ForceInline::should_run_pass_for_callee(self.tcx(), def_id)
190 }
191
192 fn check_codegen_attributes_extra(
193 &self,
194 callee_attrs: &CodegenFnAttrs,
195 ) -> Result<(), &'static str> {
196 debug_assert_matches!(callee_attrs.inline, InlineAttr::Force { .. });
197 Ok(())
198 }
199
200 fn check_caller_mir_body(&self, _: &Body<'tcx>) -> bool {
201 true
202 }
203
204 #[instrument(level = "debug", skip(self, callee_body))]
205 fn check_callee_mir_body(
206 &self,
207 _: &CallSite<'tcx>,
208 callee_body: &Body<'tcx>,
209 callee_attrs: &CodegenFnAttrs,
210 ) -> Result<(), &'static str> {
211 if callee_body.tainted_by_errors.is_some() {
212 return Err("body has errors");
213 }
214
215 let caller_attrs = self.tcx().codegen_fn_attrs(self.caller_def_id());
216 if callee_attrs.instruction_set != caller_attrs.instruction_set
217 && callee_body
218 .basic_blocks
219 .iter()
220 .any(|bb| matches!(bb.terminator().kind, TerminatorKind::InlineAsm { .. }))
221 {
222 Err("cannot move inline-asm across instruction sets")
228 } else {
229 Ok(())
230 }
231 }
232
233 fn on_inline_success(
234 &mut self,
235 callsite: &CallSite<'tcx>,
236 caller_body: &mut Body<'tcx>,
237 new_blocks: std::ops::Range<BasicBlock>,
238 ) {
239 self.changed = true;
240
241 self.history.push(callsite.callee.def_id());
242 process_blocks(self, caller_body, new_blocks);
243 self.history.pop();
244 }
245
246 fn on_inline_failure(&self, callsite: &CallSite<'tcx>, reason: &'static str) {
247 let tcx = self.tcx();
248 let InlineAttr::Force { attr_span, reason: justification } =
249 tcx.codegen_instance_attrs(callsite.callee.def).inline
250 else {
251 bug!("called on item without required inlining");
252 };
253
254 let call_span = callsite.source_info.span;
255 let callee = tcx.def_path_str(callsite.callee.def_id());
256 tcx.dcx().emit_err(crate::errors::ForceInlineFailure {
257 call_span,
258 attr_span,
259 caller_span: tcx.def_span(self.def_id),
260 caller: tcx.def_path_str(self.def_id),
261 callee_span: tcx.def_span(callsite.callee.def_id()),
262 callee: callee.clone(),
263 reason,
264 justification: justification
265 .map(|sym| crate::errors::ForceInlineJustification { sym, callee }),
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 {
417 ref place,
418 target,
419 unwind,
420 replace: _,
421 drop: _,
422 async_fut: _,
423 } = term.kind
424 {
425 work_list.push(target);
426
427 let ty = callsite
429 .callee
430 .instantiate_mir(tcx, ty::EarlyBinder::bind(&place.ty(callee_body, tcx).ty));
431 if ty.needs_drop(tcx, self.typing_env())
432 && let UnwindAction::Cleanup(unwind) = unwind
433 {
434 work_list.push(unwind);
435 }
436 } else if callee_attrs.instruction_set != caller_attrs.instruction_set
437 && matches!(term.kind, TerminatorKind::InlineAsm { .. })
438 {
439 return Err("cannot move inline-asm across instruction sets");
445 } else if let TerminatorKind::TailCall { .. } = term.kind {
446 return Err("can't inline functions with tail calls");
449 } else {
450 work_list.extend(term.successors())
451 }
452 }
453
454 let cost = checker.cost();
459 if cost <= threshold {
460 debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
461 Ok(())
462 } else {
463 debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
464 Err("cost above threshold")
465 }
466 }
467
468 fn on_inline_success(
469 &mut self,
470 callsite: &CallSite<'tcx>,
471 caller_body: &mut Body<'tcx>,
472 new_blocks: std::ops::Range<BasicBlock>,
473 ) {
474 self.changed = true;
475
476 let new_calls_count = new_blocks
477 .clone()
478 .filter(|&bb| is_call_like(caller_body.basic_blocks[bb].terminator()))
479 .count();
480 if new_calls_count > 1 {
481 self.top_down_counter += 1;
482 }
483
484 self.history.push(callsite.callee.def_id());
485 process_blocks(self, caller_body, new_blocks);
486 self.history.pop();
487
488 if self.history.is_empty() {
489 self.top_down_counter = 0;
490 }
491 }
492
493 fn on_inline_failure(&self, _: &CallSite<'tcx>, _: &'static str) {}
494}
495
496fn inline<'tcx, T: Inliner<'tcx>>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
497 let def_id = body.source.def_id();
498
499 if !tcx.hir_body_owner_kind(def_id).is_fn_or_closure() {
501 return false;
502 }
503
504 let mut inliner = T::new(tcx, def_id, body);
505 if !inliner.check_caller_mir_body(body) {
506 return false;
507 }
508
509 let blocks = START_BLOCK..body.basic_blocks.next_index();
510 process_blocks(&mut inliner, body, blocks);
511 inliner.changed()
512}
513
514fn process_blocks<'tcx, I: Inliner<'tcx>>(
515 inliner: &mut I,
516 caller_body: &mut Body<'tcx>,
517 blocks: Range<BasicBlock>,
518) {
519 for bb in blocks {
520 let bb_data = &caller_body[bb];
521 if bb_data.is_cleanup {
522 continue;
523 }
524
525 let Some(callsite) = resolve_callsite(inliner, caller_body, bb, bb_data) else {
526 continue;
527 };
528
529 let span = trace_span!("process_blocks", %callsite.callee, ?bb);
530 let _guard = span.enter();
531
532 match try_inlining(inliner, caller_body, &callsite) {
533 Err(reason) => {
534 debug!("not-inlined {} [{}]", callsite.callee, reason);
535 inliner.on_inline_failure(&callsite, reason);
536 }
537 Ok(new_blocks) => {
538 debug!("inlined {}", callsite.callee);
539 inliner.on_inline_success(&callsite, caller_body, new_blocks);
540 }
541 }
542 }
543}
544
545fn resolve_callsite<'tcx, I: Inliner<'tcx>>(
546 inliner: &I,
547 caller_body: &Body<'tcx>,
548 bb: BasicBlock,
549 bb_data: &BasicBlockData<'tcx>,
550) -> Option<CallSite<'tcx>> {
551 let tcx = inliner.tcx();
552 let terminator = bb_data.terminator();
554
555 if let TerminatorKind::Call { ref func, fn_span, .. } = terminator.kind {
557 let func_ty = func.ty(caller_body, tcx);
558 if let ty::FnDef(def_id, args) = *func_ty.kind() {
559 if !inliner.should_inline_for_callee(def_id) {
560 debug!("not enabled");
561 return None;
562 }
563
564 let args = tcx
566 .try_normalize_erasing_regions(inliner.typing_env(), Unnormalized::new_wip(args))
567 .ok()?;
568 let mut callee =
569 Instance::try_resolve(tcx, inliner.typing_env(), def_id, args).ok().flatten()?;
570
571 if let InstanceKind::Virtual(..) = callee.def {
572 return None;
573 }
574 if let InstanceKind::Intrinsic(..) = callee.def {
575 let intrinsic = tcx.intrinsic(def_id).unwrap();
576 if intrinsic.must_be_overridden {
577 return None; }
579 if !tcx.sess.fallback_intrinsics.contains(&intrinsic.name) {
580 return None; }
582 debug!("callsite is fallback body: {def_id:?}");
584 callee = ty::Instance { def: ty::InstanceKind::Item(def_id), args: callee.args };
585 }
586
587 if inliner.history().contains(&callee.def_id()) {
588 return None;
589 }
590
591 let fn_sig = tcx.fn_sig(def_id).instantiate(tcx, args).skip_norm_wip();
592
593 if let InstanceKind::Item(instance_def_id) = callee.def
596 && tcx.def_kind(instance_def_id) == DefKind::AssocFn
597 && let instance_fn_sig = tcx.fn_sig(instance_def_id).skip_binder()
598 && instance_fn_sig.abi() != fn_sig.abi()
599 {
600 return None;
601 }
602
603 let source_info = SourceInfo { span: fn_span, ..terminator.source_info };
604
605 return Some(CallSite { callee, fn_sig, block: bb, source_info });
606 }
607 }
608
609 None
610}
611
612fn try_inlining<'tcx, I: Inliner<'tcx>>(
616 inliner: &I,
617 caller_body: &mut Body<'tcx>,
618 callsite: &CallSite<'tcx>,
619) -> Result<std::ops::Range<BasicBlock>, &'static str> {
620 let tcx = inliner.tcx();
621 check_mir_is_available(inliner, caller_body, callsite.callee)?;
622
623 let callee_attrs = tcx.codegen_instance_attrs(callsite.callee.def);
624 let callee_attrs = callee_attrs.as_ref();
625 check_inline::is_inline_valid_on_fn(tcx, callsite.callee.def_id())?;
626 check_codegen_attributes(inliner, callsite, callee_attrs)?;
627
628 let terminator = caller_body[callsite.block].terminator.as_ref().unwrap();
629 let TerminatorKind::Call { args, destination, .. } = &terminator.kind else { bug!() };
630 let destination_ty = destination.ty(&caller_body.local_decls, tcx).ty;
631 for arg in args {
632 if !arg.node.ty(&caller_body.local_decls, tcx).is_sized(tcx, inliner.typing_env()) {
633 return Err("call has unsized argument");
636 }
637 }
638
639 let callee_body = try_instance_mir(tcx, callsite.callee.def)?;
640 check_inline::is_inline_valid_on_body(tcx, callee_body)?;
641 inliner.check_callee_mir_body(callsite, callee_body, callee_attrs)?;
642
643 let Ok(callee_body) = callsite.callee.try_instantiate_mir_and_normalize_erasing_regions(
644 tcx,
645 inliner.typing_env(),
646 ty::EarlyBinder::bind(callee_body.clone()),
647 ) else {
648 debug!("failed to normalize callee body");
649 return Err("implementation limitation -- could not normalize callee body");
650 };
651
652 if !validate_types(tcx, inliner.typing_env(), &callee_body, caller_body).is_empty() {
655 debug!("failed to validate callee body");
656 return Err("implementation limitation -- callee body failed validation");
657 }
658
659 let output_type = callee_body.return_ty();
663 if !util::sub_types(tcx, inliner.typing_env(), output_type, destination_ty) {
664 trace!(?output_type, ?destination_ty);
665 return Err("implementation limitation -- return type mismatch");
666 }
667 if callsite.fn_sig.abi() == ExternAbi::RustCall {
668 let (self_arg, arg_tuple) = match &args[..] {
669 [arg_tuple] => (None, arg_tuple),
670 [self_arg, arg_tuple] => (Some(self_arg), arg_tuple),
671 _ => bug!("Expected `rust-call` to have 1 or 2 args"),
672 };
673
674 let self_arg_ty = self_arg.map(|self_arg| self_arg.node.ty(&caller_body.local_decls, tcx));
675
676 let arg_tuple_ty = arg_tuple.node.ty(&caller_body.local_decls, tcx);
677 let arg_tys = if callee_body.spread_arg.is_some() {
678 std::slice::from_ref(&arg_tuple_ty)
679 } else {
680 let ty::Tuple(arg_tuple_tys) = *arg_tuple_ty.kind() else {
681 bug!("Closure arguments are not passed as a tuple");
682 };
683 arg_tuple_tys.as_slice()
684 };
685
686 for (arg_ty, input) in
687 self_arg_ty.into_iter().chain(arg_tys.iter().copied()).zip(callee_body.args_iter())
688 {
689 let input_type = callee_body.local_decls[input].ty;
690 if !util::sub_types(tcx, inliner.typing_env(), input_type, arg_ty) {
691 trace!(?arg_ty, ?input_type);
692 debug!("failed to normalize tuple argument type");
693 return Err("implementation limitation");
694 }
695 }
696 } else {
697 for (arg, input) in args.iter().zip(callee_body.args_iter()) {
698 let input_type = callee_body.local_decls[input].ty;
699 let arg_ty = arg.node.ty(&caller_body.local_decls, tcx);
700 if !util::sub_types(tcx, inliner.typing_env(), input_type, arg_ty) {
701 trace!(?arg_ty, ?input_type);
702 debug!("failed to normalize argument type");
703 return Err("implementation limitation -- arg mismatch");
704 }
705 }
706 }
707
708 let old_blocks = caller_body.basic_blocks.next_index();
709 inline_call(inliner, caller_body, callsite, callee_body);
710 let new_blocks = old_blocks..caller_body.basic_blocks.next_index();
711
712 Ok(new_blocks)
713}
714
715fn check_mir_is_available<'tcx, I: Inliner<'tcx>>(
716 inliner: &I,
717 caller_body: &Body<'tcx>,
718 callee: Instance<'tcx>,
719) -> Result<(), &'static str> {
720 let caller_def_id = caller_body.source.def_id();
721 let callee_def_id = callee.def_id();
722 if callee_def_id == caller_def_id {
723 return Err("self-recursion");
724 }
725
726 match callee.def {
727 InstanceKind::Item(_) => {
728 if !inliner.tcx().is_mir_available(callee_def_id) {
732 debug!("item MIR unavailable");
733 return Err("implementation limitation -- MIR unavailable");
734 }
735 }
736 InstanceKind::Intrinsic(_) | InstanceKind::Virtual(..) => {
738 debug!("instance without MIR (intrinsic / virtual)");
739 return Err("implementation limitation -- cannot inline intrinsic");
740 }
741
742 InstanceKind::DropGlue(_, Some(ty)) if ty.has_type_flags(TypeFlags::HAS_CT_PARAM) => {
748 debug!("still needs substitution");
749 return Err("implementation limitation -- HACK for dropping polymorphic type");
750 }
751 InstanceKind::AsyncDropGlue(_, ty) | InstanceKind::AsyncDropGlueCtorShim(_, ty) => {
752 return if ty.still_further_specializable() {
753 Err("still needs substitution")
754 } else {
755 Ok(())
756 };
757 }
758 InstanceKind::FutureDropPollShim(_, ty, ty2) => {
759 return if ty.still_further_specializable() || ty2.still_further_specializable() {
760 Err("still needs substitution")
761 } else {
762 Ok(())
763 };
764 }
765
766 InstanceKind::VTableShim(_)
771 | InstanceKind::ReifyShim(..)
772 | InstanceKind::FnPtrShim(..)
773 | InstanceKind::ClosureOnceShim { .. }
774 | InstanceKind::ConstructCoroutineInClosureShim { .. }
775 | InstanceKind::DropGlue(..)
776 | InstanceKind::CloneShim(..)
777 | InstanceKind::ThreadLocalShim(..)
778 | InstanceKind::FnPtrAddrShim(..) => return Ok(()),
779 }
780
781 if inliner.tcx().is_constructor(callee_def_id) {
782 trace!("constructors always have MIR");
783 return Ok(());
785 }
786
787 if let Some(callee_def_id) = callee_def_id.as_local()
788 && !inliner
789 .tcx()
790 .is_lang_item(inliner.tcx().parent(caller_def_id), rustc_hir::LangItem::FnOnce)
791 {
792 let Some(cyclic_callees) = inliner.tcx().mir_callgraph_cyclic(caller_def_id.expect_local())
795 else {
796 return Err("call graph cycle detection bailed due to recursion limit");
797 };
798 if cyclic_callees.contains(&callee_def_id) {
799 debug!("query cycle avoidance");
800 return Err("caller might be reachable from callee");
801 }
802
803 Ok(())
804 } else {
805 trace!("functions from other crates always have MIR");
810 Ok(())
811 }
812}
813
814fn check_codegen_attributes<'tcx, I: Inliner<'tcx>>(
817 inliner: &I,
818 callsite: &CallSite<'tcx>,
819 callee_attrs: &CodegenFnAttrs,
820) -> Result<(), &'static str> {
821 let tcx = inliner.tcx();
822 if let InlineAttr::Never = callee_attrs.inline {
823 return Err("never inline attribute");
824 }
825
826 if let OptimizeAttr::DoNotOptimize = callee_attrs.optimize {
827 return Err("has DoNotOptimize attribute");
828 }
829
830 inliner.check_codegen_attributes_extra(callee_attrs)?;
831
832 let is_generic = callsite.callee.args.non_erasable_generics().next().is_some();
835 if !is_generic && !tcx.cross_crate_inlinable(callsite.callee.def_id()) {
836 return Err("not exported");
837 }
838
839 let codegen_fn_attrs = tcx.codegen_fn_attrs(inliner.caller_def_id());
840 if callee_attrs.sanitizers != codegen_fn_attrs.sanitizers {
841 return Err("incompatible sanitizer set");
842 }
843
844 if callee_attrs.instruction_set.is_some()
848 && callee_attrs.instruction_set != codegen_fn_attrs.instruction_set
849 {
850 return Err("incompatible instruction set");
851 }
852
853 let callee_feature_names = callee_attrs.target_features.iter().map(|f| f.name);
854 let this_feature_names = codegen_fn_attrs.target_features.iter().map(|f| f.name);
855 if callee_feature_names.ne(this_feature_names) {
856 return Err("incompatible target features");
862 }
863
864 Ok(())
865}
866
867fn inline_call<'tcx, I: Inliner<'tcx>>(
868 inliner: &I,
869 caller_body: &mut Body<'tcx>,
870 callsite: &CallSite<'tcx>,
871 mut callee_body: Body<'tcx>,
872) {
873 let tcx = inliner.tcx();
874 let terminator = caller_body[callsite.block].terminator.take().unwrap();
875 let TerminatorKind::Call { func, args, destination, unwind, target, .. } = terminator.kind
876 else {
877 bug!("unexpected terminator kind {:?}", terminator.kind);
878 };
879
880 let return_block = if let Some(block) = target {
881 let data = BasicBlockData::new(
884 Some(Terminator {
885 source_info: terminator.source_info,
886 kind: TerminatorKind::Goto { target: block },
887 }),
888 caller_body[block].is_cleanup,
889 );
890 Some(caller_body.basic_blocks_mut().push(data))
891 } else {
892 None
893 };
894
895 fn dest_needs_borrow(place: Place<'_>) -> bool {
901 for elem in place.projection.iter() {
902 match elem {
903 ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
904 _ => {}
905 }
906 }
907
908 false
909 }
910
911 let dest = if dest_needs_borrow(destination) {
912 trace!("creating temp for return destination");
913 let dest = Rvalue::Ref(
914 tcx.lifetimes.re_erased,
915 BorrowKind::Mut { kind: MutBorrowKind::Default },
916 destination,
917 );
918 let dest_ty = dest.ty(caller_body, tcx);
919 let temp = Place::from(new_call_temp(caller_body, callsite, dest_ty, return_block));
920 caller_body[callsite.block].statements.push(Statement::new(
921 callsite.source_info,
922 StatementKind::Assign(Box::new((temp, dest))),
923 ));
924 tcx.mk_place_deref(temp)
925 } else {
926 destination
927 };
928
929 let (remap_destination, destination_local) = if let Some(d) = dest.as_local() {
932 (false, d)
933 } else {
934 (
935 true,
936 new_call_temp(caller_body, callsite, destination.ty(caller_body, tcx).ty, return_block),
937 )
938 };
939
940 let args = make_call_args(inliner, args, callsite, caller_body, &callee_body, return_block);
942
943 let mut integrator = Integrator {
944 args: &args,
945 new_locals: caller_body.local_decls.next_index()..,
946 new_scopes: caller_body.source_scopes.next_index()..,
947 new_blocks: caller_body.basic_blocks.next_index()..,
948 destination: destination_local,
949 callsite_scope: caller_body.source_scopes[callsite.source_info.scope].clone(),
950 callsite,
951 cleanup_block: unwind,
952 in_cleanup_block: false,
953 return_block,
954 tcx,
955 always_live_locals: UsedInStmtLocals::new(&callee_body).locals,
956 };
957
958 integrator.visit_body(&mut callee_body);
961
962 for local in callee_body.vars_and_temps_iter() {
965 if integrator.always_live_locals.contains(local) {
966 let new_local = integrator.map_local(local);
967 caller_body[callsite.block]
968 .statements
969 .push(Statement::new(callsite.source_info, StatementKind::StorageLive(new_local)));
970 }
971 }
972 if let Some(block) = return_block {
973 let mut n = 0;
976 if remap_destination {
977 caller_body[block].statements.push(Statement::new(
978 callsite.source_info,
979 StatementKind::Assign(Box::new((
980 dest,
981 Rvalue::Use(Operand::Move(destination_local.into()), WithRetag::Yes),
982 ))),
983 ));
984 n += 1;
985 }
986 for local in callee_body.vars_and_temps_iter().rev() {
987 if integrator.always_live_locals.contains(local) {
988 let new_local = integrator.map_local(local);
989 caller_body[block].statements.push(Statement::new(
990 callsite.source_info,
991 StatementKind::StorageDead(new_local),
992 ));
993 n += 1;
994 }
995 }
996 caller_body[block].statements.rotate_right(n);
997 }
998
999 caller_body.local_decls.extend(callee_body.drain_vars_and_temps());
1001 caller_body.source_scopes.append(&mut callee_body.source_scopes);
1002
1003 if tcx
1005 .sess
1006 .opts
1007 .unstable_opts
1008 .inline_mir_preserve_debug
1009 .unwrap_or(tcx.sess.opts.debuginfo == DebugInfo::Full)
1010 {
1011 caller_body.var_debug_info.append(&mut callee_body.var_debug_info);
1015 } else {
1016 for bb in callee_body.basic_blocks_mut() {
1017 bb.drop_debuginfo();
1018 }
1019 }
1020 caller_body.basic_blocks_mut().append(callee_body.basic_blocks_mut());
1021
1022 caller_body[callsite.block].terminator = Some(Terminator {
1023 source_info: callsite.source_info,
1024 kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
1025 });
1026
1027 caller_body.required_consts.as_mut().unwrap().extend(
1031 callee_body.required_consts().into_iter().filter(|ct| ct.const_.is_required_const()),
1032 );
1033 let callee_item = MentionedItem::Fn(func.ty(caller_body, tcx));
1041 let caller_mentioned_items = caller_body.mentioned_items.as_mut().unwrap();
1042 if let Some(idx) = caller_mentioned_items.iter().position(|item| item.node == callee_item) {
1043 caller_mentioned_items.remove(idx);
1045 caller_mentioned_items.extend(callee_body.mentioned_items());
1046 } else {
1047 }
1051}
1052
1053fn make_call_args<'tcx, I: Inliner<'tcx>>(
1054 inliner: &I,
1055 args: Box<[Spanned<Operand<'tcx>>]>,
1056 callsite: &CallSite<'tcx>,
1057 caller_body: &mut Body<'tcx>,
1058 callee_body: &Body<'tcx>,
1059 return_block: Option<BasicBlock>,
1060) -> Box<[Local]> {
1061 let tcx = inliner.tcx();
1062
1063 if callsite.fn_sig.abi() == ExternAbi::RustCall && callee_body.spread_arg.is_none() {
1087 let mut args = args.into_iter();
1088 let self_ = create_temp_if_necessary(
1089 inliner,
1090 args.next().unwrap().node,
1091 callsite,
1092 caller_body,
1093 return_block,
1094 );
1095 let tuple = create_temp_if_necessary(
1096 inliner,
1097 args.next().unwrap().node,
1098 callsite,
1099 caller_body,
1100 return_block,
1101 );
1102 assert!(args.next().is_none());
1103
1104 let tuple = Place::from(tuple);
1105 let ty::Tuple(tuple_tys) = tuple.ty(caller_body, tcx).ty.kind() else {
1106 bug!("Closure arguments are not passed as a tuple");
1107 };
1108
1109 let closure_ref_arg = iter::once(self_);
1111
1112 let tuple_tmp_args = tuple_tys.iter().enumerate().map(|(i, ty)| {
1114 let tuple_field = Operand::Move(tcx.mk_place_field(tuple, FieldIdx::new(i), ty));
1116
1117 create_temp_if_necessary(inliner, tuple_field, callsite, caller_body, return_block)
1119 });
1120
1121 closure_ref_arg.chain(tuple_tmp_args).collect()
1122 } else {
1123 args.into_iter()
1124 .map(|a| create_temp_if_necessary(inliner, a.node, callsite, caller_body, return_block))
1125 .collect()
1126 }
1127}
1128
1129fn create_temp_if_necessary<'tcx, I: Inliner<'tcx>>(
1132 inliner: &I,
1133 arg: Operand<'tcx>,
1134 callsite: &CallSite<'tcx>,
1135 caller_body: &mut Body<'tcx>,
1136 return_block: Option<BasicBlock>,
1137) -> Local {
1138 if let Operand::Move(place) = &arg
1140 && let Some(local) = place.as_local()
1141 && caller_body.local_kind(local) == LocalKind::Temp
1142 {
1143 return local;
1144 }
1145
1146 trace!("creating temp for argument {:?}", arg);
1148 let arg_ty = arg.ty(caller_body, inliner.tcx());
1149 let local = new_call_temp(caller_body, callsite, arg_ty, return_block);
1150 caller_body[callsite.block].statements.push(Statement::new(
1151 callsite.source_info,
1152 StatementKind::Assign(Box::new((Place::from(local), Rvalue::Use(arg, WithRetag::Yes)))),
1153 ));
1154 local
1155}
1156
1157fn new_call_temp<'tcx>(
1159 caller_body: &mut Body<'tcx>,
1160 callsite: &CallSite<'tcx>,
1161 ty: Ty<'tcx>,
1162 return_block: Option<BasicBlock>,
1163) -> Local {
1164 let local = caller_body.local_decls.push(LocalDecl::new(ty, callsite.source_info.span));
1165
1166 caller_body[callsite.block]
1167 .statements
1168 .push(Statement::new(callsite.source_info, StatementKind::StorageLive(local)));
1169
1170 if let Some(block) = return_block {
1171 caller_body[block]
1172 .statements
1173 .insert(0, Statement::new(callsite.source_info, StatementKind::StorageDead(local)));
1174 }
1175
1176 local
1177}
1178
1179struct Integrator<'a, 'tcx> {
1187 args: &'a [Local],
1188 new_locals: RangeFrom<Local>,
1189 new_scopes: RangeFrom<SourceScope>,
1190 new_blocks: RangeFrom<BasicBlock>,
1191 destination: Local,
1192 callsite_scope: SourceScopeData<'tcx>,
1193 callsite: &'a CallSite<'tcx>,
1194 cleanup_block: UnwindAction,
1195 in_cleanup_block: bool,
1196 return_block: Option<BasicBlock>,
1197 tcx: TyCtxt<'tcx>,
1198 always_live_locals: DenseBitSet<Local>,
1199}
1200
1201impl Integrator<'_, '_> {
1202 fn map_local(&self, local: Local) -> Local {
1203 let new = if local == RETURN_PLACE {
1204 self.destination
1205 } else {
1206 let idx = local.index() - 1;
1207 if idx < self.args.len() {
1208 self.args[idx]
1209 } else {
1210 self.new_locals.start + (idx - self.args.len())
1211 }
1212 };
1213 trace!("mapping local `{:?}` to `{:?}`", local, new);
1214 new
1215 }
1216
1217 fn map_scope(&self, scope: SourceScope) -> SourceScope {
1218 let new = self.new_scopes.start + scope.index();
1219 trace!("mapping scope `{:?}` to `{:?}`", scope, new);
1220 new
1221 }
1222
1223 fn map_block(&self, block: BasicBlock) -> BasicBlock {
1224 let new = self.new_blocks.start + block.index();
1225 trace!("mapping block `{:?}` to `{:?}`", block, new);
1226 new
1227 }
1228
1229 fn map_unwind(&self, unwind: UnwindAction) -> UnwindAction {
1230 if self.in_cleanup_block {
1231 match unwind {
1232 UnwindAction::Cleanup(_) | UnwindAction::Continue => {
1233 bug!("cleanup on cleanup block");
1234 }
1235 UnwindAction::Unreachable | UnwindAction::Terminate(_) => return unwind,
1236 }
1237 }
1238
1239 match unwind {
1240 UnwindAction::Unreachable | UnwindAction::Terminate(_) => unwind,
1241 UnwindAction::Cleanup(target) => UnwindAction::Cleanup(self.map_block(target)),
1242 UnwindAction::Continue => self.cleanup_block,
1244 }
1245 }
1246}
1247
1248impl<'tcx> MutVisitor<'tcx> for Integrator<'_, 'tcx> {
1249 fn tcx(&self) -> TyCtxt<'tcx> {
1250 self.tcx
1251 }
1252
1253 fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) {
1254 *local = self.map_local(*local);
1255 }
1256
1257 fn visit_source_scope_data(&mut self, scope_data: &mut SourceScopeData<'tcx>) {
1258 self.super_source_scope_data(scope_data);
1259 if scope_data.parent_scope.is_none() {
1260 scope_data.parent_scope = Some(self.callsite.source_info.scope);
1263 assert_eq!(scope_data.inlined_parent_scope, None);
1264 scope_data.inlined_parent_scope = if self.callsite_scope.inlined.is_some() {
1265 Some(self.callsite.source_info.scope)
1266 } else {
1267 self.callsite_scope.inlined_parent_scope
1268 };
1269
1270 assert_eq!(scope_data.inlined, None);
1272 scope_data.inlined = Some((self.callsite.callee, self.callsite.source_info.span));
1273 } else if scope_data.inlined_parent_scope.is_none() {
1274 scope_data.inlined_parent_scope = Some(self.map_scope(OUTERMOST_SOURCE_SCOPE));
1276 }
1277 }
1278
1279 fn visit_source_scope(&mut self, scope: &mut SourceScope) {
1280 *scope = self.map_scope(*scope);
1281 }
1282
1283 fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
1284 self.in_cleanup_block = data.is_cleanup;
1285 self.super_basic_block_data(block, data);
1286 self.in_cleanup_block = false;
1287 }
1288
1289 fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
1290 if let StatementKind::StorageLive(local) | StatementKind::StorageDead(local) =
1291 statement.kind
1292 {
1293 self.always_live_locals.remove(local);
1294 }
1295 self.super_statement(statement, location);
1296 }
1297
1298 fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, loc: Location) {
1299 if !matches!(terminator.kind, TerminatorKind::Return) {
1302 self.super_terminator(terminator, loc);
1303 } else {
1304 self.visit_source_info(&mut terminator.source_info);
1305 }
1306
1307 match terminator.kind {
1308 TerminatorKind::CoroutineDrop | TerminatorKind::Yield { .. } => bug!(),
1309 TerminatorKind::Goto { ref mut target } => {
1310 *target = self.map_block(*target);
1311 }
1312 TerminatorKind::SwitchInt { ref mut targets, .. } => {
1313 for tgt in targets.all_targets_mut() {
1314 *tgt = self.map_block(*tgt);
1315 }
1316 }
1317 TerminatorKind::Drop { ref mut target, ref mut unwind, .. } => {
1318 *target = self.map_block(*target);
1319 *unwind = self.map_unwind(*unwind);
1320 }
1321 TerminatorKind::TailCall { .. } => {
1322 unreachable!()
1324 }
1325 TerminatorKind::Call { ref mut target, ref mut unwind, .. } => {
1326 if let Some(ref mut tgt) = *target {
1327 *tgt = self.map_block(*tgt);
1328 }
1329 *unwind = self.map_unwind(*unwind);
1330 }
1331 TerminatorKind::Assert { ref mut target, ref mut unwind, .. } => {
1332 *target = self.map_block(*target);
1333 *unwind = self.map_unwind(*unwind);
1334 }
1335 TerminatorKind::Return => {
1336 terminator.kind = if let Some(tgt) = self.return_block {
1337 TerminatorKind::Goto { target: tgt }
1338 } else {
1339 TerminatorKind::Unreachable
1340 }
1341 }
1342 TerminatorKind::UnwindResume => {
1343 terminator.kind = match self.cleanup_block {
1344 UnwindAction::Cleanup(tgt) => TerminatorKind::Goto { target: tgt },
1345 UnwindAction::Continue => TerminatorKind::UnwindResume,
1346 UnwindAction::Unreachable => TerminatorKind::Unreachable,
1347 UnwindAction::Terminate(reason) => TerminatorKind::UnwindTerminate(reason),
1348 };
1349 }
1350 TerminatorKind::UnwindTerminate(_) => {}
1351 TerminatorKind::Unreachable => {}
1352 TerminatorKind::FalseEdge { ref mut real_target, ref mut imaginary_target } => {
1353 *real_target = self.map_block(*real_target);
1354 *imaginary_target = self.map_block(*imaginary_target);
1355 }
1356 TerminatorKind::FalseUnwind { real_target: _, unwind: _ } =>
1357 {
1359 bug!("False unwinds should have been removed before inlining")
1360 }
1361 TerminatorKind::InlineAsm { ref mut targets, ref mut unwind, .. } => {
1362 for tgt in targets.iter_mut() {
1363 *tgt = self.map_block(*tgt);
1364 }
1365 *unwind = self.map_unwind(*unwind);
1366 }
1367 }
1368 }
1369}
1370
1371#[instrument(skip(tcx), level = "debug")]
1372fn try_instance_mir<'tcx>(
1373 tcx: TyCtxt<'tcx>,
1374 instance: InstanceKind<'tcx>,
1375) -> Result<&'tcx Body<'tcx>, &'static str> {
1376 if let ty::InstanceKind::DropGlue(_, Some(ty)) | ty::InstanceKind::AsyncDropGlueCtorShim(_, ty) =
1377 instance
1378 && let ty::Adt(def, args) = ty.kind()
1379 {
1380 let fields = def.all_fields();
1381 for field in fields {
1382 let field_ty = field.ty(tcx, args);
1383 if field_ty.has_param() && field_ty.has_aliases() {
1384 return Err("cannot build drop shim for polymorphic type");
1385 }
1386 }
1387 }
1388 Ok(tcx.instance_mir(instance))
1389}
1390
1391fn body_is_forwarder(body: &Body<'_>) -> bool {
1392 let TerminatorKind::Call { target, .. } = body.basic_blocks[START_BLOCK].terminator().kind
1393 else {
1394 return false;
1395 };
1396 if let Some(target) = target {
1397 let TerminatorKind::Return = body.basic_blocks[target].terminator().kind else {
1398 return false;
1399 };
1400 }
1401
1402 let max_blocks = if !body.is_polymorphic {
1403 2
1404 } else if target.is_none() {
1405 3
1406 } else {
1407 4
1408 };
1409 if body.basic_blocks.len() > max_blocks {
1410 return false;
1411 }
1412
1413 body.basic_blocks.iter_enumerated().all(|(bb, bb_data)| {
1414 bb == START_BLOCK
1415 || matches!(
1416 bb_data.terminator().kind,
1417 TerminatorKind::Return
1418 | TerminatorKind::Drop { .. }
1419 | TerminatorKind::UnwindResume
1420 | TerminatorKind::UnwindTerminate(_)
1421 )
1422 })
1423}