@@ -76,9 +76,13 @@ impl<'tcx> crate::MirPass<'tcx> for Inline {
76
76
pub struct ForceInline ;
77
77
78
78
impl 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 {
80
80
matches ! ( tcx. codegen_fn_attrs( def_id) . inline, InlineAttr :: Force { .. } )
81
81
}
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
+ }
82
86
}
83
87
84
88
impl < ' tcx > crate :: MirPass < ' tcx > for ForceInline {
@@ -117,7 +121,7 @@ trait Inliner<'tcx> {
117
121
fn changed ( self ) -> bool ;
118
122
119
123
/// 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 ;
121
125
122
126
fn check_codegen_attributes_extra (
123
127
& self ,
@@ -187,15 +191,22 @@ impl<'tcx> Inliner<'tcx> for ForceInliner<'tcx> {
187
191
self . changed
188
192
}
189
193
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 =
197
+ self . tcx ( ) . codegen_fn_attrs ( def_id) . inline
198
+ {
199
+ true
200
+ } else {
201
+ false
202
+ }
192
203
}
193
204
194
205
fn check_codegen_attributes_extra (
195
206
& self ,
196
207
callee_attrs : & CodegenFnAttrs ,
197
208
) -> Result < ( ) , & ' static str > {
198
- debug_assert_matches ! ( callee_attrs. inline, InlineAttr :: Force { .. } ) ;
209
+ debug_assert_matches ! ( callee_attrs. inline, InlineAttr :: Force { .. } | InlineAttr :: Early ) ;
199
210
Ok ( ( ) )
200
211
}
201
212
@@ -247,23 +258,26 @@ impl<'tcx> Inliner<'tcx> for ForceInliner<'tcx> {
247
258
248
259
fn on_inline_failure ( & self , callsite : & CallSite < ' tcx > , reason : & ' static str ) {
249
260
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
- } ) ;
261
+ match tcx. codegen_fn_attrs ( callsite. callee . def_id ( ) ) . inline {
262
+ InlineAttr :: Early => {
263
+ // Ok, we don't actually mind if this fails.
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
276
+ . map ( |sym| crate :: errors:: ForceInlineJustification { sym } ) ,
277
+ } ) ;
278
+ }
279
+ _ => bug ! ( "called on item without required inlining" ) ,
280
+ }
267
281
}
268
282
}
269
283
@@ -334,7 +348,7 @@ impl<'tcx> Inliner<'tcx> for NormalInliner<'tcx> {
334
348
self . changed
335
349
}
336
350
337
- fn should_inline_for_callee ( & self , _: DefId ) -> bool {
351
+ fn should_inline_for_callee ( & self , _: & InstanceKind < ' tcx > ) -> bool {
338
352
true
339
353
}
340
354
@@ -556,16 +570,16 @@ fn resolve_callsite<'tcx, I: Inliner<'tcx>>(
556
570
if let TerminatorKind :: Call { ref func, fn_span, .. } = terminator. kind {
557
571
let func_ty = func. ty ( caller_body, tcx) ;
558
572
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
-
564
573
// To resolve an instance its args have to be fully normalized.
565
574
let args = tcx. try_normalize_erasing_regions ( inliner. typing_env ( ) , args) . ok ( ) ?;
566
575
let callee =
567
576
Instance :: try_resolve ( tcx, inliner. typing_env ( ) , def_id, args) . ok ( ) . flatten ( ) ?;
568
577
578
+ if !inliner. should_inline_for_callee ( & callee. def ) {
579
+ debug ! ( "not enabled" ) ;
580
+ return None ;
581
+ }
582
+
569
583
if let InstanceKind :: Virtual ( ..) | InstanceKind :: Intrinsic ( _) = callee. def {
570
584
return None ;
571
585
}
0 commit comments