1 module Vectorise( vectorise )
4 #include "HsVersions.h"
12 import CoreLint ( showPass, endPass )
21 import Name ( mkSysTvName )
25 import DsMonad hiding (mapAndUnzipM)
31 import Control.Monad ( liftM, liftM2, mapAndUnzipM, zipWithM_ )
32 import Data.Maybe ( maybeToList )
34 vectorise :: HscEnv -> ModGuts -> IO ModGuts
35 vectorise hsc_env guts
36 | not (Opt_Vectorise `dopt` dflags) = return guts
39 showPass dflags "Vectorisation"
41 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
42 Just (info', guts') <- initV hsc_env guts info (vectModule guts)
43 endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
44 return $ guts' { mg_vect_info = info' }
46 dflags = hsc_dflags hsc_env
48 vectModule :: ModGuts -> VM ModGuts
49 vectModule guts = return guts
51 -- ----------------------------------------------------------------------------
54 vectBndr :: Var -> VM (Var, Var)
57 vty <- vectType (idType v)
58 lty <- mkPArrayType vty
59 let vv = v `Id.setIdType` vty
60 lv = v `Id.setIdType` lty
64 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (Var vv, Var lv) }
66 vectBndrIn :: Var -> VM a -> VM (Var, Var, a)
70 (vv, lv) <- vectBndr v
74 vectBndrsIn :: [Var] -> VM a -> VM ([Var], [Var], a)
78 (vvs, lvs) <- mapAndUnzipM vectBndr vs
82 -- ----------------------------------------------------------------------------
85 replicateP :: CoreExpr -> CoreExpr -> VM CoreExpr
88 dict <- paDictOfType ty
89 rep <- builtin replicatePAVar
90 return $ mkApps (Var rep) [Type ty, dict, expr, len]
94 capply :: (CoreExpr, CoreExpr) -> (CoreExpr, CoreExpr) -> VM (CoreExpr, CoreExpr)
95 capply (vfn, lfn) (varg, larg)
97 apply <- builtin applyClosureVar
98 applyP <- builtin applyClosurePVar
99 return (mkApps (Var apply) [Type arg_ty, Type res_ty, vfn, varg],
100 mkApps (Var applyP) [Type arg_ty, Type res_ty, lfn, larg])
103 (arg_ty, res_ty) = splitClosureTy fn_ty
105 vectVar :: CoreExpr -> Var -> VM (CoreExpr, CoreExpr)
106 vectVar lc v = local v `orElseV` global v
108 local v = maybeV (readLEnv $ \env -> lookupVarEnv (local_vars env) v)
110 vexpr <- maybeV (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
111 lexpr <- replicateP vexpr lc
112 return (vexpr, lexpr)
114 vectPolyVar :: CoreExpr -> Var -> [Type] -> VM (CoreExpr, CoreExpr)
117 r <- readLEnv $ \env -> lookupVarEnv (local_vars env) v
119 Just (vexpr, lexpr) -> liftM2 (,) (mk_app vexpr) (mk_app lexpr)
122 poly <- maybeV (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
124 lexpr <- replicateP vexpr lc
125 return (vexpr, lexpr)
128 vtys <- mapM vectType tys
129 dicts <- mapM paDictOfType vtys
130 return $ mkApps e [arg | (vty, dict) <- zip vtys dicts
131 , arg <- [Type vty, dict]]
133 abstractOverTyVars :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
134 abstractOverTyVars tvs p
136 mdicts <- mapM mk_dict_var tvs
137 zipWithM_ (\tv -> maybe (deleteTyVarPA tv) (extendTyVarPA tv . Var)) tvs mdicts
141 r <- paDictArgType tv
143 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
144 Nothing -> return Nothing
146 mk_lams mdicts = mkLams [arg | (tv, mdict) <- zip tvs mdicts
147 , arg <- tv : maybeToList mdict]
150 vectPolyExpr :: CoreExpr -> CoreExprWithFVs -> VM (CoreExpr, CoreExpr)
153 . abstractOverTyVars tvs $ \mk_lams ->
154 -- FIXME: shadowing (tvs in lc)
156 (vmono, lmono) <- vectExpr lc mono
157 return $ (mk_lams vmono, mk_lams lmono)
159 (tvs, mono) = collectAnnTypeBinders expr
161 vectExpr :: CoreExpr -> CoreExprWithFVs -> VM (CoreExpr, CoreExpr)
162 vectExpr lc (_, AnnType ty)
165 return (Type vty, Type vty)
167 vectExpr lc (_, AnnVar v) = vectVar lc v
169 vectExpr lc (_, AnnLit lit)
172 lexpr <- replicateP vexpr lc
173 return (vexpr, lexpr)
175 vectExpr lc (_, AnnNote note expr)
177 (vexpr, lexpr) <- vectExpr lc expr
178 return (Note note vexpr, Note note lexpr)
180 vectExpr lc e@(_, AnnApp _ arg)
182 = vectTyAppExpr lc fn tys
184 (fn, tys) = collectAnnTypeArgs e
186 vectExpr lc (_, AnnApp fn arg)
188 fn' <- vectExpr lc fn
189 arg' <- vectExpr lc arg
192 vectExpr lc (_, AnnCase expr bndr ty alts)
193 = panic "vectExpr: case"
195 vectExpr lc (_, AnnLet (AnnNonRec bndr rhs) body)
197 (vrhs, lrhs) <- vectPolyExpr lc rhs
198 (vbndr, lbndr, (vbody, lbody)) <- vectBndrIn bndr (vectExpr lc body)
199 return (Let (NonRec vbndr vrhs) vbody,
200 Let (NonRec lbndr lrhs) lbody)
202 vectExpr lc (_, AnnLet (AnnRec prs) body)
204 (vbndrs, lbndrs, (vrhss, vbody, lrhss, lbody)) <- vectBndrsIn bndrs vect
205 return (Let (Rec (zip vbndrs vrhss)) vbody,
206 Let (Rec (zip lbndrs lrhss)) lbody)
208 (bndrs, rhss) = unzip prs
211 (vrhss, lrhss) <- mapAndUnzipM (vectExpr lc) rhss
212 (vbody, lbody) <- vectPolyExpr lc body
213 return (vrhss, vbody, lrhss, lbody)
215 vectExpr lc e@(_, AnnLam bndr body)
216 | isTyVar bndr = pprPanic "vectExpr" (ppr $ deAnnotate e)
218 vectTyAppExpr :: CoreExpr -> CoreExprWithFVs -> [Type] -> VM (CoreExpr, CoreExpr)
219 vectTyAppExpr lc (_, AnnVar v) tys = vectPolyVar lc v tys
220 vectTyAppExpr lc e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
222 -- ----------------------------------------------------------------------------
225 vectTyCon :: TyCon -> VM TyCon
227 | isFunTyCon tc = builtin closureTyCon
228 | isBoxedTupleTyCon tc = return tc
229 | isUnLiftedTyCon tc = return tc
233 Just tc' -> return tc'
235 -- FIXME: just for now
236 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
238 vectType :: Type -> VM Type
239 vectType ty | Just ty' <- coreView ty = vectType ty
240 vectType (TyVarTy tv) = return $ TyVarTy tv
241 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
242 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
243 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
244 (mapM vectType [ty1,ty2])
245 vectType (ForAllTy tv ty)
247 r <- paDictArgType tv
249 return $ ForAllTy tv (wrap r ty')
252 wrap (Just pa_ty) = FunTy pa_ty
254 vectType ty = pprPanic "vectType:" (ppr ty)