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 )
54 import Data.List ( sortBy, unzip4 )
56 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
57 -> IO (SimplCount, ModGuts)
58 vectorise hsc_env _ _ guts
60 showPass dflags "Vectorisation"
62 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
63 Just (info', guts') <- initV hsc_env guts info (vectModule guts)
64 endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
65 return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
67 dflags = hsc_dflags hsc_env
69 vectModule :: ModGuts -> VM ModGuts
72 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
74 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
75 updGEnv (setFamInstEnv fam_inst_env')
77 -- dicts <- mapM buildPADict pa_insts
78 -- workers <- mapM vectDataConWorkers pa_insts
79 binds' <- mapM vectTopBind (mg_binds guts)
80 return $ guts { mg_types = types'
81 , mg_binds = Rec tc_binds : binds'
82 , mg_fam_inst_env = fam_inst_env'
83 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
86 vectTopBind :: CoreBind -> VM CoreBind
87 vectTopBind b@(NonRec var expr)
89 var' <- vectTopBinder var
90 expr' <- vectTopRhs var expr
92 return . Rec $ (var, expr) : (var', expr') : hs
96 vectTopBind b@(Rec bs)
98 vars' <- mapM vectTopBinder vars
99 exprs' <- zipWithM vectTopRhs vars exprs
101 return . Rec $ bs ++ zip vars' exprs' ++ hs
105 (vars, exprs) = unzip bs
107 vectTopBinder :: Var -> VM Var
110 vty <- vectType (idType var)
111 var' <- cloneId mkVectOcc var vty
112 defGlobalVar var var'
115 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
118 closedV . liftM vectorised
120 $ vectPolyExpr (freeVars expr)
122 -- ----------------------------------------------------------------------------
125 vectBndr :: Var -> VM VVar
128 vty <- vectType (idType v)
129 lty <- mkPArrayType vty
130 let vv = v `Id.setIdType` vty
131 lv = v `Id.setIdType` lty
132 updLEnv (mapTo vv lv)
135 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
137 vectBndrNew :: Var -> FastString -> VM VVar
140 vty <- vectType (idType v)
141 vv <- newLocalVVar fs vty
145 upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
147 vectBndrIn :: Var -> VM a -> VM (VVar, a)
155 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
159 vv <- vectBndrNew v fs
163 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
171 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
175 vvs <- mapM vectBndr vs
179 -- ----------------------------------------------------------------------------
182 vectVar :: Var -> VM VExpr
187 Local (vv,lv) -> return (Var vv, Var lv)
190 lexpr <- liftPA vexpr
191 return (vexpr, lexpr)
193 vectPolyVar :: Var -> [Type] -> VM VExpr
196 vtys <- mapM vectType tys
199 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
200 (polyApply (Var lv) vtys)
202 vexpr <- polyApply (Var poly) vtys
203 lexpr <- liftPA vexpr
204 return (vexpr, lexpr)
206 vectLiteral :: Literal -> VM VExpr
209 lexpr <- liftPA (Lit lit)
210 return (Lit lit, lexpr)
212 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
214 = polyAbstract tvs $ \abstract ->
216 mono' <- vectExpr mono
217 return $ mapVect abstract mono'
219 (tvs, mono) = collectAnnTypeBinders expr
221 vectExpr :: CoreExprWithFVs -> VM VExpr
222 vectExpr (_, AnnType ty)
223 = liftM vType (vectType ty)
225 vectExpr (_, AnnVar v) = vectVar v
227 vectExpr (_, AnnLit lit) = vectLiteral lit
229 vectExpr (_, AnnNote note expr)
230 = liftM (vNote note) (vectExpr expr)
232 vectExpr e@(_, AnnApp _ arg)
234 = vectTyAppExpr fn tys
236 (fn, tys) = collectAnnTypeArgs e
238 vectExpr (_, AnnApp fn arg)
240 arg_ty' <- vectType arg_ty
241 res_ty' <- vectType res_ty
244 mkClosureApp arg_ty' res_ty' fn' arg'
246 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
248 vectExpr (_, AnnCase scrut bndr ty alts)
249 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
251 = vectAlgCase tycon ty_args scrut bndr ty alts
253 scrut_ty = exprType (deAnnotate scrut)
255 vectExpr (_, AnnCase expr bndr ty alts)
256 = panic "vectExpr: case"
258 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
260 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
261 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
262 return $ vLet (vNonRec vbndr vrhs) vbody
264 vectExpr (_, AnnLet (AnnRec bs) body)
266 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
268 (zipWithM vect_rhs bndrs rhss)
270 return $ vLet (vRec vbndrs vrhss) vbody
272 (bndrs, rhss) = unzip bs
274 vect_rhs bndr rhs = localV
278 vectExpr e@(fvs, AnnLam bndr _)
279 | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
280 | otherwise = vectLam fvs bs body
282 (bs,body) = collectAnnValBinders e
284 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
287 tyvars <- localTyVars
288 (vs, vvs) <- readLEnv $ \env ->
289 unzip [(var, vv) | var <- varSetElems fvs
290 , Just vv <- [lookupVarEnv (local_vars env) var]]
292 arg_tys <- mapM (vectType . idType) bs
293 res_ty <- vectType (exprType $ deAnnotate body)
295 buildClosures tyvars vvs arg_tys res_ty
296 . hoistPolyVExpr tyvars
298 lc <- builtin liftingContext
299 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
301 return $ vLams lc vbndrs vbody
303 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
304 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
305 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
307 type CoreAltWithFVs = AnnAlt Id VarSet
311 -- case e :: t of v { ... }
315 -- V: let v' = e in case v' of _ { ... }
316 -- L: let v' = e in case v' `cast` ... of _ { ... }
318 -- When lifting, we have to do it this way because v must have the type
319 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
320 -- have to handle the case where v is a wild var correctly.
323 -- FIXME: this is too lazy
324 vectAlgCase tycon ty_args scrut bndr ty [(DEFAULT, [], body)]
326 vscrut <- vectExpr scrut
328 lty <- mkPArrayType vty
329 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
330 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
332 vectAlgCase tycon ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
334 vect_tc <- maybeV (lookupTyCon tycon)
336 lty <- mkPArrayType vty
337 vexpr <- vectExpr scrut
338 (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
342 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
343 vect_dc <- maybeV (lookupDataCon dc)
344 let [arr_dc] = tyConDataCons arr_tc
345 repr <- mkRepr vect_tc
346 shape_bndrs <- arrShapeVars repr
347 return . vLet (vNonRec vbndr vexpr)
348 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
350 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr FSLIT("scrut")
351 | otherwise = vectBndrIn bndr
353 vectAlgCase tycon ty_args scrut bndr ty alts
355 vect_tc <- maybeV (lookupTyCon tycon)
357 lty <- mkPArrayType vty
359 repr <- mkRepr vect_tc
360 shape_bndrs <- arrShapeVars repr
361 (len, sel, indices) <- arrSelector repr (map Var shape_bndrs)
363 (vbndr, valts) <- vect_scrut_bndr $ mapM (proc_alt sel lty) alts'
364 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
366 vexpr <- vectExpr scrut
367 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
368 let [arr_dc] = tyConDataCons arr_tc
370 let (vect_scrut, lift_scrut) = vscrut
371 (vect_bodies, lift_bodies) = unzip vbodies
373 let vect_case = Case vect_scrut (mkWildId (exprType vect_scrut)) vty
374 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
376 lbody <- combinePA vty len sel indices lift_bodies
377 let lift_case = Case lift_scrut (mkWildId (exprType lift_scrut)) lty
378 [(DataAlt arr_dc, shape_bndrs ++ concat lift_bndrss,
381 return . vLet (vNonRec vbndr vexpr)
382 $ (vect_case, lift_case)
384 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr FSLIT("scrut")
385 | otherwise = vectBndrIn bndr
387 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
389 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
390 cmp DEFAULT DEFAULT = EQ
394 proc_alt sel lty (DataAlt dc, bndrs, body)
396 vect_dc <- maybeV (lookupDataCon dc)
397 let tag = mkDataConTag vect_dc
398 fvs = freeVarsOf body `delVarSetList` bndrs
399 (vect_bndrs, lift_bndrs, vbody)
400 <- vect_alt_bndrs bndrs
401 $ \len -> packLiftingContext len sel tag fvs lty
404 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
408 void_tc <- builtin voidTyCon
409 let void_ty = mkTyConApp void_tc []
410 arr_ty <- mkPArrayType void_ty
411 bndr <- newLocalVar FSLIT("voids") arr_ty
412 len <- lengthPA void_ty (Var bndr)
414 return ([], [bndr], e)
416 vect_alt_bndrs bndrs p
419 vbndrs <- mapM vectBndr bndrs
420 let (vect_bndrs, lift_bndrs) = unzip vbndrs
423 len <- lengthPA (idType vv) (Var lv)
425 return (vect_bndrs, lift_bndrs, e)
427 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
429 packLiftingContext :: CoreExpr -> CoreExpr -> CoreExpr -> VarSet -> Type -> VM VExpr -> VM VExpr
430 packLiftingContext len shape tag fvs res_ty p
432 select <- builtin selectPAIntPrimVar
433 let sel_expr = mkApps (Var select) [shape, tag]
434 sel_var <- newLocalVar FSLIT("sel#") (exprType sel_expr)
435 lc_var <- builtin liftingContext
438 bnds <- mapM (packFreeVar (Var lc_var) (Var sel_var))
442 return (vexpr, Let (NonRec sel_var sel_expr)
443 $ Case len lc_var res_ty [(DEFAULT, [], lexpr)])
445 packFreeVar :: CoreExpr -> CoreExpr -> Var -> VM [CoreBind]
446 packFreeVar len sel v
453 expr <- packPA (idType vv) (Var lv) len sel
455 return [(NonRec lv' expr)]
459 upd vv lv' env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv') }