-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new simplification rule for invariant loop parameters.
This was suggested by Cosmin to address some of the code produced by AD. The idea is that for a loop of the form loop p = x ... ...stms... in res we construct and simplify the body let p = x ...stms... in res and if that simplifies to 'x', then we conclude that the loop parameter 'p' must be invariant to the loop and simply bind it (and the loop result) to 'x'. Complication: for multi-parameter loops, we must also check that the *original* computation of 'res' does *only* depends on other invariant loop parameters. Currently we do this only for loops that have a constant as one of their initial loop parameter values. The main downside of this rule is that doing recursive simplification is quite expensive. Especially after sequentialisation, pretty much every 'reduce' will have been turned into a loop that triggers this rule (although the rule itself will fail in most cases, after doing the simplification). Therefore I'm a bit hesitant to enable it as is. Sure, the Futhark compiler is slow and it was never meant to be fast, but it is still quite easy for the compiler to become *uselessly slow* if we are not careful. E.g. on OptionPricing, this rule itself makes compilation 10% slower (and does not actually optimise anything - this is purely the cost of failing checks).
- Loading branch information
Showing
6 changed files
with
175 additions
and
42 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
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
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,12 @@ | ||
-- Removal of invariant of invariant loop parameter (and eventually entire loop). | ||
-- == | ||
-- structure { DoLoop 0 } | ||
|
||
entry main [n] (bs: [n]bool) = | ||
let res = | ||
loop (x, y) = (0i32, false) | ||
for i < n do | ||
let y' = bs[i] && y | ||
let x' = x + (i32.bool y') | ||
in (x', y') | ||
in 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- Not actually invariant if you look carefully! | ||
-- == | ||
-- input { 0 } output { 0 false } | ||
-- input { 4 } output { 3 true } | ||
|
||
entry main (n: i32) = | ||
let res = | ||
loop (x, y) = (0i32, false) | ||
for _i < n do | ||
let x' = if y then x + 1 else x | ||
let y' = y || true | ||
in (x', y') | ||
in 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
-- Also not actually invariant. | ||
-- == | ||
-- input { 0 } output { 1 } | ||
-- input { 10 } output { 89 } | ||
|
||
|
||
def fib(n: i32): i32 = | ||
entry main (n: i32) = | ||
let (x,_) = loop (x, y) = (1,1) for _i < n do (y, x+y) | ||
in x | ||
|
||
def main(n: i32): i32 = fib(n) |