Skip to content
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

Handle some more opcodes. #965

Merged
merged 3 commits into from
Feb 13, 2024
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 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 @@ -127,13 +133,13 @@ pub(crate) enum Opcode {
Store,
Alloca,
Call,
GetElementPtr,
Br,
CondBr,
Icmp,
BinaryOperator,
Ret,
InsertValue,
PtrAdd,
Unimplemented = 255,
}

Expand Down Expand Up @@ -258,12 +264,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 @@ -282,6 +308,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 @@ -323,6 +351,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 @@ -381,8 +410,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 @@ -744,6 +773,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 @@ -763,6 +807,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 @@ -861,6 +909,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 @@ -1029,7 +1078,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 @@ -1129,6 +1178,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 @@ -1198,6 +1257,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 @@ -1207,7 +1267,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