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 )
50 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
51 collectAnnTypeArgs expr = go expr []
53 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
56 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
57 collectAnnTypeBinders expr = go [] expr
59 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
60 go bs e = (reverse bs, e)
62 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
63 collectAnnValBinders expr = go [] expr
65 go bs (_, AnnLam b e) | isId b = go (b:bs) e
66 go bs e = (reverse bs, e)
68 isAnnTypeArg :: AnnExpr b ann -> Bool
69 isAnnTypeArg (_, AnnType _) = True
70 isAnnTypeArg _ = False
72 dataConTagZ :: DataCon -> Int
73 dataConTagZ con = dataConTag con - fIRST_TAG
75 mkDataConTagLit :: DataCon -> Literal
76 mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
78 mkDataConTag :: DataCon -> CoreExpr
79 mkDataConTag = mkIntLitInt . dataConTagZ
81 splitPrimTyCon :: Type -> Maybe TyCon
83 | Just (tycon, []) <- splitTyConApp_maybe ty
89 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
90 mkBuiltinTyConApp get_tc tys
93 return $ mkTyConApp tc tys
95 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
96 mkBuiltinTyConApps get_tc tys ty
99 return $ foldr (mk tc) ty tys
101 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
104 voidType = mkBuiltinTyConApp VectMonad.voidTyCon []
106 mkWrapType :: Type -> VM Type
107 mkWrapType ty = mkBuiltinTyConApp wrapTyCon [ty]
109 mkClosureTypes :: [Type] -> Type -> VM Type
110 mkClosureTypes = mkBuiltinTyConApps closureTyCon
112 mkPReprType :: Type -> VM Type
113 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
115 mkPADictType :: Type -> VM Type
116 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
118 mkPArrayType :: Type -> VM Type
120 | Just tycon <- splitPrimTyCon ty
122 r <- lookupPrimPArray tycon
124 Just arr -> return $ mkTyConApp arr []
125 Nothing -> cantVectorise "Primitive tycon not vectorised" (ppr tycon)
126 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
128 mkPDataType :: Type -> VM Type
129 mkPDataType ty = mkBuiltinTyConApp pdataTyCon [ty]
131 mkPArray :: Type -> CoreExpr -> CoreExpr -> VM CoreExpr
132 mkPArray ty len dat = do
133 tc <- builtin parrayTyCon
134 let [dc] = tyConDataCons tc
135 return $ mkConApp dc [Type ty, len, dat]
137 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
141 return $ mkTyConApp tc []
143 pdataReprTyCon :: Type -> VM (TyCon, [Type])
144 pdataReprTyCon ty = builtin pdataTyCon >>= (`lookupFamInst` [ty])
146 pdataReprDataCon :: Type -> VM (DataCon, [Type])
149 (tc, arg_tys) <- pdataReprTyCon ty
150 let [dc] = tyConDataCons tc
153 mkVScrut :: VExpr -> VM (CoreExpr, CoreExpr, TyCon, [Type])
156 (tc, arg_tys) <- pdataReprTyCon ty
157 return (ve, unwrapFamInstScrut tc arg_tys le, tc, arg_tys)
161 prDFunOfTyCon :: TyCon -> VM CoreExpr
164 . maybeCantVectoriseM "No PR dictionary for tycon" (ppr tycon)
165 $ lookupTyConPR tycon
168 paDictArgType :: TyVar -> VM (Maybe Type)
169 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
171 go ty k | Just k' <- kindView k = go ty k'
174 tv <- newTyVar (fsLit "a") k1
175 mty1 <- go (TyVarTy tv) k1
178 mty2 <- go (AppTy ty (TyVarTy tv)) k2
179 return $ fmap (ForAllTy tv . FunTy ty1) mty2
184 = liftM Just (mkPADictType ty)
186 go _ _ = return Nothing
189 -- | Get the PA dictionary for some type, or `Nothing` if there isn't one.
190 paDictOfType :: Type -> VM (Maybe CoreExpr)
192 = paDictOfTyApp ty_fn ty_args
194 (ty_fn, ty_args) = splitAppTys ty
196 paDictOfTyApp :: Type -> [Type] -> VM (Maybe CoreExpr)
197 paDictOfTyApp ty_fn ty_args
198 | Just ty_fn' <- coreView ty_fn
199 = paDictOfTyApp ty_fn' ty_args
201 paDictOfTyApp (TyVarTy tv) ty_args
202 = do dfun <- maybeV (lookupTyVarPA tv)
203 liftM Just $ paDFunApply dfun ty_args
205 paDictOfTyApp (TyConApp tc _) ty_args
206 = do mdfun <- lookupTyConPA tc
209 -> pprTrace "VectUtils.paDictOfType"
210 (vcat [ text "No PA dictionary"
211 , text "for tycon: " <> ppr tc
212 , text "in type: " <> ppr ty])
215 Just dfun -> liftM Just $ paDFunApply (Var dfun) ty_args
218 = cantVectorise "Can't construct PA dictionary for type" (ppr ty)
222 paDFunType :: TyCon -> VM Type
225 margs <- mapM paDictArgType tvs
226 res <- mkPADictType (mkTyConApp tc arg_tys)
227 return . mkForAllTys tvs
228 $ mkFunTys [arg | Just arg <- margs] res
231 arg_tys = mkTyVarTys tvs
233 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
235 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
236 return $ mkApps (mkTyApps dfun tys) dicts
239 paMethod :: (Builtins -> Var) -> String -> Type -> VM CoreExpr
241 | Just tycon <- splitPrimTyCon ty
243 . maybeCantVectoriseM "No PA method" (text name <+> text "for" <+> ppr tycon)
244 $ lookupPrimMethod tycon name
249 Just dict <- paDictOfType ty
250 return $ mkApps (Var fn) [Type ty, dict]
252 prDictOfType :: Type -> VM CoreExpr
253 prDictOfType ty = prDictOfTyApp ty_fn ty_args
255 (ty_fn, ty_args) = splitAppTys ty
257 prDictOfTyApp :: Type -> [Type] -> VM CoreExpr
258 prDictOfTyApp ty_fn ty_args
259 | Just ty_fn' <- coreView ty_fn = prDictOfTyApp ty_fn' ty_args
260 prDictOfTyApp (TyConApp tc _) ty_args
262 dfun <- liftM Var $ maybeV (lookupTyConPR tc)
263 prDFunApply dfun ty_args
264 prDictOfTyApp _ _ = noV
266 prDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
269 dicts <- mapM prDictOfType tys
270 return $ mkApps (mkTyApps dfun tys) dicts
272 wrapPR :: Type -> VM CoreExpr
275 Just pa_dict <- paDictOfType ty
276 pr_dfun <- prDFunOfTyCon =<< builtin wrapTyCon
277 return $ mkApps pr_dfun [Type ty, pa_dict]
279 replicatePD :: CoreExpr -> CoreExpr -> VM CoreExpr
280 replicatePD len x = liftM (`mkApps` [len,x])
281 (paMethod replicatePDVar "replicatePD" (exprType x))
283 emptyPD :: Type -> VM CoreExpr
284 emptyPD = paMethod emptyPDVar "emptyPD"
286 packByTagPD :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
288 packByTagPD ty xs len tags t
289 = liftM (`mkApps` [xs, len, tags, t])
290 (paMethod packByTagPDVar "packByTagPD" ty)
292 combinePD :: Type -> CoreExpr -> CoreExpr -> [CoreExpr]
294 combinePD ty len sel xs
295 = liftM (`mkApps` (len : sel : xs))
296 (paMethod (combinePDVar n) ("combine" ++ show n ++ "PD") ty)
300 -- | Like `replicatePD` but use the lifting context in the vectoriser state.
301 liftPD :: CoreExpr -> VM CoreExpr
304 lc <- builtin liftingContext
305 replicatePD (Var lc) x
307 zipScalars :: [Type] -> Type -> VM CoreExpr
308 zipScalars arg_tys res_ty
310 scalar <- builtin scalarClass
311 (dfuns, _) <- mapAndUnzipM (\ty -> lookupInst scalar [ty]) ty_args
312 zipf <- builtin (scalarZip $ length arg_tys)
313 return $ Var zipf `mkTyApps` ty_args `mkApps` map Var dfuns
315 ty_args = arg_tys ++ [res_ty]
317 scalarClosure :: [Type] -> Type -> CoreExpr -> CoreExpr -> VM CoreExpr
318 scalarClosure arg_tys res_ty scalar_fun array_fun
320 ctr <- builtin (closureCtrFun $ length arg_tys)
321 Just pas <- liftM sequence $ mapM paDictOfType (init arg_tys)
322 return $ Var ctr `mkTyApps` (arg_tys ++ [res_ty])
323 `mkApps` (pas ++ [scalar_fun, array_fun])
325 newLocalVVar :: FastString -> Type -> VM VVar
328 lty <- mkPDataType vty
329 vv <- newLocalVar fs vty
330 lv <- newLocalVar fs lty
333 polyAbstract :: [TyVar] -> ([Var] -> VM a) -> VM a
337 mdicts <- mapM mk_dict_var tvs
338 zipWithM_ (\tv -> maybe (defLocalTyVar tv)
339 (defLocalTyVarWithPA tv . Var)) tvs mdicts
343 r <- paDictArgType tv
345 Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty)
346 Nothing -> return Nothing
348 mk_args mdicts = [dict | Just dict <- mdicts]
350 polyArity :: [TyVar] -> VM Int
352 tys <- mapM paDictArgType tvs
353 return $ length [() | Just _ <- tys]
355 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
357 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
358 return $ expr `mkTyApps` tys `mkApps` dicts
360 polyVApply :: VExpr -> [Type] -> VM VExpr
362 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
363 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
365 -- Inline ---------------------------------------------------------------------
366 -- | Records whether we should inline a particular binding.
371 -- | Add to the arity contained within an `Inline`, if any.
372 addInlineArity :: Inline -> Int -> Inline
373 addInlineArity (Inline m) n = Inline (m+n)
374 addInlineArity DontInline _ = DontInline
376 -- | Says to always inline a binding.
381 -- Hoising --------------------------------------------------------------------
382 hoistBinding :: Var -> CoreExpr -> VM ()
383 hoistBinding v e = updGEnv $ \env ->
384 env { global_bindings = (v,e) : global_bindings env }
386 hoistExpr :: FastString -> CoreExpr -> Inline -> VM Var
387 hoistExpr fs expr inl
389 var <- mk_inline `liftM` newLocalVar fs (exprType expr)
390 hoistBinding var expr
393 mk_inline var = case inl of
394 Inline arity -> var `setIdUnfolding`
395 mkInlineRule expr (Just arity)
398 hoistVExpr :: VExpr -> Inline -> VM VVar
399 hoistVExpr (ve, le) inl
402 vv <- hoistExpr ('v' `consFS` fs) ve inl
403 lv <- hoistExpr ('l' `consFS` fs) le (addInlineArity inl 1)
406 hoistPolyVExpr :: [TyVar] -> Inline -> VM VExpr -> VM VExpr
407 hoistPolyVExpr tvs inline p
409 inline' <- liftM (addInlineArity inline) (polyArity tvs)
410 expr <- closedV . polyAbstract tvs $ \args ->
411 liftM (mapVect (mkLams $ tvs ++ args)) p
412 fn <- hoistVExpr expr inline'
413 polyVApply (vVar fn) (mkTyVarTys tvs)
415 takeHoisted :: VM [(Var, CoreExpr)]
419 setGEnv $ env { global_bindings = [] }
420 return $ global_bindings env
423 boxExpr :: Type -> VExpr -> VM VExpr
424 boxExpr ty (vexpr, lexpr)
425 | Just (tycon, []) <- splitTyConApp_maybe ty
426 , isUnLiftedTyCon tycon
428 r <- lookupBoxedTyCon tycon
430 Just tycon' -> let [dc] = tyConDataCons tycon'
432 return (mkConApp dc [vexpr], lexpr)
433 Nothing -> return (vexpr, lexpr)
436 -- Closures -------------------------------------------------------------------
437 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
438 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
439 = do Just dict <- paDictOfType env_ty
440 mkv <- builtin closureVar
441 mkl <- builtin liftedClosureVar
442 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
443 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
446 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
447 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
448 = do vapply <- builtin applyVar
449 lapply <- builtin liftedApplyVar
450 lc <- builtin liftingContext
451 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
452 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [Var lc, lclo, larg])
455 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
456 buildClosures _ _ [] _ mk_body
458 buildClosures tvs vars [arg_ty] res_ty mk_body
459 = -- liftM vInlineMe $
460 buildClosure tvs vars arg_ty res_ty mk_body
461 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
463 res_ty' <- mkClosureTypes arg_tys res_ty
464 arg <- newLocalVVar (fsLit "x") arg_ty
466 buildClosure tvs vars arg_ty res_ty'
467 . hoistPolyVExpr tvs (Inline (length vars + 1))
469 lc <- builtin liftingContext
470 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
471 return $ vLams lc (vars ++ [arg]) clo
473 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
475 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
476 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
478 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
479 buildClosure tvs vars arg_ty res_ty mk_body
481 (env_ty, env, bind) <- buildEnv vars
482 env_bndr <- newLocalVVar (fsLit "env") env_ty
483 arg_bndr <- newLocalVVar (fsLit "arg") arg_ty
485 fn <- hoistPolyVExpr tvs (Inline 2)
487 lc <- builtin liftingContext
489 return -- . vInlineMe
490 . vLams lc [env_bndr, arg_bndr]
491 $ bind (vVar env_bndr)
492 (vVarApps lc body (vars ++ [arg_bndr]))
494 mkClosure arg_ty res_ty env_ty fn env
497 -- Environments ---------------------------------------------------------------
498 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr)
501 void <- builtin voidVar
502 pvoid <- builtin pvoidVar
503 return (ty, vVar (void, pvoid), \_ body -> body)
505 buildEnv [v] = return (vVarType v, vVar v,
506 \env body -> vLet (vNonRec v env) body)
511 (lenv_tc, lenv_tyargs) <- pdataReprTyCon ty
513 let venv_con = tupleCon Boxed (length vs)
514 [lenv_con] = tyConDataCons lenv_tc
516 venv = mkCoreTup (map Var vvs)
517 lenv = Var (dataConWrapId lenv_con)
518 `mkTyApps` lenv_tyargs
521 vbind env body = mkWildCase env ty (exprType body)
522 [(DataAlt venv_con, vvs, body)]
525 let scrut = unwrapFamInstScrut lenv_tc lenv_tyargs env
527 mkWildCase scrut (exprType scrut) (exprType body)
528 [(DataAlt lenv_con, lvs, body)]
530 bind (venv, lenv) (vbody, lbody) = (vbind venv vbody,
533 return (ty, (venv, lenv), bind)
535 (vvs, lvs) = unzip vs
536 tys = map vVarType vs
537 ty = mkBoxedTupleTy tys