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

Futhark.CodeGen.ImpGen

Synopsis

Entry Points

compileProg :: (Mem rep inner, FreeIn op, MonadFreshNames m) => r -> Operations rep r op -> Space -> Prog rep -> m (Warnings, Definitions op) Source #

Pluggable Compiler

type OpCompiler rep r op = Pat (LetDec rep) -> Op rep -> ImpM rep r op () Source #

How to compile an Op.

type ExpCompiler rep r op = Pat (LetDec rep) -> Exp rep -> ImpM rep r op () Source #

How to compile an Exp.

type CopyCompiler rep r op = PrimType -> MemLoc -> MemLoc -> ImpM rep r op () Source #

type StmsCompiler rep r op = Names -> Stms rep -> ImpM rep r op () -> ImpM rep r op () Source #

How to compile some Stms.

type AllocCompiler rep r op = VName -> Count Bytes (TExp Int64) -> ImpM rep r op () Source #

An alternate way of compiling an allocation.

data Operations rep r op Source #

defaultOperations :: (Mem rep inner, FreeIn op) => OpCompiler rep r op -> Operations rep r op Source #

An operations set for which the expression compiler always returns defCompileExp.

data MemLoc Source #

When an array is declared, this is where it is stored.

Constructors

MemLoc 

Instances

Instances details
Show MemLoc Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

Eq MemLoc Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

Methods

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

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

newtype MemEntry Source #

Constructors

MemEntry 

Fields

Instances

Instances details
Show MemEntry Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

newtype ScalarEntry Source #

Constructors

ScalarEntry 

Instances

Instances details
Show ScalarEntry Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

Monadic Compiler Interface

data ImpM rep r op a Source #

Instances

Instances details
HasScope SOACS (ImpM rep r op) Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

Methods

lookupType :: VName -> ImpM rep r op Type Source #

lookupInfo :: VName -> ImpM rep r op (NameInfo SOACS) Source #

askScope :: ImpM rep r op (Scope SOACS) Source #

asksScope :: (Scope SOACS -> a) -> ImpM rep r op a Source #

Applicative (ImpM rep r op) Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

Methods

pure :: a -> ImpM rep r op a #

(<*>) :: ImpM rep r op (a -> b) -> ImpM rep r op a -> ImpM rep r op b #

liftA2 :: (a -> b -> c) -> ImpM rep r op a -> ImpM rep r op b -> ImpM rep r op c #

(*>) :: ImpM rep r op a -> ImpM rep r op b -> ImpM rep r op b #

(<*) :: ImpM rep r op a -> ImpM rep r op b -> ImpM rep r op a #

Functor (ImpM rep r op) Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

Methods

fmap :: (a -> b) -> ImpM rep r op a -> ImpM rep r op b #

(<$) :: a -> ImpM rep r op b -> ImpM rep r op a #

Monad (ImpM rep r op) Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

Methods

(>>=) :: ImpM rep r op a -> (a -> ImpM rep r op b) -> ImpM rep r op b #

(>>) :: ImpM rep r op a -> ImpM rep r op b -> ImpM rep r op b #

return :: a -> ImpM rep r op a #

MonadFreshNames (ImpM rep r op) Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

localDefaultSpace :: Space -> ImpM rep r op a -> ImpM rep r op a Source #

newVNameForFun :: String -> ImpM rep r op VName Source #

Generate a VName, prefixed with askFunction if it exists.

nameForFun :: String -> ImpM rep r op Name Source #

Generate a Name, prefixed with askFunction if it exists.

askEnv :: ImpM rep r op r Source #

localEnv :: (r -> r) -> ImpM rep r op a -> ImpM rep r op a Source #

localOps :: Operations rep r op -> ImpM rep r op a -> ImpM rep r op a Source #

type VTable rep = Map VName (VarEntry rep) Source #

The symbol table used during compilation.

getVTable :: ImpM rep r op (VTable rep) Source #

Get the current symbol table.

localVTable :: (VTable rep -> VTable rep) -> ImpM rep r op a -> ImpM rep r op a Source #

Run an action with a modified symbol table. All changes to the symbol table will be reverted once the action is done!

subImpM :: r' -> Operations rep r' op' -> ImpM rep r' op' a -> ImpM rep r op (a, Code op') Source #

subImpM_ :: r' -> Operations rep r' op' -> ImpM rep r' op' a -> ImpM rep r op (Code op') Source #

emit :: Code op -> ImpM rep r op () Source #

Emit some generated imperative code.

emitFunction :: Name -> Function op -> ImpM rep r op () Source #

Emit a function in the generated code.

hasFunction :: Name -> ImpM rep r op Bool Source #

Check if a function of a given name exists.

collect :: ImpM rep r op () -> ImpM rep r op (Code op) Source #

Execute a code generation action, returning the code that was emitted.

collect' :: ImpM rep r op a -> ImpM rep r op (a, Code op) Source #

data VarEntry rep Source #

Every non-scalar variable must be associated with an entry.

Constructors

ArrayVar (Maybe (Exp rep)) ArrayEntry 
ScalarVar (Maybe (Exp rep)) ScalarEntry 
MemVar (Maybe (Exp rep)) MemEntry 
AccVar (Maybe (Exp rep)) (VName, Shape, [Type]) 

Instances

Instances details
RepTypes rep => Show (VarEntry rep) Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

Methods

showsPrec :: Int -> VarEntry rep -> ShowS #

show :: VarEntry rep -> String #

showList :: [VarEntry rep] -> ShowS #

data ArrayEntry Source #

Instances

Instances details
Show ArrayEntry Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

Lookups

lookupVar :: VName -> ImpM rep r op (VarEntry rep) Source #

lookupArraySpace :: VName -> ImpM rep r op Space Source #

In which memory space is this array allocated?

lookupAcc :: VName -> [TExp Int64] -> ImpM rep r op (VName, Space, [VName], [TExp Int64], Maybe (Lambda rep)) Source #

In the case of a histogram-like accumulator, also sets the index parameters.

askAttrs :: ImpM rep r op Attrs Source #

The active attributes, including those for the statement currently being compiled.

Building Blocks

data TV t Source #

A typed variable, which we can turn into a typed expression, or use as the target for an assignment. This is used to aid in type safety when doing code generation, by keeping the types straight. It is still easy to cheat when you need to.

class MkTV t where Source #

A type class that helps ensuring that the type annotation in a TV is correct.

Methods

mkTV :: VName -> TV t Source #

Create a typed variable from a name and a dynamic type.

tvType :: TV t -> PrimType Source #

Extract type from a TV.

Instances

Instances details
MkTV Int16 Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

MkTV Int32 Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

MkTV Int64 Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

MkTV Int8 Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

MkTV Half Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

MkTV Bool Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

MkTV Double Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

MkTV Float Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

tvSize :: TV t -> DimSize Source #

Convert a typed variable to a size (a SubExp).

tvExp :: TV t -> TExp t Source #

Convert a typed variable to a similarly typed expression.

tvVar :: TV t -> VName Source #

Extract the underlying variable name from a typed variable.

class ToExp a where Source #

Compile things to Exp.

Methods

toExp :: a -> ImpM rep r op Exp Source #

Compile to an Exp, where the type (which must still be a primitive) is deduced monadically.

toExp' :: PrimType -> a -> Exp Source #

Compile where we know the type in advance.

Instances

Instances details
ToExp SubExp Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

Methods

toExp :: SubExp -> ImpM rep r op Exp Source #

toExp' :: PrimType -> SubExp -> Exp Source #

ToExp VName Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

Methods

toExp :: VName -> ImpM rep r op Exp Source #

toExp' :: PrimType -> VName -> Exp Source #

ToExp (PrimExp VName) Source # 
Instance details

Defined in Futhark.CodeGen.ImpGen

compileAlloc :: Mem rep inner => Pat (LetDec rep) -> SubExp -> Space -> ImpM rep r op () Source #

compileAlloc pat size space allocates n bytes of memory in space, writing the result to pat, which must contain a single memory-typed element.

everythingVolatile :: ImpM rep r op a -> ImpM rep r op a Source #

compileBody :: Pat (LetDec rep) -> Body rep -> ImpM rep r op () Source #

compileBody' :: [Param dec] -> Body rep -> ImpM rep r op () Source #

compileLoopBody :: Typed dec => [Param dec] -> Body rep -> ImpM rep r op () Source #

defCompileStms :: (Mem rep inner, FreeIn op) => Names -> Stms rep -> ImpM rep r op () -> ImpM rep r op () Source #

compileStms :: Names -> Stms rep -> ImpM rep r op () -> ImpM rep r op () Source #

compileExp :: Pat (LetDec rep) -> Exp rep -> ImpM rep r op () Source #

defCompileExp :: Mem rep inner => Pat (LetDec rep) -> Exp rep -> ImpM rep r op () Source #

copyDWIM :: VName -> [DimIndex (TExp Int64)] -> SubExp -> [DimIndex (TExp Int64)] -> ImpM rep r op () Source #

Copy from here to there; both destination and source be indexeded. If so, they better be arrays of enough dimensions. This function will generally just Do What I Mean, and Do The Right Thing. Both destination and source must be in scope.

copyDWIMFix :: VName -> [TExp Int64] -> SubExp -> [TExp Int64] -> ImpM rep r op () Source #

As copyDWIM, but implicitly DimFixes the indexes.

typeSize :: Type -> Count Bytes (TExp Int64) Source #

The number of bytes needed to represent the array in a straightforward contiguous format, as an Int64 expression.

inBounds :: Slice (TExp Int64) -> [TExp Int64] -> TExp Bool Source #

Is this indexing in-bounds for an array of the given shape? This is useful for things like scatter, which ignores out-of-bounds writes.

caseMatch :: [SubExp] -> [Maybe PrimValue] -> TExp Bool Source #

Generate an expression that is true if the subexpressions match the case pasttern.

Constructing code.

newVName :: MonadFreshNames m => String -> m VName Source #

Produce a fresh VName, using the given base name as a template.

dLParams :: Mem rep inner => [LParam rep] -> ImpM rep r op () Source #

dFParams :: Mem rep inner => [FParam rep] -> ImpM rep r op () Source #

addLoopVar :: VName -> IntType -> ImpM rep r op () Source #

Another hack.

dScope :: Mem rep inner => Maybe (Exp rep) -> Scope rep -> ImpM rep r op () Source #

dArray :: VName -> PrimType -> ShapeBase SubExp -> VName -> LMAD -> ImpM rep r op () Source #

dPrim :: MkTV t => String -> ImpM rep r op (TV t) Source #

Create TV of some fixed type.

dPrimS :: String -> PrimType -> ImpM rep r op VName Source #

Create variable of some provided dynamic type. You'll need this when you are compiling program code of Haskell-level unknown type. For other things, use other functions.

dPrimSV :: String -> PrimType -> ImpM rep r op (TV t) Source #

Create TV of some provided dynamic type. No guarantee that the dynamic type matches the inferred type.

dPrimVol :: String -> PrimType -> TExp t -> ImpM rep r op (TV t) Source #

dPrim_ :: VName -> PrimType -> ImpM rep r op () Source #

dPrimV_ :: VName -> TExp t -> ImpM rep r op () Source #

dPrimV :: String -> TExp t -> ImpM rep r op (TV t) Source #

dPrimVE :: String -> TExp t -> ImpM rep r op (TExp t) Source #

dIndexSpace :: [(VName, TExp Int64)] -> TExp Int64 -> ImpM rep r op () Source #

dIndexSpace f dims i computes a list of indices into an array with dimension dims given the flat index i. The resulting list will have the same size as dims. Intermediate results are passed to f.

dIndexSpace' :: String -> [TExp Int64] -> TExp Int64 -> ImpM rep r op [TExp Int64] Source #

Like dIndexSpace, but invent some new names for the indexes based on the given template.

sFor :: String -> TExp t -> (TExp t -> ImpM rep r op ()) -> ImpM rep r op () Source #

sWhile :: TExp Bool -> ImpM rep r op () -> ImpM rep r op () Source #

sComment :: Text -> ImpM rep r op () -> ImpM rep r op () Source #

Execute a code generation action, wrapping the generated code within a Comment with the given description.

sIf :: TExp Bool -> ImpM rep r op () -> ImpM rep r op () -> ImpM rep r op () Source #

sWhen :: TExp Bool -> ImpM rep r op () -> ImpM rep r op () Source #

sUnless :: TExp Bool -> ImpM rep r op () -> ImpM rep r op () Source #

sOp :: op -> ImpM rep r op () Source #

sAlloc_ :: VName -> Count Bytes (TExp Int64) -> Space -> ImpM rep r op () Source #

sArrayInMem :: String -> PrimType -> ShapeBase SubExp -> VName -> ImpM rep r op VName Source #

Declare an array in row-major order in the given memory block.

sAllocArray :: String -> PrimType -> ShapeBase SubExp -> Space -> ImpM rep r op VName Source #

Uses linear/iota index function.

sAllocArrayPerm :: String -> PrimType -> ShapeBase SubExp -> Space -> [Int] -> ImpM rep r op VName Source #

Like sAllocArray, but permute the in-memory representation of the indices as specified.

sStaticArray :: String -> PrimType -> ArrayContents -> ImpM rep r op VName Source #

Uses linear/iota index function.

sWrite :: VName -> [TExp Int64] -> Exp -> ImpM rep r op () Source #

sUpdate :: VName -> Slice (TExp Int64) -> SubExp -> ImpM rep r op () Source #

sLoopNest :: Shape -> ([TExp Int64] -> ImpM rep r op ()) -> ImpM rep r op () Source #

sLoopSpace :: [TExp t] -> ([TExp t] -> ImpM rep r op ()) -> ImpM rep r op () Source #

Create a sequential For loop covering a space of the given shape. The function is calling with the indexes for a given iteration.

(<--) :: TV t -> TExp t -> ImpM rep r op () infixl 3 Source #

Typed assignment.

(<~~) :: VName -> Exp -> ImpM rep r op () infixl 3 Source #

Untyped assignment.

function :: Name -> [Param] -> [Param] -> ImpM rep r op () -> ImpM rep r op () Source #

Constructing an ad-hoc function that does not correspond to any of the IR functions in the input program.

genConstants :: ImpM rep r op (Names, a) -> ImpM rep r op a Source #

Generate constants that get put outside of all functions. Will be executed at program startup. Action must return the names that should should be made available. This one has real sharp edges. Do not use inside subImpM. Do not use any variable from the context.

warn :: Located loc => loc -> [loc] -> Text -> ImpM rep r op () Source #

Emit a warning about something the user should be aware of.