Skip to content

Commit 4b49702

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

File tree

3 files changed

+73
-55
lines changed

3 files changed

+73
-55
lines changed

src/machine.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_middle::{
2525
},
2626
};
2727
use rustc_span::def_id::{CrateNum, DefId};
28-
use rustc_span::Symbol;
28+
use rustc_span::{Span, Symbol};
2929
use rustc_target::abi::Size;
3030
use rustc_target::spec::abi::Abi;
3131

@@ -415,6 +415,10 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
415415
let def_id = frame.instance.def_id();
416416
def_id.is_local() || self.local_crates.contains(&def_id.krate)
417417
}
418+
419+
pub(crate) fn current_span(&self) -> CurrentSpan<'_, 'mir, 'tcx> {
420+
CurrentSpan { span: None, machine: self }
421+
}
418422
}
419423

420424
/// A rustc InterpCx for Miri.
@@ -580,8 +584,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
580584
alloc.size(),
581585
stacked_borrows,
582586
kind,
583-
&ecx.machine.threads,
584-
ecx.machine.local_crates.clone(),
587+
ecx.machine.current_span(),
585588
))
586589
} else {
587590
None
@@ -663,7 +666,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
663666
tag,
664667
range,
665668
machine.stacked_borrows.as_ref().unwrap(),
666-
&machine.threads,
669+
machine.current_span(),
667670
)
668671
} else {
669672
Ok(())
@@ -687,7 +690,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
687690
tag,
688691
range,
689692
machine.stacked_borrows.as_ref().unwrap(),
690-
&machine.threads,
693+
machine.current_span(),
691694
)
692695
} else {
693696
Ok(())
@@ -789,3 +792,33 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
789792
res
790793
}
791794
}
795+
796+
#[derive(Clone)]
797+
pub struct CurrentSpan<'a, 'tcx, 'mir> {
798+
span: Option<Span>,
799+
machine: &'a Evaluator<'tcx, 'mir>,
800+
}
801+
802+
impl<'a, 'tcx, 'mir> CurrentSpan<'a, 'tcx, 'mir> {
803+
pub fn get(&mut self) -> rustc_span::Span {
804+
if self.span.is_none() {
805+
self.span = Some(self.current_span());
806+
}
807+
self.span.unwrap()
808+
}
809+
810+
#[inline(never)]
811+
fn current_span(&self) -> Span {
812+
self.machine
813+
.threads
814+
.active_thread_stack()
815+
.into_iter()
816+
.rev()
817+
.find(|frame| {
818+
let def_id = frame.instance.def_id();
819+
def_id.is_local() || self.machine.local_crates.contains(&def_id.krate)
820+
})
821+
.map(|frame| frame.current_span())
822+
.unwrap_or(rustc_span::DUMMY_SP)
823+
}
824+
}

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
})?;

src/stacked_borrows/diagnostics.rs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
use smallvec::SmallVec;
2-
use std::rc::Rc;
32

43
use rustc_middle::mir::interpret::{AllocId, AllocRange};
5-
use rustc_span::def_id::CrateNum;
64
use rustc_span::{Span, SpanData};
75
use rustc_target::abi::Size;
86

97
use crate::helpers::HexRange;
8+
use crate::machine::CurrentSpan;
109
use crate::stacked_borrows::{err_sb_ub, AccessKind, Permission};
1110
use crate::Item;
1211
use crate::SbTag;
1312
use crate::Stack;
14-
use crate::ThreadManager;
1513

1614
use rustc_middle::mir::interpret::InterpError;
1715

@@ -23,8 +21,6 @@ pub struct AllocHistory {
2321
creations: smallvec::SmallVec<[Event; 2]>,
2422
invalidations: smallvec::SmallVec<[Event; 1]>,
2523
protectors: smallvec::SmallVec<[Protection; 1]>,
26-
/// This field is a clone of the `local_crates` field on `Evaluator`.
27-
local_crates: Rc<[CrateNum]>,
2824
}
2925

3026
#[derive(Clone, Debug)]
@@ -59,37 +55,23 @@ pub enum TagHistory {
5955
}
6056

6157
impl AllocHistory {
62-
pub fn new(local_crates: Rc<[CrateNum]>) -> Self {
58+
pub fn new() -> Self {
6359
Self {
6460
current_time: 0,
6561
creations: SmallVec::new(),
6662
invalidations: SmallVec::new(),
6763
protectors: SmallVec::new(),
68-
local_crates,
6964
}
7065
}
7166

72-
fn current_span(&self, threads: &ThreadManager<'_, '_>) -> Span {
73-
threads
74-
.active_thread_stack()
75-
.into_iter()
76-
.rev()
77-
.find(|frame| {
78-
let def_id = frame.instance.def_id();
79-
def_id.is_local() || self.local_crates.contains(&def_id.krate)
80-
})
81-
.map(|frame| frame.current_span())
82-
.unwrap_or(rustc_span::DUMMY_SP)
83-
}
84-
8567
pub fn log_creation(
8668
&mut self,
8769
parent: Option<SbTag>,
8870
tag: SbTag,
8971
range: AllocRange,
90-
threads: &ThreadManager<'_, '_>,
72+
current_span: &mut CurrentSpan<'_, '_, '_>,
9173
) {
92-
let span = self.current_span(threads);
74+
let span = current_span.get();
9375
self.creations.push(Event { parent, tag, range, span, time: self.current_time });
9476
self.current_time += 1;
9577
}
@@ -98,15 +80,20 @@ impl AllocHistory {
9880
&mut self,
9981
tag: SbTag,
10082
range: AllocRange,
101-
threads: &ThreadManager<'_, '_>,
83+
current_span: &mut CurrentSpan<'_, '_, '_>,
10284
) {
103-
let span = self.current_span(threads);
85+
let span = current_span.get();
10486
self.invalidations.push(Event { parent: None, tag, range, span, time: self.current_time });
10587
self.current_time += 1;
10688
}
10789

108-
pub fn log_protector(&mut self, orig_tag: SbTag, tag: SbTag, threads: &ThreadManager<'_, '_>) {
109-
let span = self.current_span(threads);
90+
pub fn log_protector(
91+
&mut self,
92+
orig_tag: SbTag,
93+
tag: SbTag,
94+
current_span: &mut CurrentSpan<'_, '_, '_>,
95+
) {
96+
let span = current_span.get();
11097
self.protectors.push(Protection { orig_tag, tag, span });
11198
self.current_time += 1;
11299
}

0 commit comments

Comments
 (0)