4 noV, tryV, maybeV, orElseV, localV, initV,
7 Builtins(..), paDictTyCon,
11 readGEnv, setGEnv, updGEnv,
14 readLEnv, setLEnv, updLEnv,
17 lookupTyVarPA, extendTyVarPA, deleteTyVarPA,
19 lookupInst, lookupFamInst
22 #include "HsVersions.h"
45 -- ----------------------------------------------------------------------------
46 -- Vectorisation monad
48 data Builtins = Builtins {
51 , closureTyCon :: TyCon
53 , applyClosureVar :: Var
54 , mkClosurePVar :: Var
55 , applyClosurePVar :: Var
57 , replicatePAVar :: Var
60 paDictTyCon :: Builtins -> TyCon
61 paDictTyCon = classTyCon . paClass
63 initBuiltins :: DsM Builtins
66 parrayTyCon <- dsLookupTyCon parrayTyConName
67 paClass <- dsLookupClass paClassName
68 closureTyCon <- dsLookupTyCon closureTyConName
70 mkClosureVar <- dsLookupGlobalId mkClosureName
71 applyClosureVar <- dsLookupGlobalId applyClosureName
72 mkClosurePVar <- dsLookupGlobalId mkClosurePName
73 applyClosurePVar <- dsLookupGlobalId applyClosurePName
74 lengthPAVar <- dsLookupGlobalId lengthPAName
75 replicatePAVar <- dsLookupGlobalId replicatePAName
78 parrayTyCon = parrayTyCon
80 , closureTyCon = closureTyCon
81 , mkClosureVar = mkClosureVar
82 , applyClosureVar = applyClosureVar
83 , mkClosurePVar = mkClosurePVar
84 , applyClosurePVar = applyClosurePVar
85 , lengthPAVar = lengthPAVar
86 , replicatePAVar = replicatePAVar
89 data GlobalEnv = GlobalEnv {
90 -- Mapping from global variables to their vectorised versions.
92 global_vars :: VarEnv CoreExpr
94 -- Exported variables which have a vectorised version
96 , global_exported_vars :: VarEnv (Var, Var)
98 -- Mapping from TyCons to their vectorised versions.
99 -- TyCons which do not have to be vectorised are mapped to
102 , global_tycons :: NameEnv TyCon
104 -- Mapping from TyCons to their PA dictionaries
106 , global_tycon_pa :: NameEnv CoreExpr
108 -- External package inst-env & home-package inst-env for class
111 , global_inst_env :: (InstEnv, InstEnv)
113 -- External package inst-env & home-package inst-env for family
116 , global_fam_inst_env :: FamInstEnvs
119 data LocalEnv = LocalEnv {
120 -- Mapping from local variables to their vectorised and
123 local_vars :: VarEnv (CoreExpr, CoreExpr)
125 -- Mapping from tyvars to their PA dictionaries
126 , local_tyvar_pa :: VarEnv CoreExpr
130 initGlobalEnv :: VectInfo -> (InstEnv, InstEnv) -> FamInstEnvs -> GlobalEnv
131 initGlobalEnv info instEnvs famInstEnvs
133 global_vars = mapVarEnv (Var . snd) $ vectInfoCCVar info
134 , global_exported_vars = emptyVarEnv
135 , global_tycons = mapNameEnv snd $ vectInfoCCTyCon info
136 , global_tycon_pa = emptyNameEnv
137 , global_inst_env = instEnvs
138 , global_fam_inst_env = famInstEnvs
141 emptyLocalEnv = LocalEnv {
142 local_vars = emptyVarEnv
143 , local_tyvar_pa = emptyVarEnv
147 updVectInfo :: GlobalEnv -> TypeEnv -> VectInfo -> VectInfo
148 updVectInfo env tyenv info
150 vectInfoCCVar = global_exported_vars env
151 , vectInfoCCTyCon = tc_env
154 tc_env = mkNameEnv [(tc_name, (tc,tc'))
155 | tc <- typeEnvTyCons tyenv
156 , let tc_name = tyConName tc
157 , Just tc' <- [lookupNameEnv (global_tycons env) tc_name]]
159 data VResult a = Yes GlobalEnv LocalEnv a | No
161 newtype VM a = VM { runVM :: Builtins -> GlobalEnv -> LocalEnv -> DsM (VResult a) }
163 instance Monad VM where
164 return x = VM $ \bi genv lenv -> return (Yes genv lenv x)
165 VM p >>= f = VM $ \bi genv lenv -> do
168 Yes genv' lenv' x -> runVM (f x) bi genv' lenv'
172 noV = VM $ \_ _ _ -> return No
174 tryV :: VM a -> VM (Maybe a)
175 tryV (VM p) = VM $ \bi genv lenv ->
179 Yes genv' lenv' x -> return (Yes genv' lenv' (Just x))
180 No -> return (Yes genv lenv Nothing)
182 maybeV :: VM (Maybe a) -> VM a
183 maybeV p = maybe noV return =<< p
185 orElseV :: VM a -> VM a -> VM a
186 orElseV p q = maybe q return =<< tryV p
188 localV :: VM a -> VM a
195 liftDs :: DsM a -> VM a
196 liftDs p = VM $ \bi genv lenv -> do { x <- p; return (Yes genv lenv x) }
198 builtin :: (Builtins -> a) -> VM a
199 builtin f = VM $ \bi genv lenv -> return (Yes genv lenv (f bi))
201 readGEnv :: (GlobalEnv -> a) -> VM a
202 readGEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f genv))
204 setGEnv :: GlobalEnv -> VM ()
205 setGEnv genv = VM $ \_ _ lenv -> return (Yes genv lenv ())
207 updGEnv :: (GlobalEnv -> GlobalEnv) -> VM ()
208 updGEnv f = VM $ \_ genv lenv -> return (Yes (f genv) lenv ())
210 readLEnv :: (LocalEnv -> a) -> VM a
211 readLEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f lenv))
213 setLEnv :: LocalEnv -> VM ()
214 setLEnv lenv = VM $ \_ genv _ -> return (Yes genv lenv ())
216 updLEnv :: (LocalEnv -> LocalEnv) -> VM ()
217 updLEnv f = VM $ \_ genv lenv -> return (Yes genv (f lenv) ())
219 getInstEnv :: VM (InstEnv, InstEnv)
220 getInstEnv = readGEnv global_inst_env
222 getFamInstEnv :: VM FamInstEnvs
223 getFamInstEnv = readGEnv global_fam_inst_env
225 newLocalVar :: FastString -> Type -> VM Var
228 u <- liftDs newUnique
229 return $ mkSysLocal fs u ty
231 newTyVar :: FastString -> Kind -> VM Var
234 u <- liftDs newUnique
235 return $ mkTyVar (mkSysTvName u fs) k
237 lookupTyCon :: TyCon -> VM (Maybe TyCon)
238 lookupTyCon tc = readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
240 lookupTyVarPA :: Var -> VM (Maybe CoreExpr)
241 lookupTyVarPA tv = readLEnv $ \env -> lookupVarEnv (local_tyvar_pa env) tv
243 extendTyVarPA :: Var -> CoreExpr -> VM ()
244 extendTyVarPA tv pa = updLEnv $ \env -> env { local_tyvar_pa = extendVarEnv (local_tyvar_pa env) tv pa }
246 deleteTyVarPA :: Var -> VM ()
247 deleteTyVarPA tv = updLEnv $ \env -> env { local_tyvar_pa = delVarEnv (local_tyvar_pa env) tv }
249 -- Look up the dfun of a class instance.
251 -- The match must be unique - ie, match exactly one instance - but the
252 -- type arguments used for matching may be more specific than those of
253 -- the class instance declaration. The found class instances must not have
254 -- any type variables in the instance context that do not appear in the
255 -- instances head (i.e., no flexi vars); for details for what this means,
256 -- see the docs at InstEnv.lookupInstEnv.
258 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
260 = do { instEnv <- getInstEnv
261 ; case lookupInstEnv instEnv cls tys of
262 ([(inst, inst_tys)], _)
263 | noFlexiVar -> return (instanceDFunId inst, inst_tys')
264 | otherwise -> pprPanic "VectMonad.lookupInst: flexi var: "
265 (ppr $ mkTyConApp (classTyCon cls) tys)
267 inst_tys' = [ty | Right ty <- inst_tys]
268 noFlexiVar = all isRight inst_tys
272 isRight (Left _) = False
273 isRight (Right _) = True
275 -- Look up the representation tycon of a family instance.
277 -- The match must be unique - ie, match exactly one instance - but the
278 -- type arguments used for matching may be more specific than those of
279 -- the family instance declaration.
281 -- Return the instance tycon and its type instance. For example, if we have
283 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
285 -- then we have a coercion (ie, type instance of family instance coercion)
287 -- :Co:R42T Int :: T [Int] ~ :R42T Int
289 -- which implies that :R42T was declared as 'data instance T [a]'.
291 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
292 lookupFamInst tycon tys
293 = ASSERT( isOpenTyCon tycon )
294 do { instEnv <- getFamInstEnv
295 ; case lookupFamInstEnv instEnv tycon tys of
296 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
298 pprPanic "VectMonad.lookupFamInst: not found: "
299 (ppr $ mkTyConApp tycon tys)
302 initV :: HscEnv -> ModGuts -> VectInfo -> VM a -> IO (Maybe (VectInfo, a))
303 initV hsc_env guts info p
305 eps <- hscEPS hsc_env
306 let famInstEnvs = (eps_fam_inst_env eps, mg_fam_inst_env guts)
307 let instEnvs = (eps_inst_env eps, mg_inst_env guts)
309 Just r <- initDs hsc_env (mg_module guts)
312 (go instEnvs famInstEnvs)
316 go instEnvs famInstEnvs =
318 builtins <- initBuiltins
319 r <- runVM p builtins (initGlobalEnv info instEnvs famInstEnvs)
322 Yes genv _ x -> return $ Just (new_info genv, x)
325 new_info genv = updVectInfo genv (mg_types guts) info