forked from WebAssembly/spec
-
Notifications
You must be signed in to change notification settings - Fork 18
Add if-then-else introduction IL pass #211
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -11,5 +11,6 @@ | |
| undep | ||
| utils | ||
| aliasDemut | ||
| ite | ||
| ) | ||
| ) | ||
This file contains hidden or 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,113 @@ | ||
| (* | ||
| This pass recongizes when multiple subsequent clauses of a definition share the pattern and have | ||
| only boolean premises, and rewrites that to a single clause using if-then-else on the right-hand side. | ||
| *) | ||
|
|
||
| open Util | ||
| open Source | ||
| open Il.Ast | ||
|
|
||
| (* Errors *) | ||
|
|
||
| (* let error at msg = Error.error at "if-then-else introduction" msg *) | ||
|
|
||
| let clauses_have_same_args (c1 : clause) (c2 : clause) = | ||
| match (c1.it, c2.it) with | ||
| | (DefD (binds1, args1, _, _), DefD (binds2, args2, _, _)) -> | ||
| Il.Eq.eq_list Il.Eq.eq_bind binds1 binds2 && | ||
| Il.Eq.eq_list Il.Eq.eq_arg args1 args2 | ||
|
|
||
| let group_clauses_by_same_args clauses = | ||
| let group_helper acc clause = | ||
| match acc with | ||
| | [] -> [[clause]] | ||
| | group :: rest -> | ||
| let representative = List.hd group in | ||
| if clauses_have_same_args representative clause then | ||
| (group @ [clause]) :: rest | ||
| else | ||
| [clause] :: group :: rest | ||
| in | ||
| List.fold_left group_helper [] clauses |> List.rev | ||
|
|
||
| let is_else_premise prem = | ||
| match prem.it with | ||
| | ElsePr -> true | ||
| | _ -> false | ||
|
|
||
| let has_only_else_premise clause = | ||
| match clause.it with | ||
| | DefD (_, _, _, ([_] as prems)) -> | ||
| List.for_all is_else_premise prems | ||
| | _ -> false | ||
|
|
||
| let mk_and : exp -> exp -> exp = fun e1 e2 -> | ||
| BinE (`AndOp, `BoolT, e1, e2) $$ (no_region, BoolT $ no_region) | ||
|
|
||
| let rec prem_to_bool_exp (prem : prem) : exp option = | ||
| match prem.it with | ||
| | IfPr exp -> Some exp | ||
| | NegPr prem' -> | ||
| Option.bind (prem_to_bool_exp prem') (fun exp -> | ||
| Some (UnE (`NotOp, `BoolT, exp) $$ (prem.at, BoolT $ no_region))) | ||
| | _ -> None | ||
|
|
||
| and prems_to_bool_exp = function | ||
| | [] -> Some (BoolE true $$ (no_region, BoolT $ no_region)) | ||
| | [prem] -> prem_to_bool_exp prem | ||
| | prem :: rest -> | ||
| Option.bind (prem_to_bool_exp prem) (fun exp1 -> | ||
| Option.bind (prems_to_bool_exp rest) (fun exp2 -> | ||
| Some (mk_and exp1 exp2) | ||
| ) | ||
| ) | ||
|
|
||
| let clause_prems_to_bool_exp (clause : clause) : exp option = | ||
| match clause.it with | ||
| | DefD (_, _, _, prems) -> | ||
| prems_to_bool_exp prems | ||
|
|
||
| let mk_if (cond : exp) (then_exp : exp) (else_exp : exp) : exp = | ||
| IfE (cond, then_exp, else_exp) $$ (no_region, then_exp.note) | ||
|
|
||
| let rec t_clause_group_to_expr (clauses : clause list) : exp option = | ||
| match clauses with | ||
| | [] -> None | ||
| | [clause] -> | ||
| if has_only_else_premise clause then | ||
| match clause.it with | ||
| | DefD (_, _, rhs, _) -> Some rhs | ||
| else | ||
| None | ||
| | clause :: rest_clauses -> | ||
| match clause_prems_to_bool_exp clause with | ||
| | None -> None | ||
| | Some cond -> | ||
| match clause.it with | ||
| | DefD (_, _, rhs, _) -> | ||
| match t_clause_group_to_expr rest_clauses with | ||
| | None -> None | ||
| | Some else_exp -> Some (mk_if cond rhs else_exp) | ||
|
|
||
| let t_clause_group (clauses : clause list) : clause list = | ||
| match t_clause_group_to_expr clauses with | ||
| | None -> clauses | ||
| | Some exp -> | ||
| let rep = List.hd clauses in | ||
| let DefD (binds, args, _rhs, _prems) = rep.it in | ||
| [{ rep with it = DefD (binds, args, exp, []) }] | ||
|
|
||
|
|
||
| let t_clauses clauses = | ||
| List.concat_map t_clause_group (group_clauses_by_same_args clauses) | ||
|
|
||
| let rec t_def (def : def) : def = { def with it = t_def' def.it } | ||
| and t_def' = function | ||
| | RecD defs -> | ||
| RecD (List.map t_def defs) | ||
| | DecD (id, params, typ, clauses) -> | ||
| DecD (id, params, typ, t_clauses clauses) | ||
| | def -> def | ||
|
|
||
| let transform (defs : script) : script = | ||
| List.map t_def defs | ||
This file contains hidden or 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 @@ | ||
| val transform : Il.Ast.script -> Il.Ast.script |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/00-elab.il specification.act/00-elab.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/01-typefamily-removal.il specification.act/01-typefamily-removal.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/02-remove-indexed-types.il specification.act/02-remove-indexed-types.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/03-totalize.il specification.act/03-totalize.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/04-else.il specification.act/04-else.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/05-sideconditions.il specification.act/05-sideconditions.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/06-sub.il specification.act/06-sub.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/07-alias-demut.il specification.act/07-alias-demut.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/01-ite.il specification.act/01-ite.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/02-typefamily-removal.il specification.act/02-typefamily-removal.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/03-remove-indexed-types.il specification.act/03-remove-indexed-types.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/04-totalize.il specification.act/04-totalize.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/05-else.il specification.act/05-else.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/06-sideconditions.il specification.act/06-sideconditions.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/07-sub.il specification.act/07-sub.il)))) | ||
| (rule (alias runtest) (deps (alias dune.inc) (file specification.act) (glob_files_rec specification.exp/*)) (action (no-infer (diff specification.exp/08-alias-demut.il specification.act/08-alias-demut.il)))) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would we maybe want a little weaker of a condition? (i.e. if the preceding clause pattern matches to the next clause)
i.e for a function as such:
Could technically group up as well. (But would need to add n = 5 to the condition)
Though this might be too tricky for such a pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I thought about this, but didn't necessarily want it in the first version. I would actually say that should be a separate pass that runs first, an overlap-removal pass, after which all patterns are either the same or clearly apart. This would compose well, I think.