X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Fvectorise%2FVectUtils.hs;h=8cb0a11a99251000380db7234b1c11e97f97c9c2;hb=151b1170c26a325cc93b2ae151804d5a714021a3;hp=bbcd91cafcfe77b8fea185b1ee54ca02985d8630;hpb=dcc66f7459ce8b815c1c82521ac6b6214a91ba7b;p=ghc-hetmet.git diff --git a/compiler/vectorise/VectUtils.hs b/compiler/vectorise/VectUtils.hs index bbcd91c..8cb0a11 100644 --- a/compiler/vectorise/VectUtils.hs +++ b/compiler/vectorise/VectUtils.hs @@ -3,9 +3,13 @@ module VectUtils ( collectAnnValBinders, mkDataConTag, splitClosureTy, - mkPReprType, mkPReprAlts, - mkPADictType, mkPArrayType, - parrayReprTyCon, parrayReprDataCon, mkVScrut, + + TyConRepr(..), mkTyConRepr, + mkToPRepr, mkToArrPRepr, mkFromPRepr, mkFromArrPRepr, + mkPADictType, mkPArrayType, mkPReprType, + + parrayCoerce, parrayReprTyCon, parrayReprDataCon, mkVScrut, + prDictOfType, prCoerce, paDictArgType, paDictOfType, paDFunType, paMethod, lengthPA, replicatePA, emptyPA, liftPA, polyAbstract, polyApply, polyVApply, @@ -22,10 +26,11 @@ import VectMonad import DsUtils import CoreSyn import CoreUtils +import Coercion import Type import TypeRep import TyCon -import DataCon ( DataCon, dataConWrapId, dataConTag ) +import DataCon import Var import Id ( mkWildId ) import MkId ( unwrapFamInstScrut ) @@ -37,7 +42,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 [] @@ -80,14 +86,13 @@ splitBinTy s name ty | otherwise = pprPanic s (ppr ty) -splitCrossTy :: Type -> (Type, Type) -splitCrossTy = splitBinTy "splitCrossTy" ndpCrossTyConName +splitFixedTyConApp :: TyCon -> Type -> [Type] +splitFixedTyConApp tc ty + | Just (tc', tys) <- splitTyConApp_maybe ty + , tc == tc' + = tys -splitPlusTy :: Type -> (Type, Type) -splitPlusTy = splitBinTy "splitSumTy" ndpPlusTyConName - -splitEmbedTy :: Type -> Type -splitEmbedTy = splitUnTy "splitEmbedTy" embedTyConName + | otherwise = pprPanic "splitFixedTyConApp" (ppr tc <+> ppr ty) splitClosureTy :: Type -> (Type, Type) splitClosureTy = splitBinTy "splitClosureTy" closureTyConName @@ -120,66 +125,137 @@ mkBuiltinTyConApps1 get_tc dft tys where mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2] -mkPReprType :: [[Type]] -> VM Type -mkPReprType [] = return unitTy -mkPReprType tys +data TyConRepr = TyConRepr { + repr_tyvars :: [TyVar] + , repr_tys :: [[Type]] + + , repr_prod_tycons :: [Maybe TyCon] + , repr_prod_tys :: [Type] + , repr_sum_tycon :: Maybe TyCon + , repr_type :: Type + } + +mkTyConRepr :: TyCon -> VM TyConRepr +mkTyConRepr vect_tc = do - embed <- builtin embedTyCon - cross <- builtin crossTyCon - plus <- builtin plusTyCon + prod_tycons <- mapM (mk_tycon prodTyCon) rep_tys + let prod_tys = zipWith mk_tc_app_maybe prod_tycons rep_tys + sum_tycon <- mk_tycon sumTyCon prod_tys + + return $ TyConRepr { + repr_tyvars = tyvars + , repr_tys = rep_tys + + , repr_prod_tycons = prod_tycons + , repr_prod_tys = prod_tys + , repr_sum_tycon = sum_tycon + , repr_type = mk_tc_app_maybe sum_tycon prod_tys + } + where + tyvars = tyConTyVars vect_tc + data_cons = tyConDataCons vect_tc + rep_tys = map dataConRepArgTys data_cons + + mk_tycon get_tc tys + | n > 1 = builtin (Just . get_tc n) + | otherwise = return Nothing + where n = length tys + + mk_tc_app_maybe Nothing [] = unitTy + mk_tc_app_maybe Nothing [ty] = ty + mk_tc_app_maybe (Just tc) tys = mkTyConApp tc tys + +mkToPRepr :: TyConRepr -> [[CoreExpr]] -> [CoreExpr] +mkToPRepr (TyConRepr { + repr_tys = repr_tys + , repr_prod_tycons = prod_tycons + , repr_prod_tys = prod_tys + , repr_sum_tycon = repr_sum_tycon + }) + = mk_sum . zipWith3 mk_prod prod_tycons repr_tys + where + Just sum_tycon = repr_sum_tycon - let mk_embed ty = mkTyConApp embed [ty] - mk_cross ty1 ty2 = mkTyConApp cross [ty1, ty2] - mk_plus ty1 ty2 = mkTyConApp plus [ty1, ty2] + mk_sum [] = [Var unitDataConId] + mk_sum [expr] = [expr] + mk_sum exprs = zipWith (mk_alt prod_tys) (tyConDataCons sum_tycon) exprs - mk_tup [] = unitTy - mk_tup tys = foldr1 mk_cross tys + mk_alt tys dc expr = mk_con_app dc tys [expr] - mk_sum [] = unitTy - mk_sum tys = foldr1 mk_plus tys + mk_prod _ _ [] = Var unitDataConId + mk_prod _ _ [expr] = expr + mk_prod (Just tc) tys exprs = mk_con_app dc tys exprs + where + [dc] = tyConDataCons tc - return . mk_sum - . map (mk_tup . map mk_embed) - $ tys + mk_con_app dc tys exprs = mkConApp dc (map Type tys ++ exprs) -mkPReprAlts :: [[CoreExpr]] -> VM ([CoreExpr], Type) -mkPReprAlts ess +mkToArrPRepr :: CoreExpr -> CoreExpr -> [[CoreExpr]] -> VM CoreExpr +mkToArrPRepr len sel ess = do - embed_tc <- builtin embedTyCon - embed_dc <- builtin embedDataCon - cross_tc <- builtin crossTyCon - cross_dc <- builtin crossDataCon - plus_tc <- builtin plusTyCon - left_dc <- builtin leftDataCon - right_dc <- builtin rightDataCon - - let mk_embed (expr, ty, pa) - = (mkConApp embed_dc [Type ty, pa, expr], - mkTyConApp embed_tc [ty]) - - mk_cross (expr1, ty1) (expr2, ty2) - = (mkConApp cross_dc [Type ty1, Type ty2, expr1, expr2], - mkTyConApp cross_tc [ty1, ty2]) - - mk_tup [] = (Var unitDataConId, unitTy) - mk_tup es = foldr1 mk_cross es - - mk_sum [] = ([Var unitDataConId], unitTy) - mk_sum [(expr, ty)] = ([expr], ty) - mk_sum ((expr, lty) : es) - = let (alts, rty) = mk_sum es - in - (mkConApp left_dc [Type lty, Type rty, expr] - : [mkConApp right_dc [Type lty, Type rty, alt] | alt <- alts], - mkTyConApp plus_tc [lty, rty]) - - liftM (mk_sum . map (mk_tup . map mk_embed)) - (mapM (mapM init) ess) - where - init expr = let ty = exprType expr - in do - pa <- paDictOfType ty - return (expr, ty, pa) + let mk_sum [(expr, ty)] = return (expr, ty) + mk_sum es + = do + sum_tc <- builtin . sumTyCon $ length es + (sum_rtc, _) <- parrayReprTyCon (mkTyConApp sum_tc tys) + let [sum_rdc] = tyConDataCons sum_rtc + + return (mkConApp sum_rdc (map Type tys ++ (len : sel : exprs)), + mkTyConApp sum_tc tys) + where + (exprs, tys) = unzip es + + mk_prod [expr] = return (expr, splitPArrayTy (exprType expr)) + mk_prod exprs + = do + prod_tc <- builtin . prodTyCon $ length exprs + (prod_rtc, _) <- parrayReprTyCon (mkTyConApp prod_tc tys) + let [prod_rdc] = tyConDataCons prod_rtc + + return (mkConApp prod_rdc (map Type tys ++ (len : exprs)), + mkTyConApp prod_tc tys) + where + tys = map (splitPArrayTy . exprType) exprs + + liftM fst (mk_sum =<< mapM mk_prod ess) + +mkFromPRepr :: CoreExpr -> Type -> [([Var], CoreExpr)] -> VM CoreExpr +mkFromPRepr scrut res_ty alts + = do + 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 $ Let (NonRec var expr) r + un_prod expr ty vars r + = return $ Case expr (mkWildId ty) res_ty + [(DataAlt prod_dc, vars, r)] + where + prod_tc = prod_tcs $ length vars + [prod_dc] = tyConDataCons prod_tc + + un_sum scrut (exprType scrut) alts + +mkFromArrPRepr :: CoreExpr -> Type -> Var -> Var -> [[Var]] -> CoreExpr + -> VM CoreExpr +mkFromArrPRepr scrut res_ty len sel vars res + = return (Var unitDataConId) mkClosureType :: Type -> Type -> VM Type mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty] @@ -187,12 +263,26 @@ 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] +parrayCoerce :: TyCon -> [Type] -> CoreExpr -> VM CoreExpr +parrayCoerce repr_tc args expr + | Just arg_co <- tyConFamilyCoercion_maybe repr_tc + = do + parray <- builtin parrayTyCon + + let co = mkAppCoercion (mkTyConApp parray []) + (mkSymCoercion (mkTyConApp arg_co args)) + + return $ mkCoerce co expr + parrayReprTyCon :: Type -> VM (TyCon, [Type]) parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty]) @@ -209,6 +299,47 @@ mkVScrut (ve, le) (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 @@ -271,6 +402,9 @@ paMethod method ty dict <- paDictOfType ty return $ mkApps (Var fn) [Type ty, dict] +mkPR :: Type -> VM CoreExpr +mkPR = paMethod mkPRVar + lengthPA :: CoreExpr -> VM CoreExpr lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty) where