1 -- Operations on the global state of the vectorisation monad.
3 module Vectorise
.Monad
.Global
(
11 -- * Vectorisation declarations
12 lookupVectDecl
, noVectDecl
,
15 globalScalarVars
, isGlobalScalar
, globalScalarTyCons
,
19 defTyCon
, globalVectTyCons
,
33 import Vectorise
.Monad
.Base
47 -- Global Environment ---------------------------------------------------------
49 -- |Project something from the global environment.
51 readGEnv
:: (GlobalEnv
-> a
) -> VM a
52 readGEnv f
= VM
$ \_ genv lenv
-> return (Yes genv lenv
(f genv
))
54 -- |Set the value of the global environment.
56 setGEnv
:: GlobalEnv
-> VM
()
57 setGEnv genv
= VM
$ \_ _ lenv
-> return (Yes genv lenv
())
59 -- |Update the global environment using the provided function.
61 updGEnv
:: (GlobalEnv
-> GlobalEnv
) -> VM
()
62 updGEnv f
= VM
$ \_ genv lenv
-> return (Yes
(f genv
) lenv
())
65 -- Vars -----------------------------------------------------------------------
67 -- |Add a mapping between a global var and its vectorised version to the state.
69 defGlobalVar
:: Var
-> Var
-> VM
()
71 = do { traceVt
"add global var mapping:" (ppr v
<+> text
"-->" <+> ppr v
')
73 ; updGEnv
$ \env
-> env
{ global_vars
= extendVarEnv
(global_vars env
) v v
' }
77 -- Vectorisation declarations -------------------------------------------------
79 -- |Check whether a variable has a (non-scalar) vectorisation declaration.
81 lookupVectDecl
:: Var
-> VM
(Maybe (Type
, CoreExpr
))
82 lookupVectDecl var
= readGEnv
$ \env
-> lookupVarEnv
(global_vect_decls env
) var
84 -- |Check whether a variable has a 'NOVECTORISE' declaration.
86 noVectDecl
:: Var
-> VM
Bool
87 noVectDecl var
= readGEnv
$ \env
-> elemVarSet var
(global_novect_vars env
)
90 -- Scalars --------------------------------------------------------------------
92 -- |Get the set of global scalar variables.
94 globalScalarVars
:: VM VarSet
95 globalScalarVars
= readGEnv global_scalar_vars
97 -- |Check whether a given variable is in the set of global scalar variables.
99 isGlobalScalar
:: Var
-> VM
Bool
100 isGlobalScalar var
= readGEnv
$ \env
-> var `elemVarSet` global_scalar_vars env
102 -- |Get the set of global scalar type constructors including both those scalar type constructors
103 -- declared in an imported module and those declared in the current module.
105 globalScalarTyCons
:: VM NameSet
106 globalScalarTyCons
= readGEnv global_scalar_tycons
109 -- TyCons ---------------------------------------------------------------------
111 -- |Lookup the vectorised version of a `TyCon` from the global environment.
113 lookupTyCon
:: TyCon
-> VM
(Maybe TyCon
)
115 | isUnLiftedTyCon tc || isTupleTyCon tc
118 = readGEnv
$ \env
-> lookupNameEnv
(global_tycons env
) (tyConName tc
)
120 -- |Add a mapping between plain and vectorised `TyCon`s to the global environment.
122 defTyCon
:: TyCon
-> TyCon
-> VM
()
123 defTyCon tc tc
' = updGEnv
$ \env
->
124 env
{ global_tycons
= extendNameEnv
(global_tycons env
) (tyConName tc
) tc
' }
126 -- |Get the set of all vectorised type constructors.
128 globalVectTyCons
:: VM
(NameEnv TyCon
)
129 globalVectTyCons
= readGEnv global_tycons
132 -- DataCons -------------------------------------------------------------------
134 -- |Lookup the vectorised version of a `DataCon` from the global environment.
136 lookupDataCon
:: DataCon
-> VM
(Maybe DataCon
)
138 | isTupleTyCon
(dataConTyCon dc
)
142 = readGEnv
$ \env
-> lookupNameEnv
(global_datacons env
) (dataConName dc
)
144 -- |Add the mapping between plain and vectorised `DataCon`s to the global environment.
146 defDataCon
:: DataCon
-> DataCon
-> VM
()
147 defDataCon dc dc
' = updGEnv
$ \env
->
148 env
{ global_datacons
= extendNameEnv
(global_datacons env
) (dataConName dc
) dc
' }
151 -- 'PA' dictionaries ------------------------------------------------------------
153 -- |Lookup the 'PA' dfun of a vectorised type constructor in the global environment.
155 lookupTyConPA
:: TyCon
-> VM
(Maybe Var
)
157 = readGEnv
$ \env
-> lookupNameEnv
(global_pa_funs env
) (tyConName tc
)
159 -- |Associate vectorised type constructors with the dfun of their 'PA' instances in the global
162 defTyConPAs
:: [(TyCon
, Var
)] -> VM
()
163 defTyConPAs ps
= updGEnv
$ \env
->
164 env
{ global_pa_funs
= extendNameEnvList
(global_pa_funs env
)
165 [(tyConName tc
, pa
) |
(tc
, pa
) <- ps
] }
168 -- PR Dictionaries ------------------------------------------------------------
170 lookupTyConPR
:: TyCon
-> VM
(Maybe Var
)
171 lookupTyConPR tc
= readGEnv
$ \env
-> lookupNameEnv
(global_pr_funs env
) (tyConName tc
)