Skip to content

Commit 796bc7e

Browse files
authored
Rollup merge of #95202 - Urgau:check-cfg-perf-well-known-values, r=petrochenkov
Reduce the cost of loading all built-ins targets This PR started by measuring the exact slowdown of checking of well known conditional values. Than this PR implemented some technics to reduce the cost of loading all built-ins targets. cf. #82450 (comment)
2 parents 2ad4eb2 + 1a1f5b8 commit 796bc7e

File tree

226 files changed

+1337
-1296
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+1337
-1296
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,12 @@ pub(crate) fn run_aot(
304304
};
305305

306306
// FIXME handle `-Ctarget-cpu=native`
307-
let target_cpu =
308-
tcx.sess.opts.cg.target_cpu.as_ref().unwrap_or(&tcx.sess.target.cpu).to_owned();
307+
let target_cpu = match tcx.sess.opts.cg.target_cpu {
308+
Some(ref name) => name,
309+
None => tcx.sess.target.cpu.as_ref(),
310+
}
311+
.to_owned();
312+
309313
Box::new((
310314
CodegenResults {
311315
modules,

compiler/rustc_codegen_gcc/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,10 @@ fn handle_native(name: &str) -> &str {
287287
}
288288

289289
pub fn target_cpu(sess: &Session) -> &str {
290-
let name = sess.opts.cg.target_cpu.as_ref().unwrap_or(&sess.target.cpu);
291-
handle_native(name)
290+
match sess.opts.cg.target_cpu {
291+
Some(ref name) => handle_native(name),
292+
None => handle_native(sess.target.cpu.as_ref()),
293+
}
292294
}
293295

294296
pub fn target_features(sess: &Session) -> Vec<Symbol> {

compiler/rustc_codegen_llvm/src/attributes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn instrument_function_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribu
116116

117117
// The function name varies on platforms.
118118
// See test/CodeGen/mcount.c in clang.
119-
let mcount_name = cx.sess().target.mcount.as_str();
119+
let mcount_name = cx.sess().target.mcount.as_ref();
120120

121121
Some(llvm::CreateAttrStringValue(
122122
cx.llcx,

compiler/rustc_codegen_llvm/src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
14521452
}
14531453

14541454
fn fptoint_sat_broken_in_llvm(&self) -> bool {
1455-
match self.tcx.sess.target.arch.as_str() {
1455+
match self.tcx.sess.target.arch.as_ref() {
14561456
// FIXME - https://bugs.llvm.org/show_bug.cgi?id=50083
14571457
"riscv64" => llvm_util::get_version() < (13, 0, 0),
14581458
_ => false,

compiler/rustc_codegen_llvm/src/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub unsafe fn create_module<'ll>(
134134
let mod_name = SmallCStr::new(mod_name);
135135
let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
136136

137-
let mut target_data_layout = sess.target.data_layout.clone();
137+
let mut target_data_layout = sess.target.data_layout.to_string();
138138
let llvm_version = llvm_util::get_version();
139139
if llvm_version < (13, 0, 0) {
140140
if sess.target.arch == "powerpc64" {
@@ -859,7 +859,7 @@ impl<'ll> CodegenCx<'ll, '_> {
859859

860860
// This isn't an "LLVM intrinsic", but LLVM's optimization passes
861861
// recognize it like one and we assume it exists in `core::slice::cmp`
862-
match self.sess().target.arch.as_str() {
862+
match self.sess().target.arch.as_ref() {
863863
"avr" | "msp430" => ifn!("memcmp", fn(i8p, i8p, t_isize) -> t_i16),
864864
_ => ifn!("memcmp", fn(i8p, i8p, t_isize) -> t_i32),
865865
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
329329
let b_ptr = self.bitcast(b, i8p_ty);
330330
let n = self.const_usize(layout.size().bytes());
331331
let cmp = self.call_intrinsic("memcmp", &[a_ptr, b_ptr, n]);
332-
match self.cx.sess().target.arch.as_str() {
332+
match self.cx.sess().target.arch.as_ref() {
333333
"avr" | "msp430" => self.icmp(IntPredicate::IntEQ, cmp, self.const_i16(0)),
334334
_ => self.icmp(IntPredicate::IntEQ, cmp, self.const_i32(0)),
335335
}

compiler/rustc_codegen_llvm/src/llvm_util.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ unsafe fn configure_llvm(sess: &Session) {
6161
full_arg.trim().split(|c: char| c == '=' || c.is_whitespace()).next().unwrap_or("")
6262
}
6363

64-
let cg_opts = sess.opts.cg.llvm_args.iter();
65-
let tg_opts = sess.target.llvm_args.iter();
64+
let cg_opts = sess.opts.cg.llvm_args.iter().map(AsRef::as_ref);
65+
let tg_opts = sess.target.llvm_args.iter().map(AsRef::as_ref);
6666
let sess_args = cg_opts.chain(tg_opts);
6767

6868
let user_specified_args: FxHashSet<_> =
@@ -375,8 +375,10 @@ fn handle_native(name: &str) -> &str {
375375
}
376376

377377
pub fn target_cpu(sess: &Session) -> &str {
378-
let name = sess.opts.cg.target_cpu.as_ref().unwrap_or(&sess.target.cpu);
379-
handle_native(name)
378+
match sess.opts.cg.target_cpu {
379+
Some(ref name) => handle_native(name),
380+
None => handle_native(sess.target.cpu.as_ref()),
381+
}
380382
}
381383

382384
/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,

compiler/rustc_codegen_ssa/src/back/link.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use std::ffi::OsString;
4040
use std::fs::{File, OpenOptions};
4141
use std::io::{BufWriter, Write};
4242
use std::lazy::OnceCell;
43+
use std::ops::Deref;
4344
use std::path::{Path, PathBuf};
4445
use std::process::{ExitStatus, Output, Stdio};
4546
use std::{ascii, char, env, fmt, fs, io, mem, str};
@@ -674,11 +675,11 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
674675

675676
linker::disable_localization(&mut cmd);
676677

677-
for &(ref k, ref v) in &sess.target.link_env {
678-
cmd.env(k, v);
678+
for &(ref k, ref v) in sess.target.link_env.as_ref() {
679+
cmd.env(k.as_ref(), v.as_ref());
679680
}
680-
for k in &sess.target.link_env_remove {
681-
cmd.env_remove(k);
681+
for k in sess.target.link_env_remove.as_ref() {
682+
cmd.env_remove(k.as_ref());
682683
}
683684

684685
if sess.opts.prints.contains(&PrintRequest::LinkArgs) {
@@ -1216,7 +1217,7 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
12161217

12171218
if let Some(ret) = infer_from(
12181219
sess,
1219-
sess.target.linker.clone().map(PathBuf::from),
1220+
sess.target.linker.as_deref().map(PathBuf::from),
12201221
Some(sess.target.linker_flavor),
12211222
) {
12221223
return ret;
@@ -1586,7 +1587,7 @@ fn add_post_link_objects(
15861587
/// FIXME: Determine where exactly these args need to be inserted.
15871588
fn add_pre_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
15881589
if let Some(args) = sess.target.pre_link_args.get(&flavor) {
1589-
cmd.args(args);
1590+
cmd.args(args.iter().map(Deref::deref));
15901591
}
15911592
cmd.args(&sess.opts.debugging_opts.pre_link_args);
15921593
}
@@ -1602,7 +1603,7 @@ fn add_link_script(cmd: &mut dyn Linker, sess: &Session, tmpdir: &Path, crate_ty
16021603
let file_name = ["rustc", &sess.target.llvm_target, "linkfile.ld"].join("-");
16031604

16041605
let path = tmpdir.join(file_name);
1605-
if let Err(e) = fs::write(&path, script) {
1606+
if let Err(e) = fs::write(&path, script.as_ref()) {
16061607
sess.fatal(&format!("failed to write link script to {}: {}", path.display(), e));
16071608
}
16081609

@@ -1634,23 +1635,23 @@ fn add_late_link_args(
16341635
});
16351636
if any_dynamic_crate {
16361637
if let Some(args) = sess.target.late_link_args_dynamic.get(&flavor) {
1637-
cmd.args(args);
1638+
cmd.args(args.iter().map(Deref::deref));
16381639
}
16391640
} else {
16401641
if let Some(args) = sess.target.late_link_args_static.get(&flavor) {
1641-
cmd.args(args);
1642+
cmd.args(args.iter().map(Deref::deref));
16421643
}
16431644
}
16441645
if let Some(args) = sess.target.late_link_args.get(&flavor) {
1645-
cmd.args(args);
1646+
cmd.args(args.iter().map(Deref::deref));
16461647
}
16471648
}
16481649

16491650
/// Add arbitrary "post-link" args defined by the target spec.
16501651
/// FIXME: Determine where exactly these args need to be inserted.
16511652
fn add_post_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
16521653
if let Some(args) = sess.target.post_link_args.get(&flavor) {
1653-
cmd.args(args);
1654+
cmd.args(args.iter().map(Deref::deref));
16541655
}
16551656
}
16561657

@@ -1960,8 +1961,8 @@ fn add_order_independent_options(
19601961
cmd.arg(&codegen_results.crate_info.target_cpu);
19611962
cmd.arg("--cpu-features");
19621963
cmd.arg(match &sess.opts.cg.target_feature {
1963-
feat if !feat.is_empty() => feat,
1964-
_ => &sess.target.options.features,
1964+
feat if !feat.is_empty() => feat.as_ref(),
1965+
_ => sess.target.options.features.as_ref(),
19651966
});
19661967
}
19671968

@@ -2478,12 +2479,12 @@ fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
24782479
let os = &sess.target.os;
24792480
let llvm_target = &sess.target.llvm_target;
24802481
if sess.target.vendor != "apple"
2481-
|| !matches!(os.as_str(), "ios" | "tvos")
2482+
|| !matches!(os.as_ref(), "ios" | "tvos")
24822483
|| flavor != LinkerFlavor::Gcc
24832484
{
24842485
return;
24852486
}
2486-
let sdk_name = match (arch.as_str(), os.as_str()) {
2487+
let sdk_name = match (arch.as_ref(), os.as_ref()) {
24872488
("aarch64", "tvos") => "appletvos",
24882489
("x86_64", "tvos") => "appletvsimulator",
24892490
("arm", "ios") => "iphoneos",

compiler/rustc_codegen_ssa/src/back/linker.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn get_linker<'a>(
7575
if let Some(ref tool) = msvc_tool {
7676
let original_path = tool.path();
7777
if let Some(ref root_lib_path) = original_path.ancestors().nth(4) {
78-
let arch = match t.arch.as_str() {
78+
let arch = match t.arch.as_ref() {
7979
"x86_64" => Some("x64"),
8080
"x86" => Some("x86"),
8181
"aarch64" => Some("arm64"),
@@ -1520,7 +1520,7 @@ impl<'a> L4Bender<'a> {
15201520

15211521
pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
15221522
if let Some(ref exports) = tcx.sess.target.override_export_symbols {
1523-
return exports.clone();
1523+
return exports.iter().map(ToString::to_string).collect();
15241524
}
15251525

15261526
let mut symbols = Vec::new();

compiler/rustc_codegen_ssa/src/back/write.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl ModuleConfig {
218218
false
219219
),
220220
emit_obj,
221-
bc_cmdline: sess.target.bitcode_llvm_cmdline.clone(),
221+
bc_cmdline: sess.target.bitcode_llvm_cmdline.to_string(),
222222

223223
verify_llvm_ir: sess.verify_llvm_ir(),
224224
no_prepopulate_passes: sess.opts.cg.no_prepopulate_passes,
@@ -1061,7 +1061,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
10611061
is_pe_coff: tcx.sess.target.is_like_windows,
10621062
target_can_use_split_dwarf: tcx.sess.target_can_use_split_dwarf(),
10631063
target_pointer_width: tcx.sess.target.pointer_width,
1064-
target_arch: tcx.sess.target.arch.clone(),
1064+
target_arch: tcx.sess.target.arch.to_string(),
10651065
debuginfo: tcx.sess.opts.debuginfo,
10661066
split_debuginfo: tcx.sess.split_debuginfo(),
10671067
split_dwarf_kind: tcx.sess.opts.debugging_opts.split_dwarf_kind,

compiler/rustc_metadata/src/locator.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,10 @@ impl<'a> CrateLocator<'a> {
416416
(&f[rlib_prefix.len()..(f.len() - rlib_suffix.len())], CrateFlavor::Rlib)
417417
} else if f.starts_with(rmeta_prefix) && f.ends_with(rmeta_suffix) {
418418
(&f[rmeta_prefix.len()..(f.len() - rmeta_suffix.len())], CrateFlavor::Rmeta)
419-
} else if f.starts_with(dylib_prefix) && f.ends_with(dylib_suffix) {
419+
} else if f.starts_with(dylib_prefix) && f.ends_with(dylib_suffix.as_ref()) {
420420
(&f[dylib_prefix.len()..(f.len() - dylib_suffix.len())], CrateFlavor::Dylib)
421421
} else {
422-
if f.starts_with(staticlib_prefix) && f.ends_with(staticlib_suffix) {
422+
if f.starts_with(staticlib_prefix) && f.ends_with(staticlib_suffix.as_ref()) {
423423
self.crate_rejections.via_kind.push(CrateMismatch {
424424
path: spf.path.clone(),
425425
got: "static".to_string(),
@@ -698,8 +698,8 @@ impl<'a> CrateLocator<'a> {
698698
};
699699

700700
if file.starts_with("lib") && (file.ends_with(".rlib") || file.ends_with(".rmeta"))
701-
|| file.starts_with(&self.target.dll_prefix)
702-
&& file.ends_with(&self.target.dll_suffix)
701+
|| file.starts_with(self.target.dll_prefix.as_ref())
702+
&& file.ends_with(self.target.dll_suffix.as_ref())
703703
{
704704
// Make sure there's at most one rlib and at most one dylib.
705705
// Note to take care and match against the non-canonicalized name:
@@ -733,8 +733,8 @@ impl<'a> CrateLocator<'a> {
733733
crate_name: self.crate_name,
734734
root,
735735
triple: self.triple,
736-
dll_prefix: self.target.dll_prefix.clone(),
737-
dll_suffix: self.target.dll_suffix.clone(),
736+
dll_prefix: self.target.dll_prefix.to_string(),
737+
dll_suffix: self.target.dll_suffix.to_string(),
738738
crate_rejections: self.crate_rejections,
739739
})
740740
}

compiler/rustc_serialize/src/json.rs

+16
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ use self::JsonEvent::*;
170170
use self::ParserError::*;
171171
use self::ParserState::*;
172172

173+
use std::borrow::Cow;
173174
use std::collections::{BTreeMap, HashMap};
174175
use std::mem::swap;
175176
use std::num::FpCategory as Fp;
@@ -2196,6 +2197,12 @@ impl ToJson for string::String {
21962197
}
21972198
}
21982199

2200+
impl<'a> ToJson for Cow<'a, str> {
2201+
fn to_json(&self) -> Json {
2202+
Json::String(self.to_string())
2203+
}
2204+
}
2205+
21992206
macro_rules! tuple_impl {
22002207
// use variables to indicate the arity of the tuple
22012208
($($tyvar:ident),* ) => {
@@ -2240,6 +2247,15 @@ impl<A: ToJson> ToJson for Vec<A> {
22402247
}
22412248
}
22422249

2250+
impl<'a, A: ToJson> ToJson for Cow<'a, [A]>
2251+
where
2252+
[A]: ToOwned,
2253+
{
2254+
fn to_json(&self) -> Json {
2255+
Json::Array(self.iter().map(|elt| elt.to_json()).collect())
2256+
}
2257+
}
2258+
22432259
impl<T: ToString, A: ToJson> ToJson for BTreeMap<T, A> {
22442260
fn to_json(&self) -> Json {
22452261
let mut d = BTreeMap::new();

compiler/rustc_session/src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
956956
ret.reserve(7); // the minimum number of insertions
957957
// Target bindings.
958958
ret.insert((sym::target_os, Some(Symbol::intern(os))));
959-
for fam in &sess.target.families {
959+
for fam in sess.target.families.as_ref() {
960960
ret.insert((sym::target_family, Some(Symbol::intern(fam))));
961961
if fam == "windows" {
962962
ret.insert((sym::windows, None));

compiler/rustc_target/src/spec/aarch64_apple_darwin.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ use crate::spec::{FramePointer, LinkerFlavor, SanitizerSet, Target, TargetOption
22

33
pub fn target() -> Target {
44
let mut base = super::apple_base::opts("macos");
5-
base.cpu = "apple-a14".to_string();
5+
base.cpu = "apple-a14".into();
66
base.max_atomic_width = Some(128);
77

88
// FIXME: The leak sanitizer currently fails the tests, see #88132.
99
base.supported_sanitizers = SanitizerSet::ADDRESS | SanitizerSet::CFI | SanitizerSet::THREAD;
1010

11-
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-arch".to_string(), "arm64".to_string()]);
12-
base.link_env_remove.extend(super::apple_base::macos_link_env_remove());
11+
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-arch".into(), "arm64".into()]);
12+
base.link_env_remove.to_mut().extend(super::apple_base::macos_link_env_remove());
1313

1414
// Clang automatically chooses a more specific target based on
1515
// MACOSX_DEPLOYMENT_TARGET. To enable cross-language LTO to work
1616
// correctly, we do too.
1717
let llvm_target = super::apple_base::macos_llvm_target("arm64");
1818

1919
Target {
20-
llvm_target,
20+
llvm_target: llvm_target.into(),
2121
pointer_width: 64,
22-
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".to_string(),
23-
arch: "aarch64".to_string(),
22+
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
23+
arch: "aarch64".into(),
2424
options: TargetOptions {
25-
mcount: "\u{1}mcount".to_string(),
25+
mcount: "\u{1}mcount".into(),
2626
frame_pointer: FramePointer::NonLeaf,
2727
..base
2828
},

compiler/rustc_target/src/spec/aarch64_apple_ios.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ pub fn target() -> Target {
1010
let llvm_target = super::apple_base::ios_llvm_target(arch);
1111

1212
Target {
13-
llvm_target,
13+
llvm_target: llvm_target.into(),
1414
pointer_width: 64,
15-
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".to_string(),
16-
arch: "aarch64".to_string(),
15+
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
16+
arch: "aarch64".into(),
1717
options: TargetOptions {
18-
features: "+neon,+fp-armv8,+apple-a7".to_string(),
18+
features: "+neon,+fp-armv8,+apple-a7".into(),
1919
max_atomic_width: Some(128),
2020
forces_embed_bitcode: true,
2121
frame_pointer: FramePointer::NonLeaf,
@@ -29,7 +29,7 @@ pub fn target() -> Target {
2929
-target-abi\0\
3030
darwinpcs\0\
3131
-Os\0"
32-
.to_string(),
32+
.into(),
3333
..opts("ios", Arch::Arm64)
3434
},
3535
}

0 commit comments

Comments
 (0)