Browse Source

cranelift: Tiny refactor of `AliasRegion` bit packing and unpacking (#8973)

pull/8975/head
Nick Fitzgerald 4 months ago
committed by GitHub
parent
commit
f2b724262a
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 47
      cranelift/codegen/src/ir/memflags.rs

47
cranelift/codegen/src/ir/memflags.rs

@ -19,11 +19,33 @@ pub enum Endianness {
/// Which disjoint region of aliasing memory is accessed in this memory /// Which disjoint region of aliasing memory is accessed in this memory
/// operation. /// operation.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
#[repr(u8)]
#[allow(missing_docs)] #[allow(missing_docs)]
#[rustfmt::skip]
pub enum AliasRegion { pub enum AliasRegion {
Heap, // None = 0b00;
Table, Heap = 0b01,
Vmctx, Table = 0b10,
Vmctx = 0b11,
}
impl AliasRegion {
const fn from_bits(bits: u8) -> Option<Self> {
match bits {
0b00 => None,
0b01 => Some(Self::Heap),
0b10 => Some(Self::Table),
0b11 => Some(Self::Vmctx),
_ => panic!("invalid alias region bits"),
}
}
const fn to_bits(region: Option<Self>) -> u8 {
match region {
None => 0b00,
Some(r) => r as u8,
}
}
} }
/// Flags for memory operations like load/store. /// Flags for memory operations like load/store.
@ -114,27 +136,14 @@ impl MemFlags {
/// Reads the alias region that this memory operation works with. /// Reads the alias region that this memory operation works with.
pub const fn alias_region(self) -> Option<AliasRegion> { pub const fn alias_region(self) -> Option<AliasRegion> {
// NB: keep in sync with `with_alias_region` AliasRegion::from_bits(((self.bits & MASK_ALIAS_REGION) >> ALIAS_REGION_OFFSET) as u8)
match (self.bits & MASK_ALIAS_REGION) >> ALIAS_REGION_OFFSET {
0b00 => None,
0b01 => Some(AliasRegion::Heap),
0b10 => Some(AliasRegion::Table),
0b11 => Some(AliasRegion::Vmctx),
_ => unreachable!(),
}
} }
/// Sets the alias region that this works on to the specified `region`. /// Sets the alias region that this works on to the specified `region`.
pub const fn with_alias_region(mut self, region: Option<AliasRegion>) -> Self { pub const fn with_alias_region(mut self, region: Option<AliasRegion>) -> Self {
// NB: keep in sync with `alias_region` let bits = AliasRegion::to_bits(region);
let bits = match region {
None => 0b00,
Some(AliasRegion::Heap) => 0b01,
Some(AliasRegion::Table) => 0b10,
Some(AliasRegion::Vmctx) => 0b11,
};
self.bits &= !MASK_ALIAS_REGION; self.bits &= !MASK_ALIAS_REGION;
self.bits |= bits << ALIAS_REGION_OFFSET; self.bits |= (bits as u16) << ALIAS_REGION_OFFSET;
self self
} }

Loading…
Cancel
Save