Skip to content

Commit 7172250

Browse files
committed
Auto merge of #59343 - eddyb:rm-def-symbol-name, r=<try>
rustc(codegen): uncache `def_symbol_name` prefix from `symbol_name`. The `def_symbol_name` query was an optimization to avoid recomputing the common part of a symbol name, as only the hash needs to be added to it for each symbol. However, #57967 will add a new mangling scheme, which doesn't readily support this kind of reuse - while it's plausible, it requires a lot more effort, since you'd have to "symbolically evaluate" mangling, and keep it in a form where the backreference positions can be computed correctly in the final step. So I want to see how much time we're actually saving with this `def_symbol_name` optimization, nowadays.
2 parents 48e354d + 3590a46 commit 7172250

File tree

7 files changed

+19
-48
lines changed

7 files changed

+19
-48
lines changed

Diff for: src/librustc/dep_graph/dep_node.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,7 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
523523
[] ConstEval { param_env: ParamEnvAnd<'tcx, GlobalId<'tcx>> },
524524
[] ConstEvalRaw { param_env: ParamEnvAnd<'tcx, GlobalId<'tcx>> },
525525
[] CheckMatch(DefId),
526-
[] SymbolName(DefId),
527-
[] InstanceSymbolName { instance: Instance<'tcx> },
526+
[] SymbolName { instance: Instance<'tcx> },
528527
[] SpecializationGraph(DefId),
529528
[] ObjectSafety(DefId),
530529
[] FulfillObligation { param_env: ParamEnv<'tcx>, trait_ref: PolyTraitRef<'tcx> },

Diff for: src/librustc/ty/query/config.rs

-1
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,6 @@ impl_disk_cacheable_query!(unsafety_check_result, |_, def_id| def_id.is_local())
999999
impl_disk_cacheable_query!(borrowck, |_, def_id| def_id.is_local());
10001000
impl_disk_cacheable_query!(mir_const_qualif, |_, def_id| def_id.is_local());
10011001
impl_disk_cacheable_query!(check_match, |_, def_id| def_id.is_local());
1002-
impl_disk_cacheable_query!(def_symbol_name, |_, _| true);
10031002
impl_disk_cacheable_query!(predicates_of, |_, def_id| def_id.is_local());
10041003
impl_disk_cacheable_query!(used_trait_imports, |_, def_id| def_id.is_local());
10051004
impl_disk_cacheable_query!(codegen_fn_attrs, |_, _| true);

Diff for: src/librustc/ty/query/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ rustc_query_append! { [define_queries!][ <'tcx>
342342

343343
[] fn mir_shims: mir_shim_dep_node(ty::InstanceDef<'tcx>) -> &'tcx mir::Mir<'tcx>,
344344

345-
[] fn def_symbol_name: SymbolName(DefId) -> ty::SymbolName,
346345
[] fn symbol_name: symbol_name_dep_node(ty::Instance<'tcx>) -> ty::SymbolName,
347346

348347
[] fn describe_def: DescribeDef(DefId) -> Option<Def>,
@@ -780,7 +779,7 @@ fn mir_shim_dep_node<'tcx>(instance_def: ty::InstanceDef<'tcx>) -> DepConstructo
780779
}
781780

782781
fn symbol_name_dep_node<'tcx>(instance: ty::Instance<'tcx>) -> DepConstructor<'tcx> {
783-
DepConstructor::InstanceSymbolName { instance }
782+
DepConstructor::SymbolName { instance }
784783
}
785784

786785
fn typeck_item_bodies_dep_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {

Diff for: src/librustc/ty/query/on_disk_cache.rs

-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ impl<'sess> OnDiskCache<'sess> {
218218
encode_query_results::<borrowck<'_>, _>(tcx, enc, qri)?;
219219
encode_query_results::<mir_borrowck<'_>, _>(tcx, enc, qri)?;
220220
encode_query_results::<mir_const_qualif<'_>, _>(tcx, enc, qri)?;
221-
encode_query_results::<def_symbol_name<'_>, _>(tcx, enc, qri)?;
222221
encode_query_results::<const_is_rvalue_promotable_to_static<'_>, _>(tcx, enc, qri)?;
223222
encode_query_results::<symbol_name<'_>, _>(tcx, enc, qri)?;
224223
encode_query_results::<check_match<'_>, _>(tcx, enc, qri)?;

Diff for: src/librustc/ty/query/plumbing.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ pub fn force_from_dep_node<'tcx>(
12131213
DepKind::Layout |
12141214
DepKind::ConstEval |
12151215
DepKind::ConstEvalRaw |
1216-
DepKind::InstanceSymbolName |
1216+
DepKind::SymbolName |
12171217
DepKind::MirShim |
12181218
DepKind::BorrowCheckKrate |
12191219
DepKind::Specializes |
@@ -1310,7 +1310,6 @@ pub fn force_from_dep_node<'tcx>(
13101310
DepKind::TypeckTables => { force!(typeck_tables_of, def_id!()); }
13111311
DepKind::UsedTraitImports => { force!(used_trait_imports, def_id!()); }
13121312
DepKind::HasTypeckTables => { force!(has_typeck_tables, def_id!()); }
1313-
DepKind::SymbolName => { force!(def_symbol_name, def_id!()); }
13141313
DepKind::SpecializationGraph => { force!(specialization_graph_of, def_id!()); }
13151314
DepKind::ObjectSafety => { force!(is_object_safe, def_id!()); }
13161315
DepKind::TraitImpls => { force!(trait_impls_of, def_id!()); }
@@ -1496,7 +1495,6 @@ impl_load_from_cache!(
14961495
BorrowCheck => borrowck,
14971496
MirBorrowCheck => mir_borrowck,
14981497
MirConstQualif => mir_const_qualif,
1499-
SymbolName => def_symbol_name,
15001498
ConstIsRvaluePromotableToStatic => const_is_rvalue_promotable_to_static,
15011499
CheckMatch => check_match,
15021500
type_of => type_of,

Diff for: src/librustc_codegen_utils/symbol_names.rs

+15-38
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
101101
use rustc_mir::monomorphize::item::{InstantiationMode, MonoItem, MonoItemExt};
102102
use rustc_mir::monomorphize::Instance;
103103

104-
use syntax_pos::symbol::Symbol;
104+
use syntax_pos::symbol::{Symbol, InternedString};
105105

106106
use log::debug;
107107

@@ -110,7 +110,6 @@ use std::mem::{self, discriminant};
110110

111111
pub fn provide(providers: &mut Providers<'_>) {
112112
*providers = Providers {
113-
def_symbol_name,
114113
symbol_name,
115114

116115
..*providers
@@ -222,21 +221,13 @@ fn get_symbol_hash<'a, 'tcx>(
222221
hasher.finish()
223222
}
224223

225-
fn def_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> ty::SymbolName {
226-
SymbolPrinter {
227-
tcx,
228-
path: SymbolPath::new(),
229-
keep_within_component: false,
230-
}.print_def_path(def_id, &[]).unwrap().path.into_interned()
231-
}
232-
233-
fn symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance<'tcx>) -> ty::SymbolName {
224+
fn symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) -> ty::SymbolName {
234225
ty::SymbolName {
235-
name: Symbol::intern(&compute_symbol_name(tcx, instance)).as_interned_str(),
226+
name: compute_symbol_name(tcx, instance),
236227
}
237228
}
238229

239-
fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance<'tcx>) -> String {
230+
fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) -> InternedString {
240231
let def_id = instance.def_id();
241232
let substs = instance.substs;
242233

@@ -247,11 +238,13 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
247238
if def_id.is_local() {
248239
if tcx.plugin_registrar_fn(LOCAL_CRATE) == Some(def_id) {
249240
let disambiguator = tcx.sess.local_crate_disambiguator();
250-
return tcx.sess.generate_plugin_registrar_symbol(disambiguator);
241+
return Symbol::intern(&tcx.sess.generate_plugin_registrar_symbol(disambiguator))
242+
.as_interned_str();
251243
}
252244
if tcx.proc_macro_decls_static(LOCAL_CRATE) == Some(def_id) {
253245
let disambiguator = tcx.sess.local_crate_disambiguator();
254-
return tcx.sess.generate_proc_macro_decls_symbol(disambiguator);
246+
return Symbol::intern(&tcx.sess.generate_proc_macro_decls_symbol(disambiguator))
247+
.as_interned_str();
255248
}
256249
}
257250

@@ -268,20 +261,20 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
268261
let attrs = tcx.codegen_fn_attrs(def_id);
269262
if is_foreign {
270263
if let Some(name) = attrs.link_name {
271-
return name.to_string();
264+
return name.as_interned_str();
272265
}
273266
// Don't mangle foreign items.
274-
return tcx.item_name(def_id).to_string();
267+
return tcx.item_name(def_id);
275268
}
276269

277270
if let Some(name) = &attrs.export_name {
278271
// Use provided name
279-
return name.to_string();
272+
return name.as_interned_str();
280273
}
281274

282275
if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
283276
// Don't mangle
284-
return tcx.item_name(def_id).to_string();
277+
return tcx.item_name(def_id);
285278
}
286279

287280
// We want to compute the "type" of this item. Unfortunately, some
@@ -321,15 +314,15 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
321314

322315
let mut printer = SymbolPrinter {
323316
tcx,
324-
path: SymbolPath::from_interned(tcx.def_symbol_name(def_id)),
317+
path: SymbolPath::new(),
325318
keep_within_component: false,
326-
};
319+
}.print_def_path(def_id, &[]).unwrap();
327320

328321
if instance.is_vtable_shim() {
329322
let _ = printer.write_str("{{vtable-shim}}");
330323
}
331324

332-
printer.path.finish(hash)
325+
Symbol::intern(&printer.path.finish(hash)).as_interned_str()
333326
}
334327

335328
// Follow C++ namespace-mangling style, see
@@ -361,22 +354,6 @@ impl SymbolPath {
361354
result
362355
}
363356

364-
fn from_interned(symbol: ty::SymbolName) -> Self {
365-
let mut result = SymbolPath {
366-
result: String::with_capacity(64),
367-
temp_buf: String::with_capacity(16),
368-
};
369-
result.result.push_str(&symbol.as_str());
370-
result
371-
}
372-
373-
fn into_interned(mut self) -> ty::SymbolName {
374-
self.finalize_pending_component();
375-
ty::SymbolName {
376-
name: Symbol::intern(&self.result).as_interned_str(),
377-
}
378-
}
379-
380357
fn finalize_pending_component(&mut self) {
381358
if !self.temp_buf.is_empty() {
382359
let _ = write!(self.result, "{}{}", self.temp_buf.len(), self.temp_buf);

Diff for: src/librustc_mir/transform/lower_128bit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn check_lang_item_type<'a, 'tcx, D>(
138138
let place_ty = place.ty(local_decls, tcx).to_ty(tcx);
139139
let expected = [lhs_ty, rhs_ty, place_ty];
140140
assert_eq!(sig.inputs_and_output[..], expected,
141-
"lang item {}", tcx.def_symbol_name(did));
141+
"lang item `{}`", tcx.def_path_str(did));
142142
did
143143
}
144144

0 commit comments

Comments
 (0)