5 noV, tryV, maybeV, orElseV, fixV, localV, closedV, initV,
6 cloneName, newLocalVar, newTyVar,
8 Builtins(..), paDictTyCon,
12 readGEnv, setGEnv, updGEnv,
15 readLEnv, setLEnv, updLEnv,
17 defGlobalVar, lookupVar,
19 lookupTyVarPA, defLocalTyVar, defLocalTyVarWithPA, localTyVars,
21 lookupInst, lookupFamInst
24 #include "HsVersions.h"
48 import Control.Monad ( liftM )
50 data Scope a b = Global a | Local b
52 -- ----------------------------------------------------------------------------
53 -- Vectorisation monad
55 data Builtins = Builtins {
58 , closureTyCon :: TyCon
60 , applyClosureVar :: Var
61 , mkClosurePVar :: Var
62 , applyClosurePVar :: Var
64 , replicatePAVar :: Var
67 paDictTyCon :: Builtins -> TyCon
68 paDictTyCon = classTyCon . paClass
70 initBuiltins :: DsM Builtins
73 parrayTyCon <- dsLookupTyCon parrayTyConName
74 paClass <- dsLookupClass paClassName
75 closureTyCon <- dsLookupTyCon closureTyConName
77 mkClosureVar <- dsLookupGlobalId mkClosureName
78 applyClosureVar <- dsLookupGlobalId applyClosureName
79 mkClosurePVar <- dsLookupGlobalId mkClosurePName
80 applyClosurePVar <- dsLookupGlobalId applyClosurePName
81 lengthPAVar <- dsLookupGlobalId lengthPAName
82 replicatePAVar <- dsLookupGlobalId replicatePAName
85 parrayTyCon = parrayTyCon
87 , closureTyCon = closureTyCon
88 , mkClosureVar = mkClosureVar
89 , applyClosureVar = applyClosureVar
90 , mkClosurePVar = mkClosurePVar
91 , applyClosurePVar = applyClosurePVar
92 , lengthPAVar = lengthPAVar
93 , replicatePAVar = replicatePAVar
96 data GlobalEnv = GlobalEnv {
97 -- Mapping from global variables to their vectorised versions.
99 global_vars :: VarEnv CoreExpr
101 -- Exported variables which have a vectorised version
103 , global_exported_vars :: VarEnv (Var, Var)
105 -- Mapping from TyCons to their vectorised versions.
106 -- TyCons which do not have to be vectorised are mapped to
109 , global_tycons :: NameEnv TyCon
111 -- Mapping from TyCons to their PA dictionaries
113 , global_tycon_pa :: NameEnv CoreExpr
115 -- External package inst-env & home-package inst-env for class
118 , global_inst_env :: (InstEnv, InstEnv)
120 -- External package inst-env & home-package inst-env for family
123 , global_fam_inst_env :: FamInstEnvs
126 , global_bindings :: [(Var, CoreExpr)]
129 data LocalEnv = LocalEnv {
130 -- Mapping from local variables to their vectorised and
133 local_vars :: VarEnv (CoreExpr, CoreExpr)
135 -- In-scope type variables
137 , local_tyvars :: [TyVar]
139 -- Mapping from tyvars to their PA dictionaries
140 , local_tyvar_pa :: VarEnv CoreExpr
144 initGlobalEnv :: VectInfo -> (InstEnv, InstEnv) -> FamInstEnvs -> GlobalEnv
145 initGlobalEnv info instEnvs famInstEnvs
147 global_vars = mapVarEnv (Var . snd) $ vectInfoVar info
148 , global_exported_vars = emptyVarEnv
149 , global_tycons = mapNameEnv snd $ vectInfoTyCon info
150 , global_tycon_pa = emptyNameEnv
151 , global_inst_env = instEnvs
152 , global_fam_inst_env = famInstEnvs
153 , global_bindings = []
156 emptyLocalEnv = LocalEnv {
157 local_vars = emptyVarEnv
159 , local_tyvar_pa = emptyVarEnv
163 updVectInfo :: GlobalEnv -> TypeEnv -> VectInfo -> VectInfo
164 updVectInfo env tyenv info
166 vectInfoVar = global_exported_vars env
167 , vectInfoTyCon = tc_env
170 tc_env = mkNameEnv [(tc_name, (tc,tc'))
171 | tc <- typeEnvTyCons tyenv
172 , let tc_name = tyConName tc
173 , Just tc' <- [lookupNameEnv (global_tycons env) tc_name]]
175 data VResult a = Yes GlobalEnv LocalEnv a | No
177 newtype VM a = VM { runVM :: Builtins -> GlobalEnv -> LocalEnv -> DsM (VResult a) }
179 instance Monad VM where
180 return x = VM $ \bi genv lenv -> return (Yes genv lenv x)
181 VM p >>= f = VM $ \bi genv lenv -> do
184 Yes genv' lenv' x -> runVM (f x) bi genv' lenv'
188 noV = VM $ \_ _ _ -> return No
190 tryV :: VM a -> VM (Maybe a)
191 tryV (VM p) = VM $ \bi genv lenv ->
195 Yes genv' lenv' x -> return (Yes genv' lenv' (Just x))
196 No -> return (Yes genv lenv Nothing)
198 maybeV :: VM (Maybe a) -> VM a
199 maybeV p = maybe noV return =<< p
201 orElseV :: VM a -> VM a -> VM a
202 orElseV p q = maybe q return =<< tryV p
204 fixV :: (a -> VM a) -> VM a
205 fixV f = VM (\bi genv lenv -> fixDs $ \r -> runVM (f (unYes r)) bi genv lenv )
207 unYes (Yes _ _ x) = x
209 localV :: VM a -> VM a
216 closedV :: VM a -> VM a
219 setLEnv emptyLocalEnv
224 liftDs :: DsM a -> VM a
225 liftDs p = VM $ \bi genv lenv -> do { x <- p; return (Yes genv lenv x) }
227 builtin :: (Builtins -> a) -> VM a
228 builtin f = VM $ \bi genv lenv -> return (Yes genv lenv (f bi))
230 readGEnv :: (GlobalEnv -> a) -> VM a
231 readGEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f genv))
233 setGEnv :: GlobalEnv -> VM ()
234 setGEnv genv = VM $ \_ _ lenv -> return (Yes genv lenv ())
236 updGEnv :: (GlobalEnv -> GlobalEnv) -> VM ()
237 updGEnv f = VM $ \_ genv lenv -> return (Yes (f genv) lenv ())
239 readLEnv :: (LocalEnv -> a) -> VM a
240 readLEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f lenv))
242 setLEnv :: LocalEnv -> VM ()
243 setLEnv lenv = VM $ \_ genv _ -> return (Yes genv lenv ())
245 updLEnv :: (LocalEnv -> LocalEnv) -> VM ()
246 updLEnv f = VM $ \_ genv lenv -> return (Yes genv (f lenv) ())
248 getInstEnv :: VM (InstEnv, InstEnv)
249 getInstEnv = readGEnv global_inst_env
251 getFamInstEnv :: VM FamInstEnvs
252 getFamInstEnv = readGEnv global_fam_inst_env
254 cloneName :: (OccName -> OccName) -> Name -> VM Name
255 cloneName mk_occ name = liftM make (liftDs newUnique)
257 occ_name = mk_occ (nameOccName name)
259 make u | isExternalName name = mkExternalName u (nameModule name)
262 | otherwise = mkSystemName u occ_name
264 newLocalVar :: FastString -> Type -> VM Var
267 u <- liftDs newUnique
268 return $ mkSysLocal fs u ty
270 newTyVar :: FastString -> Kind -> VM Var
273 u <- liftDs newUnique
274 return $ mkTyVar (mkSysTvName u fs) k
276 defGlobalVar :: Var -> Var -> VM ()
277 defGlobalVar v v' = updGEnv $ \env ->
278 env { global_vars = extendVarEnv (global_vars env) v (Var v')
279 , global_exported_vars = upd (global_exported_vars env)
282 upd env | isExportedId v = extendVarEnv env v (v, v')
285 lookupVar :: Var -> VM (Scope CoreExpr (CoreExpr, CoreExpr))
288 r <- readLEnv $ \env -> lookupVarEnv (local_vars env) v
290 Just e -> return (Local e)
291 Nothing -> liftM Global
292 $ maybeV (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
294 lookupTyCon :: TyCon -> VM (Maybe TyCon)
295 lookupTyCon tc = readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
297 lookupTyVarPA :: Var -> VM (Maybe CoreExpr)
298 lookupTyVarPA tv = readLEnv $ \env -> lookupVarEnv (local_tyvar_pa env) tv
300 defLocalTyVar :: TyVar -> VM ()
301 defLocalTyVar tv = updLEnv $ \env ->
302 env { local_tyvars = tv : local_tyvars env
303 , local_tyvar_pa = local_tyvar_pa env `delVarEnv` tv
306 defLocalTyVarWithPA :: TyVar -> CoreExpr -> VM ()
307 defLocalTyVarWithPA tv pa = updLEnv $ \env ->
308 env { local_tyvars = tv : local_tyvars env
309 , local_tyvar_pa = extendVarEnv (local_tyvar_pa env) tv pa
312 localTyVars :: VM [TyVar]
313 localTyVars = readLEnv (reverse . local_tyvars)
315 -- Look up the dfun of a class instance.
317 -- The match must be unique - ie, match exactly one instance - but the
318 -- type arguments used for matching may be more specific than those of
319 -- the class instance declaration. The found class instances must not have
320 -- any type variables in the instance context that do not appear in the
321 -- instances head (i.e., no flexi vars); for details for what this means,
322 -- see the docs at InstEnv.lookupInstEnv.
324 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
326 = do { instEnv <- getInstEnv
327 ; case lookupInstEnv instEnv cls tys of
328 ([(inst, inst_tys)], _)
329 | noFlexiVar -> return (instanceDFunId inst, inst_tys')
330 | otherwise -> pprPanic "VectMonad.lookupInst: flexi var: "
331 (ppr $ mkTyConApp (classTyCon cls) tys)
333 inst_tys' = [ty | Right ty <- inst_tys]
334 noFlexiVar = all isRight inst_tys
338 isRight (Left _) = False
339 isRight (Right _) = True
341 -- Look up the representation tycon of a family instance.
343 -- The match must be unique - ie, match exactly one instance - but the
344 -- type arguments used for matching may be more specific than those of
345 -- the family instance declaration.
347 -- Return the instance tycon and its type instance. For example, if we have
349 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
351 -- then we have a coercion (ie, type instance of family instance coercion)
353 -- :Co:R42T Int :: T [Int] ~ :R42T Int
355 -- which implies that :R42T was declared as 'data instance T [a]'.
357 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
358 lookupFamInst tycon tys
359 = ASSERT( isOpenTyCon tycon )
360 do { instEnv <- getFamInstEnv
361 ; case lookupFamInstEnv instEnv tycon tys of
362 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
364 pprPanic "VectMonad.lookupFamInst: not found: "
365 (ppr $ mkTyConApp tycon tys)
368 initV :: HscEnv -> ModGuts -> VectInfo -> VM a -> IO (Maybe (VectInfo, a))
369 initV hsc_env guts info p
371 eps <- hscEPS hsc_env
372 let famInstEnvs = (eps_fam_inst_env eps, mg_fam_inst_env guts)
373 let instEnvs = (eps_inst_env eps, mg_inst_env guts)
375 Just r <- initDs hsc_env (mg_module guts)
378 (go instEnvs famInstEnvs)
382 go instEnvs famInstEnvs =
384 builtins <- initBuiltins
385 r <- runVM p builtins (initGlobalEnv info instEnvs famInstEnvs)
388 Yes genv _ x -> return $ Just (new_info genv, x)
391 new_info genv = updVectInfo genv (mg_types guts) info