|
| 1 | +use rustc_attr::InlineAttr; |
| 2 | +use rustc_hir::def::DefKind; |
| 3 | +use rustc_hir::def_id::LocalDefId; |
| 4 | +use rustc_middle::mir::visit::Visitor; |
| 5 | +use rustc_middle::mir::*; |
| 6 | +use rustc_middle::query::Providers; |
| 7 | +use rustc_middle::ty::TyCtxt; |
| 8 | +use rustc_session::config::OptLevel; |
| 9 | + |
| 10 | +pub fn provide(providers: &mut Providers) { |
| 11 | + providers.cross_crate_inlinable = cross_crate_inlinable; |
| 12 | +} |
| 13 | + |
| 14 | +fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { |
| 15 | + let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id); |
| 16 | + // If this has an extern indicator, then this function is globally shared and thus will not |
| 17 | + // generate cgu-internal copies which would make it cross-crate inlinable. |
| 18 | + if codegen_fn_attrs.contains_extern_indicator() { |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
| 22 | + // Obey source annotations first; this is important because it means we can use |
| 23 | + // #[inline(never)] to force code generation. |
| 24 | + match codegen_fn_attrs.inline { |
| 25 | + InlineAttr::Never => return false, |
| 26 | + InlineAttr::Hint | InlineAttr::Always => return true, |
| 27 | + _ => {} |
| 28 | + } |
| 29 | + |
| 30 | + // This just reproduces the logic from Instance::requires_inline. |
| 31 | + match tcx.def_kind(def_id) { |
| 32 | + DefKind::Ctor(..) | DefKind::Closure => return true, |
| 33 | + DefKind::Fn | DefKind::AssocFn => {} |
| 34 | + _ => return false, |
| 35 | + } |
| 36 | + |
| 37 | + // Don't do any inference when incremental compilation is enabled; the additional inlining that |
| 38 | + // inference permits also creates more work for small edits. |
| 39 | + if tcx.sess.opts.incremental.is_some() { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + // Don't do any inference unless optimizations are enabled. |
| 44 | + if matches!(tcx.sess.opts.optimize, OptLevel::No) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + if !tcx.is_mir_available(def_id) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + let mir = tcx.optimized_mir(def_id); |
| 53 | + let mut checker = |
| 54 | + CostChecker { tcx, callee_body: mir, calls: 0, statements: 0, landing_pads: 0, resumes: 0 }; |
| 55 | + checker.visit_body(mir); |
| 56 | + checker.calls == 0 |
| 57 | + && checker.resumes == 0 |
| 58 | + && checker.landing_pads == 0 |
| 59 | + && checker.statements |
| 60 | + <= tcx.sess.opts.unstable_opts.cross_crate_inline_threshold.unwrap_or(100) |
| 61 | +} |
| 62 | + |
| 63 | +struct CostChecker<'b, 'tcx> { |
| 64 | + tcx: TyCtxt<'tcx>, |
| 65 | + callee_body: &'b Body<'tcx>, |
| 66 | + calls: usize, |
| 67 | + statements: usize, |
| 68 | + landing_pads: usize, |
| 69 | + resumes: usize, |
| 70 | +} |
| 71 | + |
| 72 | +impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> { |
| 73 | + fn visit_statement(&mut self, statement: &Statement<'tcx>, _: Location) { |
| 74 | + // Don't count StorageLive/StorageDead in the inlining cost. |
| 75 | + match statement.kind { |
| 76 | + StatementKind::StorageLive(_) |
| 77 | + | StatementKind::StorageDead(_) |
| 78 | + | StatementKind::Deinit(_) |
| 79 | + | StatementKind::Nop => {} |
| 80 | + _ => self.statements += 1, |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, _: Location) { |
| 85 | + let tcx = self.tcx; |
| 86 | + match terminator.kind { |
| 87 | + TerminatorKind::Drop { ref place, unwind, .. } => { |
| 88 | + let ty = place.ty(self.callee_body, tcx).ty; |
| 89 | + if !ty.is_trivially_pure_clone_copy() { |
| 90 | + self.calls += 1; |
| 91 | + if let UnwindAction::Cleanup(_) = unwind { |
| 92 | + self.landing_pads += 1; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + TerminatorKind::Call { unwind, .. } => { |
| 97 | + self.calls += 1; |
| 98 | + if let UnwindAction::Cleanup(_) = unwind { |
| 99 | + self.landing_pads += 1; |
| 100 | + } |
| 101 | + } |
| 102 | + TerminatorKind::Assert { unwind, .. } => { |
| 103 | + self.calls += 1; |
| 104 | + if let UnwindAction::Cleanup(_) = unwind { |
| 105 | + self.landing_pads += 1; |
| 106 | + } |
| 107 | + } |
| 108 | + TerminatorKind::UnwindResume => self.resumes += 1, |
| 109 | + TerminatorKind::InlineAsm { unwind, .. } => { |
| 110 | + self.statements += 1; |
| 111 | + if let UnwindAction::Cleanup(_) = unwind { |
| 112 | + self.landing_pads += 1; |
| 113 | + } |
| 114 | + } |
| 115 | + TerminatorKind::Return => {} |
| 116 | + _ => self.statements += 1, |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments