1 module Vectorise( vectorise )
4 #include "HsVersions.h"
14 import CoreLint ( showPass, endPass )
18 import SimplMonad ( SimplCount, zeroSimplCount )
19 import Rules ( RuleBase )
23 import FamInstEnv ( extendFamInstEnvList )
24 import InstEnv ( extendInstEnvList )
28 import Name ( Name, mkSysTvName, getName )
31 import MkId ( unwrapFamInstScrut )
33 import Module ( Module )
35 import DsMonad hiding (mapAndUnzipM)
36 import DsUtils ( mkCoreTup, mkCoreTupTy )
38 import Literal ( Literal )
41 import TysPrim ( intPrimTy )
42 import BasicTypes ( Boxity(..) )
46 import Control.Monad ( liftM, liftM2, zipWithM, mapAndUnzipM )
48 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
49 -> IO (SimplCount, ModGuts)
50 vectorise hsc_env _ _ guts
52 showPass dflags "Vectorisation"
54 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
55 Just (info', guts') <- initV hsc_env guts info (vectModule guts)
56 endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
57 return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
59 dflags = hsc_dflags hsc_env
61 vectModule :: ModGuts -> VM ModGuts
64 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
66 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
67 updGEnv (setFamInstEnv fam_inst_env')
69 -- dicts <- mapM buildPADict pa_insts
70 -- workers <- mapM vectDataConWorkers pa_insts
71 binds' <- mapM vectTopBind (mg_binds guts)
72 return $ guts { mg_types = types'
73 , mg_binds = Rec tc_binds : binds'
74 , mg_fam_inst_env = fam_inst_env'
75 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
78 vectTopBind :: CoreBind -> VM CoreBind
79 vectTopBind b@(NonRec var expr)
81 var' <- vectTopBinder var
82 expr' <- vectTopRhs var expr
84 return . Rec $ (var, expr) : (var', expr') : hs
88 vectTopBind b@(Rec bs)
90 vars' <- mapM vectTopBinder vars
91 exprs' <- zipWithM vectTopRhs vars exprs
93 return . Rec $ bs ++ zip vars' exprs' ++ hs
97 (vars, exprs) = unzip bs
99 vectTopBinder :: Var -> VM Var
102 vty <- vectType (idType var)
103 var' <- cloneId mkVectOcc var vty
104 defGlobalVar var var'
107 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
110 closedV . liftM vectorised
112 $ vectPolyExpr (freeVars expr)
114 -- ----------------------------------------------------------------------------
117 vectBndr :: Var -> VM VVar
120 vty <- vectType (idType v)
121 lty <- mkPArrayType vty
122 let vv = v `Id.setIdType` vty
123 lv = v `Id.setIdType` lty
124 updLEnv (mapTo vv lv)
127 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
129 vectBndrIn :: Var -> VM a -> VM (VVar, a)
137 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
145 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
149 vvs <- mapM vectBndr vs
153 -- ----------------------------------------------------------------------------
156 vectVar :: Var -> VM VExpr
161 Local (vv,lv) -> return (Var vv, Var lv)
164 lexpr <- liftPA vexpr
165 return (vexpr, lexpr)
167 vectPolyVar :: Var -> [Type] -> VM VExpr
170 vtys <- mapM vectType tys
173 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
174 (polyApply (Var lv) vtys)
176 vexpr <- polyApply (Var poly) vtys
177 lexpr <- liftPA vexpr
178 return (vexpr, lexpr)
180 vectLiteral :: Literal -> VM VExpr
183 lexpr <- liftPA (Lit lit)
184 return (Lit lit, lexpr)
186 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
188 = polyAbstract tvs $ \abstract ->
190 mono' <- vectExpr mono
191 return $ mapVect abstract mono'
193 (tvs, mono) = collectAnnTypeBinders expr
195 vectExpr :: CoreExprWithFVs -> VM VExpr
196 vectExpr (_, AnnType ty)
197 = liftM vType (vectType ty)
199 vectExpr (_, AnnVar v) = vectVar v
201 vectExpr (_, AnnLit lit) = vectLiteral lit
203 vectExpr (_, AnnNote note expr)
204 = liftM (vNote note) (vectExpr expr)
206 vectExpr e@(_, AnnApp _ arg)
208 = vectTyAppExpr fn tys
210 (fn, tys) = collectAnnTypeArgs e
212 vectExpr (_, AnnApp fn arg)
214 arg_ty' <- vectType arg_ty
215 res_ty' <- vectType res_ty
218 mkClosureApp arg_ty' res_ty' fn' arg'
220 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
222 vectExpr (_, AnnCase scrut bndr ty alts)
224 = vectAlgCase scrut bndr ty alts
226 scrut_ty = exprType (deAnnotate scrut)
228 vectExpr (_, AnnCase expr bndr ty alts)
229 = panic "vectExpr: case"
231 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
233 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
234 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
235 return $ vLet (vNonRec vbndr vrhs) vbody
237 vectExpr (_, AnnLet (AnnRec bs) body)
239 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
241 (zipWithM vect_rhs bndrs rhss)
243 return $ vLet (vRec vbndrs vrhss) vbody
245 (bndrs, rhss) = unzip bs
247 vect_rhs bndr rhs = localV
251 vectExpr e@(fvs, AnnLam bndr _)
252 | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
253 | otherwise = vectLam fvs bs body
255 (bs,body) = collectAnnValBinders e
257 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
260 tyvars <- localTyVars
261 (vs, vvs) <- readLEnv $ \env ->
262 unzip [(var, vv) | var <- varSetElems fvs
263 , Just vv <- [lookupVarEnv (local_vars env) var]]
265 arg_tys <- mapM (vectType . idType) bs
266 res_ty <- vectType (exprType $ deAnnotate body)
268 buildClosures tyvars vvs arg_tys res_ty
269 . hoistPolyVExpr tyvars
271 lc <- builtin liftingContext
272 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
274 return $ vLams lc vbndrs vbody
276 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
277 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
278 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
280 type CoreAltWithFVs = AnnAlt Id VarSet
284 -- case e :: t of v { ... }
288 -- V: let v = e in case v of _ { ... }
289 -- L: let v = e in case v `cast` ... of _ { ... }
291 -- When lifting, we have to do it this way because v must have the type
292 -- [:V(T):] but the scrutinee must be cast to the representation type.
295 -- FIXME: this is too lazy
296 vectAlgCase scrut bndr ty [(DEFAULT, [], body)]
298 vscrut <- vectExpr scrut
300 lty <- mkPArrayType vty
301 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
302 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
304 vectAlgCase scrut bndr ty [(DataAlt dc, bndrs, body)]
307 lty <- mkPArrayType vty
308 vexpr <- vectExpr scrut
309 (vbndr, (vbndrs, vbody)) <- vectBndrIn bndr
313 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
314 vect_dc <- maybeV (lookupDataCon dc)
315 let [arr_dc] = tyConDataCons arr_tc
316 let shape_tys = take (dataConRepArity arr_dc - length bndrs)
317 (dataConRepArgTys arr_dc)
318 shape_bndrs <- mapM (newLocalVar FSLIT("s")) shape_tys
319 return . vLet (vNonRec vbndr vexpr)
320 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody