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

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 #

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

Defined in Futhark.IR.Mem

Methods

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

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

Defined in Futhark.IR.Mem

Methods

safeOp :: ASTRep rep => MemOp inner rep -> Bool Source #

cheapOp :: ASTRep rep => MemOp inner rep -> Bool Source #

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

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

Defined in Futhark.IR.Mem

Methods

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

consumedInOp :: Aliased rep => MemOp inner rep -> Names Source #

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

Defined in Futhark.IR.Mem

Methods

opType :: HasScope rep m => MemOp inner rep -> m [ExtType] 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 #

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

Defined in Futhark.IR.Mem

Methods

freeIn' :: MemOp inner rep -> FV 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 #

mapExt :: (Int -> Int) -> 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 LMAD

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 ExtLMAD

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

ReturnsNewBlock Space Int ExtLMAD

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 LMAD = LMAD (TPrimExp Int64 VName) Source #

The LMAD representation used for memory annotations.

type ExtLMAD = LMAD (TPrimExp Int64 (Ext VName)) Source #

An index function that may contain existential variables.

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, RephraseOp inner, ASTConstraints (inner rep), FreeIn (inner rep), OpC rep ~ MemOp inner) 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 rep -> m [ExpReturns] Source #

Instances

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

Defined in Futhark.IR.GPU.Op

Methods

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

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

Defined in Futhark.IR.MC.Op

Methods

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

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

Defined in Futhark.IR.Mem

Methods

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

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

Defined in Futhark.IR.Mem

Methods

opReturns :: forall rep (inner :: Type -> Type) m. (Mem rep inner, Monad m, HasScope rep 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.

nameInfoToMemInfo :: Mem rep inner => NameInfo rep -> MemBound NoUniqueness Source #

Turn info into memory information.

lookupMemInfo :: (HasScope rep m, Mem rep inner) => VName -> m (MemInfo SubExp NoUniqueness MemBind) Source #

Look up information about the memory block with this name.

lookupArraySummary :: (Mem rep inner, HasScope rep m, Monad m) => VName -> m (VName, LMAD (TPrimExp Int64 VName)) Source #

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