Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 12d0849

Browse files
committedJun 15, 2021
Auto merge of #85154 - cjgillot:lessfn, r=bjorn3
Reduce amount of function pointers in query invocation. r? `@ghost`
2 parents 6936ca8 + 8ed82eb commit 12d0849

File tree

5 files changed

+167
-114
lines changed

5 files changed

+167
-114
lines changed
 

‎compiler/rustc_query_impl/src/keys.rs

Lines changed: 109 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ use rustc_span::{Span, DUMMY_SP};
1414
pub trait Key {
1515
/// Given an instance of this key, what crate is it referring to?
1616
/// This is used to find the provider.
17-
fn query_crate(&self) -> CrateNum;
17+
fn query_crate_is_local(&self) -> bool;
1818

1919
/// In the event that a cycle occurs, if no explicit span has been
2020
/// given for a query with key `self`, what span should we use?
2121
fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
2222
}
2323

2424
impl Key for () {
25-
fn query_crate(&self) -> CrateNum {
26-
LOCAL_CRATE
25+
#[inline(always)]
26+
fn query_crate_is_local(&self) -> bool {
27+
true
2728
}
2829

2930
fn default_span(&self, _: TyCtxt<'_>) -> Span {
@@ -32,8 +33,9 @@ impl Key for () {
3233
}
3334

3435
impl<'tcx> Key for ty::InstanceDef<'tcx> {
35-
fn query_crate(&self) -> CrateNum {
36-
LOCAL_CRATE
36+
#[inline(always)]
37+
fn query_crate_is_local(&self) -> bool {
38+
true
3739
}
3840

3941
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
@@ -42,8 +44,9 @@ impl<'tcx> Key for ty::InstanceDef<'tcx> {
4244
}
4345

4446
impl<'tcx> Key for ty::Instance<'tcx> {
45-
fn query_crate(&self) -> CrateNum {
46-
LOCAL_CRATE
47+
#[inline(always)]
48+
fn query_crate_is_local(&self) -> bool {
49+
true
4750
}
4851

4952
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
@@ -52,8 +55,9 @@ impl<'tcx> Key for ty::Instance<'tcx> {
5255
}
5356

5457
impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
55-
fn query_crate(&self) -> CrateNum {
56-
self.instance.query_crate()
58+
#[inline(always)]
59+
fn query_crate_is_local(&self) -> bool {
60+
true
5761
}
5862

5963
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
@@ -62,8 +66,9 @@ impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
6266
}
6367

6468
impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
65-
fn query_crate(&self) -> CrateNum {
66-
LOCAL_CRATE
69+
#[inline(always)]
70+
fn query_crate_is_local(&self) -> bool {
71+
true
6772
}
6873

6974
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
@@ -72,125 +77,139 @@ impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
7277
}
7378

7479
impl Key for CrateNum {
75-
fn query_crate(&self) -> CrateNum {
76-
*self
80+
#[inline(always)]
81+
fn query_crate_is_local(&self) -> bool {
82+
*self == LOCAL_CRATE
7783
}
7884
fn default_span(&self, _: TyCtxt<'_>) -> Span {
7985
DUMMY_SP
8086
}
8187
}
8288

8389
impl Key for LocalDefId {
84-
fn query_crate(&self) -> CrateNum {
85-
self.to_def_id().query_crate()
90+
#[inline(always)]
91+
fn query_crate_is_local(&self) -> bool {
92+
true
8693
}
8794
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
8895
self.to_def_id().default_span(tcx)
8996
}
9097
}
9198

9299
impl Key for DefId {
93-
fn query_crate(&self) -> CrateNum {
94-
self.krate
100+
#[inline(always)]
101+
fn query_crate_is_local(&self) -> bool {
102+
self.krate == LOCAL_CRATE
95103
}
96104
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
97105
tcx.def_span(*self)
98106
}
99107
}
100108

101109
impl Key for ty::WithOptConstParam<LocalDefId> {
102-
fn query_crate(&self) -> CrateNum {
103-
self.did.query_crate()
110+
#[inline(always)]
111+
fn query_crate_is_local(&self) -> bool {
112+
true
104113
}
105114
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
106115
self.did.default_span(tcx)
107116
}
108117
}
109118

110119
impl Key for (DefId, DefId) {
111-
fn query_crate(&self) -> CrateNum {
112-
self.0.krate
120+
#[inline(always)]
121+
fn query_crate_is_local(&self) -> bool {
122+
self.0.krate == LOCAL_CRATE
113123
}
114124
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
115125
self.1.default_span(tcx)
116126
}
117127
}
118128

119129
impl Key for (ty::Instance<'tcx>, LocalDefId) {
120-
fn query_crate(&self) -> CrateNum {
121-
self.0.query_crate()
130+
#[inline(always)]
131+
fn query_crate_is_local(&self) -> bool {
132+
true
122133
}
123134
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
124135
self.0.default_span(tcx)
125136
}
126137
}
127138

128139
impl Key for (DefId, LocalDefId) {
129-
fn query_crate(&self) -> CrateNum {
130-
self.0.krate
140+
#[inline(always)]
141+
fn query_crate_is_local(&self) -> bool {
142+
self.0.krate == LOCAL_CRATE
131143
}
132144
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
133145
self.1.default_span(tcx)
134146
}
135147
}
136148

137149
impl Key for (LocalDefId, DefId) {
138-
fn query_crate(&self) -> CrateNum {
139-
LOCAL_CRATE
150+
#[inline(always)]
151+
fn query_crate_is_local(&self) -> bool {
152+
true
140153
}
141154
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
142155
self.0.default_span(tcx)
143156
}
144157
}
145158

146159
impl Key for (DefId, Option<Ident>) {
147-
fn query_crate(&self) -> CrateNum {
148-
self.0.krate
160+
#[inline(always)]
161+
fn query_crate_is_local(&self) -> bool {
162+
self.0.krate == LOCAL_CRATE
149163
}
150164
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
151165
tcx.def_span(self.0)
152166
}
153167
}
154168

155169
impl Key for (DefId, LocalDefId, Ident) {
156-
fn query_crate(&self) -> CrateNum {
157-
self.0.krate
170+
#[inline(always)]
171+
fn query_crate_is_local(&self) -> bool {
172+
self.0.krate == LOCAL_CRATE
158173
}
159174
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
160175
self.1.default_span(tcx)
161176
}
162177
}
163178

164179
impl Key for (CrateNum, DefId) {
165-
fn query_crate(&self) -> CrateNum {
166-
self.0
180+
#[inline(always)]
181+
fn query_crate_is_local(&self) -> bool {
182+
self.0 == LOCAL_CRATE
167183
}
168184
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
169185
self.1.default_span(tcx)
170186
}
171187
}
172188

173189
impl Key for (DefId, SimplifiedType) {
174-
fn query_crate(&self) -> CrateNum {
175-
self.0.krate
190+
#[inline(always)]
191+
fn query_crate_is_local(&self) -> bool {
192+
self.0.krate == LOCAL_CRATE
176193
}
177194
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
178195
self.0.default_span(tcx)
179196
}
180197
}
181198

182199
impl<'tcx> Key for SubstsRef<'tcx> {
183-
fn query_crate(&self) -> CrateNum {
184-
LOCAL_CRATE
200+
#[inline(always)]
201+
fn query_crate_is_local(&self) -> bool {
202+
true
185203
}
186204
fn default_span(&self, _: TyCtxt<'_>) -> Span {
187205
DUMMY_SP
188206
}
189207
}
190208

191209
impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
192-
fn query_crate(&self) -> CrateNum {
193-
self.0.krate
210+
#[inline(always)]
211+
fn query_crate_is_local(&self) -> bool {
212+
self.0.krate == LOCAL_CRATE
194213
}
195214
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
196215
self.0.default_span(tcx)
@@ -203,125 +222,139 @@ impl<'tcx> Key
203222
(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
204223
)
205224
{
206-
fn query_crate(&self) -> CrateNum {
207-
(self.0).0.did.krate
225+
#[inline(always)]
226+
fn query_crate_is_local(&self) -> bool {
227+
(self.0).0.did.krate == LOCAL_CRATE
208228
}
209229
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
210230
(self.0).0.did.default_span(tcx)
211231
}
212232
}
213233

214234
impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
215-
fn query_crate(&self) -> CrateNum {
216-
LOCAL_CRATE
235+
#[inline(always)]
236+
fn query_crate_is_local(&self) -> bool {
237+
true
217238
}
218239
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
219240
self.0.default_span(tcx)
220241
}
221242
}
222243

223244
impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
224-
fn query_crate(&self) -> CrateNum {
225-
self.1.def_id().krate
245+
#[inline(always)]
246+
fn query_crate_is_local(&self) -> bool {
247+
self.1.def_id().krate == LOCAL_CRATE
226248
}
227249
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
228250
tcx.def_span(self.1.def_id())
229251
}
230252
}
231253

232254
impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
233-
fn query_crate(&self) -> CrateNum {
234-
LOCAL_CRATE
255+
#[inline(always)]
256+
fn query_crate_is_local(&self) -> bool {
257+
true
235258
}
236259
fn default_span(&self, _: TyCtxt<'_>) -> Span {
237260
DUMMY_SP
238261
}
239262
}
240263

241264
impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
242-
fn query_crate(&self) -> CrateNum {
243-
LOCAL_CRATE
265+
#[inline(always)]
266+
fn query_crate_is_local(&self) -> bool {
267+
true
244268
}
245269
fn default_span(&self, _: TyCtxt<'_>) -> Span {
246270
DUMMY_SP
247271
}
248272
}
249273

250274
impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
251-
fn query_crate(&self) -> CrateNum {
252-
self.def_id().krate
275+
#[inline(always)]
276+
fn query_crate_is_local(&self) -> bool {
277+
self.def_id().krate == LOCAL_CRATE
253278
}
254279
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
255280
tcx.def_span(self.def_id())
256281
}
257282
}
258283

259284
impl<'tcx> Key for GenericArg<'tcx> {
260-
fn query_crate(&self) -> CrateNum {
261-
LOCAL_CRATE
285+
#[inline(always)]
286+
fn query_crate_is_local(&self) -> bool {
287+
true
262288
}
263289
fn default_span(&self, _: TyCtxt<'_>) -> Span {
264290
DUMMY_SP
265291
}
266292
}
267293

268294
impl<'tcx> Key for mir::ConstantKind<'tcx> {
269-
fn query_crate(&self) -> CrateNum {
270-
LOCAL_CRATE
295+
#[inline(always)]
296+
fn query_crate_is_local(&self) -> bool {
297+
true
271298
}
272299
fn default_span(&self, _: TyCtxt<'_>) -> Span {
273300
DUMMY_SP
274301
}
275302
}
276303

277304
impl<'tcx> Key for &'tcx ty::Const<'tcx> {
278-
fn query_crate(&self) -> CrateNum {
279-
LOCAL_CRATE
305+
#[inline(always)]
306+
fn query_crate_is_local(&self) -> bool {
307+
true
280308
}
281309
fn default_span(&self, _: TyCtxt<'_>) -> Span {
282310
DUMMY_SP
283311
}
284312
}
285313

286314
impl<'tcx> Key for Ty<'tcx> {
287-
fn query_crate(&self) -> CrateNum {
288-
LOCAL_CRATE
315+
#[inline(always)]
316+
fn query_crate_is_local(&self) -> bool {
317+
true
289318
}
290319
fn default_span(&self, _: TyCtxt<'_>) -> Span {
291320
DUMMY_SP
292321
}
293322
}
294323

295324
impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
296-
fn query_crate(&self) -> CrateNum {
297-
LOCAL_CRATE
325+
#[inline(always)]
326+
fn query_crate_is_local(&self) -> bool {
327+
true
298328
}
299329
fn default_span(&self, _: TyCtxt<'_>) -> Span {
300330
DUMMY_SP
301331
}
302332
}
303333

304334
impl<'tcx> Key for ty::ParamEnv<'tcx> {
305-
fn query_crate(&self) -> CrateNum {
306-
LOCAL_CRATE
335+
#[inline(always)]
336+
fn query_crate_is_local(&self) -> bool {
337+
true
307338
}
308339
fn default_span(&self, _: TyCtxt<'_>) -> Span {
309340
DUMMY_SP
310341
}
311342
}
312343

313344
impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
314-
fn query_crate(&self) -> CrateNum {
315-
self.value.query_crate()
345+
#[inline(always)]
346+
fn query_crate_is_local(&self) -> bool {
347+
self.value.query_crate_is_local()
316348
}
317349
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
318350
self.value.default_span(tcx)
319351
}
320352
}
321353

322354
impl Key for Symbol {
323-
fn query_crate(&self) -> CrateNum {
324-
LOCAL_CRATE
355+
#[inline(always)]
356+
fn query_crate_is_local(&self) -> bool {
357+
true
325358
}
326359
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
327360
DUMMY_SP
@@ -331,8 +364,9 @@ impl Key for Symbol {
331364
/// Canonical query goals correspond to abstract trait operations that
332365
/// are not tied to any crate in particular.
333366
impl<'tcx, T> Key for Canonical<'tcx, T> {
334-
fn query_crate(&self) -> CrateNum {
335-
LOCAL_CRATE
367+
#[inline(always)]
368+
fn query_crate_is_local(&self) -> bool {
369+
true
336370
}
337371

338372
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
@@ -341,8 +375,9 @@ impl<'tcx, T> Key for Canonical<'tcx, T> {
341375
}
342376

343377
impl Key for (Symbol, u32, u32) {
344-
fn query_crate(&self) -> CrateNum {
345-
LOCAL_CRATE
378+
#[inline(always)]
379+
fn query_crate_is_local(&self) -> bool {
380+
true
346381
}
347382

348383
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
@@ -351,8 +386,9 @@ impl Key for (Symbol, u32, u32) {
351386
}
352387

353388
impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
354-
fn query_crate(&self) -> CrateNum {
355-
LOCAL_CRATE
389+
#[inline(always)]
390+
fn query_crate_is_local(&self) -> bool {
391+
true
356392
}
357393

358394
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {

‎compiler/rustc_query_impl/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ extern crate tracing;
1515
use rustc_data_structures::fingerprint::Fingerprint;
1616
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1717
use rustc_errors::{DiagnosticBuilder, Handler};
18-
use rustc_hir::def_id::LOCAL_CRATE;
1918
use rustc_middle::dep_graph;
2019
use rustc_middle::ich::StableHashingContext;
2120
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};

‎compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,14 @@ macro_rules! define_queries {
353353
}
354354

355355
#[inline]
356-
fn compute(tcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
357-
let is_local = key.query_crate() == LOCAL_CRATE;
358-
let provider = if is_local {
356+
fn compute_fn(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
357+
fn(TyCtxt<'tcx>, Self::Key) -> Self::Value
358+
{
359+
if key.query_crate_is_local() {
359360
tcx.queries.local_providers.$name
360361
} else {
361362
tcx.queries.extern_providers.$name
362-
};
363-
provider(*tcx, key)
363+
}
364364
}
365365

366366
fn hash_result(

‎compiler/rustc_query_system/src/query/config.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
2323
pub dep_kind: CTX::DepKind,
2424
pub eval_always: bool,
2525

26-
// Don't use this method to compute query results, instead use the methods on TyCtxt
27-
pub compute: fn(CTX, K) -> V,
28-
2926
pub hash_result: fn(&mut CTX::StableHashingContext, &V) -> Option<Fingerprint>,
3027
pub handle_cycle_error: fn(CTX, DiagnosticBuilder<'_>) -> V,
3128
pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool,
@@ -40,10 +37,6 @@ impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
4037
DepNode::construct(tcx, self.dep_kind, key)
4138
}
4239

43-
pub(crate) fn compute(&self, tcx: CTX, key: K) -> V {
44-
(self.compute)(tcx, key)
45-
}
46-
4740
pub(crate) fn hash_result(
4841
&self,
4942
hcx: &mut CTX::StableHashingContext,
@@ -79,7 +72,7 @@ pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
7972
CTX: 'a;
8073

8174
// Don't use this method to compute query results, instead use the methods on TyCtxt
82-
fn compute(tcx: CTX, key: Self::Key) -> Self::Value;
75+
fn compute_fn(tcx: CTX, key: &Self::Key) -> fn(CTX::DepContext, Self::Key) -> Self::Value;
8376

8477
fn hash_result(
8578
hcx: &mut CTX::StableHashingContext,
@@ -115,7 +108,6 @@ where
115108
anon: Q::ANON,
116109
dep_kind: Q::DEP_KIND,
117110
eval_always: Q::EVAL_ALWAYS,
118-
compute: Q::compute,
119111
hash_result: Q::hash_result,
120112
handle_cycle_error: Q::handle_cycle_error,
121113
cache_on_disk: Q::cache_on_disk,

‎compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ fn try_execute_query<CTX, C>(
428428
key: C::Key,
429429
lookup: QueryLookup,
430430
query: &QueryVtable<CTX, C::Key, C::Value>,
431+
compute: fn(CTX::DepContext, C::Key) -> C::Value,
431432
) -> C::Stored
432433
where
433434
C: QueryCache,
@@ -457,7 +458,7 @@ where
457458
// Fast path for when incr. comp. is off.
458459
if !dep_graph.is_fully_enabled() {
459460
let prof_timer = tcx.dep_context().profiler().query_provider();
460-
let result = tcx.start_query(job.id, None, || query.compute(tcx, key));
461+
let result = tcx.start_query(job.id, None, || compute(*tcx.dep_context(), key));
461462
let dep_node_index = dep_graph.next_virtual_depnode_index();
462463
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
463464
return job.complete(result, dep_node_index);
@@ -468,8 +469,9 @@ where
468469

469470
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
470471
tcx.start_query(job.id, diagnostics, || {
471-
dep_graph
472-
.with_anon_task(*tcx.dep_context(), query.dep_kind, || query.compute(tcx, key))
472+
dep_graph.with_anon_task(*tcx.dep_context(), query.dep_kind, || {
473+
compute(*tcx.dep_context(), key)
474+
})
473475
})
474476
});
475477

@@ -501,6 +503,7 @@ where
501503
dep_node_index,
502504
&dep_node,
503505
query,
506+
compute,
504507
),
505508
dep_node_index,
506509
)
@@ -511,7 +514,7 @@ where
511514
}
512515
}
513516

514-
let (result, dep_node_index) = force_query_with_job(tcx, key, job, dep_node, query);
517+
let (result, dep_node_index) = force_query_with_job(tcx, key, job, dep_node, query, compute);
515518
dep_graph.read_index(dep_node_index);
516519
result
517520
}
@@ -523,6 +526,7 @@ fn load_from_disk_and_cache_in_memory<CTX, K, V: Debug>(
523526
dep_node_index: DepNodeIndex,
524527
dep_node: &DepNode<CTX::DepKind>,
525528
query: &QueryVtable<CTX, K, V>,
529+
compute: fn(CTX::DepContext, K) -> V,
526530
) -> V
527531
where
528532
CTX: QueryContext,
@@ -565,7 +569,7 @@ where
565569
let prof_timer = tcx.dep_context().profiler().query_provider();
566570

567571
// The dep-graph for this computation is already in-place.
568-
let result = tcx.dep_context().dep_graph().with_ignore(|| query.compute(tcx, key));
572+
let result = tcx.dep_context().dep_graph().with_ignore(|| compute(*tcx.dep_context(), key));
569573

570574
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
571575

@@ -627,6 +631,7 @@ fn force_query_with_job<C, CTX>(
627631
job: JobOwner<'_, CTX::DepKind, C>,
628632
dep_node: DepNode<CTX::DepKind>,
629633
query: &QueryVtable<CTX, C::Key, C::Value>,
634+
compute: fn(CTX::DepContext, C::Key) -> C::Value,
630635
) -> (C::Stored, DepNodeIndex)
631636
where
632637
C: QueryCache,
@@ -653,17 +658,17 @@ where
653658
if query.eval_always {
654659
tcx.dep_context().dep_graph().with_eval_always_task(
655660
dep_node,
656-
tcx,
661+
*tcx.dep_context(),
657662
key,
658-
query.compute,
663+
compute,
659664
query.hash_result,
660665
)
661666
} else {
662667
tcx.dep_context().dep_graph().with_task(
663668
dep_node,
664-
tcx,
669+
*tcx.dep_context(),
665670
key,
666-
query.compute,
671+
compute,
667672
query.hash_result,
668673
)
669674
}
@@ -690,13 +695,14 @@ fn get_query_impl<CTX, C>(
690695
key: C::Key,
691696
lookup: QueryLookup,
692697
query: &QueryVtable<CTX, C::Key, C::Value>,
698+
compute: fn(CTX::DepContext, C::Key) -> C::Value,
693699
) -> C::Stored
694700
where
695701
CTX: QueryContext,
696702
C: QueryCache,
697703
C::Key: DepNodeParams<CTX::DepContext>,
698704
{
699-
try_execute_query(tcx, state, cache, span, key, lookup, query)
705+
try_execute_query(tcx, state, cache, span, key, lookup, query, compute)
700706
}
701707

702708
/// Ensure that either this query has all green inputs or been executed.
@@ -744,8 +750,10 @@ fn force_query_impl<CTX, C>(
744750
tcx: CTX,
745751
state: &QueryState<CTX::DepKind, C::Key>,
746752
cache: &QueryCacheStore<C>,
753+
key: C::Key,
747754
dep_node: DepNode<CTX::DepKind>,
748755
query: &QueryVtable<CTX, C::Key, C::Value>,
756+
compute: fn(CTX::DepContext, C::Key) -> C::Value,
749757
) -> bool
750758
where
751759
C: QueryCache,
@@ -754,18 +762,6 @@ where
754762
{
755763
debug_assert!(!query.anon);
756764

757-
if !<C::Key as DepNodeParams<CTX::DepContext>>::can_reconstruct_query_key() {
758-
return false;
759-
}
760-
761-
let key = if let Some(key) =
762-
<C::Key as DepNodeParams<CTX::DepContext>>::recover(*tcx.dep_context(), &dep_node)
763-
{
764-
key
765-
} else {
766-
return false;
767-
};
768-
769765
// We may be concurrently trying both execute and force a query.
770766
// Ensure that only one of them runs the query.
771767
let cached = cache.cache.lookup(cache, &key, |_, index| {
@@ -798,7 +794,7 @@ where
798794
TryGetJob::JobCompleted(_) => return true,
799795
};
800796

801-
force_query_with_job(tcx, key, job, dep_node, query);
797+
force_query_with_job(tcx, key, job, dep_node, query, compute);
802798

803799
true
804800
}
@@ -828,8 +824,17 @@ where
828824
}
829825

830826
debug!("ty::query::get_query<{}>(key={:?}, span={:?})", Q::NAME, key, span);
831-
let value =
832-
get_query_impl(tcx, Q::query_state(tcx), Q::query_cache(tcx), span, key, lookup, query);
827+
let compute = Q::compute_fn(tcx, &key);
828+
let value = get_query_impl(
829+
tcx,
830+
Q::query_state(tcx),
831+
Q::query_cache(tcx),
832+
span,
833+
key,
834+
lookup,
835+
query,
836+
compute,
837+
);
833838
Some(value)
834839
}
835840

@@ -843,5 +848,26 @@ where
843848
return false;
844849
}
845850

846-
force_query_impl(tcx, Q::query_state(tcx), Q::query_cache(tcx), *dep_node, &Q::VTABLE)
851+
if !<Q::Key as DepNodeParams<CTX::DepContext>>::can_reconstruct_query_key() {
852+
return false;
853+
}
854+
855+
let key = if let Some(key) =
856+
<Q::Key as DepNodeParams<CTX::DepContext>>::recover(*tcx.dep_context(), &dep_node)
857+
{
858+
key
859+
} else {
860+
return false;
861+
};
862+
863+
let compute = Q::compute_fn(tcx, &key);
864+
force_query_impl(
865+
tcx,
866+
Q::query_state(tcx),
867+
Q::query_cache(tcx),
868+
key,
869+
*dep_node,
870+
&Q::VTABLE,
871+
compute,
872+
)
847873
}

0 commit comments

Comments
 (0)
Please sign in to comment.