X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Fvectorise%2FVectUtils.hs;h=df8c23ff5e26651de62f8e8cbc8d221f21813440;hb=346516b3930d677616e5108499d3a82b51f58853;hp=57571ab6302801604bf42216dcfeec7f86321137;hpb=6326f92d3f33f1d40d2ffa66021197fd84960742;p=ghc-hetmet.git diff --git a/compiler/vectorise/VectUtils.hs b/compiler/vectorise/VectUtils.hs index 57571ab..df8c23f 100644 --- a/compiler/vectorise/VectUtils.hs +++ b/compiler/vectorise/VectUtils.hs @@ -3,12 +3,13 @@ module VectUtils ( collectAnnValBinders, splitClosureTy, mkPADictType, mkPArrayType, - paDictArgType, paDictOfType, - paMethod, lengthPA, replicatePA, emptyPA, + paDictArgType, paDictOfType, paDFunType, + paMethod, lengthPA, replicatePA, emptyPA, liftPA, polyAbstract, polyApply, polyVApply, lookupPArrayFamInst, - hoistExpr, hoistPolyVExpr, takeHoisted, - buildClosure, buildClosures + hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted, + buildClosure, buildClosures, + mkClosureApp ) where #include "HsVersions.h" @@ -96,7 +97,7 @@ mkClosureTypes arg_tys res_ty mkPADictType :: Type -> VM Type mkPADictType ty = do - tc <- builtin paDictTyCon + tc <- builtin paTyCon return $ TyConApp tc [ty] mkPArrayType :: Type -> VM Type @@ -139,11 +140,21 @@ paDictOfTyApp (TyVarTy tv) ty_args paDFunApply dfun ty_args paDictOfTyApp (TyConApp tc _) ty_args = do - pa_class <- builtin paClass - (dfun, ty_args') <- lookupInst pa_class [TyConApp tc ty_args] - paDFunApply (Var dfun) ty_args' + dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc) + paDFunApply (Var dfun) ty_args paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty) +paDFunType :: TyCon -> VM Type +paDFunType tc + = do + margs <- mapM paDictArgType tvs + res <- mkPADictType (mkTyConApp tc arg_tys) + return . mkForAllTys tvs + $ mkFunTys [arg | Just arg <- margs] res + where + tvs = tyConTyVars tc + arg_tys = mkTyVarTys tvs + paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr paDFunApply dfun tys = do @@ -169,6 +180,12 @@ replicatePA len x = liftM (`mkApps` [len,x]) emptyPA :: Type -> VM CoreExpr emptyPA = paMethod emptyPAVar +liftPA :: CoreExpr -> VM CoreExpr +liftPA x + = do + lc <- builtin liftingContext + replicatePA (Var lc) x + newLocalVVar :: FastString -> Type -> VM VVar newLocalVVar fs vty = do @@ -208,27 +225,31 @@ polyVApply expr tys 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 } + hoistExpr :: FastString -> CoreExpr -> VM Var hoistExpr fs expr = do var <- newLocalVar fs (exprType expr) - updGEnv $ \env -> - env { global_bindings = (var, expr) : global_bindings env } + hoistBinding var expr return var -hoistVExpr :: FastString -> VExpr -> VM VVar -hoistVExpr fs (ve, le) +hoistVExpr :: VExpr -> VM VVar +hoistVExpr (ve, le) = do + fs <- getBindName vv <- hoistExpr ('v' `consFS` fs) ve lv <- hoistExpr ('l' `consFS` fs) le return (vv, lv) -hoistPolyVExpr :: FastString -> [TyVar] -> VM VExpr -> VM VExpr -hoistPolyVExpr fs tvs p +hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr +hoistPolyVExpr tvs p = do expr <- closedV . polyAbstract tvs $ \abstract -> liftM (mapVect abstract) p - fn <- hoistVExpr fs expr + fn <- hoistVExpr expr polyVApply (vVar fn) (mkTyVarTys tvs) takeHoisted :: VM [(Var, CoreExpr)] @@ -238,7 +259,6 @@ takeHoisted setGEnv $ env { global_bindings = [] } return $ global_bindings env - mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv) = do @@ -248,17 +268,28 @@ 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 +mkClosureApp :: VExpr -> VExpr -> VM VExpr +mkClosureApp (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 [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 - buildClosure tvs lc vars arg_ty res_ty' - . hoistPolyVExpr FSLIT("fn") tvs + buildClosure tvs vars arg_ty res_ty' + . hoistPolyVExpr tvs $ do - clo <- buildClosures tvs lc (vars ++ [arg]) arg_tys res_ty mk_body + lc <- builtin liftingContext + clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body return $ vLams lc (vars ++ [arg]) clo -- (clo , aclo (Arr lc xs1 ... xsn) ) @@ -266,27 +297,29 @@ buildClosures tvs lc vars (arg_ty : arg_tys) res_ty mk_body -- 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] -> Type -> Type -> VM VExpr -> VM VExpr -buildClosure tvs lv vars arg_ty res_ty mk_body +buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr +buildClosure tvs vars arg_ty res_ty mk_body = do - (env_ty, env, bind) <- buildEnv lv vars + (env_ty, env, bind) <- buildEnv vars env_bndr <- newLocalVVar FSLIT("env") env_ty arg_bndr <- newLocalVVar FSLIT("arg") arg_ty - fn <- hoistPolyVExpr FSLIT("fn") tvs + fn <- hoistPolyVExpr tvs $ do + lc <- builtin liftingContext body <- mk_body body' <- bind (vVar env_bndr) - (vVarApps lv body (vars ++ [arg_bndr])) + (vVarApps lc body (vars ++ [arg_bndr])) return (vLamsWithoutLC [env_bndr, arg_bndr] body') mkClosure arg_ty res_ty env_ty fn env -buildEnv :: Var -> [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr) -buildEnv lv vvs +buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr) +buildEnv vvs = do + lc <- builtin liftingContext let (ty, venv, vbind) = mkVectEnv tys vs - (lenv, lbind) <- mkLiftEnv lv tys ls + (lenv, lbind) <- mkLiftEnv lc tys ls return (ty, (venv, lenv), \(venv,lenv) (vbody,lbody) -> do @@ -307,28 +340,28 @@ mkVectEnv tys vs = (ty, mkCoreTup (map Var vs), ty = mkCoreTupTy tys mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr) -mkLiftEnv lv [ty] [v] +mkLiftEnv lc [ty] [v] = return (Var v, \env body -> do len <- lengthPA (Var v) return . Let (NonRec v env) - $ Case len lv (exprType body) [(DEFAULT, [], body)]) + $ Case len lc (exprType body) [(DEFAULT, [], body)]) -- NOTE: this transparently deals with empty environments -mkLiftEnv lv tys vs +mkLiftEnv lc tys vs = do (env_tc, env_tyargs) <- lookupPArrayFamInst vty let [env_con] = tyConDataCons env_tc env = Var (dataConWrapId env_con) `mkTyApps` env_tyargs - `mkVarApps` (lv : vs) + `mkVarApps` (lc : vs) bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env in return $ Case scrut (mkWildId (exprType scrut)) (exprType body) - [(DataAlt env_con, lv : bndrs, body)] + [(DataAlt env_con, lc : bndrs, body)] return (env, bind) where vty = mkCoreTupTy tys