Skip to content

Commit 2c1aedd

Browse files
incr.comp.: Cache TypeckTables and add -Zincremental-queries flag.
1 parent 0b14383 commit 2c1aedd

File tree

8 files changed

+95
-18
lines changed

8 files changed

+95
-18
lines changed

src/librustc/dep_graph/graph.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ impl DepGraph {
327327
}
328328
}
329329

330+
#[inline]
330331
pub fn fingerprint_of(&self, dep_node: &DepNode) -> Fingerprint {
331332
match self.fingerprints.borrow().get(dep_node) {
332333
Some(&fingerprint) => fingerprint,
@@ -340,6 +341,11 @@ impl DepGraph {
340341
self.data.as_ref().unwrap().previous.fingerprint_of(dep_node)
341342
}
342343

344+
#[inline]
345+
pub fn prev_dep_node_index_of(&self, dep_node: &DepNode) -> SerializedDepNodeIndex {
346+
self.data.as_ref().unwrap().previous.node_to_index(dep_node)
347+
}
348+
343349
/// Indicates that a previous work product exists for `v`. This is
344350
/// invoked during initial start-up based on what nodes are clean
345351
/// (and what files exist in the incr. directory).

src/librustc/dep_graph/prev.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ impl PreviousDepGraph {
4444
self.data.nodes[dep_node_index].0
4545
}
4646

47+
#[inline]
48+
pub fn node_to_index(&self, dep_node: &DepNode) -> SerializedDepNodeIndex {
49+
self.index[dep_node]
50+
}
51+
4752
#[inline]
4853
pub fn fingerprint_of(&self, dep_node: &DepNode) -> Option<Fingerprint> {
4954
self.index

src/librustc/hir/def_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl DefIndexAddressSpace {
184184

185185
/// A DefId identifies a particular *definition*, by combining a crate
186186
/// index and a def index.
187-
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, RustcDecodable, Hash, Copy)]
187+
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, Hash, Copy)]
188188
pub struct DefId {
189189
pub krate: CrateNum,
190190
pub index: DefIndex,

src/librustc/session/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
10421042
"enable incremental compilation (experimental)"),
10431043
incremental_cc: bool = (false, parse_bool, [UNTRACKED],
10441044
"enable cross-crate incremental compilation (even more experimental)"),
1045+
incremental_queries: bool = (false, parse_bool, [UNTRACKED],
1046+
"enable incremental compilation support for queries (experimental)"),
10451047
incremental_info: bool = (false, parse_bool, [UNTRACKED],
10461048
"print high-level information about incremental reuse (or the lack thereof)"),
10471049
incremental_dump_hash: bool = (false, parse_bool, [UNTRACKED],

src/librustc/ty/maps/config.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use dep_graph::SerializedDepNodeIndex;
1112
use hir::def_id::{CrateNum, DefId, DefIndex};
1213
use ty::{self, Ty, TyCtxt};
1314
use ty::maps::queries;
@@ -25,6 +26,16 @@ pub trait QueryConfig {
2526

2627
pub(super) trait QueryDescription<'tcx>: QueryConfig {
2728
fn describe(tcx: TyCtxt, key: Self::Key) -> String;
29+
30+
fn cache_on_disk(_: Self::Key) -> bool {
31+
false
32+
}
33+
34+
fn load_from_disk<'a>(_: TyCtxt<'a, 'tcx, 'tcx>,
35+
_: SerializedDepNodeIndex)
36+
-> Self::Value {
37+
bug!("QueryDescription::load_from_disk() called for unsupport query.")
38+
}
2839
}
2940

3041
impl<'tcx, M: QueryConfig<Key=DefId>> QueryDescription<'tcx> for M {
@@ -538,3 +549,19 @@ impl<'tcx> QueryDescription<'tcx> for queries::fully_normalize_monormophic_ty<'t
538549
format!("normalizing types")
539550
}
540551
}
552+
553+
impl<'tcx> QueryDescription<'tcx> for queries::typeck_tables_of<'tcx> {
554+
#[inline]
555+
fn cache_on_disk(def_id: Self::Key) -> bool {
556+
def_id.is_local()
557+
}
558+
559+
fn load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
560+
id: SerializedDepNodeIndex)
561+
-> Self::Value {
562+
let typeck_tables: ty::TypeckTables<'tcx> = tcx.on_disk_query_result_cache
563+
.load_query_result(tcx, id);
564+
tcx.alloc_tables(typeck_tables)
565+
}
566+
}
567+

src/librustc/ty/maps/on_disk_cache.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use syntax_pos::{BytePos, Span, NO_EXPANSION, DUMMY_SP};
3232
use ty;
3333
use ty::codec::{self as ty_codec, TyDecoder, TyEncoder};
3434
use ty::context::TyCtxt;
35+
use ty::maps::config::QueryDescription;
3536
use ty::subst::Substs;
3637

3738
// Some magic values used for verifying that encoding and decoding. These are
@@ -229,9 +230,22 @@ impl<'sess> OnDiskCache<'sess> {
229230

230231

231232
// Encode query results
232-
let query_result_index = EncodedQueryResultIndex::new();
233-
// ... we don't encode anything yet, actually
233+
let mut query_result_index = EncodedQueryResultIndex::new();
234234

235+
// Encode TypeckTables
236+
for (def_id, entry) in tcx.maps.typeck_tables_of.borrow().map.iter() {
237+
if ty::maps::queries::typeck_tables_of::cache_on_disk(*def_id) {
238+
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
239+
240+
// Record position of the cache entry
241+
query_result_index.push((dep_node, encoder.position()));
242+
243+
// Encode the type check tables with the SerializedDepNodeIndex
244+
// as tag.
245+
let typeck_tables: &ty::TypeckTables<'gcx> = &entry.value;
246+
encoder.encode_tagged(dep_node, typeck_tables)?;
247+
}
248+
}
235249

236250
// Encode query result index
237251
let query_result_index_pos = encoder.position() as u64;
@@ -522,9 +536,7 @@ impl<'a, 'tcx, 'x> SpecializedDecoder<Span> for CacheDecoder<'a, 'tcx, 'x> {
522536

523537
impl<'a, 'tcx, 'x> SpecializedDecoder<CrateNum> for CacheDecoder<'a, 'tcx, 'x> {
524538
fn specialized_decode(&mut self) -> Result<CrateNum, Self::Error> {
525-
let cnum = CrateNum::from_u32(u32::decode(self)?);
526-
let mapped = self.map_encoded_cnum_to_current(cnum);
527-
Ok(mapped)
539+
ty_codec::decode_cnum(self)
528540
}
529541
}
530542

@@ -576,6 +588,8 @@ impl<'a, 'tcx, 'x> SpecializedDecoder<hir::HirId> for CacheDecoder<'a, 'tcx, 'x>
576588
.as_ref()
577589
.unwrap()[&def_path_hash];
578590

591+
debug_assert!(def_id.is_local());
592+
579593
// The ItemLocalId needs no remapping.
580594
let local_id = hir::ItemLocalId::decode(self)?;
581595

@@ -721,6 +735,20 @@ impl<'enc, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>>
721735
}
722736
}
723737

738+
impl<'enc, 'tcx, E> SpecializedEncoder<hir::HirId> for CacheEncoder<'enc, 'tcx, E>
739+
where E: 'enc + ty_codec::TyEncoder
740+
{
741+
fn specialized_encode(&mut self, id: &hir::HirId) -> Result<(), Self::Error> {
742+
let hir::HirId {
743+
owner,
744+
local_id,
745+
} = *id;
746+
747+
owner.encode(self)?;
748+
local_id.encode(self)
749+
}
750+
}
751+
724752
// NodeIds are not stable across compilation sessions, so we store them in their
725753
// HirId representation. This allows use to map them to the current NodeId.
726754
impl<'enc, 'tcx, E> SpecializedEncoder<NodeId> for CacheEncoder<'enc, 'tcx, E>

src/librustc/ty/maps/plumbing.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -379,18 +379,26 @@ macro_rules! define_maps {
379379
{
380380
debug_assert!(tcx.dep_graph.is_green(dep_node_index));
381381

382-
// We don't do any caching yet, so recompute.
383-
// The diagnostics for this query have already been promoted to
384-
// the current session during try_mark_green(), so we can ignore
385-
// them here.
386-
let (result, _) = tcx.cycle_check(span, Query::$name(key), || {
387-
tcx.sess.diagnostic().track_diagnostics(|| {
388-
// The dep-graph for this computation is already in place
389-
tcx.dep_graph.with_ignore(|| {
390-
Self::compute_result(tcx, key)
382+
let result = if tcx.sess.opts.debugging_opts.incremental_queries &&
383+
Self::cache_on_disk(key) {
384+
let prev_dep_node_index =
385+
tcx.dep_graph.prev_dep_node_index_of(dep_node);
386+
Self::load_from_disk(tcx.global_tcx(), prev_dep_node_index)
387+
} else {
388+
let (result, _ ) = tcx.cycle_check(span, Query::$name(key), || {
389+
// The diagnostics for this query have already been
390+
// promoted to the current session during
391+
// try_mark_green(), so we can ignore them here.
392+
tcx.sess.diagnostic().track_diagnostics(|| {
393+
// The dep-graph for this computation is already in
394+
// place
395+
tcx.dep_graph.with_ignore(|| {
396+
Self::compute_result(tcx, key)
397+
})
391398
})
392-
})
393-
})?;
399+
})?;
400+
result
401+
};
394402

395403
// If -Zincremental-verify-ich is specified, re-hash results from
396404
// the cache and make sure that they have the expected fingerprint.

src/tools/compiletest/src/runtest.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,7 @@ actual:\n\
13871387
if let Some(ref incremental_dir) = self.props.incremental_dir {
13881388
rustc.args(&["-Z", &format!("incremental={}", incremental_dir.display())]);
13891389
rustc.args(&["-Z", "incremental-verify-ich"]);
1390+
rustc.args(&["-Z", "incremental-queries"]);
13901391
}
13911392

13921393
match self.config.mode {
@@ -2614,4 +2615,4 @@ fn read2_abbreviated(mut child: Child) -> io::Result<Output> {
26142615
stdout: stdout.into_bytes(),
26152616
stderr: stderr.into_bytes(),
26162617
})
2617-
}
2618+
}

0 commit comments

Comments
 (0)