Skip to content

Commit af4064f

Browse files
Auto merge of #150187 - RalfJung:visitor-in-mem-order, r=<try>
interpreter/visitor: always iterate in in-memory order
2 parents cb79c42 + ca8a59a commit af4064f

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,23 @@ impl RangeSet {
232232
return;
233233
}
234234
let v = &mut self.0;
235+
// The by far most common case is that this is added at the end. We can't rely on that
236+
// due to enum discriminants which can be "in the middle" of the active variant,
237+
// but we can at least have a fast-path here.
238+
if let Some(last) = v.last_mut()
239+
&& offset >= last.0
240+
{
241+
// The new range either overlaps with the last element, or is after it.
242+
let end = last.0 + last.1;
243+
if offset > end {
244+
// The new range is strictly after the last element.
245+
v.push((offset, size));
246+
} else {
247+
// Merge new range into last element.
248+
last.1 = last.1.max(offset + size - last.0);
249+
}
250+
return;
251+
}
235252
// We scan for a partition point where the left partition is all the elements that end
236253
// strictly before we start. Those are elements that are too "low" to merge with us.
237254
let idx =

compiler/rustc_const_eval/src/interpret/visitor.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use std::num::NonZero;
55

66
use rustc_abi::{FieldIdx, FieldsShape, VariantIdx, Variants};
7-
use rustc_index::{Idx as _, IndexVec};
87
use rustc_middle::mir::interpret::InterpResult;
98
use rustc_middle::ty::{self, Ty};
109
use tracing::trace;
@@ -24,20 +23,6 @@ pub trait ValueVisitor<'tcx, M: Machine<'tcx>>: Sized {
2423
self.ecx().read_discriminant(&v.to_op(self.ecx())?)
2524
}
2625

27-
/// This function provides the chance to reorder the order in which fields are visited for
28-
/// `FieldsShape::Aggregate`.
29-
///
30-
/// The default means we iterate in source declaration order; alternatively this can use
31-
/// `in_memory_order` to iterate in memory order.
32-
#[inline(always)]
33-
fn aggregate_field_iter(
34-
in_memory_order: &IndexVec<u32, FieldIdx>,
35-
) -> impl Iterator<Item = FieldIdx> {
36-
// Allow the optimizer to elide the bounds checking when creating each index.
37-
let _ = FieldIdx::new(in_memory_order.len());
38-
(0..in_memory_order.len()).map(FieldIdx::new)
39-
}
40-
4126
// Recursive actions, ready to be overloaded.
4227
/// Visits the given value, dispatching as appropriate to more specialized visitors.
4328
#[inline(always)]
@@ -171,7 +156,7 @@ pub trait ValueVisitor<'tcx, M: Machine<'tcx>>: Sized {
171156
self.visit_union(v, fields)?;
172157
}
173158
FieldsShape::Arbitrary { in_memory_order, .. } => {
174-
for idx in Self::aggregate_field_iter(in_memory_order) {
159+
for idx in in_memory_order.iter().copied() {
175160
let field = self.ecx().project_field(v, idx)?;
176161
self.visit_field(v, idx.as_usize(), &field)?;
177162
}

src/tools/miri/src/helpers.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_data_structures::fx::{FxBuildHasher, FxHashSet};
1010
use rustc_hir::Safety;
1111
use rustc_hir::def::{DefKind, Namespace};
1212
use rustc_hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefId, LOCAL_CRATE};
13-
use rustc_index::IndexVec;
1413
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1514
use rustc_middle::middle::dependency_format::Linkage;
1615
use rustc_middle::middle::exported_symbols::ExportedSymbol;
@@ -583,12 +582,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
583582
self.ecx
584583
}
585584

586-
fn aggregate_field_iter(
587-
in_memory_order: &IndexVec<u32, FieldIdx>,
588-
) -> impl Iterator<Item = FieldIdx> {
589-
in_memory_order.iter().copied()
590-
}
591-
592585
// Hook to detect `UnsafeCell`.
593586
fn visit_value(&mut self, v: &MPlaceTy<'tcx>) -> InterpResult<'tcx> {
594587
trace!("UnsafeCellVisitor: {:?} {:?}", *v, v.layout.ty);

0 commit comments

Comments
 (0)