Use an ItemId inside mir::GlobalAsm.

This commit is contained in:
Camille GILLOT 2021-01-30 19:18:48 +01:00
parent c676e358a5
commit bd3cd5dbed
7 changed files with 32 additions and 21 deletions

View File

@ -164,8 +164,8 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege
MonoItem::Static(def_id) => {
crate::constant::codegen_static(&mut cx.constants_cx, def_id)
}
MonoItem::GlobalAsm(hir_id) => {
let item = cx.tcx.hir().expect_item(hir_id);
MonoItem::GlobalAsm(item_id) => {
let item = cx.tcx.hir().item(item_id);
if let rustc_hir::ItemKind::GlobalAsm(rustc_hir::GlobalAsm { asm }) = item.kind {
cx.global_asm.push_str(&*asm.as_str());
cx.global_asm.push_str("\n\n");

View File

@ -93,10 +93,9 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! {
MonoItem::Static(def_id) => {
crate::constant::codegen_static(&mut cx.constants_cx, def_id);
}
MonoItem::GlobalAsm(hir_id) => {
let item = cx.tcx.hir().expect_item(hir_id);
tcx.sess
.span_fatal(item.span, "Global asm is not supported in JIT mode");
MonoItem::GlobalAsm(item_id) => {
let item = cx.tcx.hir().item(item_id);
tcx.sess.span_fatal(item.span, "Global asm is not supported in JIT mode");
}
}
}

View File

@ -30,8 +30,8 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
MonoItem::Static(def_id) => {
cx.codegen_static(def_id, cx.tcx().is_mutable_static(def_id));
}
MonoItem::GlobalAsm(hir_id) => {
let item = cx.tcx().hir().expect_item(hir_id);
MonoItem::GlobalAsm(item_id) => {
let item = cx.tcx().hir().item(item_id);
if let hir::ItemKind::GlobalAsm(ref ga) = item.kind {
cx.codegen_global_asm(ga);
} else {

View File

@ -2541,11 +2541,17 @@ impl VariantData<'hir> {
// The bodies for items are stored "out of line", in a separate
// hashmap in the `Crate`. Here we just record the hir-id of the item
// so it can fetched later.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug, Hash)]
pub struct ItemId {
pub id: HirId,
}
impl ItemId {
pub fn hir_id(&self) -> HirId {
self.id
}
}
/// An item
///
/// The name might be a dummy name in case of anonymous items
@ -2559,6 +2565,12 @@ pub struct Item<'hir> {
pub span: Span,
}
impl Item<'_> {
pub fn item_id(&self) -> ItemId {
ItemId { id: self.hir_id }
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum Unsafety {

View File

@ -7,7 +7,7 @@ use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::HirId;
use rustc_hir::{HirId, ItemId};
use rustc_session::config::OptLevel;
use rustc_span::source_map::Span;
use rustc_span::symbol::Symbol;
@ -43,7 +43,7 @@ pub enum InstantiationMode {
pub enum MonoItem<'tcx> {
Fn(Instance<'tcx>),
Static(DefId),
GlobalAsm(HirId),
GlobalAsm(ItemId),
}
impl<'tcx> MonoItem<'tcx> {
@ -71,8 +71,8 @@ impl<'tcx> MonoItem<'tcx> {
match *self {
MonoItem::Fn(instance) => tcx.symbol_name(instance),
MonoItem::Static(def_id) => tcx.symbol_name(Instance::mono(tcx, def_id)),
MonoItem::GlobalAsm(hir_id) => {
let def_id = tcx.hir().local_def_id(hir_id);
MonoItem::GlobalAsm(item_id) => {
let def_id = tcx.hir().local_def_id(item_id.hir_id());
SymbolName::new(tcx, &format!("global_asm_{:?}", def_id))
}
}
@ -178,7 +178,7 @@ impl<'tcx> MonoItem<'tcx> {
MonoItem::Static(def_id) => {
def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
}
MonoItem::GlobalAsm(hir_id) => Some(hir_id),
MonoItem::GlobalAsm(item_id) => Some(item_id.hir_id()),
}
.map(|hir_id| tcx.hir().span(hir_id))
}
@ -195,9 +195,9 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for MonoItem<'tcx> {
MonoItem::Static(def_id) => {
def_id.hash_stable(hcx, hasher);
}
MonoItem::GlobalAsm(node_id) => {
MonoItem::GlobalAsm(item_id) => {
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
node_id.hash_stable(hcx, hasher);
item_id.hash_stable(hcx, hasher);
})
}
}
@ -351,7 +351,7 @@ impl<'tcx> CodegenUnit<'tcx> {
MonoItem::Static(def_id) => {
def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
}
MonoItem::GlobalAsm(hir_id) => Some(hir_id),
MonoItem::GlobalAsm(item_id) => Some(item_id.hir_id()),
},
item.symbol_name(tcx),
)

View File

@ -1030,7 +1030,7 @@ impl ItemLikeVisitor<'v> for RootCollector<'_, 'v> {
"RootCollector: ItemKind::GlobalAsm({})",
self.tcx.def_path_str(self.tcx.hir().local_def_id(item.hir_id).to_def_id())
);
self.output.push(dummy_spanned(MonoItem::GlobalAsm(item.hir_id)));
self.output.push(dummy_spanned(MonoItem::GlobalAsm(item.item_id())));
}
hir::ItemKind::Static(..) => {
let def_id = self.tcx.hir().local_def_id(item.hir_id).to_def_id();

View File

@ -314,7 +314,7 @@ fn characteristic_def_id_of_mono_item<'tcx>(
Some(def_id)
}
MonoItem::Static(def_id) => Some(def_id),
MonoItem::GlobalAsm(hir_id) => Some(tcx.hir().local_def_id(hir_id).to_def_id()),
MonoItem::GlobalAsm(item_id) => Some(tcx.hir().local_def_id(item_id.hir_id()).to_def_id()),
}
}
@ -405,8 +405,8 @@ fn mono_item_visibility(
Visibility::Hidden
};
}
MonoItem::GlobalAsm(hir_id) => {
let def_id = tcx.hir().local_def_id(*hir_id);
MonoItem::GlobalAsm(item_id) => {
let def_id = tcx.hir().local_def_id(item_id.hir_id());
return if tcx.is_reachable_non_generic(def_id) {
*can_be_internalized = false;
default_visibility(tcx, def_id.to_def_id(), false)