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.
41 -> VM (Inline, Bool, VExpr)
43 vectPolyExpr loop_breaker recFns (_, AnnNote note expr)
44 = do (inline, isScalarFn, expr') <- vectPolyExpr loop_breaker recFns expr
45 return (inline, isScalarFn, vNote note expr')
47 vectPolyExpr loop_breaker recFns expr
49 arity <- polyArity tvs
50 polyAbstract tvs $ \args ->
52 (inline, isScalarFn, mono') <- vectFnExpr False loop_breaker recFns mono
53 return (addInlineArity inline arity, isScalarFn,
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
115 | otherwise = cantVectorise "Can't vectorise expression" (ppr scrut_ty)
117 scrut_ty = exprType (deAnnotate scrut)
119 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
121 vrhs <- localV . inBind bndr . liftM (\(_,_,z)->z) $ 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
137 . liftM (\(_,_,z)->z)
138 $ vectPolyExpr (isLoopBreaker $ idOccInfo bndr) [] rhs
140 vectExpr e@(_, AnnLam bndr _)
141 | isId bndr = liftM (\(_,_,z) ->z) $ 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 (vectExpr)" (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.
157 -> CoreExprWithFVs -- ^ Expression to vectorise. Must have an outer `AnnLam`.
158 -> VM (Inline, Bool, VExpr)
160 vectFnExpr inline loop_breaker recFns e@(fvs, AnnLam bndr _)
161 | isId bndr = onlyIfV True -- (isEmptyVarSet fvs) -- we check for free variables later. TODO: clean up
162 (mark DontInline True . vectScalarLam bs recFns $ deAnnotate body)
163 `orElseV` mark inlineMe False (vectLam inline loop_breaker fvs bs body)
165 (bs,body) = collectAnnValBinders e
167 vectFnExpr _ _ _ e = mark DontInline False $ vectExpr e
169 mark :: Inline -> Bool -> VM a -> VM (Inline, Bool, a)
170 mark b isScalarFn p = do { x <- p; return (b, isScalarFn, x) }
173 -- | Vectorise a function where are the args have scalar type,
174 -- that is Int, Float, Double etc.
176 :: [Var] -- ^ Bound variables of function
178 -> CoreExpr -- ^ Function body.
181 vectScalarLam args recFns body
182 = do scalars' <- globalScalars
183 let scalars = unionVarSet (mkVarSet recFns) scalars'
184 onlyIfV (all is_prim_ty arg_tys
186 && is_scalar (extendVarSetList scalars args) body
187 && uses scalars body)
189 fn_var <- hoistExpr (fsLit "fn") (mkLams args body) DontInline
190 zipf <- zipScalars arg_tys res_ty
191 clo <- scalarClosure arg_tys res_ty (Var fn_var)
192 (zipf `App` Var fn_var)
193 clo_var <- hoistExpr (fsLit "clo") clo DontInline
194 lclo <- liftPD (Var clo_var)
195 return (Var clo_var, lclo)
197 arg_tys = map idType args
198 res_ty = exprType body
201 | Just (tycon, []) <- splitTyConApp_maybe ty
203 || tycon == floatTyCon
204 || tycon == doubleTyCon
208 cantbe_parr_expr expr = not $ maybe_parr_ty $ exprType expr
210 maybe_parr_ty ty = maybe_parr_ty' [] ty
212 maybe_parr_ty' _ ty | Nothing <- splitTyConApp_maybe ty = False -- TODO: is this really what we want to do with polym. types?
213 maybe_parr_ty' alreadySeen ty
214 | isPArrTyCon tycon = True
215 | isPrimTyCon tycon = False
216 | isAbstractTyCon tycon = True
217 | isFunTyCon tycon || isProductTyCon tycon || isTupleTyCon tycon = any (maybe_parr_ty' alreadySeen) args
218 | isDataTyCon tycon = any (maybe_parr_ty' alreadySeen) args ||
219 hasParrDataCon alreadySeen tycon
222 Just (tycon, args) = splitTyConApp_maybe ty
225 hasParrDataCon alreadySeen tycon
226 | tycon `elem` alreadySeen = False
228 any (maybe_parr_ty' $ tycon : alreadySeen) $ concat $ map dataConOrigArgTys $ tyConDataCons tycon
230 -- checks to make sure expression can't contain a non-scalar subexpression. Might err on the side of caution whenever
231 -- an external (non data constructor) variable is used, or anonymous data constructor
232 is_scalar vs e@(Var v)
233 | Just _ <- isDataConId_maybe v = cantbe_parr_expr e
234 | otherwise = cantbe_parr_expr e && (v `elemVarSet` vs)
235 is_scalar _ e@(Lit _) = cantbe_parr_expr e
237 is_scalar vs e@(App e1 e2) = cantbe_parr_expr e &&
238 is_scalar vs e1 && is_scalar vs e2
239 is_scalar vs e@(Let (NonRec b letExpr) body)
240 = cantbe_parr_expr e &&
241 is_scalar vs letExpr && is_scalar (extendVarSet vs b) body
242 is_scalar vs e@(Let (Rec bnds) body)
243 = let vs' = extendVarSetList vs (map fst bnds)
244 in cantbe_parr_expr e &&
245 all (is_scalar vs') (map snd bnds) && is_scalar vs' body
246 is_scalar vs e@(Case eC eId ty alts)
247 = let vs' = extendVarSet vs eId
248 in cantbe_parr_expr e &&
251 (all (is_scalar_alt vs') alts)
253 is_scalar _ _ = False
255 is_scalar_alt vs (_, bs, e)
256 = is_scalar (extendVarSetList vs bs) e
258 -- A scalar function has to actually compute something. Without the check,
259 -- we would treat (\(x :: Int) -> x) as a scalar function and lift it to
260 -- (map (\x -> x)) which is very bad. Normal lifting transforms it to
261 -- (\n# x -> x) which is what we want.
262 uses funs (Var v) = v `elemVarSet` funs
263 uses funs (App e1 e2) = uses funs e1 || uses funs e2
264 uses funs (Let (NonRec _b letExpr) body)
265 = uses funs letExpr || uses funs body
266 uses funs (Case e _eId _ty alts)
267 = uses funs e || any (uses_alt funs) alts
270 uses_alt funs (_, _bs, e)
273 -- | Vectorise a lambda abstraction.
275 :: Bool -- ^ When the RHS of a binding, whether that binding should be inlined.
276 -> Bool -- ^ Whether the binding is a loop breaker.
277 -> VarSet -- ^ The free variables in the body.
278 -> [Var] -- ^ Binding variables.
279 -> CoreExprWithFVs -- ^ Body of abstraction.
282 vectLam inline loop_breaker fvs bs body
283 = do tyvars <- localTyVars
284 (vs, vvs) <- readLEnv $ \env ->
285 unzip [(var, vv) | var <- varSetElems fvs
286 , Just vv <- [lookupVarEnv (local_vars env) var]]
288 arg_tys <- mapM (vectType . idType) bs
289 res_ty <- vectType (exprType $ deAnnotate body)
291 buildClosures tyvars vvs arg_tys res_ty
292 . hoistPolyVExpr tyvars (maybe_inline (length vs + length bs))
294 lc <- builtin liftingContext
295 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs) (vectExpr body)
297 vbody' <- break_loop lc res_ty vbody
298 return $ vLams lc vbndrs vbody'
300 maybe_inline n | inline = Inline n
301 | otherwise = DontInline
303 break_loop lc ty (ve, le)
307 lty <- mkPDataType ty
308 return (ve, mkWildCase (Var lc) intPrimTy lty
310 (LitAlt (mkMachInt 0), [], empty)])
312 | otherwise = return (ve, le)
315 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
316 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
317 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression (vectTyExpr)"
318 (ppr $ deAnnotate e `mkTyApps` tys)
321 -- | Vectorise an algebraic case expression.
324 -- case e :: t of v { ... }
328 -- V: let v' = e in case v' of _ { ... }
329 -- L: let v' = e in case v' `cast` ... of _ { ... }
331 -- When lifting, we have to do it this way because v must have the type
332 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
333 -- have to handle the case where v is a wild var correctly.
336 -- FIXME: this is too lazy
337 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
338 -> [(AltCon, [Var], CoreExprWithFVs)]
340 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
342 vscrut <- vectExpr scrut
343 (vty, lty) <- vectAndLiftType ty
344 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
345 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
347 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
349 vscrut <- vectExpr scrut
350 (vty, lty) <- vectAndLiftType ty
351 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
352 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
354 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
356 (vty, lty) <- vectAndLiftType ty
357 vexpr <- vectExpr scrut
358 (vbndr, (vbndrs, (vect_body, lift_body)))
362 let (vect_bndrs, lift_bndrs) = unzip vbndrs
363 (vscrut, lscrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
364 vect_dc <- maybeV (lookupDataCon dc)
365 let [pdata_dc] = tyConDataCons pdata_tc
367 let vcase = mk_wild_case vscrut vty vect_dc vect_bndrs vect_body
368 lcase = mk_wild_case lscrut lty pdata_dc lift_bndrs lift_body
370 return $ vLet (vNonRec vbndr vexpr) (vcase, lcase)
372 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
373 | otherwise = vectBndrIn bndr
375 mk_wild_case expr ty dc bndrs body
376 = mkWildCase expr (exprType expr) ty [(DataAlt dc, bndrs, body)]
378 vectAlgCase tycon _ty_args scrut bndr ty alts
380 vect_tc <- maybeV (lookupTyCon tycon)
381 (vty, lty) <- vectAndLiftType ty
383 let arity = length (tyConDataCons vect_tc)
384 sel_ty <- builtin (selTy arity)
385 sel_bndr <- newLocalVar (fsLit "sel") sel_ty
386 let sel = Var sel_bndr
388 (vbndr, valts) <- vect_scrut_bndr
389 $ mapM (proc_alt arity sel vty lty) alts'
390 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
392 vexpr <- vectExpr scrut
393 (vect_scrut, lift_scrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
394 let [pdata_dc] = tyConDataCons pdata_tc
396 let (vect_bodies, lift_bodies) = unzip vbodies
398 vdummy <- newDummyVar (exprType vect_scrut)
399 ldummy <- newDummyVar (exprType lift_scrut)
400 let vect_case = Case vect_scrut vdummy vty
401 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
403 lc <- builtin liftingContext
404 lbody <- combinePD vty (Var lc) sel lift_bodies
405 let lift_case = Case lift_scrut ldummy lty
406 [(DataAlt pdata_dc, sel_bndr : concat lift_bndrss,
409 return . vLet (vNonRec vbndr vexpr)
410 $ (vect_case, lift_case)
412 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
413 | otherwise = vectBndrIn bndr
415 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
417 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
418 cmp DEFAULT DEFAULT = EQ
421 cmp _ _ = panic "vectAlgCase/cmp"
423 proc_alt arity sel _ lty (DataAlt dc, bndrs, body)
425 vect_dc <- maybeV (lookupDataCon dc)
426 let ntag = dataConTagZ vect_dc
427 tag = mkDataConTag vect_dc
428 fvs = freeVarsOf body `delVarSetList` bndrs
430 sel_tags <- liftM (`App` sel) (builtin (selTags arity))
431 lc <- builtin liftingContext
432 elems <- builtin (selElements arity ntag)
438 binds <- mapM (pack_var (Var lc) sel_tags tag)
441 (ve, le) <- vectExpr body
442 return (ve, Case (elems `App` sel) lc lty
443 [(DEFAULT, [], (mkLets (concat binds) le))])
444 -- empty <- emptyPD vty
445 -- return (ve, Case (elems `App` sel) lc lty
446 -- [(DEFAULT, [], Let (NonRec flags_var flags_expr)
447 -- $ mkLets (concat binds) le),
448 -- (LitAlt (mkMachInt 0), [], empty)])
449 let (vect_bndrs, lift_bndrs) = unzip vbndrs
450 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
452 proc_alt _ _ _ _ _ = panic "vectAlgCase/proc_alt"
454 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
456 pack_var len tags t v
463 expr <- packByTagPD (idType vv) (Var lv) len tags t
464 updLEnv (\env -> env { local_vars = extendVarEnv
465 (local_vars env) v (vv, lv') })
466 return [(NonRec lv' expr)]