2 module Vectorise( vectorise )
13 import CoreLint ( showPass, endPass )
17 import SimplMonad ( SimplCount, zeroSimplCount )
18 import Rules ( RuleBase )
22 import FamInstEnv ( extendFamInstEnvList )
31 import Literal ( Literal, mkMachInt )
36 import Control.Monad ( liftM, liftM2, zipWithM )
37 import Data.List ( sortBy, unzip4 )
39 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
40 -> IO (SimplCount, ModGuts)
41 vectorise hsc_env _ _ guts
43 showPass dflags "Vectorisation"
45 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
46 Just (info', guts') <- initV hsc_env guts info (vectModule guts)
47 endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
48 return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
50 dflags = hsc_dflags hsc_env
52 vectModule :: ModGuts -> VM ModGuts
55 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
57 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
58 updGEnv (setFamInstEnv fam_inst_env')
60 -- dicts <- mapM buildPADict pa_insts
61 -- workers <- mapM vectDataConWorkers pa_insts
62 binds' <- mapM vectTopBind (mg_binds guts)
63 return $ guts { mg_types = types'
64 , mg_binds = Rec tc_binds : binds'
65 , mg_fam_inst_env = fam_inst_env'
66 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
69 vectTopBind :: CoreBind -> VM CoreBind
70 vectTopBind b@(NonRec var expr)
72 var' <- vectTopBinder var
73 expr' <- vectTopRhs var expr
75 cexpr <- tryConvert var var' expr
76 return . Rec $ (var, cexpr) : (var', expr') : hs
80 vectTopBind b@(Rec bs)
82 vars' <- mapM vectTopBinder vars
83 exprs' <- zipWithM vectTopRhs vars exprs
85 cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
86 return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
90 (vars, exprs) = unzip bs
92 vectTopBinder :: Var -> VM Var
95 vty <- vectType (idType var)
96 var' <- cloneId mkVectOcc var vty
100 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
103 closedV . liftM vectorised
105 $ vectPolyExpr (freeVars expr)
107 tryConvert :: Var -> Var -> CoreExpr -> VM CoreExpr
108 tryConvert var vect_var rhs
109 = fromVect (idType var) (Var vect_var) `orElseV` return rhs
111 -- ----------------------------------------------------------------------------
114 vectBndr :: Var -> VM VVar
117 (vty, lty) <- vectAndLiftType (idType v)
118 let vv = v `Id.setIdType` vty
119 lv = v `Id.setIdType` lty
120 updLEnv (mapTo vv lv)
123 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
125 vectBndrNew :: Var -> FastString -> VM VVar
128 vty <- vectType (idType v)
129 vv <- newLocalVVar fs vty
133 upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
135 vectBndrIn :: Var -> VM a -> VM (VVar, a)
143 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
147 vv <- vectBndrNew v fs
151 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
155 vvs <- mapM vectBndr vs
159 -- ----------------------------------------------------------------------------
162 vectVar :: Var -> VM VExpr
167 Local (vv,lv) -> return (Var vv, Var lv)
170 lexpr <- liftPA vexpr
171 return (vexpr, lexpr)
173 vectPolyVar :: Var -> [Type] -> VM VExpr
176 vtys <- mapM vectType tys
179 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
180 (polyApply (Var lv) vtys)
182 vexpr <- polyApply (Var poly) vtys
183 lexpr <- liftPA vexpr
184 return (vexpr, lexpr)
186 vectLiteral :: Literal -> VM VExpr
189 lexpr <- liftPA (Lit lit)
190 return (Lit lit, lexpr)
192 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
193 vectPolyExpr (_, AnnNote note expr)
194 = liftM (vNote note) $ vectPolyExpr expr
196 = polyAbstract tvs $ \abstract ->
198 mono' <- vectExpr mono
199 return $ mapVect abstract mono'
201 (tvs, mono) = collectAnnTypeBinders expr
203 vectExpr :: CoreExprWithFVs -> VM VExpr
204 vectExpr (_, AnnType ty)
205 = liftM vType (vectType ty)
207 vectExpr (_, AnnVar v) = vectVar v
209 vectExpr (_, AnnLit lit) = vectLiteral lit
211 vectExpr (_, AnnNote note expr)
212 = liftM (vNote note) (vectExpr expr)
214 vectExpr e@(_, AnnApp _ arg)
216 = vectTyAppExpr fn tys
218 (fn, tys) = collectAnnTypeArgs e
220 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
221 | Just con <- isDataConId_maybe v
224 let vexpr = App (Var v) (Lit lit)
225 lexpr <- liftPA vexpr
226 return (vexpr, lexpr)
228 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
231 vectExpr (_, AnnApp fn arg)
233 arg_ty' <- vectType arg_ty
234 res_ty' <- vectType res_ty
237 mkClosureApp arg_ty' res_ty' fn' arg'
239 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
241 vectExpr (_, AnnCase scrut bndr ty alts)
242 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
244 = vectAlgCase tycon ty_args scrut bndr ty alts
246 scrut_ty = exprType (deAnnotate scrut)
248 vectExpr (_, AnnCase _ _ _ _)
249 = panic "vectExpr: case"
251 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
253 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
254 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
255 return $ vLet (vNonRec vbndr vrhs) vbody
257 vectExpr (_, AnnLet (AnnRec bs) body)
259 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
261 (zipWithM vect_rhs bndrs rhss)
263 return $ vLet (vRec vbndrs vrhss) vbody
265 (bndrs, rhss) = unzip bs
267 vect_rhs bndr rhs = localV
271 vectExpr e@(fvs, AnnLam bndr _)
272 | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
273 | otherwise = vectLam fvs bs body
275 (bs,body) = collectAnnValBinders e
277 vectExpr e = pprPanic "vectExpr" (ppr $ deAnnotate e)
279 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
282 tyvars <- localTyVars
283 (vs, vvs) <- readLEnv $ \env ->
284 unzip [(var, vv) | var <- varSetElems fvs
285 , Just vv <- [lookupVarEnv (local_vars env) var]]
287 arg_tys <- mapM (vectType . idType) bs
288 res_ty <- vectType (exprType $ deAnnotate body)
290 buildClosures tyvars vvs arg_tys res_ty
291 . hoistPolyVExpr tyvars
293 lc <- builtin liftingContext
294 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
296 return $ vLams lc vbndrs vbody
298 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
299 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
300 vectTyAppExpr e _ = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
304 -- case e :: t of v { ... }
308 -- V: let v' = e in case v' of _ { ... }
309 -- L: let v' = e in case v' `cast` ... of _ { ... }
311 -- When lifting, we have to do it this way because v must have the type
312 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
313 -- have to handle the case where v is a wild var correctly.
316 -- FIXME: this is too lazy
317 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
318 -> [(AltCon, [Var], CoreExprWithFVs)]
320 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
322 vscrut <- vectExpr scrut
323 (vty, lty) <- vectAndLiftType ty
324 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
325 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
327 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
329 vscrut <- vectExpr scrut
330 (vty, lty) <- vectAndLiftType ty
331 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
332 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
334 vectAlgCase tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
336 vect_tc <- maybeV (lookupTyCon tycon)
337 (vty, lty) <- vectAndLiftType ty
338 vexpr <- vectExpr scrut
339 (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
343 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
344 vect_dc <- maybeV (lookupDataCon dc)
345 let [arr_dc] = tyConDataCons arr_tc
346 repr <- mkRepr vect_tc
347 shape_bndrs <- arrShapeVars repr
348 return . vLet (vNonRec vbndr vexpr)
349 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
351 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
352 | otherwise = vectBndrIn bndr
354 vectAlgCase tycon _ty_args scrut bndr ty alts
356 vect_tc <- maybeV (lookupTyCon tycon)
357 (vty, lty) <- vectAndLiftType ty
358 repr <- mkRepr vect_tc
359 shape_bndrs <- arrShapeVars repr
360 (len, sel, indices) <- arrSelector repr (map Var shape_bndrs)
362 (vbndr, valts) <- vect_scrut_bndr $ mapM (proc_alt sel vty lty) alts'
363 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
365 vexpr <- vectExpr scrut
366 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
367 let [arr_dc] = tyConDataCons arr_tc
369 let (vect_scrut, lift_scrut) = vscrut
370 (vect_bodies, lift_bodies) = unzip vbodies
372 let vect_case = Case vect_scrut (mkWildId (exprType vect_scrut)) vty
373 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
375 lbody <- combinePA vty len sel indices lift_bodies
376 let lift_case = Case lift_scrut (mkWildId (exprType lift_scrut)) lty
377 [(DataAlt arr_dc, shape_bndrs ++ concat lift_bndrss,
380 return . vLet (vNonRec vbndr vexpr)
381 $ (vect_case, lift_case)
383 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
384 | otherwise = vectBndrIn bndr
386 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
388 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
389 cmp DEFAULT DEFAULT = EQ
392 cmp _ _ = panic "vectAlgCase/cmp"
394 proc_alt sel vty lty (DataAlt dc, bndrs, body)
396 vect_dc <- maybeV (lookupDataCon dc)
397 let tag = mkDataConTag vect_dc
398 fvs = freeVarsOf body `delVarSetList` bndrs
399 (vect_bndrs, lift_bndrs, vbody)
400 <- vect_alt_bndrs bndrs
401 $ \len -> packLiftingContext len sel tag fvs vty lty
404 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
405 proc_alt _ _ _ _ = panic "vectAlgCase/proc_alt"
409 void_tc <- builtin voidTyCon
410 let void_ty = mkTyConApp void_tc []
411 arr_ty <- mkPArrayType void_ty
412 bndr <- newLocalVar (fsLit "voids") arr_ty
413 len <- lengthPA void_ty (Var bndr)
415 return ([], [bndr], e)
417 vect_alt_bndrs bndrs p
420 vbndrs <- mapM vectBndr bndrs
421 let (vect_bndrs, lift_bndrs) = unzip vbndrs
424 len <- lengthPA (idType vv) (Var lv)
426 return (vect_bndrs, lift_bndrs, e)
428 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
430 packLiftingContext :: CoreExpr -> CoreExpr -> CoreExpr -> VarSet
431 -> Type -> Type -> VM VExpr -> VM VExpr
432 packLiftingContext len shape tag fvs vty lty p
434 select <- builtin selectPAIntPrimVar
435 let sel_expr = mkApps (Var select) [shape, tag]
436 sel_var <- newLocalVar (fsLit "sel#") (exprType sel_expr)
437 lc_var <- builtin liftingContext
440 bnds <- mapM (packFreeVar (Var lc_var) (Var sel_var))
445 return (vexpr, Let (NonRec sel_var sel_expr)
446 $ Case len lc_var lty
447 [(DEFAULT, [], mkLets (concat bnds) lexpr),
448 (LitAlt (mkMachInt 0), [], empty)])
450 packFreeVar :: CoreExpr -> CoreExpr -> Var -> VM [CoreBind]
451 packFreeVar len sel v
458 expr <- packPA (idType vv) (Var lv) len sel
460 return [(NonRec lv' expr)]
464 upd vv lv' env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv') }