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
166 paDictArgType :: TyVar -> VM (Maybe Type)
167 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
169 go ty k | Just k' <- kindView k = go ty k'
172 tv <- newTyVar (fsLit "a") k1
173 mty1 <- go (TyVarTy tv) k1
176 mty2 <- go (AppTy ty (TyVarTy tv)) k2
177 return $ fmap (ForAllTy tv . FunTy ty1) mty2
182 = liftM Just (mkPADictType ty)
184 go _ _ = return Nothing
186 paDictOfType :: Type -> VM CoreExpr
187 paDictOfType ty = paDictOfTyApp ty_fn ty_args
189 (ty_fn, ty_args) = splitAppTys ty
191 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
192 paDictOfTyApp ty_fn ty_args
193 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
194 paDictOfTyApp (TyVarTy tv) ty_args
196 dfun <- maybeV (lookupTyVarPA tv)
197 paDFunApply dfun ty_args
198 paDictOfTyApp (TyConApp tc _) ty_args
200 dfun <- maybeCantVectoriseM "No PA dictionary for tycon" (ppr tc)
202 paDFunApply (Var dfun) ty_args
204 = cantVectorise "Can't construct PA dictionary for type" (ppr ty)
206 paDFunType :: TyCon -> VM Type
209 margs <- mapM paDictArgType tvs
210 res <- mkPADictType (mkTyConApp tc arg_tys)
211 return . mkForAllTys tvs
212 $ mkFunTys [arg | Just arg <- margs] res
215 arg_tys = mkTyVarTys tvs
217 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
220 dicts <- mapM paDictOfType tys
221 return $ mkApps (mkTyApps dfun tys) dicts
223 paMethod :: (Builtins -> Var) -> String -> Type -> VM CoreExpr
225 | Just tycon <- splitPrimTyCon ty
227 . maybeCantVectoriseM "No PA method" (text name <+> text "for" <+> ppr tycon)
228 $ lookupPrimMethod tycon name
233 dict <- paDictOfType ty
234 return $ mkApps (Var fn) [Type ty, dict]
236 prDictOfType :: Type -> VM CoreExpr
237 prDictOfType ty = prDictOfTyApp ty_fn ty_args
239 (ty_fn, ty_args) = splitAppTys ty
241 prDictOfTyApp :: Type -> [Type] -> VM CoreExpr
242 prDictOfTyApp ty_fn ty_args
243 | Just ty_fn' <- coreView ty_fn = prDictOfTyApp ty_fn' ty_args
244 prDictOfTyApp (TyConApp tc _) ty_args
246 dfun <- liftM Var $ maybeV (lookupTyConPR tc)
247 prDFunApply dfun ty_args
248 prDictOfTyApp _ _ = noV
250 prDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
253 dicts <- mapM prDictOfType tys
254 return $ mkApps (mkTyApps dfun tys) dicts
256 wrapPR :: Type -> VM CoreExpr
259 pa_dict <- paDictOfType ty
260 pr_dfun <- prDFunOfTyCon =<< builtin wrapTyCon
261 return $ mkApps pr_dfun [Type ty, pa_dict]
263 replicatePD :: CoreExpr -> CoreExpr -> VM CoreExpr
264 replicatePD len x = liftM (`mkApps` [len,x])
265 (paMethod replicatePDVar "replicatePD" (exprType x))
267 emptyPD :: Type -> VM CoreExpr
268 emptyPD = paMethod emptyPDVar "emptyPD"
270 packByTagPD :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
272 packByTagPD ty xs len tags t
273 = liftM (`mkApps` [xs, len, tags, t])
274 (paMethod packByTagPDVar "packByTagPD" ty)
276 combinePD :: Type -> CoreExpr -> CoreExpr -> [CoreExpr]
278 combinePD ty len sel xs
279 = liftM (`mkApps` (len : sel : xs))
280 (paMethod (combinePDVar n) ("combine" ++ show n ++ "PD") ty)
284 -- | Like `replicatePD` but use the lifting context in the vectoriser state.
285 liftPD :: CoreExpr -> VM CoreExpr
288 lc <- builtin liftingContext
289 replicatePD (Var lc) x
291 zipScalars :: [Type] -> Type -> VM CoreExpr
292 zipScalars arg_tys res_ty
294 scalar <- builtin scalarClass
295 (dfuns, _) <- mapAndUnzipM (\ty -> lookupInst scalar [ty]) ty_args
296 zipf <- builtin (scalarZip $ length arg_tys)
297 return $ Var zipf `mkTyApps` ty_args `mkApps` map Var dfuns
299 ty_args = arg_tys ++ [res_ty]
301 scalarClosure :: [Type] -> Type -> CoreExpr -> CoreExpr -> VM CoreExpr
302 scalarClosure arg_tys res_ty scalar_fun array_fun
304 ctr <- builtin (closureCtrFun $ length arg_tys)
305 pas <- mapM paDictOfType (init arg_tys)
306 return $ Var ctr `mkTyApps` (arg_tys ++ [res_ty])
307 `mkApps` (pas ++ [scalar_fun, array_fun])
309 newLocalVVar :: FastString -> Type -> VM VVar
312 lty <- mkPDataType vty
313 vv <- newLocalVar fs vty
314 lv <- newLocalVar fs lty
317 polyAbstract :: [TyVar] -> ([Var] -> VM a) -> VM a
321 mdicts <- mapM mk_dict_var tvs
322 zipWithM_ (\tv -> maybe (defLocalTyVar tv)
323 (defLocalTyVarWithPA tv . Var)) tvs mdicts
327 r <- paDictArgType tv
329 Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty)
330 Nothing -> return Nothing
332 mk_args mdicts = [dict | Just dict <- mdicts]
334 polyArity :: [TyVar] -> VM Int
336 tys <- mapM paDictArgType tvs
337 return $ length [() | Just _ <- tys]
339 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
342 dicts <- mapM paDictOfType tys
343 return $ expr `mkTyApps` tys `mkApps` dicts
345 polyVApply :: VExpr -> [Type] -> VM VExpr
348 dicts <- mapM paDictOfType tys
349 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
352 data Inline = Inline Arity
355 addInlineArity :: Inline -> Int -> Inline
356 addInlineArity (Inline m) n = Inline (m+n)
357 addInlineArity DontInline _ = DontInline
363 -- Hoising --------------------------------------------------------------------
364 hoistBinding :: Var -> CoreExpr -> VM ()
365 hoistBinding v e = updGEnv $ \env ->
366 env { global_bindings = (v,e) : global_bindings env }
368 hoistExpr :: FastString -> CoreExpr -> Inline -> VM Var
369 hoistExpr fs expr inl
371 var <- mk_inline `liftM` newLocalVar fs (exprType expr)
372 hoistBinding var expr
375 mk_inline var = case inl of
376 Inline arity -> var `setIdUnfolding`
377 mkInlineRule expr (Just arity)
380 hoistVExpr :: VExpr -> Inline -> VM VVar
381 hoistVExpr (ve, le) inl
384 vv <- hoistExpr ('v' `consFS` fs) ve inl
385 lv <- hoistExpr ('l' `consFS` fs) le (addInlineArity inl 1)
388 hoistPolyVExpr :: [TyVar] -> Inline -> VM VExpr -> VM VExpr
389 hoistPolyVExpr tvs inline p
391 inline' <- liftM (addInlineArity inline) (polyArity tvs)
392 expr <- closedV . polyAbstract tvs $ \args ->
393 liftM (mapVect (mkLams $ tvs ++ args)) p
394 fn <- hoistVExpr expr inline'
395 polyVApply (vVar fn) (mkTyVarTys tvs)
397 takeHoisted :: VM [(Var, CoreExpr)]
401 setGEnv $ env { global_bindings = [] }
402 return $ global_bindings env
405 boxExpr :: Type -> VExpr -> VM VExpr
406 boxExpr ty (vexpr, lexpr)
407 | Just (tycon, []) <- splitTyConApp_maybe ty
408 , isUnLiftedTyCon tycon
410 r <- lookupBoxedTyCon tycon
412 Just tycon' -> let [dc] = tyConDataCons tycon'
414 return (mkConApp dc [vexpr], lexpr)
415 Nothing -> return (vexpr, lexpr)
418 -- Closures -------------------------------------------------------------------
419 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
420 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
421 = do Just dict <- paDictOfType env_ty
422 mkv <- builtin closureVar
423 mkl <- builtin liftedClosureVar
424 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
425 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
427 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
428 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
429 = do vapply <- builtin applyVar
430 lapply <- builtin liftedApplyVar
431 lc <- builtin liftingContext
432 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
433 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [Var lc, lclo, larg])
436 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
437 buildClosures _ _ [] _ mk_body
439 buildClosures tvs vars [arg_ty] res_ty mk_body
440 = -- liftM vInlineMe $
441 buildClosure tvs vars arg_ty res_ty mk_body
442 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
444 res_ty' <- mkClosureTypes arg_tys res_ty
445 arg <- newLocalVVar (fsLit "x") arg_ty
447 buildClosure tvs vars arg_ty res_ty'
448 . hoistPolyVExpr tvs (Inline (length vars + 1))
450 lc <- builtin liftingContext
451 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
452 return $ vLams lc (vars ++ [arg]) clo
454 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
456 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
457 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
459 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
460 buildClosure tvs vars arg_ty res_ty mk_body
462 (env_ty, env, bind) <- buildEnv vars
463 env_bndr <- newLocalVVar (fsLit "env") env_ty
464 arg_bndr <- newLocalVVar (fsLit "arg") arg_ty
466 fn <- hoistPolyVExpr tvs (Inline 2)
468 lc <- builtin liftingContext
470 return -- . vInlineMe
471 . vLams lc [env_bndr, arg_bndr]
472 $ bind (vVar env_bndr)
473 (vVarApps lc body (vars ++ [arg_bndr]))
475 mkClosure arg_ty res_ty env_ty fn env
478 -- Environments ---------------------------------------------------------------
479 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr)
482 void <- builtin voidVar
483 pvoid <- builtin pvoidVar
484 return (ty, vVar (void, pvoid), \_ body -> body)
486 buildEnv [v] = return (vVarType v, vVar v,
487 \env body -> vLet (vNonRec v env) body)
492 (lenv_tc, lenv_tyargs) <- pdataReprTyCon ty
494 let venv_con = tupleCon Boxed (length vs)
495 [lenv_con] = tyConDataCons lenv_tc
497 venv = mkCoreTup (map Var vvs)
498 lenv = Var (dataConWrapId lenv_con)
499 `mkTyApps` lenv_tyargs
502 vbind env body = mkWildCase env ty (exprType body)
503 [(DataAlt venv_con, vvs, body)]
506 let scrut = unwrapFamInstScrut lenv_tc lenv_tyargs env
508 mkWildCase scrut (exprType scrut) (exprType body)
509 [(DataAlt lenv_con, lvs, body)]
511 bind (venv, lenv) (vbody, lbody) = (vbind venv vbody,
514 return (ty, (venv, lenv), bind)
516 (vvs, lvs) = unzip vs
517 tys = map vVarType vs
518 ty = mkBoxedTupleTy tys