Skip to content

Commit c280357

Browse files
committed
Give codegen units symbol names that backend can use
1 parent 49ab24e commit c280357

6 files changed

Lines changed: 66 additions & 1 deletion

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4488,6 +4488,7 @@ dependencies = [
44884488
"rustc_middle",
44894489
"rustc_session",
44904490
"rustc_span",
4491+
"rustc_symbol_mangling",
44914492
"rustc_target",
44924493
"serde",
44934494
"serde_json",

compiler/rustc_middle/src/mono.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ pub struct CodegenUnit<'tcx> {
349349
/// contain something unique to this crate (e.g., a module path)
350350
/// as well as the crate name and disambiguator.
351351
name: Symbol,
352+
353+
/// Symbol name for this CGU. Backend may emit symbols prefixed with this name
354+
/// and assume uniqueness.
355+
symbol_name: Option<Symbol>,
356+
352357
items: FxIndexMap<MonoItem<'tcx>, MonoItemData>,
353358
size_estimate: usize,
354359
primary: bool,
@@ -405,6 +410,7 @@ impl<'tcx> CodegenUnit<'tcx> {
405410
pub fn new(name: Symbol) -> CodegenUnit<'tcx> {
406411
CodegenUnit {
407412
name,
413+
symbol_name: None,
408414
items: Default::default(),
409415
size_estimate: 0,
410416
primary: false,
@@ -445,6 +451,14 @@ impl<'tcx> CodegenUnit<'tcx> {
445451
self.is_code_coverage_dead_code_cgu = true;
446452
}
447453

454+
pub fn symbol_name(&self) -> Symbol {
455+
self.symbol_name.expect("CGU symbol name accessed before setting")
456+
}
457+
458+
pub fn set_symbol_name(&mut self, name: Symbol) {
459+
self.symbol_name = Some(name);
460+
}
461+
448462
pub fn mangle_name(human_readable_name: &str) -> BaseNString {
449463
let mut hasher = StableHasher::new();
450464
human_readable_name.hash(&mut hasher);

compiler/rustc_monomorphize/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rustc_macros = { path = "../rustc_macros" }
1414
rustc_middle = { path = "../rustc_middle" }
1515
rustc_session = { path = "../rustc_session" }
1616
rustc_span = { path = "../rustc_span" }
17+
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
1718
rustc_target = { path = "../rustc_target" }
1819
serde = "1"
1920
serde_json = "1"

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,13 @@ fn merge_codegen_units<'tcx>(
457457
};
458458
cgu.set_name(new_cgu_name);
459459
}
460+
461+
// Assign symbol name to each CGU units.
462+
cgu.set_symbol_name(Symbol::intern(&rustc_symbol_mangling::mangle_cgu(
463+
cx.tcx,
464+
LOCAL_CRATE,
465+
Err(cgu.name().as_str()),
466+
)));
460467
}
461468

462469
// A sorted order here ensures what follows can be deterministic.
@@ -491,6 +498,12 @@ fn merge_codegen_units<'tcx>(
491498
let numbered_codegen_unit_name =
492499
cgu_name_builder.build_cgu_name_no_mangle(LOCAL_CRATE, &["cgu"], Some(suffix));
493500
cgu.set_name(numbered_codegen_unit_name);
501+
502+
cgu.set_symbol_name(Symbol::intern(&rustc_symbol_mangling::mangle_cgu(
503+
cx.tcx,
504+
LOCAL_CRATE,
505+
Ok(index.try_into().unwrap()),
506+
)));
494507
}
495508
}
496509
}

compiler/rustc_symbol_mangling/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ mod v0;
103103

104104
pub mod test;
105105

106-
pub use v0::mangle_internal_symbol;
106+
pub use v0::{mangle_cgu, mangle_internal_symbol};
107107

108108
/// This function computes the symbol name for the given `instance` and the
109109
/// given instantiating crate. That is, if you know that instance X is

compiler/rustc_symbol_mangling/src/v0.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,42 @@ pub(super) fn mangle<'tcx>(
8484
std::mem::take(&mut p.out)
8585
}
8686

87+
pub fn mangle_cgu<'tcx>(tcx: TyCtxt<'tcx>, krate: CrateNum, cgu_name: Result<u64, &str>) -> String {
88+
let prefix = "_R";
89+
let mut p: V0SymbolMangler<'_> = V0SymbolMangler {
90+
tcx,
91+
start_offset: prefix.len(),
92+
is_exportable: false,
93+
paths: FxHashMap::default(),
94+
types: FxHashMap::default(),
95+
consts: FxHashMap::default(),
96+
binders: vec![],
97+
out: String::from(prefix),
98+
};
99+
100+
match cgu_name {
101+
Ok(cgu_index) => {
102+
// If we have a CGU index, we can easily encode this with the shim mechanism.
103+
p.path_append_ns(|p| p.print_def_path(krate.as_def_id(), &[]), 'S', cgu_index, "cgu")
104+
.unwrap();
105+
}
106+
Err(name) => {
107+
// In incremental compilation we just have a name and no index. Encode this as a str-typed generic argument to cgu shim for now.
108+
p.out.push('I');
109+
p.path_append_ns(|p| p.print_def_path(krate.as_def_id(), &[]), 'S', 0, "cgu").unwrap();
110+
p.push("KRe");
111+
112+
for byte in name.as_bytes() {
113+
let _ = write!(p.out, "{byte:02x}");
114+
}
115+
116+
p.push("_E");
117+
}
118+
}
119+
120+
std::mem::take(&mut p.out)
121+
}
122+
87123
pub fn mangle_internal_symbol<'tcx>(tcx: TyCtxt<'tcx>, item_name: &str) -> String {
88124
match item_name {
89125
// rust_eh_personality must not be renamed as LLVM hard-codes the name

0 commit comments

Comments
 (0)