Skip to content

Commit

Permalink
big binop rea
Browse files Browse the repository at this point in the history
  • Loading branch information
oflatt committed Jan 30, 2024
1 parent 1f99e1d commit 29522ce
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 149 deletions.
8 changes: 4 additions & 4 deletions src/rvsdg/tree_unique/to_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use bril_rs::{Literal, ValueOps};
use hashbrown::HashMap;
use tree_optimizer::{
ast::{
add, arg, concat, function, get, getarg, lessthan, num, parallel, parallel_vec, print,
program_vec, tfalse, tlet, tloop, ttrue,
add, arg, concat, function, get, getarg, lessthan, num, parallel, parallel_vec,
program_vec, tfalse, tlet, tloop, tprint, ttrue,
},
expr::{Expr, TreeType},
};
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'a> RegionTranslator<'a> {
// the print buffer value.
let _arg2 = self.translate_operand(args[1]);
// print outputs a new unit value
let expr = print(arg1);
let expr = tprint(arg1);
self.add_binding(expr, id)
}
}
Expand Down Expand Up @@ -308,7 +308,7 @@ fn translate_loop() {
)
),
cbind(
print(get(getarg(2), 1)), // [(), 0, [() i]]
tprint(get(getarg(2), 1)), // [(), 0, [() i]]
parallel!(getarg(3))
)
),
Expand Down
18 changes: 10 additions & 8 deletions tree_optimizer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::{
expr::Expr::*,
expr::Id::Unique,
expr::Order,
expr::PureBinOp::*,
expr::PureUnaryOp::*,
expr::{Id::Shared, TreeType},
};

Expand Down Expand Up @@ -105,31 +107,31 @@ pub fn tfalse() -> Expr {
}

pub fn add(a: Expr, b: Expr) -> Expr {
Add(Box::new(a), Box::new(b))
Expr::BinOp(Add, Box::new(a), Box::new(b))
}

pub fn sub(a: Expr, b: Expr) -> Expr {
Sub(Box::new(a), Box::new(b))
BinOp(Sub, Box::new(a), Box::new(b))
}

pub fn mul(a: Expr, b: Expr) -> Expr {
Mul(Box::new(a), Box::new(b))
BinOp(Mul, Box::new(a), Box::new(b))
}

pub fn lessthan(a: Expr, b: Expr) -> Expr {
LessThan(Box::new(a), Box::new(b))
BinOp(LessThan, Box::new(a), Box::new(b))
}

pub fn and(a: Expr, b: Expr) -> Expr {
And(Box::new(a), Box::new(b))
BinOp(And, Box::new(a), Box::new(b))
}

pub fn or(a: Expr, b: Expr) -> Expr {
Or(Box::new(a), Box::new(b))
BinOp(Or, Box::new(a), Box::new(b))
}

pub fn not(a: Expr) -> Expr {
Not(Box::new(a))
UnaryOp(Not, Box::new(a))
}

pub fn getarg(i: usize) -> Expr {
Expand All @@ -144,7 +146,7 @@ pub fn concat(a: Expr, b: Expr) -> Expr {
Concat(Box::new(a), Box::new(b))
}

pub fn print(a: Expr) -> Expr {
pub fn tprint(a: Expr) -> Expr {
Print(Box::new(a))
}

Expand Down
113 changes: 93 additions & 20 deletions tree_optimizer/src/expr.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,94 @@
use bril_rs::Type;
use strum_macros::{Display, EnumIter};

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Default)]
pub enum Order {
Parallel,
#[default]
Sequential,
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Default)]
pub enum Id {
Unique(i64),
#[default]
Shared,
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, EnumIter, Default, Display)]
pub enum PureBinOp {
#[default]
Add,
Sub,
Mul,
LessThan,
And,
Or,
}

impl PureBinOp {
pub fn from_str(s: &str) -> Option<Self> {
match s {
"Add" => Some(PureBinOp::Add),
"Sub" => Some(PureBinOp::Sub),
"Mul" => Some(PureBinOp::Mul),
"LessThan" => Some(PureBinOp::LessThan),
"And" => Some(PureBinOp::And),
"Or" => Some(PureBinOp::Or),
_ => None,
}
}

pub fn input_types(&self) -> (Type, Type) {
match self {
PureBinOp::Add | PureBinOp::Sub | PureBinOp::Mul | PureBinOp::LessThan => {
(Type::Int, Type::Int)
}
PureBinOp::And | PureBinOp::Or => (Type::Bool, Type::Bool),
}
}

pub fn output_type(&self) -> Type {
match self {
PureBinOp::Add | PureBinOp::Sub | PureBinOp::Mul => Type::Int,
PureBinOp::LessThan | PureBinOp::And | PureBinOp::Or => Type::Bool,
}
}
}

#[derive(Clone, Debug, PartialEq, EnumIter, Default, Display)]
pub enum PureUnaryOp {
#[default]
Not,
}

impl PureUnaryOp {
pub fn from_str(s: &str) -> Option<Self> {
match s {
"Not" => Some(PureUnaryOp::Not),
_ => None,
}
}

pub fn input_type(&self) -> Type {
match self {
PureUnaryOp::Not => Type::Bool,
}
}

pub fn output_type(&self) -> Type {
match self {
PureUnaryOp::Not => Type::Bool,
}
}
}

#[derive(Clone, Debug, PartialEq, EnumIter)]
pub enum Expr {
Num(i64),
Boolean(bool),
Add(Box<Expr>, Box<Expr>),
Sub(Box<Expr>, Box<Expr>),
Mul(Box<Expr>, Box<Expr>),
LessThan(Box<Expr>, Box<Expr>),
And(Box<Expr>, Box<Expr>),
Or(Box<Expr>, Box<Expr>),
Not(Box<Expr>),
BinOp(PureBinOp, Box<Expr>, Box<Expr>),
UnaryOp(PureUnaryOp, Box<Expr>),
Get(Box<Expr>, usize),
/// Concat is a convenient built-in way
/// to put two tuples together.
Expand All @@ -49,23 +115,29 @@ pub enum Expr {
Call(Id, Box<Expr>),
}

impl Default for Expr {
fn default() -> Self {
Expr::Num(0)
}
}

impl Expr {
/// Runs `func` on every child of this expression.
pub fn for_each_child(&mut self, mut func: impl FnMut(&mut Expr)) {
match self {
Expr::Num(_) | Expr::Boolean(_) | Expr::Arg(_) => {}
Expr::Add(a, b)
| Expr::Sub(a, b)
| Expr::Mul(a, b)
| Expr::LessThan(a, b)
| Expr::And(a, b)
| Expr::Or(a, b)
| Expr::Concat(a, b)
| Expr::Write(a, b) => {
Expr::BinOp(_, a, b) => {
func(a);
func(b);
}
Expr::UnaryOp(_, a) => {
func(a);
}
Expr::Concat(a, b) | Expr::Write(a, b) => {
func(a);
func(b);
}
Expr::Not(a) | Expr::Print(a) | Expr::Read(a) => {
Expr::Print(a) | Expr::Read(a) => {
func(a);
}
Expr::Get(a, _) | Expr::Function(_, _, _, _, a) | Expr::Call(_, a) => {
Expand Down Expand Up @@ -105,8 +177,9 @@ pub enum Value {
Tuple(Vec<Value>),
}

#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Debug, Default)]
pub enum TreeType {
#[default]
Unit,
Bril(Type),
Tuple(Vec<TreeType>),
Expand Down
Loading

0 comments on commit 29522ce

Please sign in to comment.