Collect hoisted vectorised functions
[ghc-hetmet.git] / compiler / vectorise / VectUtils.hs
index acf19d4..5b70bf4 100644 (file)
@@ -1,5 +1,10 @@
 module VectUtils (
-  paDictArgType, paDictOfType
+  collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
+  splitClosureTy,
+  mkPADictType, mkPArrayType,
+  paDictArgType, paDictOfType,
+  lookupPArrayFamInst,
+  hoistExpr
 ) where
 
 #include "HsVersions.h"
@@ -7,11 +12,56 @@ module VectUtils (
 import VectMonad
 
 import CoreSyn
+import CoreUtils
 import Type
 import TypeRep
+import TyCon
 import Var
+import PrelNames
 
 import Outputable
+import FastString
+
+import Control.Monad         ( liftM )
+
+collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
+collectAnnTypeArgs expr = go expr []
+  where
+    go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
+    go e                             tys = (e, tys)
+
+collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
+collectAnnTypeBinders expr = go [] expr
+  where
+    go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
+    go bs e                           = (reverse bs, e)
+
+isAnnTypeArg :: AnnExpr b ann -> Bool
+isAnnTypeArg (_, AnnType t) = True
+isAnnTypeArg _              = False
+
+isClosureTyCon :: TyCon -> Bool
+isClosureTyCon tc = tyConUnique tc == closureTyConKey
+
+splitClosureTy :: Type -> (Type, Type)
+splitClosureTy ty
+  | Just (tc, [arg_ty, res_ty]) <- splitTyConApp_maybe ty
+  , isClosureTyCon tc
+  = (arg_ty, res_ty)
+
+  | otherwise = pprPanic "splitClosureTy" (ppr ty)
+
+mkPADictType :: Type -> VM Type
+mkPADictType ty
+  = do
+      tc <- builtin paDictTyCon
+      return $ TyConApp tc [ty]
+
+mkPArrayType :: Type -> VM Type
+mkPArrayType ty
+  = do
+      tc <- builtin parrayTyCon
+      return $ TyConApp tc [ty]
 
 paDictArgType :: TyVar -> VM (Maybe Type)
 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
@@ -29,9 +79,7 @@ paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
 
     go ty k
       | isLiftedTypeKind k
-      = do
-          tc <- builtin paDictTyCon
-          return . Just $ TyConApp tc [ty]
+      = liftM Just (mkPADictType ty)
 
     go ty k = return Nothing
 
@@ -60,3 +108,14 @@ paDFunApply dfun tys
       dicts <- mapM paDictOfType tys
       return $ mkApps (mkTyApps dfun tys) dicts
 
+lookupPArrayFamInst :: Type -> VM (TyCon, [Type])
+lookupPArrayFamInst ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
+
+hoistExpr :: FastString -> CoreExpr -> VM Var
+hoistExpr fs expr
+  = do
+      var <- newLocalVar fs (exprType expr)
+      updLEnv $ \env ->
+        env { local_bindings = (var, expr) : local_bindings env }
+      return var
+