-vectTopBinder :: Var -> VM Var
-vectTopBinder var
- = do
- vty <- vectType (idType var)
- name <- cloneName mkVectOcc (getName var)
- let var' | isExportedId var = Id.mkExportedLocalId name vty
- | otherwise = Id.mkLocalId name vty
- defGlobalVar var var'
- return var'
-
-vectTopRhs :: CoreExpr -> VM CoreExpr
-vectTopRhs = liftM fst . closedV . vectPolyExpr (panic "Empty lifting context") . freeVars
-
--- ----------------------------------------------------------------------------
--- Bindings
-
-vectBndr :: Var -> VM VVar
-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 (vv, lv) }
-
-vectBndrIn :: Var -> VM a -> VM (VVar, a)
-vectBndrIn v p
- = localV
- $ do
- vv <- vectBndr v
- x <- p
- return (vv, x)
-
-vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
-vectBndrsIn vs p
- = localV
- $ do
- vvs <- mapM vectBndr vs
- x <- p
- return (vvs, x)
-
--- ----------------------------------------------------------------------------
--- Expressions
-
-capply :: VExpr -> VExpr -> VM VExpr
-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 :: Var -> Var -> VM VExpr
-vectVar lc v
- = do
- r <- lookupVar v
- case r of
- Local (vv,lv) -> return (Var vv, Var lv)
- Global vv -> do
- let vexpr = Var vv
- lexpr <- replicatePA vexpr (Var lc)
- return (vexpr, lexpr)
-
-vectPolyVar :: Var -> Var -> [Type] -> VM VExpr
-vectPolyVar lc v tys
- = do
- vtys <- mapM vectType tys
- r <- lookupVar v
- case r of
- Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
- (polyApply (Var lv) vtys)
- Global poly -> do
- vexpr <- polyApply (Var poly) vtys
- lexpr <- replicatePA vexpr (Var lc)
- return (vexpr, lexpr)
-
-vectLiteral :: Var -> Literal -> VM VExpr
-vectLiteral lc lit
- = do
- lexpr <- replicatePA (Lit lit) (Var lc)
- return (Lit lit, lexpr)
-
-vectPolyExpr :: Var -> CoreExprWithFVs -> VM VExpr
-vectPolyExpr lc expr
- = polyAbstract tvs $ \abstract ->
- -- FIXME: shadowing (tvs in lc)
- do
- mono' <- vectExpr lc mono
- return $ mapVect abstract mono'
- where
- (tvs, mono) = collectAnnTypeBinders expr
-
-vectExpr :: Var -> CoreExprWithFVs -> VM VExpr
-vectExpr lc (_, AnnType ty)
- = liftM vType (vectType ty)
-
-vectExpr lc (_, AnnVar v) = vectVar lc v
-
-vectExpr lc (_, AnnLit lit) = vectLiteral lc lit
-
-vectExpr lc (_, AnnNote note expr)
- = liftM (vNote note) (vectExpr lc expr)
-
-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 <- vectPolyExpr lc rhs
- (vbndr, vbody) <- vectBndrIn bndr (vectExpr lc body)
- return $ vLet (vNonRec vbndr vrhs) vbody
-
-vectExpr lc (_, AnnLet (AnnRec bs) body)
- = do
- (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
- $ liftM2 (,)
- (mapM (vectExpr lc) rhss)
- (vectPolyExpr lc body)
- return $ vLet (vRec vbndrs vrhss) vbody
- where
- (bndrs, rhss) = unzip bs
-
-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 (Var 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 <- polyApply (Var vfn_var) (mkTyVarTys tyvars)
- mono_lfn <- polyApply (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, (Var v, Var v')) | var <- varSetElems fvs
- , Just (v,v') <- [lookupVarEnv locals var]]
- vtys <- mapM (vectType . varType) vars
-
- (vty, repr_tycon, repr_tyargs, repr_datacon) <- mk_env_ty vtys
- lty <- mkPArrayType vty
+ unlessSomeNoVectDecl vectorise
+ = do { hasNoVectDecls <- mapM noVectDecl vars
+ ; when (and hasNoVectDecls) $
+ traceVt "NOVECTORISE" $ ppr vars
+ ; if and hasNoVectDecls
+ then return b -- all bindings have 'NOVECTORISE'
+ else if or hasNoVectDecls
+ then cantVectorise noVectoriseErr (ppr b) -- some (but not all) have 'NOVECTORISE'
+ else vectorise -- no binding has a 'NOVECTORISE' decl
+ }
+ noVectoriseErr = "NOVECTORISE must be used on all or no bindings of a recursive group"
+
+-- | Make the vectorised version of this top level binder, and add the mapping
+-- between it and the original to the state. For some binder @foo@ the vectorised
+-- version is @$v_foo@
+--
+-- NOTE: 'vectTopBinder' *MUST* be lazy in inline and expr because of how it is
+-- used inside of 'fixV' in 'vectTopBind'.
+--
+vectTopBinder :: Var -- ^ Name of the binding.
+ -> Inline -- ^ Whether it should be inlined, used to annotate it.
+ -> CoreExpr -- ^ RHS of binding, used to set the 'Unfolding' of the returned 'Var'.
+ -> VM Var -- ^ Name of the vectorised binding.
+vectTopBinder var inline expr
+ = do { -- Vectorise the type attached to the var.
+ ; vty <- vectType (idType var)