Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Constructing Futhark ASTs
This module re-exports and defines a bunch of building blocks for constructing fragments of Futhark ASTs. More importantly, it also contains a basic introduction on how to use them.
The Futhark.IR.Syntax module contains the core
AST definition. One important invariant is that all bound names in
a Futhark program must be globally unique. In principle, you
could use the facilities from Futhark.MonadFreshNames (or your
own bespoke source of unique names) to manually construct
expressions, statements, and entire ASTs. In practice, this would
be very tedious. Instead, we have defined a collection of building
blocks (centered around the MonadBuilder
type class) that permits
a more abstract way of generating code.
Constructing ASTs with these building blocks requires you to ensure that all free variables are in scope. See Futhark.IR.Prop.Scope.
MonadBuilder
A monad that implements MonadBuilder
tracks the statements added
so far, the current names in scope, and allows you to add
additional statements with addStm
. Any monad that implements
MonadBuilder
also implements the Rep
type family, which
indicates which rep it works with. Inside a MonadBuilder
we can
use collectStms
to gather up the Stms
added with addStm
in
some nested computation.
The BuilderT
monad (and its convenient Builder
version) provides
the simplest implementation of MonadBuilder
.
Higher-level building blocks
On top of the raw facilities provided by MonadBuilder
, we have
more convenient facilities. For example, letSubExp
lets us
conveniently create a Stm
for an Exp
that produces a single
value, and returns the (fresh) name for the resulting variable:
z <- letExp "z" $ BasicOp $ BinOp (Add Int32) (Var x) (Var y)
Examples
The Futhark.Transform.FirstOrderTransform module is a (relatively) simple example of how to use these components. As are some of the high-level building blocks in this very module.
Synopsis
- letSubExp :: MonadBuilder m => String -> Exp (Rep m) -> m SubExp
- letSubExps :: MonadBuilder m => String -> [Exp (Rep m)] -> m [SubExp]
- letExp :: MonadBuilder m => String -> Exp (Rep m) -> m VName
- letTupExp :: MonadBuilder m => String -> Exp (Rep m) -> m [VName]
- letTupExp' :: MonadBuilder m => String -> Exp (Rep m) -> m [SubExp]
- letInPlace :: MonadBuilder m => String -> VName -> Slice SubExp -> Exp (Rep m) -> m VName
- eSubExp :: MonadBuilder m => SubExp -> m (Exp (Rep m))
- eParam :: MonadBuilder m => Param t -> m (Exp (Rep m))
- eIf :: (MonadBuilder m, BranchType (Rep m) ~ ExtType) => m (Exp (Rep m)) -> m (Body (Rep m)) -> m (Body (Rep m)) -> m (Exp (Rep m))
- eIf' :: (MonadBuilder m, BranchType (Rep m) ~ ExtType) => m (Exp (Rep m)) -> m (Body (Rep m)) -> m (Body (Rep m)) -> IfSort -> m (Exp (Rep m))
- eBinOp :: MonadBuilder m => BinOp -> m (Exp (Rep m)) -> m (Exp (Rep m)) -> m (Exp (Rep m))
- eCmpOp :: MonadBuilder m => CmpOp -> m (Exp (Rep m)) -> m (Exp (Rep m)) -> m (Exp (Rep m))
- eConvOp :: MonadBuilder m => ConvOp -> m (Exp (Rep m)) -> m (Exp (Rep m))
- eSignum :: MonadBuilder m => m (Exp (Rep m)) -> m (Exp (Rep m))
- eCopy :: MonadBuilder m => m (Exp (Rep m)) -> m (Exp (Rep m))
- eBody :: MonadBuilder m => [m (Exp (Rep m))] -> m (Body (Rep m))
- eLambda :: MonadBuilder m => Lambda (Rep m) -> [m (Exp (Rep m))] -> m [SubExpRes]
- eRoundToMultipleOf :: MonadBuilder m => IntType -> m (Exp (Rep m)) -> m (Exp (Rep m)) -> m (Exp (Rep m))
- eSliceArray :: MonadBuilder m => Int -> VName -> m (Exp (Rep m)) -> m (Exp (Rep m)) -> m (Exp (Rep m))
- eBlank :: MonadBuilder m => Type -> m (Exp (Rep m))
- eAll :: MonadBuilder m => [SubExp] -> m (Exp (Rep m))
- eOutOfBounds :: MonadBuilder m => VName -> [m (Exp (Rep m))] -> m (Exp (Rep m))
- asIntZ :: MonadBuilder m => IntType -> SubExp -> m SubExp
- asIntS :: MonadBuilder m => IntType -> SubExp -> m SubExp
- resultBody :: Buildable rep => [SubExp] -> Body rep
- resultBodyM :: MonadBuilder m => [SubExp] -> m (Body (Rep m))
- insertStmsM :: MonadBuilder m => m (Body (Rep m)) -> m (Body (Rep m))
- buildBody :: MonadBuilder m => m (Result, a) -> m (Body (Rep m), a)
- buildBody_ :: MonadBuilder m => m Result -> m (Body (Rep m))
- mapResult :: Buildable rep => (Result -> Body rep) -> Body rep -> Body rep
- foldBinOp :: MonadBuilder m => BinOp -> SubExp -> [SubExp] -> m (Exp (Rep m))
- binOpLambda :: (MonadBuilder m, Buildable (Rep m)) => BinOp -> PrimType -> m (Lambda (Rep m))
- cmpOpLambda :: (MonadBuilder m, Buildable (Rep m)) => CmpOp -> m (Lambda (Rep m))
- mkLambda :: MonadBuilder m => [LParam (Rep m)] -> m Result -> m (Lambda (Rep m))
- sliceDim :: SubExp -> DimIndex SubExp
- fullSlice :: Type -> [DimIndex SubExp] -> Slice SubExp
- fullSliceNum :: Num d => [d] -> [DimIndex d] -> Slice d
- isFullSlice :: Shape -> Slice SubExp -> Bool
- sliceAt :: Type -> Int -> [DimIndex SubExp] -> Slice SubExp
- ifCommon :: [Type] -> IfDec ExtType
- module Futhark.Builder
- instantiateShapes :: Monad m => (Int -> m SubExp) -> [TypeBase ExtShape u] -> m [TypeBase Shape u]
- instantiateShapes' :: [VName] -> [TypeBase ExtShape u] -> [TypeBase Shape u]
- removeExistentials :: ExtType -> Type -> Type
- simpleMkLetNames :: (ExpDec rep ~ (), LetDec rep ~ Type, MonadFreshNames m, TypedOp (Op rep), HasScope rep m) => [VName] -> Exp rep -> m (Stm rep)
- class ToExp a where
- toExp :: MonadBuilder m => a -> m (Exp (Rep m))
- toSubExp :: (MonadBuilder m, ToExp a) => String -> a -> m SubExp
Documentation
letSubExps :: MonadBuilder m => String -> [Exp (Rep m)] -> m [SubExp] Source #
letTupExp :: MonadBuilder m => String -> Exp (Rep m) -> m [VName] Source #
Only returns those pattern names that are not used in the pattern itself (the "non-existential" part, you could say).
letTupExp' :: MonadBuilder m => String -> Exp (Rep m) -> m [SubExp] Source #
eIf :: (MonadBuilder m, BranchType (Rep m) ~ ExtType) => m (Exp (Rep m)) -> m (Body (Rep m)) -> m (Body (Rep m)) -> m (Exp (Rep m)) Source #
eIf' :: (MonadBuilder m, BranchType (Rep m) ~ ExtType) => m (Exp (Rep m)) -> m (Body (Rep m)) -> m (Body (Rep m)) -> IfSort -> m (Exp (Rep m)) Source #
eRoundToMultipleOf :: MonadBuilder m => IntType -> m (Exp (Rep m)) -> m (Exp (Rep m)) -> m (Exp (Rep m)) Source #
eSliceArray :: MonadBuilder m => Int -> VName -> m (Exp (Rep m)) -> m (Exp (Rep m)) -> m (Exp (Rep m)) Source #
Construct an Index
expressions that slices an array with unit stride.
eBlank :: MonadBuilder m => Type -> m (Exp (Rep m)) Source #
Construct an unspecified value of the given type.
eOutOfBounds :: MonadBuilder m => VName -> [m (Exp (Rep m))] -> m (Exp (Rep m)) Source #
Are these indexes out-of-bounds for the array?
asIntZ :: MonadBuilder m => IntType -> SubExp -> m SubExp Source #
Zero-extend to the given integer type.
asIntS :: MonadBuilder m => IntType -> SubExp -> m SubExp Source #
Sign-extend to the given integer type.
resultBody :: Buildable rep => [SubExp] -> Body rep Source #
Conveniently construct a body that contains no bindings.
resultBodyM :: MonadBuilder m => [SubExp] -> m (Body (Rep m)) Source #
Conveniently construct a body that contains no bindings - but this time, monadically!
insertStmsM :: MonadBuilder m => m (Body (Rep m)) -> m (Body (Rep m)) Source #
Evaluate the action, producing a body, then wrap it in all the
bindings it created using addStm
.
buildBody_ :: MonadBuilder m => m Result -> m (Body (Rep m)) Source #
As buildBody
, but there is no auxiliary value.
mapResult :: Buildable rep => (Result -> Body rep) -> Body rep -> Body rep Source #
Change that result where evaluation of the body would stop. Also change type annotations at branches.
foldBinOp :: MonadBuilder m => BinOp -> SubExp -> [SubExp] -> m (Exp (Rep m)) Source #
Apply a binary operator to several subexpressions. A left-fold.
binOpLambda :: (MonadBuilder m, Buildable (Rep m)) => BinOp -> PrimType -> m (Lambda (Rep m)) Source #
Create a two-parameter lambda whose body applies the given binary operation to its arguments. It is assumed that both argument and result types are the same. (This assumption should be fixed at some point.)
cmpOpLambda :: (MonadBuilder m, Buildable (Rep m)) => CmpOp -> m (Lambda (Rep m)) Source #
As binOpLambda
, but for CmpOp
s.
mkLambda :: MonadBuilder m => [LParam (Rep m)] -> m Result -> m (Lambda (Rep m)) Source #
Easily construct a Lambda
within a MonadBuilder
.
fullSliceNum :: Num d => [d] -> [DimIndex d] -> Slice d Source #
Like fullSlice
, but the dimensions are simply numeric.
isFullSlice :: Shape -> Slice SubExp -> Bool Source #
Does the slice describe the full size of the array? The most
obvious such slice is one that DimSlice
s the full span of every
dimension, but also one that fixes all unit dimensions.
module Futhark.Builder
Result types
instantiateShapes :: Monad m => (Int -> m SubExp) -> [TypeBase ExtShape u] -> m [TypeBase Shape u] Source #
Instantiate all existential parts dimensions of the given
type, using a monadic action to create the necessary SubExp
s.
You should call this function within some monad that allows you to
collect the actions performed (say, Writer
).
Convenience
simpleMkLetNames :: (ExpDec rep ~ (), LetDec rep ~ Type, MonadFreshNames m, TypedOp (Op rep), HasScope rep m) => [VName] -> Exp rep -> m (Stm rep) Source #
Can be used as the definition of mkLetNames
for a Buildable
instance for simple representations.
Instances of this class can be converted to Futhark expressions
within a MonadBuilder
.