@@ -10,8 +10,8 @@ use rustc_middle::{bug, mir, span_bug};
1010use rustc_target:: callconv:: { FnAbi , PassMode } ;
1111use tracing:: { debug, instrument} ;
1212
13- use crate :: base;
1413use crate :: traits:: * ;
14+ use crate :: { base, errors} ;
1515
1616mod analyze;
1717mod block;
@@ -30,6 +30,8 @@ use self::debuginfo::{FunctionDebugContext, PerLocalVarDebugInfo};
3030use self :: operand:: { OperandRef , OperandValue } ;
3131use self :: place:: PlaceRef ;
3232
33+ const MIN_DANGEROUS_SIZE : u64 = 1024 * 1024 * 1024 * 1 ; // 1 GB
34+
3335// Used for tracking the state of generated basic blocks.
3436enum CachedLlbb < T > {
3537 /// Nothing created yet.
@@ -245,6 +247,14 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
245247 let layout = start_bx. layout_of ( fx. monomorphize ( decl. ty ) ) ;
246248 assert ! ( !layout. ty. has_erasable_regions( ) ) ;
247249
250+ if layout. size . bytes ( ) >= MIN_DANGEROUS_SIZE {
251+ let ( size_quantity, size_unit) = human_readable_bytes ( layout. size . bytes ( ) ) ;
252+ cx. tcx ( ) . dcx ( ) . emit_warn ( errors:: DangerousStackAllocation {
253+ span : decl. source_info . span ,
254+ output : format ! ( "{:.2} {}" , size_quantity, size_unit) ,
255+ } ) ;
256+ }
257+
248258 if local == mir:: RETURN_PLACE {
249259 match fx. fn_abi . ret . mode {
250260 PassMode :: Indirect { .. } => {
@@ -310,6 +320,14 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
310320 }
311321}
312322
323+ /// Formats a number of bytes into a human readable SI-prefixed size.
324+ /// Returns a tuple of `(quantity, units)`.
325+ pub fn human_readable_bytes ( bytes : u64 ) -> ( u64 , & ' static str ) {
326+ static UNITS : [ & str ; 7 ] = [ "B" , "KiB" , "MiB" , "GiB" , "TiB" , "PiB" , "EiB" ] ;
327+ let i = ( ( bytes. checked_ilog2 ( ) . unwrap_or ( 0 ) / 10 ) as usize ) . min ( UNITS . len ( ) - 1 ) ;
328+ ( bytes >> ( 10 * i) , UNITS [ i] )
329+ }
330+
313331/// Produces, for each argument, a `Value` pointing at the
314332/// argument's value. As arguments are places, these are always
315333/// indirect.
0 commit comments