@@ -12,7 +12,7 @@ use rustc_middle::ty::{self, TyCtxt};
1212use rustc_session:: config:: OptLevel ;
1313use rustc_session:: Session ;
1414use rustc_target:: spec:: abi:: Abi ;
15- use rustc_target:: spec:: { SanitizerSet , StackProbeType } ;
15+ use rustc_target:: spec:: { FramePointer , SanitizerSet , StackProbeType } ;
1616
1717use crate :: attributes;
1818use crate :: llvm:: AttributePlace :: Function ;
@@ -69,15 +69,25 @@ fn naked(val: &'ll Value, is_naked: bool) {
6969 Attribute :: Naked . toggle_llfn ( Function , val, is_naked) ;
7070}
7171
72- pub fn set_frame_pointer_elimination ( cx : & CodegenCx < ' ll , ' _ > , llfn : & ' ll Value ) {
73- if cx. sess ( ) . must_not_eliminate_frame_pointers ( ) {
74- llvm:: AddFunctionAttrStringValue (
75- llfn,
76- llvm:: AttributePlace :: Function ,
77- cstr ! ( "frame-pointer" ) ,
78- cstr ! ( "all" ) ,
79- ) ;
72+ pub fn set_frame_pointer_type ( cx : & CodegenCx < ' ll , ' _ > , llfn : & ' ll Value ) {
73+ let mut fp = cx. sess ( ) . target . frame_pointer ;
74+ // "mcount" function relies on stack pointer.
75+ // See <https://sourceware.org/binutils/docs/gprof/Implementation.html>.
76+ if cx. sess ( ) . instrument_mcount ( ) || matches ! ( cx. sess( ) . opts. cg. force_frame_pointers, Some ( true ) )
77+ {
78+ fp = FramePointer :: Always ;
8079 }
80+ let attr_value = match fp {
81+ FramePointer :: Always => cstr ! ( "all" ) ,
82+ FramePointer :: NonLeaf => cstr ! ( "non-leaf" ) ,
83+ FramePointer :: MayOmit => return ,
84+ } ;
85+ llvm:: AddFunctionAttrStringValue (
86+ llfn,
87+ llvm:: AttributePlace :: Function ,
88+ cstr ! ( "frame-pointer" ) ,
89+ attr_value,
90+ ) ;
8191}
8292
8393/// Tell LLVM what instrument function to insert.
@@ -254,7 +264,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
254264 }
255265
256266 // FIXME: none of these three functions interact with source level attributes.
257- set_frame_pointer_elimination ( cx, llfn) ;
267+ set_frame_pointer_type ( cx, llfn) ;
258268 set_instrument_function ( cx, llfn) ;
259269 set_probestack ( cx, llfn) ;
260270
0 commit comments