X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Fvectorise%2FVectUtils.hs;h=0f101bd3cb11134be62a94e00728f2bff3288fba;hb=a52f14894e48d47e62b5b33f7d7f4b3f2cc88a79;hp=eec57d7538ff16af914726225d10723d7991d990;hpb=5eec4625961ca9064216f0161288e0d46628c10f;p=ghc-hetmet.git diff --git a/compiler/vectorise/VectUtils.hs b/compiler/vectorise/VectUtils.hs index eec57d7..0f101bd 100644 --- a/compiler/vectorise/VectUtils.hs +++ b/compiler/vectorise/VectUtils.hs @@ -3,8 +3,10 @@ module VectUtils ( collectAnnValBinders, mkDataConTag, splitClosureTy, - mkPADictType, mkPArrayType, - parrayReprTyCon, parrayReprDataCon, + mkPRepr, mkToPRepr, mkFromPRepr, + mkPADictType, mkPArrayType, mkPReprType, + parrayReprTyCon, parrayReprDataCon, mkVScrut, + prDictOfType, prCoerce, paDictArgType, paDictOfType, paDFunType, paMethod, lengthPA, replicatePA, emptyPA, liftPA, polyAbstract, polyApply, polyVApply, @@ -21,6 +23,7 @@ import VectMonad import DsUtils import CoreSyn import CoreUtils +import Coercion import Type import TypeRep import TyCon @@ -28,6 +31,7 @@ import DataCon ( DataCon, dataConWrapId, dataConTag ) import Var import Id ( mkWildId ) import MkId ( unwrapFamInstScrut ) +import Name ( Name ) import PrelNames import TysWiredIn import BasicTypes ( Boxity(..) ) @@ -35,7 +39,8 @@ import BasicTypes ( Boxity(..) ) import Outputable import FastString -import Control.Monad ( liftM, zipWithM_ ) +import Data.List ( zipWith4 ) +import Control.Monad ( liftM, liftM2, zipWithM_ ) collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type]) collectAnnTypeArgs expr = go expr [] @@ -62,53 +67,174 @@ isAnnTypeArg _ = False mkDataConTag :: DataCon -> CoreExpr mkDataConTag dc = mkConApp intDataCon [mkIntLitInt $ dataConTag dc] -isClosureTyCon :: TyCon -> Bool -isClosureTyCon tc = tyConName tc == closureTyConName +splitUnTy :: String -> Name -> Type -> Type +splitUnTy s name ty + | Just (tc, [ty']) <- splitTyConApp_maybe ty + , tyConName tc == name + = ty' -splitClosureTy :: Type -> (Type, Type) -splitClosureTy ty - | Just (tc, [arg_ty, res_ty]) <- splitTyConApp_maybe ty - , isClosureTyCon tc - = (arg_ty, res_ty) + | otherwise = pprPanic s (ppr ty) + +splitBinTy :: String -> Name -> Type -> (Type, Type) +splitBinTy s name ty + | Just (tc, [ty1, ty2]) <- splitTyConApp_maybe ty + , tyConName tc == name + = (ty1, ty2) + + | otherwise = pprPanic s (ppr ty) - | otherwise = pprPanic "splitClosureTy" (ppr ty) +splitFixedTyConApp :: TyCon -> Type -> [Type] +splitFixedTyConApp tc ty + | Just (tc', tys) <- splitTyConApp_maybe ty + , tc == tc' + = tys -isPArrayTyCon :: TyCon -> Bool -isPArrayTyCon tc = tyConName tc == parrayTyConName + | otherwise = pprPanic "splitFixedTyConApp" (ppr tc <+> ppr ty) + +splitEmbedTy :: Type -> Type +splitEmbedTy = splitUnTy "splitEmbedTy" embedTyConName + +splitClosureTy :: Type -> (Type, Type) +splitClosureTy = splitBinTy "splitClosureTy" closureTyConName splitPArrayTy :: Type -> Type -splitPArrayTy ty - | Just (tc, [arg_ty]) <- splitTyConApp_maybe ty - , isPArrayTyCon tc - = arg_ty +splitPArrayTy = splitUnTy "splitPArrayTy" parrayTyConName - | otherwise = pprPanic "splitPArrayTy" (ppr ty) +mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type +mkBuiltinTyConApp get_tc tys + = do + tc <- builtin get_tc + return $ mkTyConApp tc tys -mkClosureType :: Type -> Type -> VM Type -mkClosureType arg_ty res_ty +mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type +mkBuiltinTyConApps get_tc tys ty = do - tc <- builtin closureTyCon - return $ mkTyConApp tc [arg_ty, res_ty] + tc <- builtin get_tc + return $ foldr (mk tc) ty tys + where + mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2] -mkClosureTypes :: [Type] -> Type -> VM Type -mkClosureTypes arg_tys res_ty +mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type +mkBuiltinTyConApps1 get_tc dft [] = return dft +mkBuiltinTyConApps1 get_tc dft tys = do - tc <- builtin closureTyCon - return $ foldr (mk tc) res_ty arg_tys + tc <- builtin get_tc + case tys of + [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc) + _ -> return $ foldr1 (mk tc) tys where - mk tc arg_ty res_ty = mkTyConApp tc [arg_ty, res_ty] + mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2] -mkPADictType :: Type -> VM Type -mkPADictType ty +mkPRepr :: [[Type]] -> VM Type +mkPRepr tys = do - tc <- builtin paTyCon - return $ TyConApp tc [ty] + embed_tc <- builtin embedTyCon + sum_tcs <- builtins sumTyCon + prod_tcs <- builtins prodTyCon -mkPArrayType :: Type -> VM Type -mkPArrayType ty + let mk_sum [] = unitTy + mk_sum [ty] = ty + mk_sum tys = mkTyConApp (sum_tcs $ length tys) tys + + mk_prod [] = unitTy + mk_prod [ty] = ty + mk_prod tys = mkTyConApp (prod_tcs $ length tys) tys + + mk_embed ty = mkTyConApp embed_tc [ty] + + return . mk_sum + . map (mk_prod . map mk_embed) + $ tys + +mkToPRepr :: [[CoreExpr]] -> VM ([CoreExpr], Type) +mkToPRepr ess = do - tc <- builtin parrayTyCon - return $ TyConApp tc [ty] + embed_tc <- builtin embedTyCon + embed_dc <- builtin embedDataCon + sum_tcs <- builtins sumTyCon + prod_tcs <- builtins prodTyCon + + let mk_sum [] = ([Var unitDataConId], unitTy) + mk_sum [(expr, ty)] = ([expr], ty) + mk_sum es = (zipWith mk_alt (tyConDataCons sum_tc) exprs, + mkTyConApp sum_tc tys) + where + (exprs, tys) = unzip es + sum_tc = sum_tcs (length es) + mk_alt dc expr = mkConApp dc (map Type tys ++ [expr]) + + mk_prod [] = (Var unitDataConId, unitTy) + mk_prod [(expr, ty)] = (expr, ty) + mk_prod es = (mkConApp prod_dc (map Type tys ++ exprs), + mkTyConApp prod_tc tys) + where + (exprs, tys) = unzip es + prod_tc = prod_tcs (length es) + [prod_dc] = tyConDataCons prod_tc + + mk_embed expr = (mkConApp embed_dc [Type ty, expr], + mkTyConApp embed_tc [ty]) + where ty = exprType expr + + return . mk_sum $ map (mk_prod . map mk_embed) ess + +mkFromPRepr :: CoreExpr -> Type -> [([Var], CoreExpr)] -> VM CoreExpr +mkFromPRepr scrut res_ty alts + = do + embed_dc <- builtin embedDataCon + sum_tcs <- builtins sumTyCon + prod_tcs <- builtins prodTyCon + + let un_sum expr ty [(vars, res)] = un_prod expr ty vars res + un_sum expr ty bs + = do + ps <- mapM (newLocalVar FSLIT("p")) tys + bodies <- sequence + $ zipWith4 un_prod (map Var ps) tys vars rs + return . Case expr (mkWildId ty) res_ty + $ zipWith3 mk_alt sum_dcs ps bodies + where + (vars, rs) = unzip bs + tys = splitFixedTyConApp sum_tc ty + sum_tc = sum_tcs $ length bs + sum_dcs = tyConDataCons sum_tc + + mk_alt dc p body = (DataAlt dc, [p], body) + + un_prod expr ty [] r = return r + un_prod expr ty [var] r = return $ un_embed expr ty var r + un_prod expr ty vars r + = do + xs <- mapM (newLocalVar FSLIT("x")) tys + let body = foldr (\(e,t,v) r -> un_embed e t v r) r + $ zip3 (map Var xs) tys vars + return $ Case expr (mkWildId ty) res_ty + [(DataAlt prod_dc, xs, body)] + where + tys = splitFixedTyConApp prod_tc ty + prod_tc = prod_tcs $ length vars + [prod_dc] = tyConDataCons prod_tc + + un_embed expr ty var r + = Case expr (mkWildId ty) res_ty + [(DataAlt embed_dc, [var], r)] + + un_sum scrut (exprType scrut) alts + +mkClosureType :: Type -> Type -> VM Type +mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty] + +mkClosureTypes :: [Type] -> Type -> VM Type +mkClosureTypes = mkBuiltinTyConApps closureTyCon + +mkPReprType :: Type -> VM Type +mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty] + +mkPADictType :: Type -> VM Type +mkPADictType ty = mkBuiltinTyConApp paTyCon [ty] + +mkPArrayType :: Type -> VM Type +mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty] parrayReprTyCon :: Type -> VM (TyCon, [Type]) parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty]) @@ -120,6 +246,53 @@ parrayReprDataCon ty let [dc] = tyConDataCons tc return (dc, arg_tys) +mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type]) +mkVScrut (ve, le) + = do + (tc, arg_tys) <- parrayReprTyCon (exprType ve) + return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys) + +prDictOfType :: Type -> VM CoreExpr +prDictOfType orig_ty + | Just (tycon, ty_args) <- splitTyConApp_maybe orig_ty + = do + dfun <- traceMaybeV "prDictOfType" (ppr tycon) (lookupTyConPR tycon) + prDFunApply (Var dfun) ty_args + +prDFunApply :: CoreExpr -> [Type] -> VM CoreExpr +prDFunApply dfun tys + = do + args <- mapM mkDFunArg arg_tys + return $ mkApps mono_dfun args + where + mono_dfun = mkTyApps dfun tys + (arg_tys, _) = splitFunTys (exprType mono_dfun) + +mkDFunArg :: Type -> VM CoreExpr +mkDFunArg ty + | Just (tycon, [arg]) <- splitTyConApp_maybe ty + + = let name = tyConName tycon + + get_dict | name == paTyConName = paDictOfType + | name == prTyConName = prDictOfType + | otherwise = pprPanic "mkDFunArg" (ppr ty) + + in get_dict arg + +mkDFunArg ty = pprPanic "mkDFunArg" (ppr ty) + +prCoerce :: TyCon -> [Type] -> CoreExpr -> VM CoreExpr +prCoerce repr_tc args expr + | Just arg_co <- tyConFamilyCoercion_maybe repr_tc + = do + pr_tc <- builtin prTyCon + + let co = mkAppCoercion (mkTyConApp pr_tc []) + (mkSymCoercion (mkTyConApp arg_co args)) + + return $ mkCoerce co expr + paDictArgType :: TyVar -> VM (Maybe Type) paDictArgType tv = go (TyVarTy tv) (tyVarKind tv) where @@ -290,6 +463,8 @@ mkClosureApp (vclo, lclo) (varg, larg) (arg_ty, res_ty) = splitClosureTy (exprType vclo) buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr +buildClosures tvs vars [] res_ty mk_body + = mk_body buildClosures tvs vars [arg_ty] res_ty mk_body = buildClosure tvs vars arg_ty res_ty mk_body buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body