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 type PAMethod = (Builtins -> Var, String)
220 paMethod :: (Builtins -> Var) -> String -> Type -> VM CoreExpr
222 | Just tycon <- splitPrimTyCon ty
224 . maybeCantVectoriseM "No PA method" (text name <+> text "for" <+> ppr tycon)
225 $ lookupPrimMethod tycon name
230 dict <- paDictOfType ty
231 return $ mkApps (Var fn) [Type ty, dict]
233 mkPR :: Type -> VM CoreExpr
236 fn <- builtin mkPRVar
237 dict <- paDictOfType ty
238 return $ mkApps (Var fn) [Type ty, dict]
240 replicatePD :: CoreExpr -> CoreExpr -> VM CoreExpr
241 replicatePD len x = liftM (`mkApps` [len,x])
242 (paMethod replicatePDVar "replicatePD" (exprType x))
244 emptyPD :: Type -> VM CoreExpr
245 emptyPD = paMethod emptyPDVar "emptyPD"
247 packPD :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr
248 packPD ty xs len sel = liftM (`mkApps` [xs, len, sel])
249 (paMethod packPDVar "packPD" ty)
251 combinePD :: Type -> CoreExpr -> CoreExpr -> [CoreExpr]
253 combinePD ty len sel xs
254 = liftM (`mkApps` (len : sel : xs))
255 (paMethod (combinePDVar n) ("combine" ++ show n ++ "PD") ty)
259 liftPD :: CoreExpr -> VM CoreExpr
262 lc <- builtin liftingContext
263 replicatePD (Var lc) x
265 zipScalars :: [Type] -> Type -> VM CoreExpr
266 zipScalars arg_tys res_ty
268 scalar <- builtin scalarClass
269 (dfuns, _) <- mapAndUnzipM (\ty -> lookupInst scalar [ty]) ty_args
270 zipf <- builtin (scalarZip $ length arg_tys)
271 return $ Var zipf `mkTyApps` ty_args `mkApps` map Var dfuns
273 ty_args = arg_tys ++ [res_ty]
275 scalarClosure :: [Type] -> Type -> CoreExpr -> CoreExpr -> VM CoreExpr
276 scalarClosure arg_tys res_ty scalar_fun array_fun
278 ctr <- builtin (closureCtrFun $ length arg_tys)
279 pas <- mapM paDictOfType (init arg_tys)
280 return $ Var ctr `mkTyApps` (arg_tys ++ [res_ty])
281 `mkApps` (pas ++ [scalar_fun, array_fun])
283 newLocalVVar :: FastString -> Type -> VM VVar
286 lty <- mkPDataType vty
287 vv <- newLocalVar fs vty
288 lv <- newLocalVar fs lty
291 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
295 mdicts <- mapM mk_dict_var tvs
296 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
300 r <- paDictArgType tv
302 Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty)
303 Nothing -> return Nothing
305 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
307 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
310 dicts <- mapM paDictOfType tys
311 return $ expr `mkTyApps` tys `mkApps` dicts
313 polyVApply :: VExpr -> [Type] -> VM VExpr
316 dicts <- mapM paDictOfType tys
317 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
319 hoistBinding :: Var -> CoreExpr -> VM ()
320 hoistBinding v e = updGEnv $ \env ->
321 env { global_bindings = (v,e) : global_bindings env }
323 hoistExpr :: FastString -> CoreExpr -> VM Var
326 var <- newLocalVar fs (exprType expr)
327 hoistBinding var expr
330 hoistVExpr :: VExpr -> VM VVar
334 vv <- hoistExpr ('v' `consFS` fs) ve
335 lv <- hoistExpr ('l' `consFS` fs) le
338 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
341 expr <- closedV . polyAbstract tvs $ \abstract ->
342 liftM (mapVect abstract) p
343 fn <- hoistVExpr expr
344 polyVApply (vVar fn) (mkTyVarTys tvs)
346 takeHoisted :: VM [(Var, CoreExpr)]
350 setGEnv $ env { global_bindings = [] }
351 return $ global_bindings env
354 boxExpr :: Type -> VExpr -> VM VExpr
355 boxExpr ty (vexpr, lexpr)
356 | Just (tycon, []) <- splitTyConApp_maybe ty
357 , isUnLiftedTyCon tycon
359 r <- lookupBoxedTyCon tycon
361 Just tycon' -> let [dc] = tyConDataCons tycon'
363 return (mkConApp dc [vexpr], lexpr)
364 Nothing -> return (vexpr, lexpr)
367 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
368 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
370 dict <- paDictOfType env_ty
371 mkv <- builtin closureVar
372 mkl <- builtin liftedClosureVar
373 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
374 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
376 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
377 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
379 vapply <- builtin applyVar
380 lapply <- builtin liftedApplyVar
381 lc <- builtin liftingContext
382 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
383 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [Var lc, lclo, larg])
385 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
386 buildClosures _ _ [] _ mk_body
388 buildClosures tvs vars [arg_ty] res_ty mk_body
389 = liftM vInlineMe (buildClosure tvs vars arg_ty res_ty mk_body)
390 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
392 res_ty' <- mkClosureTypes arg_tys res_ty
393 arg <- newLocalVVar (fsLit "x") arg_ty
395 . buildClosure tvs vars arg_ty res_ty'
398 lc <- builtin liftingContext
399 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
400 return $ vLams lc (vars ++ [arg]) clo
402 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
404 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
405 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
407 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
408 buildClosure tvs vars arg_ty res_ty mk_body
410 (env_ty, env, bind) <- buildEnv vars
411 env_bndr <- newLocalVVar (fsLit "env") env_ty
412 arg_bndr <- newLocalVVar (fsLit "arg") arg_ty
414 fn <- hoistPolyVExpr tvs
416 lc <- builtin liftingContext
419 . vLams lc [env_bndr, arg_bndr]
420 $ bind (vVar env_bndr)
421 (vVarApps lc body (vars ++ [arg_bndr]))
423 mkClosure arg_ty res_ty env_ty fn env
425 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr)
428 void <- builtin voidVar
429 pvoid <- builtin pvoidVar
430 return (ty, vVar (void, pvoid), \_ body -> body)
432 buildEnv [v] = return (vVarType v, vVar v,
433 \env body -> vLet (vNonRec v env) body)
438 (lenv_tc, lenv_tyargs) <- pdataReprTyCon ty
440 let venv_con = tupleCon Boxed (length vs)
441 [lenv_con] = tyConDataCons lenv_tc
443 venv = mkCoreTup (map Var vvs)
444 lenv = Var (dataConWrapId lenv_con)
445 `mkTyApps` lenv_tyargs
448 vbind env body = mkWildCase venv ty (exprType body)
449 [(DataAlt venv_con, vvs, body)]
452 let scrut = unwrapFamInstScrut lenv_tc lenv_tyargs lenv
454 mkWildCase scrut (exprType scrut) (exprType body)
455 [(DataAlt lenv_con, lvs, body)]
457 bind (venv, lenv) (vbody, lbody) = (vbind venv vbody,
460 return (ty, (venv, lenv), bind)
462 (vvs, lvs) = unzip vs
463 tys = map vVarType vs