Skip to content

Commit bfb0196

Browse files
authored
Merge pull request #1603 from N1ark/uneval-repeat
Speedup `rustc_const_uneval`
2 parents b9da4e3 + ceb8a04 commit bfb0196

7 files changed

Lines changed: 77 additions & 4 deletions

File tree

‎charon/rustc_const_uneval/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ pub enum ConstKind<'tcx> {
130130
variant: Option<VariantIdx>,
131131
fields: Vec<Const<'tcx>>,
132132
},
133+
/// An array of `count >= 2` copies of `value`.
134+
Repeat { value: Box<Const<'tcx>>, count: u64 },
133135
/// A function item. This is a ZST, unlike `FnPtr`.
134136
FnDef {
135137
def: DefId,

‎charon/rustc_const_uneval/src/memory.rs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,41 @@ impl<'tcx> ConstReader<'tcx> {
129129
interp_ok(bytes)
130130
}
131131

132+
/// If the array `op` is made of two or more copies of one value, the number of copies. This
133+
/// compares raw bytes, so that large arrays aren't read element by element; arrays with
134+
/// uninitialized bytes or pointers are conservatively not repeats.
135+
fn repeat_count(
136+
&self,
137+
ecx: &CompileTimeInterpCx<'tcx>,
138+
op: &OpTy<'tcx>,
139+
) -> InterpResult<'tcx, Option<u64>> {
140+
let Some(mplace) = op.as_mplace_or_imm().left() else {
141+
return interp_ok(None);
142+
};
143+
let count = mplace.len(ecx)?;
144+
if count < 2 {
145+
return interp_ok(None);
146+
}
147+
let size = mplace.layout.size;
148+
// Zero-sized values are all equal.
149+
if size.bytes() == 0 {
150+
return interp_ok(Some(count));
151+
}
152+
let (alloc_id, offset, _) = ecx.ptr_get_alloc_id(mplace.ptr(), size.bytes() as i64)?;
153+
let alloc = ecx.get_alloc_raw(alloc_id)?;
154+
let range = interpret::alloc_range(offset, size);
155+
if alloc.init_mask().is_range_initialized(range).is_err()
156+
|| !alloc.provenance().range_empty(range, ecx)
157+
{
158+
return interp_ok(None);
159+
}
160+
// The elements are all equal iff the bytes are equal to themselves shifted by one element.
161+
let elem_size = (size.bytes() / count) as usize;
162+
let bytes = alloc.get_bytes_unchecked(range);
163+
let is_repeat = bytes[elem_size..] == bytes[..bytes.len() - elem_size];
164+
interp_ok(is_repeat.then_some(count))
165+
}
166+
132167
/// The sized type that `tail`, the unsized tail of `place`, was unsized from. This is found in
133168
/// the metadata of the place.
134169
pub(crate) fn sized_tail(
@@ -250,6 +285,13 @@ impl<'tcx> ConstReader<'tcx> {
250285
variant: None,
251286
fields: read_fields(op, args.len())?,
252287
},
288+
ty::Array(..) if let Some(count) = self.repeat_count(ecx, &op)? => {
289+
let value = self.read_op(ecx, ecx.project_index(&op, 0)?)?;
290+
ConstKind::Repeat {
291+
value: Box::new(value),
292+
count,
293+
}
294+
}
253295
ty::Array(..) | ty::Slice(..) => {
254296
let mut elems = ecx.project_array_fields(&op)?;
255297
let mut fields = vec![];

‎charon/rustc_const_uneval/src/pointer.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,12 @@ impl<'tcx> ConstReader<'tcx> {
200200
};
201201
alloc.init_mask().is_range_initialized(range).is_ok()
202202
&& alloc.provenance().range_empty(range, &self.tcx)
203-
&& alloc
204-
.get_bytes_unchecked(range)
205-
.chunks_exact(elem_ty.primitive_size(self.tcx).bytes_usize())
206-
.all(is_valid_elem)
203+
// Ints/floats are valid for any bit pattern.
204+
&& (matches!(elem_ty.kind(), Int(_) | Uint(_) | Float(_))
205+
|| alloc
206+
.get_bytes_unchecked(range)
207+
.chunks_exact(elem_ty.primitive_size(self.tcx).bytes_usize())
208+
.all(is_valid_elem))
207209
}
208210

209211
/// Whether we view a value of the unsized type `ty` at the sized type it was unsized from: a

‎charon/rustc_const_uneval/src/valtree.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ impl<'tcx> ConstReader<'tcx> {
6565
.collect();
6666
ConstKind::str(bytes)
6767
}
68+
// Valtrees are interned, so equal elements are the same pointer.
69+
(ty::ValTreeKind::Branch(fields), ty::Array(..))
70+
if let [first, rest @ ..] = &fields[..]
71+
&& !rest.is_empty()
72+
&& rest.iter().all(|field| field == first) =>
73+
{
74+
ConstKind::Repeat {
75+
value: Box::new(read_field(*first)?),
76+
count: fields.len() as u64,
77+
}
78+
}
6879
(ty::ValTreeKind::Branch(fields), ty::Array(..) | ty::Slice(..) | ty::Tuple(..)) => {
6980
ConstKind::Aggregate {
7081
variant: None,

‎charon/src/bin/charon-driver/hax/constant_utils.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub enum ConstantExprKind<'tcx> {
3939
Array {
4040
fields: Vec<ConstantExpr<'tcx>>,
4141
},
42+
Repeat {
43+
value: ConstantExpr<'tcx>,
44+
},
4245
Tuple {
4346
fields: Vec<ConstantExpr<'tcx>>,
4447
},

‎charon/src/bin/charon-driver/hax/constant_utils/uneval.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, ConstantExpr<'tcx>>
242242
_ => unreachable!("unexpected aggregate type {ty:?}"),
243243
}
244244
}
245+
ConstKind::Repeat { value, .. } => ConstantExprKind::Repeat {
246+
value: value.sinto(s),
247+
},
245248
ConstKind::FnDef { def, args } => {
246249
ConstantExprKind::FnDef(translate_item_ref(s, *def, args))
247250
}

‎charon/src/bin/charon-driver/translate/translate_constants.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ impl<'tcx, 'ctx> ItemTransCtx<'tcx, 'ctx> {
155155
ConstantExprKind::Array(fields)
156156
}
157157
}
158+
hax::ConstantExprKind::Repeat { value } => {
159+
let TyKind::Array(_, count, value_is_sized) = ty.kind() else {
160+
unreachable!("Unexpected type for a repeat constant: {ty:?}")
161+
};
162+
ConstantExprKind::Repeat {
163+
value: self.translate_constant_expr(span, value)?,
164+
count: count.clone(),
165+
value_is_sized: value_is_sized.clone(),
166+
}
167+
}
158168
hax::ConstantExprKind::Tuple { fields } => {
159169
let fields: IndexVec<FieldId, ConstantExpr> = fields
160170
.iter()

0 commit comments

Comments
 (0)