- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Automatically enable cross-crate inlining for small functions #116505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| use rustc_attr::InlineAttr; | ||
| use rustc_hir::def::DefKind; | ||
| use rustc_hir::def_id::LocalDefId; | ||
| use rustc_middle::mir::visit::Visitor; | ||
| use rustc_middle::mir::*; | ||
| use rustc_middle::query::Providers; | ||
| use rustc_middle::ty::TyCtxt; | ||
| use rustc_session::config::OptLevel; | ||
|  | ||
| pub fn provide(providers: &mut Providers) { | ||
| providers.cross_crate_inlinable = cross_crate_inlinable; | ||
| } | ||
|  | ||
| fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { | ||
| let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id); | ||
| // If this has an extern indicator, then this function is globally shared and thus will not | ||
| // generate cgu-internal copies which would make it cross-crate inlinable. | ||
| if codegen_fn_attrs.contains_extern_indicator() { | ||
| return false; | ||
| } | ||
|  | ||
| // Obey source annotations first; this is important because it means we can use | ||
| // #[inline(never)] to force code generation. | ||
| match codegen_fn_attrs.inline { | ||
| InlineAttr::Never => return false, | ||
| InlineAttr::Hint | InlineAttr::Always => return true, | ||
| _ => {} | ||
| } | ||
|  | ||
| // This just reproduces the logic from Instance::requires_inline. | ||
| match tcx.def_kind(def_id) { | ||
| DefKind::Ctor(..) | DefKind::Closure => return true, | ||
| DefKind::Fn | DefKind::AssocFn => {} | ||
| _ => return false, | ||
| } | ||
|  | ||
| // Don't do any inference when incremental compilation is enabled; the additional inlining that | ||
| // inference permits also creates more work for small edits. | ||
| if tcx.sess.opts.incremental.is_some() { | ||
| return false; | ||
| } | ||
|  | ||
| // Don't do any inference unless optimizations are enabled. | ||
| if matches!(tcx.sess.opts.optimize, OptLevel::No) { | ||
| return false; | ||
| } | ||
|  | ||
| if !tcx.is_mir_available(def_id) { | ||
| return false; | ||
| } | ||
|  | ||
| let mir = tcx.optimized_mir(def_id); | ||
| let mut checker = | ||
| CostChecker { tcx, callee_body: mir, calls: 0, statements: 0, landing_pads: 0, resumes: 0 }; | ||
| checker.visit_body(mir); | ||
| checker.calls == 0 | ||
| && checker.resumes == 0 | ||
| && checker.landing_pads == 0 | ||
| && checker.statements | ||
| <= tcx.sess.opts.unstable_opts.cross_crate_inline_threshold.unwrap_or(100) | ||
| } | ||
|  | ||
| struct CostChecker<'b, 'tcx> { | ||
|         
                  cjgillot marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| tcx: TyCtxt<'tcx>, | ||
| callee_body: &'b Body<'tcx>, | ||
| calls: usize, | ||
| statements: usize, | ||
| landing_pads: usize, | ||
| resumes: usize, | ||
| } | ||
|  | ||
| impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> { | ||
| fn visit_statement(&mut self, statement: &Statement<'tcx>, _: Location) { | ||
| // Don't count StorageLive/StorageDead in the inlining cost. | ||
| match statement.kind { | ||
| StatementKind::StorageLive(_) | ||
| | StatementKind::StorageDead(_) | ||
| | StatementKind::Deinit(_) | ||
| | StatementKind::Nop => {} | ||
| _ => self.statements += 1, | ||
| } | ||
| } | ||
|  | ||
| fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, _: Location) { | ||
| let tcx = self.tcx; | ||
| match terminator.kind { | ||
| TerminatorKind::Drop { ref place, unwind, .. } => { | ||
| let ty = place.ty(self.callee_body, tcx).ty; | ||
| if !ty.is_trivially_pure_clone_copy() { | ||
| self.calls += 1; | ||
| if let UnwindAction::Cleanup(_) = unwind { | ||
| self.landing_pads += 1; | ||
| } | ||
| } | ||
| } | ||
| TerminatorKind::Call { unwind, .. } => { | ||
| self.calls += 1; | ||
| if let UnwindAction::Cleanup(_) = unwind { | ||
| self.landing_pads += 1; | ||
| } | ||
| } | ||
| TerminatorKind::Assert { unwind, .. } => { | ||
| self.calls += 1; | ||
| if let UnwindAction::Cleanup(_) = unwind { | ||
| self.landing_pads += 1; | ||
| } | ||
| } | ||
| TerminatorKind::UnwindResume => self.resumes += 1, | ||
| TerminatorKind::InlineAsm { unwind, .. } => { | ||
| self.statements += 1; | ||
| if let UnwindAction::Cleanup(_) = unwind { | ||
| self.landing_pads += 1; | ||
| } | ||
| } | ||
| TerminatorKind::Return => {} | ||
| _ => self.statements += 1, | ||
| } | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not super fond of the name.
Suggestion:
export_optimized_mir?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This query is used to determine 4 things:
We do not have name for this collective group of effects or properties. The names
cross_crate_inlinableandexport_optimized_mirjust call attention to one aspect or another. People looking for better final codegen will care about this query even if MIR inlining is disabled, and people looking for better compile times will care about this query even if LLVM inlining is disabled.Also, this adds an unstable flag
-Zcross-crate-inline-thresholdwhich I fully expect people to use at some point (when the cost model here is a bit better) in order to fix missing LLVM inlining. I do not think that calling the flag-Zexport-optimized-mir-thresholdor having different names for the flag and the query is an improvement.I would really like to have a name for the whole concept here.