| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Futhark.CodeGen.Backends.GenericC.Monad
Description
C code generator framework.
Synopsis
- data Operations op s = Operations {
- opsWriteScalar :: WriteScalar op s
- opsReadScalar :: ReadScalar op s
- opsAllocate :: Allocate op s
- opsDeallocate :: Deallocate op s
- opsCopy :: Copy op s
- opsMemoryType :: MemoryType op s
- opsCompiler :: OpCompiler op s
- opsError :: ErrorCompiler op s
- opsCall :: CallCompiler op s
- opsCopies :: Map (Space, Space) (DoCopy op s)
- opsFatMemory :: Bool
- opsCritical :: ([BlockItem], [BlockItem])
- opsGetParam :: Name -> (Exp, Exp)
- data Publicness
- type OpCompiler op s = op -> CompilerM op s ()
- type ErrorCompiler op s = ErrorMsg Exp -> String -> CompilerM op s ()
- type CallCompiler op s = [VName] -> Name -> [Exp] -> CompilerM op s ()
- type PointerQuals = String -> [TypeQual]
- type MemoryType op s = SpaceId -> CompilerM op s Type
- type WriteScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> Exp -> CompilerM op s ()
- writeScalarPointerWithQuals :: PointerQuals -> WriteScalar op s
- type ReadScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> CompilerM op s Exp
- readScalarPointerWithQuals :: PointerQuals -> ReadScalar op s
- type Allocate op s = Exp -> Exp -> Exp -> SpaceId -> CompilerM op s Exp
- type Deallocate op s = Exp -> Exp -> Exp -> SpaceId -> CompilerM op s ()
- data CopyBarrier
- type Copy op s = CopyBarrier -> Exp -> Exp -> Space -> Exp -> Exp -> Space -> Exp -> CompilerM op s ()
- type DoCopy op s = CopyBarrier -> PrimType -> [Count Elements Exp] -> Exp -> (Count Elements Exp, [Count Elements Exp]) -> Exp -> (Count Elements Exp, [Count Elements Exp]) -> CompilerM op s ()
- data CompilerM op s a
- data CompilerState s = CompilerState {
- compArrayTypes :: Map ArrayType Publicness
- compEarlyDecls :: DList Definition
- compNameSrc :: VNameSource
- compUserState :: s
- compHeaderDecls :: Map HeaderSection (DList Definition)
- compLibDecls :: DList Definition
- compCtxFields :: DList (Id, Type, Maybe Exp, Maybe (Stm, Stm))
- compClearItems :: DList BlockItem
- compDeclaredMem :: [(VName, Space)]
- compItems :: DList BlockItem
- compErrorLabel :: Maybe VName
- data CompilerEnv op s = CompilerEnv {
- envOperations :: Operations op s
- envCachedMem :: Map Exp VName
- envProvenance :: Provenance
- getUserState :: CompilerM op s s
- modifyUserState :: (s -> s) -> CompilerM op s ()
- generateProgramStruct :: CompilerM op s ()
- runCompilerM :: Operations op s -> VNameSource -> s -> CompilerM op s a -> (a, CompilerState s)
- inNewFunction :: CompilerM op s a -> CompilerM op s a
- failureJump :: CompilerM op s Stm
- memoryFailure :: CompilerM op s Stm
- errorLabel :: CompilerM op s [BlockItem]
- withoutPendingFailures :: CompilerM op s a -> CompilerM op s a
- cachingMemory :: Map VName Space -> ([BlockItem] -> [Stm] -> CompilerM op s a) -> CompilerM op s a
- volQuals :: Volatility -> [TypeQual]
- rawMem :: VName -> Space -> CompilerM op s Exp
- item :: BlockItem -> CompilerM op s ()
- items :: [BlockItem] -> CompilerM op s ()
- stm :: Stm -> CompilerM op s ()
- stms :: [Stm] -> CompilerM op s ()
- comment :: Text -> CompilerM op s ()
- decl :: InitGroup -> CompilerM op s ()
- headerDecl :: HeaderSection -> Definition -> CompilerM op s ()
- publicDef :: Text -> HeaderSection -> (Text -> (Definition, Definition)) -> CompilerM op s Text
- publicDef_ :: Text -> HeaderSection -> (Text -> (Definition, Definition)) -> CompilerM op s ()
- onClear :: BlockItem -> CompilerM op s ()
- data HeaderSection
- libDecl :: Definition -> CompilerM op s ()
- earlyDecl :: Definition -> CompilerM op s ()
- publicName :: Text -> CompilerM op s Text
- contextField :: Id -> Type -> Maybe Exp -> CompilerM op s ()
- contextFieldDyn :: Id -> Type -> Stm -> Stm -> CompilerM op s ()
- memToCType :: VName -> Space -> CompilerM op s Type
- cacheMem :: ToExp a => a -> CompilerM op s (Maybe VName)
- fatMemory :: Space -> CompilerM op s Bool
- rawMemCType :: Space -> CompilerM op s Type
- freeRawMem :: (ToExp a, ToExp b, ToExp c) => a -> b -> Space -> c -> CompilerM op s ()
- allocRawMem :: (ToExp a, ToExp b, ToExp c) => a -> b -> Space -> c -> CompilerM op s Exp
- fatMemType :: Space -> Type
- declAllocatedMem :: CompilerM op s [BlockItem]
- freeAllocatedMem :: CompilerM op s [BlockItem]
- collect :: CompilerM op s () -> CompilerM op s [BlockItem]
- collect' :: CompilerM op s a -> CompilerM op s (a, [BlockItem])
- contextType :: CompilerM op s Type
- configType :: CompilerM op s Type
- localProvenance :: Provenance -> CompilerM op s a -> CompilerM op s a
- askProvenance :: CompilerM op s Provenance
- provenanceExp :: CompilerM op s Exp
- copyMemoryDefaultSpace :: Exp -> Exp -> Exp -> Exp -> Exp -> CompilerM op s ()
- derefPointer :: Exp -> Exp -> Type -> Exp
- setMem :: (ToExp a, ToExp b) => a -> b -> Space -> CompilerM op s ()
- allocMem :: (ToExp a, ToExp b) => a -> b -> Space -> CompilerM op s Stm -> CompilerM op s ()
- unRefMem :: ToExp a => a -> Space -> CompilerM op s Stm -> CompilerM op s ()
- declMem :: VName -> Space -> CompilerM op s ()
- resetMem :: ToExp a => a -> Space -> CompilerM op s ()
- fatMemAlloc :: Space -> String
- fatMemSet :: Space -> String
- fatMemUnRef :: Space -> String
- criticalSection :: Operations op s -> [BlockItem] -> [BlockItem]
- module Futhark.CodeGen.Backends.SimpleRep
Pluggable compiler
data Operations op s Source #
Constructors
| Operations | |
Fields
| |
data Publicness Source #
Instances
| Show Publicness Source # | |
Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods showsPrec :: Int -> Publicness -> ShowS # show :: Publicness -> String # showList :: [Publicness] -> ShowS # | |
| Eq Publicness Source # | |
Defined in Futhark.CodeGen.Backends.GenericC.Monad | |
| Ord Publicness Source # | |
Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods compare :: Publicness -> Publicness -> Ordering # (<) :: Publicness -> Publicness -> Bool # (<=) :: Publicness -> Publicness -> Bool # (>) :: Publicness -> Publicness -> Bool # (>=) :: Publicness -> Publicness -> Bool # max :: Publicness -> Publicness -> Publicness # min :: Publicness -> Publicness -> Publicness # | |
type OpCompiler op s = op -> CompilerM op s () Source #
A substitute expression compiler, tried before the main compilation function.
type PointerQuals = String -> [TypeQual] Source #
The address space qualifiers for a pointer of the given type with the given annotation.
type MemoryType op s = SpaceId -> CompilerM op s Type Source #
The type of a memory block in the given memory space.
type WriteScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> Exp -> CompilerM op s () Source #
Write a scalar to the given memory block with the given element index and in the given memory space.
writeScalarPointerWithQuals :: PointerQuals -> WriteScalar op s Source #
type ReadScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> CompilerM op s Exp Source #
Read a scalar from the given memory block with the given element index and in the given memory space.
readScalarPointerWithQuals :: PointerQuals -> ReadScalar op s Source #
type Allocate op s = Exp -> Exp -> Exp -> SpaceId -> CompilerM op s Exp Source #
Allocate a memory block of the given size and with the given tag
in the given memory space, saving a reference in the given variable
name. Produces an int expression that is FUTHARK_SUCCESS if
the allocation succeeded.
type Deallocate op s = Exp -> Exp -> Exp -> SpaceId -> CompilerM op s () Source #
De-allocate the given memory block, with the given tag, with the given size,, which is in the given memory space.
data CopyBarrier Source #
Whether a copying operation should implicitly function as a barrier regarding further operations on the source. This is a rather subtle detail and is mostly useful for letting some device/GPU copies be asynchronous (#1664).
Constructors
| CopyBarrier | |
| CopyNoBarrier | Explicit context synchronisation should be done before the source or target is used. |
Instances
| Show CopyBarrier Source # | |
Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods showsPrec :: Int -> CopyBarrier -> ShowS # show :: CopyBarrier -> String # showList :: [CopyBarrier] -> ShowS # | |
| Eq CopyBarrier Source # | |
Defined in Futhark.CodeGen.Backends.GenericC.Monad | |
type Copy op s = CopyBarrier -> Exp -> Exp -> Space -> Exp -> Exp -> Space -> Exp -> CompilerM op s () Source #
Copy from one memory block to another.
type DoCopy op s = CopyBarrier -> PrimType -> [Count Elements Exp] -> Exp -> (Count Elements Exp, [Count Elements Exp]) -> Exp -> (Count Elements Exp, [Count Elements Exp]) -> CompilerM op s () Source #
Perform an Copy. It is expected that these functions are
each specialised on which spaces they operate on, so that is not part of their arguments.
Monadic compiler interface
data CompilerM op s a Source #
Instances
data CompilerState s Source #
Constructors
| CompilerState | |
Fields
| |
Instances
| MonadState (CompilerState s) (CompilerM op s) Source # | |
Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods get :: CompilerM op s (CompilerState s) # put :: CompilerState s -> CompilerM op s () # state :: (CompilerState s -> (a, CompilerState s)) -> CompilerM op s a # | |
data CompilerEnv op s Source #
Constructors
| CompilerEnv | |
Fields
| |
Instances
| MonadReader (CompilerEnv op s) (CompilerM op s) Source # | |
Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods ask :: CompilerM op s (CompilerEnv op s) # local :: (CompilerEnv op s -> CompilerEnv op s) -> CompilerM op s a -> CompilerM op s a # reader :: (CompilerEnv op s -> a) -> CompilerM op s a # | |
getUserState :: CompilerM op s s Source #
modifyUserState :: (s -> s) -> CompilerM op s () Source #
generateProgramStruct :: CompilerM op s () Source #
runCompilerM :: Operations op s -> VNameSource -> s -> CompilerM op s a -> (a, CompilerState s) Source #
inNewFunction :: CompilerM op s a -> CompilerM op s a Source #
Used when we, inside an existing CompilerM action, want to
generate code for a new function. Use this so that the compiler
understands that previously declared memory doesn't need to be
freed inside this action.
failureJump :: CompilerM op s Stm Source #
A statement that jumps to the next error label; see errorLabel. Don't do
this without setting err or similar first.
memoryFailure :: CompilerM op s Stm Source #
What do do after failing a memory operation: set err and jump as by
failureJump.
errorLabel :: CompilerM op s [BlockItem] Source #
The label that the failures since the previous error label jump to, if
there have been any. A failure thus jumps to the first error label that
follows it in the program text. Every function that may fail must end with an
error label just before its cleanup label.
withoutPendingFailures :: CompilerM op s a -> CompilerM op s a Source #
Run an action as if no failures had happened before it, and afterwards restore the failures that had. The action must place the error labels for its own failures.
cachingMemory :: Map VName Space -> ([BlockItem] -> [Stm] -> CompilerM op s a) -> CompilerM op s a Source #
volQuals :: Volatility -> [TypeQual] Source #
rawMem :: VName -> Space -> CompilerM op s Exp Source #
The C expression for the raw pointer (or array) underlying a memory block.
item :: BlockItem -> CompilerM op s () Source #
Insert a block item in the generated code at this point.
comment :: Text -> CompilerM op s () Source #
Insert a comment in the generated code at this point. The comment may contain linebreaks, and must not contain any comment markers.
headerDecl :: HeaderSection -> Definition -> CompilerM op s () Source #
publicDef :: Text -> HeaderSection -> (Text -> (Definition, Definition)) -> CompilerM op s Text Source #
Construct a publicly visible definition using the specified name as the template. The first returned definition is put in the header file, and the second is the implementation. Returns the public name.
publicDef_ :: Text -> HeaderSection -> (Text -> (Definition, Definition)) -> CompilerM op s () Source #
As publicDef, but ignores the public name.
data HeaderSection Source #
In which part of the header file we put the declaration. This is to ensure that the header file remains structured and readable.
Constructors
| ArrayDecl Name | |
| OpaqueTypeDecl Name | |
| OpaqueDecl Name | |
| EntryDecl | |
| MiscDecl | |
| InitDecl |
Instances
| Eq HeaderSection Source # | |
Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods (==) :: HeaderSection -> HeaderSection -> Bool # (/=) :: HeaderSection -> HeaderSection -> Bool # | |
| Ord HeaderSection Source # | |
Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods compare :: HeaderSection -> HeaderSection -> Ordering # (<) :: HeaderSection -> HeaderSection -> Bool # (<=) :: HeaderSection -> HeaderSection -> Bool # (>) :: HeaderSection -> HeaderSection -> Bool # (>=) :: HeaderSection -> HeaderSection -> Bool # max :: HeaderSection -> HeaderSection -> HeaderSection # min :: HeaderSection -> HeaderSection -> HeaderSection # | |
libDecl :: Definition -> CompilerM op s () Source #
earlyDecl :: Definition -> CompilerM op s () Source #
fatMemType :: Space -> Type Source #
declAllocatedMem :: CompilerM op s [BlockItem] Source #
freeAllocatedMem :: CompilerM op s [BlockItem] Source #
Unreference all declared memory. The result is placed after the
cleanup label, so a failure is recorded in err rather than
jumping to cleanup again.
contextType :: CompilerM op s Type Source #
The generated code must define a context struct with this name.
configType :: CompilerM op s Type Source #
The generated code must define a configuration struct with this name.
localProvenance :: Provenance -> CompilerM op s a -> CompilerM op s a Source #
Locally replace (not extend!) the provenance.
askProvenance :: CompilerM op s Provenance Source #
The provenance of the closest enclosing MetaProvenance.
provenanceExp :: CompilerM op s Exp Source #
A C expression corresponding to the current provenance.
Building Blocks
setMem :: (ToExp a, ToExp b) => a -> b -> Space -> CompilerM op s () Source #
Set dest to src. A failure is handled by memoryFailure.
allocMem :: (ToExp a, ToExp b) => a -> b -> Space -> CompilerM op s Stm -> CompilerM op s () Source #
unRefMem :: ToExp a => a -> Space -> CompilerM op s Stm -> CompilerM op s () Source #
The failure code is only generated if it is needed.
fatMemAlloc :: Space -> String Source #
fatMemUnRef :: Space -> String Source #
criticalSection :: Operations op s -> [BlockItem] -> [BlockItem] Source #