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,
29 import MkCore ( mkCoreTup, mkWildCase )
32 import CoreUnfold ( mkInlineRule )
39 import MkId ( unwrapFamInstScrut )
40 import Id ( setIdUnfolding )
42 import BasicTypes ( Boxity(..), Arity )
43 import Literal ( Literal, mkMachInt )
51 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
52 collectAnnTypeArgs expr = go expr []
54 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
57 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
58 collectAnnTypeBinders expr = go [] expr
60 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
61 go bs e = (reverse bs, e)
63 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
64 collectAnnValBinders expr = go [] expr
66 go bs (_, AnnLam b e) | isId b = go (b:bs) e
67 go bs e = (reverse bs, e)
69 isAnnTypeArg :: AnnExpr b ann -> Bool
70 isAnnTypeArg (_, AnnType _) = True
71 isAnnTypeArg _ = False
73 dataConTagZ :: DataCon -> Int
74 dataConTagZ con = dataConTag con - fIRST_TAG
76 mkDataConTagLit :: DataCon -> Literal
77 mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
79 mkDataConTag :: DataCon -> CoreExpr
80 mkDataConTag = mkIntLitInt . dataConTagZ
82 splitPrimTyCon :: Type -> Maybe TyCon
84 | Just (tycon, []) <- splitTyConApp_maybe ty
90 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
91 mkBuiltinTyConApp get_tc tys
94 return $ mkTyConApp tc tys
96 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
97 mkBuiltinTyConApps get_tc tys ty
100 return $ foldr (mk tc) ty tys
102 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
105 voidType = mkBuiltinTyConApp VectMonad.voidTyCon []
107 mkWrapType :: Type -> VM Type
108 mkWrapType ty = mkBuiltinTyConApp wrapTyCon [ty]
110 mkClosureTypes :: [Type] -> Type -> VM Type
111 mkClosureTypes = mkBuiltinTyConApps closureTyCon
113 mkPReprType :: Type -> VM Type
114 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
116 mkPADictType :: Type -> VM Type
117 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
119 mkPArrayType :: Type -> VM Type
121 | Just tycon <- splitPrimTyCon ty
123 r <- lookupPrimPArray tycon
125 Just arr -> return $ mkTyConApp arr []
126 Nothing -> cantVectorise "Primitive tycon not vectorised" (ppr tycon)
127 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
129 mkPDataType :: Type -> VM Type
130 mkPDataType ty = mkBuiltinTyConApp pdataTyCon [ty]
132 mkPArray :: Type -> CoreExpr -> CoreExpr -> VM CoreExpr
133 mkPArray ty len dat = do
134 tc <- builtin parrayTyCon
135 let [dc] = tyConDataCons tc
136 return $ mkConApp dc [Type ty, len, dat]
138 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
142 return $ mkTyConApp tc []
144 pdataReprTyCon :: Type -> VM (TyCon, [Type])
145 pdataReprTyCon ty = builtin pdataTyCon >>= (`lookupFamInst` [ty])
147 pdataReprDataCon :: Type -> VM (DataCon, [Type])
150 (tc, arg_tys) <- pdataReprTyCon ty
151 let [dc] = tyConDataCons tc
154 mkVScrut :: VExpr -> VM (CoreExpr, CoreExpr, TyCon, [Type])
157 (tc, arg_tys) <- pdataReprTyCon ty
158 return (ve, unwrapFamInstScrut tc arg_tys le, tc, arg_tys)
162 prDFunOfTyCon :: TyCon -> VM CoreExpr
165 . maybeCantVectoriseM "No PR dictionary for tycon" (ppr tycon)
166 $ lookupTyConPR tycon
169 paDictArgType :: TyVar -> VM (Maybe Type)
170 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
172 go ty k | Just k' <- kindView k = go ty k'
175 tv <- newTyVar (fsLit "a") k1
176 mty1 <- go (TyVarTy tv) k1
179 mty2 <- go (AppTy ty (TyVarTy tv)) k2
180 return $ fmap (ForAllTy tv . FunTy ty1) mty2
185 = liftM Just (mkPADictType ty)
187 go _ _ = return Nothing
190 -- | Get the PA dictionary for some type, or `Nothing` if there isn't one.
191 paDictOfType :: Type -> VM (Maybe CoreExpr)
193 = paDictOfTyApp ty_fn ty_args
195 (ty_fn, ty_args) = splitAppTys ty
197 paDictOfTyApp :: Type -> [Type] -> VM (Maybe CoreExpr)
198 paDictOfTyApp ty_fn ty_args
199 | Just ty_fn' <- coreView ty_fn
200 = paDictOfTyApp ty_fn' ty_args
202 paDictOfTyApp (TyVarTy tv) ty_args
203 = do dfun <- maybeV (lookupTyVarPA tv)
204 liftM Just $ paDFunApply dfun ty_args
206 paDictOfTyApp (TyConApp tc _) ty_args
207 = do mdfun <- lookupTyConPA tc
210 -> pprTrace "VectUtils.paDictOfType"
211 (vcat [ text "No PA dictionary"
212 , text "for tycon: " <> ppr tc
213 , text "in type: " <> ppr ty])
216 Just dfun -> liftM Just $ paDFunApply (Var dfun) ty_args
219 = cantVectorise "Can't construct PA dictionary for type" (ppr ty)
223 paDFunType :: TyCon -> VM Type
226 margs <- mapM paDictArgType tvs
227 res <- mkPADictType (mkTyConApp tc arg_tys)
228 return . mkForAllTys tvs
229 $ mkFunTys [arg | Just arg <- margs] res
232 arg_tys = mkTyVarTys tvs
234 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
236 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
237 return $ mkApps (mkTyApps dfun tys) dicts
240 paMethod :: (Builtins -> Var) -> String -> Type -> VM CoreExpr
242 | Just tycon <- splitPrimTyCon ty
244 . maybeCantVectoriseM "No PA method" (text name <+> text "for" <+> ppr tycon)
245 $ lookupPrimMethod tycon name
250 Just dict <- paDictOfType ty
251 return $ mkApps (Var fn) [Type ty, dict]
253 prDictOfType :: Type -> VM CoreExpr
254 prDictOfType ty = prDictOfTyApp ty_fn ty_args
256 (ty_fn, ty_args) = splitAppTys ty
258 prDictOfTyApp :: Type -> [Type] -> VM CoreExpr
259 prDictOfTyApp ty_fn ty_args
260 | Just ty_fn' <- coreView ty_fn = prDictOfTyApp ty_fn' ty_args
261 prDictOfTyApp (TyConApp tc _) ty_args
263 dfun <- liftM Var $ maybeV (lookupTyConPR tc)
264 prDFunApply dfun ty_args
265 prDictOfTyApp _ _ = noV
267 prDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
270 dicts <- mapM prDictOfType tys
271 return $ mkApps (mkTyApps dfun tys) dicts
273 wrapPR :: Type -> VM CoreExpr
276 Just pa_dict <- paDictOfType ty
277 pr_dfun <- prDFunOfTyCon =<< builtin wrapTyCon
278 return $ mkApps pr_dfun [Type ty, pa_dict]
280 replicatePD :: CoreExpr -> CoreExpr -> VM CoreExpr
281 replicatePD len x = liftM (`mkApps` [len,x])
282 (paMethod replicatePDVar "replicatePD" (exprType x))
284 emptyPD :: Type -> VM CoreExpr
285 emptyPD = paMethod emptyPDVar "emptyPD"
287 packByTagPD :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
289 packByTagPD ty xs len tags t
290 = liftM (`mkApps` [xs, len, tags, t])
291 (paMethod packByTagPDVar "packByTagPD" ty)
293 combinePD :: Type -> CoreExpr -> CoreExpr -> [CoreExpr]
295 combinePD ty len sel xs
296 = liftM (`mkApps` (len : sel : xs))
297 (paMethod (combinePDVar n) ("combine" ++ show n ++ "PD") ty)
301 -- | Like `replicatePD` but use the lifting context in the vectoriser state.
302 liftPD :: CoreExpr -> VM CoreExpr
305 lc <- builtin liftingContext
306 replicatePD (Var lc) x
308 zipScalars :: [Type] -> Type -> VM CoreExpr
309 zipScalars arg_tys res_ty
311 scalar <- builtin scalarClass
312 (dfuns, _) <- mapAndUnzipM (\ty -> lookupInst scalar [ty]) ty_args
313 zipf <- builtin (scalarZip $ length arg_tys)
314 return $ Var zipf `mkTyApps` ty_args `mkApps` map Var dfuns
316 ty_args = arg_tys ++ [res_ty]
318 scalarClosure :: [Type] -> Type -> CoreExpr -> CoreExpr -> VM CoreExpr
319 scalarClosure arg_tys res_ty scalar_fun array_fun
321 ctr <- builtin (closureCtrFun $ length arg_tys)
322 Just pas <- liftM sequence $ mapM paDictOfType (init arg_tys)
323 return $ Var ctr `mkTyApps` (arg_tys ++ [res_ty])
324 `mkApps` (pas ++ [scalar_fun, array_fun])
326 newLocalVVar :: FastString -> Type -> VM VVar
329 lty <- mkPDataType vty
330 vv <- newLocalVar fs vty
331 lv <- newLocalVar fs lty
334 polyAbstract :: [TyVar] -> ([Var] -> VM a) -> VM a
338 mdicts <- mapM mk_dict_var tvs
339 zipWithM_ (\tv -> maybe (defLocalTyVar tv)
340 (defLocalTyVarWithPA tv . Var)) tvs mdicts
344 r <- paDictArgType tv
346 Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty)
347 Nothing -> return Nothing
349 mk_args mdicts = [dict | Just dict <- mdicts]
351 polyArity :: [TyVar] -> VM Int
353 tys <- mapM paDictArgType tvs
354 return $ length [() | Just _ <- tys]
356 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
358 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
359 return $ expr `mkTyApps` tys `mkApps` dicts
361 polyVApply :: VExpr -> [Type] -> VM VExpr
363 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
364 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
366 -- Inline ---------------------------------------------------------------------
367 -- | Records whether we should inline a particular binding.
372 -- | Add to the arity contained within an `Inline`, if any.
373 addInlineArity :: Inline -> Int -> Inline
374 addInlineArity (Inline m) n = Inline (m+n)
375 addInlineArity DontInline _ = DontInline
377 -- | Says to always inline a binding.
382 -- Hoising --------------------------------------------------------------------
383 hoistBinding :: Var -> CoreExpr -> VM ()
384 hoistBinding v e = updGEnv $ \env ->
385 env { global_bindings = (v,e) : global_bindings env }
387 hoistExpr :: FastString -> CoreExpr -> Inline -> VM Var
388 hoistExpr fs expr inl
390 var <- mk_inline `liftM` newLocalVar fs (exprType expr)
391 hoistBinding var expr
394 mk_inline var = case inl of
395 Inline arity -> var `setIdUnfolding`
396 mkInlineRule expr (Just arity)
399 hoistVExpr :: VExpr -> Inline -> VM VVar
400 hoistVExpr (ve, le) inl
403 vv <- hoistExpr ('v' `consFS` fs) ve inl
404 lv <- hoistExpr ('l' `consFS` fs) le (addInlineArity inl 1)
407 hoistPolyVExpr :: [TyVar] -> Inline -> VM VExpr -> VM VExpr
408 hoistPolyVExpr tvs inline p
410 inline' <- liftM (addInlineArity inline) (polyArity tvs)
411 expr <- closedV . polyAbstract tvs $ \args ->
412 liftM (mapVect (mkLams $ tvs ++ args)) p
413 fn <- hoistVExpr expr inline'
414 polyVApply (vVar fn) (mkTyVarTys tvs)
416 takeHoisted :: VM [(Var, CoreExpr)]
420 setGEnv $ env { global_bindings = [] }
421 return $ global_bindings env
424 boxExpr :: Type -> VExpr -> VM VExpr
425 boxExpr ty (vexpr, lexpr)
426 | Just (tycon, []) <- splitTyConApp_maybe ty
427 , isUnLiftedTyCon tycon
429 r <- lookupBoxedTyCon tycon
431 Just tycon' -> let [dc] = tyConDataCons tycon'
433 return (mkConApp dc [vexpr], lexpr)
434 Nothing -> return (vexpr, lexpr)
437 -- Closures -------------------------------------------------------------------
438 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
439 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
440 = do Just dict <- paDictOfType env_ty
441 mkv <- builtin closureVar
442 mkl <- builtin liftedClosureVar
443 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
444 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
447 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
448 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
449 = do vapply <- builtin applyVar
450 lapply <- builtin liftedApplyVar
451 lc <- builtin liftingContext
452 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
453 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [Var lc, lclo, larg])
456 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
457 buildClosures _ _ [] _ mk_body
459 buildClosures tvs vars [arg_ty] res_ty mk_body
460 = -- liftM vInlineMe $
461 buildClosure tvs vars arg_ty res_ty mk_body
462 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
464 res_ty' <- mkClosureTypes arg_tys res_ty
465 arg <- newLocalVVar (fsLit "x") arg_ty
467 buildClosure tvs vars arg_ty res_ty'
468 . hoistPolyVExpr tvs (Inline (length vars + 1))
470 lc <- builtin liftingContext
471 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
472 return $ vLams lc (vars ++ [arg]) clo
474 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
476 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
477 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
479 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
480 buildClosure tvs vars arg_ty res_ty mk_body
482 (env_ty, env, bind) <- buildEnv vars
483 env_bndr <- newLocalVVar (fsLit "env") env_ty
484 arg_bndr <- newLocalVVar (fsLit "arg") arg_ty
486 fn <- hoistPolyVExpr tvs (Inline 2)
488 lc <- builtin liftingContext
490 return -- . vInlineMe
491 . vLams lc [env_bndr, arg_bndr]
492 $ bind (vVar env_bndr)
493 (vVarApps lc body (vars ++ [arg_bndr]))
495 mkClosure arg_ty res_ty env_ty fn env
498 -- Environments ---------------------------------------------------------------
499 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr)
502 void <- builtin voidVar
503 pvoid <- builtin pvoidVar
504 return (ty, vVar (void, pvoid), \_ body -> body)
506 buildEnv [v] = return (vVarType v, vVar v,
507 \env body -> vLet (vNonRec v env) body)
512 (lenv_tc, lenv_tyargs) <- pdataReprTyCon ty
514 let venv_con = tupleCon Boxed (length vs)
515 [lenv_con] = tyConDataCons lenv_tc
517 venv = mkCoreTup (map Var vvs)
518 lenv = Var (dataConWrapId lenv_con)
519 `mkTyApps` lenv_tyargs
522 vbind env body = mkWildCase env ty (exprType body)
523 [(DataAlt venv_con, vvs, body)]
526 let scrut = unwrapFamInstScrut lenv_tc lenv_tyargs env
528 mkWildCase scrut (exprType scrut) (exprType body)
529 [(DataAlt lenv_con, lvs, body)]
531 bind (venv, lenv) (vbody, lbody) = (vbind venv vbody,
534 return (ty, (venv, lenv), bind)
536 (vvs, lvs) = unzip vs
537 tys = map vVarType vs
538 ty = mkBoxedTupleTy tys