{-# 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,
    patAliases,
    lookupAliases,
    Aliased (..),
    AliasesOf (..),

    -- * Consumption
    consumedInStm,
    consumedInExp,
    consumedByLambda,

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

import Data.Bifunctor (first, second)
import qualified Data.Kind
import Data.List (find)
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 representations that contain aliasing information.
class (RepTypes rep, AliasedOp (Op rep), AliasesOf (LetDec rep)) => Aliased rep where
  -- | The aliases of the body results.
  bodyAliases :: Body rep -> [Names]

  -- | The variables consumed in the body.
  consumedInBody :: Body rep -> 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 OpaqueOp
_ 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 (FlatIndex VName
ident FlatSlice SubExp
_) = [VName -> Names
vnameAliases VName
ident]
basicOpAliases FlatUpdate {} = [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]
basicOpAliases UpdateAcc {} = [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 rep) => Exp rep -> [Names]
expAliases :: Exp rep -> [Names]
expAliases (If SubExp
_ Body rep
tb Body rep
fb IfDec (BranchType rep)
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 rep] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [BranchType rep]
ts) [Names]
all_aliases
  where
    ts :: [BranchType rep]
ts = IfDec (BranchType rep) -> [BranchType rep]
forall rt. IfDec rt -> [rt]
ifReturns IfDec (BranchType rep)
dec
    all_aliases :: [Names]
all_aliases =
      ([Names], Names) -> ([Names], Names) -> [Names]
ifAliases
        (Body rep -> [Names]
forall rep. Aliased rep => Body rep -> [Names]
bodyAliases Body rep
tb, Body rep -> Names
forall rep. Aliased rep => Body rep -> Names
consumedInBody Body rep
tb)
        (Body rep -> [Names]
forall rep. Aliased rep => Body rep -> [Names]
bodyAliases Body rep
fb, Body rep -> Names
forall rep. Aliased rep => Body rep -> Names
consumedInBody Body rep
fb)
expAliases (BasicOp BasicOp
op) = BasicOp -> [Names]
basicOpAliases BasicOp
op
expAliases (DoLoop [(FParam rep, SubExp)]
merge LoopForm rep
_ Body rep
loopbody) = do
  (FParam rep
p, Names
als) <-
    [(FParam rep, Names)] -> [(FParam rep, Names)]
forall dec. Eq dec => [(Param dec, Names)] -> [(Param dec, Names)]
transitive ([(FParam rep, Names)] -> [(FParam rep, Names)])
-> ([Names] -> [(FParam rep, Names)])
-> [Names]
-> [(FParam rep, Names)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FParam rep] -> [Names] -> [(FParam rep, Names)]
forall a b. [a] -> [b] -> [(a, b)]
zip [FParam rep]
params ([Names] -> [(FParam rep, Names)])
-> [Names] -> [(FParam rep, 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]
arg_aliases (Body rep -> [Names]
forall rep. Aliased rep => Body rep -> [Names]
bodyAliases Body rep
loopbody)
  let als' :: Names
als' = Names
als Names -> Names -> Names
`namesSubtract` Names
param_names
  if TypeBase Shape Uniqueness -> Bool
forall shape. TypeBase shape Uniqueness -> Bool
unique (TypeBase Shape Uniqueness -> Bool)
-> TypeBase Shape Uniqueness -> Bool
forall a b. (a -> b) -> a -> b
$ FParam rep -> TypeBase Shape Uniqueness
forall dec. DeclTyped dec => Param dec -> TypeBase Shape Uniqueness
paramDeclType FParam rep
p
    then Names -> [Names]
forall (f :: * -> *) a. Applicative f => a -> f a
pure Names
forall a. Monoid a => a
mempty
    else Names -> [Names]
forall (f :: * -> *) a. Applicative f => a -> f a
pure Names
als'
  where
    arg_aliases :: [Names]
arg_aliases = ((FParam rep, SubExp) -> Names)
-> [(FParam rep, SubExp)] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map (SubExp -> Names
subExpAliases (SubExp -> Names)
-> ((FParam rep, SubExp) -> SubExp)
-> (FParam rep, SubExp)
-> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FParam rep, SubExp) -> SubExp
forall a b. (a, b) -> b
snd) [(FParam rep, SubExp)]
merge
    params :: [FParam rep]
params = ((FParam rep, SubExp) -> FParam rep)
-> [(FParam rep, SubExp)] -> [FParam rep]
forall a b. (a -> b) -> [a] -> [b]
map (FParam rep, SubExp) -> FParam rep
forall a b. (a, b) -> a
fst [(FParam rep, SubExp)]
merge
    param_names :: Names
param_names = [VName] -> Names
namesFromList ([VName] -> Names) -> [VName] -> Names
forall a b. (a -> b) -> a -> b
$ (FParam rep -> VName) -> [FParam rep] -> [VName]
forall a b. (a -> b) -> [a] -> [b]
map FParam rep -> VName
forall dec. Param dec -> VName
paramName [FParam rep]
params
    transitive :: [(Param dec, Names)] -> [(Param dec, Names)]
transitive [(Param dec, Names)]
merge_and_als =
      let merge_and_als' :: [(Param dec, Names)]
merge_and_als' = ((Param dec, Names) -> (Param dec, Names))
-> [(Param dec, Names)] -> [(Param dec, Names)]
forall a b. (a -> b) -> [a] -> [b]
map ((Names -> Names) -> (Param dec, Names) -> (Param dec, Names)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second Names -> Names
expand) [(Param dec, Names)]
merge_and_als
       in if [(Param dec, Names)]
merge_and_als' [(Param dec, Names)] -> [(Param dec, Names)] -> Bool
forall a. Eq a => a -> a -> Bool
== [(Param dec, Names)]
merge_and_als
            then [(Param dec, Names)]
merge_and_als
            else [(Param dec, Names)] -> [(Param dec, Names)]
transitive [(Param dec, Names)]
merge_and_als'
      where
        look :: VName -> Names
look VName
v = Names
-> ((Param dec, Names) -> Names)
-> Maybe (Param dec, Names)
-> Names
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Names
forall a. Monoid a => a
mempty (Param dec, Names) -> Names
forall a b. (a, b) -> b
snd (Maybe (Param dec, Names) -> Names)
-> Maybe (Param dec, Names) -> Names
forall a b. (a -> b) -> a -> b
$ ((Param dec, Names) -> Bool)
-> [(Param dec, Names)] -> Maybe (Param dec, Names)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((VName -> VName -> Bool
forall a. Eq a => a -> a -> Bool
== VName
v) (VName -> Bool)
-> ((Param dec, Names) -> VName) -> (Param dec, Names) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Param dec -> VName
forall dec. Param dec -> VName
paramName (Param dec -> VName)
-> ((Param dec, Names) -> Param dec) -> (Param dec, Names) -> VName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Param dec, Names) -> Param dec
forall a b. (a, b) -> a
fst) [(Param dec, Names)]
merge_and_als
        expand :: Names -> Names
expand Names
als = Names
als Names -> Names -> Names
forall a. Semigroup a => a -> a -> a
<> (VName -> Names) -> [VName] -> Names
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap VName -> Names
look (Names -> [VName]
namesToList Names
als)
expAliases (Apply Name
_ [(SubExp, Diet)]
args [RetType rep]
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 rep -> TypeBase ExtShape Uniqueness)
-> [RetType rep] -> [TypeBase ExtShape Uniqueness]
forall a b. (a -> b) -> [a] -> [b]
map RetType rep -> TypeBase ExtShape Uniqueness
forall t. DeclExtTyped t => t -> TypeBase ExtShape Uniqueness
declExtTypeOf [RetType rep]
t
expAliases (WithAcc [WithAccInput rep]
inputs Lambda rep
lam) =
  (WithAccInput rep -> [Names]) -> [WithAccInput rep] -> [Names]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap WithAccInput rep -> [Names]
forall (t :: * -> *) a a a c.
(Foldable t, Monoid a) =>
(a, t a, c) -> [a]
inputAliases [WithAccInput rep]
inputs [Names] -> [Names] -> [Names]
forall a. [a] -> [a] -> [a]
++ Int -> [Names] -> [Names]
forall a. Int -> [a] -> [a]
drop Int
num_accs (Body rep -> [Names]
forall rep. Aliased rep => Body rep -> [Names]
bodyAliases (Lambda rep -> Body rep
forall rep. Lambda rep -> Body rep
lambdaBody Lambda rep
lam))
  where
    inputAliases :: (a, t a, c) -> [a]
inputAliases (a
_, t a
arrs, c
_) = Int -> a -> [a]
forall a. Int -> a -> [a]
replicate (t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
arrs) a
forall a. Monoid a => a
mempty
    num_accs :: Int
num_accs = [WithAccInput rep] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [WithAccInput rep]
inputs
expAliases (Op Op rep
op) = Op rep -> [Names]
forall op. AliasedOp op => op -> [Names]
opAliases Op rep
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' Acc {} =
      [Char] -> Names
forall a. HasCallStack => [Char] -> a
error [Char]
"returnAliases Acc"
    returnType' Mem {} =
      [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

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 rep => Stm rep -> Names
consumedInStm :: Stm rep -> Names
consumedInStm = Exp rep -> Names
forall rep. Aliased rep => Exp rep -> Names
consumedInExp (Exp rep -> Names) -> (Stm rep -> Exp rep) -> Stm rep -> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stm rep -> Exp rep
forall rep. Stm rep -> Exp rep
stmExp

-- | The variables consumed in this expression.
consumedInExp :: (Aliased rep) => Exp rep -> Names
consumedInExp :: Exp rep -> Names
consumedInExp (Apply Name
_ [(SubExp, Diet)]
args [RetType rep]
_ (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 (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
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
_ Body rep
tb Body rep
fb IfDec (BranchType rep)
_) =
  Body rep -> Names
forall rep. Aliased rep => Body rep -> Names
consumedInBody Body rep
tb Names -> Names -> Names
forall a. Semigroup a => a -> a -> a
<> Body rep -> Names
forall rep. Aliased rep => Body rep -> Names
consumedInBody Body rep
fb
consumedInExp (DoLoop [(FParam rep, SubExp)]
merge LoopForm rep
form Body rep
body) =
  [Names] -> Names
forall a. Monoid a => [a] -> a
mconcat
    ( ((FParam rep, SubExp) -> Names)
-> [(FParam rep, SubExp)] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map (SubExp -> Names
subExpAliases (SubExp -> Names)
-> ((FParam rep, SubExp) -> SubExp)
-> (FParam rep, SubExp)
-> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FParam rep, SubExp) -> SubExp
forall a b. (a, b) -> b
snd) ([(FParam rep, SubExp)] -> [Names])
-> [(FParam rep, SubExp)] -> [Names]
forall a b. (a -> b) -> a -> b
$
        ((FParam rep, SubExp) -> Bool)
-> [(FParam rep, SubExp)] -> [(FParam rep, SubExp)]
forall a. (a -> Bool) -> [a] -> [a]
filter (TypeBase Shape Uniqueness -> Bool
forall shape. TypeBase shape Uniqueness -> Bool
unique (TypeBase Shape Uniqueness -> Bool)
-> ((FParam rep, SubExp) -> TypeBase Shape Uniqueness)
-> (FParam rep, SubExp)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FParam rep -> TypeBase Shape Uniqueness
forall dec. DeclTyped dec => Param dec -> TypeBase Shape Uniqueness
paramDeclType (FParam rep -> TypeBase Shape Uniqueness)
-> ((FParam rep, SubExp) -> FParam rep)
-> (FParam rep, SubExp)
-> TypeBase Shape Uniqueness
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FParam rep, SubExp) -> FParam rep
forall a b. (a, b) -> a
fst) [(FParam rep, SubExp)]
merge
    )
    Names -> Names -> Names
forall a. Semigroup a => a -> a -> a
<> LoopForm rep -> Names
consumedInForm LoopForm rep
form
  where
    body_consumed :: Names
body_consumed = Body rep -> Names
forall rep. Aliased rep => Body rep -> Names
consumedInBody Body rep
body
    varConsumed :: (Param (LParamInfo rep), VName) -> Bool
varConsumed = (VName -> Names -> Bool
`nameIn` Names
body_consumed) (VName -> Bool)
-> ((Param (LParamInfo rep), VName) -> VName)
-> (Param (LParamInfo rep), VName)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Param (LParamInfo rep) -> VName
forall dec. Param dec -> VName
paramName (Param (LParamInfo rep) -> VName)
-> ((Param (LParamInfo rep), VName) -> Param (LParamInfo rep))
-> (Param (LParamInfo rep), VName)
-> VName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Param (LParamInfo rep), VName) -> Param (LParamInfo rep)
forall a b. (a, b) -> a
fst
    consumedInForm :: LoopForm rep -> Names
consumedInForm (ForLoop VName
_ IntType
_ SubExp
_ [(Param (LParamInfo rep), VName)]
loopvars) =
      [VName] -> Names
namesFromList ([VName] -> Names) -> [VName] -> Names
forall a b. (a -> b) -> a -> b
$ ((Param (LParamInfo rep), VName) -> VName)
-> [(Param (LParamInfo rep), VName)] -> [VName]
forall a b. (a -> b) -> [a] -> [b]
map (Param (LParamInfo rep), VName) -> VName
forall a b. (a, b) -> b
snd ([(Param (LParamInfo rep), VName)] -> [VName])
-> [(Param (LParamInfo rep), VName)] -> [VName]
forall a b. (a -> b) -> a -> b
$ ((Param (LParamInfo rep), VName) -> Bool)
-> [(Param (LParamInfo rep), VName)]
-> [(Param (LParamInfo rep), VName)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Param (LParamInfo rep), VName) -> Bool
varConsumed [(Param (LParamInfo rep), VName)]
loopvars
    consumedInForm WhileLoop {} =
      Names
forall a. Monoid a => a
mempty
consumedInExp (WithAcc [WithAccInput rep]
inputs Lambda rep
lam) =
  [Names] -> Names
forall a. Monoid a => [a] -> a
mconcat ((WithAccInput rep -> Names) -> [WithAccInput rep] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map WithAccInput rep -> Names
forall a c. (a, [VName], c) -> Names
inputConsumed [WithAccInput rep]
inputs)
    Names -> Names -> Names
forall a. Semigroup a => a -> a -> a
<> ( Lambda rep -> Names
forall rep. Aliased rep => Lambda rep -> Names
consumedByLambda Lambda rep
lam
           Names -> Names -> Names
`namesSubtract` [VName] -> Names
namesFromList ((Param (LParamInfo rep) -> VName)
-> [Param (LParamInfo rep)] -> [VName]
forall a b. (a -> b) -> [a] -> [b]
map Param (LParamInfo rep) -> VName
forall dec. Param dec -> VName
paramName (Lambda rep -> [Param (LParamInfo rep)]
forall rep. Lambda rep -> [LParam rep]
lambdaParams Lambda rep
lam))
       )
  where
    inputConsumed :: (a, [VName], c) -> Names
inputConsumed (a
_, [VName]
arrs, c
_) = [VName] -> Names
namesFromList [VName]
arrs
consumedInExp (BasicOp (Update Safety
_ VName
src Slice SubExp
_ SubExp
_)) = VName -> Names
oneName VName
src
consumedInExp (BasicOp (FlatUpdate VName
src FlatSlice SubExp
_ VName
_)) = VName -> Names
oneName VName
src
consumedInExp (BasicOp (UpdateAcc VName
acc [SubExp]
_ [SubExp]
_)) = VName -> Names
oneName VName
acc
consumedInExp (BasicOp BasicOp
_) = Names
forall a. Monoid a => a
mempty
consumedInExp (Op Op rep
op) = Op rep -> Names
forall op. AliasedOp op => op -> Names
consumedInOp Op rep
op

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

-- | The aliases of each pattern element (including the context).
patAliases :: AliasesOf dec => Pat dec -> [Names]
patAliases :: Pat dec -> [Names]
patAliases = (PatElem dec -> Names) -> [PatElem dec] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map (dec -> Names
forall a. AliasesOf a => a -> Names
aliasesOf (dec -> Names) -> (PatElem dec -> dec) -> PatElem dec -> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatElem dec -> dec
forall dec. PatElem dec -> dec
patElemDec) ([PatElem dec] -> [Names])
-> (Pat dec -> [PatElem dec]) -> Pat dec -> [Names]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pat dec -> [PatElem dec]
forall dec. Pat dec -> [PatElem dec]
patElems

-- | 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 (PatElem dec) where
  aliasesOf :: PatElem dec -> Names
aliasesOf = dec -> Names
forall a. AliasesOf a => a -> Names
aliasesOf (dec -> Names) -> (PatElem dec -> dec) -> PatElem dec -> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatElem dec -> dec
forall dec. PatElem dec -> dec
patElemDec

-- | Also includes the name itself.
lookupAliases :: AliasesOf (LetDec rep) => VName -> Scope rep -> Names
lookupAliases :: VName -> Scope rep -> Names
lookupAliases VName
v Scope rep
scope =
  case VName -> Scope rep -> Maybe (NameInfo rep)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup VName
v Scope rep
scope of
    Just (LetName LetDec rep
dec) ->
      VName -> Names
oneName VName
v Names -> Names -> Names
forall a. Semigroup a => a -> a -> a
<> (VName -> Names) -> [VName] -> Names
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (VName -> Scope rep -> Names
forall rep. AliasesOf (LetDec rep) => VName -> Scope rep -> Names
`lookupAliases` Scope rep
scope) (Names -> [VName]
namesToList (LetDec rep -> Names
forall a. AliasesOf a => a -> Names
aliasesOf LetDec rep
dec))
    Maybe (NameInfo rep)
_ -> 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 "rep 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