2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
4 dataConTagZ, mkDataConTag, mkDataConTagLit,
8 mkBuiltinCo, voidType, mkWrapType,
9 mkPADictType, mkPArrayType, mkPDataType, mkPReprType, mkPArray,
10 mkBuiltinTyConApps, mkClosureTypes,
12 pdataReprTyCon, pdataReprDataCon, mkVScrut,
13 prDictOfType, prDFunOfTyCon,
14 paDictArgType, paDictOfType, paDFunType,
15 paMethod, wrapPR, replicatePD, emptyPD, packByTagPD,
18 zipScalars, scalarClosure,
19 polyAbstract, polyApply, polyVApply, polyArity
21 import Vectorise.Monad
23 import Vectorise.Builtins
40 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
41 collectAnnTypeArgs expr = go expr []
43 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
46 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
47 collectAnnTypeBinders expr = go [] expr
49 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
50 go bs e = (reverse bs, e)
52 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
53 collectAnnValBinders expr = go [] expr
55 go bs (_, AnnLam b e) | isId b = go (b:bs) e
56 go bs e = (reverse bs, e)
58 isAnnTypeArg :: AnnExpr b ann -> Bool
59 isAnnTypeArg (_, AnnType _) = True
60 isAnnTypeArg _ = False
62 dataConTagZ :: DataCon -> Int
63 dataConTagZ con = dataConTag con - fIRST_TAG
65 mkDataConTagLit :: DataCon -> Literal
66 mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
68 mkDataConTag :: DataCon -> CoreExpr
69 mkDataConTag = mkIntLitInt . dataConTagZ
71 splitPrimTyCon :: Type -> Maybe TyCon
73 | Just (tycon, []) <- splitTyConApp_maybe ty
79 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
80 mkBuiltinTyConApp get_tc tys
83 return $ mkTyConApp tc tys
85 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
86 mkBuiltinTyConApps get_tc tys ty
89 return $ foldr (mk tc) ty tys
91 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
94 voidType = mkBuiltinTyConApp voidTyCon []
96 mkWrapType :: Type -> VM Type
97 mkWrapType ty = mkBuiltinTyConApp wrapTyCon [ty]
100 mkClosureTypes :: [Type] -> Type -> VM Type
101 mkClosureTypes = mkBuiltinTyConApps closureTyCon
103 mkPReprType :: Type -> VM Type
104 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
106 mkPADictType :: Type -> VM Type
107 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
109 mkPArrayType :: Type -> VM Type
111 | Just tycon <- splitPrimTyCon ty
113 r <- lookupPrimPArray tycon
115 Just arr -> return $ mkTyConApp arr []
116 Nothing -> cantVectorise "Primitive tycon not vectorised" (ppr tycon)
117 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
119 mkPDataType :: Type -> VM Type
120 mkPDataType ty = mkBuiltinTyConApp pdataTyCon [ty]
122 mkPArray :: Type -> CoreExpr -> CoreExpr -> VM CoreExpr
123 mkPArray ty len dat = do
124 tc <- builtin parrayTyCon
125 let [dc] = tyConDataCons tc
126 return $ mkConApp dc [Type ty, len, dat]
128 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
132 return $ mkTyConApp tc []
134 pdataReprTyCon :: Type -> VM (TyCon, [Type])
135 pdataReprTyCon ty = builtin pdataTyCon >>= (`lookupFamInst` [ty])
137 pdataReprDataCon :: Type -> VM (DataCon, [Type])
140 (tc, arg_tys) <- pdataReprTyCon ty
141 let [dc] = tyConDataCons tc
144 mkVScrut :: VExpr -> VM (CoreExpr, CoreExpr, TyCon, [Type])
147 (tc, arg_tys) <- pdataReprTyCon ty
148 return (ve, unwrapFamInstScrut tc arg_tys le, tc, arg_tys)
152 prDFunOfTyCon :: TyCon -> VM CoreExpr
155 . maybeCantVectoriseM "No PR dictionary for tycon" (ppr tycon)
156 $ lookupTyConPR tycon
159 paDictArgType :: TyVar -> VM (Maybe Type)
160 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
162 go ty k | Just k' <- kindView k = go ty k'
165 tv <- newTyVar (fsLit "a") k1
166 mty1 <- go (TyVarTy tv) k1
169 mty2 <- go (AppTy ty (TyVarTy tv)) k2
170 return $ fmap (ForAllTy tv . FunTy ty1) mty2
175 = liftM Just (mkPADictType ty)
177 go _ _ = return Nothing
180 -- | Get the PA dictionary for some type, or `Nothing` if there isn't one.
181 paDictOfType :: Type -> VM (Maybe CoreExpr)
183 = paDictOfTyApp ty_fn ty_args
185 (ty_fn, ty_args) = splitAppTys ty
187 paDictOfTyApp :: Type -> [Type] -> VM (Maybe CoreExpr)
188 paDictOfTyApp ty_fn ty_args
189 | Just ty_fn' <- coreView ty_fn
190 = paDictOfTyApp ty_fn' ty_args
192 paDictOfTyApp (TyVarTy tv) ty_args
193 = do dfun <- maybeV (lookupTyVarPA tv)
194 liftM Just $ paDFunApply dfun ty_args
196 paDictOfTyApp (TyConApp tc _) ty_args
197 = do mdfun <- lookupTyConPA tc
200 -> pprTrace "VectUtils.paDictOfType"
201 (vcat [ text "No PA dictionary"
202 , text "for tycon: " <> ppr tc
203 , text "in type: " <> ppr ty])
206 Just dfun -> liftM Just $ paDFunApply (Var dfun) ty_args
209 = cantVectorise "Can't construct PA dictionary for type" (ppr ty)
213 paDFunType :: TyCon -> VM Type
216 margs <- mapM paDictArgType tvs
217 res <- mkPADictType (mkTyConApp tc arg_tys)
218 return . mkForAllTys tvs
219 $ mkFunTys [arg | Just arg <- margs] res
222 arg_tys = mkTyVarTys tvs
224 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
226 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
227 return $ mkApps (mkTyApps dfun tys) dicts
230 paMethod :: (Builtins -> Var) -> String -> Type -> VM CoreExpr
232 | Just tycon <- splitPrimTyCon ty
234 . maybeCantVectoriseM "No PA method" (text name <+> text "for" <+> ppr tycon)
235 $ lookupPrimMethod tycon name
240 Just dict <- paDictOfType ty
241 return $ mkApps (Var fn) [Type ty, dict]
243 prDictOfType :: Type -> VM CoreExpr
244 prDictOfType ty = prDictOfTyApp ty_fn ty_args
246 (ty_fn, ty_args) = splitAppTys ty
248 prDictOfTyApp :: Type -> [Type] -> VM CoreExpr
249 prDictOfTyApp ty_fn ty_args
250 | Just ty_fn' <- coreView ty_fn = prDictOfTyApp ty_fn' ty_args
251 prDictOfTyApp (TyConApp tc _) ty_args
253 dfun <- liftM Var $ maybeV (lookupTyConPR tc)
254 prDFunApply dfun ty_args
255 prDictOfTyApp _ _ = noV
257 prDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
260 dicts <- mapM prDictOfType tys
261 return $ mkApps (mkTyApps dfun tys) dicts
263 wrapPR :: Type -> VM CoreExpr
266 Just pa_dict <- paDictOfType ty
267 pr_dfun <- prDFunOfTyCon =<< builtin wrapTyCon
268 return $ mkApps pr_dfun [Type ty, pa_dict]
270 replicatePD :: CoreExpr -> CoreExpr -> VM CoreExpr
271 replicatePD len x = liftM (`mkApps` [len,x])
272 (paMethod replicatePDVar "replicatePD" (exprType x))
274 emptyPD :: Type -> VM CoreExpr
275 emptyPD = paMethod emptyPDVar "emptyPD"
277 packByTagPD :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
279 packByTagPD ty xs len tags t
280 = liftM (`mkApps` [xs, len, tags, t])
281 (paMethod packByTagPDVar "packByTagPD" ty)
283 combinePD :: Type -> CoreExpr -> CoreExpr -> [CoreExpr]
285 combinePD ty len sel xs
286 = liftM (`mkApps` (len : sel : xs))
287 (paMethod (combinePDVar n) ("combine" ++ show n ++ "PD") ty)
291 -- | Like `replicatePD` but use the lifting context in the vectoriser state.
292 liftPD :: CoreExpr -> VM CoreExpr
295 lc <- builtin liftingContext
296 replicatePD (Var lc) x
298 zipScalars :: [Type] -> Type -> VM CoreExpr
299 zipScalars arg_tys res_ty
301 scalar <- builtin scalarClass
302 (dfuns, _) <- mapAndUnzipM (\ty -> lookupInst scalar [ty]) ty_args
303 zipf <- builtin (scalarZip $ length arg_tys)
304 return $ Var zipf `mkTyApps` ty_args `mkApps` map Var dfuns
306 ty_args = arg_tys ++ [res_ty]
308 scalarClosure :: [Type] -> Type -> CoreExpr -> CoreExpr -> VM CoreExpr
309 scalarClosure arg_tys res_ty scalar_fun array_fun
311 ctr <- builtin (closureCtrFun $ length arg_tys)
312 Just pas <- liftM sequence $ mapM paDictOfType (init arg_tys)
313 return $ Var ctr `mkTyApps` (arg_tys ++ [res_ty])
314 `mkApps` (pas ++ [scalar_fun, array_fun])
316 newLocalVVar :: FastString -> Type -> VM VVar
319 lty <- mkPDataType vty
320 vv <- newLocalVar fs vty
321 lv <- newLocalVar fs lty
324 polyAbstract :: [TyVar] -> ([Var] -> VM a) -> VM a
328 mdicts <- mapM mk_dict_var tvs
329 zipWithM_ (\tv -> maybe (defLocalTyVar tv)
330 (defLocalTyVarWithPA tv . Var)) tvs mdicts
334 r <- paDictArgType tv
336 Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty)
337 Nothing -> return Nothing
339 mk_args mdicts = [dict | Just dict <- mdicts]
341 polyArity :: [TyVar] -> VM Int
343 tys <- mapM paDictArgType tvs
344 return $ length [() | Just _ <- tys]
346 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
348 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
349 return $ expr `mkTyApps` tys `mkApps` dicts
351 polyVApply :: VExpr -> [Type] -> VM VExpr
353 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
354 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
358 boxExpr :: Type -> VExpr -> VM VExpr
359 boxExpr ty (vexpr, lexpr)
360 | Just (tycon, []) <- splitTyConApp_maybe ty
361 , isUnLiftedTyCon tycon
363 r <- lookupBoxedTyCon tycon
365 Just tycon' -> let [dc] = tyConDataCons tycon'
367 return (mkConApp dc [vexpr], lexpr)
368 Nothing -> return (vexpr, lexpr)