Skip to content

Commit

Permalink
Merge pull request #965 from ptersilie/more-bytecode2
Browse files Browse the repository at this point in the history
Handle some more opcodes.
  • Loading branch information
ltratt authored Feb 13, 2024
2 parents c1b76a8 + 6b8b1bb commit a3d1db3
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 15 deletions.
1 change: 1 addition & 0 deletions tests/ir_lowering/empty.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
; # IR format version: 0
; # Num funcs: 1
; # Num consts: 0
; # Num globals: 0
; # Num types: 2
;
; func main() {
Expand Down
2 changes: 1 addition & 1 deletion ykllvm
70 changes: 65 additions & 5 deletions ykrt/src/compile/jitc_yk/aot_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ index!(InstrIdx);
pub(crate) struct ConstIdx(usize);
index!(ConstIdx);

/// A global variable index.
#[deku_derive(DekuRead)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(crate) struct GlobalIdx(usize);
index!(GlobalIdx);

/// A function argument index.
#[deku_derive(DekuRead)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -128,13 +134,13 @@ pub(crate) enum Opcode {
Store,
Alloca,
Call,
GetElementPtr,
Br,
CondBr,
Icmp,
BinaryOperator,
Ret,
InsertValue,
PtrAdd,
Unimplemented = 255,
}

Expand Down Expand Up @@ -259,12 +265,32 @@ impl IRDisplay for ArgOperand {
}
}

/// An operand that describes a global variable.
#[deku_derive(DekuRead)]
#[derive(Debug)]
pub(crate) struct GlobalOperand {
global_idx: GlobalIdx,
}

impl GlobalOperand {
pub(crate) fn index(&self) -> GlobalIdx {
self.global_idx
}
}

impl IRDisplay for GlobalOperand {
fn to_str(&self, m: &Module) -> String {
m.globals[self.global_idx].to_str(m)
}
}

const OPKIND_CONST: u8 = 0;
const OPKIND_LOCAL_VARIABLE: u8 = 1;
const OPKIND_TYPE: u8 = 2;
const OPKIND_FUNCTION: u8 = 3;
const OPKIND_BLOCK: u8 = 4;
const OPKIND_ARG: u8 = 5;
const OPKIND_GLOBAL: u8 = 6;
const OPKIND_UNIMPLEMENTED: u8 = 255;

#[deku_derive(DekuRead)]
Expand All @@ -283,6 +309,8 @@ pub(crate) enum Operand {
Block(BlockOperand),
#[deku(id = "OPKIND_ARG")]
Arg(ArgOperand),
#[deku(id = "OPKIND_GLOBAL")]
Global(GlobalOperand),
#[deku(id = "OPKIND_UNIMPLEMENTED")]
Unimplemented(#[deku(until = "|v: &u8| *v == 0", map = "deserialise_string")] String),
}
Expand Down Expand Up @@ -324,6 +352,7 @@ impl IRDisplay for Operand {
Self::Type(t) => m.types[t.type_idx].to_str(m),
Self::Function(f) => m.funcs[f.func_idx].name.to_owned(),
Self::Block(bb) => bb.to_str(m),
Self::Global(g) => g.to_str(m),
Self::Arg(a) => a.to_str(m),
Self::Unimplemented(s) => format!("?op<{}>", s),
}
Expand Down Expand Up @@ -382,8 +411,8 @@ impl Instruction {
self.opcode == Opcode::Store
}

pub(crate) fn is_gep(&self) -> bool {
self.opcode == Opcode::GetElementPtr
pub(crate) fn is_ptr_add(&self) -> bool {
self.opcode == Opcode::PtrAdd
}

pub(crate) fn is_control_point(&self, aot_mod: &Module) -> bool {
Expand Down Expand Up @@ -745,6 +774,21 @@ impl IRDisplay for Constant {
}
}

/// A constant.
#[deku_derive(DekuRead)]
#[derive(Debug)]
pub(crate) struct Global {
is_threadlocal: bool,
#[deku(until = "|v: &u8| *v == 0", map = "deserialise_string")]
name: String,
}

impl IRDisplay for Global {
fn to_str(&self, _m: &Module) -> String {
format!("Global({}, tls={})", self.name, self.is_threadlocal)
}
}

/// A bytecode module.
///
/// This is the top-level container for the bytecode.
Expand All @@ -764,6 +808,10 @@ pub(crate) struct Module {
#[deku(count = "num_consts", map = "deserialise_into_ti_vec")]
consts: TiVec<ConstIdx, Constant>,
#[deku(temp)]
num_globals: usize,
#[deku(count = "num_globals", map = "deserialise_into_ti_vec")]
globals: TiVec<GlobalIdx, Global>,
#[deku(temp)]
num_types: usize,
#[deku(count = "num_types", map = "deserialise_into_ti_vec")]
types: TiVec<TypeIdx, Type>,
Expand Down Expand Up @@ -862,6 +910,7 @@ impl Module {
ret.push_str(&format!("# IR format version: {}\n", self.version));
ret.push_str(&format!("# Num funcs: {}\n", self.funcs.len()));
ret.push_str(&format!("# Num consts: {}\n", self.consts.len()));
ret.push_str(&format!("# Num globals: {}\n", self.globals.len()));
ret.push_str(&format!("# Num types: {}\n", self.types.len()));

for func in &self.funcs {
Expand Down Expand Up @@ -1030,7 +1079,7 @@ mod tests {
// type_idx:
write_native_usize(&mut data, 2);
// opcode:
data.write_u8(Opcode::GetElementPtr as u8).unwrap();
data.write_u8(Opcode::PtrAdd as u8).unwrap();
// num_operands:
data.write_u32::<NativeEndian>(1).unwrap();
// OPERAND 0
Expand Down Expand Up @@ -1130,6 +1179,16 @@ mod tests {
// bytes:
data.write_u32::<NativeEndian>(50).unwrap();

// GLOBALS
// num_globals:
write_native_usize(&mut data, 1);

// GLOBAL 1
// is_threadlocal:
let _ = data.write_u8(0);
// name:
write_str(&mut data, "aaa");

// TYPES
// num_types:
write_native_usize(&mut data, 7);
Expand Down Expand Up @@ -1199,6 +1258,7 @@ mod tests {
# IR format version: 0
# Num funcs: 2
# Num consts: 3
# Num globals: 1
# Num types: 7
func foo($arg0: ptr, $arg1: i32) -> i32 {
Expand All @@ -1208,7 +1268,7 @@ func foo($arg0: ptr, $arg1: i32) -> i32 {
condbr $0_0, bb0, bb1
bb1:
?inst<%3 = some_llvm_instruction ...>
$1_1: ptr = getelementptr -1i32
$1_1: ptr = ptradd -1i32
$1_2: ptr = alloca i32, 50i32
$1_3: ptr = call bar(50i32, 50i32)
br
Expand Down
Loading

0 comments on commit a3d1db3

Please sign in to comment.