@@ -43,7 +43,14 @@ crate fn is_enabled(tcx: TyCtxt<'_>) -> bool {
4343 return enabled;
4444 }
4545
46- tcx. sess . mir_opt_level ( ) >= 3
46+ // If you change this optimization level, also change the level in
47+ // `mir_drops_elaborated_and_const_checked` for the call to `mir_inliner_callees`.
48+ // Otherwise you will get an ICE about stolen MIR.
49+ match tcx. sess . mir_opt_level ( ) {
50+ 0 | 1 => false ,
51+ 2 => tcx. sess . opts . incremental == None ,
52+ _ => true ,
53+ }
4754}
4855
4956impl < ' tcx > MirPass < ' tcx > for Inline {
@@ -325,6 +332,17 @@ impl Inliner<'tcx> {
325332 }
326333 }
327334
335+ // At mir-opt-level=1, only inline `#[inline(always)]` functions
336+ if self . tcx . sess . mir_opt_level ( ) == 1 && callee_attrs. inline != InlineAttr :: Always {
337+ return Err ( "at mir-opt-level=1, only inline(always) is inlined" ) ;
338+ }
339+
340+ if self . tcx . sess . mir_opt_level ( ) == 1
341+ && self . tcx . sess . opts . optimize != rustc_session:: config:: OptLevel :: Aggressive
342+ {
343+ return Err ( "at mir-opt-level=1, only inline if -O is specified" ) ;
344+ }
345+
328346 Ok ( ( ) )
329347 }
330348
@@ -470,8 +488,24 @@ impl Inliner<'tcx> {
470488 }
471489
472490 if let InlineAttr :: Always = callee_attrs. inline {
473- debug ! ( "INLINING {:?} because inline(always) [cost={}]" , callsite, cost) ;
474- Ok ( ( ) )
491+ if self . tcx . sess . mir_opt_level ( ) == 1 {
492+ if cost <= 25 {
493+ debug ! (
494+ "INLINING {:?} because inline(always) and [cost={} <= threshold=25]" ,
495+ callsite, cost
496+ ) ;
497+ Ok ( ( ) )
498+ } else {
499+ debug ! (
500+ "NOT inlining {:?} because inline(always) but [cost={} > threshold=25]" ,
501+ callsite, cost
502+ ) ;
503+ Err ( "cost above threshold" )
504+ }
505+ } else {
506+ debug ! ( "INLINING {:?} because inline(always) [cost={}]" , callsite, cost) ;
507+ Ok ( ( ) )
508+ }
475509 } else {
476510 if cost <= threshold {
477511 debug ! ( "INLINING {:?} [cost={} <= threshold={}]" , callsite, cost, threshold) ;
0 commit comments