Skip to content

Commit 9b47acf

Browse files
committed
Create a struct for optimization fuel data
1 parent 03b7cec commit 9b47acf

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

src/librustc/session/mod.rs

+26-30
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use util::common::ProfileQueriesMsg;
1717

1818
use rustc_data_structures::base_n;
1919
use rustc_data_structures::sync::{
20-
self, Lrc, Lock, OneThread, Once, RwLock, AtomicU64, AtomicUsize, AtomicBool, Ordering,
20+
self, Lrc, Lock, OneThread, Once, RwLock, AtomicU64, AtomicUsize, Ordering,
2121
Ordering::SeqCst,
2222
};
2323

@@ -50,6 +50,13 @@ pub mod config;
5050
pub mod filesearch;
5151
pub mod search_paths;
5252

53+
pub struct OptimizationFuel {
54+
/// If -zfuel=crate=n is specified, initially set to n. Otherwise 0.
55+
remaining: u64,
56+
/// We're rejecting all further optimizations.
57+
out_of_fuel: bool,
58+
}
59+
5360
/// Represents the data associated with a compilation
5461
/// session for a single crate.
5562
pub struct Session {
@@ -139,10 +146,9 @@ pub struct Session {
139146

140147
/// If -zfuel=crate=n is specified, Some(crate).
141148
optimization_fuel_crate: Option<String>,
142-
/// If -zfuel=crate=n is specified, initially set to n. Otherwise 0.
143-
optimization_fuel_limit: AtomicU64,
144-
/// We're rejecting all further optimizations.
145-
out_of_fuel: AtomicBool,
149+
150+
/// Tracks fuel info if If -zfuel=crate=n is specified
151+
optimization_fuel: Lock<OptimizationFuel>,
146152

147153
// The next two are public because the driver needs to read them.
148154
/// If -zprint-fuel=crate, Some(crate).
@@ -859,43 +865,32 @@ impl Session {
859865
self.perf_stats.normalize_projection_ty.load(Ordering::Relaxed));
860866
}
861867

862-
#[inline(never)]
863-
#[cold]
864-
pub fn consider_optimizing_cold<T: Fn() -> String>(&self, crate_name: &str, msg: T) -> bool {
868+
/// We want to know if we're allowed to do an optimization for crate foo from -z fuel=foo=n.
869+
/// This expends fuel if applicable, and records fuel if applicable.
870+
pub fn consider_optimizing<T: Fn() -> String>(&self, crate_name: &str, msg: T) -> bool {
865871
let mut ret = true;
866872
if let Some(ref c) = self.optimization_fuel_crate {
867873
if c == crate_name {
868874
assert_eq!(self.query_threads(), 1);
869-
let fuel = self.optimization_fuel_limit.load(SeqCst);
870-
ret = fuel != 0;
871-
if fuel == 0 && !self.out_of_fuel.load(SeqCst) {
875+
let mut fuel = self.optimization_fuel.lock();
876+
ret = fuel.remaining != 0;
877+
if fuel.remaining == 0 && !fuel.out_of_fuel {
872878
eprintln!("optimization-fuel-exhausted: {}", msg());
873-
self.out_of_fuel.store(true, SeqCst);
874-
} else if fuel > 0 {
875-
self.optimization_fuel_limit.store(fuel - 1, SeqCst);
879+
fuel.out_of_fuel = true;
880+
} else if fuel.remaining > 0 {
881+
fuel.remaining -= 1;
876882
}
877883
}
878884
}
879885
if let Some(ref c) = self.print_fuel_crate {
880886
if c == crate_name {
881887
assert_eq!(self.query_threads(), 1);
882-
self.print_fuel.store(self.print_fuel.load(SeqCst) + 1, SeqCst);
888+
self.print_fuel.fetch_add(1, SeqCst);
883889
}
884890
}
885891
ret
886892
}
887893

888-
/// We want to know if we're allowed to do an optimization for crate foo from -z fuel=foo=n.
889-
/// This expends fuel if applicable, and records fuel if applicable.
890-
#[inline(always)]
891-
pub fn consider_optimizing<T: Fn() -> String>(&self, crate_name: &str, msg: T) -> bool {
892-
if likely!(self.optimization_fuel_crate.is_none() && self.print_fuel_crate.is_none()) {
893-
true
894-
} else {
895-
self.consider_optimizing_cold(crate_name, msg)
896-
}
897-
}
898-
899894
/// Returns the number of query threads that should be used for this
900895
/// compilation
901896
pub fn query_threads_from_opts(opts: &config::Options) -> usize {
@@ -1140,8 +1135,10 @@ pub fn build_session_(
11401135
local_crate_source_file.map(|path| file_path_mapping.map_prefix(path).0);
11411136

11421137
let optimization_fuel_crate = sopts.debugging_opts.fuel.as_ref().map(|i| i.0.clone());
1143-
let optimization_fuel_limit =
1144-
AtomicU64::new(sopts.debugging_opts.fuel.as_ref().map(|i| i.1).unwrap_or(0));
1138+
let optimization_fuel = Lock::new(OptimizationFuel {
1139+
remaining: sopts.debugging_opts.fuel.as_ref().map(|i| i.1).unwrap_or(0),
1140+
out_of_fuel: false,
1141+
});
11451142
let print_fuel_crate = sopts.debugging_opts.print_fuel.clone();
11461143
let print_fuel = AtomicU64::new(0);
11471144

@@ -1205,10 +1202,9 @@ pub fn build_session_(
12051202
},
12061203
code_stats: Default::default(),
12071204
optimization_fuel_crate,
1208-
optimization_fuel_limit,
1205+
optimization_fuel,
12091206
print_fuel_crate,
12101207
print_fuel,
1211-
out_of_fuel: AtomicBool::new(false),
12121208
// Note that this is unsafe because it may misinterpret file descriptors
12131209
// on Unix as jobserver file descriptors. We hopefully execute this near
12141210
// the beginning of the process though to ensure we don't get false

0 commit comments

Comments
 (0)