]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/inline.rs
Rollup merge of #105236 - JohnTitor:issue-47814, r=compiler-errors
[rust.git] / compiler / rustc_mir_transform / src / inline.rs
1 //! Inlining pass for MIR functions
2 use crate::deref_separator::deref_finder;
3 use rustc_attr::InlineAttr;
4 use rustc_index::bit_set::BitSet;
5 use rustc_index::vec::Idx;
6 use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
7 use rustc_middle::mir::visit::*;
8 use rustc_middle::mir::*;
9 use rustc_middle::ty::{self, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
10 use rustc_session::config::OptLevel;
11 use rustc_span::def_id::DefId;
12 use rustc_span::{hygiene::ExpnKind, ExpnData, LocalExpnId, Span};
13 use rustc_target::abi::VariantIdx;
14 use rustc_target::spec::abi::Abi;
15
16 use crate::simplify::{remove_dead_blocks, CfgSimplifier};
17 use crate::util;
18 use crate::MirPass;
19 use std::iter;
20 use std::ops::{Range, RangeFrom};
21
22 pub(crate) mod cycle;
23
24 const INSTR_COST: usize = 5;
25 const CALL_PENALTY: usize = 25;
26 const LANDINGPAD_PENALTY: usize = 50;
27 const RESUME_PENALTY: usize = 45;
28
29 const UNKNOWN_SIZE_COST: usize = 10;
30
31 pub struct Inline;
32
33 #[derive(Copy, Clone, Debug)]
34 struct CallSite<'tcx> {
35     callee: Instance<'tcx>,
36     fn_sig: ty::PolyFnSig<'tcx>,
37     block: BasicBlock,
38     target: Option<BasicBlock>,
39     source_info: SourceInfo,
40 }
41
42 impl<'tcx> MirPass<'tcx> for Inline {
43     fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
44         if let Some(enabled) = sess.opts.unstable_opts.inline_mir {
45             return enabled;
46         }
47
48         match sess.mir_opt_level() {
49             0 | 1 => false,
50             2 => {
51                 (sess.opts.optimize == OptLevel::Default
52                     || sess.opts.optimize == OptLevel::Aggressive)
53                     && sess.opts.incremental == None
54             }
55             _ => true,
56         }
57     }
58
59     fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
60         let span = trace_span!("inline", body = %tcx.def_path_str(body.source.def_id()));
61         let _guard = span.enter();
62         if inline(tcx, body) {
63             debug!("running simplify cfg on {:?}", body.source);
64             CfgSimplifier::new(body).simplify();
65             remove_dead_blocks(tcx, body);
66             deref_finder(tcx, body);
67         }
68     }
69 }
70
71 fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
72     let def_id = body.source.def_id().expect_local();
73
74     // Only do inlining into fn bodies.
75     if !tcx.hir().body_owner_kind(def_id).is_fn_or_closure() {
76         return false;
77     }
78     if body.source.promoted.is_some() {
79         return false;
80     }
81     // Avoid inlining into generators, since their `optimized_mir` is used for layout computation,
82     // which can create a cycle, even when no attempt is made to inline the function in the other
83     // direction.
84     if body.generator.is_some() {
85         return false;
86     }
87
88     let param_env = tcx.param_env_reveal_all_normalized(def_id);
89
90     let mut this = Inliner {
91         tcx,
92         param_env,
93         codegen_fn_attrs: tcx.codegen_fn_attrs(def_id),
94         history: Vec::new(),
95         changed: false,
96     };
97     let blocks = BasicBlock::new(0)..body.basic_blocks.next_index();
98     this.process_blocks(body, blocks);
99     this.changed
100 }
101
102 struct Inliner<'tcx> {
103     tcx: TyCtxt<'tcx>,
104     param_env: ParamEnv<'tcx>,
105     /// Caller codegen attributes.
106     codegen_fn_attrs: &'tcx CodegenFnAttrs,
107     /// Stack of inlined instances.
108     /// We only check the `DefId` and not the substs because we want to
109     /// avoid inlining cases of polymorphic recursion.
110     /// The number of `DefId`s is finite, so checking history is enough
111     /// to ensure that we do not loop endlessly while inlining.
112     history: Vec<DefId>,
113     /// Indicates that the caller body has been modified.
114     changed: bool,
115 }
116
117 impl<'tcx> Inliner<'tcx> {
118     fn process_blocks(&mut self, caller_body: &mut Body<'tcx>, blocks: Range<BasicBlock>) {
119         for bb in blocks {
120             let bb_data = &caller_body[bb];
121             if bb_data.is_cleanup {
122                 continue;
123             }
124
125             let Some(callsite) = self.resolve_callsite(caller_body, bb, bb_data) else {
126                 continue;
127             };
128
129             let span = trace_span!("process_blocks", %callsite.callee, ?bb);
130             let _guard = span.enter();
131
132             match self.try_inlining(caller_body, &callsite) {
133                 Err(reason) => {
134                     debug!("not-inlined {} [{}]", callsite.callee, reason);
135                     continue;
136                 }
137                 Ok(new_blocks) => {
138                     debug!("inlined {}", callsite.callee);
139                     self.changed = true;
140                     self.history.push(callsite.callee.def_id());
141                     self.process_blocks(caller_body, new_blocks);
142                     self.history.pop();
143                 }
144             }
145         }
146     }
147
148     /// Attempts to inline a callsite into the caller body. When successful returns basic blocks
149     /// containing the inlined body. Otherwise returns an error describing why inlining didn't take
150     /// place.
151     fn try_inlining(
152         &self,
153         caller_body: &mut Body<'tcx>,
154         callsite: &CallSite<'tcx>,
155     ) -> Result<std::ops::Range<BasicBlock>, &'static str> {
156         let callee_attrs = self.tcx.codegen_fn_attrs(callsite.callee.def_id());
157         self.check_codegen_attributes(callsite, callee_attrs)?;
158         self.check_mir_is_available(caller_body, &callsite.callee)?;
159         let callee_body = self.tcx.instance_mir(callsite.callee.def);
160         self.check_mir_body(callsite, callee_body, callee_attrs)?;
161
162         if !self.tcx.consider_optimizing(|| {
163             format!("Inline {:?} into {:?}", callsite.callee, caller_body.source)
164         }) {
165             return Err("optimization fuel exhausted");
166         }
167
168         let Ok(callee_body) = callsite.callee.try_subst_mir_and_normalize_erasing_regions(
169             self.tcx,
170             self.param_env,
171             callee_body.clone(),
172         ) else {
173             return Err("failed to normalize callee body");
174         };
175
176         // Check call signature compatibility.
177         // Normally, this shouldn't be required, but trait normalization failure can create a
178         // validation ICE.
179         let terminator = caller_body[callsite.block].terminator.as_ref().unwrap();
180         let TerminatorKind::Call { args, destination, .. } = &terminator.kind else { bug!() };
181         let destination_ty = destination.ty(&caller_body.local_decls, self.tcx).ty;
182         let output_type = callee_body.return_ty();
183         if !util::is_subtype(self.tcx, self.param_env, output_type, destination_ty) {
184             trace!(?output_type, ?destination_ty);
185             return Err("failed to normalize return type");
186         }
187         if callsite.fn_sig.abi() == Abi::RustCall {
188             let (arg_tuple, skipped_args) = match &args[..] {
189                 [arg_tuple] => (arg_tuple, 0),
190                 [_, arg_tuple] => (arg_tuple, 1),
191                 _ => bug!("Expected `rust-call` to have 1 or 2 args"),
192             };
193
194             let arg_tuple_ty = arg_tuple.ty(&caller_body.local_decls, self.tcx);
195             let ty::Tuple(arg_tuple_tys) = arg_tuple_ty.kind() else {
196                 bug!("Closure arguments are not passed as a tuple");
197             };
198
199             for (arg_ty, input) in
200                 arg_tuple_tys.iter().zip(callee_body.args_iter().skip(skipped_args))
201             {
202                 let input_type = callee_body.local_decls[input].ty;
203                 if !util::is_subtype(self.tcx, self.param_env, input_type, arg_ty) {
204                     trace!(?arg_ty, ?input_type);
205                     return Err("failed to normalize tuple argument type");
206                 }
207             }
208         } else {
209             for (arg, input) in args.iter().zip(callee_body.args_iter()) {
210                 let input_type = callee_body.local_decls[input].ty;
211                 let arg_ty = arg.ty(&caller_body.local_decls, self.tcx);
212                 if !util::is_subtype(self.tcx, self.param_env, input_type, arg_ty) {
213                     trace!(?arg_ty, ?input_type);
214                     return Err("failed to normalize argument type");
215                 }
216             }
217         }
218
219         let old_blocks = caller_body.basic_blocks.next_index();
220         self.inline_call(caller_body, &callsite, callee_body);
221         let new_blocks = old_blocks..caller_body.basic_blocks.next_index();
222
223         Ok(new_blocks)
224     }
225
226     fn check_mir_is_available(
227         &self,
228         caller_body: &Body<'tcx>,
229         callee: &Instance<'tcx>,
230     ) -> Result<(), &'static str> {
231         let caller_def_id = caller_body.source.def_id();
232         let callee_def_id = callee.def_id();
233         if callee_def_id == caller_def_id {
234             return Err("self-recursion");
235         }
236
237         match callee.def {
238             InstanceDef::Item(_) => {
239                 // If there is no MIR available (either because it was not in metadata or
240                 // because it has no MIR because it's an extern function), then the inliner
241                 // won't cause cycles on this.
242                 if !self.tcx.is_mir_available(callee_def_id) {
243                     return Err("item MIR unavailable");
244                 }
245             }
246             // These have no own callable MIR.
247             InstanceDef::Intrinsic(_) | InstanceDef::Virtual(..) => {
248                 return Err("instance without MIR (intrinsic / virtual)");
249             }
250             // This cannot result in an immediate cycle since the callee MIR is a shim, which does
251             // not get any optimizations run on it. Any subsequent inlining may cause cycles, but we
252             // do not need to catch this here, we can wait until the inliner decides to continue
253             // inlining a second time.
254             InstanceDef::VTableShim(_)
255             | InstanceDef::ReifyShim(_)
256             | InstanceDef::FnPtrShim(..)
257             | InstanceDef::ClosureOnceShim { .. }
258             | InstanceDef::DropGlue(..)
259             | InstanceDef::CloneShim(..) => return Ok(()),
260         }
261
262         if self.tcx.is_constructor(callee_def_id) {
263             trace!("constructors always have MIR");
264             // Constructor functions cannot cause a query cycle.
265             return Ok(());
266         }
267
268         if callee_def_id.is_local() {
269             // Avoid a cycle here by only using `instance_mir` only if we have
270             // a lower `DefPathHash` than the callee. This ensures that the callee will
271             // not inline us. This trick even works with incremental compilation,
272             // since `DefPathHash` is stable.
273             if self.tcx.def_path_hash(caller_def_id).local_hash()
274                 < self.tcx.def_path_hash(callee_def_id).local_hash()
275             {
276                 return Ok(());
277             }
278
279             // If we know for sure that the function we're calling will itself try to
280             // call us, then we avoid inlining that function.
281             if self.tcx.mir_callgraph_reachable((*callee, caller_def_id.expect_local())) {
282                 return Err("caller might be reachable from callee (query cycle avoidance)");
283             }
284
285             Ok(())
286         } else {
287             // This cannot result in an immediate cycle since the callee MIR is from another crate
288             // and is already optimized. Any subsequent inlining may cause cycles, but we do
289             // not need to catch this here, we can wait until the inliner decides to continue
290             // inlining a second time.
291             trace!("functions from other crates always have MIR");
292             Ok(())
293         }
294     }
295
296     fn resolve_callsite(
297         &self,
298         caller_body: &Body<'tcx>,
299         bb: BasicBlock,
300         bb_data: &BasicBlockData<'tcx>,
301     ) -> Option<CallSite<'tcx>> {
302         // Only consider direct calls to functions
303         let terminator = bb_data.terminator();
304         if let TerminatorKind::Call { ref func, target, .. } = terminator.kind {
305             let func_ty = func.ty(caller_body, self.tcx);
306             if let ty::FnDef(def_id, substs) = *func_ty.kind() {
307                 // To resolve an instance its substs have to be fully normalized.
308                 let substs = self.tcx.try_normalize_erasing_regions(self.param_env, substs).ok()?;
309                 let callee =
310                     Instance::resolve(self.tcx, self.param_env, def_id, substs).ok().flatten()?;
311
312                 if let InstanceDef::Virtual(..) | InstanceDef::Intrinsic(_) = callee.def {
313                     return None;
314                 }
315
316                 if self.history.contains(&callee.def_id()) {
317                     return None;
318                 }
319
320                 let fn_sig = self.tcx.bound_fn_sig(def_id).subst(self.tcx, substs);
321
322                 return Some(CallSite {
323                     callee,
324                     fn_sig,
325                     block: bb,
326                     target,
327                     source_info: terminator.source_info,
328                 });
329             }
330         }
331
332         None
333     }
334
335     /// Returns an error if inlining is not possible based on codegen attributes alone. A success
336     /// indicates that inlining decision should be based on other criteria.
337     fn check_codegen_attributes(
338         &self,
339         callsite: &CallSite<'tcx>,
340         callee_attrs: &CodegenFnAttrs,
341     ) -> Result<(), &'static str> {
342         match callee_attrs.inline {
343             InlineAttr::Never => return Err("never inline hint"),
344             InlineAttr::Always | InlineAttr::Hint => {}
345             InlineAttr::None => {
346                 if self.tcx.sess.mir_opt_level() <= 2 {
347                     return Err("at mir-opt-level=2, only #[inline] is inlined");
348                 }
349             }
350         }
351
352         // Only inline local functions if they would be eligible for cross-crate
353         // inlining. This is to ensure that the final crate doesn't have MIR that
354         // reference unexported symbols
355         if callsite.callee.def_id().is_local() {
356             let is_generic = callsite.callee.substs.non_erasable_generics().next().is_some();
357             if !is_generic && !callee_attrs.requests_inline() {
358                 return Err("not exported");
359             }
360         }
361
362         if callsite.fn_sig.c_variadic() {
363             return Err("C variadic");
364         }
365
366         if callee_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
367             return Err("cold");
368         }
369
370         if callee_attrs.no_sanitize != self.codegen_fn_attrs.no_sanitize {
371             return Err("incompatible sanitizer set");
372         }
373
374         // Two functions are compatible if the callee has no attribute (meaning
375         // that it's codegen agnostic), or sets an attribute that is identical
376         // to this function's attribute.
377         if callee_attrs.instruction_set.is_some()
378             && callee_attrs.instruction_set != self.codegen_fn_attrs.instruction_set
379         {
380             return Err("incompatible instruction set");
381         }
382
383         for feature in &callee_attrs.target_features {
384             if !self.codegen_fn_attrs.target_features.contains(feature) {
385                 return Err("incompatible target feature");
386             }
387         }
388
389         Ok(())
390     }
391
392     /// Returns inlining decision that is based on the examination of callee MIR body.
393     /// Assumes that codegen attributes have been checked for compatibility already.
394     #[instrument(level = "debug", skip(self, callee_body))]
395     fn check_mir_body(
396         &self,
397         callsite: &CallSite<'tcx>,
398         callee_body: &Body<'tcx>,
399         callee_attrs: &CodegenFnAttrs,
400     ) -> Result<(), &'static str> {
401         let tcx = self.tcx;
402
403         let mut threshold = if callee_attrs.requests_inline() {
404             self.tcx.sess.opts.unstable_opts.inline_mir_hint_threshold.unwrap_or(100)
405         } else {
406             self.tcx.sess.opts.unstable_opts.inline_mir_threshold.unwrap_or(50)
407         };
408
409         // Give a bonus functions with a small number of blocks,
410         // We normally have two or three blocks for even
411         // very small functions.
412         if callee_body.basic_blocks.len() <= 3 {
413             threshold += threshold / 4;
414         }
415         debug!("    final inline threshold = {}", threshold);
416
417         // FIXME: Give a bonus to functions with only a single caller
418         let diverges = matches!(
419             callee_body.basic_blocks[START_BLOCK].terminator().kind,
420             TerminatorKind::Unreachable | TerminatorKind::Call { target: None, .. }
421         );
422         if diverges && !matches!(callee_attrs.inline, InlineAttr::Always) {
423             return Err("callee diverges unconditionally");
424         }
425
426         let mut checker = CostChecker {
427             tcx: self.tcx,
428             param_env: self.param_env,
429             instance: callsite.callee,
430             callee_body,
431             cost: 0,
432             validation: Ok(()),
433         };
434
435         // Traverse the MIR manually so we can account for the effects of inlining on the CFG.
436         let mut work_list = vec![START_BLOCK];
437         let mut visited = BitSet::new_empty(callee_body.basic_blocks.len());
438         while let Some(bb) = work_list.pop() {
439             if !visited.insert(bb.index()) {
440                 continue;
441             }
442
443             let blk = &callee_body.basic_blocks[bb];
444             checker.visit_basic_block_data(bb, blk);
445
446             let term = blk.terminator();
447             if let TerminatorKind::Drop { ref place, target, unwind }
448             | TerminatorKind::DropAndReplace { ref place, target, unwind, .. } = term.kind
449             {
450                 work_list.push(target);
451
452                 // If the place doesn't actually need dropping, treat it like a regular goto.
453                 let ty = callsite.callee.subst_mir(self.tcx, &place.ty(callee_body, tcx).ty);
454                 if ty.needs_drop(tcx, self.param_env) && let Some(unwind) = unwind {
455                         work_list.push(unwind);
456                     }
457             } else if callee_attrs.instruction_set != self.codegen_fn_attrs.instruction_set
458                 && matches!(term.kind, TerminatorKind::InlineAsm { .. })
459             {
460                 // During the attribute checking stage we allow a callee with no
461                 // instruction_set assigned to count as compatible with a function that does
462                 // assign one. However, during this stage we require an exact match when any
463                 // inline-asm is detected. LLVM will still possibly do an inline later on
464                 // if the no-attribute function ends up with the same instruction set anyway.
465                 return Err("Cannot move inline-asm across instruction sets");
466             } else {
467                 work_list.extend(term.successors())
468             }
469         }
470
471         // Count up the cost of local variables and temps, if we know the size
472         // use that, otherwise we use a moderately-large dummy cost.
473         for v in callee_body.vars_and_temps_iter() {
474             checker.visit_local_decl(v, &callee_body.local_decls[v]);
475         }
476
477         // Abort if type validation found anything fishy.
478         checker.validation?;
479
480         let cost = checker.cost;
481         if let InlineAttr::Always = callee_attrs.inline {
482             debug!("INLINING {:?} because inline(always) [cost={}]", callsite, cost);
483             Ok(())
484         } else if cost <= threshold {
485             debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
486             Ok(())
487         } else {
488             debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
489             Err("cost above threshold")
490         }
491     }
492
493     fn inline_call(
494         &self,
495         caller_body: &mut Body<'tcx>,
496         callsite: &CallSite<'tcx>,
497         mut callee_body: Body<'tcx>,
498     ) {
499         let terminator = caller_body[callsite.block].terminator.take().unwrap();
500         match terminator.kind {
501             TerminatorKind::Call { args, destination, cleanup, .. } => {
502                 // If the call is something like `a[*i] = f(i)`, where
503                 // `i : &mut usize`, then just duplicating the `a[*i]`
504                 // Place could result in two different locations if `f`
505                 // writes to `i`. To prevent this we need to create a temporary
506                 // borrow of the place and pass the destination as `*temp` instead.
507                 fn dest_needs_borrow(place: Place<'_>) -> bool {
508                     for elem in place.projection.iter() {
509                         match elem {
510                             ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
511                             _ => {}
512                         }
513                     }
514
515                     false
516                 }
517
518                 let dest = if dest_needs_borrow(destination) {
519                     trace!("creating temp for return destination");
520                     let dest = Rvalue::Ref(
521                         self.tcx.lifetimes.re_erased,
522                         BorrowKind::Mut { allow_two_phase_borrow: false },
523                         destination,
524                     );
525                     let dest_ty = dest.ty(caller_body, self.tcx);
526                     let temp = Place::from(self.new_call_temp(caller_body, &callsite, dest_ty));
527                     caller_body[callsite.block].statements.push(Statement {
528                         source_info: callsite.source_info,
529                         kind: StatementKind::Assign(Box::new((temp, dest))),
530                     });
531                     self.tcx.mk_place_deref(temp)
532                 } else {
533                     destination
534                 };
535
536                 // Copy the arguments if needed.
537                 let args: Vec<_> = self.make_call_args(args, &callsite, caller_body, &callee_body);
538
539                 let mut expn_data = ExpnData::default(
540                     ExpnKind::Inlined,
541                     callsite.source_info.span,
542                     self.tcx.sess.edition(),
543                     None,
544                     None,
545                 );
546                 expn_data.def_site = callee_body.span;
547                 let expn_data =
548                     self.tcx.with_stable_hashing_context(|hcx| LocalExpnId::fresh(expn_data, hcx));
549                 let mut integrator = Integrator {
550                     args: &args,
551                     new_locals: Local::new(caller_body.local_decls.len())..,
552                     new_scopes: SourceScope::new(caller_body.source_scopes.len())..,
553                     new_blocks: BasicBlock::new(caller_body.basic_blocks.len())..,
554                     destination: dest,
555                     callsite_scope: caller_body.source_scopes[callsite.source_info.scope].clone(),
556                     callsite,
557                     cleanup_block: cleanup,
558                     in_cleanup_block: false,
559                     tcx: self.tcx,
560                     expn_data,
561                     always_live_locals: BitSet::new_filled(callee_body.local_decls.len()),
562                 };
563
564                 // Map all `Local`s, `SourceScope`s and `BasicBlock`s to new ones
565                 // (or existing ones, in a few special cases) in the caller.
566                 integrator.visit_body(&mut callee_body);
567
568                 // If there are any locals without storage markers, give them storage only for the
569                 // duration of the call.
570                 for local in callee_body.vars_and_temps_iter() {
571                     if !callee_body.local_decls[local].internal
572                         && integrator.always_live_locals.contains(local)
573                     {
574                         let new_local = integrator.map_local(local);
575                         caller_body[callsite.block].statements.push(Statement {
576                             source_info: callsite.source_info,
577                             kind: StatementKind::StorageLive(new_local),
578                         });
579                     }
580                 }
581                 if let Some(block) = callsite.target {
582                     // To avoid repeated O(n) insert, push any new statements to the end and rotate
583                     // the slice once.
584                     let mut n = 0;
585                     for local in callee_body.vars_and_temps_iter().rev() {
586                         if !callee_body.local_decls[local].internal
587                             && integrator.always_live_locals.contains(local)
588                         {
589                             let new_local = integrator.map_local(local);
590                             caller_body[block].statements.push(Statement {
591                                 source_info: callsite.source_info,
592                                 kind: StatementKind::StorageDead(new_local),
593                             });
594                             n += 1;
595                         }
596                     }
597                     caller_body[block].statements.rotate_right(n);
598                 }
599
600                 // Insert all of the (mapped) parts of the callee body into the caller.
601                 caller_body.local_decls.extend(callee_body.drain_vars_and_temps());
602                 caller_body.source_scopes.extend(&mut callee_body.source_scopes.drain(..));
603                 caller_body.var_debug_info.append(&mut callee_body.var_debug_info);
604                 caller_body.basic_blocks_mut().extend(callee_body.basic_blocks_mut().drain(..));
605
606                 caller_body[callsite.block].terminator = Some(Terminator {
607                     source_info: callsite.source_info,
608                     kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
609                 });
610
611                 // Copy only unevaluated constants from the callee_body into the caller_body.
612                 // Although we are only pushing `ConstKind::Unevaluated` consts to
613                 // `required_consts`, here we may not only have `ConstKind::Unevaluated`
614                 // because we are calling `subst_and_normalize_erasing_regions`.
615                 caller_body.required_consts.extend(
616                     callee_body.required_consts.iter().copied().filter(|&ct| match ct.literal {
617                         ConstantKind::Ty(_) => {
618                             bug!("should never encounter ty::UnevaluatedConst in `required_consts`")
619                         }
620                         ConstantKind::Val(..) | ConstantKind::Unevaluated(..) => true,
621                     }),
622                 );
623             }
624             kind => bug!("unexpected terminator kind {:?}", kind),
625         }
626     }
627
628     fn make_call_args(
629         &self,
630         args: Vec<Operand<'tcx>>,
631         callsite: &CallSite<'tcx>,
632         caller_body: &mut Body<'tcx>,
633         callee_body: &Body<'tcx>,
634     ) -> Vec<Local> {
635         let tcx = self.tcx;
636
637         // There is a bit of a mismatch between the *caller* of a closure and the *callee*.
638         // The caller provides the arguments wrapped up in a tuple:
639         //
640         //     tuple_tmp = (a, b, c)
641         //     Fn::call(closure_ref, tuple_tmp)
642         //
643         // meanwhile the closure body expects the arguments (here, `a`, `b`, and `c`)
644         // as distinct arguments. (This is the "rust-call" ABI hack.) Normally, codegen has
645         // the job of unpacking this tuple. But here, we are codegen. =) So we want to create
646         // a vector like
647         //
648         //     [closure_ref, tuple_tmp.0, tuple_tmp.1, tuple_tmp.2]
649         //
650         // Except for one tiny wrinkle: we don't actually want `tuple_tmp.0`. It's more convenient
651         // if we "spill" that into *another* temporary, so that we can map the argument
652         // variable in the callee MIR directly to an argument variable on our side.
653         // So we introduce temporaries like:
654         //
655         //     tmp0 = tuple_tmp.0
656         //     tmp1 = tuple_tmp.1
657         //     tmp2 = tuple_tmp.2
658         //
659         // and the vector is `[closure_ref, tmp0, tmp1, tmp2]`.
660         if callsite.fn_sig.abi() == Abi::RustCall && callee_body.spread_arg.is_none() {
661             let mut args = args.into_iter();
662             let self_ = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_body);
663             let tuple = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_body);
664             assert!(args.next().is_none());
665
666             let tuple = Place::from(tuple);
667             let ty::Tuple(tuple_tys) = tuple.ty(caller_body, tcx).ty.kind() else {
668                 bug!("Closure arguments are not passed as a tuple");
669             };
670
671             // The `closure_ref` in our example above.
672             let closure_ref_arg = iter::once(self_);
673
674             // The `tmp0`, `tmp1`, and `tmp2` in our example above.
675             let tuple_tmp_args = tuple_tys.iter().enumerate().map(|(i, ty)| {
676                 // This is e.g., `tuple_tmp.0` in our example above.
677                 let tuple_field = Operand::Move(tcx.mk_place_field(tuple, Field::new(i), ty));
678
679                 // Spill to a local to make e.g., `tmp0`.
680                 self.create_temp_if_necessary(tuple_field, callsite, caller_body)
681             });
682
683             closure_ref_arg.chain(tuple_tmp_args).collect()
684         } else {
685             args.into_iter()
686                 .map(|a| self.create_temp_if_necessary(a, callsite, caller_body))
687                 .collect()
688         }
689     }
690
691     /// If `arg` is already a temporary, returns it. Otherwise, introduces a fresh
692     /// temporary `T` and an instruction `T = arg`, and returns `T`.
693     fn create_temp_if_necessary(
694         &self,
695         arg: Operand<'tcx>,
696         callsite: &CallSite<'tcx>,
697         caller_body: &mut Body<'tcx>,
698     ) -> Local {
699         // Reuse the operand if it is a moved temporary.
700         if let Operand::Move(place) = &arg
701             && let Some(local) = place.as_local()
702             && caller_body.local_kind(local) == LocalKind::Temp
703         {
704             return local;
705         }
706
707         // Otherwise, create a temporary for the argument.
708         trace!("creating temp for argument {:?}", arg);
709         let arg_ty = arg.ty(caller_body, self.tcx);
710         let local = self.new_call_temp(caller_body, callsite, arg_ty);
711         caller_body[callsite.block].statements.push(Statement {
712             source_info: callsite.source_info,
713             kind: StatementKind::Assign(Box::new((Place::from(local), Rvalue::Use(arg)))),
714         });
715         local
716     }
717
718     /// Introduces a new temporary into the caller body that is live for the duration of the call.
719     fn new_call_temp(
720         &self,
721         caller_body: &mut Body<'tcx>,
722         callsite: &CallSite<'tcx>,
723         ty: Ty<'tcx>,
724     ) -> Local {
725         let local = caller_body.local_decls.push(LocalDecl::new(ty, callsite.source_info.span));
726
727         caller_body[callsite.block].statements.push(Statement {
728             source_info: callsite.source_info,
729             kind: StatementKind::StorageLive(local),
730         });
731
732         if let Some(block) = callsite.target {
733             caller_body[block].statements.insert(
734                 0,
735                 Statement {
736                     source_info: callsite.source_info,
737                     kind: StatementKind::StorageDead(local),
738                 },
739             );
740         }
741
742         local
743     }
744 }
745
746 fn type_size_of<'tcx>(
747     tcx: TyCtxt<'tcx>,
748     param_env: ty::ParamEnv<'tcx>,
749     ty: Ty<'tcx>,
750 ) -> Option<u64> {
751     tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes())
752 }
753
754 /// Verify that the callee body is compatible with the caller.
755 ///
756 /// This visitor mostly computes the inlining cost,
757 /// but also needs to verify that types match because of normalization failure.
758 struct CostChecker<'b, 'tcx> {
759     tcx: TyCtxt<'tcx>,
760     param_env: ParamEnv<'tcx>,
761     cost: usize,
762     callee_body: &'b Body<'tcx>,
763     instance: ty::Instance<'tcx>,
764     validation: Result<(), &'static str>,
765 }
766
767 impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
768     fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
769         // Don't count StorageLive/StorageDead in the inlining cost.
770         match statement.kind {
771             StatementKind::StorageLive(_)
772             | StatementKind::StorageDead(_)
773             | StatementKind::Deinit(_)
774             | StatementKind::Nop => {}
775             _ => self.cost += INSTR_COST,
776         }
777
778         self.super_statement(statement, location);
779     }
780
781     fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
782         let tcx = self.tcx;
783         match terminator.kind {
784             TerminatorKind::Drop { ref place, unwind, .. }
785             | TerminatorKind::DropAndReplace { ref place, unwind, .. } => {
786                 // If the place doesn't actually need dropping, treat it like a regular goto.
787                 let ty = self.instance.subst_mir(tcx, &place.ty(self.callee_body, tcx).ty);
788                 if ty.needs_drop(tcx, self.param_env) {
789                     self.cost += CALL_PENALTY;
790                     if unwind.is_some() {
791                         self.cost += LANDINGPAD_PENALTY;
792                     }
793                 } else {
794                     self.cost += INSTR_COST;
795                 }
796             }
797             TerminatorKind::Call { func: Operand::Constant(ref f), cleanup, .. } => {
798                 let fn_ty = self.instance.subst_mir(tcx, &f.literal.ty());
799                 self.cost += if let ty::FnDef(def_id, _) = *fn_ty.kind() && tcx.is_intrinsic(def_id) {
800                     // Don't give intrinsics the extra penalty for calls
801                     INSTR_COST
802                 } else {
803                     CALL_PENALTY
804                 };
805                 if cleanup.is_some() {
806                     self.cost += LANDINGPAD_PENALTY;
807                 }
808             }
809             TerminatorKind::Assert { cleanup, .. } => {
810                 self.cost += CALL_PENALTY;
811                 if cleanup.is_some() {
812                     self.cost += LANDINGPAD_PENALTY;
813                 }
814             }
815             TerminatorKind::Resume => self.cost += RESUME_PENALTY,
816             TerminatorKind::InlineAsm { cleanup, .. } => {
817                 self.cost += INSTR_COST;
818                 if cleanup.is_some() {
819                     self.cost += LANDINGPAD_PENALTY;
820                 }
821             }
822             _ => self.cost += INSTR_COST,
823         }
824
825         self.super_terminator(terminator, location);
826     }
827
828     /// Count up the cost of local variables and temps, if we know the size
829     /// use that, otherwise we use a moderately-large dummy cost.
830     fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
831         let tcx = self.tcx;
832         let ptr_size = tcx.data_layout.pointer_size.bytes();
833
834         let ty = self.instance.subst_mir(tcx, &local_decl.ty);
835         // Cost of the var is the size in machine-words, if we know
836         // it.
837         if let Some(size) = type_size_of(tcx, self.param_env, ty) {
838             self.cost += ((size + ptr_size - 1) / ptr_size) as usize;
839         } else {
840             self.cost += UNKNOWN_SIZE_COST;
841         }
842
843         self.super_local_decl(local, local_decl)
844     }
845
846     /// This method duplicates code from MIR validation in an attempt to detect type mismatches due
847     /// to normalization failure.
848     fn visit_projection_elem(
849         &mut self,
850         local: Local,
851         proj_base: &[PlaceElem<'tcx>],
852         elem: PlaceElem<'tcx>,
853         context: PlaceContext,
854         location: Location,
855     ) {
856         if let ProjectionElem::Field(f, ty) = elem {
857             let parent = Place { local, projection: self.tcx.intern_place_elems(proj_base) };
858             let parent_ty = parent.ty(&self.callee_body.local_decls, self.tcx);
859             let check_equal = |this: &mut Self, f_ty| {
860                 if !util::is_equal_up_to_subtyping(this.tcx, this.param_env, ty, f_ty) {
861                     trace!(?ty, ?f_ty);
862                     this.validation = Err("failed to normalize projection type");
863                     return;
864                 }
865             };
866
867             let kind = match parent_ty.ty.kind() {
868                 &ty::Opaque(def_id, substs) => {
869                     self.tcx.bound_type_of(def_id).subst(self.tcx, substs).kind()
870                 }
871                 kind => kind,
872             };
873
874             match kind {
875                 ty::Tuple(fields) => {
876                     let Some(f_ty) = fields.get(f.as_usize()) else {
877                         self.validation = Err("malformed MIR");
878                         return;
879                     };
880                     check_equal(self, *f_ty);
881                 }
882                 ty::Adt(adt_def, substs) => {
883                     let var = parent_ty.variant_index.unwrap_or(VariantIdx::from_u32(0));
884                     let Some(field) = adt_def.variant(var).fields.get(f.as_usize()) else {
885                         self.validation = Err("malformed MIR");
886                         return;
887                     };
888                     check_equal(self, field.ty(self.tcx, substs));
889                 }
890                 ty::Closure(_, substs) => {
891                     let substs = substs.as_closure();
892                     let Some(f_ty) = substs.upvar_tys().nth(f.as_usize()) else {
893                         self.validation = Err("malformed MIR");
894                         return;
895                     };
896                     check_equal(self, f_ty);
897                 }
898                 &ty::Generator(def_id, substs, _) => {
899                     let f_ty = if let Some(var) = parent_ty.variant_index {
900                         let gen_body = if def_id == self.callee_body.source.def_id() {
901                             self.callee_body
902                         } else {
903                             self.tcx.optimized_mir(def_id)
904                         };
905
906                         let Some(layout) = gen_body.generator_layout() else {
907                             self.validation = Err("malformed MIR");
908                             return;
909                         };
910
911                         let Some(&local) = layout.variant_fields[var].get(f) else {
912                             self.validation = Err("malformed MIR");
913                             return;
914                         };
915
916                         let Some(&f_ty) = layout.field_tys.get(local) else {
917                             self.validation = Err("malformed MIR");
918                             return;
919                         };
920
921                         f_ty
922                     } else {
923                         let Some(f_ty) = substs.as_generator().prefix_tys().nth(f.index()) else {
924                             self.validation = Err("malformed MIR");
925                             return;
926                         };
927
928                         f_ty
929                     };
930
931                     check_equal(self, f_ty);
932                 }
933                 _ => self.validation = Err("malformed MIR"),
934             }
935         }
936
937         self.super_projection_elem(local, proj_base, elem, context, location);
938     }
939 }
940
941 /**
942  * Integrator.
943  *
944  * Integrates blocks from the callee function into the calling function.
945  * Updates block indices, references to locals and other control flow
946  * stuff.
947 */
948 struct Integrator<'a, 'tcx> {
949     args: &'a [Local],
950     new_locals: RangeFrom<Local>,
951     new_scopes: RangeFrom<SourceScope>,
952     new_blocks: RangeFrom<BasicBlock>,
953     destination: Place<'tcx>,
954     callsite_scope: SourceScopeData<'tcx>,
955     callsite: &'a CallSite<'tcx>,
956     cleanup_block: Option<BasicBlock>,
957     in_cleanup_block: bool,
958     tcx: TyCtxt<'tcx>,
959     expn_data: LocalExpnId,
960     always_live_locals: BitSet<Local>,
961 }
962
963 impl Integrator<'_, '_> {
964     fn map_local(&self, local: Local) -> Local {
965         let new = if local == RETURN_PLACE {
966             self.destination.local
967         } else {
968             let idx = local.index() - 1;
969             if idx < self.args.len() {
970                 self.args[idx]
971             } else {
972                 Local::new(self.new_locals.start.index() + (idx - self.args.len()))
973             }
974         };
975         trace!("mapping local `{:?}` to `{:?}`", local, new);
976         new
977     }
978
979     fn map_scope(&self, scope: SourceScope) -> SourceScope {
980         let new = SourceScope::new(self.new_scopes.start.index() + scope.index());
981         trace!("mapping scope `{:?}` to `{:?}`", scope, new);
982         new
983     }
984
985     fn map_block(&self, block: BasicBlock) -> BasicBlock {
986         let new = BasicBlock::new(self.new_blocks.start.index() + block.index());
987         trace!("mapping block `{:?}` to `{:?}`", block, new);
988         new
989     }
990
991     fn map_unwind(&self, unwind: Option<BasicBlock>) -> Option<BasicBlock> {
992         if self.in_cleanup_block {
993             if unwind.is_some() {
994                 bug!("cleanup on cleanup block");
995             }
996             return unwind;
997         }
998
999         match unwind {
1000             Some(target) => Some(self.map_block(target)),
1001             // Add an unwind edge to the original call's cleanup block
1002             None => self.cleanup_block,
1003         }
1004     }
1005 }
1006
1007 impl<'tcx> MutVisitor<'tcx> for Integrator<'_, 'tcx> {
1008     fn tcx(&self) -> TyCtxt<'tcx> {
1009         self.tcx
1010     }
1011
1012     fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) {
1013         *local = self.map_local(*local);
1014     }
1015
1016     fn visit_source_scope_data(&mut self, scope_data: &mut SourceScopeData<'tcx>) {
1017         self.super_source_scope_data(scope_data);
1018         if scope_data.parent_scope.is_none() {
1019             // Attach the outermost callee scope as a child of the callsite
1020             // scope, via the `parent_scope` and `inlined_parent_scope` chains.
1021             scope_data.parent_scope = Some(self.callsite.source_info.scope);
1022             assert_eq!(scope_data.inlined_parent_scope, None);
1023             scope_data.inlined_parent_scope = if self.callsite_scope.inlined.is_some() {
1024                 Some(self.callsite.source_info.scope)
1025             } else {
1026                 self.callsite_scope.inlined_parent_scope
1027             };
1028
1029             // Mark the outermost callee scope as an inlined one.
1030             assert_eq!(scope_data.inlined, None);
1031             scope_data.inlined = Some((self.callsite.callee, self.callsite.source_info.span));
1032         } else if scope_data.inlined_parent_scope.is_none() {
1033             // Make it easy to find the scope with `inlined` set above.
1034             scope_data.inlined_parent_scope = Some(self.map_scope(OUTERMOST_SOURCE_SCOPE));
1035         }
1036     }
1037
1038     fn visit_source_scope(&mut self, scope: &mut SourceScope) {
1039         *scope = self.map_scope(*scope);
1040     }
1041
1042     fn visit_span(&mut self, span: &mut Span) {
1043         // Make sure that all spans track the fact that they were inlined.
1044         *span = span.fresh_expansion(self.expn_data);
1045     }
1046
1047     fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
1048         for elem in place.projection {
1049             // FIXME: Make sure that return place is not used in an indexing projection, since it
1050             // won't be rebased as it is supposed to be.
1051             assert_ne!(ProjectionElem::Index(RETURN_PLACE), elem);
1052         }
1053
1054         // If this is the `RETURN_PLACE`, we need to rebase any projections onto it.
1055         let dest_proj_len = self.destination.projection.len();
1056         if place.local == RETURN_PLACE && dest_proj_len > 0 {
1057             let mut projs = Vec::with_capacity(dest_proj_len + place.projection.len());
1058             projs.extend(self.destination.projection);
1059             projs.extend(place.projection);
1060
1061             place.projection = self.tcx.intern_place_elems(&*projs);
1062         }
1063         // Handles integrating any locals that occur in the base
1064         // or projections
1065         self.super_place(place, context, location)
1066     }
1067
1068     fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
1069         self.in_cleanup_block = data.is_cleanup;
1070         self.super_basic_block_data(block, data);
1071         self.in_cleanup_block = false;
1072     }
1073
1074     fn visit_retag(&mut self, kind: &mut RetagKind, place: &mut Place<'tcx>, loc: Location) {
1075         self.super_retag(kind, place, loc);
1076
1077         // We have to patch all inlined retags to be aware that they are no longer
1078         // happening on function entry.
1079         if *kind == RetagKind::FnEntry {
1080             *kind = RetagKind::Default;
1081         }
1082     }
1083
1084     fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
1085         if let StatementKind::StorageLive(local) | StatementKind::StorageDead(local) =
1086             statement.kind
1087         {
1088             self.always_live_locals.remove(local);
1089         }
1090         self.super_statement(statement, location);
1091     }
1092
1093     fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, loc: Location) {
1094         // Don't try to modify the implicit `_0` access on return (`return` terminators are
1095         // replaced down below anyways).
1096         if !matches!(terminator.kind, TerminatorKind::Return) {
1097             self.super_terminator(terminator, loc);
1098         }
1099
1100         match terminator.kind {
1101             TerminatorKind::GeneratorDrop | TerminatorKind::Yield { .. } => bug!(),
1102             TerminatorKind::Goto { ref mut target } => {
1103                 *target = self.map_block(*target);
1104             }
1105             TerminatorKind::SwitchInt { ref mut targets, .. } => {
1106                 for tgt in targets.all_targets_mut() {
1107                     *tgt = self.map_block(*tgt);
1108                 }
1109             }
1110             TerminatorKind::Drop { ref mut target, ref mut unwind, .. }
1111             | TerminatorKind::DropAndReplace { ref mut target, ref mut unwind, .. } => {
1112                 *target = self.map_block(*target);
1113                 *unwind = self.map_unwind(*unwind);
1114             }
1115             TerminatorKind::Call { ref mut target, ref mut cleanup, .. } => {
1116                 if let Some(ref mut tgt) = *target {
1117                     *tgt = self.map_block(*tgt);
1118                 }
1119                 *cleanup = self.map_unwind(*cleanup);
1120             }
1121             TerminatorKind::Assert { ref mut target, ref mut cleanup, .. } => {
1122                 *target = self.map_block(*target);
1123                 *cleanup = self.map_unwind(*cleanup);
1124             }
1125             TerminatorKind::Return => {
1126                 terminator.kind = if let Some(tgt) = self.callsite.target {
1127                     TerminatorKind::Goto { target: tgt }
1128                 } else {
1129                     TerminatorKind::Unreachable
1130                 }
1131             }
1132             TerminatorKind::Resume => {
1133                 if let Some(tgt) = self.cleanup_block {
1134                     terminator.kind = TerminatorKind::Goto { target: tgt }
1135                 }
1136             }
1137             TerminatorKind::Abort => {}
1138             TerminatorKind::Unreachable => {}
1139             TerminatorKind::FalseEdge { ref mut real_target, ref mut imaginary_target } => {
1140                 *real_target = self.map_block(*real_target);
1141                 *imaginary_target = self.map_block(*imaginary_target);
1142             }
1143             TerminatorKind::FalseUnwind { real_target: _, unwind: _ } =>
1144             // see the ordering of passes in the optimized_mir query.
1145             {
1146                 bug!("False unwinds should have been removed before inlining")
1147             }
1148             TerminatorKind::InlineAsm { ref mut destination, ref mut cleanup, .. } => {
1149                 if let Some(ref mut tgt) = *destination {
1150                     *tgt = self.map_block(*tgt);
1151                 }
1152                 *cleanup = self.map_unwind(*cleanup);
1153             }
1154         }
1155     }
1156 }