1 module VectType ( vectTyCon, vectType, vectTypeEnv )
4 #include "HsVersions.h"
9 import HscTypes ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
15 import FamInstEnv ( FamInst, mkLocalFamInst )
16 import InstEnv ( Instance )
19 import BasicTypes ( StrictnessMark(..), boolToRecFlag )
22 import TysWiredIn ( intTy )
27 import Digraph ( SCC(..), stronglyConnComp )
31 import Control.Monad ( liftM2, zipWithM, zipWithM_ )
33 -- ----------------------------------------------------------------------------
36 vectTyCon :: TyCon -> VM TyCon
38 | isFunTyCon tc = builtin closureTyCon
39 | isBoxedTupleTyCon tc = return tc
40 | isUnLiftedTyCon tc = return tc
44 Just tc' -> return tc'
46 -- FIXME: just for now
47 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
49 vectType :: Type -> VM Type
50 vectType ty | Just ty' <- coreView ty = vectType ty'
51 vectType (TyVarTy tv) = return $ TyVarTy tv
52 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
53 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
54 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
55 (mapM vectType [ty1,ty2])
56 vectType ty@(ForAllTy _ _)
58 mdicts <- mapM paDictArgType tyvars
59 mono_ty' <- vectType mono_ty
60 return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
62 (tyvars, mono_ty) = splitForAllTys ty
64 vectType ty = pprPanic "vectType:" (ppr ty)
66 -- ----------------------------------------------------------------------------
69 type TyConGroup = ([TyCon], UniqSet TyCon)
71 vectTypeEnv :: TypeEnv -> VM (TypeEnv, [FamInst], [Instance])
74 cs <- readGEnv $ mk_map . global_tycons
75 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
76 keep_dcs = concatMap tyConDataCons keep_tcs
77 zipWithM_ defTyCon keep_tcs keep_tcs
78 zipWithM_ defDataCon keep_dcs keep_dcs
79 vect_tcs <- vectTyConDecls conv_tcs
80 parr_tcs1 <- mapM (\tc -> buildPArrayTyCon (tyConName tc) tc) keep_tcs
81 parr_tcs2 <- zipWithM (buildPArrayTyCon . tyConName) conv_tcs vect_tcs
82 let new_tcs = vect_tcs ++ parr_tcs1 ++ parr_tcs2
84 let new_env = extendTypeEnvList env
86 ++ [ADataCon dc | tc <- new_tcs
87 , dc <- tyConDataCons tc])
89 return (new_env, map mkLocalFamInst (parr_tcs1 ++ parr_tcs2), [])
91 tycons = typeEnvTyCons env
92 groups = tyConGroups tycons
94 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
96 keep_tc tc = let dcs = tyConDataCons tc
98 defTyCon tc tc >> zipWithM_ defDataCon dcs dcs
101 vectTyConDecls :: [TyCon] -> VM [TyCon]
102 vectTyConDecls tcs = fixV $ \tcs' ->
104 mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
105 mapM vectTyConDecl tcs
108 lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
110 vectTyConDecl :: TyCon -> VM TyCon
113 name' <- cloneName mkVectTyConOcc name
114 rhs' <- vectAlgTyConRhs (algTyConRhs tc)
116 return $ mkAlgTyCon name'
119 [] -- no stupid theta
121 [] -- no selector ids
122 NoParentTyCon -- FIXME
123 rec_flag -- FIXME: is this ok?
124 False -- FIXME: no generics
125 False -- not GADT syntax
129 tyvars = tyConTyVars tc
130 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
132 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
133 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
137 data_cons' <- mapM vectDataCon data_cons
138 zipWithM_ defDataCon data_cons data_cons'
139 return $ DataTyCon { data_cons = data_cons'
143 vectDataCon :: DataCon -> VM DataCon
145 | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
146 | not . null $ dataConEqSpec dc = pprPanic "vectDataCon: eq spec" (ppr dc)
149 name' <- cloneName mkVectDataConOcc name
150 tycon' <- vectTyCon tycon
151 arg_tys <- mapM vectType rep_arg_tys
152 wrk_name <- cloneName mkDataConWorkerOcc name'
154 let ids = mkDataConIds (panic "vectDataCon: wrapper id")
157 data_con = mkDataCon name'
159 (map (const NotMarkedStrict) arg_tys)
160 [] -- no labelled fields
162 [] -- no existential tvs for now
163 [] -- no eq spec for now
167 [] -- no stupid theta
171 name = dataConName dc
172 univ_tvs = dataConUnivTyVars dc
173 rep_arg_tys = dataConRepArgTys dc
174 tycon = dataConTyCon dc
176 buildPArrayTyCon :: Name -> TyCon -> VM TyCon
177 buildPArrayTyCon orig_name vect_tc = fixV $ \repr_tc ->
179 name' <- cloneName mkPArrayTyConOcc orig_name
180 parent <- buildPArrayParentInfo orig_name vect_tc repr_tc
181 rhs <- buildPArrayTyConRhs orig_name vect_tc repr_tc
183 return $ mkAlgTyCon name'
186 [] -- no stupid theta
188 [] -- no selector ids
190 rec_flag -- FIXME: is this ok?
191 False -- FIXME: no generics
192 False -- not GADT syntax
194 name = tyConName vect_tc
195 kind = tyConKind vect_tc
196 tyvars = tyConTyVars vect_tc
197 rec_flag = boolToRecFlag (isRecursiveTyCon vect_tc)
200 buildPArrayParentInfo :: Name -> TyCon -> TyCon -> VM TyConParent
201 buildPArrayParentInfo orig_name vect_tc repr_tc
203 parray_tc <- builtin parrayTyCon
204 co_name <- cloneName mkInstTyCoOcc (tyConName repr_tc)
206 let inst_tys = [mkTyConApp vect_tc (map mkTyVarTy tyvars)]
208 return . FamilyTyCon parray_tc inst_tys
209 $ mkFamInstCoercion co_name
215 tyvars = tyConTyVars vect_tc
217 buildPArrayTyConRhs :: Name -> TyCon -> TyCon -> VM AlgTyConRhs
218 buildPArrayTyConRhs orig_name vect_tc repr_tc
220 data_con <- buildPArrayDataCon orig_name vect_tc repr_tc
221 return $ DataTyCon { data_cons = [data_con], is_enum = False }
223 buildPArrayDataCon :: Name -> TyCon -> TyCon -> VM DataCon
224 buildPArrayDataCon orig_name vect_tc repr_tc
226 dc_name <- cloneName mkPArrayDataConOcc orig_name
227 shape_ty <- mkPArrayType intTy -- FIXME: we want to unbox this!
228 repr_tys <- mapM mkPArrayType types
229 wrk_name <- cloneName mkDataConWorkerOcc dc_name
230 wrp_name <- cloneName mkDataConWrapperOcc dc_name
232 let ids = mkDataConIds wrp_name wrk_name data_con
233 data_con = mkDataCon dc_name
235 (MarkedStrict : map (const NotMarkedStrict) repr_tys)
237 (tyConTyVars vect_tc)
241 (shape_ty : repr_tys)
248 types = [ty | dc <- tyConDataCons vect_tc
249 , ty <- dataConRepArgTys dc]
251 -- | Split the given tycons into two sets depending on whether they have to be
252 -- converted (first list) or not (second list). The first argument contains
253 -- information about the conversion status of external tycons:
255 -- * tycons which have converted versions are mapped to True
256 -- * tycons which are not changed by vectorisation are mapped to False
257 -- * tycons which can't be converted are not elements of the map
259 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
260 classifyTyCons = classify [] []
262 classify conv keep cs [] = (conv, keep)
263 classify conv keep cs ((tcs, ds) : rs)
264 | can_convert && must_convert
265 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
267 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
269 = classify conv keep cs rs
271 refs = ds `delListFromUniqSet` tcs
273 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
274 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
276 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
278 -- | Compute mutually recursive groups of tycons in topological order
280 tyConGroups :: [TyCon] -> [TyConGroup]
281 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
283 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
284 , let ds = tyConsOfTyCon tc]
286 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
287 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
289 (tcs, dss) = unzip els
291 tyConsOfTyCon :: TyCon -> UniqSet TyCon
293 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
295 tyConsOfType :: Type -> UniqSet TyCon
297 | Just ty' <- coreView ty = tyConsOfType ty'
298 tyConsOfType (TyVarTy v) = emptyUniqSet
299 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
301 extend | isUnLiftedTyCon tc
302 || isTupleTyCon tc = id
304 | otherwise = (`addOneToUniqSet` tc)
306 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
307 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
308 `addOneToUniqSet` funTyCon
309 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
310 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
312 tyConsOfTypes :: [Type] -> UniqSet TyCon
313 tyConsOfTypes = unionManyUniqSets . map tyConsOfType