Skip to content

Commit ed4ad9c

Browse files
committed
Auto merge of #62264 - RalfJung:inline-forcing, r=<try>
Fix perf regression from Miri Machine trait changes Maybe this fixes the perf regression that #62003 seemingly introduced? Cc @nnethercote
2 parents 99abdfa + ffc6670 commit ed4ad9c

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, InterpretCx, StackPopCleanup,
26-
Allocation, AllocId, MemoryKind, Memory,
26+
Allocation, AllocId, MemoryKind,
2727
snapshot, RefTracking, intern_const_alloc_recursive,
2828
};
2929

@@ -398,27 +398,27 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
398398
}
399399

400400
fn find_foreign_static(
401+
_tcx: TyCtxt<'tcx>,
401402
_def_id: DefId,
402-
_tcx: TyCtxtAt<'tcx>,
403403
) -> InterpResult<'tcx, Cow<'tcx, Allocation<Self::PointerTag>>> {
404404
err!(ReadForeignStatic)
405405
}
406406

407407
#[inline(always)]
408408
fn tag_allocation<'b>(
409+
_memory_extra: &(),
409410
_id: AllocId,
410411
alloc: Cow<'b, Allocation>,
411412
_kind: Option<MemoryKind<!>>,
412-
_memory: &Memory<'mir, 'tcx, Self>,
413413
) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
414414
// We do not use a tag so we can just cheaply forward the allocation
415415
(alloc, ())
416416
}
417417

418418
#[inline(always)]
419419
fn tag_static_base_pointer(
420+
_memory_extra: &(),
420421
_id: AllocId,
421-
_memory: &Memory<'mir, 'tcx, Self>,
422422
) -> Self::PointerTag {
423423
()
424424
}

src/librustc_mir/interpret/eval_context.rs

+38-36
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,23 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
217217
&mut self.memory
218218
}
219219

220+
#[inline(always)]
221+
pub fn force_ptr(
222+
&self,
223+
scalar: Scalar<M::PointerTag>,
224+
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
225+
self.memory.force_ptr(scalar)
226+
}
227+
228+
#[inline(always)]
229+
pub fn force_bits(
230+
&self,
231+
scalar: Scalar<M::PointerTag>,
232+
size: Size
233+
) -> InterpResult<'tcx, u128> {
234+
self.memory.force_bits(scalar, size)
235+
}
236+
220237
#[inline(always)]
221238
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
222239
self.memory.tag_static_base_pointer(ptr)
@@ -248,6 +265,27 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
248265
self.frame().body
249266
}
250267

268+
#[inline(always)]
269+
pub fn sign_extend(&self, value: u128, ty: TyLayout<'_>) -> u128 {
270+
assert!(ty.abi.is_signed());
271+
sign_extend(value, ty.size)
272+
}
273+
274+
#[inline(always)]
275+
pub fn truncate(&self, value: u128, ty: TyLayout<'_>) -> u128 {
276+
truncate(value, ty.size)
277+
}
278+
279+
#[inline]
280+
pub fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
281+
ty.is_sized(self.tcx, self.param_env)
282+
}
283+
284+
#[inline]
285+
pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
286+
ty.is_freeze(*self.tcx, self.param_env, DUMMY_SP)
287+
}
288+
251289
pub(super) fn subst_and_normalize_erasing_regions<T: TypeFoldable<'tcx>>(
252290
&self,
253291
substs: T,
@@ -283,14 +321,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
283321
).ok_or_else(|| InterpError::TooGeneric.into())
284322
}
285323

286-
pub fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
287-
ty.is_sized(self.tcx, self.param_env)
288-
}
289-
290-
pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
291-
ty.is_freeze(*self.tcx, self.param_env, DUMMY_SP)
292-
}
293-
294324
pub fn load_mir(
295325
&self,
296326
instance: ty::InstanceDef<'tcx>,
@@ -761,32 +791,4 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
761791
trace!("generate stacktrace: {:#?}, {:?}", frames, explicit_span);
762792
frames
763793
}
764-
765-
#[inline(always)]
766-
pub fn sign_extend(&self, value: u128, ty: TyLayout<'_>) -> u128 {
767-
assert!(ty.abi.is_signed());
768-
sign_extend(value, ty.size)
769-
}
770-
771-
#[inline(always)]
772-
pub fn truncate(&self, value: u128, ty: TyLayout<'_>) -> u128 {
773-
truncate(value, ty.size)
774-
}
775-
776-
#[inline(always)]
777-
pub fn force_ptr(
778-
&self,
779-
scalar: Scalar<M::PointerTag>,
780-
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
781-
self.memory.force_ptr(scalar)
782-
}
783-
784-
#[inline(always)]
785-
pub fn force_bits(
786-
&self,
787-
scalar: Scalar<M::PointerTag>,
788-
size: Size
789-
) -> InterpResult<'tcx, u128> {
790-
self.memory.force_bits(scalar, size)
791-
}
792794
}

src/librustc_mir/interpret/machine.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ 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,
13+
Allocation, AllocId, InterpResult, InterpError, Scalar, AllocationExtra,
1414
InterpretCx, PlaceTy, OpTy, ImmTy, MemoryKind, Pointer, Memory
1515
};
1616

@@ -136,8 +136,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
136136
///
137137
/// This allocation will then be fed to `tag_allocation` to initialize the "extra" state.
138138
fn find_foreign_static(
139+
tcx: TyCtxt<'tcx>,
139140
def_id: DefId,
140-
tcx: TyCtxtAt<'tcx>,
141141
) -> InterpResult<'tcx, Cow<'tcx, Allocation>>;
142142

143143
/// Called for all binary operations on integer(-like) types when one operand is a pointer
@@ -174,10 +174,10 @@ pub trait Machine<'mir, 'tcx>: Sized {
174174
/// For static allocations, the tag returned must be the same as the one returned by
175175
/// `tag_static_base_pointer`.
176176
fn tag_allocation<'b>(
177+
memory_extra: &Self::MemoryExtra,
177178
id: AllocId,
178179
alloc: Cow<'b, Allocation>,
179180
kind: Option<MemoryKind<Self::MemoryKinds>>,
180-
memory: &Memory<'mir, 'tcx, Self>,
181181
) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag);
182182

183183
/// Return the "base" tag for the given static allocation: the one that is used for direct
@@ -186,8 +186,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
186186
/// Be aware that requesting the `Allocation` for that `id` will lead to cycles
187187
/// for cyclic statics!
188188
fn tag_static_base_pointer(
189+
memory_extra: &Self::MemoryExtra,
189190
id: AllocId,
190-
memory: &Memory<'mir, 'tcx, Self>,
191191
) -> Self::PointerTag;
192192

193193
/// Executes a retagging operation
@@ -209,20 +209,22 @@ pub trait Machine<'mir, 'tcx>: Sized {
209209
extra: Self::FrameExtra,
210210
) -> InterpResult<'tcx>;
211211

212+
#[inline(always)]
212213
fn int_to_ptr(
213-
int: u64,
214214
_mem: &Memory<'mir, 'tcx, Self>,
215+
int: u64,
215216
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
216-
if int == 0 {
217-
err!(InvalidNullPointerUsage)
217+
Err((if int == 0 {
218+
InterpError::InvalidNullPointerUsage
218219
} else {
219-
err!(ReadBytesAsPointer)
220-
}
220+
InterpError::ReadBytesAsPointer
221+
}).into())
221222
}
222223

224+
#[inline(always)]
223225
fn ptr_to_int(
224-
_ptr: Pointer<Self::PointerTag>,
225226
_mem: &Memory<'mir, 'tcx, Self>,
227+
_ptr: Pointer<Self::PointerTag>,
226228
) -> InterpResult<'tcx, u64> {
227229
err!(ReadPointerAsBytes)
228230
}

src/librustc_mir/interpret/memory.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
117117

118118
#[inline]
119119
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
120-
ptr.with_tag(M::tag_static_base_pointer(ptr.alloc_id, &self))
120+
ptr.with_tag(M::tag_static_base_pointer(&self.extra, ptr.alloc_id))
121121
}
122122

123123
pub fn create_fn_alloc(&mut self, instance: Instance<'tcx>) -> Pointer<M::PointerTag> {
@@ -150,7 +150,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
150150
kind: MemoryKind<M::MemoryKinds>,
151151
) -> Pointer<M::PointerTag> {
152152
let id = self.tcx.alloc_map.lock().reserve();
153-
let (alloc, tag) = M::tag_allocation(id, Cow::Owned(alloc), Some(kind), &self);
153+
let (alloc, tag) = M::tag_allocation(&self.extra, id, Cow::Owned(alloc), Some(kind));
154154
self.alloc_map.insert(id, (kind, alloc.into_owned()));
155155
Pointer::from(id).with_tag(tag)
156156
}
@@ -365,9 +365,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
365365
/// contains a reference to memory that was created during its evaluation (i.e., not to
366366
/// another static), those inner references only exist in "resolved" form.
367367
fn get_static_alloc(
368-
id: AllocId,
368+
memory_extra: &M::MemoryExtra,
369369
tcx: TyCtxtAt<'tcx>,
370-
memory: &Memory<'mir, 'tcx, M>,
370+
id: AllocId,
371371
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
372372
let alloc = tcx.alloc_map.lock().get(id);
373373
let alloc = match alloc {
@@ -381,7 +381,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
381381
// We got a "lazy" static that has not been computed yet.
382382
if tcx.is_foreign_item(def_id) {
383383
trace!("static_alloc: foreign item {:?}", def_id);
384-
M::find_foreign_static(def_id, tcx)?
384+
M::find_foreign_static(tcx.tcx, def_id)?
385385
} else {
386386
trace!("static_alloc: Need to compute {:?}", def_id);
387387
let instance = Instance::mono(tcx.tcx, def_id);
@@ -391,7 +391,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
391391
};
392392
// use the raw query here to break validation cycles. Later uses of the static
393393
// will call the full query anyway
394-
let raw_const = tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid))
394+
let raw_const = tcx.tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid))
395395
.map_err(|err| {
396396
// no need to report anything, the const_eval call takes care of that
397397
// for statics
@@ -411,10 +411,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
411411
// We got tcx memory. Let the machine figure out whether and how to
412412
// turn that into memory with the right pointer tag.
413413
Ok(M::tag_allocation(
414+
memory_extra,
414415
id, // always use the ID we got as input, not the "hidden" one.
415416
alloc,
416417
M::STATIC_KIND.map(MemoryKind::Machine),
417-
memory
418418
).0)
419419
}
420420

@@ -427,7 +427,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
427427
// `get_static_alloc` that we can actually use directly without inserting anything anywhere.
428428
// So the error type is `InterpResult<'tcx, &Allocation<M::PointerTag>>`.
429429
let a = self.alloc_map.get_or(id, || {
430-
let alloc = Self::get_static_alloc(id, self.tcx, &self).map_err(Err)?;
430+
let alloc = Self::get_static_alloc(&self.extra, self.tcx, id).map_err(Err)?;
431431
match alloc {
432432
Cow::Borrowed(alloc) => {
433433
// We got a ref, cheaply return that as an "error" so that the
@@ -456,11 +456,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
456456
id: AllocId,
457457
) -> InterpResult<'tcx, &mut Allocation<M::PointerTag, M::AllocExtra>> {
458458
let tcx = self.tcx;
459-
let alloc = Self::get_static_alloc(id, tcx, &self);
459+
let memory_extra = &self.extra;
460460
let a = self.alloc_map.get_mut_or(id, || {
461461
// Need to make a copy, even if `get_static_alloc` is able
462462
// to give us a cheap reference.
463-
let alloc = alloc?;
463+
let alloc = Self::get_static_alloc(memory_extra, tcx, id)?;
464464
if alloc.mutability == Mutability::Immutable {
465465
return err!(ModifiedConstantMemory);
466466
}
@@ -887,7 +887,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
887887
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
888888
match scalar {
889889
Scalar::Ptr(ptr) => Ok(ptr),
890-
_ => M::int_to_ptr(scalar.to_usize(self)?, self)
890+
_ => M::int_to_ptr(&self, scalar.to_usize(self)?)
891891
}
892892
}
893893

@@ -898,7 +898,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
898898
) -> InterpResult<'tcx, u128> {
899899
match scalar.to_bits_or_ptr(size, self) {
900900
Ok(bits) => Ok(bits),
901-
Err(ptr) => Ok(M::ptr_to_int(ptr, self)? as u128)
901+
Err(ptr) => Ok(M::ptr_to_int(&self, ptr)? as u128)
902902
}
903903
}
904904
}

0 commit comments

Comments
 (0)