2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
4 dataConTagZ, mkDataConTag, mkDataConTagLit,
9 mkPADictType, mkPArrayType, mkPDataType, mkPReprType, mkPArray,
11 pdataReprTyCon, pdataReprDataCon, mkVScrut,
13 paDictArgType, paDictOfType, paDFunType,
14 paMethod, mkPR, 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 voidTyCon []
103 mkClosureTypes :: [Type] -> Type -> VM Type
104 mkClosureTypes = mkBuiltinTyConApps closureTyCon
106 mkPReprType :: Type -> VM Type
107 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
109 mkPADictType :: Type -> VM Type
110 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
112 mkPArrayType :: Type -> VM Type
114 | Just tycon <- splitPrimTyCon ty
116 r <- lookupPrimPArray tycon
118 Just arr -> return $ mkTyConApp arr []
119 Nothing -> cantVectorise "Primitive tycon not vectorised" (ppr tycon)
120 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
122 mkPDataType :: Type -> VM Type
123 mkPDataType ty = mkBuiltinTyConApp pdataTyCon [ty]
125 mkPArray :: Type -> CoreExpr -> CoreExpr -> VM CoreExpr
126 mkPArray ty len dat = do
127 tc <- builtin parrayTyCon
128 let [dc] = tyConDataCons tc
129 return $ mkConApp dc [Type ty, len, dat]
131 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
135 return $ mkTyConApp tc []
137 pdataReprTyCon :: Type -> VM (TyCon, [Type])
138 pdataReprTyCon ty = builtin pdataTyCon >>= (`lookupFamInst` [ty])
140 pdataReprDataCon :: Type -> VM (DataCon, [Type])
143 (tc, arg_tys) <- pdataReprTyCon ty
144 let [dc] = tyConDataCons tc
147 mkVScrut :: VExpr -> VM (CoreExpr, CoreExpr, TyCon, [Type])
150 (tc, arg_tys) <- pdataReprTyCon ty
151 return (ve, unwrapFamInstScrut tc arg_tys le, tc, arg_tys)
155 prDFunOfTyCon :: TyCon -> VM CoreExpr
158 . maybeCantVectoriseM "No PR dictionary for tycon" (ppr tycon)
159 $ lookupTyConPR tycon
161 paDictArgType :: TyVar -> VM (Maybe Type)
162 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
164 go ty k | Just k' <- kindView k = go ty k'
167 tv <- newTyVar (fsLit "a") k1
168 mty1 <- go (TyVarTy tv) k1
171 mty2 <- go (AppTy ty (TyVarTy tv)) k2
172 return $ fmap (ForAllTy tv . FunTy ty1) mty2
177 = liftM Just (mkPADictType ty)
179 go _ _ = return Nothing
181 paDictOfType :: Type -> VM CoreExpr
182 paDictOfType ty = paDictOfTyApp ty_fn ty_args
184 (ty_fn, ty_args) = splitAppTys ty
186 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
187 paDictOfTyApp ty_fn ty_args
188 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
189 paDictOfTyApp (TyVarTy tv) ty_args
191 dfun <- maybeV (lookupTyVarPA tv)
192 paDFunApply dfun ty_args
193 paDictOfTyApp (TyConApp tc _) ty_args
195 dfun <- maybeCantVectoriseM "No PA dictionary for tycon" (ppr tc)
197 paDFunApply (Var dfun) ty_args
199 = cantVectorise "Can't construct PA dictionary for type" (ppr ty)
201 paDFunType :: TyCon -> VM Type
204 margs <- mapM paDictArgType tvs
205 res <- mkPADictType (mkTyConApp tc arg_tys)
206 return . mkForAllTys tvs
207 $ mkFunTys [arg | Just arg <- margs] res
210 arg_tys = mkTyVarTys tvs
212 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
215 dicts <- mapM paDictOfType tys
216 return $ mkApps (mkTyApps dfun tys) dicts
218 paMethod :: (Builtins -> Var) -> String -> Type -> VM CoreExpr
220 | Just tycon <- splitPrimTyCon ty
222 . maybeCantVectoriseM "No PA method" (text name <+> text "for" <+> ppr tycon)
223 $ lookupPrimMethod tycon name
228 dict <- paDictOfType ty
229 return $ mkApps (Var fn) [Type ty, dict]
231 mkPR :: Type -> VM CoreExpr
234 fn <- builtin mkPRVar
235 dict <- paDictOfType ty
236 return $ mkApps (Var fn) [Type ty, dict]
238 replicatePD :: CoreExpr -> CoreExpr -> VM CoreExpr
239 replicatePD len x = liftM (`mkApps` [len,x])
240 (paMethod replicatePDVar "replicatePD" (exprType x))
242 emptyPD :: Type -> VM CoreExpr
243 emptyPD = paMethod emptyPDVar "emptyPD"
245 packPD :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr
246 packPD ty xs len sel = liftM (`mkApps` [xs, len, sel])
247 (paMethod packPDVar "packPD" ty)
249 combinePD :: Type -> CoreExpr -> CoreExpr -> [CoreExpr]
251 combinePD ty len sel xs
252 = liftM (`mkApps` (len : sel : xs))
253 (paMethod (combinePDVar n) ("combine" ++ show n ++ "PD") ty)
257 liftPD :: CoreExpr -> VM CoreExpr
260 lc <- builtin liftingContext
261 replicatePD (Var lc) x
263 zipScalars :: [Type] -> Type -> VM CoreExpr
264 zipScalars arg_tys res_ty
266 scalar <- builtin scalarClass
267 (dfuns, _) <- mapAndUnzipM (\ty -> lookupInst scalar [ty]) ty_args
268 zipf <- builtin (scalarZip $ length arg_tys)
269 return $ Var zipf `mkTyApps` ty_args `mkApps` map Var dfuns
271 ty_args = arg_tys ++ [res_ty]
273 scalarClosure :: [Type] -> Type -> CoreExpr -> CoreExpr -> VM CoreExpr
274 scalarClosure arg_tys res_ty scalar_fun array_fun
276 ctr <- builtin (closureCtrFun $ length arg_tys)
277 pas <- mapM paDictOfType (init arg_tys)
278 return $ Var ctr `mkTyApps` (arg_tys ++ [res_ty])
279 `mkApps` (pas ++ [scalar_fun, array_fun])
281 newLocalVVar :: FastString -> Type -> VM VVar
284 lty <- mkPDataType vty
285 vv <- newLocalVar fs vty
286 lv <- newLocalVar fs lty
289 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
293 mdicts <- mapM mk_dict_var tvs
294 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
298 r <- paDictArgType tv
300 Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty)
301 Nothing -> return Nothing
303 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
305 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
308 dicts <- mapM paDictOfType tys
309 return $ expr `mkTyApps` tys `mkApps` dicts
311 polyVApply :: VExpr -> [Type] -> VM VExpr
314 dicts <- mapM paDictOfType tys
315 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
317 hoistBinding :: Var -> CoreExpr -> VM ()
318 hoistBinding v e = updGEnv $ \env ->
319 env { global_bindings = (v,e) : global_bindings env }
321 hoistExpr :: FastString -> CoreExpr -> VM Var
324 var <- newLocalVar fs (exprType expr)
325 hoistBinding var expr
328 hoistVExpr :: VExpr -> VM VVar
332 vv <- hoistExpr ('v' `consFS` fs) ve
333 lv <- hoistExpr ('l' `consFS` fs) le
336 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
339 expr <- closedV . polyAbstract tvs $ \abstract ->
340 liftM (mapVect abstract) p
341 fn <- hoistVExpr expr
342 polyVApply (vVar fn) (mkTyVarTys tvs)
344 takeHoisted :: VM [(Var, CoreExpr)]
348 setGEnv $ env { global_bindings = [] }
349 return $ global_bindings env
352 boxExpr :: Type -> VExpr -> VM VExpr
353 boxExpr ty (vexpr, lexpr)
354 | Just (tycon, []) <- splitTyConApp_maybe ty
355 , isUnLiftedTyCon tycon
357 r <- lookupBoxedTyCon tycon
359 Just tycon' -> let [dc] = tyConDataCons tycon'
361 return (mkConApp dc [vexpr], lexpr)
362 Nothing -> return (vexpr, lexpr)
365 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
366 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
368 dict <- paDictOfType env_ty
369 mkv <- builtin closureVar
370 mkl <- builtin liftedClosureVar
371 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
372 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
374 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
375 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
377 vapply <- builtin applyVar
378 lapply <- builtin liftedApplyVar
379 lc <- builtin liftingContext
380 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
381 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [Var lc, lclo, larg])
383 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
384 buildClosures _ _ [] _ mk_body
386 buildClosures tvs vars [arg_ty] res_ty mk_body
387 = liftM vInlineMe (buildClosure tvs vars arg_ty res_ty mk_body)
388 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
390 res_ty' <- mkClosureTypes arg_tys res_ty
391 arg <- newLocalVVar (fsLit "x") arg_ty
393 . buildClosure tvs vars arg_ty res_ty'
396 lc <- builtin liftingContext
397 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
398 return $ vLams lc (vars ++ [arg]) clo
400 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
402 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
403 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
405 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
406 buildClosure tvs vars arg_ty res_ty mk_body
408 (env_ty, env, bind) <- buildEnv vars
409 env_bndr <- newLocalVVar (fsLit "env") env_ty
410 arg_bndr <- newLocalVVar (fsLit "arg") arg_ty
412 fn <- hoistPolyVExpr tvs
414 lc <- builtin liftingContext
417 . vLams lc [env_bndr, arg_bndr]
418 $ bind (vVar env_bndr)
419 (vVarApps lc body (vars ++ [arg_bndr]))
421 mkClosure arg_ty res_ty env_ty fn env
423 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr)
426 void <- builtin voidVar
427 pvoid <- builtin pvoidVar
428 return (ty, vVar (void, pvoid), \_ body -> body)
430 buildEnv [v] = return (vVarType v, vVar v,
431 \env body -> vLet (vNonRec v env) body)
436 (lenv_tc, lenv_tyargs) <- pdataReprTyCon ty
438 let venv_con = tupleCon Boxed (length vs)
439 [lenv_con] = tyConDataCons lenv_tc
441 venv = mkCoreTup (map Var vvs)
442 lenv = Var (dataConWrapId lenv_con)
443 `mkTyApps` lenv_tyargs
446 vbind env body = mkWildCase env ty (exprType body)
447 [(DataAlt venv_con, vvs, body)]
450 let scrut = unwrapFamInstScrut lenv_tc lenv_tyargs env
452 mkWildCase scrut (exprType scrut) (exprType body)
453 [(DataAlt lenv_con, lvs, body)]
455 bind (venv, lenv) (vbody, lbody) = (vbind venv vbody,
458 return (ty, (venv, lenv), bind)
460 (vvs, lvs) = unzip vs
461 tys = map vVarType vs