@@ -38,7 +38,7 @@ struct CallSite<'tcx> {
3838
3939impl < ' tcx > MirPass < ' tcx > for Inline {
4040 fn run_pass ( & self , tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
41- if tcx. sess . opts . debugging_opts . mir_opt_level >= 2 {
41+ if tcx. sess . opts . debugging_opts . mir_opt_level >= 1 {
4242 if tcx. sess . opts . debugging_opts . instrument_coverage {
4343 // The current implementation of source code coverage injects code region counters
4444 // into the MIR, and assumes a 1-to-1 correspondence between MIR and source-code-
@@ -106,7 +106,12 @@ impl Inliner<'tcx> {
106106 continue ;
107107 }
108108
109- let callee_body = if let Some ( callee_def_id) = callsite. callee . as_local ( ) {
109+ let callee_body = if self . tcx . is_trivial_mir ( callsite. callee ) {
110+ self . tcx . optimized_mir ( callsite. callee )
111+ } else if self . tcx . sess . opts . debugging_opts . mir_opt_level < 2 {
112+ // Only inline trivial functions by default.
113+ continue ;
114+ } else if let Some ( callee_def_id) = callsite. callee . as_local ( ) {
110115 let callee_hir_id = self . tcx . hir ( ) . local_def_id_to_hir_id ( callee_def_id) ;
111116 // Avoid a cycle here by only using `optimized_mir` only if we have
112117 // a lower `HirId` than the callee. This ensures that the callee will
@@ -843,3 +848,44 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
843848 * scope = self . scope_map [ * scope] ;
844849 }
845850}
851+
852+ struct FunctionCallFinder {
853+ found : bool ,
854+ }
855+
856+ impl FunctionCallFinder {
857+ fn new ( ) -> Self {
858+ FunctionCallFinder { found : false }
859+ }
860+ }
861+
862+ impl < ' tcx > Visitor < ' tcx > for FunctionCallFinder {
863+ fn visit_terminator ( & mut self , terminator : & Terminator < ' tcx > , _location : Location ) {
864+ if let TerminatorKind :: Call { .. } = terminator. kind {
865+ self . found = true ;
866+ }
867+ }
868+ }
869+
870+ pub fn is_trivial_mir ( tcx : TyCtxt < ' tcx > , did : DefId ) -> bool {
871+ debug ! ( "is_trivial_mir({:?})" , did) ;
872+ if tcx. is_constructor ( did) {
873+ debug ! ( "is_trivial_mir = true (constructor)" ) ;
874+ return true ;
875+ }
876+
877+ if let Some ( did) = did. as_local ( ) {
878+ let body = tcx
879+ . mir_drops_elaborated_and_const_checked ( ty:: WithOptConstParam :: unknown ( did) )
880+ . borrow ( ) ;
881+ let mut finder = FunctionCallFinder :: new ( ) ;
882+ finder. visit_body ( & body) ;
883+ debug ! ( "is_trivial_mir = {}" , !finder. found) ;
884+ !finder. found
885+ } else {
886+ // This branch is only taken if no `optimized_mir` is available for
887+ // an extern crate, as `is_trivial_mir` has otherwise been encoded.
888+ debug ! ( "is_trivial_mir = false (no MIR available)" ) ;
889+ false
890+ }
891+ }
0 commit comments