Skip to content

Commit c49f608

Browse files
committed
Several refactorings to constant.rs
* Move the done hash set from ConstantCx to define_all_allocs. * Move check if alloc has already been defined to the start of the loop. * Extract helper function for vtables.
1 parent b64079d commit c49f608

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/constant.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,28 @@ use cranelift_module::*;
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
88
use rustc_middle::mir::interpret::{read_target_uint, AllocId, GlobalAlloc, Scalar};
9-
use rustc_middle::ty::ScalarInt;
9+
use rustc_middle::ty::{Binder, ExistentialTraitRef, ScalarInt};
1010

1111
use crate::prelude::*;
1212

1313
pub(crate) struct ConstantCx {
1414
todo: Vec<TodoItem>,
15-
done: FxHashSet<DataId>,
1615
anon_allocs: FxHashMap<AllocId, DataId>,
1716
}
1817

19-
#[derive(Copy, Clone, Debug)]
18+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
2019
enum TodoItem {
2120
Alloc(AllocId),
2221
Static(DefId),
2322
}
2423

2524
impl ConstantCx {
2625
pub(crate) fn new() -> Self {
27-
ConstantCx { todo: vec![], done: FxHashSet::default(), anon_allocs: FxHashMap::default() }
26+
ConstantCx { todo: vec![], anon_allocs: FxHashMap::default() }
2827
}
2928

3029
pub(crate) fn finalize(mut self, tcx: TyCtxt<'_>, module: &mut dyn Module) {
3130
define_all_allocs(tcx, module, &mut self);
32-
self.done.clear();
3331
}
3432
}
3533

@@ -153,14 +151,12 @@ pub(crate) fn codegen_const_value<'tcx>(
153151
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
154152
}
155153
GlobalAlloc::VTable(ty, trait_ref) => {
156-
let alloc_id = fx.tcx.vtable_allocation((ty, trait_ref));
157-
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
158-
// FIXME: factor this common code with the `Memory` arm into a function?
159-
let data_id = data_id_for_alloc_id(
154+
let data_id = data_id_for_vtable(
155+
fx.tcx,
160156
&mut fx.constants_cx,
161157
fx.module,
162-
alloc_id,
163-
alloc.inner().mutability,
158+
ty,
159+
trait_ref,
164160
);
165161
let local_data_id =
166162
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
@@ -208,12 +204,8 @@ fn pointer_for_allocation<'tcx>(
208204
alloc_id: AllocId,
209205
) -> crate::pointer::Pointer {
210206
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
211-
let data_id = data_id_for_alloc_id(
212-
&mut fx.constants_cx,
213-
&mut *fx.module,
214-
alloc_id,
215-
alloc.inner().mutability,
216-
);
207+
let data_id =
208+
data_id_for_alloc_id(&mut fx.constants_cx, fx.module, alloc_id, alloc.inner().mutability);
217209

218210
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
219211
if fx.clif_comments.enabled() {
@@ -235,6 +227,17 @@ pub(crate) fn data_id_for_alloc_id(
235227
.or_insert_with(|| module.declare_anonymous_data(mutability.is_mut(), false).unwrap())
236228
}
237229

230+
pub(crate) fn data_id_for_vtable<'tcx>(
231+
tcx: TyCtxt<'tcx>,
232+
cx: &mut ConstantCx,
233+
module: &mut dyn Module,
234+
ty: Ty<'tcx>,
235+
trait_ref: Option<Binder<'tcx, ExistentialTraitRef<'tcx>>>,
236+
) -> DataId {
237+
let alloc_id = tcx.vtable_allocation((ty, trait_ref));
238+
data_id_for_alloc_id(cx, module, alloc_id, Mutability::Not)
239+
}
240+
238241
fn data_id_for_static(
239242
tcx: TyCtxt<'_>,
240243
module: &mut dyn Module,
@@ -327,7 +330,12 @@ fn data_id_for_static(
327330
}
328331

329332
fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut ConstantCx) {
333+
let mut done = FxHashSet::default();
330334
while let Some(todo_item) = cx.todo.pop() {
335+
if !done.insert(todo_item) {
336+
continue;
337+
}
338+
331339
let (data_id, alloc, section_name) = match todo_item {
332340
TodoItem::Alloc(alloc_id) => {
333341
let alloc = match tcx.global_alloc(alloc_id) {
@@ -358,10 +366,6 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
358366
}
359367
};
360368

361-
if cx.done.contains(&data_id) {
362-
continue;
363-
}
364-
365369
let mut data = DataDescription::new();
366370
let alloc = alloc.inner();
367371
data.set_align(alloc.align.bytes());
@@ -418,8 +422,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
418422
data_id_for_alloc_id(cx, module, alloc_id, target_alloc.inner().mutability)
419423
}
420424
GlobalAlloc::VTable(ty, trait_ref) => {
421-
let alloc_id = tcx.vtable_allocation((ty, trait_ref));
422-
data_id_for_alloc_id(cx, module, alloc_id, Mutability::Not)
425+
data_id_for_vtable(tcx, cx, module, ty, trait_ref)
423426
}
424427
GlobalAlloc::Static(def_id) => {
425428
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
@@ -446,7 +449,6 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
446449
}
447450

448451
module.define_data(data_id, &data).unwrap();
449-
cx.done.insert(data_id);
450452
}
451453

452454
assert!(cx.todo.is_empty(), "{:?}", cx.todo);

src/vtable.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! See `rustc_codegen_ssa/src/meth.rs` for reference.
44
5-
use crate::constant::data_id_for_alloc_id;
5+
use crate::constant::data_id_for_vtable;
66
use crate::prelude::*;
77

88
pub(crate) fn vtable_memflags() -> MemFlags {
@@ -92,12 +92,10 @@ pub(crate) fn get_vtable<'tcx>(
9292
ty: Ty<'tcx>,
9393
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
9494
) -> Value {
95-
let alloc_id = fx.tcx.vtable_allocation((ty, trait_ref));
96-
let data_id =
97-
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, Mutability::Not);
95+
let data_id = data_id_for_vtable(fx.tcx, &mut fx.constants_cx, fx.module, ty, trait_ref);
9896
let local_data_id = fx.module.declare_data_in_func(data_id, fx.bcx.func);
9997
if fx.clif_comments.enabled() {
100-
fx.add_comment(local_data_id, format!("vtable: {:?}", alloc_id));
98+
fx.add_comment(local_data_id, "vtable");
10199
}
102100
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
103101
}

0 commit comments

Comments
 (0)