Commit 122629e8 authored by chak@cse.unsw.edu.au.'s avatar chak@cse.unsw.edu.au.
Browse files

Fix and clean up 'PData' and 'Wrap' usage of the vectoriser

parent dc4235bc
......@@ -199,7 +199,8 @@ initBuiltinVars (Builtins { })
-- |Get a list of names to `TyCon`s in the mock prelude.
--
initBuiltinTyCons :: Builtins -> DsM [(Name, TyCon)]
-- FIXME: must be replaced by VECTORISE pragmas!!!
-- FIXME: * must be replaced by VECTORISE pragmas!!!
-- * then we can remove 'parrayTyCon' from the Builtins as well
initBuiltinTyCons bi
= do
return $ (tyConName funTyCon, closureTyCon bi)
......
......@@ -625,9 +625,8 @@ vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
. vectBndrsIn bndrs
$ vectExpr body
let (vect_bndrs, lift_bndrs) = unzip vbndrs
(vscrut, lscrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
(vscrut, lscrut, pdata_dc) <- pdataUnwrapScrut (vVar vbndr)
vect_dc <- maybeV dataConErr (lookupDataCon dc)
let [pdata_dc] = tyConDataCons pdata_tc
let vcase = mk_wild_case vscrut vty vect_dc vect_bndrs vect_body
lcase = mk_wild_case lscrut lty pdata_dc lift_bndrs lift_body
......@@ -657,8 +656,7 @@ vectAlgCase tycon _ty_args scrut bndr ty alts
let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
vexpr <- vectExpr scrut
(vect_scrut, lift_scrut, pdata_tc, _arg_tys) <- mkVScrut (vVar vbndr)
let [pdata_dc] = tyConDataCons pdata_tc
(vect_scrut, lift_scrut, pdata_dc) <- pdataUnwrapScrut (vVar vbndr)
let (vect_bodies, lift_bodies) = unzip vbodies
......
-- | Compute a description of the generic representation that we use for
-- a user defined data type.
-- |Compute a description of the generic representation that we use for a user defined data type.
--
-- During vectorisation, we generate a PRepr and PA instance for each user defined
-- data type. The PA dictionary contains methods to convert the user type to and
-- from our generic representation. This module computes a description of what
-- that generic representation is.
-- During vectorisation, we generate a PRepr and PA instance for each user defined
-- data type. The PA dictionary contains methods to convert the user type to and
-- from our generic representation. This module computes a description of what
-- that generic representation is.
--
module Vectorise.Generic.Description (
CompRepr (..), ProdRepr (..), ConRepr (..), SumRepr (..),
tyConRepr, sumReprType, conReprType, prodReprType, compReprType, compOrigType
) where
module Vectorise.Generic.Description
( CompRepr(..)
, ProdRepr(..)
, ConRepr(..)
, SumRepr(..)
, tyConRepr
, sumReprType
, compOrigType
)
where
import Vectorise.Utils
import Vectorise.Monad
......@@ -108,8 +112,8 @@ data CompRepr
-------------------------------------------------------------------------------
-- | Determine the generic representation of a data type, given its tycon.
-- The `TyCon` contains a description of the whole data type.
-- |Determine the generic representation of a data type, given its tycon.
--
tyConRepr :: TyCon -> VM SumRepr
tyConRepr tc
= sum_repr (tyConDataCons tc)
......@@ -129,9 +133,8 @@ tyConRepr tc
sum_tc <- builtin (sumTyCon arity)
-- Get the 'PData' and 'PDatas' tycons for the sum.
let sumapp = mkTyConApp sum_tc tys
psum_tc <- liftM fst $ pdataReprTyCon sumapp
psums_tc <- liftM fst $ pdatasReprTyCon sumapp
psum_tc <- pdataReprTyConExact sum_tc
psums_tc <- pdatasReprTyConExact sum_tc
sel_ty <- builtin (selTy arity)
sels_ty <- builtin (selsTy arity)
......@@ -165,9 +168,8 @@ tyConRepr tc
tup_tc <- builtin (prodTyCon arity)
-- Get the 'PData' and 'PDatas' tycons for the product.
let prodapp = mkTyConApp tup_tc tys'
ptup_tc <- liftM fst $ pdataReprTyCon prodapp
ptups_tc <- liftM fst $ pdatasReprTyCon prodapp
ptup_tc <- pdataReprTyConExact tup_tc
ptups_tc <- pdatasReprTyConExact tup_tc
return $ Prod
{ repr_tup_tc = tup_tc
......@@ -181,37 +183,35 @@ tyConRepr tc
comp_repr ty = liftM (Keep ty) (prDictOfReprType ty)
`orElseV` return (Wrap ty)
-- | Yield the type of this sum representation.
-- |Yield the type of this sum representation.
--
sumReprType :: SumRepr -> VM Type
sumReprType EmptySum = voidType
sumReprType (UnarySum r) = conReprType r
sumReprType (Sum { repr_sum_tc = sum_tc, repr_con_tys = tys })
= return $ mkTyConApp sum_tc tys
-- | Yield the type of this constructor representation.
-- Yield the type of this constructor representation.
--
conReprType :: ConRepr -> VM Type
conReprType (ConRepr _ r) = prodReprType r
-- | Yield the type of of this product representation.
-- Yield the type of of this product representation.
--
prodReprType :: ProdRepr -> VM Type
prodReprType EmptyProd = voidType
prodReprType (UnaryProd r) = compReprType r
prodReprType (Prod { repr_tup_tc = tup_tc, repr_comp_tys = tys })
= return $ mkTyConApp tup_tc tys
-- | Yield the type of this data constructor field \/ component representation.
-- Yield the type of this data constructor field \/ component representation.
--
compReprType :: CompRepr -> VM Type
compReprType (Keep ty _) = return ty
compReprType (Wrap ty)
= do wrap_tc <- builtin wrapTyCon
return $ mkTyConApp wrap_tc [ty]
compReprType (Wrap ty) = mkWrapType ty
-- Yield the original component type of a data constructor component representation.
-- |Yield the original component type of a data constructor component representation.
--
compOrigType :: CompRepr -> Type
compOrigType (Keep ty _) = ty
compOrigType (Wrap ty) = ty
......
......@@ -164,13 +164,13 @@ buildToPRepr vect_tc repr_tc _ _ repr
-- CoreExp to convert a data constructor component to the generic representation.
to_comp :: CoreExpr -> CompRepr -> VM CoreExpr
to_comp expr (Keep _ _) = return expr
to_comp expr (Wrap ty)
= do wrap_tc <- builtin wrapTyCon
return $ wrapNewTypeBody wrap_tc [ty] expr
to_comp expr (Wrap ty) = wrapNewTypeBodyOfWrap expr ty
-- buildFromPRepr -------------------------------------------------------------
-- | Build the 'fromPRepr' method of the PA class.
-- |Build the 'fromPRepr' method of the PA class.
--
buildFromPRepr :: PAInstanceBuilder
buildFromPRepr vect_tc repr_tc _ _ repr
= do
......@@ -217,14 +217,13 @@ buildFromPRepr vect_tc repr_tc _ _ repr
[(DataAlt tup_con, vars, con `mkApps` es)]
from_comp expr (Keep _ _) = return expr
from_comp expr (Wrap ty)
= do
wrap <- builtin wrapTyCon
return $ unwrapNewTypeBody wrap [ty] expr
from_comp expr (Wrap ty) = unwrapNewTypeBodyOfWrap expr ty
-- buildToArrRepr -------------------------------------------------------------
-- | Build the 'toArrRepr' method of the PA class.
-- |Build the 'toArrRepr' method of the PA class.
--
buildToArrPRepr :: PAInstanceBuilder
buildToArrPRepr vect_tc prepr_tc pdata_tc _ r
= do arg_ty <- mkPDataType el_ty
......@@ -283,17 +282,14 @@ buildToArrPRepr vect_tc prepr_tc pdata_tc _ r
to_con (ConRepr _ r) = to_prod r
-- FIXME: this is bound to be wrong!
to_comp expr (Keep _ _) = return expr
to_comp expr (Wrap ty)
= do
wrap_tc <- builtin wrapTyCon
pwrap_tc <- pdataReprTyConExact (mkTyConApp wrap_tc [ty])
return $ wrapNewTypeBody pwrap_tc [ty] expr
to_comp expr (Wrap ty) = wrapNewTypeBodyOfPDataWrap expr ty
-- buildFromArrPRepr ----------------------------------------------------------
-- | Build the 'fromArrPRepr' method for the PA class.
-- |Build the 'fromArrPRepr' method for the PA class.
--
buildFromArrPRepr :: PAInstanceBuilder
buildFromArrPRepr vect_tc prepr_tc pdata_tc _ r
= do arg_ty <- mkPDataType =<< mkPReprType el_ty
......@@ -355,11 +351,9 @@ buildFromArrPRepr vect_tc prepr_tc pdata_tc _ r
from_con res_ty res expr (ConRepr _ r) = from_prod res_ty res expr r
from_comp _ res expr (Keep _ _) = return (res, [expr])
from_comp _ res expr (Wrap ty)
= do wrap_tc <- builtin wrapTyCon
pwrap_tc <- pdataReprTyConExact (mkTyConApp wrap_tc [ty])
return (res, [unwrapNewTypeBody pwrap_tc [ty]
$ unwrapFamInstScrut pwrap_tc [ty] expr])
from_comp _ res expr (Wrap ty) = do { expr' <- unwrapNewTypeBodyOfPDataWrap expr ty
; return (res, [expr'])
}
fold f res_ty res exprs rs
= foldrM f' (res, []) (zip exprs rs)
......@@ -457,12 +451,8 @@ buildToArrPReprs vect_tc prepr_tc _ pdatas_tc r
to_con xSums (ConRepr _ r)
= to_prod xSums r
-- FIXME: this is bound to be wrong!
to_comp expr (Keep _ _) = return expr
to_comp expr (Wrap ty)
= do wrap_tc <- builtin wrapTyCon
(pwrap_tc, _) <- pdatasReprTyCon (mkTyConApp wrap_tc [ty])
return $ wrapNewTypeBody pwrap_tc [ty] expr
to_comp expr (Wrap ty) = wrapNewTypeBodyOfPDatasWrap expr ty
-- buildFromArrPReprs ---------------------------------------------------------
......@@ -545,11 +535,9 @@ buildFromArrPReprs vect_tc prepr_tc _ pdatas_tc r
= from_prod res_ty res expr r
from_comp _ res expr (Keep _ _) = return (res, [expr])
from_comp _ res expr (Wrap ty)
= do wrap_tc <- builtin wrapTyCon
(pwraps_tc, _) <- pdatasReprTyCon (mkTyConApp wrap_tc [ty])
return (res, [unwrapNewTypeBody pwraps_tc [ty]
$ unwrapFamInstScrut pwraps_tc [ty] expr])
from_comp _ res expr (Wrap ty) = do { expr' <- unwrapNewTypeBodyOfPDatasWrap expr ty
; return (res, [expr'])
}
fold f res_ty res exprs rs
= foldrM f' (res, []) (zip exprs rs)
......
......@@ -227,22 +227,18 @@ vectTypeEnv tycons vectTypeDecls vectClassDecls
; (_, binds) <- fixV $ \ ~(dfuns, _) ->
do { defTyConPAs (zipLazy vect_tcs dfuns)
-- query the 'PData' instance type constructors for type constructors that have a
-- Query the 'PData' instance type constructors for type constructors that have a
-- VECTORISE pragma with an explicit right-hand side (this is Item (3) of
-- "Note [Pragmas to vectorise tycons]" above)
; pdata_withRHS_tcs <- mapM pdataReprTyConExact
[ mkTyConApp tycon tys
| (tycon, _) <- vectTyConsWithRHS
, let tys = mkTyVarTys (tyConTyVars tycon)
]
-- build workers for all vectorised data constructors (except scalar ones)
-- "Note [Pragmas to vectorise tycons]" above).
; pdata_withRHS_tcs <- mapM pdataReprTyConExact (map fst vectTyConsWithRHS)
-- Build workers for all vectorised data constructors (except scalar ones)
; sequence_ $
zipWith3 vectDataConWorkers (orig_tcs ++ map fst vectTyConsWithRHS)
(vect_tcs ++ map snd vectTyConsWithRHS)
(pdata_tcs ++ pdata_withRHS_tcs)
-- build a 'PA' dictionary for all type constructors (except scalar ones and those
-- Build a 'PA' dictionary for all type constructors (except scalar ones and those
-- defined with an explicit right-hand side where the dictionary is user-supplied)
; dfuns <- sequence $
zipWith4 buildTyConPADict
......
{-# OPTIONS -fno-warn-tabs #-}
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and
-- detab the module (please do the detabbing in a separate patch). See
-- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
-- for details
module Vectorise.Type.PRepr
( buildPReprTyCon
, buildPAScAndMethods
) where
import Vectorise.Utils
import Vectorise.Monad
import Vectorise.Builtins
import Vectorise.Type.Repr
import CoreSyn
import CoreUtils
import MkCore ( mkWildCase )
import TyCon
import Type
import BuildTyCl
import OccName
import Coercion
import MkId
import FastString
import MonadUtils
import Control.Monad
mk_fam_inst :: TyCon -> TyCon -> (TyCon, [Type])
mk_fam_inst fam_tc arg_tc
= (fam_tc, [mkTyConApp arg_tc . mkTyVarTys $ tyConTyVars arg_tc])
buildPReprTyCon :: TyCon -> TyCon -> SumRepr -> VM TyCon
buildPReprTyCon orig_tc vect_tc repr
= do
name <- mkLocalisedName mkPReprTyConOcc (tyConName orig_tc)
-- rhs_ty <- buildPReprType vect_tc
rhs_ty <- sumReprType repr
prepr_tc <- builtin preprTyCon
liftDs $ buildSynTyCon name
tyvars
(SynonymTyCon rhs_ty)
(typeKind rhs_ty)
NoParentTyCon
(Just $ mk_fam_inst prepr_tc vect_tc)
where
tyvars = tyConTyVars vect_tc
-----------------------------------------------------
buildPAScAndMethods :: [(String, TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr)]
-- buildPAScandmethods says how to build the PR superclass and methods of PA
-- class class PR (PRepr a) => PA a where
-- toPRepr :: a -> PRepr a
-- fromPRepr :: PRepr a -> a
-- toArrPRepr :: PData a -> PData (PRepr a)
-- fromArrPRepr :: PData (PRepr a) -> PData a
buildPAScAndMethods = [("PR", buildPRDict),
("toPRepr", buildToPRepr),
("fromPRepr", buildFromPRepr),
("toArrPRepr", buildToArrPRepr),
("fromArrPRepr", buildFromArrPRepr)]
buildPRDict :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
buildPRDict vect_tc prepr_tc _ _
= prDictOfPReprInstTyCon inst_ty prepr_tc arg_tys
where
arg_tys = mkTyVarTys (tyConTyVars vect_tc)
inst_ty = mkTyConApp vect_tc arg_tys
buildToPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
buildToPRepr vect_tc repr_tc _ repr
= do
let arg_ty = mkTyConApp vect_tc ty_args
res_ty <- mkPReprType arg_ty
arg <- newLocalVar (fsLit "x") arg_ty
result <- to_sum (Var arg) arg_ty res_ty repr
return $ Lam arg result
where
ty_args = mkTyVarTys (tyConTyVars vect_tc)
wrap_repr_inst = wrapFamInstBody repr_tc ty_args
to_sum _ _ _ EmptySum
= do
void <- builtin voidVar
return $ wrap_repr_inst $ Var void
to_sum arg arg_ty res_ty (UnarySum r)
= do
(pat, vars, body) <- con_alt r
return $ mkWildCase arg arg_ty res_ty
[(pat, vars, wrap_repr_inst body)]
to_sum arg arg_ty res_ty (Sum { repr_sum_tc = sum_tc
, repr_con_tys = tys
, repr_cons = cons })
= do
alts <- mapM con_alt cons
let alts' = [(pat, vars, wrap_repr_inst
$ mkConApp sum_con (map Type tys ++ [body]))
| ((pat, vars, body), sum_con)
<- zip alts (tyConDataCons sum_tc)]
return $ mkWildCase arg arg_ty res_ty alts'
con_alt (ConRepr con r)
= do
(vars, body) <- to_prod r
return (DataAlt con, vars, body)
to_prod EmptyProd
= do
void <- builtin voidVar
return ([], Var void)
to_prod (UnaryProd comp)
= do
var <- newLocalVar (fsLit "x") (compOrigType comp)
body <- to_comp (Var var) comp
return ([var], body)
to_prod(Prod { repr_tup_tc = tup_tc
, repr_comp_tys = tys
, repr_comps = comps })
= do
vars <- newLocalVars (fsLit "x") (map compOrigType comps)
exprs <- zipWithM to_comp (map Var vars) comps
return (vars, mkConApp tup_con (map Type tys ++ exprs))
where
[tup_con] = tyConDataCons tup_tc
to_comp expr (Keep _ _) = return expr
to_comp expr (Wrap ty) = do
wrap_tc <- builtin wrapTyCon
return $ wrapNewTypeBody wrap_tc [ty] expr
buildFromPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
buildFromPRepr vect_tc repr_tc _ repr
= do
arg_ty <- mkPReprType res_ty
arg <- newLocalVar (fsLit "x") arg_ty
result <- from_sum (unwrapFamInstScrut repr_tc ty_args (Var arg))
repr
return $ Lam arg result
where
ty_args = mkTyVarTys (tyConTyVars vect_tc)
res_ty = mkTyConApp vect_tc ty_args
from_sum _ EmptySum
= do
dummy <- builtin fromVoidVar
return $ Var dummy `App` Type res_ty
from_sum expr (UnarySum r) = from_con expr r
from_sum expr (Sum { repr_sum_tc = sum_tc
, repr_con_tys = tys
, repr_cons = cons })
= do
vars <- newLocalVars (fsLit "x") tys
es <- zipWithM from_con (map Var vars) cons
return $ mkWildCase expr (exprType expr) res_ty
[(DataAlt con, [var], e)
| (con, var, e) <- zip3 (tyConDataCons sum_tc) vars es]
from_con expr (ConRepr con r)
= from_prod expr (mkConApp con $ map Type ty_args) r
from_prod _ con EmptyProd = return con
from_prod expr con (UnaryProd r)
= do
e <- from_comp expr r
return $ con `App` e
from_prod expr con (Prod { repr_tup_tc = tup_tc
, repr_comp_tys = tys
, repr_comps = comps
})
= do
vars <- newLocalVars (fsLit "y") tys
es <- zipWithM from_comp (map Var vars) comps
return $ mkWildCase expr (exprType expr) res_ty
[(DataAlt tup_con, vars, con `mkApps` es)]
where
[tup_con] = tyConDataCons tup_tc
from_comp expr (Keep _ _) = return expr
from_comp expr (Wrap ty)
= do
wrap <- builtin wrapTyCon
return $ unwrapNewTypeBody wrap [ty] expr
buildToArrPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
buildToArrPRepr vect_tc prepr_tc pdata_tc r
= do
arg_ty <- mkPDataType el_ty
res_ty <- mkPDataType =<< mkPReprType el_ty
arg <- newLocalVar (fsLit "xs") arg_ty
pdata_co <- mkBuiltinCo pdataTyCon
let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
co = mkAppCo pdata_co
. mkSymCo
$ mkAxInstCo repr_co ty_args
scrut = unwrapFamInstScrut pdata_tc ty_args (Var arg)
(vars, result) <- to_sum r
return . Lam arg
$ mkWildCase scrut (mkTyConApp pdata_tc ty_args) res_ty
[(DataAlt pdata_dc, vars, mkCast result co)]
where
ty_args = mkTyVarTys $ tyConTyVars vect_tc
el_ty = mkTyConApp vect_tc ty_args
[pdata_dc] = tyConDataCons pdata_tc
to_sum EmptySum = do
pvoid <- builtin pvoidVar
return ([], Var pvoid)
to_sum (UnarySum r) = to_con r
to_sum (Sum { repr_psum_tc = psum_tc
, repr_sel_ty = sel_ty
, repr_con_tys = tys
, repr_cons = cons
})
= do
(vars, exprs) <- mapAndUnzipM to_con cons
sel <- newLocalVar (fsLit "sel") sel_ty
return (sel : concat vars, mk_result (Var sel) exprs)
where
[psum_con] = tyConDataCons psum_tc
mk_result sel exprs = wrapFamInstBody psum_tc tys
$ mkConApp psum_con
$ map Type tys ++ (sel : exprs)
to_con (ConRepr _ r) = to_prod r
to_prod EmptyProd = do
pvoid <- builtin pvoidVar
return ([], Var pvoid)
to_prod (UnaryProd r)
= do
pty <- mkPDataType (compOrigType r)
var <- newLocalVar (fsLit "x") pty
expr <- to_comp (Var var) r
return ([var], expr)
to_prod (Prod { repr_ptup_tc = ptup_tc
, repr_comp_tys = tys
, repr_comps = comps })
= do
ptys <- mapM (mkPDataType . compOrigType) comps
vars <- newLocalVars (fsLit "x") ptys
es <- zipWithM to_comp (map Var vars) comps
return (vars, mk_result es)
where
[ptup_con] = tyConDataCons ptup_tc
mk_result exprs = wrapFamInstBody ptup_tc tys
$ mkConApp ptup_con
$ map Type tys ++ exprs
to_comp expr (Keep _ _) = return expr
-- FIXME: this is bound to be wrong!
to_comp expr (Wrap ty)
= do
wrap_tc <- builtin wrapTyCon
pwrap_tc <- pdataReprTyConExact (mkTyConApp wrap_tc [ty])
return $ wrapNewTypeBody pwrap_tc [ty] expr
buildFromArrPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
buildFromArrPRepr vect_tc prepr_tc pdata_tc r
= do
arg_ty <- mkPDataType =<< mkPReprType el_ty
res_ty <- mkPDataType el_ty
arg <- newLocalVar (fsLit "xs") arg_ty
pdata_co <- mkBuiltinCo pdataTyCon
let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
co = mkAppCo pdata_co
$ mkAxInstCo repr_co var_tys
scrut = mkCast (Var arg) co
mk_result args = wrapFamInstBody pdata_tc var_tys
$ mkConApp pdata_con
$ map Type var_tys ++ args
(expr, _) <- fixV $ \ ~(_, args) ->
from_sum res_ty (mk_result args) scrut r
return $ Lam arg expr
-- (args, mk) <- from_sum res_ty scrut r
-- let result = wrapFamInstBody pdata_tc var_tys
-- . mkConApp pdata_dc
-- $ map Type var_tys ++ args
-- return $ Lam arg (mk result)
where
var_tys = mkTyVarTys $ tyConTyVars vect_tc
el_ty = mkTyConApp vect_tc var_tys
[pdata_con] = tyConDataCons pdata_tc
from_sum _ res _ EmptySum = return (res, [])
from_sum res_ty res expr (UnarySum r) = from_con res_ty res expr r