Skip to content

Commit f688f4d

Browse files
committed
Reduce usage of Symbol in the linker code
Interning here doesn't save us anything. It rather costs a bit of time.
1 parent de57dbc commit f688f4d

File tree

4 files changed

+55
-60
lines changed

4 files changed

+55
-60
lines changed

compiler/rustc_codegen_ssa/src/back/archive.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use rustc_data_structures::temp_dir::MaybeTempDir;
22
use rustc_session::cstore::DllImport;
33
use rustc_session::Session;
4-
use rustc_span::symbol::Symbol;
54

65
use std::io;
76
use std::path::{Path, PathBuf};
87

98
pub(super) fn find_library(
10-
name: Symbol,
9+
name: &str,
1110
verbatim: bool,
1211
search_paths: &[PathBuf],
1312
sess: &Session,

compiler/rustc_codegen_ssa/src/back/command.rs

-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::io;
77
use std::mem;
88
use std::process::{self, Output};
99

10-
use rustc_span::symbol::Symbol;
1110
use rustc_target::spec::LldFlavor;
1211

1312
#[derive(Clone)]
@@ -47,11 +46,6 @@ impl Command {
4746
self
4847
}
4948

50-
pub fn sym_arg(&mut self, arg: Symbol) -> &mut Command {
51-
self.arg(arg.as_str());
52-
self
53-
}
54-
5549
pub fn args<I>(&mut self, args: I) -> &mut Command
5650
where
5751
I: IntoIterator<Item: AsRef<OsStr>>,

compiler/rustc_codegen_ssa/src/back/link.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
354354
}
355355
if let Some(name) = lib.name {
356356
let location =
357-
find_library(name, lib.verbatim.unwrap_or(false), &lib_search_paths, sess);
357+
find_library(name.as_str(), lib.verbatim.unwrap_or(false), &lib_search_paths, sess);
358358
ab.add_archive(&location, |_| false).unwrap_or_else(|e| {
359359
sess.fatal(&format!(
360360
"failed to add native library {}: {}",
@@ -1117,7 +1117,7 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
11171117
let path = find_sanitizer_runtime(&sess, &filename);
11181118
let rpath = path.to_str().expect("non-utf8 component in path");
11191119
linker.args(&["-Wl,-rpath", "-Xlinker", rpath]);
1120-
linker.link_dylib(Symbol::intern(&filename), false, true);
1120+
linker.link_dylib(&filename, false, true);
11211121
} else {
11221122
let filename = format!("librustc{}_rt.{}.a", channel, name);
11231123
let path = find_sanitizer_runtime(&sess, &filename).join(&filename);
@@ -2199,6 +2199,7 @@ fn add_local_native_libraries(
21992199
let Some(name) = lib.name else {
22002200
continue;
22012201
};
2202+
let name = name.as_str();
22022203

22032204
// Skip if this library is the same as the last.
22042205
last = if (lib.name, lib.kind, lib.verbatim) == last {
@@ -2362,6 +2363,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
23622363
let Some(name) = lib.name else {
23632364
continue;
23642365
};
2366+
let name = name.as_str();
23652367
if !relevant_lib(sess, lib) {
23662368
continue;
23672369
}
@@ -2519,7 +2521,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
25192521
}
25202522
let filestem = cratepath.file_stem().unwrap().to_str().unwrap();
25212523
cmd.link_rust_dylib(
2522-
Symbol::intern(&unlib(&sess.target, filestem)),
2524+
&unlib(&sess.target, filestem),
25232525
parent.unwrap_or_else(|| Path::new("")),
25242526
);
25252527
}
@@ -2551,6 +2553,7 @@ fn add_upstream_native_libraries(
25512553
let Some(name) = lib.name else {
25522554
continue;
25532555
};
2556+
let name = name.as_str();
25542557
if !relevant_lib(sess, &lib) {
25552558
continue;
25562559
}

compiler/rustc_codegen_ssa/src/back/linker.rs

+48-49
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo, S
1616
use rustc_middle::ty::TyCtxt;
1717
use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
1818
use rustc_session::Session;
19-
use rustc_span::symbol::Symbol;
2019
use rustc_target::spec::{LinkOutputKind, LinkerFlavor, LldFlavor};
2120

2221
use cc::windows_registry;
@@ -163,13 +162,13 @@ pub fn get_linker<'a>(
163162
pub trait Linker {
164163
fn cmd(&mut self) -> &mut Command;
165164
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path);
166-
fn link_dylib(&mut self, lib: Symbol, verbatim: bool, as_needed: bool);
167-
fn link_rust_dylib(&mut self, lib: Symbol, path: &Path);
168-
fn link_framework(&mut self, framework: Symbol, as_needed: bool);
169-
fn link_staticlib(&mut self, lib: Symbol, verbatim: bool);
165+
fn link_dylib(&mut self, lib: &str, verbatim: bool, as_needed: bool);
166+
fn link_rust_dylib(&mut self, lib: &str, path: &Path);
167+
fn link_framework(&mut self, framework: &str, as_needed: bool);
168+
fn link_staticlib(&mut self, lib: &str, verbatim: bool);
170169
fn link_rlib(&mut self, lib: &Path);
171170
fn link_whole_rlib(&mut self, lib: &Path);
172-
fn link_whole_staticlib(&mut self, lib: Symbol, verbatim: bool, search_path: &[PathBuf]);
171+
fn link_whole_staticlib(&mut self, lib: &str, verbatim: bool, search_path: &[PathBuf]);
173172
fn include_path(&mut self, path: &Path);
174173
fn framework_path(&mut self, path: &Path);
175174
fn output_filename(&mut self, path: &Path);
@@ -423,8 +422,8 @@ impl<'a> Linker for GccLinker<'a> {
423422
}
424423
}
425424

426-
fn link_dylib(&mut self, lib: Symbol, verbatim: bool, as_needed: bool) {
427-
if self.sess.target.os == "illumos" && lib.as_str() == "c" {
425+
fn link_dylib(&mut self, lib: &str, verbatim: bool, as_needed: bool) {
426+
if self.sess.target.os == "illumos" && lib == "c" {
428427
// libc will be added via late_link_args on illumos so that it will
429428
// appear last in the library search order.
430429
// FIXME: This should be replaced by a more complete and generic
@@ -454,7 +453,7 @@ impl<'a> Linker for GccLinker<'a> {
454453
}
455454
}
456455
}
457-
fn link_staticlib(&mut self, lib: Symbol, verbatim: bool) {
456+
fn link_staticlib(&mut self, lib: &str, verbatim: bool) {
458457
self.hint_static();
459458
self.cmd.arg(format!("-l{}{}", if verbatim { ":" } else { "" }, lib));
460459
}
@@ -484,20 +483,20 @@ impl<'a> Linker for GccLinker<'a> {
484483
self.linker_arg("-znorelro");
485484
}
486485

487-
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
486+
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
488487
self.hint_dynamic();
489488
self.cmd.arg(format!("-l{}", lib));
490489
}
491490

492-
fn link_framework(&mut self, framework: Symbol, as_needed: bool) {
491+
fn link_framework(&mut self, framework: &str, as_needed: bool) {
493492
self.hint_dynamic();
494493
if !as_needed {
495494
// FIXME(81490): ld64 as of macOS 11 supports the -needed_framework
496495
// flag but we have no way to detect that here.
497-
// self.cmd.arg("-needed_framework").sym_arg(framework);
496+
// self.cmd.arg("-needed_framework").arg(framework);
498497
self.sess.warn("`as-needed` modifier not implemented yet for ld64");
499498
}
500-
self.cmd.arg("-framework").sym_arg(framework);
499+
self.cmd.arg("-framework").arg(framework);
501500
}
502501

503502
// Here we explicitly ask that the entire archive is included into the
@@ -506,7 +505,7 @@ impl<'a> Linker for GccLinker<'a> {
506505
// don't otherwise explicitly reference them. This can occur for
507506
// libraries which are just providing bindings, libraries with generic
508507
// functions, etc.
509-
fn link_whole_staticlib(&mut self, lib: Symbol, verbatim: bool, search_path: &[PathBuf]) {
508+
fn link_whole_staticlib(&mut self, lib: &str, verbatim: bool, search_path: &[PathBuf]) {
510509
self.hint_static();
511510
let target = &self.sess.target;
512511
if !target.is_like_osx {
@@ -836,11 +835,11 @@ impl<'a> Linker for MsvcLinker<'a> {
836835
self.cmd.arg("/OPT:NOREF,NOICF");
837836
}
838837

839-
fn link_dylib(&mut self, lib: Symbol, verbatim: bool, _as_needed: bool) {
838+
fn link_dylib(&mut self, lib: &str, verbatim: bool, _as_needed: bool) {
840839
self.cmd.arg(format!("{}{}", lib, if verbatim { "" } else { ".lib" }));
841840
}
842841

843-
fn link_rust_dylib(&mut self, lib: Symbol, path: &Path) {
842+
fn link_rust_dylib(&mut self, lib: &str, path: &Path) {
844843
// When producing a dll, the MSVC linker may not actually emit a
845844
// `foo.lib` file if the dll doesn't actually export any symbols, so we
846845
// check to see if the file is there and just omit linking to it if it's
@@ -851,7 +850,7 @@ impl<'a> Linker for MsvcLinker<'a> {
851850
}
852851
}
853852

854-
fn link_staticlib(&mut self, lib: Symbol, verbatim: bool) {
853+
fn link_staticlib(&mut self, lib: &str, verbatim: bool) {
855854
self.cmd.arg(format!("{}{}", lib, if verbatim { "" } else { ".lib" }));
856855
}
857856

@@ -890,11 +889,11 @@ impl<'a> Linker for MsvcLinker<'a> {
890889
fn framework_path(&mut self, _path: &Path) {
891890
bug!("frameworks are not supported on windows")
892891
}
893-
fn link_framework(&mut self, _framework: Symbol, _as_needed: bool) {
892+
fn link_framework(&mut self, _framework: &str, _as_needed: bool) {
894893
bug!("frameworks are not supported on windows")
895894
}
896895

897-
fn link_whole_staticlib(&mut self, lib: Symbol, verbatim: bool, _search_path: &[PathBuf]) {
896+
fn link_whole_staticlib(&mut self, lib: &str, verbatim: bool, _search_path: &[PathBuf]) {
898897
self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", lib, if verbatim { "" } else { ".lib" }));
899898
}
900899
fn link_whole_rlib(&mut self, path: &Path) {
@@ -1047,8 +1046,8 @@ impl<'a> Linker for EmLinker<'a> {
10471046
self.cmd.arg("-L").arg(path);
10481047
}
10491048

1050-
fn link_staticlib(&mut self, lib: Symbol, _verbatim: bool) {
1051-
self.cmd.arg("-l").sym_arg(lib);
1049+
fn link_staticlib(&mut self, lib: &str, _verbatim: bool) {
1050+
self.cmd.arg("-l").arg(lib);
10521051
}
10531052

10541053
fn output_filename(&mut self, path: &Path) {
@@ -1059,12 +1058,12 @@ impl<'a> Linker for EmLinker<'a> {
10591058
self.cmd.arg(path);
10601059
}
10611060

1062-
fn link_dylib(&mut self, lib: Symbol, verbatim: bool, _as_needed: bool) {
1061+
fn link_dylib(&mut self, lib: &str, verbatim: bool, _as_needed: bool) {
10631062
// Emscripten always links statically
10641063
self.link_staticlib(lib, verbatim);
10651064
}
10661065

1067-
fn link_whole_staticlib(&mut self, lib: Symbol, verbatim: bool, _search_path: &[PathBuf]) {
1066+
fn link_whole_staticlib(&mut self, lib: &str, verbatim: bool, _search_path: &[PathBuf]) {
10681067
// not supported?
10691068
self.link_staticlib(lib, verbatim);
10701069
}
@@ -1074,7 +1073,7 @@ impl<'a> Linker for EmLinker<'a> {
10741073
self.link_rlib(lib);
10751074
}
10761075

1077-
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
1076+
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
10781077
self.link_dylib(lib, false, true);
10791078
}
10801079

@@ -1098,7 +1097,7 @@ impl<'a> Linker for EmLinker<'a> {
10981097
bug!("frameworks are not supported on Emscripten")
10991098
}
11001099

1101-
fn link_framework(&mut self, _framework: Symbol, _as_needed: bool) {
1100+
fn link_framework(&mut self, _framework: &str, _as_needed: bool) {
11021101
bug!("frameworks are not supported on Emscripten")
11031102
}
11041103

@@ -1237,12 +1236,12 @@ impl<'a> Linker for WasmLd<'a> {
12371236
}
12381237
}
12391238

1240-
fn link_dylib(&mut self, lib: Symbol, _verbatim: bool, _as_needed: bool) {
1241-
self.cmd.arg("-l").sym_arg(lib);
1239+
fn link_dylib(&mut self, lib: &str, _verbatim: bool, _as_needed: bool) {
1240+
self.cmd.arg("-l").arg(lib);
12421241
}
12431242

1244-
fn link_staticlib(&mut self, lib: Symbol, _verbatim: bool) {
1245-
self.cmd.arg("-l").sym_arg(lib);
1243+
fn link_staticlib(&mut self, lib: &str, _verbatim: bool) {
1244+
self.cmd.arg("-l").arg(lib);
12461245
}
12471246

12481247
fn link_rlib(&mut self, lib: &Path) {
@@ -1271,16 +1270,16 @@ impl<'a> Linker for WasmLd<'a> {
12711270

12721271
fn no_relro(&mut self) {}
12731272

1274-
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
1275-
self.cmd.arg("-l").sym_arg(lib);
1273+
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
1274+
self.cmd.arg("-l").arg(lib);
12761275
}
12771276

1278-
fn link_framework(&mut self, _framework: Symbol, _as_needed: bool) {
1277+
fn link_framework(&mut self, _framework: &str, _as_needed: bool) {
12791278
panic!("frameworks not supported")
12801279
}
12811280

1282-
fn link_whole_staticlib(&mut self, lib: Symbol, _verbatim: bool, _search_path: &[PathBuf]) {
1283-
self.cmd.arg("-l").sym_arg(lib);
1281+
fn link_whole_staticlib(&mut self, lib: &str, _verbatim: bool, _search_path: &[PathBuf]) {
1282+
self.cmd.arg("-l").arg(lib);
12841283
}
12851284

12861285
fn link_whole_rlib(&mut self, lib: &Path) {
@@ -1360,10 +1359,10 @@ pub struct L4Bender<'a> {
13601359
}
13611360

13621361
impl<'a> Linker for L4Bender<'a> {
1363-
fn link_dylib(&mut self, _lib: Symbol, _verbatim: bool, _as_needed: bool) {
1362+
fn link_dylib(&mut self, _lib: &str, _verbatim: bool, _as_needed: bool) {
13641363
bug!("dylibs are not supported on L4Re");
13651364
}
1366-
fn link_staticlib(&mut self, lib: Symbol, _verbatim: bool) {
1365+
fn link_staticlib(&mut self, lib: &str, _verbatim: bool) {
13671366
self.hint_static();
13681367
self.cmd.arg(format!("-PC{}", lib));
13691368
}
@@ -1404,15 +1403,15 @@ impl<'a> Linker for L4Bender<'a> {
14041403

14051404
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
14061405

1407-
fn link_rust_dylib(&mut self, _: Symbol, _: &Path) {
1406+
fn link_rust_dylib(&mut self, _: &str, _: &Path) {
14081407
panic!("Rust dylibs not supported");
14091408
}
14101409

1411-
fn link_framework(&mut self, _framework: Symbol, _as_needed: bool) {
1410+
fn link_framework(&mut self, _framework: &str, _as_needed: bool) {
14121411
bug!("frameworks not supported on L4Re");
14131412
}
14141413

1415-
fn link_whole_staticlib(&mut self, lib: Symbol, _verbatim: bool, _search_path: &[PathBuf]) {
1414+
fn link_whole_staticlib(&mut self, lib: &str, _verbatim: bool, _search_path: &[PathBuf]) {
14161415
self.hint_static();
14171416
self.cmd.arg("--whole-archive").arg(format!("-l{}", lib));
14181417
self.cmd.arg("--no-whole-archive");
@@ -1617,27 +1616,27 @@ impl<'a> Linker for PtxLinker<'a> {
16171616
self.cmd.arg("-o").arg(path);
16181617
}
16191618

1620-
fn link_dylib(&mut self, _lib: Symbol, _verbatim: bool, _as_needed: bool) {
1619+
fn link_dylib(&mut self, _lib: &str, _verbatim: bool, _as_needed: bool) {
16211620
panic!("external dylibs not supported")
16221621
}
16231622

1624-
fn link_rust_dylib(&mut self, _lib: Symbol, _path: &Path) {
1623+
fn link_rust_dylib(&mut self, _lib: &str, _path: &Path) {
16251624
panic!("external dylibs not supported")
16261625
}
16271626

1628-
fn link_staticlib(&mut self, _lib: Symbol, _verbatim: bool) {
1627+
fn link_staticlib(&mut self, _lib: &str, _verbatim: bool) {
16291628
panic!("staticlibs not supported")
16301629
}
16311630

1632-
fn link_whole_staticlib(&mut self, _lib: Symbol, _verbatim: bool, _search_path: &[PathBuf]) {
1631+
fn link_whole_staticlib(&mut self, _lib: &str, _verbatim: bool, _search_path: &[PathBuf]) {
16331632
panic!("staticlibs not supported")
16341633
}
16351634

16361635
fn framework_path(&mut self, _path: &Path) {
16371636
panic!("frameworks not supported")
16381637
}
16391638

1640-
fn link_framework(&mut self, _framework: Symbol, _as_needed: bool) {
1639+
fn link_framework(&mut self, _framework: &str, _as_needed: bool) {
16411640
panic!("frameworks not supported")
16421641
}
16431642

@@ -1717,27 +1716,27 @@ impl<'a> Linker for BpfLinker<'a> {
17171716
self.cmd.arg("-o").arg(path);
17181717
}
17191718

1720-
fn link_dylib(&mut self, _lib: Symbol, _verbatim: bool, _as_needed: bool) {
1719+
fn link_dylib(&mut self, _lib: &str, _verbatim: bool, _as_needed: bool) {
17211720
panic!("external dylibs not supported")
17221721
}
17231722

1724-
fn link_rust_dylib(&mut self, _lib: Symbol, _path: &Path) {
1723+
fn link_rust_dylib(&mut self, _lib: &str, _path: &Path) {
17251724
panic!("external dylibs not supported")
17261725
}
17271726

1728-
fn link_staticlib(&mut self, _lib: Symbol, _verbatim: bool) {
1727+
fn link_staticlib(&mut self, _lib: &str, _verbatim: bool) {
17291728
panic!("staticlibs not supported")
17301729
}
17311730

1732-
fn link_whole_staticlib(&mut self, _lib: Symbol, _verbatim: bool, _search_path: &[PathBuf]) {
1731+
fn link_whole_staticlib(&mut self, _lib: &str, _verbatim: bool, _search_path: &[PathBuf]) {
17331732
panic!("staticlibs not supported")
17341733
}
17351734

17361735
fn framework_path(&mut self, _path: &Path) {
17371736
panic!("frameworks not supported")
17381737
}
17391738

1740-
fn link_framework(&mut self, _framework: Symbol, _as_needed: bool) {
1739+
fn link_framework(&mut self, _framework: &str, _as_needed: bool) {
17411740
panic!("frameworks not supported")
17421741
}
17431742

0 commit comments

Comments
 (0)