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 liftPD :: CoreExpr -> VM CoreExpr
287 lc <- builtin liftingContext
288 replicatePD (Var lc) x
290 zipScalars :: [Type] -> Type -> VM CoreExpr
291 zipScalars arg_tys res_ty
293 scalar <- builtin scalarClass
294 (dfuns, _) <- mapAndUnzipM (\ty -> lookupInst scalar [ty]) ty_args
295 zipf <- builtin (scalarZip $ length arg_tys)
296 return $ Var zipf `mkTyApps` ty_args `mkApps` map Var dfuns
298 ty_args = arg_tys ++ [res_ty]
300 scalarClosure :: [Type] -> Type -> CoreExpr -> CoreExpr -> VM CoreExpr
301 scalarClosure arg_tys res_ty scalar_fun array_fun
303 ctr <- builtin (closureCtrFun $ length arg_tys)
304 pas <- mapM paDictOfType (init arg_tys)
305 return $ Var ctr `mkTyApps` (arg_tys ++ [res_ty])
306 `mkApps` (pas ++ [scalar_fun, array_fun])
308 newLocalVVar :: FastString -> Type -> VM VVar
311 lty <- mkPDataType vty
312 vv <- newLocalVar fs vty
313 lv <- newLocalVar fs lty
316 polyAbstract :: [TyVar] -> ([Var] -> VM a) -> VM a
320 mdicts <- mapM mk_dict_var tvs
321 zipWithM_ (\tv -> maybe (defLocalTyVar tv)
322 (defLocalTyVarWithPA tv . Var)) tvs mdicts
326 r <- paDictArgType tv
328 Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty)
329 Nothing -> return Nothing
331 mk_args mdicts = [dict | Just dict <- mdicts]
333 polyArity :: [TyVar] -> VM Int
335 tys <- mapM paDictArgType tvs
336 return $ length [() | Just _ <- tys]
338 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
341 dicts <- mapM paDictOfType tys
342 return $ expr `mkTyApps` tys `mkApps` dicts
344 polyVApply :: VExpr -> [Type] -> VM VExpr
347 dicts <- mapM paDictOfType tys
348 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
351 data Inline = Inline Arity
354 addInlineArity :: Inline -> Int -> Inline
355 addInlineArity (Inline m) n = Inline (m+n)
356 addInlineArity DontInline _ = DontInline
361 hoistBinding :: Var -> CoreExpr -> VM ()
362 hoistBinding v e = updGEnv $ \env ->
363 env { global_bindings = (v,e) : global_bindings env }
365 hoistExpr :: FastString -> CoreExpr -> Inline -> VM Var
366 hoistExpr fs expr inl
368 var <- mk_inline `liftM` newLocalVar fs (exprType expr)
369 hoistBinding var expr
372 mk_inline var = case inl of
373 Inline arity -> var `setIdUnfolding`
374 mkInlineRule expr (Just arity)
377 hoistVExpr :: VExpr -> Inline -> VM VVar
378 hoistVExpr (ve, le) inl
381 vv <- hoistExpr ('v' `consFS` fs) ve inl
382 lv <- hoistExpr ('l' `consFS` fs) le (addInlineArity inl 1)
385 hoistPolyVExpr :: [TyVar] -> Inline -> VM VExpr -> VM VExpr
386 hoistPolyVExpr tvs inline p
388 inline' <- liftM (addInlineArity inline) (polyArity tvs)
389 expr <- closedV . polyAbstract tvs $ \args ->
390 liftM (mapVect (mkLams $ tvs ++ args)) p
391 fn <- hoistVExpr expr inline'
392 polyVApply (vVar fn) (mkTyVarTys tvs)
394 takeHoisted :: VM [(Var, CoreExpr)]
398 setGEnv $ env { global_bindings = [] }
399 return $ global_bindings env
402 boxExpr :: Type -> VExpr -> VM VExpr
403 boxExpr ty (vexpr, lexpr)
404 | Just (tycon, []) <- splitTyConApp_maybe ty
405 , isUnLiftedTyCon tycon
407 r <- lookupBoxedTyCon tycon
409 Just tycon' -> let [dc] = tyConDataCons tycon'
411 return (mkConApp dc [vexpr], lexpr)
412 Nothing -> return (vexpr, lexpr)
415 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
416 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
418 dict <- paDictOfType env_ty
419 mkv <- builtin closureVar
420 mkl <- builtin liftedClosureVar
421 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
422 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
424 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
425 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
427 vapply <- builtin applyVar
428 lapply <- builtin liftedApplyVar
429 lc <- builtin liftingContext
430 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
431 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [Var lc, lclo, larg])
433 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
434 buildClosures _ _ [] _ mk_body
436 buildClosures tvs vars [arg_ty] res_ty mk_body
437 = -- liftM vInlineMe $
438 buildClosure tvs vars arg_ty res_ty mk_body
439 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
441 res_ty' <- mkClosureTypes arg_tys res_ty
442 arg <- newLocalVVar (fsLit "x") arg_ty
444 buildClosure tvs vars arg_ty res_ty'
445 . hoistPolyVExpr tvs (Inline (length vars + 1))
447 lc <- builtin liftingContext
448 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
449 return $ vLams lc (vars ++ [arg]) clo
451 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
453 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
454 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
456 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
457 buildClosure tvs vars arg_ty res_ty mk_body
459 (env_ty, env, bind) <- buildEnv vars
460 env_bndr <- newLocalVVar (fsLit "env") env_ty
461 arg_bndr <- newLocalVVar (fsLit "arg") arg_ty
463 fn <- hoistPolyVExpr tvs (Inline 2)
465 lc <- builtin liftingContext
467 return -- . vInlineMe
468 . vLams lc [env_bndr, arg_bndr]
469 $ bind (vVar env_bndr)
470 (vVarApps lc body (vars ++ [arg_bndr]))
472 mkClosure arg_ty res_ty env_ty fn env
474 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr)
477 void <- builtin voidVar
478 pvoid <- builtin pvoidVar
479 return (ty, vVar (void, pvoid), \_ body -> body)
481 buildEnv [v] = return (vVarType v, vVar v,
482 \env body -> vLet (vNonRec v env) body)
487 (lenv_tc, lenv_tyargs) <- pdataReprTyCon ty
489 let venv_con = tupleCon Boxed (length vs)
490 [lenv_con] = tyConDataCons lenv_tc
492 venv = mkCoreTup (map Var vvs)
493 lenv = Var (dataConWrapId lenv_con)
494 `mkTyApps` lenv_tyargs
497 vbind env body = mkWildCase env ty (exprType body)
498 [(DataAlt venv_con, vvs, body)]
501 let scrut = unwrapFamInstScrut lenv_tc lenv_tyargs env
503 mkWildCase scrut (exprType scrut) (exprType body)
504 [(DataAlt lenv_con, lvs, body)]
506 bind (venv, lenv) (vbody, lbody) = (vbind venv vbody,
509 return (ty, (venv, lenv), bind)
511 (vvs, lvs) = unzip vs
512 tys = map vVarType vs
513 ty = mkBoxedTupleTy tys