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

Add abs and make Oliver's example work #702

Merged
merged 11 commits into from
Jan 21, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
abs plumbing
ajpal committed Jan 7, 2025
commit 909fa47968f7aadf4d4333da2ca25a854c29325c
4 changes: 4 additions & 0 deletions dag_in_context/src/ast.rs
Original file line number Diff line number Diff line change
@@ -86,6 +86,10 @@ macro_rules! tuplev {
}
pub use tuplev;

pub fn abs(l: RcExpr) -> RcExpr {
RcExpr::new(Expr::Uop(UnaryOp::Abs, l))
}

pub fn add(l: RcExpr, r: RcExpr) -> RcExpr {
RcExpr::new(Expr::Bop(BinaryOp::Add, l, r))
}
1 change: 1 addition & 0 deletions dag_in_context/src/from_egglog.rs
Original file line number Diff line number Diff line change
@@ -200,6 +200,7 @@ impl<'a> FromEgglog<'a> {
fn uop_from_egglog(&mut self, uop: Term) -> UnaryOp {
match_term_app!(uop.clone();
{
("Abs", []) => UnaryOp::Abs,
("Not", []) => UnaryOp::Not,
_ => panic!("Invalid unary op: {:?}", uop),
})
2 changes: 1 addition & 1 deletion dag_in_context/src/greedy_dag_extractor.rs
Original file line number Diff line number Diff line change
@@ -996,7 +996,7 @@ impl CostModel for DefaultCostModel {
"Base" | "TupleT" | "TNil" | "TCons" => 0.,
"Int" | "Bool" | "Float" => 0.,
// Algebra
"Add" | "PtrAdd" | "Sub" | "And" | "Or" | "Not" | "Shl" | "Shr" => 10.,
"Abs" | "Add" | "PtrAdd" | "Sub" | "And" | "Or" | "Not" | "Shl" | "Shr" => 10.,
"FAdd" | "FSub" | "Fmax" | "Fmin" => 50.,
"Mul" => 30.,
"FMul" => 150.,
2 changes: 2 additions & 0 deletions dag_in_context/src/interpreter.rs
Original file line number Diff line number Diff line change
@@ -334,8 +334,10 @@ impl<'a> VirtualMachine<'a> {
}

fn interpret_uop(&mut self, uop: &UnaryOp, e: &RcExpr, arg: &Value) -> Value {
let get_int = |e: &RcExpr, vm: &mut Self| vm.interp_int_expr(e, arg);
match uop {
UnaryOp::Not => Const(Constant::Bool(!self.interp_bool_expr(e, arg))),
UnaryOp::Abs => Const(Constant::Int(get_int(e, self).abs())),
}
}

1 change: 1 addition & 0 deletions dag_in_context/src/optimizations/purity_analysis.egg
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@
(BinaryOpIsPure (Or))
(BinaryOpIsPure (PtrAdd))
(UnaryOpIsPure (Not))
(UnaryOpIsPure (Abs))

(rule ((Function _name _tyin _tyout _out) (ExprIsPure _out))
((ExprIsPure (Function _name _tyin _tyout _out)))
3 changes: 2 additions & 1 deletion dag_in_context/src/pretty_print.rs
Original file line number Diff line number Diff line change
@@ -662,8 +662,9 @@ impl TernaryOp {

impl UnaryOp {
pub fn to_ast(&self) -> String {
use schema::UnaryOp::Not;
use schema::UnaryOp::{Abs, Not};
match self {
Abs => "abs".into(),
Not => "not".into(),
}
}
1 change: 1 addition & 0 deletions dag_in_context/src/schema.egg
Original file line number Diff line number Diff line change
@@ -130,6 +130,7 @@
; given a pointer and state edge, frees the whole memory region at the pointer
(Free))
(datatype UnaryOp
(Abs)
(Not))

; Operators
1 change: 1 addition & 0 deletions dag_in_context/src/schema.rs
Original file line number Diff line number Diff line change
@@ -73,6 +73,7 @@ pub enum BinaryOp {

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, PartialOrd, Ord)]
pub enum UnaryOp {
Abs,
Not,
}

2 changes: 2 additions & 0 deletions dag_in_context/src/schema_helpers.rs
Original file line number Diff line number Diff line change
@@ -100,6 +100,7 @@ impl UnaryOp {
pub(crate) fn name(&self) -> &'static str {
use UnaryOp::*;
match self {
Abs => "Abs",
Not => "Not",
}
}
@@ -861,6 +862,7 @@ impl BinaryOp {
impl UnaryOp {
pub(crate) fn types(&self) -> Option<(Type, Type)> {
match self {
UnaryOp::Abs => Some((base(intt()), base(intt()))),
UnaryOp::Not => Some((base(boolt()), base(boolt()))),
}
}
2 changes: 2 additions & 0 deletions src/rvsdg/from_dag.rs
Original file line number Diff line number Diff line change
@@ -194,12 +194,14 @@ fn effect_op_from_binary_op(bop: BinaryOp) -> Option<EffectOps> {

fn value_op_from_unary_op(uop: UnaryOp) -> Option<ValueOps> {
match uop {
UnaryOp::Abs => Some(ValueOps::Abs),
UnaryOp::Not => Some(ValueOps::Not),
}
}

fn effect_op_from_unary_op(uop: UnaryOp) -> Option<EffectOps> {
match uop {
UnaryOp::Abs => None,
UnaryOp::Not => None,
}
}
1 change: 1 addition & 0 deletions src/rvsdg/to_dag.rs
Original file line number Diff line number Diff line change
@@ -263,6 +263,7 @@ impl<'a> DagTranslator<'a> {
(ValueOps::And, [a, b]) => and(a.clone(), b.clone()),
(ValueOps::Or, [a, b]) => or(a.clone(), b.clone()),
(ValueOps::Not, [a]) => not(a.clone()),
(ValueOps::Abs, [a]) => abs(a.clone()),

(ValueOps::PtrAdd, [a, b]) => ptradd(a.clone(), b.clone()),
(ValueOps::Load, [a, b]) => load(a.clone(), b.clone()),