Skip to content

Commit 6fe9822

Browse files
committed
Remove DefId from CrateItem in favor of a lookup table
1 parent 18e305d commit 6fe9822

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,28 @@
33
//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
44
//! until stable MIR is complete.
55
6+
use std::sync::RwLock;
7+
68
use crate::stable_mir;
79
pub use rustc_span::def_id::{CrateNum, DefId};
810

11+
static DEF_ID_MAP: RwLock<Vec<DefId>> = RwLock::new(Vec::new());
12+
913
pub fn item_def_id(item: &stable_mir::CrateItem) -> DefId {
10-
item.0
14+
DEF_ID_MAP.read().unwrap()[item.0]
15+
}
16+
17+
pub fn crate_item(did: DefId) -> stable_mir::CrateItem {
18+
// FIXME: this becomes inefficient when we have too many ids
19+
let mut map = DEF_ID_MAP.write().unwrap();
20+
for (i, &d) in map.iter().enumerate() {
21+
if d == did {
22+
return stable_mir::CrateItem(i);
23+
}
24+
}
25+
let id = map.len();
26+
map.push(did);
27+
stable_mir::CrateItem(id)
1128
}
1229

1330
pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {

compiler/rustc_smir/src/rustc_smir/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10-
use crate::stable_mir::{self};
10+
use crate::{
11+
rustc_internal::crate_item,
12+
stable_mir::{self},
13+
};
1114
use rustc_middle::ty::{tls::with, TyCtxt};
1215
use rustc_span::def_id::{CrateNum, LOCAL_CRATE};
1316
use tracing::debug;
@@ -34,9 +37,7 @@ pub fn find_crate(name: &str) -> Option<stable_mir::Crate> {
3437

3538
/// Retrieve all items of the local crate that have a MIR associated with them.
3639
pub fn all_local_items() -> stable_mir::CrateItems {
37-
with(|tcx| {
38-
tcx.mir_keys(()).iter().map(|item| stable_mir::CrateItem(item.to_def_id())).collect()
39-
})
40+
with(|tcx| tcx.mir_keys(()).iter().map(|item| crate_item(item.to_def_id())).collect())
4041
}
4142

4243
/// Build a stable mir crate from a given crate number.

compiler/rustc_smir/src/stable_mir/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
//! There shouldn't be any direct references to internal compiler constructs in this module.
1212
//! If you need an internal construct, consider using `rustc_internal` or `rustc_smir`.
1313
14-
use crate::rustc_internal;
15-
1614
/// Use String for now but we should replace it.
1715
pub type Symbol = String;
1816

@@ -37,7 +35,7 @@ pub struct Crate {
3735
/// For now, it only stores the item DefId. Use functions inside `rustc_internal` module to
3836
/// use this item.
3937
#[derive(Clone, PartialEq, Eq, Debug)]
40-
pub struct CrateItem(pub(crate) rustc_internal::DefId);
38+
pub struct CrateItem(pub(crate) DefId);
4139

4240
/// Access to the local crate.
4341
pub fn local_crate() -> Crate {

0 commit comments

Comments
 (0)