X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Fvectorise%2FVectUtils.hs;h=d6569e70cd8892e5bbf55dccd04520588b5a5205;hb=ce39c447ab47ac1616cea079210c7651f486f425;hp=cbca02f49e54317c119b0efe3bf54fc085b7d7a7;hpb=fede30f987edef37bc62a07bdcdf3e7b5b951475;p=ghc-hetmet.git diff --git a/compiler/vectorise/VectUtils.hs b/compiler/vectorise/VectUtils.hs index cbca02f..d6569e7 100644 --- a/compiler/vectorise/VectUtils.hs +++ b/compiler/vectorise/VectUtils.hs @@ -1,16 +1,19 @@ module VectUtils ( collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg, + collectAnnValBinders, splitClosureTy, mkPADictType, mkPArrayType, paDictArgType, paDictOfType, paMethod, lengthPA, replicatePA, emptyPA, - polyAbstract, polyApply, + polyAbstract, polyApply, polyVApply, lookupPArrayFamInst, - hoistExpr, takeHoisted + hoistExpr, hoistPolyVExpr, takeHoisted, + buildClosure, buildClosures ) where #include "HsVersions.h" +import VectCore import VectMonad import DsUtils @@ -44,6 +47,12 @@ collectAnnTypeBinders expr = go [] expr go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e go bs e = (reverse bs, e) +collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann) +collectAnnValBinders expr = go [] expr + where + go bs (_, AnnLam b e) | isId b = go (b:bs) e + go bs e = (reverse bs, e) + isAnnTypeArg :: AnnExpr b ann -> Bool isAnnTypeArg (_, AnnType t) = True isAnnTypeArg _ = False @@ -70,6 +79,20 @@ splitPArrayTy ty | otherwise = pprPanic "splitPArrayTy" (ppr ty) +mkClosureType :: Type -> Type -> VM Type +mkClosureType arg_ty res_ty + = do + tc <- builtin closureTyCon + return $ mkTyConApp tc [arg_ty, res_ty] + +mkClosureTypes :: [Type] -> Type -> VM Type +mkClosureTypes arg_tys res_ty + = do + tc <- builtin closureTyCon + return $ foldr (mk tc) res_ty arg_tys + where + mk tc arg_ty res_ty = mkTyConApp tc [arg_ty, res_ty] + mkPADictType :: Type -> VM Type mkPADictType ty = do @@ -146,19 +169,6 @@ replicatePA len x = liftM (`mkApps` [len,x]) emptyPA :: Type -> VM CoreExpr emptyPA = paMethod emptyPAVar -type Vect a = (a,a) -type VVar = Vect Var -type VExpr = Vect CoreExpr - -vectorised :: Vect a -> a -vectorised = fst - -lifted :: Vect a -> a -lifted = snd - -mapVect :: (a -> b) -> Vect a -> Vect b -mapVect f (x,y) = (f x, f y) - newLocalVVar :: FastString -> Type -> VM VVar newLocalVVar fs vty = do @@ -167,19 +177,6 @@ newLocalVVar fs vty lv <- newLocalVar fs lty return (vv,lv) -vVar :: VVar -> VExpr -vVar = mapVect Var - -mkVLams :: [VVar] -> VExpr -> VExpr -mkVLams vvs (ve,le) = (mkLams vs ve, mkLams ls le) - where - (vs,ls) = unzip vvs - -mkVVarApps :: Var -> VExpr -> [VVar] -> VExpr -mkVVarApps lc (ve, le) vvs = (ve `mkVarApps` vs, le `mkVarApps` (lc : ls)) - where - (vs,ls) = unzip vvs - polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a polyAbstract tvs p = localV @@ -202,6 +199,12 @@ polyApply expr tys dicts <- mapM paDictOfType tys return $ expr `mkTyApps` tys `mkApps` dicts +polyVApply :: VExpr -> [Type] -> VM VExpr +polyVApply expr tys + = do + dicts <- mapM paDictOfType tys + return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr + lookupPArrayFamInst :: Type -> VM (TyCon, [Type]) lookupPArrayFamInst ty = builtin parrayTyCon >>= (`lookupFamInst` [ty]) @@ -213,19 +216,21 @@ hoistExpr fs expr env { global_bindings = (var, expr) : global_bindings env } return var -hoistPolyExpr :: FastString -> [TyVar] -> CoreExpr -> VM CoreExpr -hoistPolyExpr fs tvs expr +hoistVExpr :: VExpr -> VM VVar +hoistVExpr (ve, le) = do - poly_expr <- closedV . polyAbstract tvs $ \abstract -> return (abstract expr) - fn <- hoistExpr fs poly_expr - polyApply (Var fn) (mkTyVarTys tvs) + fs <- getBindName + vv <- hoistExpr ('v' `consFS` fs) ve + lv <- hoistExpr ('l' `consFS` fs) le + return (vv, lv) -hoistPolyVExpr :: FastString -> [TyVar] -> VExpr -> VM VExpr -hoistPolyVExpr fs tvs (ve, le) +hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr +hoistPolyVExpr tvs p = do - ve' <- hoistPolyExpr ('v' `consFS` fs) tvs ve - le' <- hoistPolyExpr ('l' `consFS` fs) tvs le - return (ve',le') + expr <- closedV . polyAbstract tvs $ \abstract -> + liftM (mapVect abstract) p + fn <- hoistVExpr expr + polyVApply (vVar fn) (mkTyVarTys tvs) takeHoisted :: VM [(Var, CoreExpr)] takeHoisted @@ -244,36 +249,51 @@ 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]) +buildClosures :: [TyVar] -> Var -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr +buildClosures tvs lc vars [arg_ty] res_ty mk_body + = buildClosure tvs lc vars arg_ty res_ty mk_body +buildClosures tvs lc vars (arg_ty : arg_tys) res_ty mk_body + = do + res_ty' <- mkClosureTypes arg_tys res_ty + arg <- newLocalVVar FSLIT("x") arg_ty + buildClosure tvs lc vars arg_ty res_ty' + . hoistPolyVExpr tvs + $ do + clo <- buildClosures tvs lc (vars ++ [arg]) arg_tys res_ty mk_body + return $ vLams lc (vars ++ [arg]) clo + -- (clo , aclo (Arr lc xs1 ... xsn) ) -- where -- f = \env v -> case env of -> e x1 ... xn v -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v - -buildClosure :: [TyVar] -> Var -> [VVar] -> VVar -> VExpr -> VM VExpr -buildClosure tvs lv vars arg body +-- +buildClosure :: [TyVar] -> Var -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr +buildClosure tvs lv vars arg_ty res_ty mk_body = do (env_ty, env, bind) <- buildEnv lv vars - env_bndr <- newLocalVVar FSLIT("env") env_ty + env_bndr <- newLocalVVar FSLIT("env") env_ty + arg_bndr <- newLocalVVar FSLIT("arg") arg_ty - fn <- hoistPolyVExpr FSLIT("fn") tvs - . mkVLams [env_bndr, arg] - . bind (vVar env_bndr) - $ mkVVarApps lv body (vars ++ [arg]) + fn <- hoistPolyVExpr tvs + $ do + body <- mk_body + body' <- bind (vVar env_bndr) + (vVarApps lv body (vars ++ [arg_bndr])) + return (vLamsWithoutLC [env_bndr, arg_bndr] body') mkClosure arg_ty res_ty env_ty fn env - where - arg_ty = idType (vectorised arg) - res_ty = exprType (vectorised body) - - -buildEnv :: Var -> [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr) +buildEnv :: Var -> [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr) buildEnv lv vvs = do let (ty, venv, vbind) = mkVectEnv tys vs (lenv, lbind) <- mkLiftEnv lv tys ls return (ty, (venv, lenv), - \(venv,lenv) (vbody,lbody) -> (vbind venv vbody, lbind lenv lbody)) + \(venv,lenv) (vbody,lbody) -> + do + let vbody' = vbind venv vbody + lbody' <- lbind lenv lbody + return (vbody', lbody')) where (vs,ls) = unzip vvs tys = map idType vs @@ -287,12 +307,13 @@ mkVectEnv tys vs = (ty, mkCoreTup (map Var vs), where ty = mkCoreTupTy tys -mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> CoreExpr) +mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr) mkLiftEnv lv [ty] [v] - = do - len <- lengthPA (Var v) - return (Var v, \env body -> Let (NonRec v env) - $ Case len lv (exprType body) [(DEFAULT, [], body)]) + = return (Var v, \env body -> + do + len <- lengthPA (Var v) + return . Let (NonRec v env) + $ Case len lv (exprType body) [(DEFAULT, [], body)]) -- NOTE: this transparently deals with empty environments mkLiftEnv lv tys vs @@ -306,9 +327,13 @@ mkLiftEnv lv tys vs bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env in - Case scrut (mkWildId (exprType scrut)) (exprType body) - [(DataAlt env_con, lv : vs, body)] + return $ Case scrut (mkWildId (exprType scrut)) + (exprType body) + [(DataAlt env_con, lv : bndrs, body)] return (env, bind) where vty = mkCoreTupTy tys + bndrs | null vs = [mkWildId unitTy] + | otherwise = vs +