Skip to content

move our data structures into a central location #4454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/borrow_tracker/stacked_borrows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub type AllocState = Stacks;
#[derive(Clone, Debug)]
pub struct Stacks {
// Even reading memory can have effects on the stack, so we need a `RefCell` here.
stacks: RangeMap<Stack>,
stacks: DedupRangeMap<Stack>,
/// Stores past operations on this allocation
history: AllocHistory,
/// The set of tags that have been exposed inside this allocation.
Expand Down Expand Up @@ -468,7 +468,7 @@ impl<'tcx> Stacks {
let stack = Stack::new(item);

Stacks {
stacks: RangeMap::new(size, stack),
stacks: DedupRangeMap::new(size, stack),
history: AllocHistory::new(id, item, machine),
exposed_tags: FxHashSet::default(),
}
Expand Down
2 changes: 1 addition & 1 deletion src/borrow_tracker/tree_borrows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ trait EvalContextPrivExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let span = this.machine.current_span();

// Store initial permissions and their corresponding range.
let mut perms_map: RangeMap<LocationState> = RangeMap::new(
let mut perms_map: DedupRangeMap<LocationState> = DedupRangeMap::new(
ptr_size,
LocationState::new_accessed(Permission::new_disabled(), IdempotentForeignAccess::None), // this will be overwritten
);
Expand Down
6 changes: 3 additions & 3 deletions src/borrow_tracker/tree_borrows/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub struct Tree {
/// `unwrap` any `perm.get(key)`.
///
/// We do uphold the fact that `keys(perms)` is a subset of `keys(nodes)`
pub(super) rperms: RangeMap<UniValMap<LocationState>>,
pub(super) rperms: DedupRangeMap<UniValMap<LocationState>>,
/// The index of the root node.
pub(super) root: UniIndex,
}
Expand Down Expand Up @@ -609,7 +609,7 @@ impl Tree {
IdempotentForeignAccess::None,
),
);
RangeMap::new(size, perms)
DedupRangeMap::new(size, perms)
};
Self { root: root_idx, nodes, rperms, tag_mapping }
}
Expand All @@ -631,7 +631,7 @@ impl<'tcx> Tree {
base_offset: Size,
parent_tag: BorTag,
new_tag: BorTag,
initial_perms: RangeMap<LocationState>,
initial_perms: DedupRangeMap<LocationState>,
default_perm: Permission,
protected: bool,
span: Span,
Expand Down
4 changes: 2 additions & 2 deletions src/concurrency/data_race.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ pub trait EvalContextExt<'tcx>: MiriInterpCxExt<'tcx> {
#[derive(Debug, Clone)]
pub struct VClockAlloc {
/// Assigning each byte a MemoryCellClocks.
alloc_ranges: RefCell<RangeMap<MemoryCellClocks>>,
alloc_ranges: RefCell<DedupRangeMap<MemoryCellClocks>>,
}

impl VisitProvenance for VClockAlloc {
Expand Down Expand Up @@ -1045,7 +1045,7 @@ impl VClockAlloc {
(VTimestamp::ZERO, global.thread_index(ThreadId::MAIN_THREAD)),
};
VClockAlloc {
alloc_ranges: RefCell::new(RangeMap::new(
alloc_ranges: RefCell::new(DedupRangeMap::new(
len,
MemoryCellClocks::new(alloc_timestamp, alloc_index),
)),
Expand Down
1 change: 0 additions & 1 deletion src/concurrency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod cpu_affinity;
pub mod data_race;
mod data_race_handler;
pub mod init_once;
mod range_object_map;
pub mod sync;
pub mod thread;
mod vector_clock;
Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/weak_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ use rustc_data_structures::fx::FxHashMap;

use super::AllocDataRaceHandler;
use super::data_race::{GlobalState as DataRaceState, ThreadClockSet};
use super::range_object_map::{AccessType, RangeObjectMap};
use super::vector_clock::{VClock, VTimestamp, VectorIdx};
use crate::concurrency::GlobalDataRaceHandler;
use crate::data_structures::range_object_map::{AccessType, RangeObjectMap};
use crate::*;

pub type AllocState = StoreBufferAlloc;
Expand Down
22 changes: 11 additions & 11 deletions src/range_map.rs → src/data_structures/dedup_range_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ struct Elem<T> {
data: T,
}
#[derive(Clone, Debug)]
pub struct RangeMap<T> {
pub struct DedupRangeMap<T> {
v: Vec<Elem<T>>,
}

impl<T> RangeMap<T> {
impl<T> DedupRangeMap<T> {
/// Creates a new `RangeMap` for the given size, and with the given initial value used for
/// the entire range.
#[inline(always)]
pub fn new(size: Size, init: T) -> RangeMap<T> {
pub fn new(size: Size, init: T) -> DedupRangeMap<T> {
let size = size.bytes();
let v = if size > 0 { vec![Elem { range: 0..size, data: init }] } else { Vec::new() };
RangeMap { v }
DedupRangeMap { v }
}

pub fn size(&self) -> Size {
Expand Down Expand Up @@ -246,7 +246,7 @@ mod tests {
use super::*;

/// Query the map at every offset in the range and collect the results.
fn to_vec<T: Copy>(map: &RangeMap<T>, offset: u64, len: u64) -> Vec<T> {
fn to_vec<T: Copy>(map: &DedupRangeMap<T>, offset: u64, len: u64) -> Vec<T> {
(offset..offset + len)
.map(|i| {
map.iter(Size::from_bytes(i), Size::from_bytes(1)).next().map(|(_, &t)| t).unwrap()
Expand All @@ -256,7 +256,7 @@ mod tests {

#[test]
fn basic_insert() {
let mut map = RangeMap::<i32>::new(Size::from_bytes(20), -1);
let mut map = DedupRangeMap::<i32>::new(Size::from_bytes(20), -1);
// Insert.
for (_, x) in map.iter_mut(Size::from_bytes(10), Size::from_bytes(1)) {
*x = 42;
Expand All @@ -278,7 +278,7 @@ mod tests {

#[test]
fn gaps() {
let mut map = RangeMap::<i32>::new(Size::from_bytes(20), -1);
let mut map = DedupRangeMap::<i32>::new(Size::from_bytes(20), -1);
for (_, x) in map.iter_mut(Size::from_bytes(11), Size::from_bytes(1)) {
*x = 42;
}
Expand Down Expand Up @@ -319,26 +319,26 @@ mod tests {
#[test]
#[should_panic]
fn out_of_range_iter_mut() {
let mut map = RangeMap::<i32>::new(Size::from_bytes(20), -1);
let mut map = DedupRangeMap::<i32>::new(Size::from_bytes(20), -1);
let _ = map.iter_mut(Size::from_bytes(11), Size::from_bytes(11));
}

#[test]
#[should_panic]
fn out_of_range_iter() {
let map = RangeMap::<i32>::new(Size::from_bytes(20), -1);
let map = DedupRangeMap::<i32>::new(Size::from_bytes(20), -1);
let _ = map.iter(Size::from_bytes(11), Size::from_bytes(11));
}

#[test]
fn empty_map_iter() {
let map = RangeMap::<i32>::new(Size::from_bytes(0), -1);
let map = DedupRangeMap::<i32>::new(Size::from_bytes(0), -1);
let _ = map.iter(Size::from_bytes(0), Size::from_bytes(0));
}

#[test]
fn empty_map_iter_mut() {
let mut map = RangeMap::<i32>::new(Size::from_bytes(0), -1);
let mut map = DedupRangeMap::<i32>::new(Size::from_bytes(0), -1);
let _ = map.iter_mut(Size::from_bytes(0), Size::from_bytes(0));
}
}
3 changes: 3 additions & 0 deletions src/data_structures/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod dedup_range_map;
pub mod mono_hash_map;
pub mod range_object_map;
File renamed without changes.
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,15 @@ mod alloc_addresses;
mod borrow_tracker;
mod clock;
mod concurrency;
mod data_structures;
mod diagnostics;
mod eval;
mod helpers;
mod intrinsics;
mod machine;
mod math;
mod mono_hash_map;
mod operator;
mod provenance_gc;
mod range_map;
mod shims;

// Establish a "crate-wide prelude": we often import `crate::*`.
Expand Down Expand Up @@ -132,6 +131,8 @@ pub use crate::concurrency::thread::{
ThreadManager, TimeoutAnchor, TimeoutClock, UnblockKind,
};
pub use crate::concurrency::{GenmcConfig, GenmcCtx};
pub use crate::data_structures::dedup_range_map::DedupRangeMap;
pub use crate::data_structures::mono_hash_map::MonoHashMap;
pub use crate::diagnostics::{
EvalContextExt as _, NonHaltingDiagnostic, TerminationInfo, report_error,
};
Expand All @@ -145,10 +146,8 @@ pub use crate::machine::{
AllocExtra, DynMachineCallback, FrameExtra, MachineCallback, MemoryKind, MiriInterpCx,
MiriInterpCxExt, MiriMachine, MiriMemoryKind, PrimitiveLayouts, Provenance, ProvenanceExtra,
};
pub use crate::mono_hash_map::MonoHashMap;
pub use crate::operator::EvalContextExt as _;
pub use crate::provenance_gc::{EvalContextExt as _, LiveAllocs, VisitProvenance, VisitWith};
pub use crate::range_map::RangeMap;
pub use crate::shims::EmulateItemResult;
pub use crate::shims::env::{EnvVars, EvalContextExt as _};
pub use crate::shims::foreign_items::{DynSym, EvalContextExt as _};
Expand Down