Skip to content

Commit 6e0e612

Browse files
committed
Factor current-span logic into a caching handle
1 parent b96610b commit 6e0e612

File tree

4 files changed

+76
-60
lines changed

4 files changed

+76
-60
lines changed

src/helpers.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ pub mod convert;
22

33
use std::mem;
44
use std::num::NonZeroUsize;
5-
use std::rc::Rc;
65
use std::time::Duration;
76

87
use log::trace;
@@ -822,7 +821,7 @@ pub fn isolation_abort_error(name: &str) -> InterpResult<'static> {
822821

823822
/// Retrieve the list of local crates that should have been passed by cargo-miri in
824823
/// MIRI_LOCAL_CRATES and turn them into `CrateNum`s.
825-
pub fn get_local_crates(tcx: &TyCtxt<'_>) -> Rc<[CrateNum]> {
824+
pub fn get_local_crates(tcx: &TyCtxt<'_>) -> Vec<CrateNum> {
826825
// Convert the local crate names from the passed-in config into CrateNums so that they can
827826
// be looked up quickly during execution
828827
let local_crate_names = std::env::var("MIRI_LOCAL_CRATES")
@@ -836,7 +835,7 @@ pub fn get_local_crates(tcx: &TyCtxt<'_>) -> Rc<[CrateNum]> {
836835
local_crates.push(crate_num);
837836
}
838837
}
839-
Rc::from(local_crates.as_slice())
838+
local_crates
840839
}
841840

842841
/// Formats an AllocRange like [0x1..0x3], for use in diagnostics.

src/machine.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::cell::RefCell;
66
use std::collections::HashSet;
77
use std::fmt;
88
use std::num::NonZeroU64;
9-
use std::rc::Rc;
109
use std::time::Instant;
1110

1211
use rand::rngs::StdRng;
@@ -25,7 +24,7 @@ use rustc_middle::{
2524
},
2625
};
2726
use rustc_span::def_id::{CrateNum, DefId};
28-
use rustc_span::Symbol;
27+
use rustc_span::{Span, Symbol};
2928
use rustc_target::abi::Size;
3029
use rustc_target::spec::abi::Abi;
3130

@@ -274,7 +273,7 @@ pub struct Evaluator<'mir, 'tcx> {
274273
pub(crate) backtrace_style: BacktraceStyle,
275274

276275
/// Crates which are considered local for the purposes of error reporting.
277-
pub(crate) local_crates: Rc<[CrateNum]>,
276+
pub(crate) local_crates: Vec<CrateNum>,
278277

279278
/// Mapping extern static names to their base pointer.
280279
extern_statics: FxHashMap<Symbol, Pointer<Tag>>,
@@ -415,6 +414,10 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
415414
let def_id = frame.instance.def_id();
416415
def_id.is_local() || self.local_crates.contains(&def_id.krate)
417416
}
417+
418+
pub(crate) fn current_span(&self) -> CurrentSpan<'_, 'mir, 'tcx> {
419+
CurrentSpan { span: None, machine: self }
420+
}
418421
}
419422

420423
/// A rustc InterpCx for Miri.
@@ -580,8 +583,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
580583
alloc.size(),
581584
stacked_borrows,
582585
kind,
583-
&ecx.machine.threads,
584-
ecx.machine.local_crates.clone(),
586+
ecx.machine.current_span(),
585587
))
586588
} else {
587589
None
@@ -663,7 +665,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
663665
tag,
664666
range,
665667
machine.stacked_borrows.as_ref().unwrap(),
666-
&machine.threads,
668+
machine.current_span(),
667669
)
668670
} else {
669671
Ok(())
@@ -687,7 +689,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
687689
tag,
688690
range,
689691
machine.stacked_borrows.as_ref().unwrap(),
690-
&machine.threads,
692+
machine.current_span(),
691693
)
692694
} else {
693695
Ok(())
@@ -789,3 +791,33 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
789791
res
790792
}
791793
}
794+
795+
#[derive(Clone)]
796+
pub struct CurrentSpan<'a, 'tcx, 'mir> {
797+
span: Option<Span>,
798+
machine: &'a Evaluator<'tcx, 'mir>,
799+
}
800+
801+
impl<'a, 'tcx, 'mir> CurrentSpan<'a, 'tcx, 'mir> {
802+
pub fn get(&mut self) -> rustc_span::Span {
803+
if self.span.is_none() {
804+
self.span = Some(self.current_span());
805+
}
806+
self.span.unwrap()
807+
}
808+
809+
#[inline(never)]
810+
fn current_span(&self) -> Span {
811+
self.machine
812+
.threads
813+
.active_thread_stack()
814+
.into_iter()
815+
.rev()
816+
.find(|frame| {
817+
let def_id = frame.instance.def_id();
818+
def_id.is_local() || self.machine.local_crates.contains(&def_id.krate)
819+
})
820+
.map(|frame| frame.current_span())
821+
.unwrap_or(rustc_span::DUMMY_SP)
822+
}
823+
}

src/stacked_borrows.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use log::trace;
55
use std::cell::RefCell;
66
use std::fmt;
77
use std::num::NonZeroU64;
8-
use std::rc::Rc;
98

109
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1110
use rustc_hir::Mutability;
@@ -14,17 +13,15 @@ use rustc_middle::ty::{
1413
self,
1514
layout::{HasParamEnv, LayoutOf},
1615
};
17-
use rustc_span::def_id::CrateNum;
1816
use rustc_span::DUMMY_SP;
1917
use rustc_target::abi::Size;
2018
use std::collections::HashSet;
2119

20+
use crate::machine::CurrentSpan;
2221
use crate::*;
2322

2423
pub mod diagnostics;
25-
use diagnostics::AllocHistory;
26-
27-
use diagnostics::TagHistory;
24+
use diagnostics::{AllocHistory, TagHistory};
2825

2926
pub type PtrId = NonZeroU64;
3027
pub type CallId = NonZeroU64;
@@ -376,7 +373,7 @@ impl<'tcx> Stack {
376373
tag: SbTag,
377374
(alloc_id, alloc_range, offset): (AllocId, AllocRange, Size), // just for debug printing and error messages
378375
global: &mut GlobalStateInner,
379-
threads: &ThreadManager<'_, 'tcx>,
376+
current_span: &mut CurrentSpan<'_, '_, 'tcx>,
380377
alloc_history: &mut AllocHistory,
381378
) -> InterpResult<'tcx> {
382379
// Two main steps: Find granting item, remove incompatible items above.
@@ -400,7 +397,7 @@ impl<'tcx> Stack {
400397
global,
401398
alloc_history,
402399
)?;
403-
alloc_history.log_invalidation(item.tag, alloc_range, threads);
400+
alloc_history.log_invalidation(item.tag, alloc_range, current_span);
404401
}
405402
} else {
406403
// On a read, *disable* all `Unique` above the granting item. This ensures U2 for read accesses.
@@ -422,7 +419,7 @@ impl<'tcx> Stack {
422419
alloc_history,
423420
)?;
424421
item.perm = Permission::Disabled;
425-
alloc_history.log_invalidation(item.tag, alloc_range, threads);
422+
alloc_history.log_invalidation(item.tag, alloc_range, current_span);
426423
}
427424
}
428425
}
@@ -471,7 +468,7 @@ impl<'tcx> Stack {
471468
new: Item,
472469
(alloc_id, alloc_range, offset): (AllocId, AllocRange, Size), // just for debug printing and error messages
473470
global: &mut GlobalStateInner,
474-
threads: &ThreadManager<'_, 'tcx>,
471+
current_span: &mut CurrentSpan<'_, '_, 'tcx>,
475472
alloc_history: &mut AllocHistory,
476473
) -> InterpResult<'tcx> {
477474
// Figure out which access `perm` corresponds to.
@@ -505,7 +502,7 @@ impl<'tcx> Stack {
505502
derived_from,
506503
(alloc_id, alloc_range, offset),
507504
global,
508-
threads,
505+
current_span,
509506
alloc_history,
510507
)?;
511508

@@ -533,13 +530,13 @@ impl<'tcx> Stack {
533530
/// Map per-stack operations to higher-level per-location-range operations.
534531
impl<'tcx> Stacks {
535532
/// Creates new stack with initial tag.
536-
fn new(size: Size, perm: Permission, tag: SbTag, local_crates: Rc<[CrateNum]>) -> Self {
533+
fn new(size: Size, perm: Permission, tag: SbTag) -> Self {
537534
let item = Item { perm, tag, protector: None };
538535
let stack = Stack { borrows: vec![item] };
539536

540537
Stacks {
541538
stacks: RefCell::new(RangeMap::new(size, stack)),
542-
history: RefCell::new(AllocHistory::new(local_crates)),
539+
history: RefCell::new(AllocHistory::new()),
543540
}
544541
}
545542

@@ -579,8 +576,7 @@ impl Stacks {
579576
size: Size,
580577
state: &GlobalState,
581578
kind: MemoryKind<MiriMemoryKind>,
582-
threads: &ThreadManager<'_, '_>,
583-
local_crates: Rc<[CrateNum]>,
579+
mut current_span: CurrentSpan<'_, '_, '_>,
584580
) -> Self {
585581
let mut extra = state.borrow_mut();
586582
let (base_tag, perm) = match kind {
@@ -614,12 +610,12 @@ impl Stacks {
614610
(tag, Permission::SharedReadWrite)
615611
}
616612
};
617-
let stacks = Stacks::new(size, perm, base_tag, local_crates);
613+
let stacks = Stacks::new(size, perm, base_tag);
618614
stacks.history.borrow_mut().log_creation(
619615
None,
620616
base_tag,
621617
alloc_range(Size::ZERO, size),
622-
threads,
618+
&mut current_span,
623619
);
624620
stacks
625621
}
@@ -631,7 +627,7 @@ impl Stacks {
631627
tag: SbTag,
632628
range: AllocRange,
633629
state: &GlobalState,
634-
threads: &ThreadManager<'_, 'tcx>,
630+
mut current_span: CurrentSpan<'_, '_, 'tcx>,
635631
) -> InterpResult<'tcx> {
636632
trace!(
637633
"read access with tag {:?}: {:?}, size {}",
@@ -646,7 +642,7 @@ impl Stacks {
646642
tag,
647643
(alloc_id, range, offset),
648644
&mut state,
649-
threads,
645+
&mut current_span,
650646
history,
651647
)
652648
})
@@ -659,7 +655,7 @@ impl Stacks {
659655
tag: SbTag,
660656
range: AllocRange,
661657
state: &GlobalState,
662-
threads: &ThreadManager<'_, 'tcx>,
658+
mut current_span: CurrentSpan<'_, '_, 'tcx>,
663659
) -> InterpResult<'tcx> {
664660
trace!(
665661
"write access with tag {:?}: {:?}, size {}",
@@ -674,7 +670,7 @@ impl Stacks {
674670
tag,
675671
(alloc_id, range, offset),
676672
&mut state,
677-
threads,
673+
&mut current_span,
678674
history,
679675
)
680676
})
@@ -723,6 +719,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
723719
}
724720
let (alloc_id, base_offset, orig_tag) = this.ptr_get_alloc_id(place.ptr)?;
725721

722+
let mut current_span = this.machine.current_span();
726723
{
727724
let extra = this.get_alloc_extra(alloc_id)?;
728725
let stacked_borrows =
@@ -732,10 +729,10 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
732729
Some(orig_tag),
733730
new_tag,
734731
alloc_range(base_offset, size),
735-
&this.machine.threads,
732+
&mut current_span,
736733
);
737734
if protect {
738-
alloc_history.log_protector(orig_tag, new_tag, &this.machine.threads);
735+
alloc_history.log_protector(orig_tag, new_tag, &mut current_span);
739736
}
740737
}
741738

@@ -804,7 +801,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
804801
item,
805802
(alloc_id, range, offset),
806803
&mut *global,
807-
&this.machine.threads,
804+
&mut current_span,
808805
history,
809806
)
810807
})
@@ -821,13 +818,14 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
821818
let item = Item { perm, tag: new_tag, protector };
822819
let range = alloc_range(base_offset, size);
823820
let mut global = machine.stacked_borrows.as_ref().unwrap().borrow_mut();
821+
let mut current_span = machine.current_span();
824822
stacked_borrows.for_each_mut(range, |offset, stack, history| {
825823
stack.grant(
826824
orig_tag,
827825
item,
828826
(alloc_id, range, offset),
829827
&mut global,
830-
&machine.threads,
828+
&mut current_span,
831829
history,
832830
)
833831
})?;

0 commit comments

Comments
 (0)