4 noV, tryV, maybeV, orElseV, localV, closedV, initV,
7 Builtins(..), paDictTyCon,
11 readGEnv, setGEnv, updGEnv,
14 readLEnv, setLEnv, updLEnv,
17 lookupTyVarPA, extendTyVarPA, deleteTyVarPA,
19 lookupInst, lookupFamInst
22 #include "HsVersions.h"
45 -- ----------------------------------------------------------------------------
46 -- Vectorisation monad
48 data Builtins = Builtins {
51 , closureTyCon :: TyCon
53 , applyClosureVar :: Var
54 , mkClosurePVar :: Var
55 , applyClosurePVar :: Var
57 , replicatePAVar :: Var
60 paDictTyCon :: Builtins -> TyCon
61 paDictTyCon = classTyCon . paClass
63 initBuiltins :: DsM Builtins
66 parrayTyCon <- dsLookupTyCon parrayTyConName
67 paClass <- dsLookupClass paClassName
68 closureTyCon <- dsLookupTyCon closureTyConName
70 mkClosureVar <- dsLookupGlobalId mkClosureName
71 applyClosureVar <- dsLookupGlobalId applyClosureName
72 mkClosurePVar <- dsLookupGlobalId mkClosurePName
73 applyClosurePVar <- dsLookupGlobalId applyClosurePName
74 lengthPAVar <- dsLookupGlobalId lengthPAName
75 replicatePAVar <- dsLookupGlobalId replicatePAName
78 parrayTyCon = parrayTyCon
80 , closureTyCon = closureTyCon
81 , mkClosureVar = mkClosureVar
82 , applyClosureVar = applyClosureVar
83 , mkClosurePVar = mkClosurePVar
84 , applyClosurePVar = applyClosurePVar
85 , lengthPAVar = lengthPAVar
86 , replicatePAVar = replicatePAVar
89 data GlobalEnv = GlobalEnv {
90 -- Mapping from global variables to their vectorised versions.
92 global_vars :: VarEnv CoreExpr
94 -- Exported variables which have a vectorised version
96 , global_exported_vars :: VarEnv (Var, Var)
98 -- Mapping from TyCons to their vectorised versions.
99 -- TyCons which do not have to be vectorised are mapped to
102 , global_tycons :: NameEnv TyCon
104 -- Mapping from TyCons to their PA dictionaries
106 , global_tycon_pa :: NameEnv CoreExpr
108 -- External package inst-env & home-package inst-env for class
111 , global_inst_env :: (InstEnv, InstEnv)
113 -- External package inst-env & home-package inst-env for family
116 , global_fam_inst_env :: FamInstEnvs
119 data LocalEnv = LocalEnv {
120 -- Mapping from local variables to their vectorised and
123 local_vars :: VarEnv (CoreExpr, CoreExpr)
125 -- Mapping from tyvars to their PA dictionaries
126 , local_tyvar_pa :: VarEnv CoreExpr
130 initGlobalEnv :: VectInfo -> (InstEnv, InstEnv) -> FamInstEnvs -> GlobalEnv
131 initGlobalEnv info instEnvs famInstEnvs
133 global_vars = mapVarEnv (Var . snd) $ vectInfoCCVar info
134 , global_exported_vars = emptyVarEnv
135 , global_tycons = mapNameEnv snd $ vectInfoCCTyCon info
136 , global_tycon_pa = emptyNameEnv
137 , global_inst_env = instEnvs
138 , global_fam_inst_env = famInstEnvs
141 emptyLocalEnv = LocalEnv {
142 local_vars = emptyVarEnv
143 , local_tyvar_pa = emptyVarEnv
147 updVectInfo :: GlobalEnv -> TypeEnv -> VectInfo -> VectInfo
148 updVectInfo env tyenv info
150 vectInfoCCVar = global_exported_vars env
151 , vectInfoCCTyCon = tc_env
154 tc_env = mkNameEnv [(tc_name, (tc,tc'))
155 | tc <- typeEnvTyCons tyenv
156 , let tc_name = tyConName tc
157 , Just tc' <- [lookupNameEnv (global_tycons env) tc_name]]
159 data VResult a = Yes GlobalEnv LocalEnv a | No
161 newtype VM a = VM { runVM :: Builtins -> GlobalEnv -> LocalEnv -> DsM (VResult a) }
163 instance Monad VM where
164 return x = VM $ \bi genv lenv -> return (Yes genv lenv x)
165 VM p >>= f = VM $ \bi genv lenv -> do
168 Yes genv' lenv' x -> runVM (f x) bi genv' lenv'
172 noV = VM $ \_ _ _ -> return No
174 tryV :: VM a -> VM (Maybe a)
175 tryV (VM p) = VM $ \bi genv lenv ->
179 Yes genv' lenv' x -> return (Yes genv' lenv' (Just x))
180 No -> return (Yes genv lenv Nothing)
182 maybeV :: VM (Maybe a) -> VM a
183 maybeV p = maybe noV return =<< p
185 orElseV :: VM a -> VM a -> VM a
186 orElseV p q = maybe q return =<< tryV p
188 localV :: VM a -> VM a
195 closedV :: VM a -> VM a
198 setLEnv emptyLocalEnv
203 liftDs :: DsM a -> VM a
204 liftDs p = VM $ \bi genv lenv -> do { x <- p; return (Yes genv lenv x) }
206 builtin :: (Builtins -> a) -> VM a
207 builtin f = VM $ \bi genv lenv -> return (Yes genv lenv (f bi))
209 readGEnv :: (GlobalEnv -> a) -> VM a
210 readGEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f genv))
212 setGEnv :: GlobalEnv -> VM ()
213 setGEnv genv = VM $ \_ _ lenv -> return (Yes genv lenv ())
215 updGEnv :: (GlobalEnv -> GlobalEnv) -> VM ()
216 updGEnv f = VM $ \_ genv lenv -> return (Yes (f genv) lenv ())
218 readLEnv :: (LocalEnv -> a) -> VM a
219 readLEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f lenv))
221 setLEnv :: LocalEnv -> VM ()
222 setLEnv lenv = VM $ \_ genv _ -> return (Yes genv lenv ())
224 updLEnv :: (LocalEnv -> LocalEnv) -> VM ()
225 updLEnv f = VM $ \_ genv lenv -> return (Yes genv (f lenv) ())
227 getInstEnv :: VM (InstEnv, InstEnv)
228 getInstEnv = readGEnv global_inst_env
230 getFamInstEnv :: VM FamInstEnvs
231 getFamInstEnv = readGEnv global_fam_inst_env
233 newLocalVar :: FastString -> Type -> VM Var
236 u <- liftDs newUnique
237 return $ mkSysLocal fs u ty
239 newTyVar :: FastString -> Kind -> VM Var
242 u <- liftDs newUnique
243 return $ mkTyVar (mkSysTvName u fs) k
245 lookupTyCon :: TyCon -> VM (Maybe TyCon)
246 lookupTyCon tc = readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
248 lookupTyVarPA :: Var -> VM (Maybe CoreExpr)
249 lookupTyVarPA tv = readLEnv $ \env -> lookupVarEnv (local_tyvar_pa env) tv
251 extendTyVarPA :: Var -> CoreExpr -> VM ()
252 extendTyVarPA tv pa = updLEnv $ \env -> env { local_tyvar_pa = extendVarEnv (local_tyvar_pa env) tv pa }
254 deleteTyVarPA :: Var -> VM ()
255 deleteTyVarPA tv = updLEnv $ \env -> env { local_tyvar_pa = delVarEnv (local_tyvar_pa env) tv }
257 -- Look up the dfun of a class instance.
259 -- The match must be unique - ie, match exactly one instance - but the
260 -- type arguments used for matching may be more specific than those of
261 -- the class instance declaration. The found class instances must not have
262 -- any type variables in the instance context that do not appear in the
263 -- instances head (i.e., no flexi vars); for details for what this means,
264 -- see the docs at InstEnv.lookupInstEnv.
266 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
268 = do { instEnv <- getInstEnv
269 ; case lookupInstEnv instEnv cls tys of
270 ([(inst, inst_tys)], _)
271 | noFlexiVar -> return (instanceDFunId inst, inst_tys')
272 | otherwise -> pprPanic "VectMonad.lookupInst: flexi var: "
273 (ppr $ mkTyConApp (classTyCon cls) tys)
275 inst_tys' = [ty | Right ty <- inst_tys]
276 noFlexiVar = all isRight inst_tys
280 isRight (Left _) = False
281 isRight (Right _) = True
283 -- Look up the representation tycon of a family instance.
285 -- The match must be unique - ie, match exactly one instance - but the
286 -- type arguments used for matching may be more specific than those of
287 -- the family instance declaration.
289 -- Return the instance tycon and its type instance. For example, if we have
291 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
293 -- then we have a coercion (ie, type instance of family instance coercion)
295 -- :Co:R42T Int :: T [Int] ~ :R42T Int
297 -- which implies that :R42T was declared as 'data instance T [a]'.
299 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
300 lookupFamInst tycon tys
301 = ASSERT( isOpenTyCon tycon )
302 do { instEnv <- getFamInstEnv
303 ; case lookupFamInstEnv instEnv tycon tys of
304 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
306 pprPanic "VectMonad.lookupFamInst: not found: "
307 (ppr $ mkTyConApp tycon tys)
310 initV :: HscEnv -> ModGuts -> VectInfo -> VM a -> IO (Maybe (VectInfo, a))
311 initV hsc_env guts info p
313 eps <- hscEPS hsc_env
314 let famInstEnvs = (eps_fam_inst_env eps, mg_fam_inst_env guts)
315 let instEnvs = (eps_inst_env eps, mg_inst_env guts)
317 Just r <- initDs hsc_env (mg_module guts)
320 (go instEnvs famInstEnvs)
324 go instEnvs famInstEnvs =
326 builtins <- initBuiltins
327 r <- runVM p builtins (initGlobalEnv info instEnvs famInstEnvs)
330 Yes genv _ x -> return $ Just (new_info genv, x)
333 new_info genv = updVectInfo genv (mg_types guts) info