Skip to content

Commit

Permalink
Rollup merge of rust-lang#137072 - Urgau:check-cfg-load-builtins-at-o…
Browse files Browse the repository at this point in the history
…nce, r=Noratrieb

Load all builtin targets at once instead of one by one in check-cfg

This PR adds a method on `rustc_target::Target` to load all the builtin targets at once, and then uses that method when constructing the `target_*` values in check-cfg instead of load loading each target one by one by their name, which requires a lookup and was more of a hack anyway.

This may give us some performance improvements as we won't need to do the lookup for the _currently_ 287 targets we have.
  • Loading branch information
matthiaskrgr authored Feb 16, 2025
2 parents 07584db + 6ec3cf9 commit 3091202
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
8 changes: 2 additions & 6 deletions compiler/rustc_session/src/config/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
use rustc_lint_defs::BuiltinLintDiag;
use rustc_lint_defs::builtin::EXPLICIT_BUILTIN_CFGS_IN_FLAGS;
use rustc_span::{Symbol, sym};
use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet, TARGETS, Target, TargetTuple};
use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet, Target};

use crate::Session;
use crate::config::{CrateType, FmtDebug};
Expand Down Expand Up @@ -432,11 +432,7 @@ impl CheckCfg {
panic!("unable to get all the check-cfg values buckets");
};

for target in TARGETS
.iter()
.map(|target| Target::expect_builtin(&TargetTuple::from_tuple(target)))
.chain(iter::once(current_target.clone()))
{
for target in Target::builtins().chain(iter::once(current_target.clone())) {
values_target_abi.insert(Symbol::intern(&target.options.abi));
values_target_arch.insert(Symbol::intern(&target.arch));
values_target_endian.insert(Symbol::intern(target.options.endian.as_str()));
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,14 @@ macro_rules! supported_targets {
Some(t)
}

fn load_all_builtins() -> impl Iterator<Item = Target> {
[
$( targets::$module::target, )+
]
.into_iter()
.map(|f| f())
}

#[cfg(test)]
mod tests {
// Cannot put this into a separate file without duplication, make an exception.
Expand Down Expand Up @@ -3360,6 +3368,11 @@ impl Target {
}
}

/// Load all built-in targets
pub fn builtins() -> impl Iterator<Item = Target> {
load_all_builtins()
}

/// Search for a JSON file specifying the given target tuple.
///
/// If none is found in `$RUST_TARGET_PATH`, look for a file called `target.json` inside the
Expand Down

0 comments on commit 3091202

Please sign in to comment.