@@ -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,24 @@ 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 them to be inlined.
399+ // They shouldn't be called, but on the off-chance that they are, they should be inlined.
400+ //
401+ // When the panic strategies that support panic messages are enabled, we want panic
402+ // entrypoints outlined and optimized for size.
403+ // Most panic entrypoints want #[track_caller] but not all, so we do not add it.
404+ codegen_fn_attrs. flags |= CodegenFnAttrFlags :: COLD ;
405+ if tcx. sess . panic_strategy ( ) == PanicStrategy :: ImmediateAbort {
406+ codegen_fn_attrs. inline = InlineAttr :: Always ;
407+ } else {
408+ codegen_fn_attrs. inline = InlineAttr :: Never ;
409+ codegen_fn_attrs. optimize = OptimizeAttr :: Size ;
410+ }
411+ }
394412}
395413
396414fn check_result (
0 commit comments