Skip to content

Commit e49e7f6

Browse files
committed
Do not fetch HIR to compute symbols.
1 parent 0d39f9d commit e49e7f6

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+34-33
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ use std::collections::hash_map::Entry::*;
22

33
use rustc_ast::expand::allocator::ALLOCATOR_METHODS;
44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_hir as hir;
5+
use rustc_hir::def::DefKind;
66
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
7-
use rustc_hir::Node;
87
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
98
use rustc_middle::middle::exported_symbols::{
109
metadata_symbol_name, ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
1110
};
1211
use rustc_middle::ty::query::{ExternProviders, Providers};
1312
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
1413
use rustc_middle::ty::Instance;
15-
use rustc_middle::ty::{self, SymbolName, TyCtxt};
14+
use rustc_middle::ty::{self, DefIdTree, SymbolName, TyCtxt};
1615
use rustc_session::config::{CrateType, OomStrategy};
1716
use rustc_target::spec::SanitizerSet;
1817

@@ -74,32 +73,34 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
7473
//
7574
// As a result, if this id is an FFI item (foreign item) then we only
7675
// let it through if it's included statically.
77-
match tcx.hir().get_by_def_id(def_id) {
78-
Node::ForeignItem(..) => {
79-
tcx.native_library(def_id).map_or(false, |library| library.kind.is_statically_included()).then_some(def_id)
80-
}
76+
if let Some(parent_id) = tcx.opt_local_parent(def_id)
77+
&& let DefKind::ForeignMod = tcx.def_kind(parent_id)
78+
{
79+
let library = tcx.native_library(def_id)?;
80+
return library.kind.is_statically_included().then_some(def_id);
81+
}
8182

82-
// Only consider nodes that actually have exported symbols.
83-
Node::Item(&hir::Item {
84-
kind: hir::ItemKind::Static(..) | hir::ItemKind::Fn(..),
85-
..
86-
})
87-
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) => {
88-
let generics = tcx.generics_of(def_id);
89-
if !generics.requires_monomorphization(tcx)
90-
// Functions marked with #[inline] are codegened with "internal"
91-
// linkage and are not exported unless marked with an extern
92-
// indicator
93-
&& (!Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
94-
|| tcx.codegen_fn_attrs(def_id.to_def_id()).contains_extern_indicator())
95-
{
96-
Some(def_id)
97-
} else {
98-
None
99-
}
100-
}
83+
// Only consider nodes that actually have exported symbols.
84+
match tcx.def_kind(def_id) {
85+
DefKind::Fn | DefKind::Static(_) => {}
86+
DefKind::AssocFn if tcx.impl_of_method(def_id.to_def_id()).is_some() => {}
87+
_ => return None,
88+
};
10189

102-
_ => None,
90+
let generics = tcx.generics_of(def_id);
91+
if generics.requires_monomorphization(tcx) {
92+
return None;
93+
}
94+
95+
// Functions marked with #[inline] are codegened with "internal"
96+
// linkage and are not exported unless marked with an extern
97+
// indicator
98+
if !Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
99+
|| tcx.codegen_fn_attrs(def_id.to_def_id()).contains_extern_indicator()
100+
{
101+
Some(def_id)
102+
} else {
103+
None
103104
}
104105
})
105106
.map(|def_id| {
@@ -118,7 +119,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
118119
tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())),
119120
export_level
120121
);
121-
(def_id.to_def_id(), SymbolExportInfo {
122+
let info = SymbolExportInfo {
122123
level: export_level,
123124
kind: if tcx.is_static(def_id.to_def_id()) {
124125
if codegen_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
@@ -130,8 +131,10 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
130131
SymbolExportKind::Text
131132
},
132133
used: codegen_attrs.flags.contains(CodegenFnAttrFlags::USED)
133-
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER) || used,
134-
})
134+
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
135+
|| used,
136+
};
137+
(def_id.to_def_id(), info)
135138
})
136139
.collect();
137140

@@ -457,9 +460,7 @@ fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel
457460
let target = &tcx.sess.target.llvm_target;
458461
// WebAssembly cannot export data symbols, so reduce their export level
459462
if target.contains("emscripten") {
460-
if let Some(Node::Item(&hir::Item { kind: hir::ItemKind::Static(..), .. })) =
461-
tcx.hir().get_if_local(sym_def_id)
462-
{
463+
if let DefKind::Static(_) = tcx.def_kind(sym_def_id) {
463464
return SymbolExportLevel::Rust;
464465
}
465466
}

0 commit comments

Comments
 (0)