2 module Vectorise( vectorise )
10 import HscTypes hiding ( MonadThings(..) )
12 import Module ( PackageId )
15 import MkCore ( mkWildCase )
17 import CoreMonad ( CoreM, getHscEnv )
21 import FamInstEnv ( extendFamInstEnvList )
28 import Literal ( Literal, mkMachInt )
30 import TysPrim ( intPrimTy )
34 import Control.Monad ( liftM, liftM2, zipWithM )
35 import Data.List ( sortBy, unzip4 )
37 vectorise :: PackageId -> ModGuts -> CoreM ModGuts
38 vectorise backend guts = do
40 liftIO $ vectoriseIO backend hsc_env guts
42 vectoriseIO :: PackageId -> HscEnv -> ModGuts -> IO ModGuts
43 vectoriseIO backend hsc_env guts
46 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
47 Just (info', guts') <- initV backend hsc_env guts info (vectModule guts)
48 return (guts' { mg_vect_info = info' })
50 vectModule :: ModGuts -> VM ModGuts
53 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
55 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
56 updGEnv (setFamInstEnv fam_inst_env')
58 -- dicts <- mapM buildPADict pa_insts
59 -- workers <- mapM vectDataConWorkers pa_insts
60 binds' <- mapM vectTopBind (mg_binds guts)
61 return $ guts { mg_types = types'
62 , mg_binds = Rec tc_binds : binds'
63 , mg_fam_inst_env = fam_inst_env'
64 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
67 vectTopBind :: CoreBind -> VM CoreBind
68 vectTopBind b@(NonRec var expr)
70 var' <- vectTopBinder var
71 expr' <- vectTopRhs var expr
73 cexpr <- tryConvert var var' expr
74 return . Rec $ (var, cexpr) : (var', expr') : hs
78 vectTopBind b@(Rec bs)
80 vars' <- mapM vectTopBinder vars
81 exprs' <- zipWithM vectTopRhs vars exprs
83 cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
84 return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
88 (vars, exprs) = unzip bs
90 vectTopBinder :: Var -> VM Var
93 vty <- vectType (idType var)
94 var' <- cloneId mkVectOcc var vty
98 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
101 closedV . liftM vectorised
103 $ vectPolyExpr (freeVars expr)
105 tryConvert :: Var -> Var -> CoreExpr -> VM CoreExpr
106 tryConvert var vect_var rhs
107 = fromVect (idType var) (Var vect_var) `orElseV` return rhs
109 -- ----------------------------------------------------------------------------
112 vectBndr :: Var -> VM VVar
115 (vty, lty) <- vectAndLiftType (idType v)
116 let vv = v `Id.setIdType` vty
117 lv = v `Id.setIdType` lty
118 updLEnv (mapTo vv lv)
121 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
123 vectBndrNew :: Var -> FastString -> VM VVar
126 vty <- vectType (idType v)
127 vv <- newLocalVVar fs vty
131 upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
133 vectBndrIn :: Var -> VM a -> VM (VVar, a)
141 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
145 vv <- vectBndrNew v fs
149 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
153 vvs <- mapM vectBndr vs
157 -- ----------------------------------------------------------------------------
160 vectVar :: Var -> VM VExpr
165 Local (vv,lv) -> return (Var vv, Var lv)
168 lexpr <- liftPD vexpr
169 return (vexpr, lexpr)
171 vectPolyVar :: Var -> [Type] -> VM VExpr
174 vtys <- mapM vectType tys
177 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
178 (polyApply (Var lv) vtys)
180 vexpr <- polyApply (Var poly) vtys
181 lexpr <- liftPD vexpr
182 return (vexpr, lexpr)
184 vectLiteral :: Literal -> VM VExpr
187 lexpr <- liftPD (Lit lit)
188 return (Lit lit, lexpr)
190 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
191 vectPolyExpr (_, AnnNote note expr)
192 = liftM (vNote note) $ vectPolyExpr expr
194 = polyAbstract tvs $ \abstract ->
196 mono' <- vectFnExpr False mono
197 return $ mapVect abstract mono'
199 (tvs, mono) = collectAnnTypeBinders expr
201 vectExpr :: CoreExprWithFVs -> VM VExpr
202 vectExpr (_, AnnType ty)
203 = liftM vType (vectType ty)
205 vectExpr (_, AnnVar v) = vectVar v
207 vectExpr (_, AnnLit lit) = vectLiteral lit
209 vectExpr (_, AnnNote note expr)
210 = liftM (vNote note) (vectExpr expr)
212 vectExpr e@(_, AnnApp _ arg)
214 = vectTyAppExpr fn tys
216 (fn, tys) = collectAnnTypeArgs e
218 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
219 | Just con <- isDataConId_maybe v
222 let vexpr = App (Var v) (Lit lit)
223 lexpr <- liftPD vexpr
224 return (vexpr, lexpr)
226 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
229 vectExpr (_, AnnApp fn arg)
231 arg_ty' <- vectType arg_ty
232 res_ty' <- vectType res_ty
235 mkClosureApp arg_ty' res_ty' fn' arg'
237 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
239 vectExpr (_, AnnCase scrut bndr ty alts)
240 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
242 = vectAlgCase tycon ty_args scrut bndr ty alts
244 scrut_ty = exprType (deAnnotate scrut)
246 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
248 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
249 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
250 return $ vLet (vNonRec vbndr vrhs) vbody
252 vectExpr (_, AnnLet (AnnRec bs) body)
254 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
256 (zipWithM vect_rhs bndrs rhss)
258 return $ vLet (vRec vbndrs vrhss) vbody
260 (bndrs, rhss) = unzip bs
262 vect_rhs bndr rhs = localV
266 vectExpr e@(_, AnnLam bndr _)
267 | isId bndr = vectFnExpr True e
269 onlyIfV (isEmptyVarSet fvs) (vectScalarLam bs $ deAnnotate body)
270 `orElseV` vectLam True fvs bs body
272 (bs,body) = collectAnnValBinders e
275 vectExpr e = cantVectorise "Can't vectorise expression" (ppr $ deAnnotate e)
277 vectFnExpr :: Bool -> CoreExprWithFVs -> VM VExpr
278 vectFnExpr inline e@(fvs, AnnLam bndr _)
279 | isId bndr = onlyIfV (isEmptyVarSet fvs) (vectScalarLam bs $ deAnnotate body)
280 `orElseV` vectLam inline fvs bs body
282 (bs,body) = collectAnnValBinders e
283 vectFnExpr _ e = vectExpr e
286 vectScalarLam :: [Var] -> CoreExpr -> VM VExpr
287 vectScalarLam args body
289 scalars <- globalScalars
290 onlyIfV (all is_scalar_ty arg_tys
291 && is_scalar_ty res_ty
292 && is_scalar (extendVarSetList scalars args) body)
294 fn_var <- hoistExpr (fsLit "fn") (mkLams args body)
295 zipf <- zipScalars arg_tys res_ty
296 clo <- scalarClosure arg_tys res_ty (Var fn_var)
297 (zipf `App` Var fn_var)
298 clo_var <- hoistExpr (fsLit "clo") clo
299 lclo <- liftPD (Var clo_var)
300 return (Var clo_var, lclo)
302 arg_tys = map idType args
303 res_ty = exprType body
305 is_scalar_ty ty | Just (tycon, []) <- splitTyConApp_maybe ty
307 || tycon == floatTyCon
308 || tycon == doubleTyCon
312 is_scalar vs (Var v) = v `elemVarSet` vs
313 is_scalar _ e@(Lit _) = is_scalar_ty $ exprType e
314 is_scalar vs (App e1 e2) = is_scalar vs e1 && is_scalar vs e2
315 is_scalar _ _ = False
317 vectLam :: Bool -> VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
318 vectLam inline fvs bs body
320 tyvars <- localTyVars
321 (vs, vvs) <- readLEnv $ \env ->
322 unzip [(var, vv) | var <- varSetElems fvs
323 , Just vv <- [lookupVarEnv (local_vars env) var]]
325 arg_tys <- mapM (vectType . idType) bs
326 res_ty <- vectType (exprType $ deAnnotate body)
328 buildClosures tyvars vvs arg_tys res_ty
329 . hoistPolyVExpr tyvars
331 lc <- builtin liftingContext
332 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
334 return . maybe_inline $ vLams lc vbndrs vbody
336 maybe_inline = if inline then vInlineMe else id
338 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
339 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
340 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
341 (ppr $ deAnnotate e `mkTyApps` tys)
345 -- case e :: t of v { ... }
349 -- V: let v' = e in case v' of _ { ... }
350 -- L: let v' = e in case v' `cast` ... of _ { ... }
352 -- When lifting, we have to do it this way because v must have the type
353 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
354 -- have to handle the case where v is a wild var correctly.
357 -- FIXME: this is too lazy
358 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
359 -> [(AltCon, [Var], CoreExprWithFVs)]
361 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
363 vscrut <- vectExpr scrut
364 (vty, lty) <- vectAndLiftType ty
365 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
366 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
368 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
370 vscrut <- vectExpr scrut
371 (vty, lty) <- vectAndLiftType ty
372 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
373 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
375 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
377 (vty, lty) <- vectAndLiftType ty
378 vexpr <- vectExpr scrut
379 (vbndr, (vbndrs, (vect_body, lift_body)))
383 let (vect_bndrs, lift_bndrs) = unzip vbndrs
384 (vscrut, lscrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
385 vect_dc <- maybeV (lookupDataCon dc)
386 let [pdata_dc] = tyConDataCons pdata_tc
388 let vcase = mk_wild_case vscrut vty vect_dc vect_bndrs vect_body
389 lcase = mk_wild_case lscrut lty pdata_dc lift_bndrs lift_body
391 return $ vLet (vNonRec vbndr vexpr) (vcase, lcase)
393 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
394 | otherwise = vectBndrIn bndr
396 mk_wild_case expr ty dc bndrs body
397 = mkWildCase expr (exprType expr) ty [(DataAlt dc, bndrs, body)]
399 vectAlgCase tycon _ty_args scrut bndr ty alts
401 vect_tc <- maybeV (lookupTyCon tycon)
402 (vty, lty) <- vectAndLiftType ty
404 let arity = length (tyConDataCons vect_tc)
405 sel_ty <- builtin (selTy arity)
406 sel_bndr <- newLocalVar (fsLit "sel") sel_ty
407 let sel = Var sel_bndr
409 (vbndr, valts) <- vect_scrut_bndr
410 $ mapM (proc_alt arity sel vty lty) alts'
411 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
413 vexpr <- vectExpr scrut
414 (vect_scrut, lift_scrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
415 let [pdata_dc] = tyConDataCons pdata_tc
417 let (vect_bodies, lift_bodies) = unzip vbodies
419 vdummy <- newDummyVar (exprType vect_scrut)
420 ldummy <- newDummyVar (exprType lift_scrut)
421 let vect_case = Case vect_scrut vdummy vty
422 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
424 lc <- builtin liftingContext
425 lbody <- combinePD vty (Var lc) sel lift_bodies
426 let lift_case = Case lift_scrut ldummy lty
427 [(DataAlt pdata_dc, sel_bndr : concat lift_bndrss,
430 return . vLet (vNonRec vbndr vexpr)
431 $ (vect_case, lift_case)
433 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
434 | otherwise = vectBndrIn bndr
436 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
438 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
439 cmp DEFAULT DEFAULT = EQ
442 cmp _ _ = panic "vectAlgCase/cmp"
444 proc_alt arity sel vty lty (DataAlt dc, bndrs, body)
446 vect_dc <- maybeV (lookupDataCon dc)
447 let ntag = dataConTagZ vect_dc
448 tag = mkDataConTag vect_dc
449 fvs = freeVarsOf body `delVarSetList` bndrs
451 sel_tags <- liftM (`App` sel) (builtin (selTags arity))
452 lc <- builtin liftingContext
453 elems <- builtin (selElements arity ntag)
459 binds <- mapM (pack_var (Var lc) sel_tags tag)
462 (ve, le) <- vectExpr body
463 return (ve, Case (elems `App` sel) lc lty
464 [(DEFAULT, [], (mkLets (concat binds) le))])
465 -- empty <- emptyPD vty
466 -- return (ve, Case (elems `App` sel) lc lty
467 -- [(DEFAULT, [], Let (NonRec flags_var flags_expr)
468 -- $ mkLets (concat binds) le),
469 -- (LitAlt (mkMachInt 0), [], empty)])
470 let (vect_bndrs, lift_bndrs) = unzip vbndrs
471 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
473 proc_alt _ _ _ _ _ = panic "vectAlgCase/proc_alt"
475 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
477 pack_var len tags t v
484 expr <- packByTagPD (idType vv) (Var lv) len tags t
485 updLEnv (\env -> env { local_vars = extendVarEnv
486 (local_vars env) v (vv, lv') })
487 return [(NonRec lv' expr)]