@@ -10,7 +10,6 @@ use rustc_middle::mir::visit::*;
10
10
use rustc_middle:: mir:: * ;
11
11
use rustc_middle:: ty:: TypeVisitableExt ;
12
12
use rustc_middle:: ty:: { self , Instance , InstanceDef , ParamEnv , Ty , TyCtxt } ;
13
- use rustc_session:: config:: OptLevel ;
14
13
use rustc_target:: abi:: FieldIdx ;
15
14
use rustc_target:: spec:: abi:: Abi ;
16
15
@@ -47,12 +46,8 @@ impl<'tcx> MirPass<'tcx> for Inline {
47
46
}
48
47
49
48
match sess. mir_opt_level ( ) {
50
- 0 | 1 => false ,
51
- 2 => {
52
- ( sess. opts . optimize == OptLevel :: Default
53
- || sess. opts . optimize == OptLevel :: Aggressive )
54
- && sess. opts . incremental == None
55
- }
49
+ 0 => false ,
50
+ 1 | 2 => sess. opts . incremental . is_none ( ) ,
56
51
_ => true ,
57
52
}
58
53
}
@@ -471,7 +466,15 @@ impl<'tcx> Inliner<'tcx> {
471
466
if callee_body. basic_blocks . len ( ) <= 3 {
472
467
threshold += threshold / 4 ;
473
468
}
474
- debug ! ( " final inline threshold = {}" , threshold) ;
469
+
470
+ if tcx. sess . mir_opt_level ( ) == 1 {
471
+ threshold = 2 * INSTR_COST ;
472
+
473
+ if callee_body. basic_blocks . len ( ) > 1 {
474
+ return Err ( "cost above threshold" ) ;
475
+ }
476
+ }
477
+ debug ! ( "final inline threshold = {}" , threshold) ;
475
478
476
479
// FIXME: Give a bonus to functions with only a single caller
477
480
@@ -481,6 +484,7 @@ impl<'tcx> Inliner<'tcx> {
481
484
instance : callsite. callee ,
482
485
callee_body,
483
486
cost : 0 ,
487
+ threshold,
484
488
} ;
485
489
486
490
// Traverse the MIR manually so we can account for the effects of inlining on the CFG.
@@ -493,6 +497,10 @@ impl<'tcx> Inliner<'tcx> {
493
497
494
498
let blk = & callee_body. basic_blocks [ bb] ;
495
499
checker. visit_basic_block_data ( bb, blk) ;
500
+ if callee_attrs. inline != InlineAttr :: Always && checker. cost > threshold {
501
+ debug ! ( "checker cost exceeded threshold partway through" ) ;
502
+ return Err ( "cost above threshold" ) ;
503
+ }
496
504
497
505
let term = blk. terminator ( ) ;
498
506
if let TerminatorKind :: Drop { ref place, target, unwind, replace : _ } = term. kind {
@@ -805,12 +813,17 @@ struct CostChecker<'b, 'tcx> {
805
813
tcx : TyCtxt < ' tcx > ,
806
814
param_env : ParamEnv < ' tcx > ,
807
815
cost : usize ,
816
+ threshold : usize ,
808
817
callee_body : & ' b Body < ' tcx > ,
809
818
instance : ty:: Instance < ' tcx > ,
810
819
}
811
820
812
821
impl < ' tcx > Visitor < ' tcx > for CostChecker < ' _ , ' tcx > {
813
822
fn visit_statement ( & mut self , statement : & Statement < ' tcx > , _: Location ) {
823
+ if self . cost > self . threshold {
824
+ return ;
825
+ }
826
+
814
827
// Don't count StorageLive/StorageDead in the inlining cost.
815
828
match statement. kind {
816
829
StatementKind :: StorageLive ( _)
0 commit comments