Skip to content

Commit 769138e

Browse files
committed
try to avoid some layout_of calls
1 parent d7c3643 commit 769138e

File tree

5 files changed

+33
-34
lines changed

5 files changed

+33
-34
lines changed

src/helpers.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1013,15 +1013,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
10131013
fn float_to_int_checked<F>(
10141014
&self,
10151015
f: F,
1016-
dest_ty: Ty<'tcx>,
1016+
cast_to: TyAndLayout<'tcx>,
10171017
round: rustc_apfloat::Round,
10181018
) -> Option<ImmTy<'tcx, Provenance>>
10191019
where
10201020
F: rustc_apfloat::Float + Into<Scalar<Provenance>>,
10211021
{
10221022
let this = self.eval_context_ref();
10231023

1024-
let val = match dest_ty.kind() {
1024+
let val = match cast_to.ty.kind() {
10251025
// Unsigned
10261026
ty::Uint(t) => {
10271027
let size = Integer::from_uint_ty(this, *t).size();
@@ -1062,10 +1062,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
10621062
_ =>
10631063
span_bug!(
10641064
this.cur_span(),
1065-
"attempted float-to-int conversion with non-int output type {dest_ty:?}"
1065+
"attempted float-to-int conversion with non-int output type {}",
1066+
cast_to.ty,
10661067
),
10671068
};
1068-
Some(ImmTy::from_scalar(val, this.layout_of(dest_ty).unwrap()))
1069+
Some(ImmTy::from_scalar(val, cast_to))
10691070
}
10701071

10711072
/// Returns an integer type that is twice wide as `ty`

src/shims/intrinsics/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
368368
ty::Float(FloatTy::F32) => {
369369
let f = val.to_scalar().to_f32()?;
370370
this
371-
.float_to_int_checked(f, dest.layout.ty, Round::TowardZero)
371+
.float_to_int_checked(f, dest.layout, Round::TowardZero)
372372
.ok_or_else(|| {
373373
err_ub_format!(
374374
"`float_to_int_unchecked` intrinsic called on {f} which cannot be represented in target type `{:?}`",
@@ -379,7 +379,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
379379
ty::Float(FloatTy::F64) => {
380380
let f = val.to_scalar().to_f64()?;
381381
this
382-
.float_to_int_checked(f, dest.layout.ty, Round::TowardZero)
382+
.float_to_int_checked(f, dest.layout, Round::TowardZero)
383383
.ok_or_else(|| {
384384
err_ub_format!(
385385
"`float_to_int_unchecked` intrinsic called on {f} which cannot be represented in target type `{:?}`",

src/shims/intrinsics/simd.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -441,17 +441,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
441441
// Int-to-(int|float): always safe
442442
(ty::Int(_) | ty::Uint(_), ty::Int(_) | ty::Uint(_) | ty::Float(_))
443443
if safe_cast || unsafe_cast =>
444-
this.int_to_int_or_float(&op, dest.layout.ty)?,
444+
this.int_to_int_or_float(&op, dest.layout)?,
445445
// Float-to-float: always safe
446446
(ty::Float(_), ty::Float(_)) if safe_cast || unsafe_cast =>
447-
this.float_to_float_or_int(&op, dest.layout.ty)?,
447+
this.float_to_float_or_int(&op, dest.layout)?,
448448
// Float-to-int in safe mode
449449
(ty::Float(_), ty::Int(_) | ty::Uint(_)) if safe_cast =>
450-
this.float_to_float_or_int(&op, dest.layout.ty)?,
450+
this.float_to_float_or_int(&op, dest.layout)?,
451451
// Float-to-int in unchecked mode
452452
(ty::Float(FloatTy::F32), ty::Int(_) | ty::Uint(_)) if unsafe_cast => {
453453
let f = op.to_scalar().to_f32()?;
454-
this.float_to_int_checked(f, dest.layout.ty, Round::TowardZero)
454+
this.float_to_int_checked(f, dest.layout, Round::TowardZero)
455455
.ok_or_else(|| {
456456
err_ub_format!(
457457
"`simd_cast` intrinsic called on {f} which cannot be represented in target type `{:?}`",
@@ -462,7 +462,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
462462
}
463463
(ty::Float(FloatTy::F64), ty::Int(_) | ty::Uint(_)) if unsafe_cast => {
464464
let f = op.to_scalar().to_f64()?;
465-
this.float_to_int_checked(f, dest.layout.ty, Round::TowardZero)
465+
this.float_to_int_checked(f, dest.layout, Round::TowardZero)
466466
.ok_or_else(|| {
467467
err_ub_format!(
468468
"`simd_cast` intrinsic called on {f} which cannot be represented in target type `{:?}`",
@@ -473,12 +473,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
473473
}
474474
// Ptr-to-ptr cast
475475
(ty::RawPtr(..), ty::RawPtr(..)) if ptr_cast =>
476-
this.ptr_to_ptr(&op, dest.layout.ty)?,
476+
this.ptr_to_ptr(&op, dest.layout)?,
477477
// Ptr/Int casts
478478
(ty::RawPtr(..), ty::Int(_) | ty::Uint(_)) if expose_cast =>
479-
this.pointer_expose_address_cast(&op, dest.layout.ty)?,
479+
this.pointer_expose_address_cast(&op, dest.layout)?,
480480
(ty::Int(_) | ty::Uint(_), ty::RawPtr(..)) if from_exposed_cast =>
481-
this.pointer_from_exposed_address_cast(&op, dest.layout.ty)?,
481+
this.pointer_from_exposed_address_cast(&op, dest.layout)?,
482482
// Error otherwise
483483
_ =>
484484
throw_unsup_format!(

src/shims/x86/sse.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
173173
_ => unreachable!(),
174174
};
175175

176-
let res = this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
176+
let res = this.float_to_int_checked(op, dest.layout, rnd).unwrap_or_else(|| {
177177
// Fallback to minimum acording to SSE semantics.
178178
ImmTy::from_int(dest.layout.size.signed_int_min(), dest.layout)
179179
});
@@ -196,7 +196,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
196196

197197
let right = this.read_immediate(right)?;
198198
let dest0 = this.project_index(&dest, 0)?;
199-
let res0 = this.int_to_int_or_float(&right, dest0.layout.ty)?;
199+
let res0 = this.int_to_int_or_float(&right, dest0.layout)?;
200200
this.write_immediate(*res0, &dest0)?;
201201

202202
for i in 1..dest_len {

src/shims/x86/sse2.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
5656
let dest = this.project_index(&dest, i)?;
5757

5858
// Widen the operands to avoid overflow
59-
let twice_wide_ty = this.get_twice_wide_int_ty(left.layout.ty);
60-
let twice_wide_layout = this.layout_of(twice_wide_ty)?;
61-
let left = this.int_to_int_or_float(&left, twice_wide_ty)?;
62-
let right = this.int_to_int_or_float(&right, twice_wide_ty)?;
59+
let twice_wide = this.layout_of(this.get_twice_wide_int_ty(left.layout.ty))?;
60+
let left = this.int_to_int_or_float(&left, twice_wide)?;
61+
let right = this.int_to_int_or_float(&right, twice_wide)?;
6362

6463
// Calculate left + right + 1
6564
let added = this.wrapping_binary_op(
@@ -70,20 +69,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
7069
let added = this.wrapping_binary_op(
7170
mir::BinOp::Add,
7271
&added,
73-
&ImmTy::from_uint(1u32, twice_wide_layout),
72+
&ImmTy::from_uint(1u32, twice_wide),
7473
)?;
7574

7675
// Calculate (left + right + 1) / 2
7776
let divided = this.wrapping_binary_op(
7877
mir::BinOp::Div,
7978
&added,
80-
&ImmTy::from_uint(2u32, twice_wide_layout),
79+
&ImmTy::from_uint(2u32, twice_wide),
8180
)?;
8281

8382
// Narrow back to the original type
8483
let res = this.int_to_int_or_float(
8584
&divided,
86-
dest.layout.ty,
85+
dest.layout,
8786
)?;
8887
this.write_immediate(*res, &dest)?;
8988
}
@@ -106,10 +105,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
106105
let dest = this.project_index(&dest, i)?;
107106

108107
// Widen the operands to avoid overflow
109-
let twice_wide_ty = this.get_twice_wide_int_ty(left.layout.ty);
110-
let twice_wide_layout = this.layout_of(twice_wide_ty)?;
111-
let left = this.int_to_int_or_float(&left, twice_wide_ty)?;
112-
let right = this.int_to_int_or_float(&right, twice_wide_ty)?;
108+
let twice_wide = this.layout_of(this.get_twice_wide_int_ty(left.layout.ty))?;
109+
let left = this.int_to_int_or_float(&left, twice_wide)?;
110+
let right = this.int_to_int_or_float(&right, twice_wide)?;
113111

114112
// Multiply
115113
let multiplied = this.wrapping_binary_op(
@@ -121,13 +119,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
121119
let high = this.wrapping_binary_op(
122120
mir::BinOp::Shr,
123121
&multiplied,
124-
&ImmTy::from_uint(dest.layout.size.bits(), twice_wide_layout),
122+
&ImmTy::from_uint(dest.layout.size.bits(), twice_wide),
125123
)?;
126124

127125
// Narrow back to the original type
128126
let res = this.int_to_int_or_float(
129127
&high,
130-
dest.layout.ty,
128+
dest.layout,
131129
)?;
132130
this.write_immediate(*res, &dest)?;
133131
}
@@ -392,7 +390,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
392390
let dest = this.project_index(&dest, i)?;
393391

394392
let res =
395-
this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
393+
this.float_to_int_checked(op, dest.layout, rnd).unwrap_or_else(|| {
396394
// Fallback to minimum acording to SSE2 semantics.
397395
ImmTy::from_int(i32::MIN, this.machine.layouts.i32)
398396
});
@@ -648,7 +646,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
648646
let op = this.read_immediate(&this.project_index(&op, i)?)?;
649647
let dest = this.project_index(&dest, i)?;
650648

651-
let res = this.float_to_float_or_int(&op, dest.layout.ty)?;
649+
let res = this.float_to_float_or_int(&op, dest.layout)?;
652650
this.write_immediate(*res, &dest)?;
653651
}
654652
// For f32 -> f64, ignore the remaining
@@ -685,7 +683,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
685683
let dest = this.project_index(&dest, i)?;
686684

687685
let res =
688-
this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
686+
this.float_to_int_checked(op, dest.layout, rnd).unwrap_or_else(|| {
689687
// Fallback to minimum acording to SSE2 semantics.
690688
ImmTy::from_int(i32::MIN, this.machine.layouts.i32)
691689
});
@@ -716,7 +714,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
716714
_ => unreachable!(),
717715
};
718716

719-
let res = this.float_to_int_checked(op, dest.layout.ty, rnd).unwrap_or_else(|| {
717+
let res = this.float_to_int_checked(op, dest.layout, rnd).unwrap_or_else(|| {
720718
// Fallback to minimum acording to SSE semantics.
721719
ImmTy::from_int(dest.layout.size.signed_int_min(), dest.layout)
722720
});
@@ -741,7 +739,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
741739
let dest0 = this.project_index(&dest, 0)?;
742740
// `float_to_float_or_int` here will convert from f64 to f32 (cvtsd2ss) or
743741
// from f32 to f64 (cvtss2sd).
744-
let res0 = this.float_to_float_or_int(&right0, dest0.layout.ty)?;
742+
let res0 = this.float_to_float_or_int(&right0, dest0.layout)?;
745743
this.write_immediate(*res0, &dest0)?;
746744

747745
// Copy remianing from `left`

0 commit comments

Comments
 (0)