2 module Vectorise( vectorise )
10 import HscTypes hiding ( MonadThings(..) )
12 import Module ( PackageId )
16 import CoreMonad ( CoreM, getHscEnv )
20 import FamInstEnv ( extendFamInstEnvList )
27 import Literal ( Literal, mkMachInt )
32 import Control.Monad ( liftM, liftM2, zipWithM )
33 import Data.List ( sortBy, unzip4 )
35 vectorise :: PackageId -> ModGuts -> CoreM ModGuts
36 vectorise backend guts = do
38 liftIO $ vectoriseIO backend hsc_env guts
40 vectoriseIO :: PackageId -> HscEnv -> ModGuts -> IO ModGuts
41 vectoriseIO backend hsc_env guts
44 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
45 Just (info', guts') <- initV backend hsc_env guts info (vectModule guts)
46 return (guts' { mg_vect_info = info' })
48 vectModule :: ModGuts -> VM ModGuts
51 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
53 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
54 updGEnv (setFamInstEnv fam_inst_env')
56 -- dicts <- mapM buildPADict pa_insts
57 -- workers <- mapM vectDataConWorkers pa_insts
58 binds' <- mapM vectTopBind (mg_binds guts)
59 return $ guts { mg_types = types'
60 , mg_binds = Rec tc_binds : binds'
61 , mg_fam_inst_env = fam_inst_env'
62 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
65 vectTopBind :: CoreBind -> VM CoreBind
66 vectTopBind b@(NonRec var expr)
68 var' <- vectTopBinder var
69 expr' <- vectTopRhs var expr
71 cexpr <- tryConvert var var' expr
72 return . Rec $ (var, cexpr) : (var', expr') : hs
76 vectTopBind b@(Rec bs)
78 vars' <- mapM vectTopBinder vars
79 exprs' <- zipWithM vectTopRhs vars exprs
81 cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
82 return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
86 (vars, exprs) = unzip bs
88 vectTopBinder :: Var -> VM Var
91 vty <- vectType (idType var)
92 var' <- cloneId mkVectOcc var vty
96 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
99 closedV . liftM vectorised
101 $ vectPolyExpr (freeVars expr)
103 tryConvert :: Var -> Var -> CoreExpr -> VM CoreExpr
104 tryConvert var vect_var rhs
105 = fromVect (idType var) (Var vect_var) `orElseV` return rhs
107 -- ----------------------------------------------------------------------------
110 vectBndr :: Var -> VM VVar
113 (vty, lty) <- vectAndLiftType (idType v)
114 let vv = v `Id.setIdType` vty
115 lv = v `Id.setIdType` lty
116 updLEnv (mapTo vv lv)
119 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
121 vectBndrNew :: Var -> FastString -> VM VVar
124 vty <- vectType (idType v)
125 vv <- newLocalVVar fs vty
129 upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
131 vectBndrIn :: Var -> VM a -> VM (VVar, a)
139 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
143 vv <- vectBndrNew v fs
147 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
151 vvs <- mapM vectBndr vs
155 -- ----------------------------------------------------------------------------
158 vectVar :: Var -> VM VExpr
163 Local (vv,lv) -> return (Var vv, Var lv)
166 lexpr <- liftPA vexpr
167 return (vexpr, lexpr)
169 vectPolyVar :: Var -> [Type] -> VM VExpr
172 vtys <- mapM vectType tys
175 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
176 (polyApply (Var lv) vtys)
178 vexpr <- polyApply (Var poly) vtys
179 lexpr <- liftPA vexpr
180 return (vexpr, lexpr)
182 vectLiteral :: Literal -> VM VExpr
185 lexpr <- liftPA (Lit lit)
186 return (Lit lit, lexpr)
188 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
189 vectPolyExpr (_, AnnNote note expr)
190 = liftM (vNote note) $ vectPolyExpr expr
192 = polyAbstract tvs $ \abstract ->
194 mono' <- vectFnExpr False mono
195 return $ mapVect abstract mono'
197 (tvs, mono) = collectAnnTypeBinders expr
199 vectExpr :: CoreExprWithFVs -> VM VExpr
200 vectExpr (_, AnnType ty)
201 = liftM vType (vectType ty)
203 vectExpr (_, AnnVar v) = vectVar v
205 vectExpr (_, AnnLit lit) = vectLiteral lit
207 vectExpr (_, AnnNote note expr)
208 = liftM (vNote note) (vectExpr expr)
210 vectExpr e@(_, AnnApp _ arg)
212 = vectTyAppExpr fn tys
214 (fn, tys) = collectAnnTypeArgs e
216 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
217 | Just con <- isDataConId_maybe v
220 let vexpr = App (Var v) (Lit lit)
221 lexpr <- liftPA vexpr
222 return (vexpr, lexpr)
224 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
227 vectExpr (_, AnnApp fn arg)
229 arg_ty' <- vectType arg_ty
230 res_ty' <- vectType res_ty
233 mkClosureApp arg_ty' res_ty' fn' arg'
235 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
237 vectExpr (_, AnnCase scrut bndr ty alts)
238 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
240 = vectAlgCase tycon ty_args scrut bndr ty alts
242 scrut_ty = exprType (deAnnotate scrut)
244 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
246 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
247 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
248 return $ vLet (vNonRec vbndr vrhs) vbody
250 vectExpr (_, AnnLet (AnnRec bs) body)
252 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
254 (zipWithM vect_rhs bndrs rhss)
256 return $ vLet (vRec vbndrs vrhss) vbody
258 (bndrs, rhss) = unzip bs
260 vect_rhs bndr rhs = localV
264 vectExpr e@(_, AnnLam bndr _)
265 | isId bndr = vectFnExpr True e
267 onlyIfV (isEmptyVarSet fvs) (vectScalarLam bs $ deAnnotate body)
268 `orElseV` vectLam True fvs bs body
270 (bs,body) = collectAnnValBinders e
273 vectExpr e = cantVectorise "Can't vectorise expression" (ppr $ deAnnotate e)
275 vectFnExpr :: Bool -> CoreExprWithFVs -> VM VExpr
276 vectFnExpr inline e@(fvs, AnnLam bndr _)
277 | isId bndr = onlyIfV (isEmptyVarSet fvs) (vectScalarLam bs $ deAnnotate body)
278 `orElseV` vectLam inline fvs bs body
280 (bs,body) = collectAnnValBinders e
281 vectFnExpr _ e = vectExpr e
284 vectScalarLam :: [Var] -> CoreExpr -> VM VExpr
285 vectScalarLam args body
287 scalars <- globalScalars
288 onlyIfV (all is_scalar_ty arg_tys
289 && is_scalar_ty res_ty
290 && is_scalar (extendVarSetList scalars args) body)
292 fn_var <- hoistExpr (fsLit "fn") (mkLams args body)
293 zipf <- zipScalars arg_tys res_ty
294 clo <- scalarClosure arg_tys res_ty (Var fn_var)
295 (zipf `App` Var fn_var)
296 clo_var <- hoistExpr (fsLit "clo") clo
297 lclo <- liftPA (Var clo_var)
298 return (Var clo_var, lclo)
300 arg_tys = map idType args
301 res_ty = exprType body
303 is_scalar_ty ty | Just (tycon, []) <- splitTyConApp_maybe ty
305 || tycon == floatTyCon
306 || tycon == doubleTyCon
310 is_scalar vs (Var v) = v `elemVarSet` vs
311 is_scalar _ e@(Lit _) = is_scalar_ty $ exprType e
312 is_scalar vs (App e1 e2) = is_scalar vs e1 && is_scalar vs e2
313 is_scalar _ _ = False
315 vectLam :: Bool -> VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
316 vectLam inline fvs bs body
318 tyvars <- localTyVars
319 (vs, vvs) <- readLEnv $ \env ->
320 unzip [(var, vv) | var <- varSetElems fvs
321 , Just vv <- [lookupVarEnv (local_vars env) var]]
323 arg_tys <- mapM (vectType . idType) bs
324 res_ty <- vectType (exprType $ deAnnotate body)
326 buildClosures tyvars vvs arg_tys res_ty
327 . hoistPolyVExpr tyvars
329 lc <- builtin liftingContext
330 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
332 return . maybe_inline $ vLams lc vbndrs vbody
334 maybe_inline = if inline then vInlineMe else id
336 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
337 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
338 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
339 (ppr $ deAnnotate e `mkTyApps` tys)
343 -- case e :: t of v { ... }
347 -- V: let v' = e in case v' of _ { ... }
348 -- L: let v' = e in case v' `cast` ... of _ { ... }
350 -- When lifting, we have to do it this way because v must have the type
351 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
352 -- have to handle the case where v is a wild var correctly.
355 -- FIXME: this is too lazy
356 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
357 -> [(AltCon, [Var], CoreExprWithFVs)]
359 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
361 vscrut <- vectExpr scrut
362 (vty, lty) <- vectAndLiftType ty
363 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
364 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
366 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
368 vscrut <- vectExpr scrut
369 (vty, lty) <- vectAndLiftType ty
370 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
371 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
373 vectAlgCase tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
375 vect_tc <- maybeV (lookupTyCon tycon)
376 (vty, lty) <- vectAndLiftType ty
377 vexpr <- vectExpr scrut
378 (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
382 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
383 vect_dc <- maybeV (lookupDataCon dc)
384 let [arr_dc] = tyConDataCons arr_tc
385 repr <- mkRepr vect_tc
386 shape_bndrs <- arrShapeVars repr
387 return . vLet (vNonRec vbndr vexpr)
388 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
390 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
391 | otherwise = vectBndrIn bndr
393 vectAlgCase tycon _ty_args scrut bndr ty alts
395 vect_tc <- maybeV (lookupTyCon tycon)
396 (vty, lty) <- vectAndLiftType ty
397 repr <- mkRepr vect_tc
398 shape_bndrs <- arrShapeVars repr
399 (len, sel, indices) <- arrSelector repr (map Var shape_bndrs)
401 (vbndr, valts) <- vect_scrut_bndr $ mapM (proc_alt sel vty lty) alts'
402 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
404 vexpr <- vectExpr scrut
405 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
406 let [arr_dc] = tyConDataCons arr_tc
408 let (vect_scrut, lift_scrut) = vscrut
409 (vect_bodies, lift_bodies) = unzip vbodies
411 vdummy <- newDummyVar (exprType vect_scrut)
412 ldummy <- newDummyVar (exprType lift_scrut)
413 let vect_case = Case vect_scrut vdummy vty
414 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
416 lbody <- combinePA vty len sel indices lift_bodies
417 let lift_case = Case lift_scrut ldummy lty
418 [(DataAlt arr_dc, shape_bndrs ++ concat lift_bndrss,
421 return . vLet (vNonRec vbndr vexpr)
422 $ (vect_case, lift_case)
424 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
425 | otherwise = vectBndrIn bndr
427 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
429 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
430 cmp DEFAULT DEFAULT = EQ
433 cmp _ _ = panic "vectAlgCase/cmp"
435 proc_alt sel vty lty (DataAlt dc, bndrs, body)
437 vect_dc <- maybeV (lookupDataCon dc)
438 let tag = mkDataConTag vect_dc
439 fvs = freeVarsOf body `delVarSetList` bndrs
440 (vect_bndrs, lift_bndrs, vbody)
441 <- vect_alt_bndrs bndrs
442 $ \len -> packLiftingContext len sel tag fvs vty lty
445 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
446 proc_alt _ _ _ _ = panic "vectAlgCase/proc_alt"
450 void_tc <- builtin voidTyCon
451 let void_ty = mkTyConApp void_tc []
452 arr_ty <- mkPArrayType void_ty
453 bndr <- newLocalVar (fsLit "voids") arr_ty
454 len <- lengthPA void_ty (Var bndr)
456 return ([], [bndr], e)
458 vect_alt_bndrs bndrs p
461 vbndrs <- mapM vectBndr bndrs
462 let (vect_bndrs, lift_bndrs) = unzip vbndrs
465 len <- lengthPA (idType vv) (Var lv)
467 return (vect_bndrs, lift_bndrs, e)
469 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
471 packLiftingContext :: CoreExpr -> CoreExpr -> CoreExpr -> VarSet
472 -> Type -> Type -> VM VExpr -> VM VExpr
473 packLiftingContext len shape tag fvs vty lty p
475 select <- builtin selectPAIntPrimVar
476 let sel_expr = mkApps (Var select) [shape, tag]
477 sel_var <- newLocalVar (fsLit "sel#") (exprType sel_expr)
478 lc_var <- builtin liftingContext
481 bnds <- mapM (packFreeVar (Var lc_var) (Var sel_var))
486 return (vexpr, Let (NonRec sel_var sel_expr)
487 $ Case len lc_var lty
488 [(DEFAULT, [], mkLets (concat bnds) lexpr),
489 (LitAlt (mkMachInt 0), [], empty)])
491 packFreeVar :: CoreExpr -> CoreExpr -> Var -> VM [CoreBind]
492 packFreeVar len sel v
499 expr <- packPA (idType vv) (Var lv) len sel
501 return [(NonRec lv' expr)]
505 upd vv lv' env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv') }