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 vectBndrNew :: Var -> FastString -> VM VVar
139 vty <- vectType (idType v)
140 vv <- newLocalVVar fs vty
144 upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
146 vectBndrIn :: Var -> VM a -> VM (VVar, a)
154 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
158 vv <- vectBndrNew v fs
162 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
170 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
174 vvs <- mapM vectBndr vs
178 -- ----------------------------------------------------------------------------
181 vectVar :: Var -> VM VExpr
186 Local (vv,lv) -> return (Var vv, Var lv)
189 lexpr <- liftPA vexpr
190 return (vexpr, lexpr)
192 vectPolyVar :: Var -> [Type] -> VM VExpr
195 vtys <- mapM vectType tys
198 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
199 (polyApply (Var lv) vtys)
201 vexpr <- polyApply (Var poly) vtys
202 lexpr <- liftPA vexpr
203 return (vexpr, lexpr)
205 vectLiteral :: Literal -> VM VExpr
208 lexpr <- liftPA (Lit lit)
209 return (Lit lit, lexpr)
211 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
213 = polyAbstract tvs $ \abstract ->
215 mono' <- vectExpr mono
216 return $ mapVect abstract mono'
218 (tvs, mono) = collectAnnTypeBinders expr
220 vectExpr :: CoreExprWithFVs -> VM VExpr
221 vectExpr (_, AnnType ty)
222 = liftM vType (vectType ty)
224 vectExpr (_, AnnVar v) = vectVar v
226 vectExpr (_, AnnLit lit) = vectLiteral lit
228 vectExpr (_, AnnNote note expr)
229 = liftM (vNote note) (vectExpr expr)
231 vectExpr e@(_, AnnApp _ arg)
233 = vectTyAppExpr fn tys
235 (fn, tys) = collectAnnTypeArgs e
237 vectExpr (_, AnnApp fn arg)
239 arg_ty' <- vectType arg_ty
240 res_ty' <- vectType res_ty
243 mkClosureApp arg_ty' res_ty' fn' arg'
245 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
247 vectExpr (_, AnnCase scrut bndr ty alts)
249 = vectAlgCase scrut bndr ty alts
251 scrut_ty = exprType (deAnnotate scrut)
253 vectExpr (_, AnnCase expr bndr ty alts)
254 = panic "vectExpr: case"
256 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
258 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
259 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
260 return $ vLet (vNonRec vbndr vrhs) vbody
262 vectExpr (_, AnnLet (AnnRec bs) body)
264 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
266 (zipWithM vect_rhs bndrs rhss)
268 return $ vLet (vRec vbndrs vrhss) vbody
270 (bndrs, rhss) = unzip bs
272 vect_rhs bndr rhs = localV
276 vectExpr e@(fvs, AnnLam bndr _)
277 | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
278 | otherwise = vectLam fvs bs body
280 (bs,body) = collectAnnValBinders e
282 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
285 tyvars <- localTyVars
286 (vs, vvs) <- readLEnv $ \env ->
287 unzip [(var, vv) | var <- varSetElems fvs
288 , Just vv <- [lookupVarEnv (local_vars env) var]]
290 arg_tys <- mapM (vectType . idType) bs
291 res_ty <- vectType (exprType $ deAnnotate body)
293 buildClosures tyvars vvs arg_tys res_ty
294 . hoistPolyVExpr tyvars
296 lc <- builtin liftingContext
297 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
299 return $ vLams lc vbndrs vbody
301 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
302 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
303 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
305 type CoreAltWithFVs = AnnAlt Id VarSet
309 -- case e :: t of v { ... }
313 -- V: let v' = e in case v' of _ { ... }
314 -- L: let v' = e in case v' `cast` ... of _ { ... }
316 -- When lifting, we have to do it this way because v must have the type
317 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
318 -- have to handle the case where v is a wild var correctly.
321 -- FIXME: this is too lazy
322 vectAlgCase scrut bndr ty [(DEFAULT, [], body)]
324 vscrut <- vectExpr scrut
326 lty <- mkPArrayType vty
327 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
328 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
330 vectAlgCase scrut bndr ty [(DataAlt dc, bndrs, body)]
333 lty <- mkPArrayType vty
334 vexpr <- vectExpr scrut
335 (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
339 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
340 vect_dc <- maybeV (lookupDataCon dc)
341 let [arr_dc] = tyConDataCons arr_tc
342 let shape_tys = take (dataConRepArity arr_dc - length bndrs)
343 (dataConRepArgTys arr_dc)
344 shape_bndrs <- mapM (newLocalVar FSLIT("s")) shape_tys
345 return . vLet (vNonRec vbndr vexpr)
346 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
348 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr FSLIT("scrut")
349 | otherwise = vectBndrIn bndr