-vectTopRhs :: CoreExpr -> VM CoreExpr
-vectTopRhs = liftM fst . closedV . vectPolyExpr (panic "Empty lifting context") . freeVars
-
--- ----------------------------------------------------------------------------
--- Bindings
-
-vectBndr :: Var -> VM (Var, Var)
-vectBndr v
- = do
- vty <- vectType (idType v)
- lty <- mkPArrayType vty
- let vv = v `Id.setIdType` vty
- lv = v `Id.setIdType` lty
- updLEnv (mapTo vv lv)
- return (vv, lv)
- where
- mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (Var vv, Var lv) }
-
-vectBndrIn :: Var -> VM a -> VM (Var, Var, a)
-vectBndrIn v p
- = localV
- $ do
- (vv, lv) <- vectBndr v
- x <- p
- return (vv, lv, x)
-
-vectBndrsIn :: [Var] -> VM a -> VM ([Var], [Var], a)
-vectBndrsIn vs p
- = localV
- $ do
- (vvs, lvs) <- mapAndUnzipM vectBndr vs
- x <- p
- return (vvs, lvs, x)
-
--- ----------------------------------------------------------------------------
--- Expressions
-
-capply :: (CoreExpr, CoreExpr) -> (CoreExpr, CoreExpr) -> VM (CoreExpr, CoreExpr)
-capply (vfn, lfn) (varg, larg)
- = do
- apply <- builtin applyClosureVar
- applyP <- builtin applyClosurePVar
- return (mkApps (Var apply) [Type arg_ty, Type res_ty, vfn, varg],
- mkApps (Var applyP) [Type arg_ty, Type res_ty, lfn, larg])
- where
- fn_ty = exprType vfn
- (arg_ty, res_ty) = splitClosureTy fn_ty
-
-vectVar :: CoreExpr -> Var -> VM (CoreExpr, CoreExpr)
-vectVar lc v
- = do
- r <- lookupVar v
- case r of
- Local es -> return es
- Global vexpr -> do
- lexpr <- replicatePA vexpr lc
- return (vexpr, lexpr)
-
-vectPolyVar :: CoreExpr -> Var -> [Type] -> VM (CoreExpr, CoreExpr)
-vectPolyVar lc v tys
- = do
- r <- lookupVar v
- case r of
- Local (vexpr, lexpr) -> liftM2 (,) (mk_app vexpr) (mk_app lexpr)
- Global poly -> do
- vexpr <- mk_app poly
- lexpr <- replicatePA vexpr lc
- return (vexpr, lexpr)
- where
- mk_app e = applyToTypes e =<< mapM vectType tys
-
-abstractOverTyVars :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
-abstractOverTyVars tvs p
- = do
- mdicts <- mapM mk_dict_var tvs
- zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
- p (mk_lams mdicts)
- where
- mk_dict_var tv = do
- r <- paDictArgType tv
- case r of
- Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
- Nothing -> return Nothing
-
- mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
-
-applyToTypes :: CoreExpr -> [Type] -> VM CoreExpr
-applyToTypes expr tys
- = do
- dicts <- mapM paDictOfType tys
- return $ expr `mkTyApps` tys `mkApps` dicts
-
-vectPolyExpr :: CoreExpr -> CoreExprWithFVs -> VM (CoreExpr, CoreExpr)
-vectPolyExpr lc expr
- = localV
- . abstractOverTyVars tvs $ \mk_lams ->
- -- FIXME: shadowing (tvs in lc)
- do
- (vmono, lmono) <- vectExpr lc mono
- return $ (mk_lams vmono, mk_lams lmono)
- where
- (tvs, mono) = collectAnnTypeBinders expr
-
-vectExpr :: CoreExpr -> CoreExprWithFVs -> VM (CoreExpr, CoreExpr)
-vectExpr lc (_, AnnType ty)
- = do
- vty <- vectType ty
- return (Type vty, Type vty)
-
-vectExpr lc (_, AnnVar v) = vectVar lc v
-
-vectExpr lc (_, AnnLit lit)
- = do
- let vexpr = Lit lit
- lexpr <- replicatePA vexpr lc
- return (vexpr, lexpr)
-
-vectExpr lc (_, AnnNote note expr)
- = do
- (vexpr, lexpr) <- vectExpr lc expr
- return (Note note vexpr, Note note lexpr)
-
-vectExpr lc e@(_, AnnApp _ arg)
- | isAnnTypeArg arg
- = vectTyAppExpr lc fn tys
- where
- (fn, tys) = collectAnnTypeArgs e
-
-vectExpr lc (_, AnnApp fn arg)
- = do
- fn' <- vectExpr lc fn
- arg' <- vectExpr lc arg
- capply fn' arg'
-
-vectExpr lc (_, AnnCase expr bndr ty alts)
- = panic "vectExpr: case"
-
-vectExpr lc (_, AnnLet (AnnNonRec bndr rhs) body)
- = do
- (vrhs, lrhs) <- vectPolyExpr lc rhs
- (vbndr, lbndr, (vbody, lbody)) <- vectBndrIn bndr (vectExpr lc body)
- return (Let (NonRec vbndr vrhs) vbody,
- Let (NonRec lbndr lrhs) lbody)
-
-vectExpr lc (_, AnnLet (AnnRec prs) body)
- = do
- (vbndrs, lbndrs, (vrhss, vbody, lrhss, lbody)) <- vectBndrsIn bndrs vect
- return (Let (Rec (zip vbndrs vrhss)) vbody,
- Let (Rec (zip lbndrs lrhss)) lbody)
- where
- (bndrs, rhss) = unzip prs
-
- vect = do
- (vrhss, lrhss) <- mapAndUnzipM (vectExpr lc) rhss
- (vbody, lbody) <- vectPolyExpr lc body
- return (vrhss, vbody, lrhss, lbody)
-
-vectExpr lc e@(_, AnnLam bndr body)
- | isTyVar bndr = pprPanic "vectExpr" (ppr $ deAnnotate e)
-
-vectExpr lc (fvs, AnnLam bndr body)
- = do
- tyvars <- localTyVars
- info <- mkCEnvInfo fvs bndr body
- (poly_vfn, poly_lfn) <- mkClosureFns info tyvars bndr body
-
- vfn_var <- hoistExpr FSLIT("vfn") poly_vfn
- lfn_var <- hoistExpr FSLIT("lfn") poly_lfn
-
- let (venv, lenv) = mkClosureEnvs info lc
-
- let env_ty = cenv_vty info
-
- pa_dict <- paDictOfType env_ty
-
- arg_ty <- vectType (varType bndr)
- res_ty <- vectType (exprType $ deAnnotate body)
-
- -- FIXME: move the functions to the top level
- mono_vfn <- applyToTypes (Var vfn_var) (mkTyVarTys tyvars)
- mono_lfn <- applyToTypes (Var lfn_var) (mkTyVarTys tyvars)
-
- mk_clo <- builtin mkClosureVar
- mk_cloP <- builtin mkClosurePVar
-
- let vclo = Var mk_clo `mkTyApps` [arg_ty, res_ty, env_ty]
- `mkApps` [pa_dict, mono_vfn, mono_lfn, venv]
-
- lclo = Var mk_cloP `mkTyApps` [arg_ty, res_ty, env_ty]
- `mkApps` [pa_dict, mono_vfn, mono_lfn, lenv]
-
- return (vclo, lclo)
-
-
-data CEnvInfo = CEnvInfo {
- cenv_vars :: [Var]
- , cenv_values :: [(CoreExpr, CoreExpr)]
- , cenv_vty :: Type
- , cenv_lty :: Type
- , cenv_repr_tycon :: TyCon
- , cenv_repr_tyargs :: [Type]
- , cenv_repr_datacon :: DataCon
- }
-
-mkCEnvInfo :: VarSet -> Var -> CoreExprWithFVs -> VM CEnvInfo
-mkCEnvInfo fvs arg body
- = do
- locals <- readLEnv local_vars
- let
- (vars, vals) = unzip
- [(var, val) | var <- varSetElems fvs
- , Just val <- [lookupVarEnv locals var]]
- vtys <- mapM (vectType . varType) vars
-
- (vty, repr_tycon, repr_tyargs, repr_datacon) <- mk_env_ty vtys
- lty <- mkPArrayType vty