Skip to content

Commit e473fee

Browse files
committed
Factor out sc_read and sc_writes
1 parent e276a78 commit e473fee

File tree

1 file changed

+14
-41
lines changed

1 file changed

+14
-41
lines changed

src/data_race.rs

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -481,16 +481,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
481481
// the *value* (including the associated provenance if this is an AtomicPtr) at this location.
482482
// Only metadata on the location itself is used.
483483
let scalar = this.allow_data_races_ref(move |this| this.read_scalar(&place.into()))?;
484-
if atomic == AtomicReadOp::SeqCst {
485-
if let Some(global) = &this.memory.extra.data_race {
486-
global.sc_read();
487-
}
488-
}
489484

490485
if let Some(global) = &this.memory.extra.data_race {
491486
let (alloc_id, base_offset, ..) = this.memory.ptr_get_alloc(place.ptr)?;
492487
if let Some(alloc_buffers) = this.memory.get_alloc_extra(alloc_id)?.weak_memory.as_ref()
493488
{
489+
if atomic == AtomicReadOp::SeqCst {
490+
global.sc_read();
491+
}
494492
let mut rng = this.memory.extra.rng.borrow_mut();
495493
let loaded = alloc_buffers.buffered_read(
496494
alloc_range(base_offset, place.layout.size),
@@ -518,19 +516,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
518516
let this = self.eval_context_mut();
519517
this.allow_data_races_mut(move |this| this.write_scalar(val, &(*dest).into()))?;
520518

521-
if atomic == AtomicWriteOp::SeqCst {
522-
if let Some(global) = this.memory.extra.data_race.as_ref() {
523-
global.sc_write();
524-
}
525-
}
526-
527519
this.validate_atomic_store(dest, atomic)?;
528520
let (alloc_id, base_offset, ..) = this.memory.ptr_get_alloc(dest.ptr)?;
529521
if let (
530522
crate::AllocExtra { weak_memory: Some(alloc_buffers), .. },
531523
crate::MemoryExtra { data_race: Some(global), .. },
532524
) = this.memory.get_alloc_extra_mut(alloc_id)?
533525
{
526+
if atomic == AtomicWriteOp::SeqCst {
527+
global.sc_write();
528+
}
534529
let size = dest.layout.size;
535530
alloc_buffers.buffered_write(
536531
val,
@@ -561,12 +556,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
561556
let val = if neg { this.unary_op(mir::UnOp::Not, &val)? } else { val };
562557
this.allow_data_races_mut(|this| this.write_immediate(*val, &(*place).into()))?;
563558

564-
if atomic == AtomicRwOp::SeqCst {
565-
if let Some(global) = &this.memory.extra.data_race {
566-
global.sc_read();
567-
global.sc_write();
568-
}
569-
}
570559
this.validate_atomic_rmw(place, atomic)?;
571560

572561
this.buffered_atomic_rmw(val.to_scalar_or_uninit(), place, atomic)?;
@@ -585,12 +574,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
585574

586575
let old = this.allow_data_races_mut(|this| this.read_scalar(&place.into()))?;
587576
this.allow_data_races_mut(|this| this.write_scalar(new, &(*place).into()))?;
588-
if atomic == AtomicRwOp::SeqCst {
589-
if let Some(global) = &this.memory.extra.data_race {
590-
global.sc_read();
591-
global.sc_write();
592-
}
593-
}
577+
594578
this.validate_atomic_rmw(place, atomic)?;
595579

596580
this.buffered_atomic_rmw(new, place, atomic)?;
@@ -619,12 +603,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
619603

620604
this.allow_data_races_mut(|this| this.write_immediate(**new_val, &(*place).into()))?;
621605

622-
if atomic == AtomicRwOp::SeqCst {
623-
if let Some(global) = &this.memory.extra.data_race {
624-
global.sc_read();
625-
global.sc_write();
626-
}
627-
}
628606
this.validate_atomic_rmw(&place, atomic)?;
629607

630608
this.buffered_atomic_rmw(new_val.to_scalar_or_uninit(), place, atomic)?;
@@ -673,27 +651,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
673651
// otherwise treat this as an atomic load with the fail ordering.
674652
if cmpxchg_success {
675653
this.allow_data_races_mut(|this| this.write_scalar(new, &(*place).into()))?;
676-
if success == AtomicRwOp::SeqCst {
677-
if let Some(global) = &this.memory.extra.data_race {
678-
global.sc_read();
679-
global.sc_write();
680-
}
681-
}
682654
this.validate_atomic_rmw(place, success)?;
683655
this.buffered_atomic_rmw(new, place, success)?;
684656
} else {
685-
if fail == AtomicReadOp::SeqCst {
686-
if let Some(global) = &this.memory.extra.data_race {
687-
global.sc_read();
688-
}
689-
}
690-
691657
this.validate_atomic_load(place, fail)?;
692658
// A failed compare exchange is equivalent to a load, reading from the latest store
693659
// in the modification order.
694660
// Since `old` is only a value and not the store element, we need to separately
695661
// find it in our store buffer and perform load_impl on it.
696662
if let Some(global) = &this.memory.extra.data_race {
663+
if fail == AtomicReadOp::SeqCst {
664+
global.sc_read();
665+
}
697666
let size = place.layout.size;
698667
let (alloc_id, base_offset, ..) = this.memory.ptr_get_alloc(place.ptr)?;
699668
if let Some(alloc_buffers) =
@@ -723,6 +692,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
723692
crate::MemoryExtra { data_race: Some(global), .. },
724693
) = this.memory.get_alloc_extra_mut(alloc_id)?
725694
{
695+
if atomic == AtomicRwOp::SeqCst {
696+
global.sc_read();
697+
global.sc_write();
698+
}
726699
let size = place.layout.size;
727700
let range = alloc_range(base_offset, size);
728701
alloc_buffers.read_from_last_store(range, global);

0 commit comments

Comments
 (0)