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)
216 mkClosureApp fn' arg'
218 vectExpr (_, AnnCase scrut bndr ty alts)
220 = vectAlgCase scrut bndr ty alts
222 scrut_ty = exprType (deAnnotate scrut)
224 vectExpr (_, AnnCase expr bndr ty alts)
225 = panic "vectExpr: case"
227 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
229 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
230 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
231 return $ vLet (vNonRec vbndr vrhs) vbody
233 vectExpr (_, AnnLet (AnnRec bs) body)
235 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
237 (zipWithM vect_rhs bndrs rhss)
239 return $ vLet (vRec vbndrs vrhss) vbody
241 (bndrs, rhss) = unzip bs
243 vect_rhs bndr rhs = localV
247 vectExpr e@(fvs, AnnLam bndr _)
248 | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
249 | otherwise = vectLam fvs bs body
251 (bs,body) = collectAnnValBinders e
253 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
256 tyvars <- localTyVars
257 (vs, vvs) <- readLEnv $ \env ->
258 unzip [(var, vv) | var <- varSetElems fvs
259 , Just vv <- [lookupVarEnv (local_vars env) var]]
261 arg_tys <- mapM (vectType . idType) bs
262 res_ty <- vectType (exprType $ deAnnotate body)
264 buildClosures tyvars vvs arg_tys res_ty
265 . hoistPolyVExpr tyvars
267 lc <- builtin liftingContext
268 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
270 return $ vLams lc vbndrs vbody
272 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
273 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
274 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
276 type CoreAltWithFVs = AnnAlt Id VarSet
280 -- case e :: t of v { ... }
284 -- V: let v = e in case v of _ { ... }
285 -- L: let v = e in case v `cast` ... of _ { ... }
287 -- When lifting, we have to do it this way because v must have the type
288 -- [:V(T):] but the scrutinee must be cast to the representation type.
291 -- FIXME: this is too lazy
292 vectAlgCase scrut bndr ty [(DEFAULT, [], body)]
294 vscrut <- vectExpr scrut
296 lty <- mkPArrayType vty
297 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
298 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
300 vectAlgCase scrut bndr ty [(DataAlt dc, bndrs, body)]
303 lty <- mkPArrayType vty
304 vexpr <- vectExpr scrut
305 (vbndr, (vbndrs, vbody)) <- vectBndrIn bndr
309 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
310 vect_dc <- maybeV (lookupDataCon dc)
311 let [arr_dc] = tyConDataCons arr_tc
312 let shape_tys = take (dataConRepArity arr_dc - length bndrs)
313 (dataConRepArgTys arr_dc)
314 shape_bndrs <- mapM (newLocalVar FSLIT("s")) shape_tys
315 return . vLet (vNonRec vbndr vexpr)
316 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody