-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 = local v `orElseV` global v
- where
- local v = maybeV (readLEnv $ \env -> lookupVarEnv (local_vars env) v)
- global v = do
- vexpr <- maybeV (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
- lexpr <- replicateP vexpr lc
- return (vexpr, lexpr)
-
-vectPolyVar :: CoreExpr -> Var -> [Type] -> VM (CoreExpr, CoreExpr)
-vectPolyVar lc v tys
- = do
- r <- readLEnv $ \env -> lookupVarEnv (local_vars env) v
- case r of
- Just (vexpr, lexpr) -> liftM2 (,) (mk_app vexpr) (mk_app lexpr)
- Nothing ->
- do
- poly <- maybeV (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
- vexpr <- mk_app poly
- lexpr <- replicateP 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 (deleteTyVarPA tv) (extendTyVarPA 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 [arg | (tv, mdict) <- zip tvs mdicts
- , arg <- tv : maybeToList mdict]
-
-applyToTypes :: CoreExpr -> [Type] -> VM CoreExpr
-applyToTypes expr tys
- = do
- dicts <- mapM paDictOfType tys
- return $ mkApps expr [arg | (ty, dict) <- zip tys dicts
- , arg <- [Type ty, dict]]
-
-
-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 <- replicateP 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
- let tyvars = filter isTyVar (varSetElems fvs)
- info <- mkCEnvInfo fvs bndr body
- (poly_vfn, poly_lfn) <- mkClosureFns info tyvars bndr body
- 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 poly_vfn (map TyVarTy tyvars)
- mono_lfn <- applyToTypes poly_lfn (map TyVarTy 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
-
- return $ CEnvInfo {
- cenv_vars = vars
- , cenv_values = vals
- , cenv_vty = vty
- , cenv_lty = lty
- , cenv_repr_tycon = repr_tycon
- , cenv_repr_tyargs = repr_tyargs
- , cenv_repr_datacon = repr_datacon
+-- | Vectorise a single module, in the VM monad.
+--
+vectModule :: ModGuts -> VM ModGuts
+vectModule guts@(ModGuts { mg_types = types
+ , mg_binds = binds
+ , mg_fam_insts = fam_insts
+ })
+ = do { dumpOptVt Opt_D_dump_vt_trace "Before vectorisation" $
+ pprCoreBindings binds
+
+ -- Vectorise the type environment.
+ -- This may add new TyCons and DataCons.
+ ; (types', new_fam_insts, tc_binds) <- vectTypeEnv types
+
+ ; (_, fam_inst_env) <- readGEnv global_fam_inst_env
+
+ -- dicts <- mapM buildPADict pa_insts
+ -- workers <- mapM vectDataConWorkers pa_insts
+
+ -- Vectorise all the top level bindings.
+ ; binds' <- mapM vectTopBind binds
+
+ ; return $ guts { mg_types = types'
+ , mg_binds = Rec tc_binds : binds'
+ , mg_fam_inst_env = fam_inst_env
+ , mg_fam_insts = fam_insts ++ new_fam_insts
+ }
+ }
+
+-- | Try to vectorise a top-level binding.
+-- If it doesn't vectorise then return it unharmed.
+--
+-- For example, for the binding
+--
+-- @
+-- foo :: Int -> Int
+-- foo = \x -> x + x
+-- @
+--
+-- we get
+-- @
+-- foo :: Int -> Int
+-- foo = \x -> vfoo $: x
+--
+-- v_foo :: Closure void vfoo lfoo
+-- v_foo = closure vfoo lfoo void
+--
+-- vfoo :: Void -> Int -> Int
+-- vfoo = ...
+--
+-- lfoo :: PData Void -> PData Int -> PData Int
+-- lfoo = ...
+-- @
+--
+-- @vfoo@ is the "vectorised", or scalar, version that does the same as the original
+-- function foo, but takes an explicit environment.
+--
+-- @lfoo@ is the "lifted" version that works on arrays.
+--
+-- @v_foo@ combines both of these into a `Closure` that also contains the
+-- environment.
+--
+-- The original binding @foo@ is rewritten to call the vectorised version
+-- present in the closure.
+--
+vectTopBind :: CoreBind -> VM CoreBind
+vectTopBind b@(NonRec var expr)
+ = do { -- Vectorise the right-hand side, create an appropriate top-level binding and add it to
+ -- the vectorisation map.
+ ; (inline, isScalar, expr') <- vectTopRhs [] var expr
+ ; var' <- vectTopBinder var inline expr'
+ ; when isScalar $
+ addGlobalScalar var
+
+ -- We replace the original top-level binding by a value projected from the vectorised
+ -- closure and add any newly created hoisted top-level bindings.
+ ; cexpr <- tryConvert var var' expr
+ ; hs <- takeHoisted
+ ; return . Rec $ (var, cexpr) : (var', expr') : hs
+ }
+ `orElseV`
+ return b
+vectTopBind b@(Rec bs)
+ = let (vars, exprs) = unzip bs
+ in
+ do { (vars', _, exprs', hs) <- fixV $
+ \ ~(_, inlines, rhss, _) ->
+ do { -- Vectorise the right-hand sides, create an appropriate top-level bindings and
+ -- add them to the vectorisation map.
+ ; vars' <- sequence [vectTopBinder var inline rhs
+ | (var, ~(inline, rhs)) <- zipLazy vars (zip inlines rhss)]
+ ; (inlines, areScalars, exprs') <- mapAndUnzip3M (uncurry $ vectTopRhs vars) bs
+ ; hs <- takeHoisted
+ ; if and areScalars
+ then -- (1) Entire recursive group is scalar
+ -- => add all variables to the global set of scalars
+ do { mapM addGlobalScalar vars
+ ; return (vars', inlines, exprs', hs)
+ }
+ else -- (2) At least one binding is not scalar
+ -- => vectorise again with empty set of local scalars
+ do { (inlines, _, exprs') <- mapAndUnzip3M (uncurry $ vectTopRhs []) bs
+ ; hs <- takeHoisted
+ ; return (vars', inlines, exprs', hs)
+ }