@@ -76,9 +76,13 @@ impl<'tcx> crate::MirPass<'tcx> for Inline {
7676pub struct ForceInline ;
7777
7878impl ForceInline {
79- pub fn should_run_pass_for_callee < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId ) -> bool {
79+ pub fn needs_callgraph_for_callee < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId ) -> bool {
8080 matches ! ( tcx. codegen_fn_attrs( def_id) . inline, InlineAttr :: Force { .. } )
8181 }
82+
83+ pub fn applies_for_resolved_callee < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId ) -> bool {
84+ matches ! ( tcx. codegen_fn_attrs( def_id) . inline, InlineAttr :: Force { .. } | InlineAttr :: Early )
85+ }
8286}
8387
8488impl < ' tcx > crate :: MirPass < ' tcx > for ForceInline {
@@ -117,7 +121,7 @@ trait Inliner<'tcx> {
117121 fn changed ( self ) -> bool ;
118122
119123 /// Should inlining happen for a given callee?
120- fn should_inline_for_callee ( & self , def_id : DefId ) -> bool ;
124+ fn should_inline_for_callee ( & self , instance_kind : & InstanceKind < ' tcx > ) -> bool ;
121125
122126 fn check_codegen_attributes_extra (
123127 & self ,
@@ -187,15 +191,21 @@ impl<'tcx> Inliner<'tcx> for ForceInliner<'tcx> {
187191 self . changed
188192 }
189193
190- fn should_inline_for_callee ( & self , def_id : DefId ) -> bool {
191- ForceInline :: should_run_pass_for_callee ( self . tcx ( ) , def_id)
194+ fn should_inline_for_callee ( & self , instance_kind : & InstanceKind < ' tcx > ) -> bool {
195+ if let InstanceKind :: Item ( def_id) = instance_kind
196+ && let InlineAttr :: Force { .. } | InlineAttr :: Early = self . tcx ( ) . codegen_fn_attrs ( def_id) . inline
197+ {
198+ true
199+ } else {
200+ false
201+ }
192202 }
193203
194204 fn check_codegen_attributes_extra (
195205 & self ,
196206 callee_attrs : & CodegenFnAttrs ,
197207 ) -> Result < ( ) , & ' static str > {
198- debug_assert_matches ! ( callee_attrs. inline, InlineAttr :: Force { .. } ) ;
208+ debug_assert_matches ! ( callee_attrs. inline, InlineAttr :: Force { .. } | InlineAttr :: Early ) ;
199209 Ok ( ( ) )
200210 }
201211
@@ -247,23 +257,26 @@ impl<'tcx> Inliner<'tcx> for ForceInliner<'tcx> {
247257
248258 fn on_inline_failure ( & self , callsite : & CallSite < ' tcx > , reason : & ' static str ) {
249259 let tcx = self . tcx ( ) ;
250- let InlineAttr :: Force { attr_span, reason : justification } =
251- tcx. codegen_fn_attrs ( callsite. callee . def_id ( ) ) . inline
252- else {
253- bug ! ( "called on item without required inlining" ) ;
254- } ;
255-
256- let call_span = callsite. source_info . span ;
257- tcx. dcx ( ) . emit_err ( crate :: errors:: ForceInlineFailure {
258- call_span,
259- attr_span,
260- caller_span : tcx. def_span ( self . def_id ) ,
261- caller : tcx. def_path_str ( self . def_id ) ,
262- callee_span : tcx. def_span ( callsite. callee . def_id ( ) ) ,
263- callee : tcx. def_path_str ( callsite. callee . def_id ( ) ) ,
264- reason,
265- justification : justification. map ( |sym| crate :: errors:: ForceInlineJustification { sym } ) ,
266- } ) ;
260+ match tcx. codegen_fn_attrs ( callsite. callee . def_id ( ) ) . inline {
261+ InlineAttr :: Early => {
262+ // Ok, we don't actually mind if this fails.
263+ bug ! ( "early failed for callsite {callsite:?} because {reason}" ) ;
264+ }
265+ InlineAttr :: Force { attr_span, reason : justification } => {
266+ let call_span = callsite. source_info . span ;
267+ tcx. dcx ( ) . emit_err ( crate :: errors:: ForceInlineFailure {
268+ call_span,
269+ attr_span,
270+ caller_span : tcx. def_span ( self . def_id ) ,
271+ caller : tcx. def_path_str ( self . def_id ) ,
272+ callee_span : tcx. def_span ( callsite. callee . def_id ( ) ) ,
273+ callee : tcx. def_path_str ( callsite. callee . def_id ( ) ) ,
274+ reason,
275+ justification : justification. map ( |sym| crate :: errors:: ForceInlineJustification { sym } ) ,
276+ } ) ;
277+ }
278+ _ => bug ! ( "called on item without required inlining" ) ,
279+ }
267280 }
268281}
269282
@@ -334,7 +347,7 @@ impl<'tcx> Inliner<'tcx> for NormalInliner<'tcx> {
334347 self . changed
335348 }
336349
337- fn should_inline_for_callee ( & self , _: DefId ) -> bool {
350+ fn should_inline_for_callee ( & self , _: & InstanceKind < ' tcx > ) -> bool {
338351 true
339352 }
340353
@@ -556,16 +569,16 @@ fn resolve_callsite<'tcx, I: Inliner<'tcx>>(
556569 if let TerminatorKind :: Call { ref func, fn_span, .. } = terminator. kind {
557570 let func_ty = func. ty ( caller_body, tcx) ;
558571 if let ty:: FnDef ( def_id, args) = * func_ty. kind ( ) {
559- if !inliner. should_inline_for_callee ( def_id) {
560- debug ! ( "not enabled" ) ;
561- return None ;
562- }
563-
564572 // To resolve an instance its args have to be fully normalized.
565573 let args = tcx. try_normalize_erasing_regions ( inliner. typing_env ( ) , args) . ok ( ) ?;
566574 let callee =
567575 Instance :: try_resolve ( tcx, inliner. typing_env ( ) , def_id, args) . ok ( ) . flatten ( ) ?;
568576
577+ if !inliner. should_inline_for_callee ( & callee. def ) {
578+ debug ! ( "not enabled" ) ;
579+ return None ;
580+ }
581+
569582 if let InstanceKind :: Virtual ( ..) | InstanceKind :: Intrinsic ( _) = callee. def {
570583 return None ;
571584 }
0 commit comments