From 247e6e575732ae0f194d49114949d41e3294fa5e Mon Sep 17 00:00:00 2001 From: Troels Henriksen Date: Mon, 5 Oct 2020 09:10:25 +0200 Subject: [PATCH] Fix #1142. --- CHANGELOG.md | 2 ++ src/Futhark/IR/Prop/Aliases.hs | 11 ++++++++++- src/Futhark/Optimise/InPlaceLowering.hs | 16 +++++++--------- .../Optimise/InPlaceLowering/LowerIntoStm.hs | 19 ++++++++++--------- src/Futhark/TypeCheck.hs | 2 +- tests/issue1142.fut | 7 +++++++ 6 files changed, 37 insertions(+), 20 deletions(-) create mode 100644 tests/issue1142.fut diff --git a/CHANGELOG.md b/CHANGELOG.md index 762f696235..9085fd1b16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Futhark/IR/Prop/Aliases.hs b/src/Futhark/IR/Prop/Aliases.hs index 0679923aee..961912b76f 100644 --- a/src/Futhark/IR/Prop/Aliases.hs +++ b/src/Futhark/IR/Prop/Aliases.hs @@ -15,6 +15,7 @@ module Futhark.IR.Prop.Aliases ( subExpAliases, expAliases, patternAliases, + lookupAliases, Aliased (..), AliasesOf (..), @@ -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 @@ -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 diff --git a/src/Futhark/Optimise/InPlaceLowering.hs b/src/Futhark/Optimise/InPlaceLowering.hs index 209a9ddf40..7dc78458bf 100644 --- a/src/Futhark/Optimise/InPlaceLowering.hs +++ b/src/Futhark/Optimise/InPlaceLowering.hs @@ -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 -- ... @@ -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 -- ... -- @ -- diff --git a/src/Futhark/Optimise/InPlaceLowering/LowerIntoStm.hs b/src/Futhark/Optimise/InPlaceLowering/LowerIntoStm.hs index 8a127b6300..87a8c20cad 100644 --- a/src/Futhark/Optimise/InPlaceLowering/LowerIntoStm.hs +++ b/src/Futhark/Optimise/InPlaceLowering/LowerIntoStm.hs @@ -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 @@ -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 @@ -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 :: @@ -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 diff --git a/src/Futhark/TypeCheck.hs b/src/Futhark/TypeCheck.hs index 263a68d929..902ba000e1 100644 --- a/src/Futhark/TypeCheck.hs +++ b/src/Futhark/TypeCheck.hs @@ -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, (<+>)) diff --git a/tests/issue1142.fut b/tests/issue1142.fut new file mode 100644 index 0000000000..f2edb63b0a --- /dev/null +++ b/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