X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2Fvectorise%2FVectUtils.hs;h=83b482a669bee0d44c6c48c57080200992585caa;hp=c7336efbc58514c01999e79115a88dc308a621a7;hb=7fc749a43b4b6b85d234fa95d4928648259584f4;hpb=724425265ded8958a719b3a62f43006674b506c8 diff --git a/compiler/vectorise/VectUtils.hs b/compiler/vectorise/VectUtils.hs index c7336ef..83b482a 100644 --- a/compiler/vectorise/VectUtils.hs +++ b/compiler/vectorise/VectUtils.hs @@ -1,17 +1,22 @@ +{-# OPTIONS -w #-} +-- The above warning supression flag is a temporary kludge. +-- While working on this module you are encouraged to remove it and fix +-- any warnings in the module. See +-- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings +-- for details + module VectUtils ( collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg, collectAnnValBinders, - mkDataConTag, - splitClosureTy, + mkDataConTag, mkDataConTagLit, - TyConRepr(..), mkTyConRepr, - mkToArrPRepr, mkFromPRepr, mkFromArrPRepr, + mkBuiltinCo, mkPADictType, mkPArrayType, mkPReprType, - parrayCoerce, parrayReprTyCon, parrayReprDataCon, mkVScrut, - prDictOfType, prCoerce, + parrayReprTyCon, parrayReprDataCon, mkVScrut, + prDFunOfTyCon, paDictArgType, paDictOfType, paDFunType, - paMethod, lengthPA, replicatePA, emptyPA, liftPA, + paMethod, mkPR, lengthPA, replicatePA, emptyPA, liftPA, polyAbstract, polyApply, polyVApply, hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted, buildClosure, buildClosures, @@ -37,7 +42,9 @@ import MkId ( unwrapFamInstScrut ) import Name ( Name ) import PrelNames import TysWiredIn +import TysPrim ( intPrimTy ) import BasicTypes ( Boxity(..) ) +import Literal ( Literal, mkMachInt ) import Outputable import FastString @@ -67,38 +74,20 @@ isAnnTypeArg :: AnnExpr b ann -> Bool isAnnTypeArg (_, AnnType t) = True isAnnTypeArg _ = False -mkDataConTag :: DataCon -> CoreExpr -mkDataConTag dc = mkConApp intDataCon [mkIntLitInt $ dataConTag dc] - -splitUnTy :: String -> Name -> Type -> Type -splitUnTy s name ty - | Just (tc, [ty']) <- splitTyConApp_maybe ty - , tyConName tc == name - = 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) +mkDataConTagLit :: DataCon -> Literal +mkDataConTagLit con + = mkMachInt . toInteger $ dataConTag con - fIRST_TAG -splitFixedTyConApp :: TyCon -> Type -> [Type] -splitFixedTyConApp tc ty - | Just (tc', tys) <- splitTyConApp_maybe ty - , tc == tc' - = tys - - | otherwise = pprPanic "splitFixedTyConApp" (ppr tc <+> ppr ty) +mkDataConTag :: DataCon -> CoreExpr +mkDataConTag con = mkIntLitInt (dataConTag con - fIRST_TAG) -splitClosureTy :: Type -> (Type, Type) -splitClosureTy = splitBinTy "splitClosureTy" closureTyConName +splitPrimTyCon :: Type -> Maybe TyCon +splitPrimTyCon ty + | Just (tycon, []) <- splitTyConApp_maybe ty + , isPrimTyCon tycon + = Just tycon -splitPArrayTy :: Type -> Type -splitPArrayTy = splitUnTy "splitPArrayTy" parrayTyConName + | otherwise = Nothing mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type mkBuiltinTyConApp get_tc tys @@ -125,113 +114,6 @@ mkBuiltinTyConApps1 get_tc dft tys where mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2] -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 - 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 - -mkToArrPRepr :: CoreExpr -> CoreExpr -> [[CoreExpr]] -> VM CoreExpr -mkToArrPRepr len sel ess - = do - 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] @@ -245,18 +127,19 @@ mkPADictType :: Type -> VM Type mkPADictType ty = mkBuiltinTyConApp paTyCon [ty] mkPArrayType :: Type -> VM Type +mkPArrayType ty + | Just tycon <- splitPrimTyCon ty + = do + arr <- traceMaybeV "mkPArrayType" (ppr tycon) + $ lookupPrimPArray tycon + return $ mkTyConApp arr [] mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty] -parrayCoerce :: TyCon -> [Type] -> CoreExpr -> VM CoreExpr -parrayCoerce repr_tc args expr - | Just arg_co <- tyConFamilyCoercion_maybe repr_tc +mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion +mkBuiltinCo get_tc = do - parray <- builtin parrayTyCon - - let co = mkAppCoercion (mkTyConApp parray []) - (mkSymCoercion (mkTyConApp arg_co args)) - - return $ mkCoerce co expr + tc <- builtin get_tc + return $ mkTyConApp tc [] parrayReprTyCon :: Type -> VM (TyCon, [Type]) parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty]) @@ -274,46 +157,9 @@ 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 +prDFunOfTyCon :: TyCon -> VM CoreExpr +prDFunOfTyCon tycon + = liftM Var (traceMaybeV "prDictOfTyCon" (ppr tycon) (lookupTyConPR tycon)) paDictArgType :: TyVar -> VM (Maybe Type) paDictArgType tv = go (TyVarTy tv) (tyVarKind tv) @@ -370,27 +216,42 @@ paDFunApply dfun tys dicts <- mapM paDictOfType tys return $ mkApps (mkTyApps dfun tys) dicts -paMethod :: (Builtins -> Var) -> Type -> VM CoreExpr -paMethod method ty +type PAMethod = (Builtins -> Var, String) + +pa_length = (lengthPAVar, "lengthPA") +pa_replicate = (replicatePAVar, "replicatePA") +pa_empty = (emptyPAVar, "emptyPA") + +paMethod :: PAMethod -> Type -> VM CoreExpr +paMethod (method, name) ty + | Just tycon <- splitPrimTyCon ty + = do + fn <- traceMaybeV "paMethod" (ppr tycon <+> text name) + $ lookupPrimMethod tycon name + return (Var fn) + +paMethod (method, name) ty = do fn <- builtin method dict <- paDictOfType ty return $ mkApps (Var fn) [Type ty, dict] mkPR :: Type -> VM CoreExpr -mkPR = paMethod mkPRVar +mkPR ty + = do + fn <- builtin mkPRVar + dict <- paDictOfType ty + return $ mkApps (Var fn) [Type ty, dict] -lengthPA :: CoreExpr -> VM CoreExpr -lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty) - where - ty = splitPArrayTy (exprType x) +lengthPA :: Type -> CoreExpr -> VM CoreExpr +lengthPA ty x = liftM (`App` x) (paMethod pa_length ty) replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr replicatePA len x = liftM (`mkApps` [len,x]) - (paMethod replicatePAVar (exprType x)) + (paMethod pa_replicate (exprType x)) emptyPA :: Type -> VM CoreExpr -emptyPA = paMethod emptyPAVar +emptyPA = paMethod pa_empty liftPA :: CoreExpr -> VM CoreExpr liftPA x @@ -477,15 +338,13 @@ mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv) return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv], Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv]) -mkClosureApp :: VExpr -> VExpr -> VM VExpr -mkClosureApp (vclo, lclo) (varg, larg) +mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr +mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg) = do vapply <- builtin applyClosureVar lapply <- builtin applyClosurePVar return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg], Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg]) - where - (arg_ty, res_ty) = splitClosureTy (exprType vclo) buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr buildClosures tvs vars [] res_ty mk_body @@ -554,7 +413,7 @@ mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM mkLiftEnv lc [ty] [v] = return (Var v, \env body -> do - len <- lengthPA (Var v) + len <- lengthPA ty (Var v) return . Let (NonRec v env) $ Case len lc (exprType body) [(DEFAULT, [], body)])