X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2Fvectorise%2FVectUtils.hs;h=7aef39b6b959ca76c2d37266d96ee141a5955315;hp=f7df2779b3a6cf733ad32c8f8f64a33444c0c25a;hb=9ffadf219cbc4f8ec57264786df936a3cee88aec;hpb=263a30f1db8fc4f5b6cecc26176c6f881807e348 diff --git a/compiler/vectorise/VectUtils.hs b/compiler/vectorise/VectUtils.hs index f7df277..7aef39b 100644 --- a/compiler/vectorise/VectUtils.hs +++ b/compiler/vectorise/VectUtils.hs @@ -1,41 +1,45 @@ module VectUtils ( collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg, collectAnnValBinders, - mkDataConTag, - splitClosureTy, - mkPADictType, mkPArrayType, + dataConTagZ, mkDataConTag, mkDataConTagLit, + + newLocalVVar, + + mkBuiltinCo, + mkPADictType, mkPArrayType, mkPReprType, + + parrayReprTyCon, parrayReprDataCon, mkVScrut, + prDFunOfTyCon, paDictArgType, paDictOfType, paDFunType, - paMethod, lengthPA, replicatePA, emptyPA, liftPA, + paMethod, mkPR, lengthPA, replicatePA, emptyPA, packPA, combinePA, liftPA, polyAbstract, polyApply, polyVApply, - lookupPArrayFamInst, hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted, buildClosure, buildClosures, mkClosureApp ) where -#include "HsVersions.h" - import VectCore import VectMonad -import DsUtils +import MkCore 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 ) -import PrelNames import TysWiredIn import BasicTypes ( Boxity(..) ) +import Literal ( Literal, mkMachInt ) import Outputable import FastString -import Control.Monad ( liftM, zipWithM_ ) +import Control.Monad + collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type]) collectAnnTypeArgs expr = go expr [] @@ -56,59 +60,102 @@ collectAnnValBinders expr = go [] expr go bs e = (reverse bs, e) isAnnTypeArg :: AnnExpr b ann -> Bool -isAnnTypeArg (_, AnnType t) = True +isAnnTypeArg (_, AnnType _) = True isAnnTypeArg _ = False -mkDataConTag :: DataCon -> CoreExpr -mkDataConTag dc = mkConApp intDataCon [mkIntLitInt $ dataConTag dc] +dataConTagZ :: DataCon -> Int +dataConTagZ con = dataConTag con - fIRST_TAG -isClosureTyCon :: TyCon -> Bool -isClosureTyCon tc = tyConName tc == closureTyConName +mkDataConTagLit :: DataCon -> Literal +mkDataConTagLit = mkMachInt . toInteger . dataConTagZ -splitClosureTy :: Type -> (Type, Type) -splitClosureTy ty - | Just (tc, [arg_ty, res_ty]) <- splitTyConApp_maybe ty - , isClosureTyCon tc - = (arg_ty, res_ty) - - | otherwise = pprPanic "splitClosureTy" (ppr ty) +mkDataConTag :: DataCon -> CoreExpr +mkDataConTag = mkIntLitInt . dataConTagZ -isPArrayTyCon :: TyCon -> Bool -isPArrayTyCon tc = tyConName tc == parrayTyConName +splitPrimTyCon :: Type -> Maybe TyCon +splitPrimTyCon ty + | Just (tycon, []) <- splitTyConApp_maybe ty + , isPrimTyCon tycon + = Just tycon -splitPArrayTy :: Type -> Type -splitPArrayTy ty - | Just (tc, [arg_ty]) <- splitTyConApp_maybe ty - , isPArrayTyCon tc - = arg_ty + | otherwise = Nothing - | 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 _ dft [] = return dft +mkBuiltinTyConApps1 get_tc _ 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] + +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 - = do - tc <- builtin paTyCon - return $ TyConApp tc [ty] +mkPADictType ty = mkBuiltinTyConApp paTyCon [ty] mkPArrayType :: Type -> VM Type mkPArrayType ty + | Just tycon <- splitPrimTyCon ty + = do + r <- lookupPrimPArray tycon + case r of + Just arr -> return $ mkTyConApp arr [] + Nothing -> cantVectorise "Primitive tycon not vectorised" (ppr tycon) +mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty] + +mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion +mkBuiltinCo get_tc = do - tc <- builtin parrayTyCon - return $ TyConApp tc [ty] + tc <- builtin get_tc + return $ mkTyConApp tc [] + +parrayReprTyCon :: Type -> VM (TyCon, [Type]) +parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty]) + +parrayReprDataCon :: Type -> VM (DataCon, [Type]) +parrayReprDataCon ty + = do + (tc, arg_tys) <- parrayReprTyCon 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) + +prDFunOfTyCon :: TyCon -> VM CoreExpr +prDFunOfTyCon tycon + = liftM Var + . maybeCantVectoriseM "No PR dictionary for tycon" (ppr tycon) + $ lookupTyConPR tycon paDictArgType :: TyVar -> VM (Maybe Type) paDictArgType tv = go (TyVarTy tv) (tyVarKind tv) @@ -116,7 +163,7 @@ paDictArgType tv = go (TyVarTy tv) (tyVarKind tv) go ty k | Just k' <- kindView k = go ty k' go ty (FunTy k1 k2) = do - tv <- newTyVar FSLIT("a") k1 + tv <- newTyVar (fsLit "a") k1 mty1 <- go (TyVarTy tv) k1 case mty1 of Just ty1 -> do @@ -128,7 +175,7 @@ paDictArgType tv = go (TyVarTy tv) (tyVarKind tv) | isLiftedTypeKind k = liftM Just (mkPADictType ty) - go ty k = return Nothing + go _ _ = return Nothing paDictOfType :: Type -> VM CoreExpr paDictOfType ty = paDictOfTyApp ty_fn ty_args @@ -144,9 +191,11 @@ paDictOfTyApp (TyVarTy tv) ty_args paDFunApply dfun ty_args paDictOfTyApp (TyConApp tc _) ty_args = do - dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc) + dfun <- maybeCantVectoriseM "No PA dictionary for tycon" (ppr tc) + $ lookupTyConPA tc paDFunApply (Var dfun) ty_args -paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty) +paDictOfTyApp ty _ + = cantVectorise "Can't construct PA dictionary for type" (ppr ty) paDFunType :: TyCon -> VM Type paDFunType tc @@ -165,24 +214,55 @@ 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, pa_replicate, pa_empty, pa_pack :: (Builtins -> Var, String) +pa_length = (lengthPAVar, "lengthPA") +pa_replicate = (replicatePAVar, "replicatePA") +pa_empty = (emptyPAVar, "emptyPA") +pa_pack = (packPAVar, "packPA") + +paMethod :: PAMethod -> Type -> VM CoreExpr +paMethod (_method, name) ty + | Just tycon <- splitPrimTyCon ty + = liftM Var + . maybeCantVectoriseM "No PA method" (text name <+> text "for" <+> ppr tycon) + $ lookupPrimMethod tycon name + +paMethod (method, _name) ty = do fn <- builtin method 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) +mkPR :: Type -> VM CoreExpr +mkPR ty + = do + fn <- builtin mkPRVar + dict <- paDictOfType ty + return $ mkApps (Var fn) [Type ty, dict] + +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 + +packPA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr +packPA ty xs len sel = liftM (`mkApps` [xs, len, sel]) + (paMethod pa_pack ty) + +combinePA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> [CoreExpr] + -> VM CoreExpr +combinePA ty len sel is xs + = liftM (`mkApps` (len : sel : is : xs)) + (paMethod (combinePAVar n, "combine" ++ show n ++ "PA") ty) + where + n = length xs liftPA :: CoreExpr -> VM CoreExpr liftPA x @@ -209,7 +289,7 @@ polyAbstract tvs p mk_dict_var tv = do r <- paDictArgType tv case r of - Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty) + Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty) Nothing -> return Nothing mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts]) @@ -226,9 +306,6 @@ polyVApply expr tys dicts <- mapM paDictOfType tys return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr -lookupPArrayFamInst :: Type -> VM (TyCon, [Type]) -lookupPArrayFamInst ty = builtin parrayTyCon >>= (`lookupFamInst` [ty]) - hoistBinding :: Var -> CoreExpr -> VM () hoistBinding v e = updGEnv $ \env -> env { global_bindings = (v,e) : global_bindings env } @@ -263,6 +340,20 @@ takeHoisted setGEnv $ env { global_bindings = [] } return $ global_bindings env +{- +boxExpr :: Type -> VExpr -> VM VExpr +boxExpr ty (vexpr, lexpr) + | Just (tycon, []) <- splitTyConApp_maybe ty + , isUnLiftedTyCon tycon + = do + r <- lookupBoxedTyCon tycon + case r of + Just tycon' -> let [dc] = tyConDataCons tycon' + in + return (mkConApp dc [vexpr], lexpr) + Nothing -> return (vexpr, lexpr) +-} + mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv) = do @@ -272,23 +363,23 @@ 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 _ _ [] _ 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 = do res_ty' <- mkClosureTypes arg_tys res_ty - arg <- newLocalVVar FSLIT("x") arg_ty + arg <- newLocalVVar (fsLit "x") arg_ty buildClosure tvs vars arg_ty res_ty' . hoistPolyVExpr tvs $ do @@ -305,8 +396,8 @@ buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr buildClosure tvs vars arg_ty res_ty mk_body = do (env_ty, env, bind) <- buildEnv vars - env_bndr <- newLocalVVar FSLIT("env") env_ty - arg_bndr <- newLocalVVar FSLIT("arg") arg_ty + env_bndr <- newLocalVVar (fsLit "env") env_ty + arg_bndr <- newLocalVVar (fsLit "arg") arg_ty fn <- hoistPolyVExpr tvs $ do @@ -332,13 +423,13 @@ buildEnv vvs return (vbody', lbody')) where (vs,ls) = unzip vvs - tys = map idType vs + tys = map varType vs mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr) -mkVectEnv [] [] = (unitTy, Var unitDataConId, \env body -> body) +mkVectEnv [] [] = (unitTy, Var unitDataConId, \_ body -> body) mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body) mkVectEnv tys vs = (ty, mkCoreTup (map Var vs), - \env body -> Case env (mkWildId ty) (exprType body) + \env body -> mkWildCase env ty (exprType body) [(DataAlt (tupleCon Boxed (length vs)), vs, body)]) where ty = mkCoreTupTy tys @@ -347,29 +438,34 @@ 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)]) -- NOTE: this transparently deals with empty environments mkLiftEnv lc tys vs = do - (env_tc, env_tyargs) <- lookupPArrayFamInst vty + (env_tc, env_tyargs) <- parrayReprTyCon vty + + bndrs <- if null vs then do + v <- newDummyVar unitTy + return [v] + else return vs let [env_con] = tyConDataCons env_tc env = Var (dataConWrapId env_con) `mkTyApps` env_tyargs - `mkVarApps` (lc : vs) + `mkApps` (Var lc : args) bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env in - return $ Case scrut (mkWildId (exprType scrut)) + return $ mkWildCase scrut (exprType scrut) (exprType body) [(DataAlt env_con, lc : bndrs, body)] return (env, bind) where vty = mkCoreTupTy tys - bndrs | null vs = [mkWildId unitTy] - | otherwise = vs + args | null vs = [Var unitDataConId] + | otherwise = map Var vs