-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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 large constant arrays #51833
Changes from 5 commits
202aea5
63ab0cb
429bc8d
1ffa99d
8f969ed
c431f3f
84fe0c4
faef6a3
46512e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -594,6 +594,19 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { | |
dest_align: Align, | ||
size: Size, | ||
nonoverlapping: bool, | ||
) -> EvalResult<'tcx> { | ||
self.copy_repeatedly(src, src_align, dest, dest_align, size, 1, nonoverlapping) | ||
} | ||
|
||
pub fn copy_repeatedly( | ||
&mut self, | ||
src: Scalar, | ||
src_align: Align, | ||
dest: Scalar, | ||
dest_align: Align, | ||
size: Size, | ||
length: u64, | ||
nonoverlapping: bool, | ||
) -> EvalResult<'tcx> { | ||
// Empty accesses don't need to be valid pointers, but they should still be aligned | ||
self.check_align(src, src_align)?; | ||
|
@@ -617,7 +630,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { | |
.collect(); | ||
|
||
let src_bytes = self.get_bytes_unchecked(src, size, src_align)?.as_ptr(); | ||
let dest_bytes = self.get_bytes_mut(dest, size, dest_align)?.as_mut_ptr(); | ||
let dest_bytes = self.get_bytes_mut(dest, size * length, dest_align)?.as_mut_ptr(); | ||
|
||
// SAFE: The above indexing would have panicked if there weren't at least `size` bytes | ||
// behind `src` and `dest`. Also, we use the overlapping-safe `ptr::copy` if `src` and | ||
|
@@ -634,13 +647,18 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { | |
)); | ||
} | ||
} | ||
ptr::copy(src_bytes, dest_bytes, size.bytes() as usize); | ||
|
||
for i in 0..length { | ||
ptr::copy(src_bytes, dest_bytes.offset((size.bytes() * i) as isize), size.bytes() as usize); | ||
} | ||
} else { | ||
ptr::copy_nonoverlapping(src_bytes, dest_bytes, size.bytes() as usize); | ||
for i in 0..length { | ||
ptr::copy_nonoverlapping(src_bytes, dest_bytes.offset((size.bytes() * i) as isize), size.bytes() as usize); | ||
} | ||
} | ||
} | ||
|
||
self.copy_undef_mask(src, dest, size)?; | ||
self.copy_undef_mask(src, dest, size * length)?; | ||
// copy back the relocations | ||
self.get_mut(dest.alloc_id)?.relocations.insert_presorted(relocations); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need to reapeat this, too (and offset the indices). Try a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, thanks! Can you double check my math? |
||
|
||
|
@@ -864,18 +882,18 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { | |
) -> EvalResult<'tcx> { | ||
// The bits have to be saved locally before writing to dest in case src and dest overlap. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment makes me think that we should not do this commit, otherwise we'll run into trouble in the future (and in miri right now). Can you do an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. I thought I preserved the existing behavior by cloning the source allocation's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh right. sorry. I misread the code. I still think the code isn't doing the right thing. It's only copying once, when it should be copying N-1 times. You can try this out by creating an array of types with padding, everything starting at the third element will probably not have undef masks for the padding. (you'll need unions to get the bits and then attempt to use them for an array length to actually get a compiler error from that) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm afraid I'm not quite following. We do call this function with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, you are using the length, but that just means that the entire array is copied from I'll make a regression test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fairly certain that the following test will succeed to compile on your PR: http://play.rust-lang.org/?gist=1d0183fcfb65164d1ca58ccd9614c33c |
||
assert_eq!(size.bytes() as usize as u64, size.bytes()); | ||
let mut v = Vec::with_capacity(size.bytes() as usize); | ||
|
||
let undef_mask = self.get(src.alloc_id)?.undef_mask.clone(); | ||
let dest_allocation = self.get_mut(dest.alloc_id)?; | ||
|
||
for i in 0..size.bytes() { | ||
let defined = self.get(src.alloc_id)?.undef_mask.get(src.offset + Size::from_bytes(i)); | ||
v.push(defined); | ||
} | ||
for (i, defined) in v.into_iter().enumerate() { | ||
self.get_mut(dest.alloc_id)?.undef_mask.set( | ||
dest.offset + | ||
Size::from_bytes(i as u64), | ||
defined, | ||
let defined = undef_mask.get(src.offset + Size::from_bytes(i)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you pass a repeat counter to the function, you should be able to just modulo the |
||
dest_allocation.undef_mask.set( | ||
dest.offset + Size::from_bytes(i), | ||
defined | ||
); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this results in the correct result, it does
n^2/2
copies instead ofn
copies. Inside the function itself we should probably move theself.get(src.alloc_id)?
out of the loops, too. We can probably improve the nonoverlapping case enormously, too by not requiring an intermediate allocation.