futhark-0.25.6: An optimising compiler for a functional, array-oriented language.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Futhark.IR.Mem

Description

Building blocks for defining representations where every array is given information about which memory block is it based in, and how array elements map to memory block offsets.

There are two primary concepts you will need to understand:

  1. Memory blocks, which are Futhark values of type Mem (parametrized with their size). These correspond to arbitrary blocks of memory, and are created using the Alloc operation.
  2. Index functions, which describe a mapping from the index space of an array (eg. a two-dimensional space for an array of type [[int]]) to a one-dimensional offset into a memory block. Thus, index functions describe how arbitrary-dimensional arrays are mapped to the single-dimensional world of memory.

At a conceptual level, imagine that we have a two-dimensional array a of 32-bit integers, consisting of n rows of m elements each. This array could be represented in classic row-major format with an index function like the following:

  f(i,j) = i * m + j

When we want to know the location of element a[2,3], we simply call the index function as f(2,3) and obtain 2*m+3. We could also have chosen another index function, one that represents the array in column-major (or "transposed") format:

  f(i,j) = j * n + i

Index functions are not Futhark-level functions, but a special construct that the final code generator will eventually use to generate concrete access code. By modifying the index functions we can change how an array is represented in memory, which can permit memory access pattern optimisations.

Every time we bind an array, whether in a let-binding, loop merge parameter, or lambda parameter, we have an annotation specifying a memory block and an index function. In some cases, such as let-bindings for many expressions, we are free to specify an arbitrary index function and memory block - for example, we get to decide where Copy stores its result - but in other cases the type rules of the expression chooses for us. For example, Index always produces an array in the same memory block as its input, and with the same index function, except with some indices fixed.

Synopsis

Documentation

data MemOp (inner :: Type -> Type) (rep :: Type) Source #

Constructors

Alloc SubExp Space

Allocate a memory block.

Inner (inner rep) 

Instances

Instances details
CanBeAliased inner => CanBeAliased (MemOp inner) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

addOpAliases :: AliasableRep rep => AliasTable -> MemOp inner rep -> MemOp inner (Aliases rep) Source #

RephraseOp inner => RephraseOp (MemOp inner) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

rephraseInOp :: Monad m => Rephraser m from to -> MemOp inner from -> m (MemOp inner to) Source #

CanBeWise inner => CanBeWise (MemOp inner) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

addOpWisdom :: Informing rep => MemOp inner rep -> MemOp inner (Wise rep) Source #

Show (inner rep) => Show (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

showsPrec :: Int -> MemOp inner rep -> ShowS #

show :: MemOp inner rep -> String #

showList :: [MemOp inner rep] -> ShowS #

OpMetrics (inner rep) => OpMetrics (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

opMetrics :: MemOp inner rep -> MetricsM () Source #

IndexOp (inner rep) => IndexOp (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

indexOp :: (ASTRep rep0, IndexOp (Op rep0)) => SymbolTable rep0 -> Int -> MemOp inner rep -> [TPrimExp Int64 VName] -> Maybe Indexed Source #

OpReturns (inner rep) => OpReturns (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

opReturns :: forall rep0 (inner0 :: Type -> Type) m. (Mem rep0 inner0, Monad m, HasScope rep0 m) => MemOp inner rep -> m [ExpReturns] Source #

IsOp (inner rep) => IsOp (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

safeOp :: MemOp inner rep -> Bool Source #

cheapOp :: MemOp inner rep -> Bool Source #

opDependencies :: MemOp inner rep -> [Names] Source #

AliasedOp (inner rep) => AliasedOp (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

opAliases :: MemOp inner rep -> [Names] Source #

consumedInOp :: MemOp inner rep -> Names Source #

FreeIn (inner rep) => FreeIn (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

freeIn' :: MemOp inner rep -> FV Source #

TypedOp (inner rep) => TypedOp (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

opType :: HasScope t m => MemOp inner rep -> m [ExtType] Source #

CSEInOp (op rep) => CSEInOp (MemOp op rep) Source # 
Instance details

Defined in Futhark.Optimise.CSE

Methods

cseInOp :: MemOp op rep -> CSEM rep0 (MemOp op rep)

SizeSubst (op rep) => SizeSubst (MemOp op rep) Source # 
Instance details

Defined in Futhark.Pass.ExplicitAllocations

Methods

opIsConst :: MemOp op rep -> Bool Source #

Rename (inner rep) => Rename (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

rename :: MemOp inner rep -> RenameM (MemOp inner rep) Source #

Substitute (inner rep) => Substitute (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

substituteNames :: Map VName VName -> MemOp inner rep -> MemOp inner rep Source #

Eq (inner rep) => Eq (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

(==) :: MemOp inner rep -> MemOp inner rep -> Bool #

(/=) :: MemOp inner rep -> MemOp inner rep -> Bool #

Ord (inner rep) => Ord (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

compare :: MemOp inner rep -> MemOp inner rep -> Ordering #

(<) :: MemOp inner rep -> MemOp inner rep -> Bool #

(<=) :: MemOp inner rep -> MemOp inner rep -> Bool #

(>) :: MemOp inner rep -> MemOp inner rep -> Bool #

(>=) :: MemOp inner rep -> MemOp inner rep -> Bool #

max :: MemOp inner rep -> MemOp inner rep -> MemOp inner rep #

min :: MemOp inner rep -> MemOp inner rep -> MemOp inner rep #

Pretty (inner rep) => Pretty (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

pretty :: MemOp inner rep -> Doc ann #

prettyList :: [MemOp inner rep] -> Doc ann #

traverseMemOpStms :: Monad m => OpStmsTraverser m (inner rep) rep -> OpStmsTraverser m (MemOp inner rep) rep Source #

A helper for defining TraverseOpStms.

data MemInfo d u ret Source #

A summary of the memory information for every let-bound identifier, function parameter, and return value. Parameterisered over uniqueness, dimension, and auxiliary array information.

Constructors

MemPrim PrimType

A primitive value.

MemMem Space

A memory block.

MemArray PrimType (ShapeBase d) u ret

The array is stored in the named memory block, and with the given index function. The index function maps indices in the array to element offset, not byte offsets! To translate to byte offsets, multiply the offset with the size of the array element type.

MemAcc VName Shape [Type] u

An accumulator, which is not stored anywhere.

Instances

Instances details
HasLetDecMem LetDecMem Source # 
Instance details

Defined in Futhark.IR.Mem

IsBodyType BodyReturns Source # 
Instance details

Defined in Futhark.IR.Mem

IsRetType FunReturns Source # 
Instance details

Defined in Futhark.IR.Mem

Simplifiable [FunReturns] Source # 
Instance details

Defined in Futhark.IR.Mem

(Show d, Show ret, Show u) => Show (MemInfo d u ret) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

showsPrec :: Int -> MemInfo d u ret -> ShowS #

show :: MemInfo d u ret -> String #

showList :: [MemInfo d u ret] -> ShowS #

(FreeIn d, FreeIn ret) => FreeIn (MemInfo d u ret) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

freeIn' :: MemInfo d u ret -> FV Source #

FixExt ret => DeclExtTyped (MemInfo ExtSize Uniqueness ret) Source # 
Instance details

Defined in Futhark.IR.Mem

DeclTyped (MemInfo SubExp Uniqueness ret) Source # 
Instance details

Defined in Futhark.IR.Mem

FixExt ret => ExtTyped (MemInfo ExtSize NoUniqueness ret) Source # 
Instance details

Defined in Futhark.IR.Mem

FixExt ret => ExtTyped (MemInfo ExtSize Uniqueness ret) Source # 
Instance details

Defined in Futhark.IR.Mem

FixExt ret => FixExt (MemInfo ExtSize u ret) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

fixExt :: Int -> SubExp -> MemInfo ExtSize u ret -> MemInfo ExtSize u ret Source #

Typed (MemInfo SubExp NoUniqueness ret) Source # 
Instance details

Defined in Futhark.IR.Mem

Typed (MemInfo SubExp Uniqueness ret) Source # 
Instance details

Defined in Futhark.IR.Mem

(Simplifiable d, Simplifiable ret) => Simplifiable (MemInfo d u ret) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

simplify :: SimplifiableRep rep => MemInfo d u ret -> SimpleM rep (MemInfo d u ret) Source #

(Substitute d, Substitute ret) => Rename (MemInfo d u ret) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

rename :: MemInfo d u ret -> RenameM (MemInfo d u ret) Source #

(Substitute d, Substitute ret) => Substitute (MemInfo d u ret) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

substituteNames :: Map VName VName -> MemInfo d u ret -> MemInfo d u ret Source #

(Eq d, Eq ret, Eq u) => Eq (MemInfo d u ret) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

(==) :: MemInfo d u ret -> MemInfo d u ret -> Bool #

(/=) :: MemInfo d u ret -> MemInfo d u ret -> Bool #

(Ord d, Ord ret, Ord u) => Ord (MemInfo d u ret) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

compare :: MemInfo d u ret -> MemInfo d u ret -> Ordering #

(<) :: MemInfo d u ret -> MemInfo d u ret -> Bool #

(<=) :: MemInfo d u ret -> MemInfo d u ret -> Bool #

(>) :: MemInfo d u ret -> MemInfo d u ret -> Bool #

(>=) :: MemInfo d u ret -> MemInfo d u ret -> Bool #

max :: MemInfo d u ret -> MemInfo d u ret -> MemInfo d u ret #

min :: MemInfo d u ret -> MemInfo d u ret -> MemInfo d u ret #

(Pretty (ShapeBase d), Pretty (TypeBase (ShapeBase d) u), Pretty d, Pretty u, Pretty ret) => Pretty (MemInfo d u ret) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

pretty :: MemInfo d u ret -> Doc ann #

prettyList :: [MemInfo d u ret] -> Doc ann #

data MemBind Source #

Memory information for an array bound somewhere in the program.

Constructors

ArrayIn VName IxFun

Located in this memory block with this index function.

Instances

Instances details
Show MemBind Source # 
Instance details

Defined in Futhark.IR.Mem

HasLetDecMem LetDecMem Source # 
Instance details

Defined in Futhark.IR.Mem

FreeIn MemBind Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

freeIn' :: MemBind -> FV Source #

Simplifiable MemBind Source # 
Instance details

Defined in Futhark.IR.Mem

Rename MemBind Source # 
Instance details

Defined in Futhark.IR.Mem

Substitute MemBind Source # 
Instance details

Defined in Futhark.IR.Mem

Eq MemBind Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

(==) :: MemBind -> MemBind -> Bool #

(/=) :: MemBind -> MemBind -> Bool #

Ord MemBind Source # 
Instance details

Defined in Futhark.IR.Mem

Pretty MemBind Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

pretty :: MemBind -> Doc ann #

prettyList :: [MemBind] -> Doc ann #

data MemReturn Source #

A description of the memory properties of an array being returned by an operation.

Constructors

ReturnsInBlock VName ExtIxFun

The array is located in a memory block that is already in scope.

ReturnsNewBlock Space Int ExtIxFun

The operation returns a new (existential) memory block.

Instances

Instances details
Show MemReturn Source # 
Instance details

Defined in Futhark.IR.Mem

FreeIn MemReturn Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

freeIn' :: MemReturn -> FV Source #

FixExt MemReturn Source # 
Instance details

Defined in Futhark.IR.Mem

IsBodyType BodyReturns Source # 
Instance details

Defined in Futhark.IR.Mem

IsRetType FunReturns Source # 
Instance details

Defined in Futhark.IR.Mem

Simplifiable MemReturn Source # 
Instance details

Defined in Futhark.IR.Mem

Rename MemReturn Source # 
Instance details

Defined in Futhark.IR.Mem

Substitute MemReturn Source # 
Instance details

Defined in Futhark.IR.Mem

Eq MemReturn Source # 
Instance details

Defined in Futhark.IR.Mem

Ord MemReturn Source # 
Instance details

Defined in Futhark.IR.Mem

Pretty MemReturn Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

pretty :: MemReturn -> Doc ann #

prettyList :: [MemReturn] -> Doc ann #

Simplifiable [FunReturns] Source # 
Instance details

Defined in Futhark.IR.Mem

type IxFun = IxFun (TPrimExp Int64 VName) Source #

The index function representation used for memory annotations.

type ExtIxFun = IxFun (TPrimExp Int64 (Ext VName)) Source #

An index function that may contain existential variables.

type LMAD = LMAD (TPrimExp Int64 VName) Source #

The LMAD representation used for memory annotations.

type ExpReturns = MemInfo ExtSize NoUniqueness (Maybe MemReturn) Source #

The memory return of an expression. An array is annotated with Maybe MemReturn, which can be interpreted as the expression either dictating exactly where the array is located when it is returned (if Just), or able to put it whereever the binding prefers (if Nothing).

This is necessary to capture the difference between an expression that is just an array-typed variable, in which the array being "returned" is located where it already is, and a copy expression, whose entire purpose is to store an existing array in some arbitrary location. This is a consequence of the design decision never to have implicit memory copies.

type BodyReturns = MemInfo ExtSize NoUniqueness MemReturn Source #

The return of a body, which must always indicate where returned arrays are located.

type FunReturns = MemInfo ExtSize Uniqueness MemReturn Source #

The memory return of a function, which must always indicate where returned arrays are located.

type Mem rep inner = (FParamInfo rep ~ FParamMem, LParamInfo rep ~ LParamMem, HasLetDecMem (LetDec rep), RetType rep ~ RetTypeMem, BranchType rep ~ BranchTypeMem, ASTRep rep, OpReturns (inner rep), RephraseOp inner, Op rep ~ MemOp inner rep) Source #

class HasLetDecMem t where Source #

The class of pattern element decorators that contain memory information.

Methods

letDecMem :: t -> LetDecMem Source #

Instances

Instances details
HasLetDecMem LetDecMem Source # 
Instance details

Defined in Futhark.IR.Mem

HasLetDecMem b => HasLetDecMem (a, b) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

letDecMem :: (a, b) -> LetDecMem Source #

class IsOp op => OpReturns op where Source #

Minimal complete definition

Nothing

Methods

opReturns :: (Mem rep inner, Monad m, HasScope rep m) => op -> m [ExpReturns] Source #

Instances

Instances details
OpReturns (HostOp (NoOp :: Type -> Type) (Aliases GPUMem)) Source # 
Instance details

Defined in Futhark.IR.GPUMem

Methods

opReturns :: forall rep (inner :: Type -> Type) m. (Mem rep inner, Monad m, HasScope rep m) => HostOp NoOp (Aliases GPUMem) -> m [ExpReturns] Source #

OpReturns (HostOp (NoOp :: Type -> Type) GPUMem) Source # 
Instance details

Defined in Futhark.IR.GPUMem

Methods

opReturns :: forall rep (inner :: Type -> Type) m. (Mem rep inner, Monad m, HasScope rep m) => HostOp NoOp GPUMem -> m [ExpReturns] Source #

OpReturns (HostOp (NoOp :: Type -> Type) (Wise GPUMem)) Source # 
Instance details

Defined in Futhark.IR.GPUMem

Methods

opReturns :: forall rep (inner :: Type -> Type) m. (Mem rep inner, Monad m, HasScope rep m) => HostOp NoOp (Wise GPUMem) -> m [ExpReturns] Source #

OpReturns (MCOp (NoOp :: Type -> Type) (Aliases MCMem)) Source # 
Instance details

Defined in Futhark.IR.MCMem

Methods

opReturns :: forall rep (inner :: Type -> Type) m. (Mem rep inner, Monad m, HasScope rep m) => MCOp NoOp (Aliases MCMem) -> m [ExpReturns] Source #

OpReturns (MCOp (NoOp :: Type -> Type) MCMem) Source # 
Instance details

Defined in Futhark.IR.MCMem

Methods

opReturns :: forall rep (inner :: Type -> Type) m. (Mem rep inner, Monad m, HasScope rep m) => MCOp NoOp MCMem -> m [ExpReturns] Source #

OpReturns (MCOp (NoOp :: Type -> Type) (Wise MCMem)) Source # 
Instance details

Defined in Futhark.IR.MCMem

Methods

opReturns :: forall rep (inner :: Type -> Type) m. (Mem rep inner, Monad m, HasScope rep m) => MCOp NoOp (Wise MCMem) -> m [ExpReturns] Source #

OpReturns (inner rep) => OpReturns (MemOp inner rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

opReturns :: forall rep0 (inner0 :: Type -> Type) m. (Mem rep0 inner0, Monad m, HasScope rep0 m) => MemOp inner rep -> m [ExpReturns] Source #

OpReturns (NoOp rep) Source # 
Instance details

Defined in Futhark.IR.Mem

Methods

opReturns :: forall rep0 (inner :: Type -> Type) m. (Mem rep0 inner, Monad m, HasScope rep0 m) => NoOp rep -> m [ExpReturns] Source #

varReturns :: (HasScope rep m, Monad m, Mem rep inner) => VName -> m ExpReturns Source #

expReturns :: (LocalScope rep m, Mem rep inner) => Exp rep -> m (Maybe [ExpReturns]) Source #

The return information of an expression. This can be seen as the "return type with memory annotations" of the expression.

This can produce Nothing, which signifies that the result is an array layout that is not expressible as an index function.

lookupMemSpace :: (Mem rep inner, HasScope rep m, Monad m) => VName -> m Space Source #

Type checking parts

matchBranchReturnType :: (Mem rep inner, Checkable rep) => [BodyReturns] -> Body (Aliases rep) -> TypeM rep () Source #

matchPatToExp :: (Mem rep inner, LetDec rep ~ LetDecMem, Checkable rep) => Pat (LetDec (Aliases rep)) -> Exp (Aliases rep) -> TypeM rep () Source #

matchFunctionReturnType :: (Mem rep inner, Checkable rep) => [FunReturns] -> Result -> TypeM rep () Source #

matchLoopResultMem :: (Mem rep inner, Checkable rep) => [FParam (Aliases rep)] -> Result -> TypeM rep () Source #

Module re-exports