1 {-# LANGUAGE NamedFieldPuns #-}
3 -- | The Vectorisation monad.
7 noV, traceNoV, ensureV, traceEnsureV, tryV, maybeV, traceMaybeV, orElseV,
8 onlyIfV, fixV, localV, closedV,
9 initV, cantVectorise, maybeCantVectorise, maybeCantVectoriseM,
11 cloneName, cloneId, cloneVar,
12 newExportedVar, newLocalVar, newLocalVars, newDummyVar, newTyVar,
14 Builtins(..), sumTyCon, prodTyCon, prodDataCon,
15 selTy, selReplicate, selPick, selTags, selElements,
16 combinePDVar, scalarZip, closureCtrFun,
20 readGEnv, setGEnv, updGEnv,
22 readLEnv, setLEnv, updLEnv,
26 lookupVar, defGlobalVar, globalScalars,
27 lookupTyCon, defTyCon,
28 lookupDataCon, defDataCon,
29 lookupTyConPA, defTyConPA, defTyConPAs,
32 lookupPrimMethod, lookupPrimPArray,
33 lookupTyVarPA, defLocalTyVar, defLocalTyVarWithPA, localTyVars,
35 lookupInst, lookupFamInst
38 #include "HsVersions.h"
44 import HscTypes hiding ( MonadThings(..) )
45 import Module ( PackageId )
65 import SrcLoc ( noSrcSpan )
70 -- The Vectorisation Monad ----------------------------------------------------
72 -- Vectorisation can either succeed with new envionment and a value,
73 -- or return with failure.
75 data VResult a = Yes GlobalEnv LocalEnv a | No
77 newtype VM a = VM { runVM :: Builtins -> GlobalEnv -> LocalEnv -> DsM (VResult a) }
79 instance Monad VM where
80 return x = VM $ \_ genv lenv -> return (Yes genv lenv x)
81 VM p >>= f = VM $ \bi genv lenv -> do
84 Yes genv' lenv' x -> runVM (f x) bi genv' lenv'
88 -- | Throw an error saying we can't vectorise something
89 cantVectorise :: String -> SDoc -> a
90 cantVectorise s d = pgmError
92 $ vcat [text "*** Vectorisation error ***",
93 nest 4 $ sep [text s, nest 4 d]]
95 maybeCantVectorise :: String -> SDoc -> Maybe a -> a
96 maybeCantVectorise s d Nothing = cantVectorise s d
97 maybeCantVectorise _ _ (Just x) = x
99 maybeCantVectoriseM :: Monad m => String -> SDoc -> m (Maybe a) -> m a
100 maybeCantVectoriseM s d p
105 Nothing -> cantVectorise s d
108 -- Control --------------------------------------------------------------------
109 -- | Return some result saying we've failed.
111 noV = VM $ \_ _ _ -> return No
113 traceNoV :: String -> SDoc -> VM a
114 traceNoV s d = pprTrace s d noV
117 -- | If True then carry on, otherwise fail.
118 ensureV :: Bool -> VM ()
120 ensureV True = return ()
123 -- | If True then return the first argument, otherwise fail.
124 onlyIfV :: Bool -> VM a -> VM a
125 onlyIfV b p = ensureV b >> p
127 traceEnsureV :: String -> SDoc -> Bool -> VM ()
128 traceEnsureV s d False = traceNoV s d
129 traceEnsureV _ _ True = return ()
132 -- | Try some vectorisation computaton.
133 -- If it succeeds then return Just the result,
134 -- otherwise return Nothing.
135 tryV :: VM a -> VM (Maybe a)
136 tryV (VM p) = VM $ \bi genv lenv ->
140 Yes genv' lenv' x -> return (Yes genv' lenv' (Just x))
141 No -> return (Yes genv lenv Nothing)
144 maybeV :: VM (Maybe a) -> VM a
145 maybeV p = maybe noV return =<< p
147 traceMaybeV :: String -> SDoc -> VM (Maybe a) -> VM a
148 traceMaybeV s d p = maybe (traceNoV s d) return =<< p
150 orElseV :: VM a -> VM a -> VM a
151 orElseV p q = maybe q return =<< tryV p
153 fixV :: (a -> VM a) -> VM a
154 fixV f = VM (\bi genv lenv -> fixDs $ \r -> runVM (f (unYes r)) bi genv lenv )
156 -- NOTE: It is essential that we are lazy in r above so do not replace
157 -- calls to this function by an explicit case.
158 unYes (Yes _ _ x) = x
159 unYes No = panic "VectMonad.fixV: no result"
162 -- Local Environments ---------------------------------------------------------
163 -- | Perform a computation in its own local environment.
164 -- This does not alter the environment of the current state.
165 localV :: VM a -> VM a
172 -- | Perform a computation in an empty local environment.
173 closedV :: VM a -> VM a
176 setLEnv (emptyLocalEnv { local_bind_name = local_bind_name env })
181 -- Lifting --------------------------------------------------------------------
182 -- | Lift a desugaring computation into the vectorisation monad.
183 liftDs :: DsM a -> VM a
184 liftDs p = VM $ \_ genv lenv -> do { x <- p; return (Yes genv lenv x) }
188 -- Builtins -------------------------------------------------------------------
189 -- Operations on Builtins
190 liftBuiltinDs :: (Builtins -> DsM a) -> VM a
191 liftBuiltinDs p = VM $ \bi genv lenv -> do { x <- p bi; return (Yes genv lenv x)}
194 -- | Project something from the set of builtins.
195 builtin :: (Builtins -> a) -> VM a
196 builtin f = VM $ \bi genv lenv -> return (Yes genv lenv (f bi))
198 builtins :: (a -> Builtins -> b) -> VM (a -> b)
199 builtins f = VM $ \bi genv lenv -> return (Yes genv lenv (`f` bi))
202 -- Environments ---------------------------------------------------------------
203 -- | Project something from the global environment.
204 readGEnv :: (GlobalEnv -> a) -> VM a
205 readGEnv f = VM $ \_ genv lenv -> return (Yes genv lenv (f genv))
207 setGEnv :: GlobalEnv -> VM ()
208 setGEnv genv = VM $ \_ _ lenv -> return (Yes genv lenv ())
210 updGEnv :: (GlobalEnv -> GlobalEnv) -> VM ()
211 updGEnv f = VM $ \_ genv lenv -> return (Yes (f genv) lenv ())
214 -- | Project something from the local environment.
215 readLEnv :: (LocalEnv -> a) -> VM a
216 readLEnv f = VM $ \_ genv lenv -> return (Yes genv lenv (f lenv))
218 -- | Set the local environment.
219 setLEnv :: LocalEnv -> VM ()
220 setLEnv lenv = VM $ \_ genv _ -> return (Yes genv lenv ())
222 -- | Update the enviroment using a provided function.
223 updLEnv :: (LocalEnv -> LocalEnv) -> VM ()
224 updLEnv f = VM $ \_ genv lenv -> return (Yes genv (f lenv) ())
227 -- InstEnv --------------------------------------------------------------------
228 getInstEnv :: VM (InstEnv, InstEnv)
229 getInstEnv = readGEnv global_inst_env
231 getFamInstEnv :: VM FamInstEnvs
232 getFamInstEnv = readGEnv global_fam_inst_env
235 -- Names ----------------------------------------------------------------------
236 -- | Get the name of the local binding currently being vectorised.
237 getBindName :: VM FastString
238 getBindName = readLEnv local_bind_name
240 inBind :: Id -> VM a -> VM a
242 = do updLEnv $ \env -> env { local_bind_name = occNameFS (getOccName id) }
245 cloneName :: (OccName -> OccName) -> Name -> VM Name
246 cloneName mk_occ name = liftM make (liftDs newUnique)
248 occ_name = mk_occ (nameOccName name)
250 make u | isExternalName name = mkExternalName u (nameModule name)
253 | otherwise = mkSystemName u occ_name
255 cloneId :: (OccName -> OccName) -> Id -> Type -> VM Id
258 name <- cloneName mk_occ (getName id)
259 let id' | isExportedId id = Id.mkExportedLocalId name ty
260 | otherwise = Id.mkLocalId name ty
263 -- Make a fresh instance of this var, with a new unique.
264 cloneVar :: Var -> VM Var
265 cloneVar var = liftM (setIdUnique var) (liftDs newUnique)
267 newExportedVar :: OccName -> Type -> VM Var
268 newExportedVar occ_name ty
270 mod <- liftDs getModuleDs
271 u <- liftDs newUnique
273 let name = mkExternalName u mod occ_name noSrcSpan
275 return $ Id.mkExportedLocalId name ty
277 newLocalVar :: FastString -> Type -> VM Var
280 u <- liftDs newUnique
281 return $ mkSysLocal fs u ty
283 newLocalVars :: FastString -> [Type] -> VM [Var]
284 newLocalVars fs = mapM (newLocalVar fs)
286 newDummyVar :: Type -> VM Var
287 newDummyVar = newLocalVar (fsLit "vv")
289 newTyVar :: FastString -> Kind -> VM Var
292 u <- liftDs newUnique
293 return $ mkTyVar (mkSysTvName u fs) k
296 -- | Add a mapping between a global var and its vectorised version to the state.
297 defGlobalVar :: Var -> Var -> VM ()
298 defGlobalVar v v' = updGEnv $ \env ->
299 env { global_vars = extendVarEnv (global_vars env) v v'
300 , global_exported_vars = upd (global_exported_vars env)
303 upd env | isExportedId v = extendVarEnv env v (v, v')
306 -- Var ------------------------------------------------------------------------
307 -- | Lookup the vectorised and\/or lifted versions of this variable.
308 -- If it's in the global environment we get the vectorised version.
309 -- If it's in the local environment we get both the vectorised and lifted version.
311 lookupVar :: Var -> VM (Scope Var (Var, Var))
313 = do r <- readLEnv $ \env -> lookupVarEnv (local_vars env) v
315 Just e -> return (Local e)
316 Nothing -> liftM Global
317 . maybeCantVectoriseVarM v
318 . readGEnv $ \env -> lookupVarEnv (global_vars env) v
320 maybeCantVectoriseVarM :: Monad m => Var -> m (Maybe Var) -> m Var
321 maybeCantVectoriseVarM v p
329 | Just _ <- isClassOpId_maybe var
330 = cantVectorise "ClassOpId not vectorised:" (ppr var)
333 = cantVectorise "Variable not vectorised:" (ppr var)
335 -------------------------------------------------------------------------------
336 globalScalars :: VM VarSet
337 globalScalars = readGEnv global_scalars
339 lookupTyCon :: TyCon -> VM (Maybe TyCon)
341 | isUnLiftedTyCon tc || isTupleTyCon tc = return (Just tc)
343 | otherwise = readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
345 defTyCon :: TyCon -> TyCon -> VM ()
346 defTyCon tc tc' = updGEnv $ \env ->
347 env { global_tycons = extendNameEnv (global_tycons env) (tyConName tc) tc' }
349 lookupDataCon :: DataCon -> VM (Maybe DataCon)
351 | isTupleTyCon (dataConTyCon dc) = return (Just dc)
352 | otherwise = readGEnv $ \env -> lookupNameEnv (global_datacons env) (dataConName dc)
354 defDataCon :: DataCon -> DataCon -> VM ()
355 defDataCon dc dc' = updGEnv $ \env ->
356 env { global_datacons = extendNameEnv (global_datacons env) (dataConName dc) dc' }
358 lookupPrimPArray :: TyCon -> VM (Maybe TyCon)
359 lookupPrimPArray = liftBuiltinDs . primPArray
361 lookupPrimMethod :: TyCon -> String -> VM (Maybe Var)
362 lookupPrimMethod tycon = liftBuiltinDs . primMethod tycon
364 lookupTyConPA :: TyCon -> VM (Maybe Var)
365 lookupTyConPA tc = readGEnv $ \env -> lookupNameEnv (global_pa_funs env) (tyConName tc)
367 defTyConPA :: TyCon -> Var -> VM ()
368 defTyConPA tc pa = updGEnv $ \env ->
369 env { global_pa_funs = extendNameEnv (global_pa_funs env) (tyConName tc) pa }
371 defTyConPAs :: [(TyCon, Var)] -> VM ()
372 defTyConPAs ps = updGEnv $ \env ->
373 env { global_pa_funs = extendNameEnvList (global_pa_funs env)
374 [(tyConName tc, pa) | (tc, pa) <- ps] }
376 lookupTyVarPA :: Var -> VM (Maybe CoreExpr)
377 lookupTyVarPA tv = readLEnv $ \env -> lookupVarEnv (local_tyvar_pa env) tv
379 lookupTyConPR :: TyCon -> VM (Maybe Var)
380 lookupTyConPR tc = readGEnv $ \env -> lookupNameEnv (global_pr_funs env) (tyConName tc)
382 lookupBoxedTyCon :: TyCon -> VM (Maybe TyCon)
383 lookupBoxedTyCon tc = readGEnv $ \env -> lookupNameEnv (global_boxed_tycons env)
386 defLocalTyVar :: TyVar -> VM ()
387 defLocalTyVar tv = updLEnv $ \env ->
388 env { local_tyvars = tv : local_tyvars env
389 , local_tyvar_pa = local_tyvar_pa env `delVarEnv` tv
392 defLocalTyVarWithPA :: TyVar -> CoreExpr -> VM ()
393 defLocalTyVarWithPA tv pa = updLEnv $ \env ->
394 env { local_tyvars = tv : local_tyvars env
395 , local_tyvar_pa = extendVarEnv (local_tyvar_pa env) tv pa
398 localTyVars :: VM [TyVar]
399 localTyVars = readLEnv (reverse . local_tyvars)
401 -- Look up the dfun of a class instance.
403 -- The match must be unique - ie, match exactly one instance - but the
404 -- type arguments used for matching may be more specific than those of
405 -- the class instance declaration. The found class instances must not have
406 -- any type variables in the instance context that do not appear in the
407 -- instances head (i.e., no flexi vars); for details for what this means,
408 -- see the docs at InstEnv.lookupInstEnv.
410 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
412 = do { instEnv <- getInstEnv
413 ; case lookupInstEnv instEnv cls tys of
414 ([(inst, inst_tys)], _)
415 | noFlexiVar -> return (instanceDFunId inst, inst_tys')
416 | otherwise -> pprPanic "VectMonad.lookupInst: flexi var: "
417 (ppr $ mkTyConApp (classTyCon cls) tys)
419 inst_tys' = [ty | Right ty <- inst_tys]
420 noFlexiVar = all isRight inst_tys
422 pprPanic "VectMonad.lookupInst: not found " (ppr cls <+> ppr tys)
425 isRight (Left _) = False
426 isRight (Right _) = True
428 -- Look up the representation tycon of a family instance.
430 -- The match must be unique - ie, match exactly one instance - but the
431 -- type arguments used for matching may be more specific than those of
432 -- the family instance declaration.
434 -- Return the instance tycon and its type instance. For example, if we have
436 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
438 -- then we have a coercion (ie, type instance of family instance coercion)
440 -- :Co:R42T Int :: T [Int] ~ :R42T Int
442 -- which implies that :R42T was declared as 'data instance T [a]'.
444 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
445 lookupFamInst tycon tys
446 = ASSERT( isOpenTyCon tycon )
447 do { instEnv <- getFamInstEnv
448 ; case lookupFamInstEnv instEnv tycon tys of
449 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
451 pprPanic "VectMonad.lookupFamInst: not found: "
452 (ppr $ mkTyConApp tycon tys)
456 -- | Run a vectorisation computation.
457 initV :: PackageId -> HscEnv -> ModGuts -> VectInfo -> VM a -> IO (Maybe (VectInfo, a))
458 initV pkg hsc_env guts info p
460 -- XXX: ignores error messages and warnings, check that this is
461 -- indeed ok (the use of "Just r" suggests so)
462 (_,Just r) <- initDs hsc_env (mg_module guts)
471 builtins <- initBuiltins pkg
472 builtin_vars <- initBuiltinVars builtins
473 builtin_tycons <- initBuiltinTyCons builtins
474 let builtin_datacons = initBuiltinDataCons builtins
475 builtin_boxed <- initBuiltinBoxedTyCons builtins
476 builtin_scalars <- initBuiltinScalars builtins
478 eps <- liftIO $ hscEPS hsc_env
479 let famInstEnvs = (eps_fam_inst_env eps, mg_fam_inst_env guts)
480 instEnvs = (eps_inst_env eps, mg_inst_env guts)
482 builtin_prs <- initBuiltinPRs builtins instEnvs
483 builtin_pas <- initBuiltinPAs builtins instEnvs
485 let genv = extendImportedVarsEnv builtin_vars
486 . extendScalars builtin_scalars
487 . extendTyConsEnv builtin_tycons
488 . extendDataConsEnv builtin_datacons
489 . extendPAFunsEnv builtin_pas
490 . setPRFunsEnv builtin_prs
491 . setBoxedTyConsEnv builtin_boxed
492 $ initGlobalEnv info instEnvs famInstEnvs
494 r <- runVM p builtins genv emptyLocalEnv
496 Yes genv _ x -> return $ Just (new_info genv, x)
499 new_info genv = updVectInfo genv (mg_types guts) info