From 52ef8cdeb83af35d816806ec6894dd8b42fbab65 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Jul 2022 14:41:19 +0200 Subject: [PATCH 1/4] Enable MIR inlining in incremental mode too. --- compiler/rustc_mir_transform/src/inline.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_mir_transform/src/inline.rs b/compiler/rustc_mir_transform/src/inline.rs index 1e46b0a0e8164..98187cdcd0dce 100644 --- a/compiler/rustc_mir_transform/src/inline.rs +++ b/compiler/rustc_mir_transform/src/inline.rs @@ -47,9 +47,8 @@ impl<'tcx> MirPass<'tcx> for Inline { match sess.mir_opt_level() { 0 | 1 => false, 2 => { - (sess.opts.optimize == OptLevel::Default - || sess.opts.optimize == OptLevel::Aggressive) - && sess.opts.incremental == None + sess.opts.optimize == OptLevel::Default + || sess.opts.optimize == OptLevel::Aggressive } _ => true, } From 4838281512b2b9eecf04adef90e81ce7d630ef84 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 24 Jul 2022 11:10:43 +0200 Subject: [PATCH 2/4] Unify new expansions with ones loaded from incremental cache. --- compiler/rustc_span/src/hygiene.rs | 100 ++++++++++++++++++++++------- 1 file changed, 78 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index e169d3c7cfb7c..e87b2466bf5f9 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -39,6 +39,7 @@ use rustc_data_structures::unhash::UnhashMap; use rustc_index::vec::IndexVec; use rustc_macros::HashStable_Generic; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; +use std::collections::hash_map::Entry; use std::fmt; use std::hash::Hash; use tracing::*; @@ -179,6 +180,9 @@ impl LocalExpnId { ExpnIndex::from_u32(self.as_u32()) } + /// Creates an empty expansion to be filled later. + /// This method should only be used outside of the incremental compilation engine. + /// Prefer using `fresh` when the expansion data is available. pub fn fresh_empty() -> LocalExpnId { HygieneData::with(|data| { let expn_id = data.local_expn_data.push(None); @@ -188,17 +192,51 @@ impl LocalExpnId { }) } + /// Creates an expansion. pub fn fresh(mut expn_data: ExpnData, ctx: impl HashStableContext) -> LocalExpnId { + debug_assert_eq!(expn_data.parent.krate, LOCAL_CRATE); + #[cfg(debug_assertions)] + let hashing_controls = ctx.hashing_controls(); + let expn_hash = update_disambiguator(&mut expn_data, ctx); + HygieneData::with(|data| match data.expn_hash_to_expn_id.entry(expn_hash) { + Entry::Vacant(v) => { + let expn_id = data.local_expn_data.push(Some(expn_data)); + let _eid = data.local_expn_hashes.push(expn_hash); + debug_assert_eq!(expn_id, _eid); + debug!(?expn_hash, ?expn_id); + v.insert(expn_id.to_expn_id()); + expn_id + } + Entry::Occupied(o) => { + let old_id = *o.get(); + // The hash starts with the local crate's hash, so this must be a local id. + let old_id = old_id.expect_local(); + #[cfg(debug_assertions)] + { + let old_data = data.local_expn_data[old_id].as_ref().unwrap(); + debug!(?old_id, ?old_data, ?expn_data); + expn_data.assert_identical(&old_data, hashing_controls); + } + old_id + } + }) + } + + /// Fill an empty expansion. + /// This method should only be used outside of the incremental compilation engine. + pub fn set_expn_data(self, mut expn_data: ExpnData, ctx: impl HashStableContext) { debug_assert_eq!(expn_data.parent.krate, LOCAL_CRATE); let expn_hash = update_disambiguator(&mut expn_data, ctx); HygieneData::with(|data| { - let expn_id = data.local_expn_data.push(Some(expn_data)); - let _eid = data.local_expn_hashes.push(expn_hash); - debug_assert_eq!(expn_id, _eid); - let _old_id = data.expn_hash_to_expn_id.insert(expn_hash, expn_id.to_expn_id()); + let old_expn_data = &mut data.local_expn_data[self]; + assert!(old_expn_data.is_none(), "expansion data is reset for an expansion ID"); + *old_expn_data = Some(expn_data); + debug_assert_eq!(data.local_expn_hashes[self].0, Fingerprint::ZERO); + data.local_expn_hashes[self] = expn_hash; + debug!(?expn_hash, ?self); + let _old_id = data.expn_hash_to_expn_id.insert(expn_hash, self.to_expn_id()); debug_assert!(_old_id.is_none()); - expn_id - }) + }); } #[inline] @@ -216,21 +254,6 @@ impl LocalExpnId { ExpnId { krate: LOCAL_CRATE, local_id: self.as_raw() } } - #[inline] - pub fn set_expn_data(self, mut expn_data: ExpnData, ctx: impl HashStableContext) { - debug_assert_eq!(expn_data.parent.krate, LOCAL_CRATE); - let expn_hash = update_disambiguator(&mut expn_data, ctx); - HygieneData::with(|data| { - let old_expn_data = &mut data.local_expn_data[self]; - assert!(old_expn_data.is_none(), "expansion data is reset for an expansion ID"); - *old_expn_data = Some(expn_data); - debug_assert_eq!(data.local_expn_hashes[self].0, Fingerprint::ZERO); - data.local_expn_hashes[self] = expn_hash; - let _old_id = data.expn_hash_to_expn_id.insert(expn_hash, self.to_expn_id()); - debug_assert!(_old_id.is_none()); - }); - } - #[inline] pub fn is_descendant_of(self, ancestor: LocalExpnId) -> bool { self.to_expn_id().is_descendant_of(ancestor.to_expn_id()) @@ -1039,6 +1062,36 @@ impl ExpnData { self.hash_stable(ctx, &mut hasher); hasher.finish() } + + #[inline] + #[cfg(debug_assertions)] + fn assert_identical(&self, other: &ExpnData, ctx: HashingControls) { + let ExpnData { + kind, + parent, + call_site, + def_site, + allow_internal_unstable, + allow_internal_unsafe, + local_inner_macros, + edition, + macro_def_id, + parent_module, + disambiguator: _, + } = self; + debug_assert_eq!(*kind, other.kind); + debug_assert_eq!(*parent, other.parent); + if ctx.hash_spans { + debug_assert_eq!(*call_site, other.call_site); + debug_assert_eq!(*def_site, other.def_site); + } + debug_assert_eq!(*allow_internal_unstable, other.allow_internal_unstable); + debug_assert_eq!(*allow_internal_unsafe, other.allow_internal_unsafe); + debug_assert_eq!(*local_inner_macros, other.local_inner_macros); + debug_assert_eq!(*edition, other.edition); + debug_assert_eq!(*macro_def_id, other.macro_def_id); + debug_assert_eq!(*parent_module, other.parent_module); + } } /// Expansion kind. @@ -1253,8 +1306,10 @@ pub fn register_local_expn_id(data: ExpnData, hash: ExpnHash) -> ExpnId { let expn_id = expn_id.to_expn_id(); + debug!(?hash, ?expn_id); let _old_id = hygiene_data.expn_hash_to_expn_id.insert(hash, expn_id); - debug_assert!(_old_id.is_none()); + debug_assert_eq!(_old_id, None); + expn_id }) } @@ -1273,6 +1328,7 @@ pub fn register_expn_id( debug_assert!(_old_data.is_none()); let _old_hash = hygiene_data.foreign_expn_hashes.insert(expn_id, hash); debug_assert!(_old_hash.is_none()); + debug!(?hash, ?expn_id); let _old_id = hygiene_data.expn_hash_to_expn_id.insert(hash, expn_id); debug_assert!(_old_id.is_none()); }); From 6f0f26be5120579bd3c6ec8122130a10f8702313 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 24 Jul 2022 11:10:59 +0200 Subject: [PATCH 3/4] Disable MIR inlining in codegen-units tests. --- .../codegen-units/partitioning/inlining-from-extern-crate.rs | 1 + .../codegen-units/partitioning/local-inlining-but-not-all.rs | 1 + src/test/codegen-units/partitioning/local-inlining.rs | 1 + src/test/codegen-units/partitioning/local-transitive-inlining.rs | 1 + 4 files changed, 4 insertions(+) diff --git a/src/test/codegen-units/partitioning/inlining-from-extern-crate.rs b/src/test/codegen-units/partitioning/inlining-from-extern-crate.rs index 1cc21632e4818..33e45e21c0351 100644 --- a/src/test/codegen-units/partitioning/inlining-from-extern-crate.rs +++ b/src/test/codegen-units/partitioning/inlining-from-extern-crate.rs @@ -4,6 +4,7 @@ // incremental // compile-flags:-Zprint-mono-items=lazy // compile-flags:-Zinline-in-all-cgus +// compile-flags:-Zinline-mir=no #![crate_type="lib"] diff --git a/src/test/codegen-units/partitioning/local-inlining-but-not-all.rs b/src/test/codegen-units/partitioning/local-inlining-but-not-all.rs index 318f0c28a5981..3457c8d64c64a 100644 --- a/src/test/codegen-units/partitioning/local-inlining-but-not-all.rs +++ b/src/test/codegen-units/partitioning/local-inlining-but-not-all.rs @@ -4,6 +4,7 @@ // incremental // compile-flags:-Zprint-mono-items=lazy // compile-flags:-Zinline-in-all-cgus=no +// compile-flags:-Zinline-mir=no #![allow(dead_code)] #![crate_type="lib"] diff --git a/src/test/codegen-units/partitioning/local-inlining.rs b/src/test/codegen-units/partitioning/local-inlining.rs index 841a428e9dd2f..5a22de3884510 100644 --- a/src/test/codegen-units/partitioning/local-inlining.rs +++ b/src/test/codegen-units/partitioning/local-inlining.rs @@ -4,6 +4,7 @@ // incremental // compile-flags:-Zprint-mono-items=lazy // compile-flags:-Zinline-in-all-cgus +// compile-flags:-Zinline-mir=no #![allow(dead_code)] #![crate_type="lib"] diff --git a/src/test/codegen-units/partitioning/local-transitive-inlining.rs b/src/test/codegen-units/partitioning/local-transitive-inlining.rs index 03c37954d1513..1f0c335bd08b1 100644 --- a/src/test/codegen-units/partitioning/local-transitive-inlining.rs +++ b/src/test/codegen-units/partitioning/local-transitive-inlining.rs @@ -4,6 +4,7 @@ // incremental // compile-flags:-Zprint-mono-items=lazy // compile-flags:-Zinline-in-all-cgus +// compile-flags:-Zinline-mir=no #![allow(dead_code)] #![crate_type="rlib"] From ffec672af2d26dbdbdddc90e18b6cf2fcda7073e Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 24 Jul 2022 11:11:07 +0200 Subject: [PATCH 4/4] Bless incremental test. --- src/test/incremental/hashes/function_interfaces.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/incremental/hashes/function_interfaces.rs b/src/test/incremental/hashes/function_interfaces.rs index 076eddaabc048..913a5e7e19876 100644 --- a/src/test/incremental/hashes/function_interfaces.rs +++ b/src/test/incremental/hashes/function_interfaces.rs @@ -305,7 +305,7 @@ pub fn return_impl_trait() -> i32 { } #[cfg(not(any(cfail1,cfail4)))] -#[rustc_clean(cfg = "cfail2", except = "hir_owner, hir_owner_nodes, typeck, fn_sig, optimized_mir")] +#[rustc_clean(cfg = "cfail2", except = "hir_owner, hir_owner_nodes, typeck, fn_sig")] #[rustc_clean(cfg = "cfail3")] #[rustc_clean(cfg = "cfail5", except = "hir_owner, hir_owner_nodes, typeck, fn_sig, optimized_mir")] #[rustc_clean(cfg = "cfail6")]