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
57 vectModule :: ModGuts -> VM ModGuts
60 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
62 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
63 updGEnv (setFamInstEnv fam_inst_env')
65 -- dicts <- mapM buildPADict pa_insts
66 -- workers <- mapM vectDataConWorkers pa_insts
67 binds' <- mapM vectTopBind (mg_binds guts)
68 return $ guts { mg_types = types'
69 , mg_binds = Rec tc_binds : binds'
70 , mg_fam_inst_env = fam_inst_env'
71 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
74 vectTopBind :: CoreBind -> VM CoreBind
75 vectTopBind b@(NonRec var expr)
77 var' <- vectTopBinder var
78 expr' <- vectTopRhs var expr
80 cexpr <- tryConvert var var' expr
81 return . Rec $ (var, cexpr) : (var', expr') : hs
85 vectTopBind b@(Rec bs)
87 vars' <- mapM vectTopBinder vars
88 exprs' <- zipWithM vectTopRhs vars exprs
90 cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
91 return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
95 (vars, exprs) = unzip bs
97 vectTopBinder :: Var -> VM Var
100 vty <- vectType (idType var)
101 var' <- cloneId mkVectOcc var vty
102 defGlobalVar var var'
105 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
108 closedV . liftM vectorised
110 $ vectPolyExpr (freeVars expr)
112 tryConvert :: Var -> Var -> CoreExpr -> VM CoreExpr
113 tryConvert var vect_var rhs
114 = fromVect (idType var) (Var vect_var) `orElseV` return rhs
116 -- ----------------------------------------------------------------------------
119 vectBndr :: Var -> VM VVar
122 (vty, lty) <- vectAndLiftType (idType v)
123 let vv = v `Id.setIdType` vty
124 lv = v `Id.setIdType` lty
125 updLEnv (mapTo vv lv)
128 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
130 vectBndrNew :: Var -> FastString -> VM VVar
133 vty <- vectType (idType v)
134 vv <- newLocalVVar fs vty
138 upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
140 vectBndrIn :: Var -> VM a -> VM (VVar, a)
148 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
152 vv <- vectBndrNew v fs
156 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
160 vvs <- mapM vectBndr vs
164 -- ----------------------------------------------------------------------------
167 vectVar :: Var -> VM VExpr
172 Local (vv,lv) -> return (Var vv, Var lv)
175 lexpr <- liftPA vexpr
176 return (vexpr, lexpr)
178 vectPolyVar :: Var -> [Type] -> VM VExpr
181 vtys <- mapM vectType tys
184 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
185 (polyApply (Var lv) vtys)
187 vexpr <- polyApply (Var poly) vtys
188 lexpr <- liftPA vexpr
189 return (vexpr, lexpr)
191 vectLiteral :: Literal -> VM VExpr
194 lexpr <- liftPA (Lit lit)
195 return (Lit lit, lexpr)
197 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
198 vectPolyExpr (_, AnnNote note expr)
199 = liftM (vNote note) $ vectPolyExpr expr
201 = polyAbstract tvs $ \abstract ->
203 mono' <- vectExpr mono
204 return $ mapVect abstract mono'
206 (tvs, mono) = collectAnnTypeBinders expr
208 vectExpr :: CoreExprWithFVs -> VM VExpr
209 vectExpr (_, AnnType ty)
210 = liftM vType (vectType ty)
212 vectExpr (_, AnnVar v) = vectVar v
214 vectExpr (_, AnnLit lit) = vectLiteral lit
216 vectExpr (_, AnnNote note expr)
217 = liftM (vNote note) (vectExpr expr)
219 vectExpr e@(_, AnnApp _ arg)
221 = vectTyAppExpr fn tys
223 (fn, tys) = collectAnnTypeArgs e
225 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
226 | Just con <- isDataConId_maybe v
229 let vexpr = App (Var v) (Lit lit)
230 lexpr <- liftPA vexpr
231 return (vexpr, lexpr)
233 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
236 vectExpr (_, AnnApp fn arg)
238 arg_ty' <- vectType arg_ty
239 res_ty' <- vectType res_ty
242 mkClosureApp arg_ty' res_ty' fn' arg'
244 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
246 vectExpr (_, AnnCase scrut bndr ty alts)
247 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
249 = vectAlgCase tycon ty_args scrut bndr ty alts
251 scrut_ty = exprType (deAnnotate scrut)
253 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
255 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
256 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
257 return $ vLet (vNonRec vbndr vrhs) vbody
259 vectExpr (_, AnnLet (AnnRec bs) body)
261 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
263 (zipWithM vect_rhs bndrs rhss)
265 return $ vLet (vRec vbndrs vrhss) vbody
267 (bndrs, rhss) = unzip bs
269 vect_rhs bndr rhs = localV
273 vectExpr e@(fvs, AnnLam bndr _)
274 | isId bndr = vectLam fvs bs body
276 (bs,body) = collectAnnValBinders e
278 vectExpr e = cantVectorise "Can't vectorise expression" (ppr $ deAnnotate e)
280 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
283 tyvars <- localTyVars
284 (vs, vvs) <- readLEnv $ \env ->
285 unzip [(var, vv) | var <- varSetElems fvs
286 , Just vv <- [lookupVarEnv (local_vars env) var]]
288 arg_tys <- mapM (vectType . idType) bs
289 res_ty <- vectType (exprType $ deAnnotate body)
291 buildClosures tyvars vvs arg_tys res_ty
292 . hoistPolyVExpr tyvars
294 lc <- builtin liftingContext
295 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
297 return $ vLams lc vbndrs vbody
299 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
300 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
301 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
302 (ppr $ deAnnotate e `mkTyApps` tys)
306 -- case e :: t of v { ... }
310 -- V: let v' = e in case v' of _ { ... }
311 -- L: let v' = e in case v' `cast` ... of _ { ... }
313 -- When lifting, we have to do it this way because v must have the type
314 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
315 -- have to handle the case where v is a wild var correctly.
318 -- FIXME: this is too lazy
319 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
320 -> [(AltCon, [Var], CoreExprWithFVs)]
322 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
324 vscrut <- vectExpr scrut
325 (vty, lty) <- vectAndLiftType ty
326 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
327 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
329 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
331 vscrut <- vectExpr scrut
332 (vty, lty) <- vectAndLiftType ty
333 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
334 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
336 vectAlgCase tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
338 vect_tc <- maybeV (lookupTyCon tycon)
339 (vty, lty) <- vectAndLiftType ty
340 vexpr <- vectExpr scrut
341 (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
345 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
346 vect_dc <- maybeV (lookupDataCon dc)
347 let [arr_dc] = tyConDataCons arr_tc
348 repr <- mkRepr vect_tc
349 shape_bndrs <- arrShapeVars repr
350 return . vLet (vNonRec vbndr vexpr)
351 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
353 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
354 | otherwise = vectBndrIn bndr
356 vectAlgCase tycon _ty_args scrut bndr ty alts
358 vect_tc <- maybeV (lookupTyCon tycon)
359 (vty, lty) <- vectAndLiftType ty
360 repr <- mkRepr vect_tc
361 shape_bndrs <- arrShapeVars repr
362 (len, sel, indices) <- arrSelector repr (map Var shape_bndrs)
364 (vbndr, valts) <- vect_scrut_bndr $ mapM (proc_alt sel vty lty) alts'
365 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
367 vexpr <- vectExpr scrut
368 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
369 let [arr_dc] = tyConDataCons arr_tc
371 let (vect_scrut, lift_scrut) = vscrut
372 (vect_bodies, lift_bodies) = unzip vbodies
374 vdummy <- newDummyVar (exprType vect_scrut)
375 ldummy <- newDummyVar (exprType lift_scrut)
376 let vect_case = Case vect_scrut vdummy vty
377 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
379 lbody <- combinePA vty len sel indices lift_bodies
380 let lift_case = Case lift_scrut ldummy lty
381 [(DataAlt arr_dc, shape_bndrs ++ concat lift_bndrss,
384 return . vLet (vNonRec vbndr vexpr)
385 $ (vect_case, lift_case)
387 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
388 | otherwise = vectBndrIn bndr
390 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
392 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
393 cmp DEFAULT DEFAULT = EQ
396 cmp _ _ = panic "vectAlgCase/cmp"
398 proc_alt sel vty lty (DataAlt dc, bndrs, body)
400 vect_dc <- maybeV (lookupDataCon dc)
401 let tag = mkDataConTag vect_dc
402 fvs = freeVarsOf body `delVarSetList` bndrs
403 (vect_bndrs, lift_bndrs, vbody)
404 <- vect_alt_bndrs bndrs
405 $ \len -> packLiftingContext len sel tag fvs vty lty
408 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
409 proc_alt _ _ _ _ = panic "vectAlgCase/proc_alt"
413 void_tc <- builtin voidTyCon
414 let void_ty = mkTyConApp void_tc []
415 arr_ty <- mkPArrayType void_ty
416 bndr <- newLocalVar (fsLit "voids") arr_ty
417 len <- lengthPA void_ty (Var bndr)
419 return ([], [bndr], e)
421 vect_alt_bndrs bndrs p
424 vbndrs <- mapM vectBndr bndrs
425 let (vect_bndrs, lift_bndrs) = unzip vbndrs
428 len <- lengthPA (idType vv) (Var lv)
430 return (vect_bndrs, lift_bndrs, e)
432 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
434 packLiftingContext :: CoreExpr -> CoreExpr -> CoreExpr -> VarSet
435 -> Type -> Type -> VM VExpr -> VM VExpr
436 packLiftingContext len shape tag fvs vty lty p
438 select <- builtin selectPAIntPrimVar
439 let sel_expr = mkApps (Var select) [shape, tag]
440 sel_var <- newLocalVar (fsLit "sel#") (exprType sel_expr)
441 lc_var <- builtin liftingContext
444 bnds <- mapM (packFreeVar (Var lc_var) (Var sel_var))
449 return (vexpr, Let (NonRec sel_var sel_expr)
450 $ Case len lc_var lty
451 [(DEFAULT, [], mkLets (concat bnds) lexpr),
452 (LitAlt (mkMachInt 0), [], empty)])
454 packFreeVar :: CoreExpr -> CoreExpr -> Var -> VM [CoreBind]
455 packFreeVar len sel v
462 expr <- packPA (idType vv) (Var lv) len sel
464 return [(NonRec lv' expr)]
468 upd vv lv' env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv') }