{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}

-- | The IR tracks aliases, mostly to ensure the soundness of in-place
-- updates, but it can also be used for other things (such as memory
-- optimisations).  This module contains the raw building blocks for
-- determining the aliases of the values produced by expressions.  It
-- also contains some building blocks for inspecting consumption.
--
-- One important caveat is that all aliases computed here are /local/.
-- Thus, they do not take aliases-of-aliases into account.  See
-- "Futhark.Analysis.Alias" if this is not what you want.
module Futhark.IR.Prop.Aliases
  ( subExpAliases,
    expAliases,
    patternAliases,
    lookupAliases,
    Aliased (..),
    AliasesOf (..),

    -- * Consumption
    consumedInStm,
    consumedInExp,
    consumedByLambda,

    -- * Extensibility
    AliasTable,
    AliasedOp (..),
    CanBeAliased (..),
  )
where

import Control.Arrow (first)
import qualified Data.Kind
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
import Futhark.IR.Syntax

-- | The class of lores that contain aliasing information.
class
  ( Decorations lore,
    AliasedOp (Op lore),
    AliasesOf (LetDec lore)
  ) =>
  Aliased lore
  where
  -- | The aliases of the body results.
  bodyAliases :: Body lore -> [Names]

  -- | The variables consumed in the body.
  consumedInBody :: Body lore -> Names

vnameAliases :: VName -> Names
vnameAliases :: VName -> Names
vnameAliases = VName -> Names
oneName

-- | The alises of a subexpression.
subExpAliases :: SubExp -> Names
subExpAliases :: SubExp -> Names
subExpAliases Constant {} = Names
forall a. Monoid a => a
mempty
subExpAliases (Var VName
v) = VName -> Names
vnameAliases VName
v

basicOpAliases :: BasicOp -> [Names]
basicOpAliases :: BasicOp -> [Names]
basicOpAliases (SubExp SubExp
se) = [SubExp -> Names
subExpAliases SubExp
se]
basicOpAliases (Opaque SubExp
se) = [SubExp -> Names
subExpAliases SubExp
se]
basicOpAliases (ArrayLit [SubExp]
_ Type
_) = [Names
forall a. Monoid a => a
mempty]
basicOpAliases BinOp {} = [Names
forall a. Monoid a => a
mempty]
basicOpAliases ConvOp {} = [Names
forall a. Monoid a => a
mempty]
basicOpAliases CmpOp {} = [Names
forall a. Monoid a => a
mempty]
basicOpAliases UnOp {} = [Names
forall a. Monoid a => a
mempty]
basicOpAliases (Index VName
ident Slice SubExp
_) = [VName -> Names
vnameAliases VName
ident]
basicOpAliases Update {} = [Names
forall a. Monoid a => a
mempty]
basicOpAliases Iota {} = [Names
forall a. Monoid a => a
mempty]
basicOpAliases Replicate {} = [Names
forall a. Monoid a => a
mempty]
basicOpAliases Scratch {} = [Names
forall a. Monoid a => a
mempty]
basicOpAliases (Reshape ShapeChange SubExp
_ VName
e) = [VName -> Names
vnameAliases VName
e]
basicOpAliases (Rearrange [Int]
_ VName
e) = [VName -> Names
vnameAliases VName
e]
basicOpAliases (Rotate [SubExp]
_ VName
e) = [VName -> Names
vnameAliases VName
e]
basicOpAliases Concat {} = [Names
forall a. Monoid a => a
mempty]
basicOpAliases Copy {} = [Names
forall a. Monoid a => a
mempty]
basicOpAliases Manifest {} = [Names
forall a. Monoid a => a
mempty]
basicOpAliases Assert {} = [Names
forall a. Monoid a => a
mempty]

ifAliases :: ([Names], Names) -> ([Names], Names) -> [Names]
ifAliases :: ([Names], Names) -> ([Names], Names) -> [Names]
ifAliases ([Names]
als1, Names
cons1) ([Names]
als2, Names
cons2) =
  (Names -> Names) -> [Names] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map (Names -> Names -> Names
`namesSubtract` Names
cons) ([Names] -> [Names]) -> [Names] -> [Names]
forall a b. (a -> b) -> a -> b
$ (Names -> Names -> Names) -> [Names] -> [Names] -> [Names]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Names -> Names -> Names
forall a. Monoid a => a -> a -> a
mappend [Names]
als1 [Names]
als2
  where
    cons :: Names
cons = Names
cons1 Names -> Names -> Names
forall a. Semigroup a => a -> a -> a
<> Names
cons2

funcallAliases :: [(SubExp, Diet)] -> [TypeBase shape Uniqueness] -> [Names]
funcallAliases :: [(SubExp, Diet)] -> [TypeBase shape Uniqueness] -> [Names]
funcallAliases [(SubExp, Diet)]
args [TypeBase shape Uniqueness]
t =
  [TypeBase shape Uniqueness] -> [(Names, Diet)] -> [Names]
forall shape.
[TypeBase shape Uniqueness] -> [(Names, Diet)] -> [Names]
returnAliases [TypeBase shape Uniqueness]
t [(SubExp -> Names
subExpAliases SubExp
se, Diet
d) | (SubExp
se, Diet
d) <- [(SubExp, Diet)]
args]

-- | The aliases of an expression, one per non-context value returned.
expAliases :: (Aliased lore) => Exp lore -> [Names]
expAliases :: Exp lore -> [Names]
expAliases (If SubExp
_ BodyT lore
tb BodyT lore
fb IfDec (BranchType lore)
dec) =
  Int -> [Names] -> [Names]
forall a. Int -> [a] -> [a]
drop ([Names] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Names]
all_aliases Int -> Int -> Int
forall a. Num a => a -> a -> a
- [BranchType lore] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [BranchType lore]
ts) [Names]
all_aliases
  where
    ts :: [BranchType lore]
ts = IfDec (BranchType lore) -> [BranchType lore]
forall rt. IfDec rt -> [rt]
ifReturns IfDec (BranchType lore)
dec
    all_aliases :: [Names]
all_aliases =
      ([Names], Names) -> ([Names], Names) -> [Names]
ifAliases
        (BodyT lore -> [Names]
forall lore. Aliased lore => Body lore -> [Names]
bodyAliases BodyT lore
tb, BodyT lore -> Names
forall lore. Aliased lore => Body lore -> Names
consumedInBody BodyT lore
tb)
        (BodyT lore -> [Names]
forall lore. Aliased lore => Body lore -> [Names]
bodyAliases BodyT lore
fb, BodyT lore -> Names
forall lore. Aliased lore => Body lore -> Names
consumedInBody BodyT lore
fb)
expAliases (BasicOp BasicOp
op) = BasicOp -> [Names]
basicOpAliases BasicOp
op
expAliases (DoLoop [(FParam lore, SubExp)]
ctxmerge [(FParam lore, SubExp)]
valmerge LoopForm lore
_ BodyT lore
loopbody) =
  (Names -> Names) -> [Names] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map (Names -> Names -> Names
`namesSubtract` Names
merge_names) [Names]
val_aliases
  where
    ([Names]
_ctx_aliases, [Names]
val_aliases) =
      Int -> [Names] -> ([Names], [Names])
forall a. Int -> [a] -> ([a], [a])
splitAt ([(FParam lore, SubExp)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(FParam lore, SubExp)]
ctxmerge) ([Names] -> ([Names], [Names])) -> [Names] -> ([Names], [Names])
forall a b. (a -> b) -> a -> b
$ BodyT lore -> [Names]
forall lore. Aliased lore => Body lore -> [Names]
bodyAliases BodyT lore
loopbody
    merge_names :: Names
merge_names = [VName] -> Names
namesFromList ([VName] -> Names) -> [VName] -> Names
forall a b. (a -> b) -> a -> b
$ ((FParam lore, SubExp) -> VName)
-> [(FParam lore, SubExp)] -> [VName]
forall a b. (a -> b) -> [a] -> [b]
map (FParam lore -> VName
forall dec. Param dec -> VName
paramName (FParam lore -> VName)
-> ((FParam lore, SubExp) -> FParam lore)
-> (FParam lore, SubExp)
-> VName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FParam lore, SubExp) -> FParam lore
forall a b. (a, b) -> a
fst) ([(FParam lore, SubExp)] -> [VName])
-> [(FParam lore, SubExp)] -> [VName]
forall a b. (a -> b) -> a -> b
$ [(FParam lore, SubExp)]
ctxmerge [(FParam lore, SubExp)]
-> [(FParam lore, SubExp)] -> [(FParam lore, SubExp)]
forall a. [a] -> [a] -> [a]
++ [(FParam lore, SubExp)]
valmerge
expAliases (Apply Name
_ [(SubExp, Diet)]
args [RetType lore]
t (Safety, SrcLoc, [SrcLoc])
_) =
  [(SubExp, Diet)] -> [TypeBase ExtShape Uniqueness] -> [Names]
forall shape.
[(SubExp, Diet)] -> [TypeBase shape Uniqueness] -> [Names]
funcallAliases [(SubExp, Diet)]
args ([TypeBase ExtShape Uniqueness] -> [Names])
-> [TypeBase ExtShape Uniqueness] -> [Names]
forall a b. (a -> b) -> a -> b
$ (RetType lore -> TypeBase ExtShape Uniqueness)
-> [RetType lore] -> [TypeBase ExtShape Uniqueness]
forall a b. (a -> b) -> [a] -> [b]
map RetType lore -> TypeBase ExtShape Uniqueness
forall t. DeclExtTyped t => t -> TypeBase ExtShape Uniqueness
declExtTypeOf [RetType lore]
t
expAliases (Op Op lore
op) = Op lore -> [Names]
forall op. AliasedOp op => op -> [Names]
opAliases Op lore
op

returnAliases :: [TypeBase shape Uniqueness] -> [(Names, Diet)] -> [Names]
returnAliases :: [TypeBase shape Uniqueness] -> [(Names, Diet)] -> [Names]
returnAliases [TypeBase shape Uniqueness]
rts [(Names, Diet)]
args = (TypeBase shape Uniqueness -> Names)
-> [TypeBase shape Uniqueness] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map TypeBase shape Uniqueness -> Names
returnType' [TypeBase shape Uniqueness]
rts
  where
    returnType' :: TypeBase shape Uniqueness -> Names
returnType' (Array PrimType
_ shape
_ Uniqueness
Nonunique) =
      [Names] -> Names
forall a. Monoid a => [a] -> a
mconcat ([Names] -> Names) -> [Names] -> Names
forall a b. (a -> b) -> a -> b
$ ((Names, Diet) -> Names) -> [(Names, Diet)] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map ((Names -> Diet -> Names) -> (Names, Diet) -> Names
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Names -> Diet -> Names
maskAliases) [(Names, Diet)]
args
    returnType' (Array PrimType
_ shape
_ Uniqueness
Unique) =
      Names
forall a. Monoid a => a
mempty
    returnType' (Prim PrimType
_) =
      Names
forall a. Monoid a => a
mempty
    returnType' Mem {} =
      [Char] -> Names
forall a. HasCallStack => [Char] -> a
error [Char]
"returnAliases Mem"

maskAliases :: Names -> Diet -> Names
maskAliases :: Names -> Diet -> Names
maskAliases Names
_ Diet
Consume = Names
forall a. Monoid a => a
mempty
maskAliases Names
_ Diet
ObservePrim = Names
forall a. Monoid a => a
mempty
maskAliases Names
als Diet
Observe = Names
als

-- | The variables consumed in this statement.
consumedInStm :: Aliased lore => Stm lore -> Names
consumedInStm :: Stm lore -> Names
consumedInStm = Exp lore -> Names
forall lore. Aliased lore => Exp lore -> Names
consumedInExp (Exp lore -> Names) -> (Stm lore -> Exp lore) -> Stm lore -> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stm lore -> Exp lore
forall lore. Stm lore -> Exp lore
stmExp

-- | The variables consumed in this expression.
consumedInExp :: (Aliased lore) => Exp lore -> Names
consumedInExp :: Exp lore -> Names
consumedInExp (Apply Name
_ [(SubExp, Diet)]
args [RetType lore]
_ (Safety, SrcLoc, [SrcLoc])
_) =
  [Names] -> Names
forall a. Monoid a => [a] -> a
mconcat (((SubExp, Diet) -> Names) -> [(SubExp, Diet)] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map ((Names, Diet) -> Names
forall p. Monoid p => (p, Diet) -> p
consumeArg ((Names, Diet) -> Names)
-> ((SubExp, Diet) -> (Names, Diet)) -> (SubExp, Diet) -> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SubExp -> Names) -> (SubExp, Diet) -> (Names, Diet)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first SubExp -> Names
subExpAliases) [(SubExp, Diet)]
args)
  where
    consumeArg :: (p, Diet) -> p
consumeArg (p
als, Diet
Consume) = p
als
    consumeArg (p, Diet)
_ = p
forall a. Monoid a => a
mempty
consumedInExp (If SubExp
_ BodyT lore
tb BodyT lore
fb IfDec (BranchType lore)
_) =
  BodyT lore -> Names
forall lore. Aliased lore => Body lore -> Names
consumedInBody BodyT lore
tb Names -> Names -> Names
forall a. Semigroup a => a -> a -> a
<> BodyT lore -> Names
forall lore. Aliased lore => Body lore -> Names
consumedInBody BodyT lore
fb
consumedInExp (DoLoop [(FParam lore, SubExp)]
_ [(FParam lore, SubExp)]
merge LoopForm lore
_ BodyT lore
_) =
  [Names] -> Names
forall a. Monoid a => [a] -> a
mconcat
    ( ((FParam lore, SubExp) -> Names)
-> [(FParam lore, SubExp)] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map (SubExp -> Names
subExpAliases (SubExp -> Names)
-> ((FParam lore, SubExp) -> SubExp)
-> (FParam lore, SubExp)
-> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FParam lore, SubExp) -> SubExp
forall a b. (a, b) -> b
snd) ([(FParam lore, SubExp)] -> [Names])
-> [(FParam lore, SubExp)] -> [Names]
forall a b. (a -> b) -> a -> b
$
        ((FParam lore, SubExp) -> Bool)
-> [(FParam lore, SubExp)] -> [(FParam lore, SubExp)]
forall a. (a -> Bool) -> [a] -> [a]
filter (TypeBase Shape Uniqueness -> Bool
forall shape. TypeBase shape Uniqueness -> Bool
unique (TypeBase Shape Uniqueness -> Bool)
-> ((FParam lore, SubExp) -> TypeBase Shape Uniqueness)
-> (FParam lore, SubExp)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FParam lore -> TypeBase Shape Uniqueness
forall dec. DeclTyped dec => Param dec -> TypeBase Shape Uniqueness
paramDeclType (FParam lore -> TypeBase Shape Uniqueness)
-> ((FParam lore, SubExp) -> FParam lore)
-> (FParam lore, SubExp)
-> TypeBase Shape Uniqueness
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FParam lore, SubExp) -> FParam lore
forall a b. (a, b) -> a
fst) [(FParam lore, SubExp)]
merge
    )
consumedInExp (BasicOp (Update VName
src Slice SubExp
_ SubExp
_)) = VName -> Names
oneName VName
src
consumedInExp (Op Op lore
op) = Op lore -> Names
forall op. AliasedOp op => op -> Names
consumedInOp Op lore
op
consumedInExp Exp lore
_ = Names
forall a. Monoid a => a
mempty

-- | The variables consumed by this lambda.
consumedByLambda :: Aliased lore => Lambda lore -> Names
consumedByLambda :: Lambda lore -> Names
consumedByLambda = Body lore -> Names
forall lore. Aliased lore => Body lore -> Names
consumedInBody (Body lore -> Names)
-> (Lambda lore -> Body lore) -> Lambda lore -> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lambda lore -> Body lore
forall lore. LambdaT lore -> BodyT lore
lambdaBody

-- | The aliases of each pattern element (including the context).
patternAliases :: AliasesOf dec => PatternT dec -> [Names]
patternAliases :: PatternT dec -> [Names]
patternAliases = (PatElemT dec -> Names) -> [PatElemT dec] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map (dec -> Names
forall a. AliasesOf a => a -> Names
aliasesOf (dec -> Names) -> (PatElemT dec -> dec) -> PatElemT dec -> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatElemT dec -> dec
forall dec. PatElemT dec -> dec
patElemDec) ([PatElemT dec] -> [Names])
-> (PatternT dec -> [PatElemT dec]) -> PatternT dec -> [Names]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatternT dec -> [PatElemT dec]
forall dec. PatternT dec -> [PatElemT dec]
patternElements

-- | Something that contains alias information.
class AliasesOf a where
  -- | The alias of the argument element.
  aliasesOf :: a -> Names

instance AliasesOf Names where
  aliasesOf :: Names -> Names
aliasesOf = Names -> Names
forall a. a -> a
id

instance AliasesOf dec => AliasesOf (PatElemT dec) where
  aliasesOf :: PatElemT dec -> Names
aliasesOf = dec -> Names
forall a. AliasesOf a => a -> Names
aliasesOf (dec -> Names) -> (PatElemT dec -> dec) -> PatElemT dec -> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatElemT dec -> dec
forall dec. PatElemT dec -> dec
patElemDec

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

-- | The class of operations that can produce aliasing and consumption
-- information.
class IsOp op => AliasedOp op where
  opAliases :: op -> [Names]
  consumedInOp :: op -> Names

instance AliasedOp () where
  opAliases :: () -> [Names]
opAliases () = []
  consumedInOp :: () -> Names
consumedInOp () = Names
forall a. Monoid a => a
mempty

-- | Pre-existing aliases for variables.  Used to add transitive
-- aliases.
type AliasTable = M.Map VName Names

-- | The class of operations that can be given aliasing information.
-- This is a somewhat subtle concept that is only used in the
-- simplifier and when using "lore adapters".
class AliasedOp (OpWithAliases op) => CanBeAliased op where
  -- | The op that results when we add aliases to this op.
  type OpWithAliases op :: Data.Kind.Type

  -- | Remove aliases from this op.
  removeOpAliases :: OpWithAliases op -> op

  -- | Add aliases to this op.
  addOpAliases :: AliasTable -> op -> OpWithAliases op

instance CanBeAliased () where
  type OpWithAliases () = ()
  removeOpAliases :: OpWithAliases () -> ()
removeOpAliases = OpWithAliases () -> ()
forall a. a -> a
id
  addOpAliases :: AliasTable -> () -> OpWithAliases ()
addOpAliases = (() -> ()) -> AliasTable -> () -> ()
forall a b. a -> b -> a
const () -> ()
forall a. a -> a
id