2 -- | Vectorisation of expressions.
7 import Vectorise.Type.Type
11 import Vectorise.Monad
12 import Vectorise.Builtins
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
203 is_scalar vs (Var v) = v `elemVarSet` vs
204 is_scalar _ e@(Lit _) = is_scalar_ty $ exprType e
205 is_scalar vs (App e1 e2) = is_scalar vs e1 && is_scalar vs e2
206 is_scalar _ _ = False
208 -- A scalar function has to actually compute something. Without the check,
209 -- we would treat (\(x :: Int) -> x) as a scalar function and lift it to
210 -- (map (\x -> x)) which is very bad. Normal lifting transforms it to
211 -- (\n# x -> x) which is what we want.
212 uses funs (Var v) = v `elemVarSet` funs
213 uses funs (App e1 e2) = uses funs e1 || uses funs e2
217 -- | Vectorise a lambda abstraction.
219 :: Bool -- ^ When the RHS of a binding, whether that binding should be inlined.
220 -> Bool -- ^ Whether the binding is a loop breaker.
221 -> VarSet -- ^ The free variables in the body.
222 -> [Var] -- ^ Binding variables.
223 -> CoreExprWithFVs -- ^ Body of abstraction.
226 vectLam inline loop_breaker fvs bs body
227 = do tyvars <- localTyVars
228 (vs, vvs) <- readLEnv $ \env ->
229 unzip [(var, vv) | var <- varSetElems fvs
230 , Just vv <- [lookupVarEnv (local_vars env) var]]
232 arg_tys <- mapM (vectType . idType) bs
233 res_ty <- vectType (exprType $ deAnnotate body)
235 buildClosures tyvars vvs arg_tys res_ty
236 . hoistPolyVExpr tyvars (maybe_inline (length vs + length bs))
238 lc <- builtin liftingContext
239 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs) (vectExpr body)
241 vbody' <- break_loop lc res_ty vbody
242 return $ vLams lc vbndrs vbody'
244 maybe_inline n | inline = Inline n
245 | otherwise = DontInline
247 break_loop lc ty (ve, le)
251 lty <- mkPDataType ty
252 return (ve, mkWildCase (Var lc) intPrimTy lty
254 (LitAlt (mkMachInt 0), [], empty)])
256 | otherwise = return (ve, le)
259 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
260 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
261 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
262 (ppr $ deAnnotate e `mkTyApps` tys)
265 -- | Vectorise an algebraic case expression.
268 -- case e :: t of v { ... }
272 -- V: let v' = e in case v' of _ { ... }
273 -- L: let v' = e in case v' `cast` ... of _ { ... }
275 -- When lifting, we have to do it this way because v must have the type
276 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
277 -- have to handle the case where v is a wild var correctly.
280 -- FIXME: this is too lazy
281 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
282 -> [(AltCon, [Var], CoreExprWithFVs)]
284 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
286 vscrut <- vectExpr scrut
287 (vty, lty) <- vectAndLiftType ty
288 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
289 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
291 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
293 vscrut <- vectExpr scrut
294 (vty, lty) <- vectAndLiftType ty
295 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
296 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
298 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
300 (vty, lty) <- vectAndLiftType ty
301 vexpr <- vectExpr scrut
302 (vbndr, (vbndrs, (vect_body, lift_body)))
306 let (vect_bndrs, lift_bndrs) = unzip vbndrs
307 (vscrut, lscrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
308 vect_dc <- maybeV (lookupDataCon dc)
309 let [pdata_dc] = tyConDataCons pdata_tc
311 let vcase = mk_wild_case vscrut vty vect_dc vect_bndrs vect_body
312 lcase = mk_wild_case lscrut lty pdata_dc lift_bndrs lift_body
314 return $ vLet (vNonRec vbndr vexpr) (vcase, lcase)
316 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
317 | otherwise = vectBndrIn bndr
319 mk_wild_case expr ty dc bndrs body
320 = mkWildCase expr (exprType expr) ty [(DataAlt dc, bndrs, body)]
322 vectAlgCase tycon _ty_args scrut bndr ty alts
324 vect_tc <- maybeV (lookupTyCon tycon)
325 (vty, lty) <- vectAndLiftType ty
327 let arity = length (tyConDataCons vect_tc)
328 sel_ty <- builtin (selTy arity)
329 sel_bndr <- newLocalVar (fsLit "sel") sel_ty
330 let sel = Var sel_bndr
332 (vbndr, valts) <- vect_scrut_bndr
333 $ mapM (proc_alt arity sel vty lty) alts'
334 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
336 vexpr <- vectExpr scrut
337 (vect_scrut, lift_scrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
338 let [pdata_dc] = tyConDataCons pdata_tc
340 let (vect_bodies, lift_bodies) = unzip vbodies
342 vdummy <- newDummyVar (exprType vect_scrut)
343 ldummy <- newDummyVar (exprType lift_scrut)
344 let vect_case = Case vect_scrut vdummy vty
345 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
347 lc <- builtin liftingContext
348 lbody <- combinePD vty (Var lc) sel lift_bodies
349 let lift_case = Case lift_scrut ldummy lty
350 [(DataAlt pdata_dc, sel_bndr : concat lift_bndrss,
353 return . vLet (vNonRec vbndr vexpr)
354 $ (vect_case, lift_case)
356 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
357 | otherwise = vectBndrIn bndr
359 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
361 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
362 cmp DEFAULT DEFAULT = EQ
365 cmp _ _ = panic "vectAlgCase/cmp"
367 proc_alt arity sel _ lty (DataAlt dc, bndrs, body)
369 vect_dc <- maybeV (lookupDataCon dc)
370 let ntag = dataConTagZ vect_dc
371 tag = mkDataConTag vect_dc
372 fvs = freeVarsOf body `delVarSetList` bndrs
374 sel_tags <- liftM (`App` sel) (builtin (selTags arity))
375 lc <- builtin liftingContext
376 elems <- builtin (selElements arity ntag)
382 binds <- mapM (pack_var (Var lc) sel_tags tag)
385 (ve, le) <- vectExpr body
386 return (ve, Case (elems `App` sel) lc lty
387 [(DEFAULT, [], (mkLets (concat binds) le))])
388 -- empty <- emptyPD vty
389 -- return (ve, Case (elems `App` sel) lc lty
390 -- [(DEFAULT, [], Let (NonRec flags_var flags_expr)
391 -- $ mkLets (concat binds) le),
392 -- (LitAlt (mkMachInt 0), [], empty)])
393 let (vect_bndrs, lift_bndrs) = unzip vbndrs
394 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
396 proc_alt _ _ _ _ _ = panic "vectAlgCase/proc_alt"
398 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
400 pack_var len tags t v
407 expr <- packByTagPD (idType vv) (Var lv) len tags t
408 updLEnv (\env -> env { local_vars = extendVarEnv
409 (local_vars env) v (vv, lv') })
410 return [(NonRec lv' expr)]