@@ -4,7 +4,7 @@ use rustc_abi::{Align, ExternAbi};
44use rustc_ast:: expand:: autodiff_attrs:: { AutoDiffAttrs , DiffActivity , DiffMode } ;
55use rustc_ast:: { LitKind , MetaItem , MetaItemInner } ;
66use rustc_hir:: attrs:: {
7- AttributeKind , EiiImplResolution , InlineAttr , Linkage , RtsanSetting , UsedBy ,
7+ AttributeKind , EiiImplResolution , InlineAttr , Linkage , OptimizeAttr , RtsanSetting , UsedBy ,
88} ;
99use rustc_hir:: def:: DefKind ;
1010use rustc_hir:: def_id:: { DefId , LOCAL_CRATE , LocalDefId } ;
@@ -19,7 +19,7 @@ use rustc_middle::ty::{self as ty, TyCtxt};
1919use rustc_session:: lint;
2020use rustc_session:: parse:: feature_err;
2121use rustc_span:: { Span , sym} ;
22- use rustc_target:: spec:: Os ;
22+ use rustc_target:: spec:: { Os , PanicStrategy } ;
2323
2424use crate :: errors;
2525use crate :: target_features:: {
@@ -391,6 +391,28 @@ fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut Code
391391 codegen_fn_attrs. flags |= CodegenFnAttrFlags :: NO_MANGLE ;
392392 }
393393 }
394+
395+ if tcx. is_panic_entrypoint ( did) {
396+ // Panic entrypoints are always cold.
397+ //
398+ // If we have immediate-abort enabled, we want panic entrypoints to be inlined. We have a
399+ // MIR transform that tries to patch out immediate-abort panic entrypoint calls, but it can
400+ // miss indirect calls. In such cases, this should encourage the optimizer to clean up the
401+ // mess that we missed.
402+ //
403+ // When the panic strategies that support panic messages are enabled, we want panic
404+ // entrypoints outlined and optimized for size.
405+ // Most panic entrypoints want #[track_caller] but not all, so we do not add it.
406+ // FIXME: It seems unlikely that all choices of #[track_caller] were deliberate, but it can
407+ // have a code size impact, so it makes sense to apply judiciously.
408+ codegen_fn_attrs. flags |= CodegenFnAttrFlags :: COLD ;
409+ if tcx. sess . panic_strategy ( ) == PanicStrategy :: ImmediateAbort {
410+ codegen_fn_attrs. inline = InlineAttr :: Always ;
411+ } else {
412+ codegen_fn_attrs. inline = InlineAttr :: Never ;
413+ codegen_fn_attrs. optimize = OptimizeAttr :: Size ;
414+ }
415+ }
394416}
395417
396418fn check_result (
0 commit comments