Skip to content

Commit

Permalink
Fix #1142.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Oct 5, 2020
1 parent 5eca00b commit 247e6e5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

* Fix in monomorphisation of types with constant sizes.

* Fix in in-place loweing (#1142).

## [0.17.2]

### Added
Expand Down
11 changes: 10 additions & 1 deletion src/Futhark/IR/Prop/Aliases.hs
Expand Up @@ -15,6 +15,7 @@ module Futhark.IR.Prop.Aliases
( subExpAliases,
expAliases,
patternAliases,
lookupAliases,
Aliased (..),
AliasesOf (..),

Expand All @@ -31,7 +32,8 @@ where

import Control.Arrow (first)
import qualified Data.Kind
import Futhark.IR.Prop (IsOp)
import qualified Data.Map as M
import Futhark.IR.Prop (IsOp, NameInfo (..), Scope)
import Futhark.IR.Prop.Names
import Futhark.IR.Prop.Patterns
import Futhark.IR.Prop.Types
Expand Down Expand Up @@ -169,6 +171,13 @@ instance AliasesOf Names where
instance AliasesOf dec => AliasesOf (PatElemT dec) where
aliasesOf = aliasesOf . patElemDec

-- | Also includes the name itself.
lookupAliases :: AliasesOf (LetDec lore) => VName -> Scope lore -> Names
lookupAliases v scope =
case M.lookup v scope of
Just (LetName dec) -> oneName v <> aliasesOf dec
_ -> oneName v

-- | The class of operations that can produce aliasing and consumption
-- information.
class IsOp op => AliasedOp op where
Expand Down
16 changes: 7 additions & 9 deletions src/Futhark/Optimise/InPlaceLowering.hs
Expand Up @@ -13,10 +13,9 @@
-- @
-- let r =
-- loop (r1 = r0) = for i < n do
-- let a = r1[i] in
-- let r1[i] = a * i in
-- r1
-- in
-- let a = r1[i]
-- let r1[i] = a * i
-- in r1
-- ...
-- let x = y with [k] <- r in
-- ...
Expand All @@ -27,11 +26,10 @@
-- @
-- let x0 = y with [k] <- r0
-- loop (x = x0) = for i < n do
-- let a = a[k,i] in
-- let x[k,i] = a * i in
-- x
-- in
-- let r = x[y] in
-- let a = a[k,i]
-- let x[k,i] = a * i
-- in x
-- let r = x[k] in
-- ...
-- @
--
Expand Down
19 changes: 10 additions & 9 deletions src/Futhark/Optimise/InPlaceLowering/LowerIntoStm.hs
Expand Up @@ -13,7 +13,6 @@ import Control.Monad
import Control.Monad.Writer
import Data.Either
import Data.List (find, unzip4)
import qualified Data.Map as M
import Data.Maybe (mapMaybe)
import Futhark.Analysis.PrimExp.Convert
import Futhark.Construct
Expand Down Expand Up @@ -205,7 +204,7 @@ lowerUpdateIntoLoop scope updates pat ctx val form body = do
forM_ (zip val $ bodyAliases body) $ \((p, _), als) ->
guard $ not $ paramName p `nameIn` als

mk_in_place_map <- summariseLoop updates usedInBody resmap val
mk_in_place_map <- summariseLoop scope updates usedInBody resmap val

Just $ do
in_place_map <- mk_in_place_map
Expand All @@ -217,10 +216,8 @@ lowerUpdateIntoLoop scope updates pat ctx val form body = do
let body' = mkBody (newbnds <> res_bnds) body_res
return (prebnds, postbnds, ctxpat, valpat, ctx, val', body')
where
usedInBody = mconcat $ map expandAliases $ namesToList $ freeIn body <> freeIn form
expandAliases v = case M.lookup v scope of
Just (LetName dec) -> oneName v <> aliasesOf dec
_ -> oneName v
usedInBody =
mconcat $ map (`lookupAliases` scope) $ namesToList $ freeIn body <> freeIn form
resmap = zip (bodyResult body) $ patternValueIdents pat

mkMerges ::
Expand Down Expand Up @@ -277,18 +274,22 @@ lowerUpdateIntoLoop scope updates pat ctx val form body = do
Left (inPatternAs summary)

summariseLoop ::
MonadFreshNames m =>
( Aliased lore,
MonadFreshNames m
) =>
Scope lore ->
[DesiredUpdate (als, Type)] ->
Names ->
[(SubExp, Ident)] ->
[(Param DeclType, SubExp)] ->
Maybe (m [LoopResultSummary (als, Type)])
summariseLoop updates usedInBody resmap merge =
summariseLoop scope updates usedInBody resmap merge =
sequence <$> zipWithM summariseLoopResult resmap merge
where
summariseLoopResult (se, v) (fparam, mergeinit)
| Just update <- find (updateHasValue $ identName v) updates =
if updateSource update `nameIn` usedInBody
-- Safety condition (7)
if usedInBody `namesIntersect` lookupAliases (updateSource update) scope
then Nothing
else
if hasLoopInvariantShape fparam
Expand Down
2 changes: 1 addition & 1 deletion src/Futhark/TypeCheck.hs
Expand Up @@ -60,7 +60,7 @@ import Data.Maybe
import qualified Data.Set as S
import Futhark.Analysis.PrimExp
import Futhark.Construct (instantiateShapes)
import Futhark.IR.Aliases
import Futhark.IR.Aliases hiding (lookupAliases)
import Futhark.Util
import Futhark.Util.Pretty (Pretty, align, indent, ppr, prettyDoc, text, (<+>))

Expand Down
7 changes: 7 additions & 0 deletions tests/issue1142.fut
@@ -0,0 +1,7 @@
let main [n][m] (xsss: *[2][n][m]i32) =
#[unsafe]
let xss = xsss[0]
let ys = loop acc = replicate m 0 for i < m do
let acc[i] = xsss[0,0,i]+1
in acc
in xss with [0] = ys

0 comments on commit 247e6e5

Please sign in to comment.