@@ -123,6 +123,20 @@ pub struct Session {
123
123
pub code_stats : RefCell < CodeStats > ,
124
124
125
125
next_node_id : Cell < ast:: NodeId > ,
126
+
127
+ /// If -zfuel=crate=n is specified, Some(crate).
128
+ optimization_fuel_crate : Option < String > ,
129
+ /// If -zfuel=crate=n is specified, initially set to n. Otherwise 0.
130
+ optimization_fuel_limit : Cell < u64 > ,
131
+ /// We're rejecting all further optimizations.
132
+ out_of_fuel : Cell < bool > ,
133
+
134
+ // The next two are public because the driver needs to read them.
135
+
136
+ /// If -zprint-fuel=crate, Some(crate).
137
+ pub print_fuel_crate : Option < String > ,
138
+ /// Always set to zero and incremented so that we can print fuel expended by a crate.
139
+ pub print_fuel : Cell < u64 > ,
126
140
}
127
141
128
142
pub struct PerfStats {
@@ -504,6 +518,33 @@ impl Session {
504
518
println ! ( "Total time spent decoding DefPath tables: {}" ,
505
519
duration_to_secs_str( self . perf_stats. decode_def_path_tables_time. get( ) ) ) ;
506
520
}
521
+
522
+ /// We want to know if we're allowed to do an optimization for crate crate.
523
+ /// This expends fuel if applicable, and records fuel if applicable.
524
+ pub fn consider_optimizing < T : Fn ( ) -> String > ( & self , crate_name : & str , msg : T ) -> bool {
525
+ let mut ret = true ;
526
+ match self . optimization_fuel_crate {
527
+ Some ( ref c) if c == crate_name => {
528
+ let fuel = self . optimization_fuel_limit . get ( ) ;
529
+ ret = fuel != 0 ;
530
+ if fuel == 0 && !self . out_of_fuel . get ( ) {
531
+ println ! ( "optimization-fuel-exhausted: {}" , msg( ) ) ;
532
+ self . out_of_fuel . set ( true ) ;
533
+ }
534
+ else {
535
+ self . optimization_fuel_limit . set ( fuel-1 ) ;
536
+ }
537
+ }
538
+ _ => { }
539
+ }
540
+ match self . print_fuel_crate {
541
+ Some ( ref c) if c == crate_name=> {
542
+ self . print_fuel . set ( self . print_fuel . get ( ) +1 ) ;
543
+ } ,
544
+ _ => { }
545
+ }
546
+ ret
547
+ }
507
548
}
508
549
509
550
pub fn build_session ( sopts : config:: Options ,
@@ -599,6 +640,12 @@ pub fn build_session_(sopts: config::Options,
599
640
}
600
641
) ;
601
642
643
+ let optimization_fuel_crate = sopts. debugging_opts . fuel . as_ref ( ) . map ( |i| i. 0 . clone ( ) ) ;
644
+ let optimization_fuel_limit = Cell :: new ( sopts. debugging_opts . fuel . as_ref ( )
645
+ . map ( |i| i. 1 ) . unwrap_or ( 0 ) ) ;
646
+ let print_fuel_crate = sopts. debugging_opts . print_fuel . clone ( ) ;
647
+ let print_fuel = Cell :: new ( 0 ) ;
648
+
602
649
let sess = Session {
603
650
dep_graph : dep_graph. clone ( ) ,
604
651
target : target_cfg,
@@ -640,6 +687,11 @@ pub fn build_session_(sopts: config::Options,
640
687
decode_def_path_tables_time : Cell :: new ( Duration :: from_secs ( 0 ) ) ,
641
688
} ,
642
689
code_stats : RefCell :: new ( CodeStats :: new ( ) ) ,
690
+ optimization_fuel_crate : optimization_fuel_crate,
691
+ optimization_fuel_limit : optimization_fuel_limit,
692
+ print_fuel_crate : print_fuel_crate,
693
+ print_fuel : print_fuel,
694
+ out_of_fuel : Cell :: new ( false ) ,
643
695
} ;
644
696
645
697
init_llvm ( & sess) ;
0 commit comments