2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
4 dataConTagZ, mkDataConTag, mkDataConTagLit,
8 mkBuiltinCo, voidType, mkWrapType,
9 mkPADictType, mkPArrayType, mkPDataType, mkPReprType, mkPArray,
11 pdataReprTyCon, pdataReprDataCon, mkVScrut,
12 prDictOfType, prDFunOfTyCon,
13 paDictArgType, paDictOfType, paDFunType,
14 paMethod, wrapPR, replicatePD, emptyPD, packByTagPD,
17 zipScalars, scalarClosure,
18 polyAbstract, polyApply, polyVApply, polyArity,
19 Inline(..), addInlineArity, inlineMe,
20 hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
21 buildClosure, buildClosures,
28 import MkCore ( mkCoreTup, mkWildCase )
31 import CoreUnfold ( mkInlineRule )
38 import MkId ( unwrapFamInstScrut )
39 import Id ( setIdUnfolding )
41 import BasicTypes ( Boxity(..), Arity )
42 import Literal ( Literal, mkMachInt )
49 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
50 collectAnnTypeArgs expr = go expr []
52 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
55 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
56 collectAnnTypeBinders expr = go [] expr
58 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
59 go bs e = (reverse bs, e)
61 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
62 collectAnnValBinders expr = go [] expr
64 go bs (_, AnnLam b e) | isId b = go (b:bs) e
65 go bs e = (reverse bs, e)
67 isAnnTypeArg :: AnnExpr b ann -> Bool
68 isAnnTypeArg (_, AnnType _) = True
69 isAnnTypeArg _ = False
71 dataConTagZ :: DataCon -> Int
72 dataConTagZ con = dataConTag con - fIRST_TAG
74 mkDataConTagLit :: DataCon -> Literal
75 mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
77 mkDataConTag :: DataCon -> CoreExpr
78 mkDataConTag = mkIntLitInt . dataConTagZ
80 splitPrimTyCon :: Type -> Maybe TyCon
82 | Just (tycon, []) <- splitTyConApp_maybe ty
88 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
89 mkBuiltinTyConApp get_tc tys
92 return $ mkTyConApp tc tys
94 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
95 mkBuiltinTyConApps get_tc tys ty
98 return $ foldr (mk tc) ty tys
100 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
103 voidType = mkBuiltinTyConApp VectMonad.voidTyCon []
105 mkWrapType :: Type -> VM Type
106 mkWrapType ty = mkBuiltinTyConApp wrapTyCon [ty]
108 mkClosureTypes :: [Type] -> Type -> VM Type
109 mkClosureTypes = mkBuiltinTyConApps closureTyCon
111 mkPReprType :: Type -> VM Type
112 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
114 mkPADictType :: Type -> VM Type
115 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
117 mkPArrayType :: Type -> VM Type
119 | Just tycon <- splitPrimTyCon ty
121 r <- lookupPrimPArray tycon
123 Just arr -> return $ mkTyConApp arr []
124 Nothing -> cantVectorise "Primitive tycon not vectorised" (ppr tycon)
125 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
127 mkPDataType :: Type -> VM Type
128 mkPDataType ty = mkBuiltinTyConApp pdataTyCon [ty]
130 mkPArray :: Type -> CoreExpr -> CoreExpr -> VM CoreExpr
131 mkPArray ty len dat = do
132 tc <- builtin parrayTyCon
133 let [dc] = tyConDataCons tc
134 return $ mkConApp dc [Type ty, len, dat]
136 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
140 return $ mkTyConApp tc []
142 pdataReprTyCon :: Type -> VM (TyCon, [Type])
143 pdataReprTyCon ty = builtin pdataTyCon >>= (`lookupFamInst` [ty])
145 pdataReprDataCon :: Type -> VM (DataCon, [Type])
148 (tc, arg_tys) <- pdataReprTyCon ty
149 let [dc] = tyConDataCons tc
152 mkVScrut :: VExpr -> VM (CoreExpr, CoreExpr, TyCon, [Type])
155 (tc, arg_tys) <- pdataReprTyCon ty
156 return (ve, unwrapFamInstScrut tc arg_tys le, tc, arg_tys)
160 prDFunOfTyCon :: TyCon -> VM CoreExpr
163 . maybeCantVectoriseM "No PR dictionary for tycon" (ppr tycon)
164 $ lookupTyConPR tycon
167 paDictArgType :: TyVar -> VM (Maybe Type)
168 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
170 go ty k | Just k' <- kindView k = go ty k'
173 tv <- newTyVar (fsLit "a") k1
174 mty1 <- go (TyVarTy tv) k1
177 mty2 <- go (AppTy ty (TyVarTy tv)) k2
178 return $ fmap (ForAllTy tv . FunTy ty1) mty2
183 = liftM Just (mkPADictType ty)
185 go _ _ = return Nothing
188 -- | Get the PA dictionary for some type, or `Nothing` if there isn't one.
189 paDictOfType :: Type -> VM (Maybe CoreExpr)
191 = paDictOfTyApp ty_fn ty_args
193 (ty_fn, ty_args) = splitAppTys ty
195 paDictOfTyApp :: Type -> [Type] -> VM (Maybe CoreExpr)
196 paDictOfTyApp ty_fn ty_args
197 | Just ty_fn' <- coreView ty_fn
198 = paDictOfTyApp ty_fn' ty_args
200 paDictOfTyApp (TyVarTy tv) ty_args
201 = do dfun <- maybeV (lookupTyVarPA tv)
202 liftM Just $ paDFunApply dfun ty_args
204 paDictOfTyApp (TyConApp tc _) ty_args
205 = do mdfun <- lookupTyConPA tc
208 -> pprTrace "VectUtils.paDictOfType"
209 (vcat [ text "No PA dictionary"
210 , text "for tycon: " <> ppr tc
211 , text "in type: " <> ppr ty])
214 Just dfun -> liftM Just $ paDFunApply (Var dfun) ty_args
217 = cantVectorise "Can't construct PA dictionary for type" (ppr ty)
221 paDFunType :: TyCon -> VM Type
224 margs <- mapM paDictArgType tvs
225 res <- mkPADictType (mkTyConApp tc arg_tys)
226 return . mkForAllTys tvs
227 $ mkFunTys [arg | Just arg <- margs] res
230 arg_tys = mkTyVarTys tvs
232 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
234 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
235 return $ mkApps (mkTyApps dfun tys) dicts
238 paMethod :: (Builtins -> Var) -> String -> Type -> VM CoreExpr
240 | Just tycon <- splitPrimTyCon ty
242 . maybeCantVectoriseM "No PA method" (text name <+> text "for" <+> ppr tycon)
243 $ lookupPrimMethod tycon name
248 Just dict <- paDictOfType ty
249 return $ mkApps (Var fn) [Type ty, dict]
251 prDictOfType :: Type -> VM CoreExpr
252 prDictOfType ty = prDictOfTyApp ty_fn ty_args
254 (ty_fn, ty_args) = splitAppTys ty
256 prDictOfTyApp :: Type -> [Type] -> VM CoreExpr
257 prDictOfTyApp ty_fn ty_args
258 | Just ty_fn' <- coreView ty_fn = prDictOfTyApp ty_fn' ty_args
259 prDictOfTyApp (TyConApp tc _) ty_args
261 dfun <- liftM Var $ maybeV (lookupTyConPR tc)
262 prDFunApply dfun ty_args
263 prDictOfTyApp _ _ = noV
265 prDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
268 dicts <- mapM prDictOfType tys
269 return $ mkApps (mkTyApps dfun tys) dicts
271 wrapPR :: Type -> VM CoreExpr
274 Just pa_dict <- paDictOfType ty
275 pr_dfun <- prDFunOfTyCon =<< builtin wrapTyCon
276 return $ mkApps pr_dfun [Type ty, pa_dict]
278 replicatePD :: CoreExpr -> CoreExpr -> VM CoreExpr
279 replicatePD len x = liftM (`mkApps` [len,x])
280 (paMethod replicatePDVar "replicatePD" (exprType x))
282 emptyPD :: Type -> VM CoreExpr
283 emptyPD = paMethod emptyPDVar "emptyPD"
285 packByTagPD :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
287 packByTagPD ty xs len tags t
288 = liftM (`mkApps` [xs, len, tags, t])
289 (paMethod packByTagPDVar "packByTagPD" ty)
291 combinePD :: Type -> CoreExpr -> CoreExpr -> [CoreExpr]
293 combinePD ty len sel xs
294 = liftM (`mkApps` (len : sel : xs))
295 (paMethod (combinePDVar n) ("combine" ++ show n ++ "PD") ty)
299 -- | Like `replicatePD` but use the lifting context in the vectoriser state.
300 liftPD :: CoreExpr -> VM CoreExpr
303 lc <- builtin liftingContext
304 replicatePD (Var lc) x
306 zipScalars :: [Type] -> Type -> VM CoreExpr
307 zipScalars arg_tys res_ty
309 scalar <- builtin scalarClass
310 (dfuns, _) <- mapAndUnzipM (\ty -> lookupInst scalar [ty]) ty_args
311 zipf <- builtin (scalarZip $ length arg_tys)
312 return $ Var zipf `mkTyApps` ty_args `mkApps` map Var dfuns
314 ty_args = arg_tys ++ [res_ty]
316 scalarClosure :: [Type] -> Type -> CoreExpr -> CoreExpr -> VM CoreExpr
317 scalarClosure arg_tys res_ty scalar_fun array_fun
319 ctr <- builtin (closureCtrFun $ length arg_tys)
320 Just pas <- liftM sequence $ mapM paDictOfType (init arg_tys)
321 return $ Var ctr `mkTyApps` (arg_tys ++ [res_ty])
322 `mkApps` (pas ++ [scalar_fun, array_fun])
324 newLocalVVar :: FastString -> Type -> VM VVar
327 lty <- mkPDataType vty
328 vv <- newLocalVar fs vty
329 lv <- newLocalVar fs lty
332 polyAbstract :: [TyVar] -> ([Var] -> VM a) -> VM a
336 mdicts <- mapM mk_dict_var tvs
337 zipWithM_ (\tv -> maybe (defLocalTyVar tv)
338 (defLocalTyVarWithPA tv . Var)) tvs mdicts
342 r <- paDictArgType tv
344 Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty)
345 Nothing -> return Nothing
347 mk_args mdicts = [dict | Just dict <- mdicts]
349 polyArity :: [TyVar] -> VM Int
351 tys <- mapM paDictArgType tvs
352 return $ length [() | Just _ <- tys]
354 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
356 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
357 return $ expr `mkTyApps` tys `mkApps` dicts
359 polyVApply :: VExpr -> [Type] -> VM VExpr
361 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
362 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
364 -- Inline ---------------------------------------------------------------------
365 -- | Records whether we should inline a particular binding.
370 -- | Add to the arity contained within an `Inline`, if any.
371 addInlineArity :: Inline -> Int -> Inline
372 addInlineArity (Inline m) n = Inline (m+n)
373 addInlineArity DontInline _ = DontInline
375 -- | Says to always inline a binding.
380 -- Hoising --------------------------------------------------------------------
381 hoistBinding :: Var -> CoreExpr -> VM ()
382 hoistBinding v e = updGEnv $ \env ->
383 env { global_bindings = (v,e) : global_bindings env }
385 hoistExpr :: FastString -> CoreExpr -> Inline -> VM Var
386 hoistExpr fs expr inl
388 var <- mk_inline `liftM` newLocalVar fs (exprType expr)
389 hoistBinding var expr
392 mk_inline var = case inl of
393 Inline arity -> var `setIdUnfolding`
394 mkInlineRule expr (Just arity)
397 hoistVExpr :: VExpr -> Inline -> VM VVar
398 hoistVExpr (ve, le) inl
401 vv <- hoistExpr ('v' `consFS` fs) ve inl
402 lv <- hoistExpr ('l' `consFS` fs) le (addInlineArity inl 1)
405 hoistPolyVExpr :: [TyVar] -> Inline -> VM VExpr -> VM VExpr
406 hoistPolyVExpr tvs inline p
408 inline' <- liftM (addInlineArity inline) (polyArity tvs)
409 expr <- closedV . polyAbstract tvs $ \args ->
410 liftM (mapVect (mkLams $ tvs ++ args)) p
411 fn <- hoistVExpr expr inline'
412 polyVApply (vVar fn) (mkTyVarTys tvs)
414 takeHoisted :: VM [(Var, CoreExpr)]
418 setGEnv $ env { global_bindings = [] }
419 return $ global_bindings env
422 boxExpr :: Type -> VExpr -> VM VExpr
423 boxExpr ty (vexpr, lexpr)
424 | Just (tycon, []) <- splitTyConApp_maybe ty
425 , isUnLiftedTyCon tycon
427 r <- lookupBoxedTyCon tycon
429 Just tycon' -> let [dc] = tyConDataCons tycon'
431 return (mkConApp dc [vexpr], lexpr)
432 Nothing -> return (vexpr, lexpr)
435 -- Closures -------------------------------------------------------------------
436 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
437 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
438 = do Just dict <- paDictOfType env_ty
439 mkv <- builtin closureVar
440 mkl <- builtin liftedClosureVar
441 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
442 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
445 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
446 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
447 = do vapply <- builtin applyVar
448 lapply <- builtin liftedApplyVar
449 lc <- builtin liftingContext
450 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
451 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [Var lc, lclo, larg])
454 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
455 buildClosures _ _ [] _ mk_body
457 buildClosures tvs vars [arg_ty] res_ty mk_body
458 = -- liftM vInlineMe $
459 buildClosure tvs vars arg_ty res_ty mk_body
460 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
462 res_ty' <- mkClosureTypes arg_tys res_ty
463 arg <- newLocalVVar (fsLit "x") arg_ty
465 buildClosure tvs vars arg_ty res_ty'
466 . hoistPolyVExpr tvs (Inline (length vars + 1))
468 lc <- builtin liftingContext
469 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
470 return $ vLams lc (vars ++ [arg]) clo
472 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
474 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
475 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
477 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
478 buildClosure tvs vars arg_ty res_ty mk_body
480 (env_ty, env, bind) <- buildEnv vars
481 env_bndr <- newLocalVVar (fsLit "env") env_ty
482 arg_bndr <- newLocalVVar (fsLit "arg") arg_ty
484 fn <- hoistPolyVExpr tvs (Inline 2)
486 lc <- builtin liftingContext
488 return -- . vInlineMe
489 . vLams lc [env_bndr, arg_bndr]
490 $ bind (vVar env_bndr)
491 (vVarApps lc body (vars ++ [arg_bndr]))
493 mkClosure arg_ty res_ty env_ty fn env
496 -- Environments ---------------------------------------------------------------
497 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr)
500 void <- builtin voidVar
501 pvoid <- builtin pvoidVar
502 return (ty, vVar (void, pvoid), \_ body -> body)
504 buildEnv [v] = return (vVarType v, vVar v,
505 \env body -> vLet (vNonRec v env) body)
510 (lenv_tc, lenv_tyargs) <- pdataReprTyCon ty
512 let venv_con = tupleCon Boxed (length vs)
513 [lenv_con] = tyConDataCons lenv_tc
515 venv = mkCoreTup (map Var vvs)
516 lenv = Var (dataConWrapId lenv_con)
517 `mkTyApps` lenv_tyargs
520 vbind env body = mkWildCase env ty (exprType body)
521 [(DataAlt venv_con, vvs, body)]
524 let scrut = unwrapFamInstScrut lenv_tc lenv_tyargs env
526 mkWildCase scrut (exprType scrut) (exprType body)
527 [(DataAlt lenv_con, lvs, body)]
529 bind (venv, lenv) (vbody, lbody) = (vbind venv vbody,
532 return (ty, (venv, lenv), bind)
534 (vvs, lvs) = unzip vs
535 tys = map vVarType vs
536 ty = mkBoxedTupleTy tys