2 module Vectorise( vectorise )
11 import HscTypes hiding ( MonadThings(..) )
13 import Module ( dphSeqPackageId, dphParPackageId )
14 import CoreLint ( showPass, endPass )
18 import SimplMonad ( SimplCount, zeroSimplCount )
19 import Rules ( RuleBase )
23 import FamInstEnv ( extendFamInstEnvList )
32 import Literal ( Literal, mkMachInt )
37 import Control.Monad ( liftM, liftM2, zipWithM )
38 import Data.List ( sortBy, unzip4 )
40 vectorise :: DPHBackend -> HscEnv -> UniqSupply -> RuleBase -> ModGuts
41 -> IO (SimplCount, ModGuts)
42 vectorise backend hsc_env _ _ guts
44 showPass dflags "Vectorisation"
46 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
47 Just (info', guts') <- initV (backendPackage backend) hsc_env guts info
49 endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
50 return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
52 dflags = hsc_dflags hsc_env
54 backendPackage DPHSeq = dphSeqPackageId
55 backendPackage DPHPar = dphParPackageId
56 backendPackage DPHThis = thisPackage dflags
58 vectModule :: ModGuts -> VM ModGuts
61 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
63 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
64 updGEnv (setFamInstEnv fam_inst_env')
66 -- dicts <- mapM buildPADict pa_insts
67 -- workers <- mapM vectDataConWorkers pa_insts
68 binds' <- mapM vectTopBind (mg_binds guts)
69 return $ guts { mg_types = types'
70 , mg_binds = Rec tc_binds : binds'
71 , mg_fam_inst_env = fam_inst_env'
72 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
75 vectTopBind :: CoreBind -> VM CoreBind
76 vectTopBind b@(NonRec var expr)
78 var' <- vectTopBinder var
79 expr' <- vectTopRhs var expr
81 cexpr <- tryConvert var var' expr
82 return . Rec $ (var, cexpr) : (var', expr') : hs
86 vectTopBind b@(Rec bs)
88 vars' <- mapM vectTopBinder vars
89 exprs' <- zipWithM vectTopRhs vars exprs
91 cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
92 return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
96 (vars, exprs) = unzip bs
98 vectTopBinder :: Var -> VM Var
101 vty <- vectType (idType var)
102 var' <- cloneId mkVectOcc var vty
103 defGlobalVar var var'
106 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
109 closedV . liftM vectorised
111 $ vectPolyExpr (freeVars expr)
113 tryConvert :: Var -> Var -> CoreExpr -> VM CoreExpr
114 tryConvert var vect_var rhs
115 = fromVect (idType var) (Var vect_var) `orElseV` return rhs
117 -- ----------------------------------------------------------------------------
120 vectBndr :: Var -> VM VVar
123 (vty, lty) <- vectAndLiftType (idType v)
124 let vv = v `Id.setIdType` vty
125 lv = v `Id.setIdType` lty
126 updLEnv (mapTo vv lv)
129 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
131 vectBndrNew :: Var -> FastString -> VM VVar
134 vty <- vectType (idType v)
135 vv <- newLocalVVar fs vty
139 upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
141 vectBndrIn :: Var -> VM a -> VM (VVar, a)
149 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
153 vv <- vectBndrNew v fs
157 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
161 vvs <- mapM vectBndr vs
165 -- ----------------------------------------------------------------------------
168 vectVar :: Var -> VM VExpr
173 Local (vv,lv) -> return (Var vv, Var lv)
176 lexpr <- liftPA vexpr
177 return (vexpr, lexpr)
179 vectPolyVar :: Var -> [Type] -> VM VExpr
182 vtys <- mapM vectType tys
185 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
186 (polyApply (Var lv) vtys)
188 vexpr <- polyApply (Var poly) vtys
189 lexpr <- liftPA vexpr
190 return (vexpr, lexpr)
192 vectLiteral :: Literal -> VM VExpr
195 lexpr <- liftPA (Lit lit)
196 return (Lit lit, lexpr)
198 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
199 vectPolyExpr (_, AnnNote note expr)
200 = liftM (vNote note) $ vectPolyExpr expr
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 (_, AnnVar v) (_, AnnLit lit))
227 | Just con <- isDataConId_maybe v
230 let vexpr = App (Var v) (Lit lit)
231 lexpr <- liftPA vexpr
232 return (vexpr, lexpr)
234 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
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)
248 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
250 = vectAlgCase tycon ty_args scrut bndr ty alts
252 scrut_ty = exprType (deAnnotate scrut)
254 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
256 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
257 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
258 return $ vLet (vNonRec vbndr vrhs) vbody
260 vectExpr (_, AnnLet (AnnRec bs) body)
262 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
264 (zipWithM vect_rhs bndrs rhss)
266 return $ vLet (vRec vbndrs vrhss) vbody
268 (bndrs, rhss) = unzip bs
270 vect_rhs bndr rhs = localV
274 vectExpr e@(fvs, AnnLam bndr _)
275 | isId bndr = vectLam fvs bs body
277 (bs,body) = collectAnnValBinders e
279 vectExpr e = cantVectorise "Can't vectorise expression" (ppr $ deAnnotate e)
281 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
284 tyvars <- localTyVars
285 (vs, vvs) <- readLEnv $ \env ->
286 unzip [(var, vv) | var <- varSetElems fvs
287 , Just vv <- [lookupVarEnv (local_vars env) var]]
289 arg_tys <- mapM (vectType . idType) bs
290 res_ty <- vectType (exprType $ deAnnotate body)
292 buildClosures tyvars vvs arg_tys res_ty
293 . hoistPolyVExpr tyvars
295 lc <- builtin liftingContext
296 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
298 return $ vLams lc vbndrs vbody
300 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
301 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
302 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
303 (ppr $ deAnnotate e `mkTyApps` tys)
307 -- case e :: t of v { ... }
311 -- V: let v' = e in case v' of _ { ... }
312 -- L: let v' = e in case v' `cast` ... of _ { ... }
314 -- When lifting, we have to do it this way because v must have the type
315 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
316 -- have to handle the case where v is a wild var correctly.
319 -- FIXME: this is too lazy
320 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
321 -> [(AltCon, [Var], CoreExprWithFVs)]
323 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
325 vscrut <- vectExpr scrut
326 (vty, lty) <- vectAndLiftType ty
327 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
328 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
330 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
332 vscrut <- vectExpr scrut
333 (vty, lty) <- vectAndLiftType ty
334 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
335 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
337 vectAlgCase tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
339 vect_tc <- maybeV (lookupTyCon tycon)
340 (vty, lty) <- vectAndLiftType ty
341 vexpr <- vectExpr scrut
342 (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
346 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
347 vect_dc <- maybeV (lookupDataCon dc)
348 let [arr_dc] = tyConDataCons arr_tc
349 repr <- mkRepr vect_tc
350 shape_bndrs <- arrShapeVars repr
351 return . vLet (vNonRec vbndr vexpr)
352 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
354 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
355 | otherwise = vectBndrIn bndr
357 vectAlgCase tycon _ty_args scrut bndr ty alts
359 vect_tc <- maybeV (lookupTyCon tycon)
360 (vty, lty) <- vectAndLiftType ty
361 repr <- mkRepr vect_tc
362 shape_bndrs <- arrShapeVars repr
363 (len, sel, indices) <- arrSelector repr (map Var shape_bndrs)
365 (vbndr, valts) <- vect_scrut_bndr $ mapM (proc_alt sel vty lty) alts'
366 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
368 vexpr <- vectExpr scrut
369 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
370 let [arr_dc] = tyConDataCons arr_tc
372 let (vect_scrut, lift_scrut) = vscrut
373 (vect_bodies, lift_bodies) = unzip vbodies
375 vdummy <- newDummyVar (exprType vect_scrut)
376 ldummy <- newDummyVar (exprType lift_scrut)
377 let vect_case = Case vect_scrut vdummy vty
378 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
380 lbody <- combinePA vty len sel indices lift_bodies
381 let lift_case = Case lift_scrut ldummy lty
382 [(DataAlt arr_dc, shape_bndrs ++ concat lift_bndrss,
385 return . vLet (vNonRec vbndr vexpr)
386 $ (vect_case, lift_case)
388 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
389 | otherwise = vectBndrIn bndr
391 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
393 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
394 cmp DEFAULT DEFAULT = EQ
397 cmp _ _ = panic "vectAlgCase/cmp"
399 proc_alt sel vty lty (DataAlt dc, bndrs, body)
401 vect_dc <- maybeV (lookupDataCon dc)
402 let tag = mkDataConTag vect_dc
403 fvs = freeVarsOf body `delVarSetList` bndrs
404 (vect_bndrs, lift_bndrs, vbody)
405 <- vect_alt_bndrs bndrs
406 $ \len -> packLiftingContext len sel tag fvs vty lty
409 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
410 proc_alt _ _ _ _ = panic "vectAlgCase/proc_alt"
414 void_tc <- builtin voidTyCon
415 let void_ty = mkTyConApp void_tc []
416 arr_ty <- mkPArrayType void_ty
417 bndr <- newLocalVar (fsLit "voids") arr_ty
418 len <- lengthPA void_ty (Var bndr)
420 return ([], [bndr], e)
422 vect_alt_bndrs bndrs p
425 vbndrs <- mapM vectBndr bndrs
426 let (vect_bndrs, lift_bndrs) = unzip vbndrs
429 len <- lengthPA (idType vv) (Var lv)
431 return (vect_bndrs, lift_bndrs, e)
433 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
435 packLiftingContext :: CoreExpr -> CoreExpr -> CoreExpr -> VarSet
436 -> Type -> Type -> VM VExpr -> VM VExpr
437 packLiftingContext len shape tag fvs vty lty p
439 select <- builtin selectPAIntPrimVar
440 let sel_expr = mkApps (Var select) [shape, tag]
441 sel_var <- newLocalVar (fsLit "sel#") (exprType sel_expr)
442 lc_var <- builtin liftingContext
445 bnds <- mapM (packFreeVar (Var lc_var) (Var sel_var))
450 return (vexpr, Let (NonRec sel_var sel_expr)
451 $ Case len lc_var lty
452 [(DEFAULT, [], mkLets (concat bnds) lexpr),
453 (LitAlt (mkMachInt 0), [], empty)])
455 packFreeVar :: CoreExpr -> CoreExpr -> Var -> VM [CoreBind]
456 packFreeVar len sel v
463 expr <- packPA (idType vv) (Var lv) len sel
465 return [(NonRec lv' expr)]
469 upd vv lv' env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv') }