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, packPD,
17 zipScalars, scalarClosure,
18 polyAbstract, polyApply, polyVApply,
19 hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
20 buildClosure, buildClosures,
27 import MkCore ( mkCoreTup, mkCoreTupTy, mkWildCase )
36 import MkId ( unwrapFamInstScrut )
38 import BasicTypes ( Boxity(..) )
39 import Literal ( Literal, mkMachInt )
47 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
48 collectAnnTypeArgs expr = go expr []
50 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
53 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
54 collectAnnTypeBinders expr = go [] expr
56 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
57 go bs e = (reverse bs, e)
59 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
60 collectAnnValBinders expr = go [] expr
62 go bs (_, AnnLam b e) | isId b = go (b:bs) e
63 go bs e = (reverse bs, e)
65 isAnnTypeArg :: AnnExpr b ann -> Bool
66 isAnnTypeArg (_, AnnType _) = True
67 isAnnTypeArg _ = False
69 dataConTagZ :: DataCon -> Int
70 dataConTagZ con = dataConTag con - fIRST_TAG
72 mkDataConTagLit :: DataCon -> Literal
73 mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
75 mkDataConTag :: DataCon -> CoreExpr
76 mkDataConTag = mkIntLitInt . dataConTagZ
78 splitPrimTyCon :: Type -> Maybe TyCon
80 | Just (tycon, []) <- splitTyConApp_maybe ty
86 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
87 mkBuiltinTyConApp get_tc tys
90 return $ mkTyConApp tc tys
92 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
93 mkBuiltinTyConApps get_tc tys ty
96 return $ foldr (mk tc) ty tys
98 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
101 voidType = mkBuiltinTyConApp VectMonad.voidTyCon []
103 mkWrapType :: Type -> VM Type
104 mkWrapType ty = mkBuiltinTyConApp wrapTyCon [ty]
106 mkClosureTypes :: [Type] -> Type -> VM Type
107 mkClosureTypes = mkBuiltinTyConApps closureTyCon
109 mkPReprType :: Type -> VM Type
110 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
112 mkPADictType :: Type -> VM Type
113 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
115 mkPArrayType :: Type -> VM Type
117 | Just tycon <- splitPrimTyCon ty
119 r <- lookupPrimPArray tycon
121 Just arr -> return $ mkTyConApp arr []
122 Nothing -> cantVectorise "Primitive tycon not vectorised" (ppr tycon)
123 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
125 mkPDataType :: Type -> VM Type
126 mkPDataType ty = mkBuiltinTyConApp pdataTyCon [ty]
128 mkPArray :: Type -> CoreExpr -> CoreExpr -> VM CoreExpr
129 mkPArray ty len dat = do
130 tc <- builtin parrayTyCon
131 let [dc] = tyConDataCons tc
132 return $ mkConApp dc [Type ty, len, dat]
134 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
138 return $ mkTyConApp tc []
140 pdataReprTyCon :: Type -> VM (TyCon, [Type])
141 pdataReprTyCon ty = builtin pdataTyCon >>= (`lookupFamInst` [ty])
143 pdataReprDataCon :: Type -> VM (DataCon, [Type])
146 (tc, arg_tys) <- pdataReprTyCon ty
147 let [dc] = tyConDataCons tc
150 mkVScrut :: VExpr -> VM (CoreExpr, CoreExpr, TyCon, [Type])
153 (tc, arg_tys) <- pdataReprTyCon ty
154 return (ve, unwrapFamInstScrut tc arg_tys le, tc, arg_tys)
158 prDFunOfTyCon :: TyCon -> VM CoreExpr
161 . maybeCantVectoriseM "No PR dictionary for tycon" (ppr tycon)
162 $ lookupTyConPR tycon
164 paDictArgType :: TyVar -> VM (Maybe Type)
165 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
167 go ty k | Just k' <- kindView k = go ty k'
170 tv <- newTyVar (fsLit "a") k1
171 mty1 <- go (TyVarTy tv) k1
174 mty2 <- go (AppTy ty (TyVarTy tv)) k2
175 return $ fmap (ForAllTy tv . FunTy ty1) mty2
180 = liftM Just (mkPADictType ty)
182 go _ _ = return Nothing
184 paDictOfType :: Type -> VM CoreExpr
185 paDictOfType ty = paDictOfTyApp ty_fn ty_args
187 (ty_fn, ty_args) = splitAppTys ty
189 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
190 paDictOfTyApp ty_fn ty_args
191 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
192 paDictOfTyApp (TyVarTy tv) ty_args
194 dfun <- maybeV (lookupTyVarPA tv)
195 paDFunApply dfun ty_args
196 paDictOfTyApp (TyConApp tc _) ty_args
198 dfun <- maybeCantVectoriseM "No PA dictionary for tycon" (ppr tc)
200 paDFunApply (Var dfun) ty_args
202 = cantVectorise "Can't construct PA dictionary for type" (ppr ty)
204 paDFunType :: TyCon -> VM Type
207 margs <- mapM paDictArgType tvs
208 res <- mkPADictType (mkTyConApp tc arg_tys)
209 return . mkForAllTys tvs
210 $ mkFunTys [arg | Just arg <- margs] res
213 arg_tys = mkTyVarTys tvs
215 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
218 dicts <- mapM paDictOfType tys
219 return $ mkApps (mkTyApps dfun tys) dicts
221 paMethod :: (Builtins -> Var) -> String -> Type -> VM CoreExpr
223 | Just tycon <- splitPrimTyCon ty
225 . maybeCantVectoriseM "No PA method" (text name <+> text "for" <+> ppr tycon)
226 $ lookupPrimMethod tycon name
231 dict <- paDictOfType ty
232 return $ mkApps (Var fn) [Type ty, dict]
234 prDictOfType :: Type -> VM CoreExpr
235 prDictOfType ty = prDictOfTyApp ty_fn ty_args
237 (ty_fn, ty_args) = splitAppTys ty
239 prDictOfTyApp :: Type -> [Type] -> VM CoreExpr
240 prDictOfTyApp ty_fn ty_args
241 | Just ty_fn' <- coreView ty_fn = prDictOfTyApp ty_fn' ty_args
242 prDictOfTyApp (TyConApp tc _) ty_args
244 dfun <- prDFunOfTyCon tc
245 prDFunApply dfun ty_args
246 prDictOfTyApp _ _ = noV
248 prDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
251 dicts <- mapM prDictOfType tys
252 return $ mkApps (mkTyApps dfun tys) dicts
254 wrapPR :: Type -> VM CoreExpr
257 pa_dict <- paDictOfType ty
258 pr_dfun <- prDFunOfTyCon =<< builtin wrapTyCon
259 return $ mkApps pr_dfun [Type ty, pa_dict]
261 replicatePD :: CoreExpr -> CoreExpr -> VM CoreExpr
262 replicatePD len x = liftM (`mkApps` [len,x])
263 (paMethod replicatePDVar "replicatePD" (exprType x))
265 emptyPD :: Type -> VM CoreExpr
266 emptyPD = paMethod emptyPDVar "emptyPD"
268 packPD :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr
269 packPD ty xs len sel = liftM (`mkApps` [xs, len, sel])
270 (paMethod packPDVar "packPD" ty)
272 combinePD :: Type -> CoreExpr -> CoreExpr -> [CoreExpr]
274 combinePD ty len sel xs
275 = liftM (`mkApps` (len : sel : xs))
276 (paMethod (combinePDVar n) ("combine" ++ show n ++ "PD") ty)
280 liftPD :: CoreExpr -> VM CoreExpr
283 lc <- builtin liftingContext
284 replicatePD (Var lc) x
286 zipScalars :: [Type] -> Type -> VM CoreExpr
287 zipScalars arg_tys res_ty
289 scalar <- builtin scalarClass
290 (dfuns, _) <- mapAndUnzipM (\ty -> lookupInst scalar [ty]) ty_args
291 zipf <- builtin (scalarZip $ length arg_tys)
292 return $ Var zipf `mkTyApps` ty_args `mkApps` map Var dfuns
294 ty_args = arg_tys ++ [res_ty]
296 scalarClosure :: [Type] -> Type -> CoreExpr -> CoreExpr -> VM CoreExpr
297 scalarClosure arg_tys res_ty scalar_fun array_fun
299 ctr <- builtin (closureCtrFun $ length arg_tys)
300 pas <- mapM paDictOfType (init arg_tys)
301 return $ Var ctr `mkTyApps` (arg_tys ++ [res_ty])
302 `mkApps` (pas ++ [scalar_fun, array_fun])
304 newLocalVVar :: FastString -> Type -> VM VVar
307 lty <- mkPDataType vty
308 vv <- newLocalVar fs vty
309 lv <- newLocalVar fs lty
312 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
316 mdicts <- mapM mk_dict_var tvs
317 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
321 r <- paDictArgType tv
323 Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty)
324 Nothing -> return Nothing
326 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
328 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
331 dicts <- mapM paDictOfType tys
332 return $ expr `mkTyApps` tys `mkApps` dicts
334 polyVApply :: VExpr -> [Type] -> VM VExpr
337 dicts <- mapM paDictOfType tys
338 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
340 hoistBinding :: Var -> CoreExpr -> VM ()
341 hoistBinding v e = updGEnv $ \env ->
342 env { global_bindings = (v,e) : global_bindings env }
344 hoistExpr :: FastString -> CoreExpr -> VM Var
347 var <- newLocalVar fs (exprType expr)
348 hoistBinding var expr
351 hoistVExpr :: VExpr -> VM VVar
355 vv <- hoistExpr ('v' `consFS` fs) ve
356 lv <- hoistExpr ('l' `consFS` fs) le
359 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
362 expr <- closedV . polyAbstract tvs $ \abstract ->
363 liftM (mapVect abstract) p
364 fn <- hoistVExpr expr
365 polyVApply (vVar fn) (mkTyVarTys tvs)
367 takeHoisted :: VM [(Var, CoreExpr)]
371 setGEnv $ env { global_bindings = [] }
372 return $ global_bindings env
375 boxExpr :: Type -> VExpr -> VM VExpr
376 boxExpr ty (vexpr, lexpr)
377 | Just (tycon, []) <- splitTyConApp_maybe ty
378 , isUnLiftedTyCon tycon
380 r <- lookupBoxedTyCon tycon
382 Just tycon' -> let [dc] = tyConDataCons tycon'
384 return (mkConApp dc [vexpr], lexpr)
385 Nothing -> return (vexpr, lexpr)
388 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
389 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
391 dict <- paDictOfType env_ty
392 mkv <- builtin closureVar
393 mkl <- builtin liftedClosureVar
394 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
395 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
397 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
398 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
400 vapply <- builtin applyVar
401 lapply <- builtin liftedApplyVar
402 lc <- builtin liftingContext
403 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
404 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [Var lc, lclo, larg])
406 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
407 buildClosures _ _ [] _ mk_body
409 buildClosures tvs vars [arg_ty] res_ty mk_body
410 = liftM vInlineMe (buildClosure tvs vars arg_ty res_ty mk_body)
411 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
413 res_ty' <- mkClosureTypes arg_tys res_ty
414 arg <- newLocalVVar (fsLit "x") arg_ty
416 . buildClosure tvs vars arg_ty res_ty'
419 lc <- builtin liftingContext
420 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
421 return $ vLams lc (vars ++ [arg]) clo
423 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
425 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
426 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
428 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
429 buildClosure tvs vars arg_ty res_ty mk_body
431 (env_ty, env, bind) <- buildEnv vars
432 env_bndr <- newLocalVVar (fsLit "env") env_ty
433 arg_bndr <- newLocalVVar (fsLit "arg") arg_ty
435 fn <- hoistPolyVExpr tvs
437 lc <- builtin liftingContext
440 . vLams lc [env_bndr, arg_bndr]
441 $ bind (vVar env_bndr)
442 (vVarApps lc body (vars ++ [arg_bndr]))
444 mkClosure arg_ty res_ty env_ty fn env
446 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr)
449 void <- builtin voidVar
450 pvoid <- builtin pvoidVar
451 return (ty, vVar (void, pvoid), \_ body -> body)
453 buildEnv [v] = return (vVarType v, vVar v,
454 \env body -> vLet (vNonRec v env) body)
459 (lenv_tc, lenv_tyargs) <- pdataReprTyCon ty
461 let venv_con = tupleCon Boxed (length vs)
462 [lenv_con] = tyConDataCons lenv_tc
464 venv = mkCoreTup (map Var vvs)
465 lenv = Var (dataConWrapId lenv_con)
466 `mkTyApps` lenv_tyargs
469 vbind env body = mkWildCase env ty (exprType body)
470 [(DataAlt venv_con, vvs, body)]
473 let scrut = unwrapFamInstScrut lenv_tc lenv_tyargs env
475 mkWildCase scrut (exprType scrut) (exprType body)
476 [(DataAlt lenv_con, lvs, body)]
478 bind (venv, lenv) (vbody, lbody) = (vbind venv vbody,
481 return (ty, (venv, lenv), bind)
483 (vvs, lvs) = unzip vs
484 tys = map vVarType vs