1 module VectType ( vectTyCon, vectType, vectTypeEnv )
4 #include "HsVersions.h"
9 import HscTypes ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
16 import BasicTypes ( StrictnessMark(..), boolToRecFlag )
23 import Digraph ( SCC(..), stronglyConnComp )
27 import Control.Monad ( liftM2, zipWithM_ )
29 -- ----------------------------------------------------------------------------
32 vectTyCon :: TyCon -> VM TyCon
34 | isFunTyCon tc = builtin closureTyCon
35 | isBoxedTupleTyCon tc = return tc
36 | isUnLiftedTyCon tc = return tc
40 Just tc' -> return tc'
42 -- FIXME: just for now
43 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
45 vectType :: Type -> VM Type
46 vectType ty | Just ty' <- coreView ty = vectType ty'
47 vectType (TyVarTy tv) = return $ TyVarTy tv
48 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
49 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
50 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
51 (mapM vectType [ty1,ty2])
52 vectType ty@(ForAllTy _ _)
54 mdicts <- mapM paDictArgType tyvars
55 mono_ty' <- vectType mono_ty
56 return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
58 (tyvars, mono_ty) = splitForAllTys ty
60 vectType ty = pprPanic "vectType:" (ppr ty)
62 -- ----------------------------------------------------------------------------
65 type TyConGroup = ([TyCon], UniqSet TyCon)
67 vectTypeEnv :: TypeEnv -> VM TypeEnv
70 cs <- readGEnv $ mk_map . global_tycons
71 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
72 keep_dcs = concatMap tyConDataCons keep_tcs
73 zipWithM_ defTyCon keep_tcs keep_tcs
74 zipWithM_ defDataCon keep_dcs keep_dcs
75 new_tcs <- vectTyConDecls conv_tcs
76 return $ extendTypeEnvList env
77 (map ATyCon new_tcs ++ [ADataCon dc | tc <- new_tcs
78 , dc <- tyConDataCons tc])
80 tycons = typeEnvTyCons env
81 groups = tyConGroups tycons
83 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
85 keep_tc tc = let dcs = tyConDataCons tc
87 defTyCon tc tc >> zipWithM_ defDataCon dcs dcs
90 vectTyConDecls :: [TyCon] -> VM [TyCon]
91 vectTyConDecls tcs = fixV $ \tcs' ->
93 mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
94 mapM vectTyConDecl tcs
97 lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
99 vectTyConDecl :: TyCon -> VM TyCon
102 name' <- cloneName mkVectTyConOcc name
103 rhs' <- vectAlgTyConRhs (algTyConRhs tc)
105 return $ mkAlgTyCon name'
108 [] -- no stupid theta
110 [] -- no selector ids
111 NoParentTyCon -- FIXME
112 rec_flag -- FIXME: is this ok?
113 False -- FIXME: no generics
114 False -- not GADT syntax
118 tyvars = tyConTyVars tc
119 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
121 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
122 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
126 data_cons' <- mapM vectDataCon data_cons
127 zipWithM_ defDataCon data_cons data_cons'
128 return $ DataTyCon { data_cons = data_cons'
132 vectDataCon :: DataCon -> VM DataCon
134 | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
135 | not . null $ dataConEqSpec dc = pprPanic "vectDataCon: eq spec" (ppr dc)
138 name' <- cloneName mkVectDataConOcc name
139 tycon' <- vectTyCon tycon
140 arg_tys <- mapM vectType rep_arg_tys
141 wrk_name <- cloneName mkDataConWorkerOcc name'
143 let ids = mkDataConIds (panic "vectDataCon: wrapper id")
146 data_con = mkDataCon name'
148 (map (const NotMarkedStrict) arg_tys)
149 [] -- no labelled fields
151 [] -- no existential tvs for now
152 [] -- no eq spec for now
156 [] -- no stupid theta
160 name = dataConName dc
161 univ_tvs = dataConUnivTyVars dc
162 rep_arg_tys = dataConRepArgTys dc
163 tycon = dataConTyCon dc
166 mkPArrTyCon :: TyCon -> VM TyCon
167 mkPArrTyCon tc = fixV $ \repr_tc ->
171 mkPArrayDataCon :: TyCon -> TyCon -> VM DataCon
172 mkPArrayDataCon orig_tc repr_tc
174 name <- cloneName mkPArrayDataConOcc (tyConName orig_tc)
175 shape_ty <- mkPArrayType intTy -- FIXME: we want to unbox this!
176 repr_tys <- mapM mkPArrayType types
177 wrk_name <- cloneName mkDataConWorkerOcc name
178 wrp_name <- cloneName mkDataConWrapperOcc name
180 let ids = mkDataConIds wrp_name wrk_name data_con
181 data_con = mkDataCon name
183 (MarkedStrict : map (const NotMarkedStrict) repr_tys)
185 (tyConTyVars orig_tc)
196 types = [ty | dc <- tyConDataCons orig_tc
197 , ty <- dataConRepArgTys dc]
199 -- | Split the given tycons into two sets depending on whether they have to be
200 -- converted (first list) or not (second list). The first argument contains
201 -- information about the conversion status of external tycons:
203 -- * tycons which have converted versions are mapped to True
204 -- * tycons which are not changed by vectorisation are mapped to False
205 -- * tycons which can't be converted are not elements of the map
207 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
208 classifyTyCons = classify [] []
210 classify conv keep cs [] = (conv, keep)
211 classify conv keep cs ((tcs, ds) : rs)
212 | can_convert && must_convert
213 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
215 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
217 = classify conv keep cs rs
219 refs = ds `delListFromUniqSet` tcs
221 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
222 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
224 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
226 -- | Compute mutually recursive groups of tycons in topological order
228 tyConGroups :: [TyCon] -> [TyConGroup]
229 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
231 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
232 , let ds = tyConsOfTyCon tc]
234 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
235 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
237 (tcs, dss) = unzip els
239 tyConsOfTyCon :: TyCon -> UniqSet TyCon
241 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
243 tyConsOfType :: Type -> UniqSet TyCon
245 | Just ty' <- coreView ty = tyConsOfType ty'
246 tyConsOfType (TyVarTy v) = emptyUniqSet
247 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
249 extend | isUnLiftedTyCon tc
250 || isTupleTyCon tc = id
252 | otherwise = (`addOneToUniqSet` tc)
254 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
255 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
256 `addOneToUniqSet` funTyCon
257 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
258 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
260 tyConsOfTypes :: [Type] -> UniqSet TyCon
261 tyConsOfTypes = unionManyUniqSets . map tyConsOfType