Skip to content

Commit 552991e

Browse files
committed
Replace FxHashMap with IndexVec for local_map
Fixes rust-lang#745
1 parent a18a194 commit 552991e

File tree

5 files changed

+31
-33
lines changed

5 files changed

+31
-33
lines changed

src/abi/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
325325
}
326326
}
327327

328-
fn local_place<'tcx>(
328+
fn make_local_place<'tcx>(
329329
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
330330
local: Local,
331331
layout: TyAndLayout<'tcx>,
@@ -344,9 +344,7 @@ fn local_place<'tcx>(
344344
#[cfg(debug_assertions)]
345345
self::comments::add_local_place_comments(fx, place, local);
346346

347-
let prev_place = fx.local_map.insert(local, place);
348-
debug_assert!(prev_place.is_none());
349-
fx.local_map[&local]
347+
place
350348
}
351349

352350
pub(crate) fn codegen_fn_prelude<'tcx>(
@@ -358,7 +356,8 @@ pub(crate) fn codegen_fn_prelude<'tcx>(
358356
#[cfg(debug_assertions)]
359357
self::comments::add_args_header_comment(fx);
360358

361-
self::returning::codegen_return_param(fx, &ssa_analyzed, start_block);
359+
let ret_place = self::returning::codegen_return_param(fx, &ssa_analyzed, start_block);
360+
assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE);
362361

363362
// None means pass_mode == NoPass
364363
enum ArgKind<'tcx> {
@@ -441,16 +440,16 @@ pub(crate) fn codegen_fn_prelude<'tcx>(
441440
#[cfg(debug_assertions)]
442441
self::comments::add_local_place_comments(fx, place, local);
443442

444-
let prev_place = fx.local_map.insert(local, place);
445-
debug_assert!(prev_place.is_none());
443+
assert_eq!(fx.local_map.push(place), local);
446444
continue;
447445
}
448446
}
449447
}
450448
_ => {}
451449
}
452450

453-
let place = local_place(fx, local, layout, is_ssa);
451+
let place = make_local_place(fx, local, layout, is_ssa);
452+
assert_eq!(fx.local_map.push(place), local);
454453

455454
match arg_kind {
456455
ArgKind::Normal(param) => {
@@ -476,7 +475,8 @@ pub(crate) fn codegen_fn_prelude<'tcx>(
476475

477476
let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
478477

479-
local_place(fx, local, layout, is_ssa);
478+
let place = make_local_place(fx, local, layout, is_ssa);
479+
assert_eq!(fx.local_map.push(place), local);
480480
}
481481

482482
fx.bcx

src/abi/returning.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,28 @@ pub(crate) fn can_return_to_ssa_var<'tcx>(
1616
}
1717
}
1818

19-
pub(super) fn codegen_return_param(
20-
fx: &mut FunctionCx<'_, '_, impl Backend>,
19+
pub(super) fn codegen_return_param<'tcx>(
20+
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
2121
ssa_analyzed: &rustc_index::vec::IndexVec<Local, crate::analyze::SsaKind>,
2222
start_block: Block,
23-
) {
23+
) -> CPlace<'tcx> {
2424
let ret_layout = return_layout(fx);
2525
let ret_pass_mode = get_pass_mode(fx.tcx, ret_layout);
26-
let ret_param = match ret_pass_mode {
27-
PassMode::NoPass => {
28-
fx.local_map
29-
.insert(RETURN_PLACE, CPlace::no_place(ret_layout));
30-
Empty
31-
}
26+
let (ret_place, ret_param) = match ret_pass_mode {
27+
PassMode::NoPass => (CPlace::no_place(ret_layout), Empty),
3228
PassMode::ByVal(_) | PassMode::ByValPair(_, _) => {
3329
let is_ssa = ssa_analyzed[RETURN_PLACE] == crate::analyze::SsaKind::Ssa;
34-
35-
super::local_place(fx, RETURN_PLACE, ret_layout, is_ssa);
36-
37-
Empty
30+
(
31+
super::make_local_place(fx, RETURN_PLACE, ret_layout, is_ssa),
32+
Empty,
33+
)
3834
}
3935
PassMode::ByRef { size: Some(_) } => {
4036
let ret_param = fx.bcx.append_block_param(start_block, fx.pointer_type);
41-
fx.local_map.insert(
42-
RETURN_PLACE,
37+
(
4338
CPlace::for_ptr(Pointer::new(ret_param), ret_layout),
44-
);
45-
46-
Single(ret_param)
39+
Single(ret_param),
40+
)
4741
}
4842
PassMode::ByRef { size: None } => todo!(),
4943
};
@@ -61,6 +55,8 @@ pub(super) fn codegen_return_param(
6155
ret_pass_mode,
6256
ret_layout.ty,
6357
);
58+
59+
ret_place
6460
}
6561

6662
pub(super) fn codegen_with_call_return_arg<'tcx, B: Backend, T>(

src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub(crate) fn trans_fn<'tcx, B: Backend + 'static>(
4747

4848
bcx,
4949
block_map,
50-
local_map: FxHashMap::with_capacity_and_hasher(mir.local_decls.len(), Default::default()),
50+
local_map: IndexVec::with_capacity(mir.local_decls.len()),
5151
caller_location: None, // set by `codegen_fn_prelude`
5252
cold_blocks: EntitySet::new(),
5353

src/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ pub(crate) struct FunctionCx<'clif, 'tcx, B: Backend + 'static> {
297297

298298
pub(crate) bcx: FunctionBuilder<'clif>,
299299
pub(crate) block_map: IndexVec<BasicBlock, Block>,
300-
pub(crate) local_map: FxHashMap<Local, CPlace<'tcx>>,
300+
pub(crate) local_map: IndexVec<Local, CPlace<'tcx>>,
301301

302302
/// When `#[track_caller]` is used, the implicit caller location is stored in this variable.
303303
pub(crate) caller_location: Option<CValue<'tcx>>,
@@ -383,7 +383,7 @@ impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
383383
}
384384

385385
pub(crate) fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
386-
*self.local_map.get(&local).unwrap_or_else(|| {
386+
*self.local_map.get(local).unwrap_or_else(|| {
387387
panic!("Local {:?} doesn't exist", local);
388388
})
389389
}

src/debuginfo/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ mod unwind;
44

55
use crate::prelude::*;
66

7+
use rustc_index::vec::IndexVec;
8+
79
use cranelift_codegen::entity::EntityRef;
810
use cranelift_codegen::ir::{StackSlots, ValueLabel, ValueLoc};
911
use cranelift_codegen::isa::TargetIsa;
@@ -269,7 +271,7 @@ impl<'tcx> DebugContext<'tcx> {
269271
isa: &dyn TargetIsa,
270272
context: &Context,
271273
source_info_set: &indexmap::IndexSet<SourceInfo>,
272-
local_map: FxHashMap<mir::Local, CPlace<'tcx>>,
274+
local_map: IndexVec<mir::Local, CPlace<'tcx>>,
273275
) {
274276
let symbol = func_id.as_u32() as usize;
275277
let mir = self.tcx.instance_mir(instance.def);
@@ -390,7 +392,7 @@ fn place_location<'tcx>(
390392
isa: &dyn TargetIsa,
391393
symbol: usize,
392394
context: &Context,
393-
local_map: &FxHashMap<mir::Local, CPlace<'tcx>>,
395+
local_map: &IndexVec<mir::Local, CPlace<'tcx>>,
394396
#[allow(rustc::default_hash_types)] value_labels_ranges: &std::collections::HashMap<
395397
ValueLabel,
396398
Vec<ValueLocRange>,
@@ -399,7 +401,7 @@ fn place_location<'tcx>(
399401
) -> AttributeValue {
400402
assert!(place.projection.is_empty()); // FIXME implement them
401403

402-
match local_map[&place.local].inner() {
404+
match local_map[place.local].inner() {
403405
CPlaceInner::Var(_local, var) => {
404406
let value_label = cranelift_codegen::ir::ValueLabel::new(var.index());
405407
if let Some(value_loc_ranges) = value_labels_ranges.get(&value_label) {

0 commit comments

Comments
 (0)