Add vectorisation built-ins
[ghc-hetmet.git] / compiler / vectorise / Vectorise.hs
index 31defa5..c1d4e19 100644 (file)
@@ -1,3 +1,10 @@
+{-# OPTIONS -w #-}
+-- The above warning supression flag is a temporary kludge.
+-- While working on this module you are encouraged to remove it and fix
+-- any warnings in the module. See
+--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
+-- for details
+
 module Vectorise( vectorise )
 where
 
@@ -45,19 +52,6 @@ import Outputable
 import FastString
 import Control.Monad        ( liftM, liftM2, zipWithM, mapAndUnzipM )
 
-builtin_PAs :: [(Name, Module, FastString)]
-builtin_PAs = [
-                (closureTyConName, nDP_CLOSURE, FSLIT("dPA_Clo"))
-              , mk intTyConName     FSLIT("dPA_Int")
-              ]
-              ++ tups
-  where
-    mk name fs = (name, nDP_INSTANCES, fs)
-
-    tups = mk_tup 0 : map mk_tup [2..3]
-    mk_tup n   = (getName $ tupleTyCon Boxed n, nDP_INSTANCES,
-                  mkFastString $ "dPA_" ++ show n)
-
 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
           -> IO (SimplCount, ModGuts)
 vectorise hsc_env _ _ guts
@@ -74,7 +68,6 @@ vectorise hsc_env _ _ guts
 vectModule :: ModGuts -> VM ModGuts
 vectModule guts
   = do
-      defTyConBuiltinPAs builtin_PAs
       (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
       
       let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
@@ -140,6 +133,16 @@ vectBndr v
   where
     mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
 
+vectBndrNew :: Var -> FastString -> VM VVar
+vectBndrNew v fs
+  = do
+      vty <- vectType (idType v)
+      vv  <- newLocalVVar fs vty
+      updLEnv (upd vv)
+      return vv
+  where
+    upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
+
 vectBndrIn :: Var -> VM a -> VM (VVar, a)
 vectBndrIn v p
   = localV
@@ -148,6 +151,14 @@ vectBndrIn v p
       x <- p
       return (vv, x)
 
+vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
+vectBndrNewIn v fs p
+  = localV
+  $ do
+      vv <- vectBndrNew v fs
+      x  <- p
+      return (vv, x)
+
 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
 vectBndrIn' v p
   = localV
@@ -225,9 +236,13 @@ vectExpr e@(_, AnnApp _ arg)
 
 vectExpr (_, AnnApp fn arg)
   = do
-      fn'  <- vectExpr fn
-      arg' <- vectExpr arg
-      mkClosureApp fn' arg'
+      arg_ty' <- vectType arg_ty
+      res_ty' <- vectType res_ty
+      fn'     <- vectExpr fn
+      arg'    <- vectExpr arg
+      mkClosureApp arg_ty' res_ty' fn' arg'
+  where
+    (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
 
 vectExpr (_, AnnCase scrut bndr ty alts)
   | isAlgType scrut_ty
@@ -295,11 +310,12 @@ type CoreAltWithFVs = AnnAlt Id VarSet
 --
 -- to
 --
---   V:    let v = e in case v of _ { ... }
---   L:    let v = e in case v `cast` ... of _ { ... }
+--   V:    let v' = e in case v' of _ { ... }
+--   L:    let v' = e in case v' `cast` ... of _ { ... }
 --
 -- When lifting, we have to do it this way because v must have the type
--- [:V(T):] but the scrutinee must be cast to the representation type.
+-- [:V(T):] but the scrutinee must be cast to the representation type. We also
+-- have to handle the case where v is a wild var correctly.
 --   
 
 -- FIXME: this is too lazy
@@ -316,7 +332,7 @@ vectAlgCase scrut bndr ty [(DataAlt dc, bndrs, body)]
       vty <- vectType ty
       lty <- mkPArrayType vty
       vexpr <- vectExpr scrut
-      (vbndr, (vbndrs, vbody)) <- vectBndrIn bndr
+      (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
                                 . vectBndrsIn bndrs
                                 $ vectExpr body
 
@@ -328,3 +344,7 @@ vectAlgCase scrut bndr ty [(DataAlt dc, bndrs, body)]
       shape_bndrs <- mapM (newLocalVar FSLIT("s")) shape_tys
       return . vLet (vNonRec vbndr vexpr)
              $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
+  where
+    vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr FSLIT("scrut")
+                    | otherwise         = vectBndrIn bndr
+