2 -- | Vectorisation of expressions.
8 import Vectorise.Utils.Closure
9 import Vectorise.Utils.Hoisting
13 import Vectorise.Monad
14 import Vectorise.Builtins
37 -- | Vectorise a polymorphic expression.
39 :: Bool -- ^ When vectorising the RHS of a binding, whether that
40 -- binding is a loop breaker.
44 vectPolyExpr loop_breaker (_, AnnNote note expr)
45 = do (inline, expr') <- vectPolyExpr loop_breaker expr
46 return (inline, vNote note expr')
48 vectPolyExpr loop_breaker expr
50 arity <- polyArity tvs
51 polyAbstract tvs $ \args ->
53 (inline, mono') <- vectFnExpr False loop_breaker mono
54 return (addInlineArity inline arity,
55 mapVect (mkLams $ tvs ++ args) mono')
57 (tvs, mono) = collectAnnTypeBinders expr
60 -- | Vectorise an expression.
61 vectExpr :: CoreExprWithFVs -> VM VExpr
62 vectExpr (_, AnnType ty)
63 = liftM vType (vectType ty)
65 vectExpr (_, AnnVar v)
68 vectExpr (_, AnnLit lit)
71 vectExpr (_, AnnNote note expr)
72 = liftM (vNote note) (vectExpr expr)
74 vectExpr e@(_, AnnApp _ arg)
76 = vectTyAppExpr fn tys
78 (fn, tys) = collectAnnTypeArgs e
80 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
81 | Just con <- isDataConId_maybe v
84 let vexpr = App (Var v) (Lit lit)
88 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
91 -- TODO: Avoid using closure application for dictionaries.
92 -- vectExpr (_, AnnApp fn arg)
93 -- | if is application of dictionary
94 -- just use regular app instead of closure app.
96 -- for lifted version.
97 -- do liftPD (sub a dNumber)
98 -- lift the result of the selection, not sub and dNumber seprately.
100 vectExpr (_, AnnApp fn arg)
102 arg_ty' <- vectType arg_ty
103 res_ty' <- vectType res_ty
108 mkClosureApp arg_ty' res_ty' fn' arg'
110 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
112 vectExpr (_, AnnCase scrut bndr ty alts)
113 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
115 = vectAlgCase tycon ty_args scrut bndr ty alts
117 scrut_ty = exprType (deAnnotate scrut)
119 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
121 vrhs <- localV . inBind bndr . liftM snd $ vectPolyExpr False rhs
122 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
123 return $ vLet (vNonRec vbndr vrhs) vbody
125 vectExpr (_, AnnLet (AnnRec bs) body)
127 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
129 (zipWithM vect_rhs bndrs rhss)
131 return $ vLet (vRec vbndrs vrhss) vbody
133 (bndrs, rhss) = unzip bs
135 vect_rhs bndr rhs = localV
138 $ vectPolyExpr (isLoopBreaker $ idOccInfo bndr) rhs
140 vectExpr e@(_, AnnLam bndr _)
141 | isId bndr = liftM snd $ vectFnExpr True False e
143 onlyIfV (isEmptyVarSet fvs) (vectScalarLam bs $ deAnnotate body)
144 `orElseV` vectLam True fvs bs body
146 (bs,body) = collectAnnValBinders e
149 vectExpr e = cantVectorise "Can't vectorise expression" (ppr $ deAnnotate e)
152 -- | Vectorise an expression with an outer lambda abstraction.
154 :: Bool -- ^ When the RHS of a binding, whether that binding should be inlined.
155 -> Bool -- ^ Whether the binding is a loop breaker.
156 -> CoreExprWithFVs -- ^ Expression to vectorise. Must have an outer `AnnLam`.
157 -> VM (Inline, VExpr)
159 vectFnExpr inline loop_breaker e@(fvs, AnnLam bndr _)
160 | isId bndr = onlyIfV (isEmptyVarSet fvs)
161 (mark DontInline . vectScalarLam bs $ deAnnotate body)
162 `orElseV` mark inlineMe (vectLam inline loop_breaker fvs bs body)
164 (bs,body) = collectAnnValBinders e
166 vectFnExpr _ _ e = mark DontInline $ vectExpr e
168 mark :: Inline -> VM a -> VM (Inline, a)
169 mark b p = do { x <- p; return (b,x) }
172 -- | Vectorise a function where are the args have scalar type,
173 -- that is Int, Float, Double etc.
175 :: [Var] -- ^ Bound variables of function.
176 -> CoreExpr -- ^ Function body.
179 vectScalarLam args body
180 = do scalars <- globalScalars
181 onlyIfV (all is_scalar_ty arg_tys
182 && is_scalar_ty res_ty
183 && is_scalar (extendVarSetList scalars args) body
184 && uses scalars body)
186 fn_var <- hoistExpr (fsLit "fn") (mkLams args body) DontInline
187 zipf <- zipScalars arg_tys res_ty
188 clo <- scalarClosure arg_tys res_ty (Var fn_var)
189 (zipf `App` Var fn_var)
190 clo_var <- hoistExpr (fsLit "clo") clo DontInline
191 lclo <- liftPD (Var clo_var)
192 return (Var clo_var, lclo)
194 arg_tys = map idType args
195 res_ty = exprType body
198 | Just (tycon, []) <- splitTyConApp_maybe ty
200 || tycon == floatTyCon
201 || tycon == doubleTyCon
205 is_scalar vs (Var v) = v `elemVarSet` vs
206 is_scalar _ e@(Lit _) = is_scalar_ty $ exprType e
207 is_scalar vs (App e1 e2) = is_scalar vs e1 && is_scalar vs e2
208 is_scalar _ _ = False
210 -- A scalar function has to actually compute something. Without the check,
211 -- we would treat (\(x :: Int) -> x) as a scalar function and lift it to
212 -- (map (\x -> x)) which is very bad. Normal lifting transforms it to
213 -- (\n# x -> x) which is what we want.
214 uses funs (Var v) = v `elemVarSet` funs
215 uses funs (App e1 e2) = uses funs e1 || uses funs e2
219 -- | Vectorise a lambda abstraction.
221 :: Bool -- ^ When the RHS of a binding, whether that binding should be inlined.
222 -> Bool -- ^ Whether the binding is a loop breaker.
223 -> VarSet -- ^ The free variables in the body.
224 -> [Var] -- ^ Binding variables.
225 -> CoreExprWithFVs -- ^ Body of abstraction.
228 vectLam inline loop_breaker fvs bs body
229 = do tyvars <- localTyVars
230 (vs, vvs) <- readLEnv $ \env ->
231 unzip [(var, vv) | var <- varSetElems fvs
232 , Just vv <- [lookupVarEnv (local_vars env) var]]
234 arg_tys <- mapM (vectType . idType) bs
235 res_ty <- vectType (exprType $ deAnnotate body)
237 buildClosures tyvars vvs arg_tys res_ty
238 . hoistPolyVExpr tyvars (maybe_inline (length vs + length bs))
240 lc <- builtin liftingContext
241 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs) (vectExpr body)
243 vbody' <- break_loop lc res_ty vbody
244 return $ vLams lc vbndrs vbody'
246 maybe_inline n | inline = Inline n
247 | otherwise = DontInline
249 break_loop lc ty (ve, le)
253 lty <- mkPDataType ty
254 return (ve, mkWildCase (Var lc) intPrimTy lty
256 (LitAlt (mkMachInt 0), [], empty)])
258 | otherwise = return (ve, le)
261 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
262 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
263 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
264 (ppr $ deAnnotate e `mkTyApps` tys)
267 -- | Vectorise an algebraic case expression.
270 -- case e :: t of v { ... }
274 -- V: let v' = e in case v' of _ { ... }
275 -- L: let v' = e in case v' `cast` ... of _ { ... }
277 -- When lifting, we have to do it this way because v must have the type
278 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
279 -- have to handle the case where v is a wild var correctly.
282 -- FIXME: this is too lazy
283 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
284 -> [(AltCon, [Var], CoreExprWithFVs)]
286 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
288 vscrut <- vectExpr scrut
289 (vty, lty) <- vectAndLiftType ty
290 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
291 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
293 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
295 vscrut <- vectExpr scrut
296 (vty, lty) <- vectAndLiftType ty
297 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
298 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
300 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
302 (vty, lty) <- vectAndLiftType ty
303 vexpr <- vectExpr scrut
304 (vbndr, (vbndrs, (vect_body, lift_body)))
308 let (vect_bndrs, lift_bndrs) = unzip vbndrs
309 (vscrut, lscrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
310 vect_dc <- maybeV (lookupDataCon dc)
311 let [pdata_dc] = tyConDataCons pdata_tc
313 let vcase = mk_wild_case vscrut vty vect_dc vect_bndrs vect_body
314 lcase = mk_wild_case lscrut lty pdata_dc lift_bndrs lift_body
316 return $ vLet (vNonRec vbndr vexpr) (vcase, lcase)
318 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
319 | otherwise = vectBndrIn bndr
321 mk_wild_case expr ty dc bndrs body
322 = mkWildCase expr (exprType expr) ty [(DataAlt dc, bndrs, body)]
324 vectAlgCase tycon _ty_args scrut bndr ty alts
326 vect_tc <- maybeV (lookupTyCon tycon)
327 (vty, lty) <- vectAndLiftType ty
329 let arity = length (tyConDataCons vect_tc)
330 sel_ty <- builtin (selTy arity)
331 sel_bndr <- newLocalVar (fsLit "sel") sel_ty
332 let sel = Var sel_bndr
334 (vbndr, valts) <- vect_scrut_bndr
335 $ mapM (proc_alt arity sel vty lty) alts'
336 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
338 vexpr <- vectExpr scrut
339 (vect_scrut, lift_scrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
340 let [pdata_dc] = tyConDataCons pdata_tc
342 let (vect_bodies, lift_bodies) = unzip vbodies
344 vdummy <- newDummyVar (exprType vect_scrut)
345 ldummy <- newDummyVar (exprType lift_scrut)
346 let vect_case = Case vect_scrut vdummy vty
347 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
349 lc <- builtin liftingContext
350 lbody <- combinePD vty (Var lc) sel lift_bodies
351 let lift_case = Case lift_scrut ldummy lty
352 [(DataAlt pdata_dc, sel_bndr : concat lift_bndrss,
355 return . vLet (vNonRec vbndr vexpr)
356 $ (vect_case, lift_case)
358 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
359 | otherwise = vectBndrIn bndr
361 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
363 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
364 cmp DEFAULT DEFAULT = EQ
367 cmp _ _ = panic "vectAlgCase/cmp"
369 proc_alt arity sel _ lty (DataAlt dc, bndrs, body)
371 vect_dc <- maybeV (lookupDataCon dc)
372 let ntag = dataConTagZ vect_dc
373 tag = mkDataConTag vect_dc
374 fvs = freeVarsOf body `delVarSetList` bndrs
376 sel_tags <- liftM (`App` sel) (builtin (selTags arity))
377 lc <- builtin liftingContext
378 elems <- builtin (selElements arity ntag)
384 binds <- mapM (pack_var (Var lc) sel_tags tag)
387 (ve, le) <- vectExpr body
388 return (ve, Case (elems `App` sel) lc lty
389 [(DEFAULT, [], (mkLets (concat binds) le))])
390 -- empty <- emptyPD vty
391 -- return (ve, Case (elems `App` sel) lc lty
392 -- [(DEFAULT, [], Let (NonRec flags_var flags_expr)
393 -- $ mkLets (concat binds) le),
394 -- (LitAlt (mkMachInt 0), [], empty)])
395 let (vect_bndrs, lift_bndrs) = unzip vbndrs
396 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
398 proc_alt _ _ _ _ _ = panic "vectAlgCase/proc_alt"
400 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
402 pack_var len tags t v
409 expr <- packByTagPD (idType vv) (Var lv) len tags t
410 updLEnv (\env -> env { local_vars = extendVarEnv
411 (local_vars env) v (vv, lv') })
412 return [(NonRec lv' expr)]