Skip to content

Commit aa404c2

Browse files
committed
Make hash_result an Option.
1 parent e53404c commit aa404c2

File tree

7 files changed

+39
-41
lines changed

7 files changed

+39
-41
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ pub(crate) fn run_aot(
224224
tcx,
225225
(backend_config.clone(), cgu.name()),
226226
module_codegen,
227-
rustc_middle::dep_graph::hash_result,
227+
Some(rustc_middle::dep_graph::hash_result),
228228
);
229229

230230
if let Some((id, product)) = work_product {

compiler/rustc_codegen_gcc/src/base.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (Modul
5959
let start_time = Instant::now();
6060

6161
let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
62-
let (module, _) = tcx.dep_graph.with_task(dep_node, tcx, cgu_name, module_codegen, dep_graph::hash_result);
62+
let (module, _) = tcx.dep_graph.with_task(
63+
dep_node,
64+
tcx,
65+
cgu_name,
66+
module_codegen,
67+
Some(dep_graph::hash_result),
68+
);
6369
let time_to_codegen = start_time.elapsed();
6470
drop(prof_timer);
6571

compiler/rustc_codegen_llvm/src/base.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,13 @@ pub fn compile_codegen_unit(
113113
let start_time = Instant::now();
114114

115115
let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
116-
let (module, _) =
117-
tcx.dep_graph.with_task(dep_node, tcx, cgu_name, module_codegen, dep_graph::hash_result);
116+
let (module, _) = tcx.dep_graph.with_task(
117+
dep_node,
118+
tcx,
119+
cgu_name,
120+
module_codegen,
121+
Some(dep_graph::hash_result),
122+
);
118123
let time_to_codegen = start_time.elapsed();
119124

120125
// We assume that the cost to run LLVM on a CGU is proportional to

compiler/rustc_query_impl/src/plumbing.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,14 @@ macro_rules! is_eval_always {
291291
}
292292

293293
macro_rules! hash_result {
294-
([][$hcx:expr, $result:expr]) => {{
295-
dep_graph::hash_result($hcx, &$result)
294+
([]) => {{
295+
Some(dep_graph::hash_result)
296296
}};
297-
([(no_hash) $($rest:tt)*][$hcx:expr, $result:expr]) => {{
297+
([(no_hash) $($rest:tt)*]) => {{
298298
None
299299
}};
300-
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
301-
hash_result!([$($modifiers)*][$($args)*])
300+
([$other:tt $($modifiers:tt)*]) => {
301+
hash_result!([$($modifiers)*])
302302
};
303303
}
304304

@@ -378,6 +378,7 @@ macro_rules! define_queries {
378378
const ANON: bool = is_anon!([$($modifiers)*]);
379379
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
380380
const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$name;
381+
const HASH_RESULT: Option<fn(&mut StableHashingContext<'_>, &Self::Value) -> Fingerprint> = hash_result!([$($modifiers)*]);
381382

382383
type Cache = query_storage::$name<$tcx>;
383384

@@ -406,13 +407,6 @@ macro_rules! define_queries {
406407
}
407408
}
408409

409-
fn hash_result(
410-
_hcx: &mut StableHashingContext<'_>,
411-
_result: &Self::Value
412-
) -> Option<Fingerprint> {
413-
hash_result!([$($modifiers)*][_hcx, _result])
414-
}
415-
416410
fn handle_cycle_error(
417411
tcx: QueryCtxt<'tcx>,
418412
mut error: DiagnosticBuilder<'_>,

compiler/rustc_query_system/src/dep_graph/graph.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,13 @@ struct DepGraphData<K: DepKind> {
9696
dep_node_debug: Lock<FxHashMap<DepNode<K>, String>>,
9797
}
9898

99-
pub fn hash_result<R>(hcx: &mut StableHashingContext<'_>, result: &R) -> Option<Fingerprint>
99+
pub fn hash_result<R>(hcx: &mut StableHashingContext<'_>, result: &R) -> Fingerprint
100100
where
101101
R: for<'a> HashStable<StableHashingContext<'a>>,
102102
{
103103
let mut stable_hasher = StableHasher::new();
104104
result.hash_stable(hcx, &mut stable_hasher);
105-
106-
Some(stable_hasher.finish())
105+
stable_hasher.finish()
107106
}
108107

109108
impl<K: DepKind> DepGraph<K> {
@@ -215,7 +214,7 @@ impl<K: DepKind> DepGraph<K> {
215214
cx: Ctxt,
216215
arg: A,
217216
task: fn(Ctxt, A) -> R,
218-
hash_result: fn(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
217+
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
219218
) -> (R, DepNodeIndex) {
220219
if self.is_fully_enabled() {
221220
self.with_task_impl(key, cx, arg, task, hash_result)
@@ -234,7 +233,7 @@ impl<K: DepKind> DepGraph<K> {
234233
cx: Ctxt,
235234
arg: A,
236235
task: fn(Ctxt, A) -> R,
237-
hash_result: fn(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
236+
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
238237
) -> (R, DepNodeIndex) {
239238
// This function is only called when the graph is enabled.
240239
let data = self.data.as_ref().unwrap();
@@ -268,9 +267,11 @@ impl<K: DepKind> DepGraph<K> {
268267
let edges = task_deps.map_or_else(|| smallvec![], |lock| lock.into_inner().reads);
269268

270269
let dcx = cx.dep_context();
271-
let mut hcx = dcx.create_stable_hashing_context();
272270
let hashing_timer = dcx.profiler().incr_result_hashing();
273-
let current_fingerprint = hash_result(&mut hcx, &result);
271+
let current_fingerprint = hash_result.map(|f| {
272+
let mut hcx = dcx.create_stable_hashing_context();
273+
f(&mut hcx, &result)
274+
});
274275

275276
let print_status = cfg!(debug_assertions) && dcx.sess().opts.debugging_opts.dep_tasks;
276277

compiler/rustc_query_system/src/query/config.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
2424
pub dep_kind: CTX::DepKind,
2525
pub eval_always: bool,
2626

27-
pub hash_result: fn(&mut StableHashingContext<'_>, &V) -> Option<Fingerprint>,
27+
pub hash_result: Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>,
2828
pub handle_cycle_error: fn(CTX, DiagnosticBuilder<'_>) -> V,
2929
pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool,
3030
pub try_load_from_disk: fn(CTX, SerializedDepNodeIndex) -> Option<V>,
@@ -38,14 +38,6 @@ impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
3838
DepNode::construct(tcx, self.dep_kind, key)
3939
}
4040

41-
pub(crate) fn hash_result(
42-
&self,
43-
hcx: &mut StableHashingContext<'_>,
44-
value: &V,
45-
) -> Option<Fingerprint> {
46-
(self.hash_result)(hcx, value)
47-
}
48-
4941
pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool {
5042
(self.cache_on_disk)(tcx, key, value)
5143
}
@@ -59,6 +51,9 @@ pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
5951
const ANON: bool;
6052
const EVAL_ALWAYS: bool;
6153
const DEP_KIND: CTX::DepKind;
54+
const HASH_RESULT: Option<
55+
fn(hcx: &mut StableHashingContext<'_>, result: &Self::Value) -> Fingerprint,
56+
>;
6257

6358
type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
6459

@@ -75,9 +70,6 @@ pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
7570
// Don't use this method to compute query results, instead use the methods on TyCtxt
7671
fn compute_fn(tcx: CTX, key: &Self::Key) -> fn(CTX::DepContext, Self::Key) -> Self::Value;
7772

78-
fn hash_result(hcx: &mut StableHashingContext<'_>, result: &Self::Value)
79-
-> Option<Fingerprint>;
80-
8173
fn handle_cycle_error(tcx: CTX, diag: DiagnosticBuilder<'_>) -> Self::Value;
8274
}
8375

@@ -107,7 +99,7 @@ where
10799
anon: Q::ANON,
108100
dep_kind: Q::DEP_KIND,
109101
eval_always: Q::EVAL_ALWAYS,
110-
hash_result: Q::hash_result,
102+
hash_result: Q::HASH_RESULT,
111103
handle_cycle_error: Q::handle_cycle_error,
112104
cache_on_disk: Q::cache_on_disk,
113105
try_load_from_disk: Q::try_load_from_disk,

compiler/rustc_query_system/src/query/plumbing.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -577,12 +577,12 @@ fn incremental_verify_ich<CTX, K, V: Debug>(
577577
);
578578

579579
debug!("BEGIN verify_ich({:?})", dep_node);
580-
let mut hcx = tcx.create_stable_hashing_context();
581-
582-
let new_hash = query.hash_result(&mut hcx, result).unwrap_or(Fingerprint::ZERO);
583-
debug!("END verify_ich({:?})", dep_node);
584-
580+
let new_hash = query.hash_result.map_or(Fingerprint::ZERO, |f| {
581+
let mut hcx = tcx.create_stable_hashing_context();
582+
f(&mut hcx, result)
583+
});
585584
let old_hash = tcx.dep_graph().prev_fingerprint_of(dep_node);
585+
debug!("END verify_ich({:?})", dep_node);
586586

587587
if Some(new_hash) != old_hash {
588588
let run_cmd = if let Some(crate_name) = &tcx.sess().opts.crate_name {

0 commit comments

Comments
 (0)