2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
8 module Vectorise( vectorise )
11 #include "HsVersions.h"
21 import CoreLint ( showPass, endPass )
25 import SimplMonad ( SimplCount, zeroSimplCount )
26 import Rules ( RuleBase )
30 import FamInstEnv ( extendFamInstEnvList )
31 import InstEnv ( extendInstEnvList )
35 import Name ( Name, mkSysTvName, getName )
38 import MkId ( unwrapFamInstScrut )
40 import Module ( Module )
42 import DsMonad hiding (mapAndUnzipM)
43 import DsUtils ( mkCoreTup, mkCoreTupTy )
45 import Literal ( Literal )
48 import TysPrim ( intPrimTy )
49 import BasicTypes ( Boxity(..) )
53 import Control.Monad ( liftM, liftM2, zipWithM, mapAndUnzipM )
55 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
56 -> IO (SimplCount, ModGuts)
57 vectorise hsc_env _ _ guts
59 showPass dflags "Vectorisation"
61 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
62 Just (info', guts') <- initV hsc_env guts info (vectModule guts)
63 endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
64 return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
66 dflags = hsc_dflags hsc_env
68 vectModule :: ModGuts -> VM ModGuts
71 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
73 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
74 updGEnv (setFamInstEnv fam_inst_env')
76 -- dicts <- mapM buildPADict pa_insts
77 -- workers <- mapM vectDataConWorkers pa_insts
78 binds' <- mapM vectTopBind (mg_binds guts)
79 return $ guts { mg_types = types'
80 , mg_binds = Rec tc_binds : binds'
81 , mg_fam_inst_env = fam_inst_env'
82 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
85 vectTopBind :: CoreBind -> VM CoreBind
86 vectTopBind b@(NonRec var expr)
88 var' <- vectTopBinder var
89 expr' <- vectTopRhs var expr
91 return . Rec $ (var, expr) : (var', expr') : hs
95 vectTopBind b@(Rec bs)
97 vars' <- mapM vectTopBinder vars
98 exprs' <- zipWithM vectTopRhs vars exprs
100 return . Rec $ bs ++ zip vars' exprs' ++ hs
104 (vars, exprs) = unzip bs
106 vectTopBinder :: Var -> VM Var
109 vty <- vectType (idType var)
110 var' <- cloneId mkVectOcc var vty
111 defGlobalVar var var'
114 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
117 closedV . liftM vectorised
119 $ vectPolyExpr (freeVars expr)
121 -- ----------------------------------------------------------------------------
124 vectBndr :: Var -> VM VVar
127 vty <- vectType (idType v)
128 lty <- mkPArrayType vty
129 let vv = v `Id.setIdType` vty
130 lv = v `Id.setIdType` lty
131 updLEnv (mapTo vv lv)
134 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
136 vectBndrIn :: Var -> VM a -> VM (VVar, a)
144 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
152 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
156 vvs <- mapM vectBndr vs
160 -- ----------------------------------------------------------------------------
163 vectVar :: Var -> VM VExpr
168 Local (vv,lv) -> return (Var vv, Var lv)
171 lexpr <- liftPA vexpr
172 return (vexpr, lexpr)
174 vectPolyVar :: Var -> [Type] -> VM VExpr
177 vtys <- mapM vectType tys
180 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
181 (polyApply (Var lv) vtys)
183 vexpr <- polyApply (Var poly) vtys
184 lexpr <- liftPA vexpr
185 return (vexpr, lexpr)
187 vectLiteral :: Literal -> VM VExpr
190 lexpr <- liftPA (Lit lit)
191 return (Lit lit, lexpr)
193 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
195 = polyAbstract tvs $ \abstract ->
197 mono' <- vectExpr mono
198 return $ mapVect abstract mono'
200 (tvs, mono) = collectAnnTypeBinders expr
202 vectExpr :: CoreExprWithFVs -> VM VExpr
203 vectExpr (_, AnnType ty)
204 = liftM vType (vectType ty)
206 vectExpr (_, AnnVar v) = vectVar v
208 vectExpr (_, AnnLit lit) = vectLiteral lit
210 vectExpr (_, AnnNote note expr)
211 = liftM (vNote note) (vectExpr expr)
213 vectExpr e@(_, AnnApp _ arg)
215 = vectTyAppExpr fn tys
217 (fn, tys) = collectAnnTypeArgs e
219 vectExpr (_, AnnApp fn arg)
221 arg_ty' <- vectType arg_ty
222 res_ty' <- vectType res_ty
225 mkClosureApp arg_ty' res_ty' fn' arg'
227 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
229 vectExpr (_, AnnCase scrut bndr ty alts)
231 = vectAlgCase scrut bndr ty alts
233 scrut_ty = exprType (deAnnotate scrut)
235 vectExpr (_, AnnCase expr bndr ty alts)
236 = panic "vectExpr: case"
238 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
240 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
241 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
242 return $ vLet (vNonRec vbndr vrhs) vbody
244 vectExpr (_, AnnLet (AnnRec bs) body)
246 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
248 (zipWithM vect_rhs bndrs rhss)
250 return $ vLet (vRec vbndrs vrhss) vbody
252 (bndrs, rhss) = unzip bs
254 vect_rhs bndr rhs = localV
258 vectExpr e@(fvs, AnnLam bndr _)
259 | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
260 | otherwise = vectLam fvs bs body
262 (bs,body) = collectAnnValBinders e
264 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
267 tyvars <- localTyVars
268 (vs, vvs) <- readLEnv $ \env ->
269 unzip [(var, vv) | var <- varSetElems fvs
270 , Just vv <- [lookupVarEnv (local_vars env) var]]
272 arg_tys <- mapM (vectType . idType) bs
273 res_ty <- vectType (exprType $ deAnnotate body)
275 buildClosures tyvars vvs arg_tys res_ty
276 . hoistPolyVExpr tyvars
278 lc <- builtin liftingContext
279 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
281 return $ vLams lc vbndrs vbody
283 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
284 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
285 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
287 type CoreAltWithFVs = AnnAlt Id VarSet
291 -- case e :: t of v { ... }
295 -- V: let v = e in case v of _ { ... }
296 -- L: let v = e in case v `cast` ... of _ { ... }
298 -- When lifting, we have to do it this way because v must have the type
299 -- [:V(T):] but the scrutinee must be cast to the representation type.
302 -- FIXME: this is too lazy
303 vectAlgCase scrut bndr ty [(DEFAULT, [], body)]
305 vscrut <- vectExpr scrut
307 lty <- mkPArrayType vty
308 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
309 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
311 vectAlgCase scrut bndr ty [(DataAlt dc, bndrs, body)]
314 lty <- mkPArrayType vty
315 vexpr <- vectExpr scrut
316 (vbndr, (vbndrs, vbody)) <- vectBndrIn bndr
320 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
321 vect_dc <- maybeV (lookupDataCon dc)
322 let [arr_dc] = tyConDataCons arr_tc
323 let shape_tys = take (dataConRepArity arr_dc - length bndrs)
324 (dataConRepArgTys arr_dc)
325 shape_bndrs <- mapM (newLocalVar FSLIT("s")) shape_tys
326 return . vLet (vNonRec vbndr vexpr)
327 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody