-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #713 from egraphs-good/ajpal-push-in
Conditional Push In
- Loading branch information
Showing
13 changed files
with
581 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
dag_in_context/src/optimizations/conditional_push_in.egg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
(ruleset push-in) | ||
|
||
; new version of the rule where one side of bop is constant | ||
(rule ( | ||
(= if_e (If pred orig_inputs thn els)) | ||
(ContextOf if_e outer_ctx) | ||
(= (Bop o (Const c ty outer_ctx) x) (Get orig_inputs i)) | ||
(HasArgType thn (TupleT tylist)) | ||
(HasArgType els (TupleT tylist)) | ||
(HasType x (Base x_ty)) | ||
) | ||
( | ||
; New inputs | ||
(let new_ins (Concat orig_inputs (Single x))) | ||
(let new_ins_ty (TupleT (TLConcat tylist (TCons x_ty (TNil))))) | ||
|
||
; New contexts | ||
(let if_tr (InIf true pred new_ins)) | ||
(let if_fa (InIf false pred new_ins)) | ||
|
||
; New args | ||
(let arg_tr (Arg new_ins_ty if_tr)) | ||
(let arg_fa (Arg new_ins_ty if_fa)) | ||
|
||
; SubTuple | ||
(let orig_ins_len (TypeList-length tylist)) | ||
(let st_tr (SubTuple arg_tr 0 orig_ins_len)) | ||
(let st_fa (SubTuple arg_fa 0 orig_ins_len)) | ||
|
||
; New regions | ||
(let new_thn (Subst if_tr st_tr thn)) | ||
(let new_els (Subst if_fa st_fa els)) | ||
|
||
; Union the original input with Bop(c, x) in the new regions | ||
(union (Get arg_tr i) (Bop o (Const c new_ins_ty if_tr) (Get arg_tr orig_ins_len))) | ||
(union (Get arg_fa i) (Bop o (Const c new_ins_ty if_fa) (Get arg_fa orig_ins_len))) | ||
|
||
; Union the ifs | ||
(union if_e (If pred new_ins new_thn new_els)) | ||
) | ||
:ruleset push-in) | ||
|
||
(rule ( | ||
(= if_e (If pred orig_inputs thn els)) | ||
(ContextOf if_e outer_ctx) | ||
(= (Bop o x (Const c ty outer_ctx)) (Get orig_inputs i)) | ||
(HasArgType thn (TupleT tylist)) | ||
(HasArgType els (TupleT tylist)) | ||
(HasType x (Base x_ty)) | ||
) | ||
( | ||
; New inputs | ||
(let new_ins (Concat orig_inputs (Single x))) | ||
(let new_ins_ty (TupleT (TLConcat tylist (TCons x_ty (TNil))))) | ||
|
||
; New contexts | ||
(let if_tr (InIf true pred new_ins)) | ||
(let if_fa (InIf false pred new_ins)) | ||
|
||
; New args | ||
(let arg_tr (Arg new_ins_ty if_tr)) | ||
(let arg_fa (Arg new_ins_ty if_fa)) | ||
|
||
; SubTuple | ||
(let orig_ins_len (TypeList-length tylist)) | ||
(let st_tr (SubTuple arg_tr 0 orig_ins_len)) | ||
(let st_fa (SubTuple arg_fa 0 orig_ins_len)) | ||
|
||
; New regions | ||
(let new_thn (Subst if_tr st_tr thn)) | ||
(let new_els (Subst if_fa st_fa els)) | ||
|
||
; Union the original input with Bop(x, c) in the new regions | ||
(union (Get arg_tr i) (Bop o (Get arg_tr orig_ins_len) (Const c new_ins_ty if_tr))) | ||
(union (Get arg_fa i) (Bop o (Get arg_fa orig_ins_len) (Const c new_ins_ty if_fa))) | ||
|
||
; Union the ifs | ||
(union if_e (If pred new_ins new_thn new_els)) | ||
) | ||
:ruleset push-in) | ||
|
||
(rule ( | ||
(= if_e (If pred orig_inputs thn els)) | ||
(ContextOf if_e outer_ctx) | ||
(= (Uop o x) (Get orig_inputs i)) | ||
(HasArgType thn (TupleT tylist)) | ||
(HasArgType els (TupleT tylist)) | ||
(HasType x (Base x_ty)) | ||
) | ||
( | ||
; New inputs | ||
(let new_ins (Concat orig_inputs (Single x))) | ||
(let new_ins_ty (TupleT (TLConcat tylist (TCons x_ty (TNil))))) | ||
|
||
; New contexts | ||
(let if_tr (InIf true pred new_ins)) | ||
(let if_fa (InIf false pred new_ins)) | ||
|
||
; New args | ||
(let arg_tr (Arg new_ins_ty if_tr)) | ||
(let arg_fa (Arg new_ins_ty if_fa)) | ||
|
||
; SubTuple | ||
(let orig_ins_len (TypeList-length tylist)) | ||
(let st_tr (SubTuple arg_tr 0 orig_ins_len)) | ||
(let st_fa (SubTuple arg_fa 0 orig_ins_len)) | ||
|
||
; New regions | ||
(let new_thn (Subst if_tr st_tr thn)) | ||
(let new_els (Subst if_fa st_fa els)) | ||
|
||
; Union the original input with Uop(x) in the new regions | ||
(union (Get arg_tr i) (Uop o (Get arg_tr orig_ins_len))) | ||
(union (Get arg_fa i) (Uop o (Get arg_fa orig_ins_len))) | ||
|
||
; Union the ifs | ||
(union if_e (If pred new_ins new_thn new_els)) | ||
) | ||
:ruleset push-in) | ||
|
||
; OLD VERSION - Too slow for now | ||
; ; push bop input into region | ||
; (rule ( | ||
; (= if_e (If pred orig_inputs thn els)) | ||
; (ContextOf if_e outer_ctx) | ||
; (= (Bop o x y) (Get orig_inputs i)) | ||
; (HasArgType thn (TupleT tylist)) | ||
; (HasArgType els (TupleT tylist)) | ||
; (HasType x (Base x_ty)) | ||
; (HasType y (Base y_ty)) | ||
; ) | ||
; ( | ||
; ; New inputs | ||
; (let new_ins (Concat orig_inputs (Concat (Single x) (Single y)))) | ||
; (let new_ins_ty (TupleT (TLConcat tylist (TCons x_ty (TCons y_ty (TNil)))))) | ||
|
||
; ; New contexts | ||
; (let if_tr (InIf true pred new_ins)) | ||
; (let if_fa (InIf false pred new_ins)) | ||
|
||
; ; New args | ||
; (let arg_tr (Arg new_ins_ty if_tr)) | ||
; (let arg_fa (Arg new_ins_ty if_fa)) | ||
|
||
; ; SubTuple | ||
; (let orig_ins_len (TypeList-length tylist)) | ||
; (let st_tr (SubTuple arg_tr 0 orig_ins_len)) | ||
; (let st_fa (SubTuple arg_fa 0 orig_ins_len)) | ||
|
||
; ; New regions | ||
; (let new_thn (Subst if_tr st_tr thn)) | ||
; (let new_els (Subst if_fa st_fa els)) | ||
|
||
; ; Union the original input with Bop(x, y) in the new regions | ||
; (union (Get arg_tr i) (Bop o (Get arg_tr orig_ins_len) (Get arg_tr (+ orig_ins_len 1)))) | ||
; (union (Get arg_fa i) (Bop o (Get arg_fa orig_ins_len) (Get arg_fa (+ orig_ins_len 1)))) | ||
|
||
; ; Union the ifs | ||
; (union if_e (If pred new_ins new_thn new_els)) | ||
; ) | ||
; :ruleset push-in) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// ARGS: 20 | ||
fn main(input: i64) { | ||
let a: i64 = input * 2; | ||
if (input > 0) { | ||
let x: i64 = a * 5; | ||
} else { | ||
let x: i64 = a * -3; | ||
} | ||
|
||
if (x >= 0) { | ||
let res: i64 = x * 10; | ||
} else { | ||
let res: i64 = x * 37; | ||
} | ||
|
||
println!("{}", res); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.