5 noV, tryV, maybeV, traceMaybeV, orElseV, fixV, localV, closedV, initV,
7 cloneName, cloneId, cloneVar,
8 newExportedVar, newLocalVar, newDummyVar, newTyVar,
10 Builtins(..), sumTyCon, prodTyCon, uarrTy, intPrimArrayTy,
16 readGEnv, setGEnv, updGEnv,
19 readLEnv, setLEnv, updLEnv,
23 lookupVar, defGlobalVar,
24 lookupTyCon, defTyCon,
25 lookupDataCon, defDataCon,
26 lookupTyConPA, defTyConPA, defTyConPAs,
29 lookupPrimMethod, lookupPrimPArray,
30 lookupTyVarPA, defLocalTyVar, defLocalTyVarWithPA, localTyVars,
32 {-lookupInst,-} lookupFamInst
35 #include "HsVersions.h"
49 import IOEnv ( liftIO )
58 import SrcLoc ( noSrcSpan )
62 data Scope a b = Global a | Local b
64 -- ----------------------------------------------------------------------------
65 -- Vectorisation monad
67 data GlobalEnv = GlobalEnv {
68 -- Mapping from global variables to their vectorised versions.
70 global_vars :: VarEnv Var
72 -- Exported variables which have a vectorised version
74 , global_exported_vars :: VarEnv (Var, Var)
76 -- Mapping from TyCons to their vectorised versions.
77 -- TyCons which do not have to be vectorised are mapped to
80 , global_tycons :: NameEnv TyCon
82 -- Mapping from DataCons to their vectorised versions
84 , global_datacons :: NameEnv DataCon
86 -- Mapping from TyCons to their PA dfuns
88 , global_pa_funs :: NameEnv Var
90 -- Mapping from TyCons to their PR dfuns
91 , global_pr_funs :: NameEnv Var
93 -- Mapping from unboxed TyCons to their boxed versions
94 , global_boxed_tycons :: NameEnv TyCon
96 -- External package inst-env & home-package inst-env for class
99 , global_inst_env :: (InstEnv, InstEnv)
101 -- External package inst-env & home-package inst-env for family
104 , global_fam_inst_env :: FamInstEnvs
107 , global_bindings :: [(Var, CoreExpr)]
110 data LocalEnv = LocalEnv {
111 -- Mapping from local variables to their vectorised and
114 local_vars :: VarEnv (Var, Var)
116 -- In-scope type variables
118 , local_tyvars :: [TyVar]
120 -- Mapping from tyvars to their PA dictionaries
121 , local_tyvar_pa :: VarEnv CoreExpr
123 -- Local binding name
124 , local_bind_name :: FastString
127 initGlobalEnv :: VectInfo -> (InstEnv, InstEnv) -> FamInstEnvs -> GlobalEnv
128 initGlobalEnv info instEnvs famInstEnvs
130 global_vars = mapVarEnv snd $ vectInfoVar info
131 , global_exported_vars = emptyVarEnv
132 , global_tycons = mapNameEnv snd $ vectInfoTyCon info
133 , global_datacons = mapNameEnv snd $ vectInfoDataCon info
134 , global_pa_funs = mapNameEnv snd $ vectInfoPADFun info
135 , global_pr_funs = emptyNameEnv
136 , global_boxed_tycons = emptyNameEnv
137 , global_inst_env = instEnvs
138 , global_fam_inst_env = famInstEnvs
139 , global_bindings = []
142 extendImportedVarsEnv :: [(Var, Var)] -> GlobalEnv -> GlobalEnv
143 extendImportedVarsEnv ps genv
144 = genv { global_vars = extendVarEnvList (global_vars genv) ps }
146 setFamInstEnv :: FamInstEnv -> GlobalEnv -> GlobalEnv
147 setFamInstEnv l_fam_inst genv
148 = genv { global_fam_inst_env = (g_fam_inst, l_fam_inst) }
150 (g_fam_inst, _) = global_fam_inst_env genv
152 extendTyConsEnv :: [(Name, TyCon)] -> GlobalEnv -> GlobalEnv
153 extendTyConsEnv ps genv
154 = genv { global_tycons = extendNameEnvList (global_tycons genv) ps }
156 extendDataConsEnv :: [(Name, DataCon)] -> GlobalEnv -> GlobalEnv
157 extendDataConsEnv ps genv
158 = genv { global_datacons = extendNameEnvList (global_datacons genv) ps }
160 extendPAFunsEnv :: [(Name, Var)] -> GlobalEnv -> GlobalEnv
161 extendPAFunsEnv ps genv
162 = genv { global_pa_funs = extendNameEnvList (global_pa_funs genv) ps }
164 setPRFunsEnv :: [(Name, Var)] -> GlobalEnv -> GlobalEnv
166 = genv { global_pr_funs = mkNameEnv ps }
168 setBoxedTyConsEnv :: [(Name, TyCon)] -> GlobalEnv -> GlobalEnv
169 setBoxedTyConsEnv ps genv
170 = genv { global_boxed_tycons = mkNameEnv ps }
172 emptyLocalEnv :: LocalEnv
173 emptyLocalEnv = LocalEnv {
174 local_vars = emptyVarEnv
176 , local_tyvar_pa = emptyVarEnv
177 , local_bind_name = FSLIT("fn")
181 updVectInfo :: GlobalEnv -> TypeEnv -> VectInfo -> VectInfo
182 updVectInfo env tyenv info
184 vectInfoVar = global_exported_vars env
185 , vectInfoTyCon = mk_env typeEnvTyCons global_tycons
186 , vectInfoDataCon = mk_env typeEnvDataCons global_datacons
187 , vectInfoPADFun = mk_env typeEnvTyCons global_pa_funs
190 mk_env from_tyenv from_env = mkNameEnv [(name, (from,to))
191 | from <- from_tyenv tyenv
192 , let name = getName from
193 , Just to <- [lookupNameEnv (from_env env) name]]
195 data VResult a = Yes GlobalEnv LocalEnv a | No
197 newtype VM a = VM { runVM :: Builtins -> GlobalEnv -> LocalEnv -> DsM (VResult a) }
199 instance Monad VM where
200 return x = VM $ \_ genv lenv -> return (Yes genv lenv x)
201 VM p >>= f = VM $ \bi genv lenv -> do
204 Yes genv' lenv' x -> runVM (f x) bi genv' lenv'
208 noV = VM $ \_ _ _ -> return No
210 traceNoV :: String -> SDoc -> VM a
211 traceNoV s d = pprTrace s d noV
213 tryV :: VM a -> VM (Maybe a)
214 tryV (VM p) = VM $ \bi genv lenv ->
218 Yes genv' lenv' x -> return (Yes genv' lenv' (Just x))
219 No -> return (Yes genv lenv Nothing)
221 maybeV :: VM (Maybe a) -> VM a
222 maybeV p = maybe noV return =<< p
224 traceMaybeV :: String -> SDoc -> VM (Maybe a) -> VM a
225 traceMaybeV s d p = maybe (traceNoV s d) return =<< p
227 orElseV :: VM a -> VM a -> VM a
228 orElseV p q = maybe q return =<< tryV p
230 fixV :: (a -> VM a) -> VM a
231 fixV f = VM (\bi genv lenv -> fixDs $ \r -> runVM (f (unYes r)) bi genv lenv )
233 -- NOTE: It is essential that we are lazy in r above so do not replace
234 -- calls to this function by an explicit case.
235 unYes (Yes _ _ x) = x
236 unYes No = panic "VectMonad.fixV: no result"
238 localV :: VM a -> VM a
245 closedV :: VM a -> VM a
248 setLEnv (emptyLocalEnv { local_bind_name = local_bind_name env })
253 liftDs :: DsM a -> VM a
254 liftDs p = VM $ \_ genv lenv -> do { x <- p; return (Yes genv lenv x) }
256 builtin :: (Builtins -> a) -> VM a
257 builtin f = VM $ \bi genv lenv -> return (Yes genv lenv (f bi))
259 builtins :: (a -> Builtins -> b) -> VM (a -> b)
260 builtins f = VM $ \bi genv lenv -> return (Yes genv lenv (`f` bi))
262 readGEnv :: (GlobalEnv -> a) -> VM a
263 readGEnv f = VM $ \_ genv lenv -> return (Yes genv lenv (f genv))
265 setGEnv :: GlobalEnv -> VM ()
266 setGEnv genv = VM $ \_ _ lenv -> return (Yes genv lenv ())
268 updGEnv :: (GlobalEnv -> GlobalEnv) -> VM ()
269 updGEnv f = VM $ \_ genv lenv -> return (Yes (f genv) lenv ())
271 readLEnv :: (LocalEnv -> a) -> VM a
272 readLEnv f = VM $ \_ genv lenv -> return (Yes genv lenv (f lenv))
274 setLEnv :: LocalEnv -> VM ()
275 setLEnv lenv = VM $ \_ genv _ -> return (Yes genv lenv ())
277 updLEnv :: (LocalEnv -> LocalEnv) -> VM ()
278 updLEnv f = VM $ \_ genv lenv -> return (Yes genv (f lenv) ())
281 getInstEnv :: VM (InstEnv, InstEnv)
282 getInstEnv = readGEnv global_inst_env
285 getFamInstEnv :: VM FamInstEnvs
286 getFamInstEnv = readGEnv global_fam_inst_env
288 getBindName :: VM FastString
289 getBindName = readLEnv local_bind_name
291 inBind :: Id -> VM a -> VM a
293 = do updLEnv $ \env -> env { local_bind_name = occNameFS (getOccName id) }
296 cloneName :: (OccName -> OccName) -> Name -> VM Name
297 cloneName mk_occ name = liftM make (liftDs newUnique)
299 occ_name = mk_occ (nameOccName name)
301 make u | isExternalName name = mkExternalName u (nameModule name)
304 | otherwise = mkSystemName u occ_name
306 cloneId :: (OccName -> OccName) -> Id -> Type -> VM Id
309 name <- cloneName mk_occ (getName id)
310 let id' | isExportedId id = Id.mkExportedLocalId name ty
311 | otherwise = Id.mkLocalId name ty
314 cloneVar :: Var -> VM Var
315 cloneVar var = liftM (setIdUnique var) (liftDs newUnique)
317 newExportedVar :: OccName -> Type -> VM Var
318 newExportedVar occ_name ty
320 mod <- liftDs getModuleDs
321 u <- liftDs newUnique
323 let name = mkExternalName u mod occ_name noSrcSpan
325 return $ Id.mkExportedLocalId name ty
327 newLocalVar :: FastString -> Type -> VM Var
330 u <- liftDs newUnique
331 return $ mkSysLocal fs u ty
333 newDummyVar :: Type -> VM Var
334 newDummyVar = newLocalVar FSLIT("ds")
336 newTyVar :: FastString -> Kind -> VM Var
339 u <- liftDs newUnique
340 return $ mkTyVar (mkSysTvName u fs) k
342 defGlobalVar :: Var -> Var -> VM ()
343 defGlobalVar v v' = updGEnv $ \env ->
344 env { global_vars = extendVarEnv (global_vars env) v v'
345 , global_exported_vars = upd (global_exported_vars env)
348 upd env | isExportedId v = extendVarEnv env v (v, v')
351 lookupVar :: Var -> VM (Scope Var (Var, Var))
354 r <- readLEnv $ \env -> lookupVarEnv (local_vars env) v
356 Just e -> return (Local e)
357 Nothing -> liftM Global
358 $ traceMaybeV "lookupVar" (ppr v)
359 (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
361 lookupTyCon :: TyCon -> VM (Maybe TyCon)
363 | isUnLiftedTyCon tc || isTupleTyCon tc = return (Just tc)
365 | otherwise = readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
367 defTyCon :: TyCon -> TyCon -> VM ()
368 defTyCon tc tc' = updGEnv $ \env ->
369 env { global_tycons = extendNameEnv (global_tycons env) (tyConName tc) tc' }
371 lookupDataCon :: DataCon -> VM (Maybe DataCon)
373 | isTupleTyCon (dataConTyCon dc) = return (Just dc)
374 | otherwise = readGEnv $ \env -> lookupNameEnv (global_datacons env) (dataConName dc)
376 defDataCon :: DataCon -> DataCon -> VM ()
377 defDataCon dc dc' = updGEnv $ \env ->
378 env { global_datacons = extendNameEnv (global_datacons env) (dataConName dc) dc' }
380 lookupPrimPArray :: TyCon -> VM (Maybe TyCon)
381 lookupPrimPArray = liftDs . primPArray
383 lookupPrimMethod :: TyCon -> String -> VM (Maybe Var)
384 lookupPrimMethod tycon = liftDs . primMethod tycon
386 lookupTyConPA :: TyCon -> VM (Maybe Var)
387 lookupTyConPA tc = readGEnv $ \env -> lookupNameEnv (global_pa_funs env) (tyConName tc)
389 defTyConPA :: TyCon -> Var -> VM ()
390 defTyConPA tc pa = updGEnv $ \env ->
391 env { global_pa_funs = extendNameEnv (global_pa_funs env) (tyConName tc) pa }
393 defTyConPAs :: [(TyCon, Var)] -> VM ()
394 defTyConPAs ps = updGEnv $ \env ->
395 env { global_pa_funs = extendNameEnvList (global_pa_funs env)
396 [(tyConName tc, pa) | (tc, pa) <- ps] }
398 lookupTyVarPA :: Var -> VM (Maybe CoreExpr)
399 lookupTyVarPA tv = readLEnv $ \env -> lookupVarEnv (local_tyvar_pa env) tv
401 lookupTyConPR :: TyCon -> VM (Maybe Var)
402 lookupTyConPR tc = readGEnv $ \env -> lookupNameEnv (global_pr_funs env) (tyConName tc)
404 lookupBoxedTyCon :: TyCon -> VM (Maybe TyCon)
405 lookupBoxedTyCon tc = readGEnv $ \env -> lookupNameEnv (global_boxed_tycons env)
408 defLocalTyVar :: TyVar -> VM ()
409 defLocalTyVar tv = updLEnv $ \env ->
410 env { local_tyvars = tv : local_tyvars env
411 , local_tyvar_pa = local_tyvar_pa env `delVarEnv` tv
414 defLocalTyVarWithPA :: TyVar -> CoreExpr -> VM ()
415 defLocalTyVarWithPA tv pa = updLEnv $ \env ->
416 env { local_tyvars = tv : local_tyvars env
417 , local_tyvar_pa = extendVarEnv (local_tyvar_pa env) tv pa
420 localTyVars :: VM [TyVar]
421 localTyVars = readLEnv (reverse . local_tyvars)
423 -- Look up the dfun of a class instance.
425 -- The match must be unique - ie, match exactly one instance - but the
426 -- type arguments used for matching may be more specific than those of
427 -- the class instance declaration. The found class instances must not have
428 -- any type variables in the instance context that do not appear in the
429 -- instances head (i.e., no flexi vars); for details for what this means,
430 -- see the docs at InstEnv.lookupInstEnv.
433 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
435 = do { instEnv <- getInstEnv
436 ; case lookupInstEnv instEnv cls tys of
437 ([(inst, inst_tys)], _)
438 | noFlexiVar -> return (instanceDFunId inst, inst_tys')
439 | otherwise -> pprPanic "VectMonad.lookupInst: flexi var: "
440 (ppr $ mkTyConApp (classTyCon cls) tys)
442 inst_tys' = [ty | Right ty <- inst_tys]
443 noFlexiVar = all isRight inst_tys
444 _other -> traceNoV "lookupInst" (ppr cls <+> ppr tys)
447 isRight (Left _) = False
448 isRight (Right _) = True
451 -- Look up the representation tycon of a family instance.
453 -- The match must be unique - ie, match exactly one instance - but the
454 -- type arguments used for matching may be more specific than those of
455 -- the family instance declaration.
457 -- Return the instance tycon and its type instance. For example, if we have
459 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
461 -- then we have a coercion (ie, type instance of family instance coercion)
463 -- :Co:R42T Int :: T [Int] ~ :R42T Int
465 -- which implies that :R42T was declared as 'data instance T [a]'.
467 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
468 lookupFamInst tycon tys
469 = ASSERT( isOpenTyCon tycon )
470 do { instEnv <- getFamInstEnv
471 ; case lookupFamInstEnv instEnv tycon tys of
472 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
474 pprPanic "VectMonad.lookupFamInst: not found: "
475 (ppr $ mkTyConApp tycon tys)
478 initV :: HscEnv -> ModGuts -> VectInfo -> VM a -> IO (Maybe (VectInfo, a))
479 initV hsc_env guts info p
481 Just r <- initDs hsc_env (mg_module guts)
490 builtins <- initBuiltins
491 builtin_vars <- initBuiltinVars builtins
492 builtin_tycons <- initBuiltinTyCons builtins
493 let builtin_datacons = initBuiltinDataCons builtins
494 builtin_pas <- initBuiltinPAs builtins
495 builtin_prs <- initBuiltinPRs builtins
496 builtin_boxed <- initBuiltinBoxedTyCons builtins
498 eps <- liftIO $ hscEPS hsc_env
499 let famInstEnvs = (eps_fam_inst_env eps, mg_fam_inst_env guts)
500 instEnvs = (eps_inst_env eps, mg_inst_env guts)
502 let genv = extendImportedVarsEnv builtin_vars
503 . extendTyConsEnv builtin_tycons
504 . extendDataConsEnv builtin_datacons
505 . extendPAFunsEnv builtin_pas
506 . setPRFunsEnv builtin_prs
507 . setBoxedTyConsEnv builtin_boxed
508 $ initGlobalEnv info instEnvs famInstEnvs
510 r <- runVM p builtins genv emptyLocalEnv
512 Yes genv _ x -> return $ Just (new_info genv, x)
515 new_info genv = updVectInfo genv (mg_types guts) info