Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up compilation of huge constant arrays #51672

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl<'tcx> Scalar {
bits: layout.offset(bits as u64, i.bytes())? as u128,
defined: pointer_size,
})
}
}
}
Scalar::Ptr(ptr) => ptr.offset(i, layout).map(Scalar::Ptr),
}
Expand Down
69 changes: 63 additions & 6 deletions src/librustc_mir/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,16 +586,11 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
)
}
};
let elem_size = self.layout_of(elem_ty)?.size;
let value = self.eval_operand(operand)?.value;

let (dest, dest_align) = self.force_allocation(dest)?.to_ptr_align();

// FIXME: speed up repeat filling
for i in 0..length {
let elem_dest = dest.ptr_offset(elem_size * i as u64, &self)?;
self.write_value_to_ptr(value, elem_dest, dest_align, elem_ty)?;
}
self.write_values_to_ptr(value, length, dest, dest_align, elem_ty)?;
}

Len(ref place) => {
Expand Down Expand Up @@ -1257,6 +1252,68 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
}
}

pub fn write_values_to_ptr(
&mut self,
value: Value,
length: u64,
dest: Scalar,
dest_align: Align,
dest_ty: Ty<'tcx>,
) -> EvalResult<'tcx> {
let layout = self.layout_of(dest_ty)?;
let elem_size = layout.size;
trace!("write_value_to_ptr: {:#?}, {}, {:#?}", value, dest_ty, layout);
match value {
Value::ByRef(ptr, align) => {
let align_min = align.min(layout.align);
let dest_align_min = dest_align.min(layout.align);
// FIXME: speed up repeat filling
for i in 0..length {
let elem_dest = dest.ptr_offset(elem_size * i, &self)?;
self.memory.copy(ptr, align_min, elem_dest, dest_align_min, layout.size,
false)?;
}
Ok(())
}
Value::Scalar(scalar) => {
let signed = match layout.abi {
layout::Abi::Scalar(ref scal) => match scal.value {
layout::Primitive::Int(_, signed) => signed,
_ => false,
},
_ => match scalar {
Scalar::Bits { defined: 0, .. } => false,
_ => bug!("write_value_to_ptr: invalid ByVal layout: {:#?}", layout),
}
};
// FIXME: speed up repeat filling
for i in 0..length {
let elem_dest = dest.ptr_offset(elem_size * i, &self)?;
self.memory.write_scalar(elem_dest, dest_align, scalar, layout.size, signed)?;
}
Ok(())
}
Value::ScalarPair(a_val, b_val) => {
let (a, b) = match layout.abi {
layout::Abi::ScalarPair(ref a, ref b) => (&a.value, &b.value),
_ => bug!("write_value_to_ptr: invalid ScalarPair layout: {:#?}", layout)
};
let (a_size, b_size) = (a.size(&self), b.size(&self));
let b_offset = a_size.abi_align(b.align(&self));
// FIXME: speed up repeat filling
for i in 0..length {
let elem_dest = dest.ptr_offset(elem_size * i, &self)?;
let a_ptr = elem_dest;
let b_ptr = elem_dest.ptr_offset(b_offset, &self)?.into();
// TODO: What about signedess?
self.memory.write_scalar(a_ptr, dest_align, a_val, a_size, false)?;
self.memory.write_scalar(b_ptr, dest_align, b_val, b_size, false)?;
}
Ok(())
}
}
}

fn ensure_valid_value(&self, val: Scalar, ty: Ty<'tcx>) -> EvalResult<'tcx> {
match ty.sty {
ty::TyBool => val.to_bool().map(|_| ()),
Expand Down
10 changes: 10 additions & 0 deletions src/librustc_target/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ pub trait HasDataLayout: Copy {
}

impl<'a> HasDataLayout for &'a TargetDataLayout {
#[inline]
fn data_layout(&self) -> &TargetDataLayout {
self
}
Expand Down Expand Up @@ -240,10 +241,12 @@ impl Size {
}
}

#[inline]
pub fn bytes(self) -> u64 {
self.raw
}

#[inline]
pub fn bits(self) -> u64 {
self.bytes().checked_mul(8).unwrap_or_else(|| {
panic!("Size::bits: {} bytes in bits doesn't fit in u64", self.bytes())
Expand Down Expand Up @@ -289,6 +292,7 @@ impl Size {

impl Add for Size {
type Output = Size;
#[inline]
fn add(self, other: Size) -> Size {
Size::from_bytes(self.bytes().checked_add(other.bytes()).unwrap_or_else(|| {
panic!("Size::add: {} + {} doesn't fit in u64", self.bytes(), other.bytes())
Expand All @@ -298,6 +302,7 @@ impl Add for Size {

impl Sub for Size {
type Output = Size;
#[inline]
fn sub(self, other: Size) -> Size {
Size::from_bytes(self.bytes().checked_sub(other.bytes()).unwrap_or_else(|| {
panic!("Size::sub: {} - {} would result in negative size", self.bytes(), other.bytes())
Expand All @@ -314,6 +319,7 @@ impl Mul<Size> for u64 {

impl Mul<u64> for Size {
type Output = Size;
#[inline]
fn mul(self, count: u64) -> Size {
match self.bytes().checked_mul(count) {
Some(bytes) => Size::from_bytes(bytes),
Expand Down Expand Up @@ -374,18 +380,22 @@ impl Align {
})
}

#[inline]
pub fn abi(self) -> u64 {
1 << self.abi_pow2
}

#[inline]
pub fn pref(self) -> u64 {
1 << self.pref_pow2
}

#[inline]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops!

pub fn abi_bits(self) -> u64 {
self.abi() * 8
}

#[inline]
pub fn pref_bits(self) -> u64 {
self.pref() * 8
}
Expand Down