Skip to content

Commit 9512d87

Browse files
committed
Factor out sc_read and sc_writes
1 parent 8186d1c commit 9512d87

File tree

1 file changed

+14
-41
lines changed

1 file changed

+14
-41
lines changed

src/data_race.rs

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

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

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

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

573562
this.buffered_atomic_rmw(val.to_scalar_or_uninit(), place, atomic)?;
@@ -586,12 +575,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
586575

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

597581
this.buffered_atomic_rmw(new, place, atomic)?;
@@ -620,12 +604,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
620604

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

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

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

0 commit comments

Comments
 (0)