2 -- | Vectorisation of expressions.
7 import Vectorise.Type.Type
11 import Vectorise.Monad
12 import Vectorise.Builtins
25 import BasicTypes( isLoopBreaker )
35 -- | Vectorise a polymorphic expression.
37 :: Bool -- ^ When vectorising the RHS of a binding, whether that
38 -- binding is a loop breaker.
42 vectPolyExpr loop_breaker (_, AnnNote note expr)
43 = do (inline, expr') <- vectPolyExpr loop_breaker expr
44 return (inline, vNote note expr')
46 vectPolyExpr loop_breaker expr
48 arity <- polyArity tvs
49 polyAbstract tvs $ \args ->
51 (inline, mono') <- vectFnExpr False loop_breaker mono
52 return (addInlineArity inline arity,
53 mapVect (mkLams $ tvs ++ args) mono')
55 (tvs, mono) = collectAnnTypeBinders expr
58 -- | Vectorise an expression.
59 vectExpr :: CoreExprWithFVs -> VM VExpr
60 vectExpr (_, AnnType ty)
61 = liftM vType (vectType ty)
63 vectExpr (_, AnnVar v)
66 vectExpr (_, AnnLit lit)
69 vectExpr (_, AnnNote note expr)
70 = liftM (vNote note) (vectExpr expr)
72 vectExpr e@(_, AnnApp _ arg)
74 = vectTyAppExpr fn tys
76 (fn, tys) = collectAnnTypeArgs e
78 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
79 | Just con <- isDataConId_maybe v
82 let vexpr = App (Var v) (Lit lit)
86 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
89 -- TODO: Avoid using closure application for dictionaries.
90 -- vectExpr (_, AnnApp fn arg)
91 -- | if is application of dictionary
92 -- just use regular app instead of closure app.
94 -- for lifted version.
95 -- do liftPD (sub a dNumber)
96 -- lift the result of the selection, not sub and dNumber seprately.
98 vectExpr (_, AnnApp fn arg)
100 arg_ty' <- vectType arg_ty
101 res_ty' <- vectType res_ty
106 mkClosureApp arg_ty' res_ty' fn' arg'
108 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
110 vectExpr (_, AnnCase scrut bndr ty alts)
111 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
113 = vectAlgCase tycon ty_args scrut bndr ty alts
115 scrut_ty = exprType (deAnnotate scrut)
117 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
119 vrhs <- localV . inBind bndr . liftM snd $ vectPolyExpr False rhs
120 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
121 return $ vLet (vNonRec vbndr vrhs) vbody
123 vectExpr (_, AnnLet (AnnRec bs) body)
125 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
127 (zipWithM vect_rhs bndrs rhss)
129 return $ vLet (vRec vbndrs vrhss) vbody
131 (bndrs, rhss) = unzip bs
133 vect_rhs bndr rhs = localV
136 $ vectPolyExpr (isLoopBreaker $ idOccInfo bndr) rhs
138 vectExpr e@(_, AnnLam bndr _)
139 | isId bndr = liftM snd $ vectFnExpr True False e
141 onlyIfV (isEmptyVarSet fvs) (vectScalarLam bs $ deAnnotate body)
142 `orElseV` vectLam True fvs bs body
144 (bs,body) = collectAnnValBinders e
147 vectExpr e = cantVectorise "Can't vectorise expression" (ppr $ deAnnotate e)
150 -- | Vectorise an expression with an outer lambda abstraction.
152 :: Bool -- ^ When the RHS of a binding, whether that binding should be inlined.
153 -> Bool -- ^ Whether the binding is a loop breaker.
154 -> CoreExprWithFVs -- ^ Expression to vectorise. Must have an outer `AnnLam`.
155 -> VM (Inline, VExpr)
157 vectFnExpr inline loop_breaker e@(fvs, AnnLam bndr _)
158 | isId bndr = onlyIfV (isEmptyVarSet fvs)
159 (mark DontInline . vectScalarLam bs $ deAnnotate body)
160 `orElseV` mark inlineMe (vectLam inline loop_breaker fvs bs body)
162 (bs,body) = collectAnnValBinders e
164 vectFnExpr _ _ e = mark DontInline $ vectExpr e
166 mark :: Inline -> VM a -> VM (Inline, a)
167 mark b p = do { x <- p; return (b,x) }
170 -- | Vectorise a function where are the args have scalar type,
171 -- that is Int, Float, Double etc.
173 :: [Var] -- ^ Bound variables of function.
174 -> CoreExpr -- ^ Function body.
177 vectScalarLam args body
178 = do scalars <- globalScalars
179 onlyIfV (all is_scalar_ty arg_tys
180 && is_scalar_ty res_ty
181 && is_scalar (extendVarSetList scalars args) body
182 && uses scalars body)
184 fn_var <- hoistExpr (fsLit "fn") (mkLams args body) DontInline
185 zipf <- zipScalars arg_tys res_ty
186 clo <- scalarClosure arg_tys res_ty (Var fn_var)
187 (zipf `App` Var fn_var)
188 clo_var <- hoistExpr (fsLit "clo") clo DontInline
189 lclo <- liftPD (Var clo_var)
190 return (Var clo_var, lclo)
192 arg_tys = map idType args
193 res_ty = exprType body
196 | Just (tycon, []) <- splitTyConApp_maybe ty
198 || tycon == floatTyCon
199 || tycon == doubleTyCon
200 || tycon == boolTyCon
204 is_scalar vs (Var v) = v `elemVarSet` vs
205 is_scalar _ e@(Lit _) = is_scalar_ty $ exprType e
207 is_scalar _ (App (Var v) (Lit _))
208 | Just con <- isDataConId_maybe v = con `elem` [intDataCon, floatDataCon, doubleDataCon]
210 is_scalar vs (App e1 e2) = is_scalar vs e1 && is_scalar vs e2
211 is_scalar vs (Let (NonRec b letExpr) body)
212 = is_scalar vs letExpr && is_scalar (extendVarSet vs b) body
213 is_scalar vs (Let (Rec bnds) body)
214 = let vs' = extendVarSetList vs (map fst bnds)
215 in all (is_scalar vs') (map snd bnds) && is_scalar vs' body
216 is_scalar vs (Case e eId ty alts)
217 = let vs' = extendVarSet vs eId
218 in is_scalar_ty ty &&
220 (all (is_scalar_alt vs') alts)
222 is_scalar _ _ = False
224 is_scalar_alt vs (_, bs, e)
225 = is_scalar (extendVarSetList vs bs) e
227 -- A scalar function has to actually compute something. Without the check,
228 -- we would treat (\(x :: Int) -> x) as a scalar function and lift it to
229 -- (map (\x -> x)) which is very bad. Normal lifting transforms it to
230 -- (\n# x -> x) which is what we want.
231 uses funs (Var v) = v `elemVarSet` funs
232 uses funs (App e1 e2) = uses funs e1 || uses funs e2
233 uses funs (Let (NonRec _b letExpr) body)
234 = uses funs letExpr || uses funs body
235 uses funs (Case e _eId _ty alts)
236 = uses funs e || any (uses_alt funs) alts
239 uses_alt funs (_, _bs, e)
242 -- | Vectorise a lambda abstraction.
244 :: Bool -- ^ When the RHS of a binding, whether that binding should be inlined.
245 -> Bool -- ^ Whether the binding is a loop breaker.
246 -> VarSet -- ^ The free variables in the body.
247 -> [Var] -- ^ Binding variables.
248 -> CoreExprWithFVs -- ^ Body of abstraction.
251 vectLam inline loop_breaker fvs bs body
252 = do tyvars <- localTyVars
253 (vs, vvs) <- readLEnv $ \env ->
254 unzip [(var, vv) | var <- varSetElems fvs
255 , Just vv <- [lookupVarEnv (local_vars env) var]]
257 arg_tys <- mapM (vectType . idType) bs
258 res_ty <- vectType (exprType $ deAnnotate body)
260 buildClosures tyvars vvs arg_tys res_ty
261 . hoistPolyVExpr tyvars (maybe_inline (length vs + length bs))
263 lc <- builtin liftingContext
264 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs) (vectExpr body)
266 vbody' <- break_loop lc res_ty vbody
267 return $ vLams lc vbndrs vbody'
269 maybe_inline n | inline = Inline n
270 | otherwise = DontInline
272 break_loop lc ty (ve, le)
276 lty <- mkPDataType ty
277 return (ve, mkWildCase (Var lc) intPrimTy lty
279 (LitAlt (mkMachInt 0), [], empty)])
281 | otherwise = return (ve, le)
284 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
285 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
286 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
287 (ppr $ deAnnotate e `mkTyApps` tys)
290 -- | Vectorise an algebraic case expression.
293 -- case e :: t of v { ... }
297 -- V: let v' = e in case v' of _ { ... }
298 -- L: let v' = e in case v' `cast` ... of _ { ... }
300 -- When lifting, we have to do it this way because v must have the type
301 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
302 -- have to handle the case where v is a wild var correctly.
305 -- FIXME: this is too lazy
306 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
307 -> [(AltCon, [Var], CoreExprWithFVs)]
309 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
311 vscrut <- vectExpr scrut
312 (vty, lty) <- vectAndLiftType ty
313 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
314 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
316 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
318 vscrut <- vectExpr scrut
319 (vty, lty) <- vectAndLiftType ty
320 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
321 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
323 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
325 (vty, lty) <- vectAndLiftType ty
326 vexpr <- vectExpr scrut
327 (vbndr, (vbndrs, (vect_body, lift_body)))
331 let (vect_bndrs, lift_bndrs) = unzip vbndrs
332 (vscrut, lscrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
333 vect_dc <- maybeV (lookupDataCon dc)
334 let [pdata_dc] = tyConDataCons pdata_tc
336 let vcase = mk_wild_case vscrut vty vect_dc vect_bndrs vect_body
337 lcase = mk_wild_case lscrut lty pdata_dc lift_bndrs lift_body
339 return $ vLet (vNonRec vbndr vexpr) (vcase, lcase)
341 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
342 | otherwise = vectBndrIn bndr
344 mk_wild_case expr ty dc bndrs body
345 = mkWildCase expr (exprType expr) ty [(DataAlt dc, bndrs, body)]
347 vectAlgCase tycon _ty_args scrut bndr ty alts
349 vect_tc <- maybeV (lookupTyCon tycon)
350 (vty, lty) <- vectAndLiftType ty
352 let arity = length (tyConDataCons vect_tc)
353 sel_ty <- builtin (selTy arity)
354 sel_bndr <- newLocalVar (fsLit "sel") sel_ty
355 let sel = Var sel_bndr
357 (vbndr, valts) <- vect_scrut_bndr
358 $ mapM (proc_alt arity sel vty lty) alts'
359 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
361 vexpr <- vectExpr scrut
362 (vect_scrut, lift_scrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
363 let [pdata_dc] = tyConDataCons pdata_tc
365 let (vect_bodies, lift_bodies) = unzip vbodies
367 vdummy <- newDummyVar (exprType vect_scrut)
368 ldummy <- newDummyVar (exprType lift_scrut)
369 let vect_case = Case vect_scrut vdummy vty
370 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
372 lc <- builtin liftingContext
373 lbody <- combinePD vty (Var lc) sel lift_bodies
374 let lift_case = Case lift_scrut ldummy lty
375 [(DataAlt pdata_dc, sel_bndr : concat lift_bndrss,
378 return . vLet (vNonRec vbndr vexpr)
379 $ (vect_case, lift_case)
381 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
382 | otherwise = vectBndrIn bndr
384 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
386 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
387 cmp DEFAULT DEFAULT = EQ
390 cmp _ _ = panic "vectAlgCase/cmp"
392 proc_alt arity sel _ lty (DataAlt dc, bndrs, body)
394 vect_dc <- maybeV (lookupDataCon dc)
395 let ntag = dataConTagZ vect_dc
396 tag = mkDataConTag vect_dc
397 fvs = freeVarsOf body `delVarSetList` bndrs
399 sel_tags <- liftM (`App` sel) (builtin (selTags arity))
400 lc <- builtin liftingContext
401 elems <- builtin (selElements arity ntag)
407 binds <- mapM (pack_var (Var lc) sel_tags tag)
410 (ve, le) <- vectExpr body
411 return (ve, Case (elems `App` sel) lc lty
412 [(DEFAULT, [], (mkLets (concat binds) le))])
413 -- empty <- emptyPD vty
414 -- return (ve, Case (elems `App` sel) lc lty
415 -- [(DEFAULT, [], Let (NonRec flags_var flags_expr)
416 -- $ mkLets (concat binds) le),
417 -- (LitAlt (mkMachInt 0), [], empty)])
418 let (vect_bndrs, lift_bndrs) = unzip vbndrs
419 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
421 proc_alt _ _ _ _ _ = panic "vectAlgCase/proc_alt"
423 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
425 pack_var len tags t v
432 expr <- packByTagPD (idType vv) (Var lv) len tags t
433 updLEnv (\env -> env { local_vars = extendVarEnv
434 (local_vars env) v (vv, lv') })
435 return [(NonRec lv' expr)]