5 noV, tryV, maybeV, traceMaybeV, orElseV, fixV, localV, closedV, initV,
8 newExportedVar, newLocalVar, newDummyVar, newTyVar,
15 readGEnv, setGEnv, updGEnv,
18 readLEnv, setLEnv, updLEnv,
22 lookupVar, defGlobalVar,
23 lookupTyCon, defTyCon,
24 lookupDataCon, defDataCon,
25 lookupTyConPA, defTyConPA, defTyConPAs,
26 lookupTyVarPA, defLocalTyVar, defLocalTyVarWithPA, localTyVars,
28 {-lookupInst,-} lookupFamInst
31 #include "HsVersions.h"
47 import TysPrim ( intPrimTy )
60 import SrcLoc ( noSrcSpan )
62 import Control.Monad ( liftM, zipWithM )
64 data Scope a b = Global a | Local b
66 -- ----------------------------------------------------------------------------
67 -- Vectorisation monad
69 data GlobalEnv = GlobalEnv {
70 -- Mapping from global variables to their vectorised versions.
72 global_vars :: VarEnv Var
74 -- Exported variables which have a vectorised version
76 , global_exported_vars :: VarEnv (Var, Var)
78 -- Mapping from TyCons to their vectorised versions.
79 -- TyCons which do not have to be vectorised are mapped to
82 , global_tycons :: NameEnv TyCon
84 -- Mapping from DataCons to their vectorised versions
86 , global_datacons :: NameEnv DataCon
88 -- Mapping from TyCons to their PA dfuns
90 , global_pa_funs :: NameEnv Var
92 -- External package inst-env & home-package inst-env for class
95 , global_inst_env :: (InstEnv, InstEnv)
97 -- External package inst-env & home-package inst-env for family
100 , global_fam_inst_env :: FamInstEnvs
103 , global_bindings :: [(Var, CoreExpr)]
106 data LocalEnv = LocalEnv {
107 -- Mapping from local variables to their vectorised and
110 local_vars :: VarEnv (Var, Var)
112 -- In-scope type variables
114 , local_tyvars :: [TyVar]
116 -- Mapping from tyvars to their PA dictionaries
117 , local_tyvar_pa :: VarEnv CoreExpr
119 -- Local binding name
120 , local_bind_name :: FastString
123 initGlobalEnv :: VectInfo -> (InstEnv, InstEnv) -> FamInstEnvs -> GlobalEnv
124 initGlobalEnv info instEnvs famInstEnvs
126 global_vars = mapVarEnv snd $ vectInfoVar info
127 , global_exported_vars = emptyVarEnv
128 , global_tycons = mapNameEnv snd $ vectInfoTyCon info
129 , global_datacons = mapNameEnv snd $ vectInfoDataCon info
130 , global_pa_funs = mapNameEnv snd $ vectInfoPADFun info
131 , global_inst_env = instEnvs
132 , global_fam_inst_env = famInstEnvs
133 , global_bindings = []
136 setFamInstEnv :: FamInstEnv -> GlobalEnv -> GlobalEnv
137 setFamInstEnv l_fam_inst genv
138 = genv { global_fam_inst_env = (g_fam_inst, l_fam_inst) }
140 (g_fam_inst, _) = global_fam_inst_env genv
142 extendTyConsEnv :: [(Name, TyCon)] -> GlobalEnv -> GlobalEnv
143 extendTyConsEnv ps genv
144 = genv { global_tycons = extendNameEnvList (global_tycons genv) ps }
146 extendPAFunsEnv :: [(Name, Var)] -> GlobalEnv -> GlobalEnv
147 extendPAFunsEnv ps genv
148 = genv { global_pa_funs = extendNameEnvList (global_pa_funs genv) ps }
150 emptyLocalEnv = LocalEnv {
151 local_vars = emptyVarEnv
153 , local_tyvar_pa = emptyVarEnv
154 , local_bind_name = FSLIT("fn")
158 updVectInfo :: GlobalEnv -> TypeEnv -> VectInfo -> VectInfo
159 updVectInfo env tyenv info
161 vectInfoVar = global_exported_vars env
162 , vectInfoTyCon = mk_env typeEnvTyCons global_tycons
163 , vectInfoDataCon = mk_env typeEnvDataCons global_datacons
164 , vectInfoPADFun = mk_env typeEnvTyCons global_pa_funs
167 mk_env from_tyenv from_env = mkNameEnv [(name, (from,to))
168 | from <- from_tyenv tyenv
169 , let name = getName from
170 , Just to <- [lookupNameEnv (from_env env) name]]
172 data VResult a = Yes GlobalEnv LocalEnv a | No
174 newtype VM a = VM { runVM :: Builtins -> GlobalEnv -> LocalEnv -> DsM (VResult a) }
176 instance Monad VM where
177 return x = VM $ \bi genv lenv -> return (Yes genv lenv x)
178 VM p >>= f = VM $ \bi genv lenv -> do
181 Yes genv' lenv' x -> runVM (f x) bi genv' lenv'
185 noV = VM $ \_ _ _ -> return No
187 traceNoV :: String -> SDoc -> VM a
188 traceNoV s d = pprTrace s d noV
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 traceMaybeV :: String -> SDoc -> VM (Maybe a) -> VM a
202 traceMaybeV s d p = maybe (traceNoV s d) return =<< p
204 orElseV :: VM a -> VM a -> VM a
205 orElseV p q = maybe q return =<< tryV p
207 fixV :: (a -> VM a) -> VM a
208 fixV f = VM (\bi genv lenv -> fixDs $ \r -> runVM (f (unYes r)) bi genv lenv )
210 unYes (Yes _ _ x) = x
212 localV :: VM a -> VM a
219 closedV :: VM a -> VM a
222 setLEnv (emptyLocalEnv { local_bind_name = local_bind_name env })
227 liftDs :: DsM a -> VM a
228 liftDs p = VM $ \bi genv lenv -> do { x <- p; return (Yes genv lenv x) }
230 builtin :: (Builtins -> a) -> VM a
231 builtin f = VM $ \bi genv lenv -> return (Yes genv lenv (f bi))
233 readGEnv :: (GlobalEnv -> a) -> VM a
234 readGEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f genv))
236 setGEnv :: GlobalEnv -> VM ()
237 setGEnv genv = VM $ \_ _ lenv -> return (Yes genv lenv ())
239 updGEnv :: (GlobalEnv -> GlobalEnv) -> VM ()
240 updGEnv f = VM $ \_ genv lenv -> return (Yes (f genv) lenv ())
242 readLEnv :: (LocalEnv -> a) -> VM a
243 readLEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f lenv))
245 setLEnv :: LocalEnv -> VM ()
246 setLEnv lenv = VM $ \_ genv _ -> return (Yes genv lenv ())
248 updLEnv :: (LocalEnv -> LocalEnv) -> VM ()
249 updLEnv f = VM $ \_ genv lenv -> return (Yes genv (f lenv) ())
251 getInstEnv :: VM (InstEnv, InstEnv)
252 getInstEnv = readGEnv global_inst_env
254 getFamInstEnv :: VM FamInstEnvs
255 getFamInstEnv = readGEnv global_fam_inst_env
257 getBindName :: VM FastString
258 getBindName = readLEnv local_bind_name
260 inBind :: Id -> VM a -> VM a
262 = do updLEnv $ \env -> env { local_bind_name = occNameFS (getOccName id) }
265 cloneName :: (OccName -> OccName) -> Name -> VM Name
266 cloneName mk_occ name = liftM make (liftDs newUnique)
268 occ_name = mk_occ (nameOccName name)
270 make u | isExternalName name = mkExternalName u (nameModule name)
273 | otherwise = mkSystemName u occ_name
275 cloneId :: (OccName -> OccName) -> Id -> Type -> VM Id
278 name <- cloneName mk_occ (getName id)
279 let id' | isExportedId id = Id.mkExportedLocalId name ty
280 | otherwise = Id.mkLocalId name ty
283 newExportedVar :: OccName -> Type -> VM Var
284 newExportedVar occ_name ty
286 mod <- liftDs getModuleDs
287 u <- liftDs newUnique
289 let name = mkExternalName u mod occ_name noSrcSpan
291 return $ Id.mkExportedLocalId name ty
293 newLocalVar :: FastString -> Type -> VM Var
296 u <- liftDs newUnique
297 return $ mkSysLocal fs u ty
299 newDummyVar :: Type -> VM Var
300 newDummyVar = newLocalVar FSLIT("ds")
302 newTyVar :: FastString -> Kind -> VM Var
305 u <- liftDs newUnique
306 return $ mkTyVar (mkSysTvName u fs) k
308 defGlobalVar :: Var -> Var -> VM ()
309 defGlobalVar v v' = updGEnv $ \env ->
310 env { global_vars = extendVarEnv (global_vars env) v v'
311 , global_exported_vars = upd (global_exported_vars env)
314 upd env | isExportedId v = extendVarEnv env v (v, v')
317 lookupVar :: Var -> VM (Scope Var (Var, Var))
320 r <- readLEnv $ \env -> lookupVarEnv (local_vars env) v
322 Just e -> return (Local e)
323 Nothing -> liftM Global
324 $ traceMaybeV "lookupVar" (ppr v)
325 (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
327 lookupTyCon :: TyCon -> VM (Maybe TyCon)
329 | isUnLiftedTyCon tc || isTupleTyCon tc = return (Just tc)
331 | otherwise = readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
333 defTyCon :: TyCon -> TyCon -> VM ()
334 defTyCon tc tc' = updGEnv $ \env ->
335 env { global_tycons = extendNameEnv (global_tycons env) (tyConName tc) tc' }
337 lookupDataCon :: DataCon -> VM (Maybe DataCon)
338 lookupDataCon dc = readGEnv $ \env -> lookupNameEnv (global_datacons env) (dataConName dc)
340 defDataCon :: DataCon -> DataCon -> VM ()
341 defDataCon dc dc' = updGEnv $ \env ->
342 env { global_datacons = extendNameEnv (global_datacons env) (dataConName dc) dc' }
344 lookupTyConPA :: TyCon -> VM (Maybe Var)
345 lookupTyConPA tc = readGEnv $ \env -> lookupNameEnv (global_pa_funs env) (tyConName tc)
347 defTyConPA :: TyCon -> Var -> VM ()
348 defTyConPA tc pa = updGEnv $ \env ->
349 env { global_pa_funs = extendNameEnv (global_pa_funs env) (tyConName tc) pa }
351 defTyConPAs :: [(TyCon, Var)] -> VM ()
352 defTyConPAs ps = updGEnv $ \env ->
353 env { global_pa_funs = extendNameEnvList (global_pa_funs env)
354 [(tyConName tc, pa) | (tc, pa) <- ps] }
356 lookupTyVarPA :: Var -> VM (Maybe CoreExpr)
357 lookupTyVarPA tv = readLEnv $ \env -> lookupVarEnv (local_tyvar_pa env) tv
359 defLocalTyVar :: TyVar -> VM ()
360 defLocalTyVar tv = updLEnv $ \env ->
361 env { local_tyvars = tv : local_tyvars env
362 , local_tyvar_pa = local_tyvar_pa env `delVarEnv` tv
365 defLocalTyVarWithPA :: TyVar -> CoreExpr -> VM ()
366 defLocalTyVarWithPA tv pa = updLEnv $ \env ->
367 env { local_tyvars = tv : local_tyvars env
368 , local_tyvar_pa = extendVarEnv (local_tyvar_pa env) tv pa
371 localTyVars :: VM [TyVar]
372 localTyVars = readLEnv (reverse . local_tyvars)
374 -- Look up the dfun of a class instance.
376 -- The match must be unique - ie, match exactly one instance - but the
377 -- type arguments used for matching may be more specific than those of
378 -- the class instance declaration. The found class instances must not have
379 -- any type variables in the instance context that do not appear in the
380 -- instances head (i.e., no flexi vars); for details for what this means,
381 -- see the docs at InstEnv.lookupInstEnv.
384 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
386 = do { instEnv <- getInstEnv
387 ; case lookupInstEnv instEnv cls tys of
388 ([(inst, inst_tys)], _)
389 | noFlexiVar -> return (instanceDFunId inst, inst_tys')
390 | otherwise -> pprPanic "VectMonad.lookupInst: flexi var: "
391 (ppr $ mkTyConApp (classTyCon cls) tys)
393 inst_tys' = [ty | Right ty <- inst_tys]
394 noFlexiVar = all isRight inst_tys
395 _other -> traceNoV "lookupInst" (ppr cls <+> ppr tys)
398 isRight (Left _) = False
399 isRight (Right _) = True
402 -- Look up the representation tycon of a family instance.
404 -- The match must be unique - ie, match exactly one instance - but the
405 -- type arguments used for matching may be more specific than those of
406 -- the family instance declaration.
408 -- Return the instance tycon and its type instance. For example, if we have
410 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
412 -- then we have a coercion (ie, type instance of family instance coercion)
414 -- :Co:R42T Int :: T [Int] ~ :R42T Int
416 -- which implies that :R42T was declared as 'data instance T [a]'.
418 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
419 lookupFamInst tycon tys
420 = ASSERT( isOpenTyCon tycon )
421 do { instEnv <- getFamInstEnv
422 ; case lookupFamInstEnv instEnv tycon tys of
423 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
425 pprPanic "VectMonad.lookupFamInst: not found: "
426 (ppr $ mkTyConApp tycon tys)
429 initV :: HscEnv -> ModGuts -> VectInfo -> VM a -> IO (Maybe (VectInfo, a))
430 initV hsc_env guts info p
432 eps <- hscEPS hsc_env
433 let famInstEnvs = (eps_fam_inst_env eps, mg_fam_inst_env guts)
434 let instEnvs = (eps_inst_env eps, mg_inst_env guts)
436 Just r <- initDs hsc_env (mg_module guts)
439 (go instEnvs famInstEnvs)
443 go instEnvs famInstEnvs =
445 builtins <- initBuiltins
446 builtin_tycons <- initBuiltinTyCons
447 builtin_pas <- initBuiltinPAs
449 let genv = extendTyConsEnv builtin_tycons
450 . extendPAFunsEnv builtin_pas
451 $ initGlobalEnv info instEnvs famInstEnvs
453 r <- runVM p builtins genv emptyLocalEnv
455 Yes genv _ x -> return $ Just (new_info genv, x)
458 new_info genv = updVectInfo genv (mg_types guts) info