1 module VectType ( vectTyCon, vectType, vectTypeEnv )
4 #include "HsVersions.h"
9 import HscTypes ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
17 import FamInstEnv ( FamInst, mkLocalFamInst )
18 import InstEnv ( Instance )
21 import BasicTypes ( StrictnessMark(..), boolToRecFlag )
23 import Id ( mkWildId )
26 import TysWiredIn ( intTy, intDataCon )
27 import TysPrim ( intPrimTy )
32 import Digraph ( SCC(..), stronglyConnComp )
36 import Control.Monad ( liftM, liftM2, zipWithM, zipWithM_ )
37 import Data.List ( inits, tails )
39 -- ----------------------------------------------------------------------------
42 vectTyCon :: TyCon -> VM TyCon
44 | isFunTyCon tc = builtin closureTyCon
45 | isBoxedTupleTyCon tc = return tc
46 | isUnLiftedTyCon tc = return tc
50 Just tc' -> return tc'
52 -- FIXME: just for now
53 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
55 vectType :: Type -> VM Type
56 vectType ty | Just ty' <- coreView ty = vectType ty'
57 vectType (TyVarTy tv) = return $ TyVarTy tv
58 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
59 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
60 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
61 (mapM vectType [ty1,ty2])
62 vectType ty@(ForAllTy _ _)
64 mdicts <- mapM paDictArgType tyvars
65 mono_ty' <- vectType mono_ty
66 return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
68 (tyvars, mono_ty) = splitForAllTys ty
70 vectType ty = pprPanic "vectType:" (ppr ty)
72 -- ----------------------------------------------------------------------------
75 type TyConGroup = ([TyCon], UniqSet TyCon)
77 vectTypeEnv :: TypeEnv -> VM (TypeEnv, [FamInst], [Instance])
80 cs <- readGEnv $ mk_map . global_tycons
81 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
82 keep_dcs = concatMap tyConDataCons keep_tcs
83 zipWithM_ defTyCon keep_tcs keep_tcs
84 zipWithM_ defDataCon keep_dcs keep_dcs
85 vect_tcs <- vectTyConDecls conv_tcs
86 parr_tcs1 <- zipWithM buildPArrayTyCon keep_tcs keep_tcs
87 parr_tcs2 <- zipWithM buildPArrayTyCon conv_tcs vect_tcs
88 let new_tcs = vect_tcs ++ parr_tcs1 ++ parr_tcs2
90 let new_env = extendTypeEnvList env
92 ++ [ADataCon dc | tc <- new_tcs
93 , dc <- tyConDataCons tc])
95 return (new_env, map mkLocalFamInst (parr_tcs1 ++ parr_tcs2), [])
97 tycons = typeEnvTyCons env
98 groups = tyConGroups tycons
100 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
102 keep_tc tc = let dcs = tyConDataCons tc
104 defTyCon tc tc >> zipWithM_ defDataCon dcs dcs
107 vectTyConDecls :: [TyCon] -> VM [TyCon]
108 vectTyConDecls tcs = fixV $ \tcs' ->
110 mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
111 mapM vectTyConDecl tcs
114 lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
116 vectTyConDecl :: TyCon -> VM TyCon
119 name' <- cloneName mkVectTyConOcc name
120 rhs' <- vectAlgTyConRhs (algTyConRhs tc)
122 return $ mkAlgTyCon name'
125 [] -- no stupid theta
127 [] -- no selector ids
128 NoParentTyCon -- FIXME
129 rec_flag -- FIXME: is this ok?
130 False -- FIXME: no generics
131 False -- not GADT syntax
135 tyvars = tyConTyVars tc
136 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
138 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
139 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
143 data_cons' <- mapM vectDataCon data_cons
144 zipWithM_ defDataCon data_cons data_cons'
145 return $ DataTyCon { data_cons = data_cons'
149 vectDataCon :: DataCon -> VM DataCon
151 | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
152 | not . null $ dataConEqSpec dc = pprPanic "vectDataCon: eq spec" (ppr dc)
155 name' <- cloneName mkVectDataConOcc name
156 tycon' <- vectTyCon tycon
157 arg_tys <- mapM vectType rep_arg_tys
158 wrk_name <- cloneName mkDataConWorkerOcc name'
160 let ids = mkDataConIds (panic "vectDataCon: wrapper id")
163 data_con = mkDataCon name'
165 (map (const NotMarkedStrict) arg_tys)
166 [] -- no labelled fields
168 [] -- no existential tvs for now
169 [] -- no eq spec for now
173 [] -- no stupid theta
177 name = dataConName dc
178 univ_tvs = dataConUnivTyVars dc
179 rep_arg_tys = dataConRepArgTys dc
180 tycon = dataConTyCon dc
182 buildPArrayTyCon :: TyCon -> TyCon -> VM TyCon
183 buildPArrayTyCon orig_tc vect_tc = fixV $ \repr_tc ->
185 name' <- cloneName mkPArrayTyConOcc orig_name
186 parent <- buildPArrayParentInfo orig_name vect_tc repr_tc
187 rhs <- buildPArrayTyConRhs orig_name vect_tc repr_tc
189 return $ mkAlgTyCon name'
192 [] -- no stupid theta
194 [] -- no selector ids
196 rec_flag -- FIXME: is this ok?
197 False -- FIXME: no generics
198 False -- not GADT syntax
200 orig_name = tyConName orig_tc
201 name = tyConName vect_tc
202 kind = tyConKind vect_tc
203 tyvars = tyConTyVars vect_tc
204 rec_flag = boolToRecFlag (isRecursiveTyCon vect_tc)
207 buildPArrayParentInfo :: Name -> TyCon -> TyCon -> VM TyConParent
208 buildPArrayParentInfo orig_name vect_tc repr_tc
210 parray_tc <- builtin parrayTyCon
211 co_name <- cloneName mkInstTyCoOcc (tyConName repr_tc)
213 let inst_tys = [mkTyConApp vect_tc (map mkTyVarTy tyvars)]
215 return . FamilyTyCon parray_tc inst_tys
216 $ mkFamInstCoercion co_name
222 tyvars = tyConTyVars vect_tc
224 buildPArrayTyConRhs :: Name -> TyCon -> TyCon -> VM AlgTyConRhs
225 buildPArrayTyConRhs orig_name vect_tc repr_tc
227 data_con <- buildPArrayDataCon orig_name vect_tc repr_tc
228 return $ DataTyCon { data_cons = [data_con], is_enum = False }
230 buildPArrayDataCon :: Name -> TyCon -> TyCon -> VM DataCon
231 buildPArrayDataCon orig_name vect_tc repr_tc
233 dc_name <- cloneName mkPArrayDataConOcc orig_name
234 shape_ty <- mkPArrayType intTy -- FIXME: we want to unbox this!
235 repr_tys <- mapM mkPArrayType types
236 wrk_name <- cloneName mkDataConWorkerOcc dc_name
237 wrp_name <- cloneName mkDataConWrapperOcc dc_name
239 let ids = mkDataConIds wrp_name wrk_name data_con
240 data_con = mkDataCon dc_name
242 (MarkedStrict : map (const NotMarkedStrict) repr_tys)
244 (tyConTyVars vect_tc)
248 (shape_ty : repr_tys)
255 types = [ty | dc <- tyConDataCons vect_tc
256 , ty <- dataConRepArgTys dc]
258 buildPADict :: Var -> TyCon -> TyCon -> VM [(Var, CoreExpr)]
259 buildPADict var vect_tc arr_tc
260 = localV . abstractOverTyVars (tyConTyVars arr_tc) $ \abstract ->
262 meth_binds <- mapM (mk_method abstract) paMethods
263 let meth_vars = map (Var . fst) meth_binds
264 meth_exprs <- mapM (`applyToTypes` arg_tys) meth_vars
266 pa_dc <- builtin paDictDataCon
267 let dict = mkConApp pa_dc (Type (mkTyConApp vect_tc arg_tys) : meth_exprs)
268 return $ (var, dict) : meth_binds
270 tvs = tyConTyVars arr_tc
271 arg_tys = mkTyVarTys tvs
273 mk_method abstract (name, build)
276 body <- liftM abstract $ build vect_tc arr_tc
277 var <- newLocalVar name (exprType body)
278 return (var, mkInlineMe body)
280 paMethods = [(FSLIT("lengthPA"), buildLengthPA),
281 (FSLIT("replicatePA"), buildReplicatePA)]
283 buildLengthPA :: TyCon -> TyCon -> VM CoreExpr
284 buildLengthPA _ arr_tc
286 arg <- newLocalVar FSLIT("xs") arg_ty
287 shape <- newLocalVar FSLIT("sel") shape_ty
288 body <- lengthPA (Var shape)
290 $ Case (Var arg) (mkWildId arg_ty) intPrimTy
291 [(DataAlt repr_dc, shape : map mkWildId repr_tys, body)]
293 arg_ty = mkTyConApp arr_tc . mkTyVarTys $ tyConTyVars arr_tc
294 [repr_dc] = tyConDataCons arr_tc
295 shape_ty : repr_tys = dataConRepArgTys repr_dc
298 -- data T = C0 t1 ... tm
302 -- data [:T:] = A ![:Int:] [:t1:] ... [:un:]
304 -- replicatePA :: Int# -> T -> [:T:]
306 -- = let c = case t of
312 -- C0 x1 _ ... _ -> replicatePA @t1 n# x1
318 -- Ck _ ... _ yn -> replicatePA @un n# yn
321 -- A (replicatePA @Int n# c) xs1 ... ysn
325 buildReplicatePA :: TyCon -> TyCon -> VM CoreExpr
326 buildReplicatePA vect_tc arr_tc
328 len_var <- newLocalVar FSLIT("n") intPrimTy
329 val_var <- newLocalVar FSLIT("x") val_ty
331 let len = Var len_var
334 shape <- replicatePA len (ctr_num val)
335 reprs <- liftM concat $ mapM (mk_comp_arrs len val) vect_dcs
337 return . mkLams [len_var, val_var]
338 $ mkConApp arr_dc (map (Type . TyVarTy) (tyConTyVars arr_tc) ++ (shape : reprs))
340 val_ty = mkTyConApp vect_tc . mkTyVarTys $ tyConTyVars arr_tc
341 wild = mkWildId val_ty
342 vect_dcs = tyConDataCons vect_tc
343 [arr_dc] = tyConDataCons arr_tc
345 ctr_num val = Case val wild intTy (zipWith ctr_num_alt vect_dcs [0..])
346 ctr_num_alt dc i = (DataAlt dc, map mkWildId (dataConRepArgTys dc),
347 mkConApp intDataCon [mkIntLitInt i])
350 mk_comp_arrs len val dc = let tys = dataConRepArgTys dc
351 wilds = map mkWildId tys
353 sequence (zipWith3 (mk_comp_arr len val dc)
354 tys (inits wilds) (tails wilds))
356 mk_comp_arr len val dc ty pre (_:post)
358 var <- newLocalVar FSLIT("x") ty
359 rep <- replicatePA len (Var var)
361 arr_ty <- mkPArrayType ty
363 return $ Case val wild arr_ty
364 [(DataAlt dc, pre ++ (var : post), rep), (DEFAULT, [], empty)]
366 -- | Split the given tycons into two sets depending on whether they have to be
367 -- converted (first list) or not (second list). The first argument contains
368 -- information about the conversion status of external tycons:
370 -- * tycons which have converted versions are mapped to True
371 -- * tycons which are not changed by vectorisation are mapped to False
372 -- * tycons which can't be converted are not elements of the map
374 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
375 classifyTyCons = classify [] []
377 classify conv keep cs [] = (conv, keep)
378 classify conv keep cs ((tcs, ds) : rs)
379 | can_convert && must_convert
380 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
382 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
384 = classify conv keep cs rs
386 refs = ds `delListFromUniqSet` tcs
388 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
389 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
391 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
393 -- | Compute mutually recursive groups of tycons in topological order
395 tyConGroups :: [TyCon] -> [TyConGroup]
396 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
398 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
399 , let ds = tyConsOfTyCon tc]
401 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
402 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
404 (tcs, dss) = unzip els
406 tyConsOfTyCon :: TyCon -> UniqSet TyCon
408 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
410 tyConsOfType :: Type -> UniqSet TyCon
412 | Just ty' <- coreView ty = tyConsOfType ty'
413 tyConsOfType (TyVarTy v) = emptyUniqSet
414 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
416 extend | isUnLiftedTyCon tc
417 || isTupleTyCon tc = id
419 | otherwise = (`addOneToUniqSet` tc)
421 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
422 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
423 `addOneToUniqSet` funTyCon
424 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
425 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
427 tyConsOfTypes :: [Type] -> UniqSet TyCon
428 tyConsOfTypes = unionManyUniqSets . map tyConsOfType