1 module VectType ( vectTyCon, vectType, vectTypeEnv )
4 #include "HsVersions.h"
9 import HscTypes ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
17 import BasicTypes ( StrictnessMark(..), boolToRecFlag )
20 import TysWiredIn ( intTy )
25 import Digraph ( SCC(..), stronglyConnComp )
29 import Control.Monad ( liftM2, zipWithM, zipWithM_ )
31 -- ----------------------------------------------------------------------------
34 vectTyCon :: TyCon -> VM TyCon
36 | isFunTyCon tc = builtin closureTyCon
37 | isBoxedTupleTyCon tc = return tc
38 | isUnLiftedTyCon tc = return tc
42 Just tc' -> return tc'
44 -- FIXME: just for now
45 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
47 vectType :: Type -> VM Type
48 vectType ty | Just ty' <- coreView ty = vectType ty'
49 vectType (TyVarTy tv) = return $ TyVarTy tv
50 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
51 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
52 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
53 (mapM vectType [ty1,ty2])
54 vectType ty@(ForAllTy _ _)
56 mdicts <- mapM paDictArgType tyvars
57 mono_ty' <- vectType mono_ty
58 return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
60 (tyvars, mono_ty) = splitForAllTys ty
62 vectType ty = pprPanic "vectType:" (ppr ty)
64 -- ----------------------------------------------------------------------------
67 type TyConGroup = ([TyCon], UniqSet TyCon)
69 vectTypeEnv :: TypeEnv -> VM TypeEnv
72 cs <- readGEnv $ mk_map . global_tycons
73 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
74 keep_dcs = concatMap tyConDataCons keep_tcs
75 zipWithM_ defTyCon keep_tcs keep_tcs
76 zipWithM_ defDataCon keep_dcs keep_dcs
77 vect_tcs <- vectTyConDecls conv_tcs
78 parr_tcs1 <- mapM (\tc -> buildPArrayTyCon (tyConName tc) tc) keep_tcs
79 parr_tcs2 <- zipWithM (buildPArrayTyCon . tyConName) conv_tcs vect_tcs
80 let new_tcs = vect_tcs ++ parr_tcs1 ++ parr_tcs2
81 return $ extendTypeEnvList env
82 (map ATyCon new_tcs ++ [ADataCon dc | tc <- new_tcs
83 , dc <- tyConDataCons tc])
85 tycons = typeEnvTyCons env
86 groups = tyConGroups tycons
88 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
90 keep_tc tc = let dcs = tyConDataCons tc
92 defTyCon tc tc >> zipWithM_ defDataCon dcs dcs
95 vectTyConDecls :: [TyCon] -> VM [TyCon]
96 vectTyConDecls tcs = fixV $ \tcs' ->
98 mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
99 mapM vectTyConDecl tcs
102 lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
104 vectTyConDecl :: TyCon -> VM TyCon
107 name' <- cloneName mkVectTyConOcc name
108 rhs' <- vectAlgTyConRhs (algTyConRhs tc)
110 return $ mkAlgTyCon name'
113 [] -- no stupid theta
115 [] -- no selector ids
116 NoParentTyCon -- FIXME
117 rec_flag -- FIXME: is this ok?
118 False -- FIXME: no generics
119 False -- not GADT syntax
123 tyvars = tyConTyVars tc
124 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
126 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
127 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
131 data_cons' <- mapM vectDataCon data_cons
132 zipWithM_ defDataCon data_cons data_cons'
133 return $ DataTyCon { data_cons = data_cons'
137 vectDataCon :: DataCon -> VM DataCon
139 | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
140 | not . null $ dataConEqSpec dc = pprPanic "vectDataCon: eq spec" (ppr dc)
143 name' <- cloneName mkVectDataConOcc name
144 tycon' <- vectTyCon tycon
145 arg_tys <- mapM vectType rep_arg_tys
146 wrk_name <- cloneName mkDataConWorkerOcc name'
148 let ids = mkDataConIds (panic "vectDataCon: wrapper id")
151 data_con = mkDataCon name'
153 (map (const NotMarkedStrict) arg_tys)
154 [] -- no labelled fields
156 [] -- no existential tvs for now
157 [] -- no eq spec for now
161 [] -- no stupid theta
165 name = dataConName dc
166 univ_tvs = dataConUnivTyVars dc
167 rep_arg_tys = dataConRepArgTys dc
168 tycon = dataConTyCon dc
170 buildPArrayTyCon :: Name -> TyCon -> VM TyCon
171 buildPArrayTyCon orig_name vect_tc = fixV $ \repr_tc ->
173 name' <- cloneName mkPArrayTyConOcc orig_name
174 parent <- buildPArrayParentInfo orig_name vect_tc repr_tc
175 rhs <- buildPArrayTyConRhs orig_name vect_tc repr_tc
177 return $ mkAlgTyCon name'
180 [] -- no stupid theta
182 [] -- no selector ids
184 rec_flag -- FIXME: is this ok?
185 False -- FIXME: no generics
186 False -- not GADT syntax
188 name = tyConName vect_tc
189 kind = tyConKind vect_tc
190 tyvars = tyConTyVars vect_tc
191 rec_flag = boolToRecFlag (isRecursiveTyCon vect_tc)
194 buildPArrayParentInfo :: Name -> TyCon -> TyCon -> VM TyConParent
195 buildPArrayParentInfo orig_name vect_tc repr_tc
197 parray_tc <- builtin parrayTyCon
198 co_name <- cloneName mkInstTyCoOcc orig_name
200 let inst_tys = [mkTyConApp vect_tc (map mkTyVarTy tyvars)]
202 return . FamilyTyCon parray_tc inst_tys
203 $ mkFamInstCoercion co_name
209 tyvars = tyConTyVars vect_tc
211 buildPArrayTyConRhs :: Name -> TyCon -> TyCon -> VM AlgTyConRhs
212 buildPArrayTyConRhs orig_name vect_tc repr_tc
214 data_con <- buildPArrayDataCon orig_name vect_tc repr_tc
215 return $ DataTyCon { data_cons = [data_con], is_enum = False }
217 buildPArrayDataCon :: Name -> TyCon -> TyCon -> VM DataCon
218 buildPArrayDataCon orig_name vect_tc repr_tc
220 dc_name <- cloneName mkPArrayDataConOcc orig_name
221 shape_ty <- mkPArrayType intTy -- FIXME: we want to unbox this!
222 repr_tys <- mapM mkPArrayType types
223 wrk_name <- cloneName mkDataConWorkerOcc dc_name
224 wrp_name <- cloneName mkDataConWrapperOcc dc_name
226 let ids = mkDataConIds wrp_name wrk_name data_con
227 data_con = mkDataCon dc_name
229 (MarkedStrict : map (const NotMarkedStrict) repr_tys)
231 (tyConTyVars vect_tc)
235 (shape_ty : repr_tys)
242 types = [ty | dc <- tyConDataCons vect_tc
243 , ty <- dataConRepArgTys dc]
245 -- | Split the given tycons into two sets depending on whether they have to be
246 -- converted (first list) or not (second list). The first argument contains
247 -- information about the conversion status of external tycons:
249 -- * tycons which have converted versions are mapped to True
250 -- * tycons which are not changed by vectorisation are mapped to False
251 -- * tycons which can't be converted are not elements of the map
253 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
254 classifyTyCons = classify [] []
256 classify conv keep cs [] = (conv, keep)
257 classify conv keep cs ((tcs, ds) : rs)
258 | can_convert && must_convert
259 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
261 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
263 = classify conv keep cs rs
265 refs = ds `delListFromUniqSet` tcs
267 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
268 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
270 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
272 -- | Compute mutually recursive groups of tycons in topological order
274 tyConGroups :: [TyCon] -> [TyConGroup]
275 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
277 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
278 , let ds = tyConsOfTyCon tc]
280 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
281 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
283 (tcs, dss) = unzip els
285 tyConsOfTyCon :: TyCon -> UniqSet TyCon
287 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
289 tyConsOfType :: Type -> UniqSet TyCon
291 | Just ty' <- coreView ty = tyConsOfType ty'
292 tyConsOfType (TyVarTy v) = emptyUniqSet
293 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
295 extend | isUnLiftedTyCon tc
296 || isTupleTyCon tc = id
298 | otherwise = (`addOneToUniqSet` tc)
300 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
301 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
302 `addOneToUniqSet` funTyCon
303 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
304 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
306 tyConsOfTypes :: [Type] -> UniqSet TyCon
307 tyConsOfTypes = unionManyUniqSets . map tyConsOfType