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 builtin_PAs :: [(Name, Module, FastString)]
50 mk closureTyConName FSLIT("dPA_Clo")
51 , mk intTyConName FSLIT("dPA_Int")
55 mk name fs = (name, nDP_INSTANCES, fs)
57 tups = mk_tup 0 : map mk_tup [2..3]
58 mk_tup n = (getName $ tupleTyCon Boxed n, nDP_INSTANCES,
59 mkFastString $ "dPA_" ++ show n)
61 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
62 -> IO (SimplCount, ModGuts)
63 vectorise hsc_env _ _ guts
65 showPass dflags "Vectorisation"
67 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
68 Just (info', guts') <- initV hsc_env guts info (vectModule guts)
69 endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
70 return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
72 dflags = hsc_dflags hsc_env
74 vectModule :: ModGuts -> VM ModGuts
77 defTyConBuiltinPAs builtin_PAs
78 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
80 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
81 updGEnv (setFamInstEnv fam_inst_env')
83 -- dicts <- mapM buildPADict pa_insts
84 -- workers <- mapM vectDataConWorkers pa_insts
85 binds' <- mapM vectTopBind (mg_binds guts)
86 return $ guts { mg_types = types'
87 , mg_binds = Rec tc_binds : binds'
88 , mg_fam_inst_env = fam_inst_env'
89 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
92 vectTopBind :: CoreBind -> VM CoreBind
93 vectTopBind b@(NonRec var expr)
95 var' <- vectTopBinder var
96 expr' <- vectTopRhs var expr
98 return . Rec $ (var, expr) : (var', expr') : hs
102 vectTopBind b@(Rec bs)
104 vars' <- mapM vectTopBinder vars
105 exprs' <- zipWithM vectTopRhs vars exprs
107 return . Rec $ bs ++ zip vars' exprs' ++ hs
111 (vars, exprs) = unzip bs
113 vectTopBinder :: Var -> VM Var
116 vty <- vectType (idType var)
117 var' <- cloneId mkVectOcc var vty
118 defGlobalVar var var'
121 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
124 closedV . liftM vectorised
126 $ vectPolyExpr (freeVars expr)
128 -- ----------------------------------------------------------------------------
131 vectBndr :: Var -> VM VVar
134 vty <- vectType (idType v)
135 lty <- mkPArrayType vty
136 let vv = v `Id.setIdType` vty
137 lv = v `Id.setIdType` lty
138 updLEnv (mapTo vv lv)
141 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
143 vectBndrIn :: Var -> VM a -> VM (VVar, a)
151 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
159 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
163 vvs <- mapM vectBndr vs
167 -- ----------------------------------------------------------------------------
170 vectVar :: Var -> VM VExpr
175 Local (vv,lv) -> return (Var vv, Var lv)
178 lexpr <- liftPA vexpr
179 return (vexpr, lexpr)
181 vectPolyVar :: Var -> [Type] -> VM VExpr
184 vtys <- mapM vectType tys
187 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
188 (polyApply (Var lv) vtys)
190 vexpr <- polyApply (Var poly) vtys
191 lexpr <- liftPA vexpr
192 return (vexpr, lexpr)
194 vectLiteral :: Literal -> VM VExpr
197 lexpr <- liftPA (Lit lit)
198 return (Lit lit, lexpr)
200 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
202 = polyAbstract tvs $ \abstract ->
204 mono' <- vectExpr mono
205 return $ mapVect abstract mono'
207 (tvs, mono) = collectAnnTypeBinders expr
209 vectExpr :: CoreExprWithFVs -> VM VExpr
210 vectExpr (_, AnnType ty)
211 = liftM vType (vectType ty)
213 vectExpr (_, AnnVar v) = vectVar v
215 vectExpr (_, AnnLit lit) = vectLiteral lit
217 vectExpr (_, AnnNote note expr)
218 = liftM (vNote note) (vectExpr expr)
220 vectExpr e@(_, AnnApp _ arg)
222 = vectTyAppExpr fn tys
224 (fn, tys) = collectAnnTypeArgs e
226 vectExpr (_, AnnApp fn arg)
230 mkClosureApp fn' arg'
232 vectExpr (_, AnnCase scrut bndr ty alts)
234 = vectAlgCase scrut bndr ty alts
236 scrut_ty = exprType (deAnnotate scrut)
238 vectExpr (_, AnnCase expr bndr ty alts)
239 = panic "vectExpr: case"
241 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
243 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
244 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
245 return $ vLet (vNonRec vbndr vrhs) vbody
247 vectExpr (_, AnnLet (AnnRec bs) body)
249 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
251 (zipWithM vect_rhs bndrs rhss)
253 return $ vLet (vRec vbndrs vrhss) vbody
255 (bndrs, rhss) = unzip bs
257 vect_rhs bndr rhs = localV
261 vectExpr e@(fvs, AnnLam bndr _)
262 | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
263 | otherwise = vectLam fvs bs body
265 (bs,body) = collectAnnValBinders e
267 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
270 tyvars <- localTyVars
271 (vs, vvs) <- readLEnv $ \env ->
272 unzip [(var, vv) | var <- varSetElems fvs
273 , Just vv <- [lookupVarEnv (local_vars env) var]]
275 arg_tys <- mapM (vectType . idType) bs
276 res_ty <- vectType (exprType $ deAnnotate body)
278 buildClosures tyvars vvs arg_tys res_ty
279 . hoistPolyVExpr tyvars
281 lc <- builtin liftingContext
282 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
284 return $ vLams lc vbndrs vbody
286 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
287 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
288 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
290 type CoreAltWithFVs = AnnAlt Id VarSet
294 -- case e :: t of v { ... }
298 -- V: let v = e in case v of _ { ... }
299 -- L: let v = e in case v `cast` ... of _ { ... }
301 -- When lifting, we have to do it this way because v must have the type
302 -- [:V(T):] but the scrutinee must be cast to the representation type.
305 -- FIXME: this is too lazy
306 vectAlgCase scrut bndr ty [(DEFAULT, [], body)]
308 vscrut <- vectExpr scrut
310 lty <- mkPArrayType vty
311 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
312 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
314 vectAlgCase scrut bndr ty [(DataAlt dc, bndrs, body)]
317 lty <- mkPArrayType vty
318 vexpr <- vectExpr scrut
319 (vbndr, (vbndrs, vbody)) <- vectBndrIn bndr
323 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
324 vect_dc <- maybeV (lookupDataCon dc)
325 let [arr_dc] = tyConDataCons arr_tc
326 let shape_tys = take (dataConRepArity arr_dc - length bndrs)
327 (dataConRepArgTys arr_dc)
328 shape_bndrs <- mapM (newLocalVar FSLIT("s")) shape_tys
329 return . vLet (vNonRec vbndr vexpr)
330 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody