2 -- | Vectorisation of expressions.
8 import Vectorise.Utils.Closure
12 import Vectorise.Monad
13 import Vectorise.Builtins
36 -- | Vectorise a polymorphic expression.
38 :: Bool -- ^ When vectorising the RHS of a binding, whether that
39 -- binding is a loop breaker.
43 vectPolyExpr loop_breaker (_, AnnNote note expr)
44 = do (inline, expr') <- vectPolyExpr loop_breaker expr
45 return (inline, vNote note expr')
47 vectPolyExpr loop_breaker expr
49 arity <- polyArity tvs
50 polyAbstract tvs $ \args ->
52 (inline, mono') <- vectFnExpr False loop_breaker mono
53 return (addInlineArity inline arity,
54 mapVect (mkLams $ tvs ++ args) mono')
56 (tvs, mono) = collectAnnTypeBinders expr
59 -- | Vectorise an expression.
60 vectExpr :: CoreExprWithFVs -> VM VExpr
61 vectExpr (_, AnnType ty)
62 = liftM vType (vectType ty)
64 vectExpr (_, AnnVar v)
67 vectExpr (_, AnnLit lit)
70 vectExpr (_, AnnNote note expr)
71 = liftM (vNote note) (vectExpr expr)
73 vectExpr e@(_, AnnApp _ arg)
75 = vectTyAppExpr fn tys
77 (fn, tys) = collectAnnTypeArgs e
79 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
80 | Just con <- isDataConId_maybe v
83 let vexpr = App (Var v) (Lit lit)
87 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
90 -- TODO: Avoid using closure application for dictionaries.
91 -- vectExpr (_, AnnApp fn arg)
92 -- | if is application of dictionary
93 -- just use regular app instead of closure app.
95 -- for lifted version.
96 -- do liftPD (sub a dNumber)
97 -- lift the result of the selection, not sub and dNumber seprately.
99 vectExpr (_, AnnApp fn arg)
101 arg_ty' <- vectType arg_ty
102 res_ty' <- vectType res_ty
107 mkClosureApp arg_ty' res_ty' fn' arg'
109 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
111 vectExpr (_, AnnCase scrut bndr ty alts)
112 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
114 = vectAlgCase tycon ty_args scrut bndr ty alts
116 scrut_ty = exprType (deAnnotate scrut)
118 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
120 vrhs <- localV . inBind bndr . liftM snd $ vectPolyExpr False rhs
121 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
122 return $ vLet (vNonRec vbndr vrhs) vbody
124 vectExpr (_, AnnLet (AnnRec bs) body)
126 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
128 (zipWithM vect_rhs bndrs rhss)
130 return $ vLet (vRec vbndrs vrhss) vbody
132 (bndrs, rhss) = unzip bs
134 vect_rhs bndr rhs = localV
137 $ vectPolyExpr (isLoopBreaker $ idOccInfo bndr) rhs
139 vectExpr e@(_, AnnLam bndr _)
140 | isId bndr = liftM snd $ vectFnExpr True False e
142 onlyIfV (isEmptyVarSet fvs) (vectScalarLam bs $ deAnnotate body)
143 `orElseV` vectLam True fvs bs body
145 (bs,body) = collectAnnValBinders e
148 vectExpr e = cantVectorise "Can't vectorise expression" (ppr $ deAnnotate e)
151 -- | Vectorise an expression with an outer lambda abstraction.
153 :: Bool -- ^ When the RHS of a binding, whether that binding should be inlined.
154 -> Bool -- ^ Whether the binding is a loop breaker.
155 -> CoreExprWithFVs -- ^ Expression to vectorise. Must have an outer `AnnLam`.
156 -> VM (Inline, VExpr)
158 vectFnExpr inline loop_breaker e@(fvs, AnnLam bndr _)
159 | isId bndr = onlyIfV (isEmptyVarSet fvs)
160 (mark DontInline . vectScalarLam bs $ deAnnotate body)
161 `orElseV` mark inlineMe (vectLam inline loop_breaker fvs bs body)
163 (bs,body) = collectAnnValBinders e
165 vectFnExpr _ _ e = mark DontInline $ vectExpr e
167 mark :: Inline -> VM a -> VM (Inline, a)
168 mark b p = do { x <- p; return (b,x) }
171 -- | Vectorise a function where are the args have scalar type,
172 -- that is Int, Float, Double etc.
174 :: [Var] -- ^ Bound variables of function.
175 -> CoreExpr -- ^ Function body.
178 vectScalarLam args body
179 = do scalars <- globalScalars
180 onlyIfV (all is_scalar_ty arg_tys
181 && is_scalar_ty res_ty
182 && is_scalar (extendVarSetList scalars args) body
183 && uses scalars body)
185 fn_var <- hoistExpr (fsLit "fn") (mkLams args body) DontInline
186 zipf <- zipScalars arg_tys res_ty
187 clo <- scalarClosure arg_tys res_ty (Var fn_var)
188 (zipf `App` Var fn_var)
189 clo_var <- hoistExpr (fsLit "clo") clo DontInline
190 lclo <- liftPD (Var clo_var)
191 return (Var clo_var, lclo)
193 arg_tys = map idType args
194 res_ty = exprType body
197 | Just (tycon, []) <- splitTyConApp_maybe ty
199 || tycon == floatTyCon
200 || tycon == doubleTyCon
204 is_scalar vs (Var v) = v `elemVarSet` vs
205 is_scalar _ e@(Lit _) = is_scalar_ty $ exprType e
206 is_scalar vs (App e1 e2) = is_scalar vs e1 && is_scalar vs e2
207 is_scalar _ _ = False
209 -- A scalar function has to actually compute something. Without the check,
210 -- we would treat (\(x :: Int) -> x) as a scalar function and lift it to
211 -- (map (\x -> x)) which is very bad. Normal lifting transforms it to
212 -- (\n# x -> x) which is what we want.
213 uses funs (Var v) = v `elemVarSet` funs
214 uses funs (App e1 e2) = uses funs e1 || uses funs e2
218 -- | Vectorise a lambda abstraction.
220 :: Bool -- ^ When the RHS of a binding, whether that binding should be inlined.
221 -> Bool -- ^ Whether the binding is a loop breaker.
222 -> VarSet -- ^ The free variables in the body.
223 -> [Var] -- ^ Binding variables.
224 -> CoreExprWithFVs -- ^ Body of abstraction.
227 vectLam inline loop_breaker fvs bs body
228 = do tyvars <- localTyVars
229 (vs, vvs) <- readLEnv $ \env ->
230 unzip [(var, vv) | var <- varSetElems fvs
231 , Just vv <- [lookupVarEnv (local_vars env) var]]
233 arg_tys <- mapM (vectType . idType) bs
234 res_ty <- vectType (exprType $ deAnnotate body)
236 buildClosures tyvars vvs arg_tys res_ty
237 . hoistPolyVExpr tyvars (maybe_inline (length vs + length bs))
239 lc <- builtin liftingContext
240 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs) (vectExpr body)
242 vbody' <- break_loop lc res_ty vbody
243 return $ vLams lc vbndrs vbody'
245 maybe_inline n | inline = Inline n
246 | otherwise = DontInline
248 break_loop lc ty (ve, le)
252 lty <- mkPDataType ty
253 return (ve, mkWildCase (Var lc) intPrimTy lty
255 (LitAlt (mkMachInt 0), [], empty)])
257 | otherwise = return (ve, le)
260 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
261 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
262 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
263 (ppr $ deAnnotate e `mkTyApps` tys)
266 -- | Vectorise an algebraic case expression.
269 -- case e :: t of v { ... }
273 -- V: let v' = e in case v' of _ { ... }
274 -- L: let v' = e in case v' `cast` ... of _ { ... }
276 -- When lifting, we have to do it this way because v must have the type
277 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
278 -- have to handle the case where v is a wild var correctly.
281 -- FIXME: this is too lazy
282 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
283 -> [(AltCon, [Var], CoreExprWithFVs)]
285 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
287 vscrut <- vectExpr scrut
288 (vty, lty) <- vectAndLiftType ty
289 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
290 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
292 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
294 vscrut <- vectExpr scrut
295 (vty, lty) <- vectAndLiftType ty
296 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
297 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
299 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
301 (vty, lty) <- vectAndLiftType ty
302 vexpr <- vectExpr scrut
303 (vbndr, (vbndrs, (vect_body, lift_body)))
307 let (vect_bndrs, lift_bndrs) = unzip vbndrs
308 (vscrut, lscrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
309 vect_dc <- maybeV (lookupDataCon dc)
310 let [pdata_dc] = tyConDataCons pdata_tc
312 let vcase = mk_wild_case vscrut vty vect_dc vect_bndrs vect_body
313 lcase = mk_wild_case lscrut lty pdata_dc lift_bndrs lift_body
315 return $ vLet (vNonRec vbndr vexpr) (vcase, lcase)
317 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
318 | otherwise = vectBndrIn bndr
320 mk_wild_case expr ty dc bndrs body
321 = mkWildCase expr (exprType expr) ty [(DataAlt dc, bndrs, body)]
323 vectAlgCase tycon _ty_args scrut bndr ty alts
325 vect_tc <- maybeV (lookupTyCon tycon)
326 (vty, lty) <- vectAndLiftType ty
328 let arity = length (tyConDataCons vect_tc)
329 sel_ty <- builtin (selTy arity)
330 sel_bndr <- newLocalVar (fsLit "sel") sel_ty
331 let sel = Var sel_bndr
333 (vbndr, valts) <- vect_scrut_bndr
334 $ mapM (proc_alt arity sel vty lty) alts'
335 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
337 vexpr <- vectExpr scrut
338 (vect_scrut, lift_scrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
339 let [pdata_dc] = tyConDataCons pdata_tc
341 let (vect_bodies, lift_bodies) = unzip vbodies
343 vdummy <- newDummyVar (exprType vect_scrut)
344 ldummy <- newDummyVar (exprType lift_scrut)
345 let vect_case = Case vect_scrut vdummy vty
346 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
348 lc <- builtin liftingContext
349 lbody <- combinePD vty (Var lc) sel lift_bodies
350 let lift_case = Case lift_scrut ldummy lty
351 [(DataAlt pdata_dc, sel_bndr : concat lift_bndrss,
354 return . vLet (vNonRec vbndr vexpr)
355 $ (vect_case, lift_case)
357 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
358 | otherwise = vectBndrIn bndr
360 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
362 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
363 cmp DEFAULT DEFAULT = EQ
366 cmp _ _ = panic "vectAlgCase/cmp"
368 proc_alt arity sel _ lty (DataAlt dc, bndrs, body)
370 vect_dc <- maybeV (lookupDataCon dc)
371 let ntag = dataConTagZ vect_dc
372 tag = mkDataConTag vect_dc
373 fvs = freeVarsOf body `delVarSetList` bndrs
375 sel_tags <- liftM (`App` sel) (builtin (selTags arity))
376 lc <- builtin liftingContext
377 elems <- builtin (selElements arity ntag)
383 binds <- mapM (pack_var (Var lc) sel_tags tag)
386 (ve, le) <- vectExpr body
387 return (ve, Case (elems `App` sel) lc lty
388 [(DEFAULT, [], (mkLets (concat binds) le))])
389 -- empty <- emptyPD vty
390 -- return (ve, Case (elems `App` sel) lc lty
391 -- [(DEFAULT, [], Let (NonRec flags_var flags_expr)
392 -- $ mkLets (concat binds) le),
393 -- (LitAlt (mkMachInt 0), [], empty)])
394 let (vect_bndrs, lift_bndrs) = unzip vbndrs
395 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
397 proc_alt _ _ _ _ _ = panic "vectAlgCase/proc_alt"
399 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
401 pack_var len tags t v
408 expr <- packByTagPD (idType vv) (Var lv) len tags t
409 updLEnv (\env -> env { local_vars = extendVarEnv
410 (local_vars env) v (vv, lv') })
411 return [(NonRec lv' expr)]