5 noV, traceNoV, ensureV, traceEnsureV, tryV, maybeV, traceMaybeV, orElseV,
6 onlyIfV, fixV, localV, closedV,
7 initV, cantVectorise, maybeCantVectorise, maybeCantVectoriseM,
9 cloneName, cloneId, cloneVar,
10 newExportedVar, newLocalVar, newDummyVar, newTyVar,
12 Builtins(..), sumTyCon, prodTyCon,
13 combinePAVar, scalarZip, closureCtrFun,
18 readGEnv, setGEnv, updGEnv,
21 readLEnv, setLEnv, updLEnv,
25 lookupVar, defGlobalVar, globalScalars,
26 lookupTyCon, defTyCon,
27 lookupDataCon, defDataCon,
28 lookupTyConPA, defTyConPA, defTyConPAs,
31 lookupPrimMethod, lookupPrimPArray,
32 lookupTyVarPA, defLocalTyVar, defLocalTyVarWithPA, localTyVars,
34 lookupInst, lookupFamInst
37 #include "HsVersions.h"
41 import HscTypes hiding ( MonadThings(..) )
42 import Module ( PackageId )
62 import SrcLoc ( noSrcSpan )
66 data Scope a b = Global a | Local b
68 -- ----------------------------------------------------------------------------
69 -- Vectorisation monad
71 data GlobalEnv = GlobalEnv {
72 -- Mapping from global variables to their vectorised versions.
74 global_vars :: VarEnv Var
76 -- Purely scalar variables. Code which mentions only these
77 -- variables doesn't have to be lifted.
78 , global_scalars :: VarSet
80 -- Exported variables which have a vectorised version
82 , global_exported_vars :: VarEnv (Var, Var)
84 -- Mapping from TyCons to their vectorised versions.
85 -- TyCons which do not have to be vectorised are mapped to
88 , global_tycons :: NameEnv TyCon
90 -- Mapping from DataCons to their vectorised versions
92 , global_datacons :: NameEnv DataCon
94 -- Mapping from TyCons to their PA dfuns
96 , global_pa_funs :: NameEnv Var
98 -- Mapping from TyCons to their PR dfuns
99 , global_pr_funs :: NameEnv Var
101 -- Mapping from unboxed TyCons to their boxed versions
102 , global_boxed_tycons :: NameEnv TyCon
104 -- External package inst-env & home-package inst-env for class
107 , global_inst_env :: (InstEnv, InstEnv)
109 -- External package inst-env & home-package inst-env for family
112 , global_fam_inst_env :: FamInstEnvs
115 , global_bindings :: [(Var, CoreExpr)]
118 data LocalEnv = LocalEnv {
119 -- Mapping from local variables to their vectorised and
122 local_vars :: VarEnv (Var, Var)
124 -- In-scope type variables
126 , local_tyvars :: [TyVar]
128 -- Mapping from tyvars to their PA dictionaries
129 , local_tyvar_pa :: VarEnv CoreExpr
131 -- Local binding name
132 , local_bind_name :: FastString
135 initGlobalEnv :: VectInfo -> (InstEnv, InstEnv) -> FamInstEnvs -> GlobalEnv
136 initGlobalEnv info instEnvs famInstEnvs
138 global_vars = mapVarEnv snd $ vectInfoVar info
139 , global_scalars = emptyVarSet
140 , global_exported_vars = emptyVarEnv
141 , global_tycons = mapNameEnv snd $ vectInfoTyCon info
142 , global_datacons = mapNameEnv snd $ vectInfoDataCon info
143 , global_pa_funs = mapNameEnv snd $ vectInfoPADFun info
144 , global_pr_funs = emptyNameEnv
145 , global_boxed_tycons = emptyNameEnv
146 , global_inst_env = instEnvs
147 , global_fam_inst_env = famInstEnvs
148 , global_bindings = []
151 extendImportedVarsEnv :: [(Var, Var)] -> GlobalEnv -> GlobalEnv
152 extendImportedVarsEnv ps genv
153 = genv { global_vars = extendVarEnvList (global_vars genv) ps }
155 extendScalars :: [Var] -> GlobalEnv -> GlobalEnv
156 extendScalars vs genv
157 = genv { global_scalars = extendVarSetList (global_scalars genv) vs }
159 setFamInstEnv :: FamInstEnv -> GlobalEnv -> GlobalEnv
160 setFamInstEnv l_fam_inst genv
161 = genv { global_fam_inst_env = (g_fam_inst, l_fam_inst) }
163 (g_fam_inst, _) = global_fam_inst_env genv
165 extendTyConsEnv :: [(Name, TyCon)] -> GlobalEnv -> GlobalEnv
166 extendTyConsEnv ps genv
167 = genv { global_tycons = extendNameEnvList (global_tycons genv) ps }
169 extendDataConsEnv :: [(Name, DataCon)] -> GlobalEnv -> GlobalEnv
170 extendDataConsEnv ps genv
171 = genv { global_datacons = extendNameEnvList (global_datacons genv) ps }
173 extendPAFunsEnv :: [(Name, Var)] -> GlobalEnv -> GlobalEnv
174 extendPAFunsEnv ps genv
175 = genv { global_pa_funs = extendNameEnvList (global_pa_funs genv) ps }
177 setPRFunsEnv :: [(Name, Var)] -> GlobalEnv -> GlobalEnv
179 = genv { global_pr_funs = mkNameEnv ps }
181 setBoxedTyConsEnv :: [(Name, TyCon)] -> GlobalEnv -> GlobalEnv
182 setBoxedTyConsEnv ps genv
183 = genv { global_boxed_tycons = mkNameEnv ps }
185 emptyLocalEnv :: LocalEnv
186 emptyLocalEnv = LocalEnv {
187 local_vars = emptyVarEnv
189 , local_tyvar_pa = emptyVarEnv
190 , local_bind_name = fsLit "fn"
194 updVectInfo :: GlobalEnv -> TypeEnv -> VectInfo -> VectInfo
195 updVectInfo env tyenv info
197 vectInfoVar = global_exported_vars env
198 , vectInfoTyCon = mk_env typeEnvTyCons global_tycons
199 , vectInfoDataCon = mk_env typeEnvDataCons global_datacons
200 , vectInfoPADFun = mk_env typeEnvTyCons global_pa_funs
203 mk_env from_tyenv from_env = mkNameEnv [(name, (from,to))
204 | from <- from_tyenv tyenv
205 , let name = getName from
206 , Just to <- [lookupNameEnv (from_env env) name]]
208 data VResult a = Yes GlobalEnv LocalEnv a | No
210 newtype VM a = VM { runVM :: Builtins -> GlobalEnv -> LocalEnv -> DsM (VResult a) }
212 instance Monad VM where
213 return x = VM $ \_ genv lenv -> return (Yes genv lenv x)
214 VM p >>= f = VM $ \bi genv lenv -> do
217 Yes genv' lenv' x -> runVM (f x) bi genv' lenv'
221 cantVectorise :: String -> SDoc -> a
222 cantVectorise s d = pgmError
224 $ vcat [text "*** Vectorisation error ***",
225 nest 4 $ sep [text s, nest 4 d]]
227 maybeCantVectorise :: String -> SDoc -> Maybe a -> a
228 maybeCantVectorise s d Nothing = cantVectorise s d
229 maybeCantVectorise _ _ (Just x) = x
231 maybeCantVectoriseM :: Monad m => String -> SDoc -> m (Maybe a) -> m a
232 maybeCantVectoriseM s d p
237 Nothing -> cantVectorise s d
240 noV = VM $ \_ _ _ -> return No
242 traceNoV :: String -> SDoc -> VM a
243 traceNoV s d = pprTrace s d noV
245 ensureV :: Bool -> VM ()
247 ensureV True = return ()
249 onlyIfV :: Bool -> VM a -> VM a
250 onlyIfV b p = ensureV b >> p
252 traceEnsureV :: String -> SDoc -> Bool -> VM ()
253 traceEnsureV s d False = traceNoV s d
254 traceEnsureV _ _ True = return ()
256 tryV :: VM a -> VM (Maybe a)
257 tryV (VM p) = VM $ \bi genv lenv ->
261 Yes genv' lenv' x -> return (Yes genv' lenv' (Just x))
262 No -> return (Yes genv lenv Nothing)
264 maybeV :: VM (Maybe a) -> VM a
265 maybeV p = maybe noV return =<< p
267 traceMaybeV :: String -> SDoc -> VM (Maybe a) -> VM a
268 traceMaybeV s d p = maybe (traceNoV s d) return =<< p
270 orElseV :: VM a -> VM a -> VM a
271 orElseV p q = maybe q return =<< tryV p
273 fixV :: (a -> VM a) -> VM a
274 fixV f = VM (\bi genv lenv -> fixDs $ \r -> runVM (f (unYes r)) bi genv lenv )
276 -- NOTE: It is essential that we are lazy in r above so do not replace
277 -- calls to this function by an explicit case.
278 unYes (Yes _ _ x) = x
279 unYes No = panic "VectMonad.fixV: no result"
281 localV :: VM a -> VM a
288 closedV :: VM a -> VM a
291 setLEnv (emptyLocalEnv { local_bind_name = local_bind_name env })
296 liftDs :: DsM a -> VM a
297 liftDs p = VM $ \_ genv lenv -> do { x <- p; return (Yes genv lenv x) }
299 liftBuiltinDs :: (Builtins -> DsM a) -> VM a
300 liftBuiltinDs p = VM $ \bi genv lenv -> do { x <- p bi; return (Yes genv lenv x)}
302 builtin :: (Builtins -> a) -> VM a
303 builtin f = VM $ \bi genv lenv -> return (Yes genv lenv (f bi))
305 builtins :: (a -> Builtins -> b) -> VM (a -> b)
306 builtins f = VM $ \bi genv lenv -> return (Yes genv lenv (`f` bi))
308 readGEnv :: (GlobalEnv -> a) -> VM a
309 readGEnv f = VM $ \_ genv lenv -> return (Yes genv lenv (f genv))
311 setGEnv :: GlobalEnv -> VM ()
312 setGEnv genv = VM $ \_ _ lenv -> return (Yes genv lenv ())
314 updGEnv :: (GlobalEnv -> GlobalEnv) -> VM ()
315 updGEnv f = VM $ \_ genv lenv -> return (Yes (f genv) lenv ())
317 readLEnv :: (LocalEnv -> a) -> VM a
318 readLEnv f = VM $ \_ genv lenv -> return (Yes genv lenv (f lenv))
320 setLEnv :: LocalEnv -> VM ()
321 setLEnv lenv = VM $ \_ genv _ -> return (Yes genv lenv ())
323 updLEnv :: (LocalEnv -> LocalEnv) -> VM ()
324 updLEnv f = VM $ \_ genv lenv -> return (Yes genv (f lenv) ())
326 getInstEnv :: VM (InstEnv, InstEnv)
327 getInstEnv = readGEnv global_inst_env
329 getFamInstEnv :: VM FamInstEnvs
330 getFamInstEnv = readGEnv global_fam_inst_env
332 getBindName :: VM FastString
333 getBindName = readLEnv local_bind_name
335 inBind :: Id -> VM a -> VM a
337 = do updLEnv $ \env -> env { local_bind_name = occNameFS (getOccName id) }
340 cloneName :: (OccName -> OccName) -> Name -> VM Name
341 cloneName mk_occ name = liftM make (liftDs newUnique)
343 occ_name = mk_occ (nameOccName name)
345 make u | isExternalName name = mkExternalName u (nameModule name)
348 | otherwise = mkSystemName u occ_name
350 cloneId :: (OccName -> OccName) -> Id -> Type -> VM Id
353 name <- cloneName mk_occ (getName id)
354 let id' | isExportedId id = Id.mkExportedLocalId name ty
355 | otherwise = Id.mkLocalId name ty
358 cloneVar :: Var -> VM Var
359 cloneVar var = liftM (setIdUnique var) (liftDs newUnique)
361 newExportedVar :: OccName -> Type -> VM Var
362 newExportedVar occ_name ty
364 mod <- liftDs getModuleDs
365 u <- liftDs newUnique
367 let name = mkExternalName u mod occ_name noSrcSpan
369 return $ Id.mkExportedLocalId name ty
371 newLocalVar :: FastString -> Type -> VM Var
374 u <- liftDs newUnique
375 return $ mkSysLocal fs u ty
377 newDummyVar :: Type -> VM Var
378 newDummyVar = newLocalVar (fsLit "vv")
380 newTyVar :: FastString -> Kind -> VM Var
383 u <- liftDs newUnique
384 return $ mkTyVar (mkSysTvName u fs) k
386 defGlobalVar :: Var -> Var -> VM ()
387 defGlobalVar v v' = updGEnv $ \env ->
388 env { global_vars = extendVarEnv (global_vars env) v v'
389 , global_exported_vars = upd (global_exported_vars env)
392 upd env | isExportedId v = extendVarEnv env v (v, v')
395 lookupVar :: Var -> VM (Scope Var (Var, Var))
398 r <- readLEnv $ \env -> lookupVarEnv (local_vars env) v
400 Just e -> return (Local e)
401 Nothing -> liftM Global
402 . maybeCantVectoriseM "Variable not vectorised:" (ppr v)
403 . readGEnv $ \env -> lookupVarEnv (global_vars env) v
405 globalScalars :: VM VarSet
406 globalScalars = readGEnv global_scalars
408 lookupTyCon :: TyCon -> VM (Maybe TyCon)
410 | isUnLiftedTyCon tc || isTupleTyCon tc = return (Just tc)
412 | otherwise = readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
414 defTyCon :: TyCon -> TyCon -> VM ()
415 defTyCon tc tc' = updGEnv $ \env ->
416 env { global_tycons = extendNameEnv (global_tycons env) (tyConName tc) tc' }
418 lookupDataCon :: DataCon -> VM (Maybe DataCon)
420 | isTupleTyCon (dataConTyCon dc) = return (Just dc)
421 | otherwise = readGEnv $ \env -> lookupNameEnv (global_datacons env) (dataConName dc)
423 defDataCon :: DataCon -> DataCon -> VM ()
424 defDataCon dc dc' = updGEnv $ \env ->
425 env { global_datacons = extendNameEnv (global_datacons env) (dataConName dc) dc' }
427 lookupPrimPArray :: TyCon -> VM (Maybe TyCon)
428 lookupPrimPArray = liftBuiltinDs . primPArray
430 lookupPrimMethod :: TyCon -> String -> VM (Maybe Var)
431 lookupPrimMethod tycon = liftBuiltinDs . primMethod tycon
433 lookupTyConPA :: TyCon -> VM (Maybe Var)
434 lookupTyConPA tc = readGEnv $ \env -> lookupNameEnv (global_pa_funs env) (tyConName tc)
436 defTyConPA :: TyCon -> Var -> VM ()
437 defTyConPA tc pa = updGEnv $ \env ->
438 env { global_pa_funs = extendNameEnv (global_pa_funs env) (tyConName tc) pa }
440 defTyConPAs :: [(TyCon, Var)] -> VM ()
441 defTyConPAs ps = updGEnv $ \env ->
442 env { global_pa_funs = extendNameEnvList (global_pa_funs env)
443 [(tyConName tc, pa) | (tc, pa) <- ps] }
445 lookupTyVarPA :: Var -> VM (Maybe CoreExpr)
446 lookupTyVarPA tv = readLEnv $ \env -> lookupVarEnv (local_tyvar_pa env) tv
448 lookupTyConPR :: TyCon -> VM (Maybe Var)
449 lookupTyConPR tc = readGEnv $ \env -> lookupNameEnv (global_pr_funs env) (tyConName tc)
451 lookupBoxedTyCon :: TyCon -> VM (Maybe TyCon)
452 lookupBoxedTyCon tc = readGEnv $ \env -> lookupNameEnv (global_boxed_tycons env)
455 defLocalTyVar :: TyVar -> VM ()
456 defLocalTyVar tv = updLEnv $ \env ->
457 env { local_tyvars = tv : local_tyvars env
458 , local_tyvar_pa = local_tyvar_pa env `delVarEnv` tv
461 defLocalTyVarWithPA :: TyVar -> CoreExpr -> VM ()
462 defLocalTyVarWithPA tv pa = updLEnv $ \env ->
463 env { local_tyvars = tv : local_tyvars env
464 , local_tyvar_pa = extendVarEnv (local_tyvar_pa env) tv pa
467 localTyVars :: VM [TyVar]
468 localTyVars = readLEnv (reverse . local_tyvars)
470 -- Look up the dfun of a class instance.
472 -- The match must be unique - ie, match exactly one instance - but the
473 -- type arguments used for matching may be more specific than those of
474 -- the class instance declaration. The found class instances must not have
475 -- any type variables in the instance context that do not appear in the
476 -- instances head (i.e., no flexi vars); for details for what this means,
477 -- see the docs at InstEnv.lookupInstEnv.
479 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
481 = do { instEnv <- getInstEnv
482 ; case lookupInstEnv instEnv cls tys of
483 ([(inst, inst_tys)], _)
484 | noFlexiVar -> return (instanceDFunId inst, inst_tys')
485 | otherwise -> pprPanic "VectMonad.lookupInst: flexi var: "
486 (ppr $ mkTyConApp (classTyCon cls) tys)
488 inst_tys' = [ty | Right ty <- inst_tys]
489 noFlexiVar = all isRight inst_tys
491 pprPanic "VectMonad.lookupInst: not found " (ppr cls <+> ppr tys)
494 isRight (Left _) = False
495 isRight (Right _) = True
497 -- Look up the representation tycon of a family instance.
499 -- The match must be unique - ie, match exactly one instance - but the
500 -- type arguments used for matching may be more specific than those of
501 -- the family instance declaration.
503 -- Return the instance tycon and its type instance. For example, if we have
505 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
507 -- then we have a coercion (ie, type instance of family instance coercion)
509 -- :Co:R42T Int :: T [Int] ~ :R42T Int
511 -- which implies that :R42T was declared as 'data instance T [a]'.
513 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
514 lookupFamInst tycon tys
515 = ASSERT( isOpenTyCon tycon )
516 do { instEnv <- getFamInstEnv
517 ; case lookupFamInstEnv instEnv tycon tys of
518 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
520 pprPanic "VectMonad.lookupFamInst: not found: "
521 (ppr $ mkTyConApp tycon tys)
524 initV :: PackageId -> HscEnv -> ModGuts -> VectInfo -> VM a -> IO (Maybe (VectInfo, a))
525 initV pkg hsc_env guts info p
527 -- XXX: ignores error messages and warnings, check that this is
528 -- indeed ok (the use of "Just r" suggests so)
529 (_,Just r) <- initDs hsc_env (mg_module guts)
538 builtins <- initBuiltins pkg
539 builtin_vars <- initBuiltinVars builtins
540 builtin_tycons <- initBuiltinTyCons builtins
541 let builtin_datacons = initBuiltinDataCons builtins
542 builtin_pas <- initBuiltinPAs builtins
543 builtin_prs <- initBuiltinPRs builtins
544 builtin_boxed <- initBuiltinBoxedTyCons builtins
545 builtin_scalars <- initBuiltinScalars builtins
547 eps <- liftIO $ hscEPS hsc_env
548 let famInstEnvs = (eps_fam_inst_env eps, mg_fam_inst_env guts)
549 instEnvs = (eps_inst_env eps, mg_inst_env guts)
551 let genv = extendImportedVarsEnv builtin_vars
552 . extendScalars builtin_scalars
553 . extendTyConsEnv builtin_tycons
554 . extendDataConsEnv builtin_datacons
555 . extendPAFunsEnv builtin_pas
556 . setPRFunsEnv builtin_prs
557 . setBoxedTyConsEnv builtin_boxed
558 $ initGlobalEnv info instEnvs famInstEnvs
560 r <- runVM p builtins genv emptyLocalEnv
562 Yes genv _ x -> return $ Just (new_info genv, x)
565 new_info genv = updVectInfo genv (mg_types guts) info