Skip to content

Commit

Permalink
Fix #774.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Jun 28, 2019
1 parent 3ba3996 commit da9cfac
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Futhark/Optimise/InPlaceLowering.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
-- minimising memory copies. As an example, consider this program:
--
-- @
-- loop (r = r0) = for i < n do
-- let a = r[i] in
-- let r[i] = a * i in
-- r
-- in
-- let r =
-- loop (r1 = r0) = for i < n do
-- let a = r1[i] in
-- let r1[i] = a * i in
-- r1
-- in
-- ...
-- let x = y with [k] <- r in
-- ...
Expand Down Expand Up @@ -48,16 +49,19 @@
-- (4) If @x@ is consumed at a point after the loop, @r@ must not
-- be used after that point.
--
-- (5) The size of @r@ is invariant inside the loop.
-- (5) The size of @r1@ is invariant inside the loop.
--
-- (6) The value @r@ must come from something that we can actually
-- optimise (e.g. not a function parameter).
--
-- (7) @y@ (or its aliases) may not be used inside the body of the
-- loop.
--
-- FIXME: the implementation is not finished yet. Specifically, the
-- above conditions are not really checked.
-- (8) The result of the loop may not alias the merge parameter
-- @r1@.
--
-- FIXME: the implementation is not finished yet. Specifically, not
-- all of the above conditions are checked.
module Futhark.Optimise.InPlaceLowering
(
inPlaceLowering
Expand Down
5 changes: 5 additions & 0 deletions src/Futhark/Optimise/InPlaceLowering/LowerIntoStm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ lowerUpdateIntoLoop scope updates pat ctx val form body = do
--
-- We also check that the merge parameters we work with have
-- loop-invariant shapes.

forM_ (zip val $ bodyAliases body) $ \((p, _), als) ->
guard $ not $ paramName p `S.member` als

mk_in_place_map <- summariseLoop updates usedInBody resmap val

Just $ do
in_place_map <- mk_in_place_map
(val',prebnds,postbnds) <- mkMerges in_place_map
Expand Down
15 changes: 15 additions & 0 deletions tests/issue774.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- In-place lowering should be careful about updates where the new
-- value potentially aliases the old one.

type t = [8]u32

let zero: t = replicate 8 0

let pack [n] (xs: [n]bool): t =
loop ret = zero for i in 0..<n do
if xs[i]
then map1 (+1) ret
else ret

let main =
map (\x -> replicate 2 (x >= 4) |> pack) (iota 10)

0 comments on commit da9cfac

Please sign in to comment.