4 noV, tryV, maybeV, orElseV, localV, closedV, 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
129 , local_bindings :: [(Var, CoreExpr)]
133 initGlobalEnv :: VectInfo -> (InstEnv, InstEnv) -> FamInstEnvs -> GlobalEnv
134 initGlobalEnv info instEnvs famInstEnvs
136 global_vars = mapVarEnv (Var . snd) $ vectInfoVar info
137 , global_exported_vars = emptyVarEnv
138 , global_tycons = mapNameEnv snd $ vectInfoTyCon info
139 , global_tycon_pa = emptyNameEnv
140 , global_inst_env = instEnvs
141 , global_fam_inst_env = famInstEnvs
144 emptyLocalEnv = LocalEnv {
145 local_vars = emptyVarEnv
146 , local_tyvar_pa = emptyVarEnv
147 , local_bindings = []
151 updVectInfo :: GlobalEnv -> TypeEnv -> VectInfo -> VectInfo
152 updVectInfo env tyenv info
154 vectInfoVar = global_exported_vars env
155 , vectInfoTyCon = tc_env
158 tc_env = mkNameEnv [(tc_name, (tc,tc'))
159 | tc <- typeEnvTyCons tyenv
160 , let tc_name = tyConName tc
161 , Just tc' <- [lookupNameEnv (global_tycons env) tc_name]]
163 data VResult a = Yes GlobalEnv LocalEnv a | No
165 newtype VM a = VM { runVM :: Builtins -> GlobalEnv -> LocalEnv -> DsM (VResult a) }
167 instance Monad VM where
168 return x = VM $ \bi genv lenv -> return (Yes genv lenv x)
169 VM p >>= f = VM $ \bi genv lenv -> do
172 Yes genv' lenv' x -> runVM (f x) bi genv' lenv'
176 noV = VM $ \_ _ _ -> return No
178 tryV :: VM a -> VM (Maybe a)
179 tryV (VM p) = VM $ \bi genv lenv ->
183 Yes genv' lenv' x -> return (Yes genv' lenv' (Just x))
184 No -> return (Yes genv lenv Nothing)
186 maybeV :: VM (Maybe a) -> VM a
187 maybeV p = maybe noV return =<< p
189 orElseV :: VM a -> VM a -> VM a
190 orElseV p q = maybe q return =<< tryV p
192 localV :: VM a -> VM a
199 closedV :: VM a -> VM a
202 setLEnv emptyLocalEnv
207 liftDs :: DsM a -> VM a
208 liftDs p = VM $ \bi genv lenv -> do { x <- p; return (Yes genv lenv x) }
210 builtin :: (Builtins -> a) -> VM a
211 builtin f = VM $ \bi genv lenv -> return (Yes genv lenv (f bi))
213 readGEnv :: (GlobalEnv -> a) -> VM a
214 readGEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f genv))
216 setGEnv :: GlobalEnv -> VM ()
217 setGEnv genv = VM $ \_ _ lenv -> return (Yes genv lenv ())
219 updGEnv :: (GlobalEnv -> GlobalEnv) -> VM ()
220 updGEnv f = VM $ \_ genv lenv -> return (Yes (f genv) lenv ())
222 readLEnv :: (LocalEnv -> a) -> VM a
223 readLEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f lenv))
225 setLEnv :: LocalEnv -> VM ()
226 setLEnv lenv = VM $ \_ genv _ -> return (Yes genv lenv ())
228 updLEnv :: (LocalEnv -> LocalEnv) -> VM ()
229 updLEnv f = VM $ \_ genv lenv -> return (Yes genv (f lenv) ())
231 getInstEnv :: VM (InstEnv, InstEnv)
232 getInstEnv = readGEnv global_inst_env
234 getFamInstEnv :: VM FamInstEnvs
235 getFamInstEnv = readGEnv global_fam_inst_env
237 newLocalVar :: FastString -> Type -> VM Var
240 u <- liftDs newUnique
241 return $ mkSysLocal fs u ty
243 newTyVar :: FastString -> Kind -> VM Var
246 u <- liftDs newUnique
247 return $ mkTyVar (mkSysTvName u fs) k
249 lookupTyCon :: TyCon -> VM (Maybe TyCon)
250 lookupTyCon tc = readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
252 lookupTyVarPA :: Var -> VM (Maybe CoreExpr)
253 lookupTyVarPA tv = readLEnv $ \env -> lookupVarEnv (local_tyvar_pa env) tv
255 extendTyVarPA :: Var -> CoreExpr -> VM ()
256 extendTyVarPA tv pa = updLEnv $ \env -> env { local_tyvar_pa = extendVarEnv (local_tyvar_pa env) tv pa }
258 deleteTyVarPA :: Var -> VM ()
259 deleteTyVarPA tv = updLEnv $ \env -> env { local_tyvar_pa = delVarEnv (local_tyvar_pa env) tv }
261 -- Look up the dfun of a class instance.
263 -- The match must be unique - ie, match exactly one instance - but the
264 -- type arguments used for matching may be more specific than those of
265 -- the class instance declaration. The found class instances must not have
266 -- any type variables in the instance context that do not appear in the
267 -- instances head (i.e., no flexi vars); for details for what this means,
268 -- see the docs at InstEnv.lookupInstEnv.
270 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
272 = do { instEnv <- getInstEnv
273 ; case lookupInstEnv instEnv cls tys of
274 ([(inst, inst_tys)], _)
275 | noFlexiVar -> return (instanceDFunId inst, inst_tys')
276 | otherwise -> pprPanic "VectMonad.lookupInst: flexi var: "
277 (ppr $ mkTyConApp (classTyCon cls) tys)
279 inst_tys' = [ty | Right ty <- inst_tys]
280 noFlexiVar = all isRight inst_tys
284 isRight (Left _) = False
285 isRight (Right _) = True
287 -- Look up the representation tycon of a family instance.
289 -- The match must be unique - ie, match exactly one instance - but the
290 -- type arguments used for matching may be more specific than those of
291 -- the family instance declaration.
293 -- Return the instance tycon and its type instance. For example, if we have
295 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
297 -- then we have a coercion (ie, type instance of family instance coercion)
299 -- :Co:R42T Int :: T [Int] ~ :R42T Int
301 -- which implies that :R42T was declared as 'data instance T [a]'.
303 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
304 lookupFamInst tycon tys
305 = ASSERT( isOpenTyCon tycon )
306 do { instEnv <- getFamInstEnv
307 ; case lookupFamInstEnv instEnv tycon tys of
308 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
310 pprPanic "VectMonad.lookupFamInst: not found: "
311 (ppr $ mkTyConApp tycon tys)
314 initV :: HscEnv -> ModGuts -> VectInfo -> VM a -> IO (Maybe (VectInfo, a))
315 initV hsc_env guts info p
317 eps <- hscEPS hsc_env
318 let famInstEnvs = (eps_fam_inst_env eps, mg_fam_inst_env guts)
319 let instEnvs = (eps_inst_env eps, mg_inst_env guts)
321 Just r <- initDs hsc_env (mg_module guts)
324 (go instEnvs famInstEnvs)
328 go instEnvs famInstEnvs =
330 builtins <- initBuiltins
331 r <- runVM p builtins (initGlobalEnv info instEnvs famInstEnvs)
334 Yes genv _ x -> return $ Just (new_info genv, x)
337 new_info genv = updVectInfo genv (mg_types guts) info