Skip to content

Commit

Permalink
Merge pull request #1556 from ltratt/aot_bit_byte_w
Browse files Browse the repository at this point in the history
Minor AOT renamings for consistency with the JIT IR
  • Loading branch information
vext01 authored Jan 22, 2025
2 parents a70179c + 2466b53 commit bfc0ca4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
42 changes: 18 additions & 24 deletions ykrt/src/compile/jitc_yk/aot_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1591,46 +1591,40 @@ pub(crate) fn const_int_bytes_to_string(num_bits: u32, bytes: &[u8]) -> String {
#[deku_derive(DekuRead)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct IntegerTy {
pub(crate) num_bits: u32,
bitw: u32,
}

impl IntegerTy {
/// Create a new integer type with the specified number of bits.
#[cfg(test)]
pub(crate) fn new(num_bits: u32) -> Self {
debug_assert!(num_bits > 0 && num_bits <= 0x800000);
Self { num_bits }
pub(crate) fn new(bitw: u32) -> Self {
debug_assert!(bitw > 0 && bitw <= 0x800000);
Self { bitw }
}

/// Return the number of bits (1..2^23 (inc.)) this integer spans.
pub(crate) fn num_bits(&self) -> u32 {
debug_assert!(self.num_bits > 0 && self.num_bits <= 0x800000);
self.num_bits
pub(crate) fn bitw(&self) -> u32 {
debug_assert!(self.bitw > 0 && self.bitw <= 0x800000);
self.bitw
}

/// Return the number of bytes required to store this integer type.
///
/// Padding for alignment is not included.
#[cfg(test)]
pub(crate) fn byte_size(&self) -> usize {
let bits = self.num_bits();
let mut ret = bits / 8;
// If it wasn't an exactly byte-sized thing, round up to the next byte.
if bits % 8 != 0 {
ret += 1;
}
usize::try_from(ret).unwrap()
pub(crate) fn bytew(&self) -> usize {
usize::try_from(self.bitw().div_ceil(8)).unwrap()
}

/// Format a constant integer value that is of the type described by `self`.
fn const_to_string(&self, c: &ConstVal) -> String {
const_int_bytes_to_string(self.num_bits, c.bytes())
const_int_bytes_to_string(self.bitw, c.bytes())
}
}

impl Display for IntegerTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "i{}", self.num_bits)
write!(f, "i{}", self.bitw)
}
}

Expand Down Expand Up @@ -2022,7 +2016,7 @@ mod tests {
let bytes = ToBytes::to_ne_bytes(&num).as_ref().to_vec();

// Construct an IR constant and check it stringifies ok.
let it = IntegerTy { num_bits };
let it = IntegerTy { bitw: num_bits };
let c = ConstVal {
tyidx: TyIdx::new(0),
bytes,
Expand Down Expand Up @@ -2110,22 +2104,22 @@ mod tests {
#[test]
fn integer_type_sizes() {
for i in 1..8 {
assert_eq!(IntegerTy::new(i).byte_size(), 1);
assert_eq!(IntegerTy::new(i).bytew(), 1);
}
for i in 9..16 {
assert_eq!(IntegerTy::new(i).byte_size(), 2);
assert_eq!(IntegerTy::new(i).bytew(), 2);
}
assert_eq!(IntegerTy::new(127).byte_size(), 16);
assert_eq!(IntegerTy::new(128).byte_size(), 16);
assert_eq!(IntegerTy::new(129).byte_size(), 17);
assert_eq!(IntegerTy::new(127).bytew(), 16);
assert_eq!(IntegerTy::new(128).bytew(), 16);
assert_eq!(IntegerTy::new(129).bytew(), 17);
}

#[test]
fn stringify_func_types() {
let mut m = Module::default();

let i8_tyidx = TyIdx::new(m.types.len());
m.types.push(Ty::Integer(IntegerTy { num_bits: 8 }));
m.types.push(Ty::Integer(IntegerTy { bitw: 8 }));
let void_tyidx = TyIdx::new(m.types.len());
m.types.push(Ty::Void);

Expand Down
14 changes: 7 additions & 7 deletions ykrt/src/compile/jitc_yk/trace_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<Register: Send + Sync + 'static> TraceBuilder<Register> {
} = inst
{
let width_bits = match self.aot_mod.type_(*tyidx) {
aot_ir::Ty::Integer(it) => it.num_bits(),
aot_ir::Ty::Integer(x) => x.bitw(),
_ => unreachable!(),
};
let width_bytes = usize::try_from(width_bits.div_ceil(8)).unwrap();
Expand Down Expand Up @@ -347,10 +347,10 @@ impl<Register: Send + Sync + 'static> TraceBuilder<Register> {
let aot_const = aot_const.unwrap_val();
let bytes = aot_const.bytes();
match self.aot_mod.type_(aot_const.tyidx()) {
aot_ir::Ty::Integer(aot_ir::IntegerTy { num_bits }) => {
aot_ir::Ty::Integer(x) => {
// FIXME: It would be better if the AOT IR had converted these integers in advance
// rather than doing this dance here.
let x = match num_bits {
let v = match x.bitw() {
1 | 8 => {
debug_assert_eq!(bytes.len(), 1);
u64::from(bytes[0])
Expand All @@ -370,10 +370,10 @@ impl<Register: Send + Sync + 'static> TraceBuilder<Register> {
bytes[7],
])
}
_ => todo!("{}", num_bits),
_ => todo!("{}", x.bitw()),
};
let jit_tyidx = self.jit_mod.insert_ty(jit_ir::Ty::Integer(*num_bits))?;
Ok(jit_ir::Const::Int(jit_tyidx, x))
let jit_tyidx = self.jit_mod.insert_ty(jit_ir::Ty::Integer(x.bitw()))?;
Ok(jit_ir::Const::Int(jit_tyidx, v))
}
aot_ir::Ty::Float(fty) => {
let jit_tyidx = self.jit_mod.insert_ty(jit_ir::Ty::Float(fty.clone()))?;
Expand Down Expand Up @@ -422,7 +422,7 @@ impl<Register: Send + Sync + 'static> TraceBuilder<Register> {
fn handle_type(&mut self, aot_type: &aot_ir::Ty) -> Result<jit_ir::TyIdx, CompilationError> {
let jit_ty = match aot_type {
aot_ir::Ty::Void => jit_ir::Ty::Void,
aot_ir::Ty::Integer(it) => jit_ir::Ty::Integer(it.num_bits()),
aot_ir::Ty::Integer(x) => jit_ir::Ty::Integer(x.bitw()),
aot_ir::Ty::Ptr => jit_ir::Ty::Ptr,
aot_ir::Ty::Func(ft) => {
let mut jit_args = Vec::new();
Expand Down

0 comments on commit bfc0ca4

Please sign in to comment.