Skip to content

Commit

Permalink
Make a start on the JIT IR and the trace builder.
Browse files Browse the repository at this point in the history
The JIT IR is designed to be small. There are two kinds of instruction:
 - short instructions, with inlined operands.
 - long ones (unimplemented as of yet)

Ideally, function, block and instruction IDs (which are indices read
from the on-disk AOT IR) would be either converted to references as we
decode, but this would require changes to the decoder. We may have to
ditch deku:

sharksforarms/deku#383

Co-authored-by: Lukas Diekmann <[email protected]>
  • Loading branch information
vext01 and ptersilie committed Dec 19, 2023
1 parent f348b1a commit f15a712
Show file tree
Hide file tree
Showing 5 changed files with 632 additions and 27 deletions.
1 change: 1 addition & 0 deletions ykrt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ykaddr = { path = "../ykaddr" }
yksmp = { path = "../yksmp" }
strum = { version = "0.25", features = ["derive"] }
yktracec = { path = "../yktracec" }
strum_macros = "0.25.3"

[dependencies.llvm-sys]
# note: using a git version to get llvm linkage features in llvm-sys (not in a
Expand Down
143 changes: 131 additions & 12 deletions ykrt/src/compile/jitc_yk/aot_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@

use byteorder::{NativeEndian, ReadBytesExt};
use deku::prelude::*;
use std::{cell::RefCell, error::Error, ffi::CStr, fs, io::Cursor, path::PathBuf};
use std::{
cell::RefCell,
error::Error,
ffi::CStr,
fs,
io::Cursor,
ops::{Deref, DerefMut},
path::PathBuf,
};

/// A magic number that all bytecode payloads begin with.
const MAGIC: u32 = 0xedd5f00d;
Expand Down Expand Up @@ -45,7 +53,7 @@ pub(crate) trait IRDisplay {

/// An instruction opcode.
#[deku_derive(DekuRead)]
#[derive(Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[deku(type = "u8")]
pub(crate) enum Opcode {
Nop = 0,
Expand Down Expand Up @@ -82,14 +90,53 @@ impl IRDisplay for ConstantOperand {
}

#[deku_derive(DekuRead)]
#[derive(Debug)]
pub(crate) struct LocalVariableOperand {
#[derive(Debug, Hash, Eq, PartialEq)]
pub(crate) struct InstructionID {
#[deku(skip)] // computed after deserialisation.
func_idx: usize,
bb_idx: usize,
inst_idx: usize,
}

impl InstructionID {
pub(crate) fn new(func_idx: usize, bb_idx: usize, inst_idx: usize) -> Self {
Self {
func_idx,
bb_idx,
inst_idx,
}
}
}

#[derive(Debug)]
pub(crate) struct BlockID {
pub(crate) func_idx: usize,
pub(crate) bb_idx: usize,
}

impl BlockID {
pub(crate) fn new(func_idx: usize, bb_idx: usize) -> Self {
Self { func_idx, bb_idx }
}
}

#[deku_derive(DekuRead)]
#[derive(Debug, Hash, Eq, PartialEq)]
pub(crate) struct LocalVariableOperand(InstructionID);

impl Deref for LocalVariableOperand {
type Target = InstructionID;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for LocalVariableOperand {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl IRDisplay for LocalVariableOperand {
fn to_str(&self, _m: &Module) -> String {
format!("${}_{}", self.bb_idx, self.inst_idx,)
Expand Down Expand Up @@ -161,6 +208,30 @@ pub(crate) enum Operand {
Unimplemented(#[deku(until = "|v: &u8| *v == 0", map = "deserialise_string")] String),
}

impl Operand {
/// If the operand is a `LocalVariable`, then return the instruction that defines it, or else
/// None.
///
/// OPT: This is expensive.
pub(crate) fn to_instr<'a>(&self, aotmod: &'a Module) -> Option<&'a Instruction> {
match self {
Self::LocalVariable(lvo) => {
Some(&aotmod.funcs[lvo.func_idx].blocks[lvo.bb_idx].instrs[lvo.inst_idx])
}
_ => None,
}
}

/// Return the `InstructionID` of a local variable operand. Must not be called on other
/// operands.
pub(crate) fn to_instr_id(&self) -> InstructionID {
match self {
Self::LocalVariable(lvo) => InstructionID::new(lvo.func_idx, lvo.bb_idx, lvo.inst_idx),
_ => unreachable!(),
}
}
}

impl IRDisplay for Operand {
fn to_str(&self, m: &Module) -> String {
match self {
Expand Down Expand Up @@ -190,6 +261,43 @@ pub(crate) struct Instruction {
name: RefCell<Option<String>>,
}

impl PartialEq for Instruction {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other)
}
}

impl Instruction {
pub(crate) fn get_operand(&self, index: usize) -> &Operand {
self.operands.get(index).unwrap()
}

pub(crate) fn opcode(&self) -> Opcode {
self.opcode
}

pub(crate) fn is_store(&self) -> bool {
self.opcode == Opcode::Store
}

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

pub(crate) fn is_control_point(&self, aot_mod: &Module) -> bool {
if self.opcode == Opcode::Call {
let op = &self.operands[0];
match op {
Operand::Function(fop) => {
return aot_mod.funcs[fop.func_idx].name == "__ykrt_control_point";
}
_ => todo!(),
}
}
false
}
}

impl IRDisplay for Instruction {
fn to_str(&self, m: &Module) -> String {
if self.opcode == Opcode::Unimplemented {
Expand Down Expand Up @@ -249,8 +357,9 @@ impl IRDisplay for Instruction {
pub(crate) struct Block {
#[deku(temp)]
num_instrs: usize,
// FIXME: unpub
#[deku(count = "num_instrs")]
instrs: Vec<Instruction>,
pub instrs: Vec<Instruction>,
}

impl IRDisplay for Block {
Expand Down Expand Up @@ -546,7 +655,21 @@ impl Module {
*self.var_names_computed.borrow_mut() = true;
}

/// Fill in the function index of local variable operands of instructions.o
pub(crate) fn func_index(&self, find_func: &str) -> Option<usize> {
// OPT: create a cache in the Module.
for (f_idx, f) in self.funcs.iter().enumerate() {
if f.name == find_func {
return Some(f_idx);
}
}
None
}

pub(crate) fn block(&self, bid: &BlockID) -> Option<&Block> {
self.funcs.get(bid.func_idx)?.block(bid.bb_idx)
}

/// Fill in the function index of local variable operands of instructions.
///
/// FIXME: It may be possible to do this as we deserialise, instead of after the fact:
/// https://github.com/sharksforarms/deku/issues/363
Expand All @@ -571,16 +694,12 @@ impl Module {
&self.types[instr.type_index]
}

// FIXME: rename this to `is_def()`, which we've decided is a beter name.
// FIXME: also move this to the `Instruction` type.
fn instr_generates_value(&self, i: &Instruction) -> bool {
self.instr_type(i) != &Type::Void
}

/// Retrieve the named function from the AOT module.
pub(crate) fn func_by_name(&self, name: &str) -> Option<&Function> {
// OPT: Cache function indices somewhere for faster lookup.
self.funcs.iter().find(|f| f.name == name)
}

pub(crate) fn to_str(&self) -> String {
let mut ret = String::new();
ret.push_str(&format!("# IR format version: {}\n", self.version));
Expand Down
Loading

0 comments on commit f15a712

Please sign in to comment.