4 noV, tryV, maybeV, orElseV, localV, initV,
7 Builtins(..), paDictTyCon,
11 readGEnv, setGEnv, updGEnv,
14 readLEnv, setLEnv, updLEnv,
16 lookupTyCon, extendTyVarPA,
18 lookupInst, lookupFamInst
21 #include "HsVersions.h"
44 -- ----------------------------------------------------------------------------
45 -- Vectorisation monad
47 data Builtins = Builtins {
50 , closureTyCon :: TyCon
52 , applyClosureVar :: Var
53 , mkClosurePVar :: Var
54 , applyClosurePVar :: Var
56 , replicatePAVar :: Var
59 paDictTyCon :: Builtins -> TyCon
60 paDictTyCon = classTyCon . paClass
62 initBuiltins :: DsM Builtins
65 parrayTyCon <- dsLookupTyCon parrayTyConName
66 paClass <- dsLookupClass paClassName
67 closureTyCon <- dsLookupTyCon closureTyConName
69 mkClosureVar <- dsLookupGlobalId mkClosureName
70 applyClosureVar <- dsLookupGlobalId applyClosureName
71 mkClosurePVar <- dsLookupGlobalId mkClosurePName
72 applyClosurePVar <- dsLookupGlobalId applyClosurePName
73 lengthPAVar <- dsLookupGlobalId lengthPAName
74 replicatePAVar <- dsLookupGlobalId replicatePAName
77 parrayTyCon = parrayTyCon
79 , closureTyCon = closureTyCon
80 , mkClosureVar = mkClosureVar
81 , applyClosureVar = applyClosureVar
82 , mkClosurePVar = mkClosurePVar
83 , applyClosurePVar = applyClosurePVar
84 , lengthPAVar = lengthPAVar
85 , replicatePAVar = replicatePAVar
88 data GlobalEnv = GlobalEnv {
89 -- Mapping from global variables to their vectorised versions.
91 global_vars :: VarEnv CoreExpr
93 -- Exported variables which have a vectorised version
95 , global_exported_vars :: VarEnv (Var, Var)
97 -- Mapping from TyCons to their vectorised versions.
98 -- TyCons which do not have to be vectorised are mapped to
101 , global_tycons :: NameEnv TyCon
103 -- Mapping from TyCons to their PA dictionaries
105 , global_tycon_pa :: NameEnv CoreExpr
107 -- External package inst-env & home-package inst-env for class
110 , global_inst_env :: (InstEnv, InstEnv)
112 -- External package inst-env & home-package inst-env for family
115 , global_fam_inst_env :: FamInstEnvs
118 data LocalEnv = LocalEnv {
119 -- Mapping from local variables to their vectorised and
122 local_vars :: VarEnv (CoreExpr, CoreExpr)
124 -- Mapping from tyvars to their PA dictionaries
125 , local_tyvar_pa :: VarEnv CoreExpr
129 initGlobalEnv :: VectInfo -> (InstEnv, InstEnv) -> FamInstEnvs -> GlobalEnv
130 initGlobalEnv info instEnvs famInstEnvs
132 global_vars = mapVarEnv (Var . snd) $ vectInfoCCVar info
133 , global_exported_vars = emptyVarEnv
134 , global_tycons = mapNameEnv snd $ vectInfoCCTyCon info
135 , global_tycon_pa = emptyNameEnv
136 , global_inst_env = instEnvs
137 , global_fam_inst_env = famInstEnvs
140 emptyLocalEnv = LocalEnv {
141 local_vars = emptyVarEnv
142 , local_tyvar_pa = emptyVarEnv
146 updVectInfo :: GlobalEnv -> TypeEnv -> VectInfo -> VectInfo
147 updVectInfo env tyenv info
149 vectInfoCCVar = global_exported_vars env
150 , vectInfoCCTyCon = tc_env
153 tc_env = mkNameEnv [(tc_name, (tc,tc'))
154 | tc <- typeEnvTyCons tyenv
155 , let tc_name = tyConName tc
156 , Just tc' <- [lookupNameEnv (global_tycons env) tc_name]]
158 data VResult a = Yes GlobalEnv LocalEnv a | No
160 newtype VM a = VM { runVM :: Builtins -> GlobalEnv -> LocalEnv -> DsM (VResult a) }
162 instance Monad VM where
163 return x = VM $ \bi genv lenv -> return (Yes genv lenv x)
164 VM p >>= f = VM $ \bi genv lenv -> do
167 Yes genv' lenv' x -> runVM (f x) bi genv' lenv'
171 noV = VM $ \_ _ _ -> return No
173 tryV :: VM a -> VM (Maybe a)
174 tryV (VM p) = VM $ \bi genv lenv ->
178 Yes genv' lenv' x -> return (Yes genv' lenv' (Just x))
179 No -> return (Yes genv lenv Nothing)
181 maybeV :: VM (Maybe a) -> VM a
182 maybeV p = maybe noV return =<< p
184 orElseV :: VM a -> VM a -> VM a
185 orElseV p q = maybe q return =<< tryV p
187 localV :: VM a -> VM a
194 liftDs :: DsM a -> VM a
195 liftDs p = VM $ \bi genv lenv -> do { x <- p; return (Yes genv lenv x) }
197 builtin :: (Builtins -> a) -> VM a
198 builtin f = VM $ \bi genv lenv -> return (Yes genv lenv (f bi))
200 readGEnv :: (GlobalEnv -> a) -> VM a
201 readGEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f genv))
203 setGEnv :: GlobalEnv -> VM ()
204 setGEnv genv = VM $ \_ _ lenv -> return (Yes genv lenv ())
206 updGEnv :: (GlobalEnv -> GlobalEnv) -> VM ()
207 updGEnv f = VM $ \_ genv lenv -> return (Yes (f genv) lenv ())
209 readLEnv :: (LocalEnv -> a) -> VM a
210 readLEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f lenv))
212 setLEnv :: LocalEnv -> VM ()
213 setLEnv lenv = VM $ \_ genv _ -> return (Yes genv lenv ())
215 updLEnv :: (LocalEnv -> LocalEnv) -> VM ()
216 updLEnv f = VM $ \_ genv lenv -> return (Yes genv (f lenv) ())
218 getInstEnv :: VM (InstEnv, InstEnv)
219 getInstEnv = readGEnv global_inst_env
221 getFamInstEnv :: VM FamInstEnvs
222 getFamInstEnv = readGEnv global_fam_inst_env
224 newLocalVar :: FastString -> Type -> VM Var
227 u <- liftDs newUnique
228 return $ mkSysLocal fs u ty
230 newTyVar :: FastString -> Kind -> VM Var
233 u <- liftDs newUnique
234 return $ mkTyVar (mkSysTvName u fs) k
236 lookupTyCon :: TyCon -> VM (Maybe TyCon)
237 lookupTyCon tc = readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
239 extendTyVarPA :: Var -> CoreExpr -> VM ()
240 extendTyVarPA tv pa = updLEnv $ \env -> env { local_tyvar_pa = extendVarEnv (local_tyvar_pa env) tv pa }
242 -- Look up the dfun of a class instance.
244 -- The match must be unique - ie, match exactly one instance - but the
245 -- type arguments used for matching may be more specific than those of
246 -- the class instance declaration. The found class instances must not have
247 -- any type variables in the instance context that do not appear in the
248 -- instances head (i.e., no flexi vars); for details for what this means,
249 -- see the docs at InstEnv.lookupInstEnv.
251 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
253 = do { instEnv <- getInstEnv
254 ; case lookupInstEnv instEnv cls tys of
255 ([(inst, inst_tys)], _)
256 | noFlexiVar -> return (instanceDFunId inst, inst_tys')
257 | otherwise -> pprPanic "VectMonad.lookupInst: flexi var: "
258 (ppr $ mkTyConApp (classTyCon cls) tys)
260 inst_tys' = [ty | Right ty <- inst_tys]
261 noFlexiVar = all isRight inst_tys
263 pprPanic "VectMonad.lookupInst: not found: "
264 (ppr $ mkTyConApp (classTyCon cls) tys)
267 isRight (Left _) = False
268 isRight (Right _) = True
270 -- Look up the representation tycon of a family instance.
272 -- The match must be unique - ie, match exactly one instance - but the
273 -- type arguments used for matching may be more specific than those of
274 -- the family instance declaration.
276 -- Return the instance tycon and its type instance. For example, if we have
278 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
280 -- then we have a coercion (ie, type instance of family instance coercion)
282 -- :Co:R42T Int :: T [Int] ~ :R42T Int
284 -- which implies that :R42T was declared as 'data instance T [a]'.
286 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
287 lookupFamInst tycon tys
288 = ASSERT( isOpenTyCon tycon )
289 do { instEnv <- getFamInstEnv
290 ; case lookupFamInstEnv instEnv tycon tys of
291 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
293 pprPanic "VectMonad.lookupFamInst: not found: "
294 (ppr $ mkTyConApp tycon tys)
297 initV :: HscEnv -> ModGuts -> VectInfo -> VM a -> IO (Maybe (VectInfo, a))
298 initV hsc_env guts info p
300 eps <- hscEPS hsc_env
301 let famInstEnvs = (eps_fam_inst_env eps, mg_fam_inst_env guts)
302 let instEnvs = (eps_inst_env eps, mg_inst_env guts)
304 Just r <- initDs hsc_env (mg_module guts)
307 (go instEnvs famInstEnvs)
311 go instEnvs famInstEnvs =
313 builtins <- initBuiltins
314 r <- runVM p builtins (initGlobalEnv info instEnvs famInstEnvs)
317 Yes genv _ x -> return $ Just (new_info genv, x)
320 new_info genv = updVectInfo genv (mg_types guts) info