Thread lifting context implicitly in the vectorisation monad
[ghc-hetmet.git] / compiler / vectorise / VectUtils.hs
index 545460d..0727c94 100644 (file)
@@ -1,16 +1,20 @@
 module VectUtils (
   collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
+  collectAnnValBinders,
   splitClosureTy,
   mkPADictType, mkPArrayType,
   paDictArgType, paDictOfType,
-  paMethod, lengthPA, replicatePA, emptyPA,
-  polyAbstract, polyApply,
+  paMethod, lengthPA, replicatePA, emptyPA, liftPA,
+  polyAbstract, polyApply, polyVApply,
   lookupPArrayFamInst,
-  hoistExpr, takeHoisted
+  hoistExpr, hoistPolyVExpr, takeHoisted,
+  buildClosure, buildClosures,
+  mkClosureApp
 ) where
 
 #include "HsVersions.h"
 
+import VectCore
 import VectMonad
 
 import DsUtils
@@ -44,6 +48,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 +80,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,6 +170,20 @@ 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
+      lty <- mkPArrayType vty
+      vv  <- newLocalVar fs vty
+      lv  <- newLocalVar fs lty
+      return (vv,lv)
+
 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
 polyAbstract tvs p
   = localV
@@ -168,6 +206,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])
 
@@ -179,12 +223,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
+      fs <- getBindName
+      vv <- hoistExpr ('v' `consFS` fs) ve
+      lv <- hoistExpr ('l' `consFS` fs) le
+      return (vv, lv)
+
+hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
+hoistPolyVExpr tvs p
   = do
-      poly_expr <- closedV . polyAbstract tvs $ \abstract -> return (abstract expr)
-      fn        <- hoistExpr fs poly_expr
-      polyApply (Var fn) (mkTyVarTys tvs)
+      expr <- closedV . polyAbstract tvs $ \abstract ->
+              liftM (mapVect abstract) p
+      fn   <- hoistVExpr expr
+      polyVApply (vVar fn) (mkTyVarTys tvs)
 
 takeHoisted :: VM [(Var, CoreExpr)]
 takeHoisted
@@ -193,59 +246,77 @@ takeHoisted
       setGEnv $ env { global_bindings = [] }
       return $ global_bindings env
 
-
-mkClosure :: Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr
-mkClosure arg_ty res_ty env_ty pa_dict vfn lfn env
+mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
+mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
+  = do
+      dict <- paDictOfType env_ty
+      mkv  <- builtin mkClosureVar
+      mkl  <- builtin mkClosurePVar
+      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])
+
+mkClosureApp :: VExpr -> VExpr -> VM VExpr
+mkClosureApp (vclo, lclo) (varg, larg)
   = do
-      mk <- builtin mkClosureVar
-      return $ Var mk `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [pa_dict, vfn, lfn, env]
+      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)
 
-mkClosureP :: Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr
-mkClosureP arg_ty res_ty env_ty pa_dict vfn lfn env
+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
-      mk <- builtin mkClosurePVar
-      return $ Var mk `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [pa_dict, vfn, lfn, env]
+      res_ty' <- mkClosureTypes arg_tys res_ty
+      arg <- newLocalVVar FSLIT("x") arg_ty
+      buildClosure tvs vars arg_ty res_ty'
+        . hoistPolyVExpr tvs
+        $ do
+            lc <- builtin liftingContext
+            clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
+            return $ vLams lc (vars ++ [arg]) clo
 
 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
 --   where
 --     f  = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
 --     f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
-
-buildClosure :: [TyVar] -> [(Var,Var)] -> (Var,Var) -> Var -> CoreExpr -> CoreExpr -> VM (CoreExpr, CoreExpr)
-buildClosure tvs env (varg, larg) lv vbody lbody
+--
+buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
+buildClosure tvs vars arg_ty res_ty mk_body
   = do
-      let (venv_ty, venv, bind_venv) = mkVectEnv tys vs
-      (lenv, bind_lenv) <- mkLiftEnv (Var lv) tys ls
-      lenv_ty <- mkPArrayType venv_ty
-
-      venv_bndr <- newLocalVar FSLIT("env") venv_ty
-      lenv_bndr <- newLocalVar FSLIT("env") lenv_ty
-
-      let mono_vfn = mkLams [venv_bndr, varg]
-                   . bind_venv (Var venv_bndr)
-                   $ vbody `mkVarApps` vs `mkVarApps` [varg]
-          mono_lfn = mkLams [lenv_bndr, larg]
-                   . bind_lenv (Var lenv_bndr) lv
-                   $ lbody `mkVarApps` (lv:ls) `mkVarApps` [larg]
-
-      vfn <- hoistPolyExpr FSLIT("vfn") tvs mono_vfn
-      lfn <- hoistPolyExpr FSLIT("lfn") tvs mono_lfn
-
-      pa_dict <- paDictOfType venv_ty
-
-      vclo <- mkClosure  arg_ty res_ty venv_ty pa_dict vfn lfn venv
-      lclo <- mkClosureP arg_ty res_ty venv_ty pa_dict vfn lfn lenv
-
-      return (vclo, lclo)
-
+      (env_ty, env, bind) <- buildEnv vars
+      env_bndr <- newLocalVVar FSLIT("env") env_ty
+      arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
+
+      fn <- hoistPolyVExpr tvs
+          $ do
+              lc    <- builtin liftingContext
+              body  <- mk_body
+              body' <- bind (vVar env_bndr)
+                            (vVarApps lc body (vars ++ [arg_bndr]))
+              return (vLamsWithoutLC [env_bndr, arg_bndr] body')
+
+      mkClosure arg_ty res_ty env_ty fn env
+
+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 lc tys ls
+      return (ty, (venv, lenv),
+              \(venv,lenv) (vbody,lbody) ->
+              do
+                let vbody' = vbind venv vbody
+                lbody' <- lbind lenv lbody
+                return (vbody', lbody'))
   where
-    vs  = map fst env
-    ls  = map snd env
-    tys = map idType vs
+    (vs,ls) = unzip vvs
+    tys     = map idType vs
 
-    arg_ty = idType varg
-    res_ty = exprType vbody
-    
 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
 mkVectEnv []   []  = (unitTy, Var unitDataConId, \env body -> body)
 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
@@ -255,13 +326,13 @@ mkVectEnv tys  vs  = (ty, mkCoreTup (map Var vs),
   where
     ty = mkCoreTupTy tys
 
-mkLiftEnv :: CoreExpr -> [Type] -> [Var]
-          -> VM (CoreExpr, CoreExpr -> Var -> CoreExpr -> CoreExpr)
+mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
 mkLiftEnv lc [ty] [v]
-  = do
-      len <- lengthPA (Var v)
-      return (Var v, \env lv 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 lc (exprType body) [(DEFAULT, [], body)])
 
 -- NOTE: this transparently deals with empty environments
 mkLiftEnv lc tys vs
@@ -270,14 +341,18 @@ mkLiftEnv lc tys vs
       let [env_con] = tyConDataCons env_tc
           
           env = Var (dataConWrapId env_con)
-                `mkTyApps` env_tyargs
-                `mkApps`   (lc : map Var vs)
-
-          bind env lv body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
-                             in
-                             Case scrut (mkWildId (exprType scrut)) (exprType body)
-                               [(DataAlt env_con, lv : vs, body)]
+                `mkTyApps`  env_tyargs
+                `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, lc : bndrs, body)]
       return (env, bind)
   where
     vty = mkCoreTupTy tys
 
+    bndrs | null vs   = [mkWildId unitTy]
+          | otherwise = vs
+