@@ -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:: {
@@ -298,6 +298,9 @@ fn process_builtin_attrs(
298298 codegen_fn_attrs. patchable_function_entry =
299299 Some ( PatchableFunctionEntry :: from_prefix_and_entry ( * prefix, * entry) ) ;
300300 }
301+ AttributeKind :: RustcPanicEntrypoint => {
302+ codegen_fn_attrs. flags |= CodegenFnAttrFlags :: PANIC_ENTRYPOINT
303+ }
301304 _ => { }
302305 }
303306 }
@@ -391,6 +394,28 @@ fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut Code
391394 codegen_fn_attrs. flags |= CodegenFnAttrFlags :: NO_MANGLE ;
392395 }
393396 }
397+
398+ if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: PANIC_ENTRYPOINT ) {
399+ // Panic entrypoints are always cold.
400+ //
401+ // If we have immediate-abort enabled, we want panic entrypoints to be inlined. We have a
402+ // MIR transform that tries to patch out immediate-abort panic entrypoint calls, but it can
403+ // miss indirect calls. In such cases, this should encourage the optimizer to clean up the
404+ // mess that we missed.
405+ //
406+ // When the panic strategies that support panic messages are enabled, we want panic
407+ // entrypoints outlined and optimized for size.
408+ // Most panic entrypoints want #[track_caller] but not all, so we do not add it.
409+ // FIXME: It seems unlikely that all choices of #[track_caller] were deliberate, but it can
410+ // have a code size impact, so it makes sense to apply judiciously.
411+ codegen_fn_attrs. flags |= CodegenFnAttrFlags :: COLD ;
412+ if tcx. sess . panic_strategy ( ) == PanicStrategy :: ImmediateAbort {
413+ codegen_fn_attrs. inline = InlineAttr :: Always ;
414+ } else {
415+ codegen_fn_attrs. inline = InlineAttr :: Never ;
416+ codegen_fn_attrs. optimize = OptimizeAttr :: Size ;
417+ }
418+ }
394419}
395420
396421fn check_result (
0 commit comments