Skip to content

Try inlining trivial functions used by CTFE #81441

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

Merged
merged 2 commits into from
Jan 28, 2021
Merged
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
1 change: 1 addition & 0 deletions compiler/rustc_mir/src/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl<Tag> MemPlace<Tag> {
}
}

#[inline]
pub fn offset(
self,
offset: Size,
Expand Down
20 changes: 18 additions & 2 deletions compiler/rustc_target/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,36 +441,50 @@ pub struct Align {
}

impl Align {
#[inline]
pub fn from_bits(bits: u64) -> Result<Align, String> {
Align::from_bytes(Size::from_bits(bits).bytes())
}

#[inline]
pub fn from_bytes(align: u64) -> Result<Align, String> {
// Treat an alignment of 0 bytes like 1-byte alignment.
if align == 0 {
return Ok(Align { pow2: 0 });
}

#[cold]
fn not_power_of_2(align: u64) -> String {
format!("`{}` is not a power of 2", align)
}

#[cold]
fn too_large(align: u64) -> String {
format!("`{}` is too large", align)
}

let mut bytes = align;
let mut pow2: u8 = 0;
while (bytes & 1) == 0 {
pow2 += 1;
bytes >>= 1;
}
if bytes != 1 {
return Err(format!("`{}` is not a power of 2", align));
return Err(not_power_of_2(align));
}
if pow2 > 29 {
return Err(format!("`{}` is too large", align));
return Err(too_large(align));
}

Ok(Align { pow2 })
}

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

#[inline]
pub fn bits(self) -> u64 {
self.bytes() * 8
}
Expand All @@ -479,12 +493,14 @@ impl Align {
/// (the largest power of two that the offset is a multiple of).
///
/// N.B., for an offset of `0`, this happens to return `2^64`.
#[inline]
pub fn max_for_offset(offset: Size) -> Align {
Align { pow2: offset.bytes().trailing_zeros() as u8 }
}

/// Lower the alignment, if necessary, such that the given offset
/// is aligned to it (the offset is a multiple of the alignment).
#[inline]
pub fn restrict_for_offset(self, offset: Size) -> Align {
self.min(Align::max_for_offset(offset))
}
Expand Down