2 module Vectorise( vectorise )
10 import HscTypes hiding ( MonadThings(..) )
12 import Module ( PackageId )
16 import CoreMonad ( CoreM, getHscEnv, liftIO )
20 import FamInstEnv ( extendFamInstEnvList )
29 import Literal ( Literal, mkMachInt )
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 <- liftPA 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 <- liftPA vexpr
182 return (vexpr, lexpr)
184 vectLiteral :: Literal -> VM VExpr
187 lexpr <- liftPA (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' <- vectExpr 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 <- liftPA 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@(fvs, AnnLam bndr _)
267 | isId bndr = onlyIfV (isEmptyVarSet fvs) (vectScalarLam bs $ deAnnotate body)
268 `orElseV` vectLam fvs bs body
270 (bs,body) = collectAnnValBinders e
272 vectExpr e = cantVectorise "Can't vectorise expression" (ppr $ deAnnotate e)
274 vectScalarLam :: [Var] -> CoreExpr -> VM VExpr
275 vectScalarLam args body
277 scalars <- globalScalars
278 onlyIfV (all is_scalar_ty arg_tys
279 && is_scalar_ty res_ty
280 && is_scalar (extendVarSetList scalars args) body)
282 fn_var <- hoistExpr (fsLit "fn") (mkLams args body)
283 zipf <- zipScalars arg_tys res_ty
284 clo <- scalarClosure arg_tys res_ty (Var fn_var)
285 (zipf `App` Var fn_var)
286 clo_var <- hoistExpr (fsLit "clo") clo
287 lclo <- liftPA (Var clo_var)
288 return (Var clo_var, lclo)
290 arg_tys = map idType args
291 res_ty = exprType body
293 is_scalar_ty ty | Just (tycon, []) <- splitTyConApp_maybe ty
295 || tycon == floatTyCon
296 || tycon == doubleTyCon
300 is_scalar vs (Var v) = v `elemVarSet` vs
301 is_scalar _ e@(Lit l) = is_scalar_ty $ exprType e
302 is_scalar vs (App e1 e2) = is_scalar vs e1 && is_scalar vs e2
303 is_scalar _ _ = False
305 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
308 tyvars <- localTyVars
309 (vs, vvs) <- readLEnv $ \env ->
310 unzip [(var, vv) | var <- varSetElems fvs
311 , Just vv <- [lookupVarEnv (local_vars env) var]]
313 arg_tys <- mapM (vectType . idType) bs
314 res_ty <- vectType (exprType $ deAnnotate body)
316 buildClosures tyvars vvs arg_tys res_ty
317 . hoistPolyVExpr tyvars
319 lc <- builtin liftingContext
320 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
322 return $ vLams lc vbndrs vbody
324 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
325 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
326 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
327 (ppr $ deAnnotate e `mkTyApps` tys)
331 -- case e :: t of v { ... }
335 -- V: let v' = e in case v' of _ { ... }
336 -- L: let v' = e in case v' `cast` ... of _ { ... }
338 -- When lifting, we have to do it this way because v must have the type
339 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
340 -- have to handle the case where v is a wild var correctly.
343 -- FIXME: this is too lazy
344 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
345 -> [(AltCon, [Var], CoreExprWithFVs)]
347 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
349 vscrut <- vectExpr scrut
350 (vty, lty) <- vectAndLiftType ty
351 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
352 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
354 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
356 vscrut <- vectExpr scrut
357 (vty, lty) <- vectAndLiftType ty
358 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
359 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
361 vectAlgCase tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
363 vect_tc <- maybeV (lookupTyCon tycon)
364 (vty, lty) <- vectAndLiftType ty
365 vexpr <- vectExpr scrut
366 (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
370 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
371 vect_dc <- maybeV (lookupDataCon dc)
372 let [arr_dc] = tyConDataCons arr_tc
373 repr <- mkRepr vect_tc
374 shape_bndrs <- arrShapeVars repr
375 return . vLet (vNonRec vbndr vexpr)
376 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
378 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
379 | otherwise = vectBndrIn bndr
381 vectAlgCase tycon _ty_args scrut bndr ty alts
383 vect_tc <- maybeV (lookupTyCon tycon)
384 (vty, lty) <- vectAndLiftType ty
385 repr <- mkRepr vect_tc
386 shape_bndrs <- arrShapeVars repr
387 (len, sel, indices) <- arrSelector repr (map Var shape_bndrs)
389 (vbndr, valts) <- vect_scrut_bndr $ mapM (proc_alt sel vty lty) alts'
390 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
392 vexpr <- vectExpr scrut
393 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
394 let [arr_dc] = tyConDataCons arr_tc
396 let (vect_scrut, lift_scrut) = vscrut
397 (vect_bodies, lift_bodies) = unzip vbodies
399 vdummy <- newDummyVar (exprType vect_scrut)
400 ldummy <- newDummyVar (exprType lift_scrut)
401 let vect_case = Case vect_scrut vdummy vty
402 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
404 lbody <- combinePA vty len sel indices lift_bodies
405 let lift_case = Case lift_scrut ldummy lty
406 [(DataAlt arr_dc, shape_bndrs ++ concat lift_bndrss,
409 return . vLet (vNonRec vbndr vexpr)
410 $ (vect_case, lift_case)
412 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
413 | otherwise = vectBndrIn bndr
415 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
417 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
418 cmp DEFAULT DEFAULT = EQ
421 cmp _ _ = panic "vectAlgCase/cmp"
423 proc_alt sel vty lty (DataAlt dc, bndrs, body)
425 vect_dc <- maybeV (lookupDataCon dc)
426 let tag = mkDataConTag vect_dc
427 fvs = freeVarsOf body `delVarSetList` bndrs
428 (vect_bndrs, lift_bndrs, vbody)
429 <- vect_alt_bndrs bndrs
430 $ \len -> packLiftingContext len sel tag fvs vty lty
433 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
434 proc_alt _ _ _ _ = panic "vectAlgCase/proc_alt"
438 void_tc <- builtin voidTyCon
439 let void_ty = mkTyConApp void_tc []
440 arr_ty <- mkPArrayType void_ty
441 bndr <- newLocalVar (fsLit "voids") arr_ty
442 len <- lengthPA void_ty (Var bndr)
444 return ([], [bndr], e)
446 vect_alt_bndrs bndrs p
449 vbndrs <- mapM vectBndr bndrs
450 let (vect_bndrs, lift_bndrs) = unzip vbndrs
453 len <- lengthPA (idType vv) (Var lv)
455 return (vect_bndrs, lift_bndrs, e)
457 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
459 packLiftingContext :: CoreExpr -> CoreExpr -> CoreExpr -> VarSet
460 -> Type -> Type -> VM VExpr -> VM VExpr
461 packLiftingContext len shape tag fvs vty lty p
463 select <- builtin selectPAIntPrimVar
464 let sel_expr = mkApps (Var select) [shape, tag]
465 sel_var <- newLocalVar (fsLit "sel#") (exprType sel_expr)
466 lc_var <- builtin liftingContext
469 bnds <- mapM (packFreeVar (Var lc_var) (Var sel_var))
474 return (vexpr, Let (NonRec sel_var sel_expr)
475 $ Case len lc_var lty
476 [(DEFAULT, [], mkLets (concat bnds) lexpr),
477 (LitAlt (mkMachInt 0), [], empty)])
479 packFreeVar :: CoreExpr -> CoreExpr -> Var -> VM [CoreBind]
480 packFreeVar len sel v
487 expr <- packPA (idType vv) (Var lv) len sel
489 return [(NonRec lv' expr)]
493 upd vv lv' env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv') }