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 )
33 vectorise :: HscEnv -> ModGuts -> IO ModGuts
34 vectorise hsc_env guts
35 | not (Opt_Vectorise `dopt` dflags) = return guts
38 showPass dflags "Vectorisation"
40 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
41 Just (info', guts') <- initV hsc_env guts info (vectModule guts)
42 endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
43 return $ guts' { mg_vect_info = info' }
45 dflags = hsc_dflags hsc_env
47 vectModule :: ModGuts -> VM ModGuts
48 vectModule guts = return guts
50 -- ----------------------------------------------------------------------------
53 vectBndr :: Var -> VM (Var, Var)
56 vty <- vectType (idType v)
57 lty <- mkPArrayType vty
58 let vv = v `Id.setIdType` vty
59 lv = v `Id.setIdType` lty
63 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (Var vv, Var lv) }
65 vectBndrIn :: Var -> VM a -> VM (Var, Var, a)
69 (vv, lv) <- vectBndr v
73 vectBndrsIn :: [Var] -> VM a -> VM ([Var], [Var], a)
77 (vvs, lvs) <- mapAndUnzipM vectBndr vs
81 -- ----------------------------------------------------------------------------
84 replicateP :: CoreExpr -> CoreExpr -> VM CoreExpr
87 dict <- paDictOfType ty
88 rep <- builtin replicatePAVar
89 return $ mkApps (Var rep) [Type ty, dict, expr, len]
93 capply :: (CoreExpr, CoreExpr) -> (CoreExpr, CoreExpr) -> VM (CoreExpr, CoreExpr)
94 capply (vfn, lfn) (varg, larg)
96 apply <- builtin applyClosureVar
97 applyP <- builtin applyClosurePVar
98 return (mkApps (Var apply) [Type arg_ty, Type res_ty, vfn, varg],
99 mkApps (Var applyP) [Type arg_ty, Type res_ty, lfn, larg])
102 (arg_ty, res_ty) = splitClosureTy fn_ty
104 vectVar :: CoreExpr -> Var -> VM (CoreExpr, CoreExpr)
105 vectVar lc v = local v `orElseV` global v
107 local v = maybeV (readLEnv $ \env -> lookupVarEnv (local_vars env) v)
109 vexpr <- maybeV (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
110 lexpr <- replicateP vexpr lc
111 return (vexpr, lexpr)
113 vectExpr :: CoreExpr -> CoreExprWithFVs -> VM (CoreExpr, CoreExpr)
114 vectExpr lc (_, AnnType ty)
117 return (Type vty, Type vty)
118 vectExpr lc (_, AnnVar v) = vectVar lc v
119 vectExpr lc (_, AnnLit lit)
122 lexpr <- replicateP vexpr lc
123 return (vexpr, lexpr)
124 vectExpr lc (_, AnnNote note expr)
126 (vexpr, lexpr) <- vectExpr lc expr
127 return (Note note vexpr, Note note lexpr)
128 vectExpr lc (_, AnnApp fn arg)
130 fn' <- vectExpr lc fn
131 arg' <- vectExpr lc arg
133 vectExpr lc (_, AnnCase expr bndr ty alts)
134 = panic "vectExpr: case"
135 vectExpr lc (_, AnnLet (AnnNonRec bndr rhs) body)
137 (vrhs, lrhs) <- vectExpr lc rhs
138 (vbndr, lbndr, (vbody, lbody)) <- vectBndrIn bndr (vectExpr lc body)
139 return (Let (NonRec vbndr vrhs) vbody,
140 Let (NonRec lbndr lrhs) lbody)
141 vectExpr lc (_, AnnLet (AnnRec prs) body)
143 (vbndrs, lbndrs, (vrhss, vbody, lrhss, lbody)) <- vectBndrsIn bndrs vect
144 return (Let (Rec (zip vbndrs vrhss)) vbody,
145 Let (Rec (zip lbndrs lrhss)) lbody)
147 (bndrs, rhss) = unzip prs
150 (vrhss, lrhss) <- mapAndUnzipM (vectExpr lc) rhss
151 (vbody, lbody) <- vectExpr lc body
152 return (vrhss, vbody, lrhss, lbody)
153 vectExpr lc (_, AnnLam bndr body)
156 r <- paDictArgType bndr
157 (upd_env, add_lam) <- get_upd r
158 (vbody, lbody) <- localV (upd_env >> vectExpr lc body)
159 return (Lam bndr (add_lam vbody), Lam bndr (add_lam lbody))
161 get_upd Nothing = return (deleteTyVarPA bndr, id)
162 get_upd (Just pa_ty) = do
163 pa_var <- newLocalVar FSLIT("dPA") pa_ty
164 return (extendTyVarPA bndr (Var pa_var),
167 -- ----------------------------------------------------------------------------
170 vectTyCon :: TyCon -> VM TyCon
172 | isFunTyCon tc = builtin closureTyCon
173 | isBoxedTupleTyCon tc = return tc
174 | isUnLiftedTyCon tc = return tc
178 Just tc' -> return tc'
180 -- FIXME: just for now
181 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
183 vectType :: Type -> VM Type
184 vectType ty | Just ty' <- coreView ty = vectType ty
185 vectType (TyVarTy tv) = return $ TyVarTy tv
186 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
187 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
188 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
189 (mapM vectType [ty1,ty2])
190 vectType (ForAllTy tv ty)
192 r <- paDictArgType tv
194 return $ ForAllTy tv (wrap r ty')
197 wrap (Just pa_ty) = FunTy pa_ty
199 vectType ty = pprPanic "vectType:" (ppr ty)