1 {-# OPTIONS -fno-warn-missing-signatures #-}
3 module Vectorise( vectorise )
11 import Vectorise.Monad
12 import Vectorise.Builtins
14 import HscTypes hiding ( MonadThings(..) )
16 import Module ( PackageId )
19 import CoreUnfold ( mkInlineRule )
20 import MkCore ( mkWildCase )
22 import CoreMonad ( CoreM, getHscEnv )
26 import FamInstEnv ( extendFamInstEnvList )
32 import BasicTypes ( isLoopBreaker )
36 import TysPrim ( intPrimTy )
40 import Util ( zipLazy )
42 import Data.List ( sortBy, unzip4 )
46 dtrace s x = if debug then pprTrace "Vectorise" s x else x
48 -- | Vectorise a single module.
49 -- Takes the package containing the DPH backend we're using. Eg either dph-par or dph-seq.
50 vectorise :: PackageId -> ModGuts -> CoreM ModGuts
51 vectorise backend guts
52 = do hsc_env <- getHscEnv
53 liftIO $ vectoriseIO backend hsc_env guts
56 -- | Vectorise a single monad, given its HscEnv (code gen environment).
57 vectoriseIO :: PackageId -> HscEnv -> ModGuts -> IO ModGuts
58 vectoriseIO backend hsc_env guts
59 = do -- Get information about currently loaded external packages.
62 -- Combine vectorisation info from the current module, and external ones.
63 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
65 -- Run the main VM computation.
66 Just (info', guts') <- initV backend hsc_env guts info (vectModule guts)
67 return (guts' { mg_vect_info = info' })
70 -- | Vectorise a single module, in the VM monad.
71 vectModule :: ModGuts -> VM ModGuts
73 = do -- Vectorise the type environment.
74 -- This may add new TyCons and DataCons.
75 -- TODO: What new binds do we get back here?
76 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
78 -- TODO: What is this?
79 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
80 updGEnv (setFamInstEnv fam_inst_env')
82 -- dicts <- mapM buildPADict pa_insts
83 -- workers <- mapM vectDataConWorkers pa_insts
85 -- Vectorise all the top level bindings.
86 binds' <- mapM vectTopBind (mg_binds guts)
88 return $ guts { mg_types = types'
89 , mg_binds = Rec tc_binds : binds'
90 , mg_fam_inst_env = fam_inst_env'
91 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
95 -- | Try to vectorise a top-level binding.
96 -- If it doesn't vectorise then return it unharmed.
98 -- For example, for the binding
108 -- foo = \x -> vfoo $: x
110 -- v_foo :: Closure void vfoo lfoo
111 -- v_foo = closure vfoo lfoo void
113 -- vfoo :: Void -> Int -> Int
116 -- lfoo :: PData Void -> PData Int -> PData Int
120 -- @vfoo@ is the "vectorised", or scalar, version that does the same as the original
121 -- function foo, but takes an explicit environment.
123 -- @lfoo@ is the "lifted" version that works on arrays.
125 -- @v_foo@ combines both of these into a `Closure` that also contains the
128 -- The original binding @foo@ is rewritten to call the vectorised version
129 -- present in the closure.
131 vectTopBind :: CoreBind -> VM CoreBind
132 vectTopBind b@(NonRec var expr)
134 (inline, expr') <- vectTopRhs var expr
135 var' <- vectTopBinder var inline expr'
137 -- Vectorising the body may create other top-level bindings.
140 -- To get the same functionality as the original body we project
141 -- out its vectorised version from the closure.
142 cexpr <- tryConvert var var' expr
144 return . Rec $ (var, cexpr) : (var', expr') : hs
148 vectTopBind b@(Rec bs)
151 <- fixV $ \ ~(_, inlines, rhss) ->
152 do vars' <- sequence [vectTopBinder var inline rhs
153 | (var, ~(inline, rhs)) <- zipLazy vars (zip inlines rhss)]
155 <- mapAndUnzipM (uncurry vectTopRhs) bs
157 return (vars', inlines', exprs')
160 cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
161 return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
165 (vars, exprs) = unzip bs
168 -- | Make the vectorised version of this top level binder, and add the mapping
169 -- between it and the original to the state. For some binder @foo@ the vectorised
170 -- version is @$v_foo@
172 -- NOTE: vectTopBinder *MUST* be lazy in inline and expr because of how it is
173 -- used inside of fixV in vectTopBind
175 :: Var -- ^ Name of the binding.
176 -> Inline -- ^ Whether it should be inlined, used to annotate it.
177 -> CoreExpr -- ^ RHS of the binding, used to set the `Unfolding` of the returned `Var`.
178 -> VM Var -- ^ Name of the vectorised binding.
180 vectTopBinder var inline expr
182 -- Vectorise the type attached to the var.
183 vty <- vectType (idType var)
185 -- Make the vectorised version of binding's name, and set the unfolding used for inlining.
186 var' <- liftM (`setIdUnfolding` unfolding)
187 $ cloneId mkVectOcc var vty
189 -- Add the mapping between the plain and vectorised name to the state.
190 defGlobalVar var var'
194 unfolding = case inline of
195 Inline arity -> mkInlineRule expr (Just arity)
196 DontInline -> noUnfolding
199 -- | Vectorise the RHS of a top-level binding, in an empty local environment.
201 :: Var -- ^ Name of the binding.
202 -> CoreExpr -- ^ Body of the binding.
203 -> VM (Inline, CoreExpr)
206 = dtrace (vcat [text "vectTopRhs", ppr expr])
208 $ do (inline, vexpr) <- inBind var
209 $ vectPolyExpr (isLoopBreaker $ idOccInfo var)
211 return (inline, vectorised vexpr)
214 -- | Project out the vectorised version of a binding from some closure,
215 -- or return the original body if that doesn't work.
217 :: Var -- ^ Name of the original binding (eg @foo@)
218 -> Var -- ^ Name of vectorised version of binding (eg @$vfoo@)
219 -> CoreExpr -- ^ The original body of the binding.
222 tryConvert var vect_var rhs
223 = fromVect (idType var) (Var vect_var) `orElseV` return rhs
226 -- ----------------------------------------------------------------------------
230 -- | Vectorise a polymorphic expression
232 :: Bool -- ^ When vectorising the RHS of a binding, whether that
233 -- binding is a loop breaker.
235 -> VM (Inline, VExpr)
237 vectPolyExpr loop_breaker (_, AnnNote note expr)
238 = do (inline, expr') <- vectPolyExpr loop_breaker expr
239 return (inline, vNote note expr')
241 vectPolyExpr loop_breaker expr
242 = dtrace (vcat [text "vectPolyExpr", ppr (deAnnotate expr)])
244 arity <- polyArity tvs
245 polyAbstract tvs $ \args ->
247 (inline, mono') <- vectFnExpr False loop_breaker mono
248 return (addInlineArity inline arity,
249 mapVect (mkLams $ tvs ++ args) mono')
251 (tvs, mono) = collectAnnTypeBinders expr
254 -- | Vectorise a core expression.
255 vectExpr :: CoreExprWithFVs -> VM VExpr
256 vectExpr (_, AnnType ty)
257 = liftM vType (vectType ty)
259 vectExpr (_, AnnVar v)
262 vectExpr (_, AnnLit lit)
265 vectExpr (_, AnnNote note expr)
266 = liftM (vNote note) (vectExpr expr)
268 vectExpr e@(_, AnnApp _ arg)
270 = vectTyAppExpr fn tys
272 (fn, tys) = collectAnnTypeArgs e
274 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
275 | Just con <- isDataConId_maybe v
278 let vexpr = App (Var v) (Lit lit)
279 lexpr <- liftPD vexpr
280 return (vexpr, lexpr)
282 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
285 -- TODO: Avoid using closure application for dictionaries.
286 -- vectExpr (_, AnnApp fn arg)
287 -- | if is application of dictionary
288 -- just use regular app instead of closure app.
290 -- for lifted version.
291 -- do liftPD (sub a dNumber)
292 -- lift the result of the selection, not sub and dNumber seprately.
294 vectExpr (_, AnnApp fn arg)
295 = dtrace (text "AnnApp" <+> ppr (deAnnotate fn) <+> ppr (deAnnotate arg))
297 arg_ty' <- vectType arg_ty
298 res_ty' <- vectType res_ty
300 dtrace (text "vectorising fn " <> ppr (deAnnotate fn)) $ return ()
302 dtrace (text "fn' = " <> ppr fn') $ return ()
306 mkClosureApp arg_ty' res_ty' fn' arg'
308 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
310 vectExpr (_, AnnCase scrut bndr ty alts)
311 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
313 = vectAlgCase tycon ty_args scrut bndr ty alts
315 scrut_ty = exprType (deAnnotate scrut)
317 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
319 vrhs <- localV . inBind bndr . liftM snd $ vectPolyExpr False rhs
320 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
321 return $ vLet (vNonRec vbndr vrhs) vbody
323 vectExpr (_, AnnLet (AnnRec bs) body)
325 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
327 (zipWithM vect_rhs bndrs rhss)
329 return $ vLet (vRec vbndrs vrhss) vbody
331 (bndrs, rhss) = unzip bs
333 vect_rhs bndr rhs = localV
336 $ vectPolyExpr (isLoopBreaker $ idOccInfo bndr) rhs
338 vectExpr e@(_, AnnLam bndr _)
339 | isId bndr = liftM snd $ vectFnExpr True False e
341 onlyIfV (isEmptyVarSet fvs) (vectScalarLam bs $ deAnnotate body)
342 `orElseV` vectLam True fvs bs body
344 (bs,body) = collectAnnValBinders e
347 vectExpr e = cantVectorise "Can't vectorise expression" (ppr $ deAnnotate e)
350 -- | Vectorise an expression with an outer lambda abstraction.
352 :: Bool -- ^ When the RHS of a binding, whether that binding should be inlined.
353 -> Bool -- ^ Whether the binding is a loop breaker.
354 -> CoreExprWithFVs -- ^ Expression to vectorise. Must have an outer `AnnLam`.
355 -> VM (Inline, VExpr)
357 vectFnExpr inline loop_breaker e@(fvs, AnnLam bndr _)
358 | isId bndr = onlyIfV (isEmptyVarSet fvs)
359 (mark DontInline . vectScalarLam bs $ deAnnotate body)
360 `orElseV` mark inlineMe (vectLam inline loop_breaker fvs bs body)
362 (bs,body) = collectAnnValBinders e
364 vectFnExpr _ _ e = mark DontInline $ vectExpr e
366 mark :: Inline -> VM a -> VM (Inline, a)
367 mark b p = do { x <- p; return (b,x) }
370 -- | Vectorise a function where are the args have scalar type, that is Int, Float or Double.
372 :: [Var] -- ^ Bound variables of function.
373 -> CoreExpr -- ^ Function body.
375 vectScalarLam args body
376 = dtrace (vcat [text "vectScalarLam ", ppr args, ppr body])
377 $ do scalars <- globalScalars
378 onlyIfV (all is_scalar_ty arg_tys
379 && is_scalar_ty res_ty
380 && is_scalar (extendVarSetList scalars args) body
381 && uses scalars body)
383 fn_var <- hoistExpr (fsLit "fn") (mkLams args body) DontInline
384 zipf <- zipScalars arg_tys res_ty
385 clo <- scalarClosure arg_tys res_ty (Var fn_var)
386 (zipf `App` Var fn_var)
387 clo_var <- hoistExpr (fsLit "clo") clo DontInline
388 lclo <- liftPD (Var clo_var)
389 return (Var clo_var, lclo)
391 arg_tys = map idType args
392 res_ty = exprType body
395 | Just (tycon, []) <- splitTyConApp_maybe ty
397 || tycon == floatTyCon
398 || tycon == doubleTyCon
402 is_scalar vs (Var v) = v `elemVarSet` vs
403 is_scalar _ e@(Lit _) = is_scalar_ty $ exprType e
404 is_scalar vs (App e1 e2) = is_scalar vs e1 && is_scalar vs e2
405 is_scalar _ _ = False
407 -- A scalar function has to actually compute something. Without the check,
408 -- we would treat (\(x :: Int) -> x) as a scalar function and lift it to
409 -- (map (\x -> x)) which is very bad. Normal lifting transforms it to
410 -- (\n# x -> x) which is what we want.
411 uses funs (Var v) = v `elemVarSet` funs
412 uses funs (App e1 e2) = uses funs e1 || uses funs e2
417 :: Bool -- ^ When the RHS of a binding, whether that binding should be inlined.
418 -> Bool -- ^ Whether the binding is a loop breaker.
419 -> VarSet -- ^ The free variables in the body.
424 vectLam inline loop_breaker fvs bs body
425 = dtrace (vcat [ text "vectLam "
426 , text "free vars = " <> ppr fvs
427 , text "binding vars = " <> ppr bs
428 , text "body = " <> ppr (deAnnotate body)])
430 $ do tyvars <- localTyVars
431 (vs, vvs) <- readLEnv $ \env ->
432 unzip [(var, vv) | var <- varSetElems fvs
433 , Just vv <- [lookupVarEnv (local_vars env) var]]
435 arg_tys <- mapM (vectType . idType) bs
437 dtrace (text "arg_tys = " <> ppr arg_tys) $ return ()
439 res_ty <- vectType (exprType $ deAnnotate body)
441 dtrace (text "res_ty = " <> ppr res_ty) $ return ()
443 buildClosures tyvars vvs arg_tys res_ty
444 . hoistPolyVExpr tyvars (maybe_inline (length vs + length bs))
446 lc <- builtin liftingContext
447 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs) (vectExpr body)
449 dtrace (text "vbody = " <> ppr vbody) $ return ()
451 vbody' <- break_loop lc res_ty vbody
452 return $ vLams lc vbndrs vbody'
454 maybe_inline n | inline = Inline n
455 | otherwise = DontInline
457 break_loop lc ty (ve, le)
461 lty <- mkPDataType ty
462 return (ve, mkWildCase (Var lc) intPrimTy lty
464 (LitAlt (mkMachInt 0), [], empty)])
466 | otherwise = return (ve, le)
469 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
470 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
471 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
472 (ppr $ deAnnotate e `mkTyApps` tys)
476 -- case e :: t of v { ... }
480 -- V: let v' = e in case v' of _ { ... }
481 -- L: let v' = e in case v' `cast` ... of _ { ... }
483 -- When lifting, we have to do it this way because v must have the type
484 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
485 -- have to handle the case where v is a wild var correctly.
488 -- FIXME: this is too lazy
489 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
490 -> [(AltCon, [Var], CoreExprWithFVs)]
492 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
494 vscrut <- vectExpr scrut
495 (vty, lty) <- vectAndLiftType ty
496 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
497 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
499 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
501 vscrut <- vectExpr scrut
502 (vty, lty) <- vectAndLiftType ty
503 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
504 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
506 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
508 (vty, lty) <- vectAndLiftType ty
509 vexpr <- vectExpr scrut
510 (vbndr, (vbndrs, (vect_body, lift_body)))
514 let (vect_bndrs, lift_bndrs) = unzip vbndrs
515 (vscrut, lscrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
516 vect_dc <- maybeV (lookupDataCon dc)
517 let [pdata_dc] = tyConDataCons pdata_tc
519 let vcase = mk_wild_case vscrut vty vect_dc vect_bndrs vect_body
520 lcase = mk_wild_case lscrut lty pdata_dc lift_bndrs lift_body
522 return $ vLet (vNonRec vbndr vexpr) (vcase, lcase)
524 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
525 | otherwise = vectBndrIn bndr
527 mk_wild_case expr ty dc bndrs body
528 = mkWildCase expr (exprType expr) ty [(DataAlt dc, bndrs, body)]
530 vectAlgCase tycon _ty_args scrut bndr ty alts
532 vect_tc <- maybeV (lookupTyCon tycon)
533 (vty, lty) <- vectAndLiftType ty
535 let arity = length (tyConDataCons vect_tc)
536 sel_ty <- builtin (selTy arity)
537 sel_bndr <- newLocalVar (fsLit "sel") sel_ty
538 let sel = Var sel_bndr
540 (vbndr, valts) <- vect_scrut_bndr
541 $ mapM (proc_alt arity sel vty lty) alts'
542 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
544 vexpr <- vectExpr scrut
545 (vect_scrut, lift_scrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
546 let [pdata_dc] = tyConDataCons pdata_tc
548 let (vect_bodies, lift_bodies) = unzip vbodies
550 vdummy <- newDummyVar (exprType vect_scrut)
551 ldummy <- newDummyVar (exprType lift_scrut)
552 let vect_case = Case vect_scrut vdummy vty
553 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
555 lc <- builtin liftingContext
556 lbody <- combinePD vty (Var lc) sel lift_bodies
557 let lift_case = Case lift_scrut ldummy lty
558 [(DataAlt pdata_dc, sel_bndr : concat lift_bndrss,
561 return . vLet (vNonRec vbndr vexpr)
562 $ (vect_case, lift_case)
564 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
565 | otherwise = vectBndrIn bndr
567 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
569 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
570 cmp DEFAULT DEFAULT = EQ
573 cmp _ _ = panic "vectAlgCase/cmp"
575 proc_alt arity sel _ lty (DataAlt dc, bndrs, body)
577 vect_dc <- maybeV (lookupDataCon dc)
578 let ntag = dataConTagZ vect_dc
579 tag = mkDataConTag vect_dc
580 fvs = freeVarsOf body `delVarSetList` bndrs
582 sel_tags <- liftM (`App` sel) (builtin (selTags arity))
583 lc <- builtin liftingContext
584 elems <- builtin (selElements arity ntag)
590 binds <- mapM (pack_var (Var lc) sel_tags tag)
593 (ve, le) <- vectExpr body
594 return (ve, Case (elems `App` sel) lc lty
595 [(DEFAULT, [], (mkLets (concat binds) le))])
596 -- empty <- emptyPD vty
597 -- return (ve, Case (elems `App` sel) lc lty
598 -- [(DEFAULT, [], Let (NonRec flags_var flags_expr)
599 -- $ mkLets (concat binds) le),
600 -- (LitAlt (mkMachInt 0), [], empty)])
601 let (vect_bndrs, lift_bndrs) = unzip vbndrs
602 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
604 proc_alt _ _ _ _ _ = panic "vectAlgCase/proc_alt"
606 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
608 pack_var len tags t v
615 expr <- packByTagPD (idType vv) (Var lv) len tags t
616 updLEnv (\env -> env { local_vars = extendVarEnv
617 (local_vars env) v (vv, lv') })
618 return [(NonRec lv' expr)]