Skip to content

Commit 015df68

Browse files
authored
Rollup merge of rust-lang#62264 - RalfJung:inline-forcing, r=zackmdavis
Fix perf regression from Miri Machine trait changes Maybe this fixes the perf regression that rust-lang#62003 seemingly introduced? Cc @nnethercote
2 parents d11dc9e + 52e6f85 commit 015df68

File tree

4 files changed

+68
-64
lines changed

4 files changed

+68
-64
lines changed

src/librustc_mir/const_eval.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc::hir::def::DefKind;
1111
use rustc::hir::def_id::DefId;
1212
use rustc::mir::interpret::{ConstEvalErr, ErrorHandled, ScalarMaybeUndef};
1313
use rustc::mir;
14-
use rustc::ty::{self, TyCtxt, query::TyCtxtAt};
14+
use rustc::ty::{self, TyCtxt};
1515
use rustc::ty::layout::{self, LayoutOf, VariantIdx};
1616
use rustc::ty::subst::Subst;
1717
use rustc::traits::Reveal;
@@ -23,7 +23,7 @@ use crate::interpret::{self,
2323
PlaceTy, MPlaceTy, OpTy, ImmTy, Immediate, Scalar,
2424
RawConst, ConstValue,
2525
InterpResult, InterpErrorInfo, InterpError, GlobalId, InterpCx, StackPopCleanup,
26-
Allocation, AllocId, MemoryKind, Memory,
26+
Allocation, AllocId, MemoryKind,
2727
snapshot, RefTracking, intern_const_alloc_recursive,
2828
};
2929

@@ -409,27 +409,27 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
409409
}
410410

411411
fn find_foreign_static(
412+
_tcx: TyCtxt<'tcx>,
412413
_def_id: DefId,
413-
_tcx: TyCtxtAt<'tcx>,
414414
) -> InterpResult<'tcx, Cow<'tcx, Allocation<Self::PointerTag>>> {
415415
err!(ReadForeignStatic)
416416
}
417417

418418
#[inline(always)]
419419
fn tag_allocation<'b>(
420+
_memory_extra: &(),
420421
_id: AllocId,
421422
alloc: Cow<'b, Allocation>,
422423
_kind: Option<MemoryKind<!>>,
423-
_memory: &Memory<'mir, 'tcx, Self>,
424424
) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
425425
// We do not use a tag so we can just cheaply forward the allocation
426426
(alloc, ())
427427
}
428428

429429
#[inline(always)]
430430
fn tag_static_base_pointer(
431+
_memory_extra: &(),
431432
_id: AllocId,
432-
_memory: &Memory<'mir, 'tcx, Self>,
433433
) -> Self::PointerTag {
434434
()
435435
}

src/librustc_mir/interpret/eval_context.rs

+38-36
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,23 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
222222
&mut self.memory
223223
}
224224

225+
#[inline(always)]
226+
pub fn force_ptr(
227+
&self,
228+
scalar: Scalar<M::PointerTag>,
229+
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
230+
self.memory.force_ptr(scalar)
231+
}
232+
233+
#[inline(always)]
234+
pub fn force_bits(
235+
&self,
236+
scalar: Scalar<M::PointerTag>,
237+
size: Size
238+
) -> InterpResult<'tcx, u128> {
239+
self.memory.force_bits(scalar, size)
240+
}
241+
225242
#[inline(always)]
226243
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
227244
self.memory.tag_static_base_pointer(ptr)
@@ -253,6 +270,27 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
253270
self.frame().body
254271
}
255272

273+
#[inline(always)]
274+
pub fn sign_extend(&self, value: u128, ty: TyLayout<'_>) -> u128 {
275+
assert!(ty.abi.is_signed());
276+
sign_extend(value, ty.size)
277+
}
278+
279+
#[inline(always)]
280+
pub fn truncate(&self, value: u128, ty: TyLayout<'_>) -> u128 {
281+
truncate(value, ty.size)
282+
}
283+
284+
#[inline]
285+
pub fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
286+
ty.is_sized(self.tcx, self.param_env)
287+
}
288+
289+
#[inline]
290+
pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
291+
ty.is_freeze(*self.tcx, self.param_env, DUMMY_SP)
292+
}
293+
256294
pub(super) fn subst_and_normalize_erasing_regions<T: TypeFoldable<'tcx>>(
257295
&self,
258296
substs: T,
@@ -288,14 +326,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
288326
).ok_or_else(|| InterpError::TooGeneric.into())
289327
}
290328

291-
pub fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
292-
ty.is_sized(self.tcx, self.param_env)
293-
}
294-
295-
pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
296-
ty.is_freeze(*self.tcx, self.param_env, DUMMY_SP)
297-
}
298-
299329
pub fn load_mir(
300330
&self,
301331
instance: ty::InstanceDef<'tcx>,
@@ -766,32 +796,4 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
766796
trace!("generate stacktrace: {:#?}, {:?}", frames, explicit_span);
767797
frames
768798
}
769-
770-
#[inline(always)]
771-
pub fn sign_extend(&self, value: u128, ty: TyLayout<'_>) -> u128 {
772-
assert!(ty.abi.is_signed());
773-
sign_extend(value, ty.size)
774-
}
775-
776-
#[inline(always)]
777-
pub fn truncate(&self, value: u128, ty: TyLayout<'_>) -> u128 {
778-
truncate(value, ty.size)
779-
}
780-
781-
#[inline(always)]
782-
pub fn force_ptr(
783-
&self,
784-
scalar: Scalar<M::PointerTag>,
785-
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
786-
self.memory.force_ptr(scalar)
787-
}
788-
789-
#[inline(always)]
790-
pub fn force_bits(
791-
&self,
792-
scalar: Scalar<M::PointerTag>,
793-
size: Size
794-
) -> InterpResult<'tcx, u128> {
795-
self.memory.force_bits(scalar, size)
796-
}
797799
}

src/librustc_mir/interpret/machine.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use std::hash::Hash;
77

88
use rustc::hir::def_id::DefId;
99
use rustc::mir;
10-
use rustc::ty::{self, query::TyCtxtAt};
10+
use rustc::ty::{self, TyCtxt};
1111

1212
use super::{
13-
Allocation, AllocId, InterpResult, Scalar, AllocationExtra,
14-
InterpCx, PlaceTy, OpTy, ImmTy, MemoryKind, Pointer, Memory
13+
Allocation, AllocId, InterpResult, InterpError, Scalar, AllocationExtra,
14+
InterpCx, PlaceTy, OpTy, ImmTy, MemoryKind, Pointer, Memory,
1515
};
1616

1717
/// Whether this kind of memory is allowed to leak
@@ -151,8 +151,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
151151
///
152152
/// This allocation will then be fed to `tag_allocation` to initialize the "extra" state.
153153
fn find_foreign_static(
154+
tcx: TyCtxt<'tcx>,
154155
def_id: DefId,
155-
tcx: TyCtxtAt<'tcx>,
156156
) -> InterpResult<'tcx, Cow<'tcx, Allocation>>;
157157

158158
/// Called for all binary operations on integer(-like) types when one operand is a pointer
@@ -189,10 +189,10 @@ pub trait Machine<'mir, 'tcx>: Sized {
189189
/// For static allocations, the tag returned must be the same as the one returned by
190190
/// `tag_static_base_pointer`.
191191
fn tag_allocation<'b>(
192+
memory_extra: &Self::MemoryExtra,
192193
id: AllocId,
193194
alloc: Cow<'b, Allocation>,
194195
kind: Option<MemoryKind<Self::MemoryKinds>>,
195-
memory: &Memory<'mir, 'tcx, Self>,
196196
) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag);
197197

198198
/// Return the "base" tag for the given static allocation: the one that is used for direct
@@ -201,8 +201,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
201201
/// Be aware that requesting the `Allocation` for that `id` will lead to cycles
202202
/// for cyclic statics!
203203
fn tag_static_base_pointer(
204+
memory_extra: &Self::MemoryExtra,
204205
id: AllocId,
205-
memory: &Memory<'mir, 'tcx, Self>,
206206
) -> Self::PointerTag;
207207

208208
/// Executes a retagging operation
@@ -224,20 +224,22 @@ pub trait Machine<'mir, 'tcx>: Sized {
224224
extra: Self::FrameExtra,
225225
) -> InterpResult<'tcx>;
226226

227+
#[inline(always)]
227228
fn int_to_ptr(
228-
int: u64,
229229
_mem: &Memory<'mir, 'tcx, Self>,
230+
int: u64,
230231
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
231-
if int == 0 {
232-
err!(InvalidNullPointerUsage)
232+
Err((if int == 0 {
233+
InterpError::InvalidNullPointerUsage
233234
} else {
234-
err!(ReadBytesAsPointer)
235-
}
235+
InterpError::ReadBytesAsPointer
236+
}).into())
236237
}
237238

239+
#[inline(always)]
238240
fn ptr_to_int(
239-
_ptr: Pointer<Self::PointerTag>,
240241
_mem: &Memory<'mir, 'tcx, Self>,
242+
_ptr: Pointer<Self::PointerTag>,
241243
) -> InterpResult<'tcx, u64> {
242244
err!(ReadPointerAsBytes)
243245
}

src/librustc_mir/interpret/memory.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
143143

144144
#[inline]
145145
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
146-
ptr.with_tag(M::tag_static_base_pointer(ptr.alloc_id, &self))
146+
ptr.with_tag(M::tag_static_base_pointer(&self.extra, ptr.alloc_id))
147147
}
148148

149149
pub fn create_fn_alloc(
@@ -189,7 +189,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
189189
kind: MemoryKind<M::MemoryKinds>,
190190
) -> Pointer<M::PointerTag> {
191191
let id = self.tcx.alloc_map.lock().reserve();
192-
let (alloc, tag) = M::tag_allocation(id, Cow::Owned(alloc), Some(kind), &self);
192+
let (alloc, tag) = M::tag_allocation(&self.extra, id, Cow::Owned(alloc), Some(kind));
193193
self.alloc_map.insert(id, (kind, alloc.into_owned()));
194194
Pointer::from(id).with_tag(tag)
195195
}
@@ -407,9 +407,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
407407
/// contains a reference to memory that was created during its evaluation (i.e., not to
408408
/// another static), those inner references only exist in "resolved" form.
409409
fn get_static_alloc(
410-
id: AllocId,
410+
memory_extra: &M::MemoryExtra,
411411
tcx: TyCtxtAt<'tcx>,
412-
memory: &Memory<'mir, 'tcx, M>,
412+
id: AllocId,
413413
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
414414
let alloc = tcx.alloc_map.lock().get(id);
415415
let alloc = match alloc {
@@ -423,7 +423,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
423423
// We got a "lazy" static that has not been computed yet.
424424
if tcx.is_foreign_item(def_id) {
425425
trace!("static_alloc: foreign item {:?}", def_id);
426-
M::find_foreign_static(def_id, tcx)?
426+
M::find_foreign_static(tcx.tcx, def_id)?
427427
} else {
428428
trace!("static_alloc: Need to compute {:?}", def_id);
429429
let instance = Instance::mono(tcx.tcx, def_id);
@@ -453,10 +453,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
453453
// We got tcx memory. Let the machine figure out whether and how to
454454
// turn that into memory with the right pointer tag.
455455
Ok(M::tag_allocation(
456+
memory_extra,
456457
id, // always use the ID we got as input, not the "hidden" one.
457458
alloc,
458459
M::STATIC_KIND.map(MemoryKind::Machine),
459-
memory
460460
).0)
461461
}
462462

@@ -469,7 +469,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
469469
// `get_static_alloc` that we can actually use directly without inserting anything anywhere.
470470
// So the error type is `InterpResult<'tcx, &Allocation<M::PointerTag>>`.
471471
let a = self.alloc_map.get_or(id, || {
472-
let alloc = Self::get_static_alloc(id, self.tcx, &self).map_err(Err)?;
472+
let alloc = Self::get_static_alloc(&self.extra, self.tcx, id).map_err(Err)?;
473473
match alloc {
474474
Cow::Borrowed(alloc) => {
475475
// We got a ref, cheaply return that as an "error" so that the
@@ -498,11 +498,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
498498
id: AllocId,
499499
) -> InterpResult<'tcx, &mut Allocation<M::PointerTag, M::AllocExtra>> {
500500
let tcx = self.tcx;
501-
let alloc = Self::get_static_alloc(id, tcx, &self);
501+
let memory_extra = &self.extra;
502502
let a = self.alloc_map.get_mut_or(id, || {
503503
// Need to make a copy, even if `get_static_alloc` is able
504504
// to give us a cheap reference.
505-
let alloc = alloc?;
505+
let alloc = Self::get_static_alloc(memory_extra, tcx, id)?;
506506
if alloc.mutability == Mutability::Immutable {
507507
return err!(ModifiedConstantMemory);
508508
}
@@ -948,7 +948,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
948948
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
949949
match scalar {
950950
Scalar::Ptr(ptr) => Ok(ptr),
951-
_ => M::int_to_ptr(scalar.to_usize(self)?, self)
951+
_ => M::int_to_ptr(&self, scalar.to_usize(self)?)
952952
}
953953
}
954954

@@ -959,7 +959,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
959959
) -> InterpResult<'tcx, u128> {
960960
match scalar.to_bits_or_ptr(size, self) {
961961
Ok(bits) => Ok(bits),
962-
Err(ptr) => Ok(M::ptr_to_int(ptr, self)? as u128)
962+
Err(ptr) => Ok(M::ptr_to_int(&self, ptr)? as u128)
963963
}
964964
}
965965
}

0 commit comments

Comments
 (0)