Skip to content

Commit

Permalink
Auto merge of rust-lang#122207 - Urgau:cfg-check-cfg-cache, r=<try>
Browse files Browse the repository at this point in the history
Add cache-based fast path for cfgs and check-cfgs

TODO, waiting for perf.
  • Loading branch information
bors committed Mar 8, 2024
2 parents a655e64 + 24d06a9 commit 4ee97b1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 32 deletions.
62 changes: 32 additions & 30 deletions compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,38 +525,40 @@ pub fn cfg_matches(
) -> bool {
eval_condition(cfg, sess, features, &mut |cfg| {
try_gate_cfg(cfg.name, cfg.span, sess, features);
match sess.psess.check_config.expecteds.get(&cfg.name) {
Some(ExpectedValues::Some(values)) if !values.contains(&cfg.value) => {
sess.psess.buffer_lint_with_diagnostic(
UNEXPECTED_CFGS,
cfg.span,
lint_node_id,
if let Some(value) = cfg.value {
format!("unexpected `cfg` condition value: `{value}`")
} else {
format!("unexpected `cfg` condition value: (none)")
},
BuiltinLintDiag::UnexpectedCfgValue(
(cfg.name, cfg.name_span),
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
),
);
}
None if sess.psess.check_config.exhaustive_names => {
sess.psess.buffer_lint_with_diagnostic(
UNEXPECTED_CFGS,
cfg.span,
lint_node_id,
format!("unexpected `cfg` condition name: `{}`", cfg.name),
BuiltinLintDiag::UnexpectedCfgName(
(cfg.name, cfg.name_span),
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
),
);
sess.psess.config_cache.contains(&(cfg.name, cfg.value)) || {
match sess.psess.check_config.expecteds.get(&cfg.name) {
Some(ExpectedValues::Some(values)) if !values.contains(&cfg.value) => {
sess.psess.buffer_lint_with_diagnostic(
UNEXPECTED_CFGS,
cfg.span,
lint_node_id,
if let Some(value) = cfg.value {
format!("unexpected `cfg` condition value: `{value}`")
} else {
format!("unexpected `cfg` condition value: (none)")
},
BuiltinLintDiag::UnexpectedCfgValue(
(cfg.name, cfg.name_span),
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
),
);
}
None if sess.psess.check_config.exhaustive_names => {
sess.psess.buffer_lint_with_diagnostic(
UNEXPECTED_CFGS,
cfg.span,
lint_node_id,
format!("unexpected `cfg` condition name: `{}`", cfg.name),
BuiltinLintDiag::UnexpectedCfgName(
(cfg.name, cfg.name_span),
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
),
);
}
_ => { /* not unexpected */ }
}
_ => { /* not unexpected */ }
sess.psess.config.contains(&(cfg.name, cfg.value))
}
sess.psess.config.contains(&(cfg.name, cfg.value))
})
}

Expand Down
19 changes: 18 additions & 1 deletion compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc_query_impl::QueryCtxt;
use rustc_query_system::query::print_query_stack;
use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName};
use rustc_session::filesearch::sysroot_candidates;
use rustc_session::parse::ParseSess;
use rustc_session::parse::{CfgCache, ParseSess};
use rustc_session::{lint, CompilerIO, EarlyDiagCtxt, Session};
use rustc_span::source_map::FileLoader;
use rustc_span::symbol::sym;
Expand Down Expand Up @@ -402,6 +402,23 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
check_cfg.fill_well_known(&sess.target);
sess.psess.check_config = check_cfg;

let mut config_cache = CfgCache::default();
if sess.psess.check_config.expecteds.is_empty() {
config_cache.extend(sess.psess.config.iter().copied());
} else {
#[allow(rustc::potential_query_instability)]
for (n, v) in &sess.psess.check_config.expecteds {
if let ExpectedValues::Some(v) = v {
for v in v {
if sess.psess.config.contains(&(*n, *v)) {
config_cache.insert((*n, *v));
}
}
}
}
}
sess.psess.config_cache = config_cache;

if let Some(psess_created) = config.psess_created {
psess_created(&mut sess.psess);
}
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_session/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::lint::{
};
use crate::Session;
use rustc_ast::node_id::NodeId;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::sync::{AppendOnlyVec, Lock, Lrc};
use rustc_errors::emitter::{stderr_destination, HumanEmitter, SilentEmitter};
use rustc_errors::{
Expand Down Expand Up @@ -196,12 +196,15 @@ pub fn add_feature_diagnostics_for_issue<G: EmissionGuarantee>(
}
}

pub type CfgCache = FxHashSet<(Symbol, Option<Symbol>)>;

/// Info about a parsing session.
pub struct ParseSess {
pub dcx: DiagCtxt,
pub unstable_features: UnstableFeatures,
pub config: Cfg,
pub check_config: CheckCfg,
pub config_cache: CfgCache,
pub edition: Edition,
/// Places where raw identifiers were used. This is used to avoid complaining about idents
/// clashing with keywords in new editions.
Expand Down Expand Up @@ -250,6 +253,7 @@ impl ParseSess {
unstable_features: UnstableFeatures::from_environment(None),
config: Cfg::default(),
check_config: CheckCfg::default(),
config_cache: CfgCache::default(),
edition: ExpnId::root().expn_data().edition,
raw_identifier_spans: Default::default(),
bad_unicode_identifiers: Lock::new(Default::default()),
Expand Down

0 comments on commit 4ee97b1

Please sign in to comment.