1 module VectType ( vectTyCon, vectAndLiftType, vectType, vectTypeEnv,
2 -- arrSumArity, pdataCompTys, pdataCompVars,
11 import HscTypes ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
14 import MkCore ( mkWildCase )
21 import FamInstEnv ( FamInst, mkLocalFamInst )
24 import BasicTypes ( StrictnessMark(..), boolToRecFlag )
25 import Var ( Var, TyVar )
26 import Name ( Name, getOccName )
33 import Digraph ( SCC(..), stronglyConnCompFromEdgedVertices )
38 import MonadUtils ( zipWith3M, foldrM, concatMapM )
39 import Control.Monad ( liftM, liftM2, zipWithM, zipWithM_, mapAndUnzipM )
40 import Data.List ( inits, tails, zipWith4, zipWith5, zipWith6 )
42 -- ----------------------------------------------------------------------------
45 vectTyCon :: TyCon -> VM TyCon
47 | isFunTyCon tc = builtin closureTyCon
48 | isBoxedTupleTyCon tc = return tc
49 | isUnLiftedTyCon tc = return tc
50 | otherwise = maybeCantVectoriseM "Tycon not vectorised:" (ppr tc)
53 vectAndLiftType :: Type -> VM (Type, Type)
54 vectAndLiftType ty | Just ty' <- coreView ty = vectAndLiftType ty'
57 mdicts <- mapM paDictArgType tyvars
58 let dicts = [dict | Just dict <- mdicts]
59 vmono_ty <- vectType mono_ty
60 lmono_ty <- mkPDataType vmono_ty
61 return (abstractType tyvars dicts vmono_ty,
62 abstractType tyvars dicts lmono_ty)
64 (tyvars, mono_ty) = splitForAllTys ty
67 vectType :: Type -> VM Type
68 vectType ty | Just ty' <- coreView ty = vectType ty'
69 vectType (TyVarTy tv) = return $ TyVarTy tv
70 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
71 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
72 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
73 (mapM vectAndBoxType [ty1,ty2])
74 vectType ty@(ForAllTy _ _)
76 mdicts <- mapM paDictArgType tyvars
77 mono_ty' <- vectType mono_ty
78 return $ abstractType tyvars [dict | Just dict <- mdicts] mono_ty'
80 (tyvars, mono_ty) = splitForAllTys ty
82 vectType ty = cantVectorise "Can't vectorise type" (ppr ty)
84 vectAndBoxType :: Type -> VM Type
85 vectAndBoxType ty = vectType ty >>= boxType
87 abstractType :: [TyVar] -> [Type] -> Type -> Type
88 abstractType tyvars dicts = mkForAllTys tyvars . mkFunTys dicts
90 -- ----------------------------------------------------------------------------
93 boxType :: Type -> VM Type
95 | Just (tycon, []) <- splitTyConApp_maybe ty
96 , isUnLiftedTyCon tycon
98 r <- lookupBoxedTyCon tycon
100 Just tycon' -> return $ mkTyConApp tycon' []
102 boxType ty = return ty
104 -- ----------------------------------------------------------------------------
107 type TyConGroup = ([TyCon], UniqSet TyCon)
109 vectTypeEnv :: TypeEnv -> VM (TypeEnv, [FamInst], [(Var, CoreExpr)])
112 cs <- readGEnv $ mk_map . global_tycons
113 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
114 keep_dcs = concatMap tyConDataCons keep_tcs
115 zipWithM_ defTyCon keep_tcs keep_tcs
116 zipWithM_ defDataCon keep_dcs keep_dcs
117 new_tcs <- vectTyConDecls conv_tcs
119 let orig_tcs = keep_tcs ++ conv_tcs
120 vect_tcs = keep_tcs ++ new_tcs
122 dfuns <- mapM mkPADFun vect_tcs
123 defTyConPAs (zip vect_tcs dfuns)
124 reprs <- mapM tyConRepr vect_tcs
125 repr_tcs <- zipWith3M buildPReprTyCon orig_tcs vect_tcs reprs
126 pdata_tcs <- zipWith3M buildPDataTyCon orig_tcs vect_tcs reprs
127 binds <- sequence (zipWith6 buildTyConBindings orig_tcs
134 let all_new_tcs = new_tcs ++ repr_tcs ++ pdata_tcs
136 let new_env = extendTypeEnvList env
137 (map ATyCon all_new_tcs
138 ++ [ADataCon dc | tc <- all_new_tcs
139 , dc <- tyConDataCons tc])
141 return (new_env, map mkLocalFamInst (repr_tcs ++ pdata_tcs), concat binds)
143 tycons = typeEnvTyCons env
144 groups = tyConGroups tycons
146 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
149 vectTyConDecls :: [TyCon] -> VM [TyCon]
150 vectTyConDecls tcs = fixV $ \tcs' ->
152 mapM_ (uncurry defTyCon) (zipLazy tcs tcs')
153 mapM vectTyConDecl tcs
155 vectTyConDecl :: TyCon -> VM TyCon
158 name' <- cloneName mkVectTyConOcc name
159 rhs' <- vectAlgTyConRhs tc (algTyConRhs tc)
161 liftDs $ buildAlgTyCon name'
163 [] -- no stupid theta
165 rec_flag -- FIXME: is this ok?
166 False -- FIXME: no generics
167 False -- not GADT syntax
168 Nothing -- not a family instance
171 tyvars = tyConTyVars tc
172 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
174 vectAlgTyConRhs :: TyCon -> AlgTyConRhs -> VM AlgTyConRhs
175 vectAlgTyConRhs _ (DataTyCon { data_cons = data_cons
179 data_cons' <- mapM vectDataCon data_cons
180 zipWithM_ defDataCon data_cons data_cons'
181 return $ DataTyCon { data_cons = data_cons'
184 vectAlgTyConRhs tc _ = cantVectorise "Can't vectorise type definition:" (ppr tc)
186 vectDataCon :: DataCon -> VM DataCon
188 | not . null $ dataConExTyVars dc
189 = cantVectorise "Can't vectorise constructor (existentials):" (ppr dc)
190 | not . null $ dataConEqSpec dc
191 = cantVectorise "Can't vectorise constructor (eq spec):" (ppr dc)
194 name' <- cloneName mkVectDataConOcc name
195 tycon' <- vectTyCon tycon
196 arg_tys <- mapM vectType rep_arg_tys
198 liftDs $ buildDataCon name'
200 (map (const NotMarkedStrict) arg_tys)
201 [] -- no labelled fields
203 [] -- no existential tvs for now
204 [] -- no eq spec for now
207 (mkFamilyTyConApp tycon' (mkTyVarTys univ_tvs))
210 name = dataConName dc
211 univ_tvs = dataConUnivTyVars dc
212 rep_arg_tys = dataConRepArgTys dc
213 tycon = dataConTyCon dc
215 mk_fam_inst :: TyCon -> TyCon -> (TyCon, [Type])
216 mk_fam_inst fam_tc arg_tc
217 = (fam_tc, [mkTyConApp arg_tc . mkTyVarTys $ tyConTyVars arg_tc])
220 buildPReprTyCon :: TyCon -> TyCon -> SumRepr -> VM TyCon
221 buildPReprTyCon orig_tc vect_tc repr
223 name <- cloneName mkPReprTyConOcc (tyConName orig_tc)
224 -- rhs_ty <- buildPReprType vect_tc
225 rhs_ty <- sumReprType repr
226 prepr_tc <- builtin preprTyCon
227 liftDs $ buildSynTyCon name
229 (SynonymTyCon rhs_ty)
231 (Just $ mk_fam_inst prepr_tc vect_tc)
233 tyvars = tyConTyVars vect_tc
235 data CompRepr = Keep Type
236 CoreExpr -- PR dictionary for the type
239 data ProdRepr = EmptyProd
241 | Prod { repr_tup_tc :: TyCon -- representation tuple tycon
242 , repr_ptup_tc :: TyCon -- PData representation tycon
243 , repr_comp_tys :: [Type] -- representation types of
244 , repr_comps :: [CompRepr] -- components
246 data ConRepr = ConRepr DataCon ProdRepr
248 data SumRepr = EmptySum
250 | Sum { repr_sum_tc :: TyCon -- representation sum tycon
251 , repr_psum_tc :: TyCon -- PData representation tycon
252 , repr_sel_ty :: Type -- type of selector
253 , repr_con_tys :: [Type] -- representation types of
254 , repr_cons :: [ConRepr] -- components
257 tyConRepr :: TyCon -> VM SumRepr
258 tyConRepr tc = sum_repr (tyConDataCons tc)
260 sum_repr [] = return EmptySum
261 sum_repr [con] = liftM UnarySum (con_repr con)
263 rs <- mapM con_repr cons
264 sum_tc <- builtin (sumTyCon arity)
265 tys <- mapM conReprType rs
266 (psum_tc, _) <- pdataReprTyCon (mkTyConApp sum_tc tys)
267 sel_ty <- builtin (selTy arity)
268 return $ Sum { repr_sum_tc = sum_tc
269 , repr_psum_tc = psum_tc
270 , repr_sel_ty = sel_ty
277 con_repr con = liftM (ConRepr con) (prod_repr (dataConRepArgTys con))
279 prod_repr [] = return EmptyProd
280 prod_repr [ty] = liftM UnaryProd (comp_repr ty)
282 rs <- mapM comp_repr tys
283 tup_tc <- builtin (prodTyCon arity)
284 tys' <- mapM compReprType rs
285 (ptup_tc, _) <- pdataReprTyCon (mkTyConApp tup_tc tys')
286 return $ Prod { repr_tup_tc = tup_tc
287 , repr_ptup_tc = ptup_tc
288 , repr_comp_tys = tys'
294 comp_repr ty = liftM (Keep ty) (prDictOfType ty)
295 `orElseV` return (Wrap ty)
297 sumReprType :: SumRepr -> VM Type
298 sumReprType EmptySum = voidType
299 sumReprType (UnarySum r) = conReprType r
300 sumReprType (Sum { repr_sum_tc = sum_tc, repr_con_tys = tys })
301 = return $ mkTyConApp sum_tc tys
303 conReprType :: ConRepr -> VM Type
304 conReprType (ConRepr _ r) = prodReprType r
306 prodReprType :: ProdRepr -> VM Type
307 prodReprType EmptyProd = voidType
308 prodReprType (UnaryProd r) = compReprType r
309 prodReprType (Prod { repr_tup_tc = tup_tc, repr_comp_tys = tys })
310 = return $ mkTyConApp tup_tc tys
312 compReprType :: CompRepr -> VM Type
313 compReprType (Keep ty _) = return ty
314 compReprType (Wrap ty) = do
315 wrap_tc <- builtin wrapTyCon
316 return $ mkTyConApp wrap_tc [ty]
318 compOrigType :: CompRepr -> Type
319 compOrigType (Keep ty _) = ty
320 compOrigType (Wrap ty) = ty
322 buildToPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
323 buildToPRepr vect_tc repr_tc _ repr
325 let arg_ty = mkTyConApp vect_tc ty_args
326 res_ty <- mkPReprType arg_ty
327 arg <- newLocalVar (fsLit "x") arg_ty
328 result <- to_sum (Var arg) arg_ty res_ty repr
329 return $ Lam arg result
331 ty_args = mkTyVarTys (tyConTyVars vect_tc)
333 wrap_repr_inst = wrapFamInstBody repr_tc ty_args
335 to_sum arg arg_ty res_ty EmptySum
337 void <- builtin voidVar
338 return $ wrap_repr_inst $ Var void
340 to_sum arg arg_ty res_ty (UnarySum r)
342 (pat, vars, body) <- con_alt r
343 return $ mkWildCase arg arg_ty res_ty
344 [(pat, vars, wrap_repr_inst body)]
346 to_sum arg arg_ty res_ty (Sum { repr_sum_tc = sum_tc
348 , repr_cons = cons })
350 alts <- mapM con_alt cons
351 let ty_args = map Type tys
352 alts' = [(pat, vars, wrap_repr_inst
353 $ mkConApp sum_con (map Type tys ++ [body]))
354 | ((pat, vars, body), sum_con)
355 <- zip alts (tyConDataCons sum_tc)]
356 return $ mkWildCase arg arg_ty res_ty alts'
358 con_alt (ConRepr con r)
360 (vars, body) <- to_prod r
361 return (DataAlt con, vars, body)
365 void <- builtin voidVar
366 return ([], Var void)
368 to_prod (UnaryProd comp)
370 var <- newLocalVar (fsLit "x") (compOrigType comp)
371 body <- to_comp (Var var) comp
374 to_prod(Prod { repr_tup_tc = tup_tc
375 , repr_comp_tys = tys
376 , repr_comps = comps })
378 vars <- newLocalVars (fsLit "x") (map compOrigType comps)
379 exprs <- zipWithM to_comp (map Var vars) comps
380 return (vars, mkConApp tup_con (map Type tys ++ exprs))
382 [tup_con] = tyConDataCons tup_tc
384 to_comp expr (Keep _ _) = return expr
385 to_comp expr (Wrap ty) = do
386 wrap_tc <- builtin wrapTyCon
387 return $ wrapNewTypeBody wrap_tc [ty] expr
390 buildFromPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
391 buildFromPRepr vect_tc repr_tc _ repr
393 arg_ty <- mkPReprType res_ty
394 arg <- newLocalVar (fsLit "x") arg_ty
396 result <- from_sum (unwrapFamInstScrut repr_tc ty_args (Var arg))
398 return $ Lam arg result
400 ty_args = mkTyVarTys (tyConTyVars vect_tc)
401 res_ty = mkTyConApp vect_tc ty_args
403 from_sum expr EmptySum
405 dummy <- builtin fromVoidVar
406 return $ Var dummy `App` Type res_ty
408 from_sum expr (UnarySum r) = from_con expr r
409 from_sum expr (Sum { repr_sum_tc = sum_tc
411 , repr_cons = cons })
413 vars <- newLocalVars (fsLit "x") tys
414 es <- zipWithM from_con (map Var vars) cons
415 return $ mkWildCase expr (exprType expr) res_ty
416 [(DataAlt con, [var], e)
417 | (con, var, e) <- zip3 (tyConDataCons sum_tc) vars es]
419 from_con expr (ConRepr con r)
420 = from_prod expr (mkConApp con $ map Type ty_args) r
422 from_prod expr con EmptyProd = return con
423 from_prod expr con (UnaryProd r)
425 e <- from_comp expr r
428 from_prod expr con (Prod { repr_tup_tc = tup_tc
429 , repr_comp_tys = tys
433 vars <- newLocalVars (fsLit "y") tys
434 es <- zipWithM from_comp (map Var vars) comps
435 return $ mkWildCase expr (exprType expr) res_ty
436 [(DataAlt tup_con, vars, con `mkApps` es)]
438 [tup_con] = tyConDataCons tup_tc
440 from_comp expr (Keep _ _) = return expr
441 from_comp expr (Wrap ty)
443 wrap <- builtin wrapTyCon
444 return $ unwrapNewTypeBody wrap [ty] expr
447 buildToArrPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
448 buildToArrPRepr vect_tc prepr_tc pdata_tc r
450 arg_ty <- mkPDataType el_ty
451 res_ty <- mkPDataType =<< mkPReprType el_ty
452 arg <- newLocalVar (fsLit "xs") arg_ty
454 pdata_co <- mkBuiltinCo pdataTyCon
455 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
456 co = mkAppCoercion pdata_co
458 $ mkTyConApp repr_co ty_args
460 scrut = unwrapFamInstScrut pdata_tc ty_args (Var arg)
462 (vars, result) <- to_sum r
465 $ mkWildCase scrut (mkTyConApp pdata_tc ty_args) res_ty
466 [(DataAlt pdata_dc, vars, mkCoerce co result)]
468 ty_args = mkTyVarTys $ tyConTyVars vect_tc
469 el_ty = mkTyConApp vect_tc ty_args
471 [pdata_dc] = tyConDataCons pdata_tc
475 pvoid <- builtin pvoidVar
476 return ([], Var pvoid)
477 to_sum (UnarySum r) = to_con r
478 to_sum (Sum { repr_psum_tc = psum_tc
479 , repr_sel_ty = sel_ty
484 (vars, exprs) <- mapAndUnzipM to_con cons
485 sel <- newLocalVar (fsLit "sel") sel_ty
486 return (sel : concat vars, mk_result (Var sel) exprs)
488 [psum_con] = tyConDataCons psum_tc
489 mk_result sel exprs = wrapFamInstBody psum_tc tys
491 $ map Type tys ++ (sel : exprs)
493 to_con (ConRepr _ r) = to_prod r
495 to_prod EmptyProd = do
496 pvoid <- builtin pvoidVar
497 return ([], Var pvoid)
498 to_prod (UnaryProd r)
500 pty <- mkPDataType (compOrigType r)
501 var <- newLocalVar (fsLit "x") pty
502 expr <- to_comp (Var var) r
505 to_prod (Prod { repr_ptup_tc = ptup_tc
506 , repr_comp_tys = tys
507 , repr_comps = comps })
509 ptys <- mapM (mkPDataType . compOrigType) comps
510 vars <- newLocalVars (fsLit "x") ptys
511 es <- zipWithM to_comp (map Var vars) comps
512 return (vars, mk_result es)
514 [ptup_con] = tyConDataCons ptup_tc
515 mk_result exprs = wrapFamInstBody ptup_tc tys
517 $ map Type tys ++ exprs
519 to_comp expr (Keep _ _) = return expr
521 -- FIXME: this is bound to be wrong!
522 to_comp expr (Wrap ty)
524 wrap_tc <- builtin wrapTyCon
525 (pwrap_tc, _) <- pdataReprTyCon (mkTyConApp wrap_tc [ty])
526 return $ wrapNewTypeBody pwrap_tc [ty] expr
529 buildFromArrPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
530 buildFromArrPRepr vect_tc prepr_tc pdata_tc r
532 arg_ty <- mkPDataType =<< mkPReprType el_ty
533 res_ty <- mkPDataType el_ty
534 arg <- newLocalVar (fsLit "xs") arg_ty
536 pdata_co <- mkBuiltinCo pdataTyCon
537 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
538 co = mkAppCoercion pdata_co
539 $ mkTyConApp repr_co var_tys
541 scrut = mkCoerce co (Var arg)
543 mk_result args = wrapFamInstBody pdata_tc var_tys
545 $ map Type var_tys ++ args
547 (expr, _) <- fixV $ \ ~(_, args) ->
548 from_sum res_ty (mk_result args) scrut r
550 return $ Lam arg expr
552 -- (args, mk) <- from_sum res_ty scrut r
554 -- let result = wrapFamInstBody pdata_tc var_tys
555 -- . mkConApp pdata_dc
556 -- $ map Type var_tys ++ args
558 -- return $ Lam arg (mk result)
560 var_tys = mkTyVarTys $ tyConTyVars vect_tc
561 el_ty = mkTyConApp vect_tc var_tys
563 [pdata_con] = tyConDataCons pdata_tc
565 from_sum res_ty res expr EmptySum = return (res, [])
566 from_sum res_ty res expr (UnarySum r) = from_con res_ty res expr r
567 from_sum res_ty res expr (Sum { repr_psum_tc = psum_tc
568 , repr_sel_ty = sel_ty
570 , repr_cons = cons })
572 sel <- newLocalVar (fsLit "sel") sel_ty
573 ptys <- mapM mkPDataType tys
574 vars <- newLocalVars (fsLit "xs") ptys
575 (res', args) <- fold from_con res_ty res (map Var vars) cons
576 let scrut = unwrapFamInstScrut psum_tc tys expr
577 body = mkWildCase scrut (exprType scrut) res_ty
578 [(DataAlt psum_con, sel : vars, res')]
579 return (body, Var sel : args)
581 [psum_con] = tyConDataCons psum_tc
584 from_con res_ty res expr (ConRepr _ r) = from_prod res_ty res expr r
586 from_prod res_ty res expr EmptyProd = return (res, [])
587 from_prod res_ty res expr (UnaryProd r)
588 = from_comp res_ty res expr r
589 from_prod res_ty res expr (Prod { repr_ptup_tc = ptup_tc
590 , repr_comp_tys = tys
591 , repr_comps = comps })
593 ptys <- mapM mkPDataType tys
594 vars <- newLocalVars (fsLit "ys") ptys
595 (res', args) <- fold from_comp res_ty res (map Var vars) comps
596 let scrut = unwrapFamInstScrut ptup_tc tys expr
597 body = mkWildCase scrut (exprType scrut) res_ty
598 [(DataAlt ptup_con, vars, res')]
601 [ptup_con] = tyConDataCons ptup_tc
603 from_comp res_ty res expr (Keep _ _) = return (res, [expr])
604 from_comp res_ty res expr (Wrap ty)
606 wrap_tc <- builtin wrapTyCon
607 (pwrap_tc, _) <- pdataReprTyCon (mkTyConApp wrap_tc [ty])
608 return (res, [unwrapNewTypeBody pwrap_tc [ty]
609 $ unwrapFamInstScrut pwrap_tc [ty] expr])
611 fold f res_ty res exprs rs = foldrM f' (res, []) (zip exprs rs)
613 f' (expr, r) (res, args) = do
614 (res', args') <- f res_ty res expr r
615 return (res', args' ++ args)
617 buildPRDict :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
618 buildPRDict vect_tc prepr_tc _ r
621 pr_co <- mkBuiltinCo prTyCon
622 let co = mkAppCoercion pr_co
624 $ mkTyConApp arg_co ty_args
625 return (mkCoerce co dict)
627 ty_args = mkTyVarTys (tyConTyVars vect_tc)
628 Just arg_co = tyConFamilyCoercion_maybe prepr_tc
630 sum_dict EmptySum = prDFunOfTyCon =<< builtin voidTyCon
631 sum_dict (UnarySum r) = con_dict r
632 sum_dict (Sum { repr_sum_tc = sum_tc
637 dicts <- mapM con_dict cons
638 dfun <- prDFunOfTyCon sum_tc
639 return $ dfun `mkTyApps` tys `mkApps` dicts
641 con_dict (ConRepr _ r) = prod_dict r
643 prod_dict EmptyProd = prDFunOfTyCon =<< builtin voidTyCon
644 prod_dict (UnaryProd r) = comp_dict r
645 prod_dict (Prod { repr_tup_tc = tup_tc
646 , repr_comp_tys = tys
647 , repr_comps = comps })
649 dicts <- mapM comp_dict comps
650 dfun <- prDFunOfTyCon tup_tc
651 return $ dfun `mkTyApps` tys `mkApps` dicts
653 comp_dict (Keep _ pr) = return pr
654 comp_dict (Wrap ty) = wrapPR ty
657 buildPDataTyCon :: TyCon -> TyCon -> SumRepr -> VM TyCon
658 buildPDataTyCon orig_tc vect_tc repr = fixV $ \repr_tc ->
660 name' <- cloneName mkPDataTyConOcc orig_name
661 rhs <- buildPDataTyConRhs orig_name vect_tc repr_tc repr
662 pdata <- builtin pdataTyCon
664 liftDs $ buildAlgTyCon name'
666 [] -- no stupid theta
668 rec_flag -- FIXME: is this ok?
669 False -- FIXME: no generics
670 False -- not GADT syntax
671 (Just $ mk_fam_inst pdata vect_tc)
673 orig_name = tyConName orig_tc
674 tyvars = tyConTyVars vect_tc
675 rec_flag = boolToRecFlag (isRecursiveTyCon vect_tc)
678 buildPDataTyConRhs :: Name -> TyCon -> TyCon -> SumRepr -> VM AlgTyConRhs
679 buildPDataTyConRhs orig_name vect_tc repr_tc repr
681 data_con <- buildPDataDataCon orig_name vect_tc repr_tc repr
682 return $ DataTyCon { data_cons = [data_con], is_enum = False }
684 buildPDataDataCon :: Name -> TyCon -> TyCon -> SumRepr -> VM DataCon
685 buildPDataDataCon orig_name vect_tc repr_tc repr
687 dc_name <- cloneName mkPDataDataConOcc orig_name
688 comp_tys <- sum_tys repr
690 liftDs $ buildDataCon dc_name
692 (map (const NotMarkedStrict) comp_tys)
693 [] -- no field labels
695 [] -- no existentials
699 (mkFamilyTyConApp repr_tc (mkTyVarTys tvs))
702 tvs = tyConTyVars vect_tc
704 sum_tys EmptySum = return []
705 sum_tys (UnarySum r) = con_tys r
706 sum_tys (Sum { repr_sel_ty = sel_ty
707 , repr_cons = cons })
708 = liftM (sel_ty :) (concatMapM con_tys cons)
710 con_tys (ConRepr _ r) = prod_tys r
712 prod_tys EmptyProd = return []
713 prod_tys (UnaryProd r) = liftM singleton (comp_ty r)
714 prod_tys (Prod { repr_comps = comps }) = mapM comp_ty comps
716 comp_ty r = mkPDataType (compOrigType r)
719 mkPADFun :: TyCon -> VM Var
721 = newExportedVar (mkPADFunOcc $ getOccName vect_tc) =<< paDFunType vect_tc
723 buildTyConBindings :: TyCon -> TyCon -> TyCon -> TyCon -> Var -> SumRepr
724 -> VM [(Var, CoreExpr)]
725 buildTyConBindings orig_tc vect_tc prepr_tc pdata_tc dfun repr
727 vectDataConWorkers orig_tc vect_tc pdata_tc
728 dict <- buildPADict vect_tc prepr_tc pdata_tc repr
730 return $ (dfun, dict) : binds
732 vectDataConWorkers :: TyCon -> TyCon -> TyCon -> VM ()
733 vectDataConWorkers orig_tc vect_tc arr_tc
736 . zipWith3 def_worker (tyConDataCons orig_tc) rep_tys
737 $ zipWith4 mk_data_con (tyConDataCons vect_tc)
740 (tail $ tails rep_tys)
741 mapM_ (uncurry hoistBinding) bs
743 tyvars = tyConTyVars vect_tc
744 var_tys = mkTyVarTys tyvars
745 ty_args = map Type var_tys
746 res_ty = mkTyConApp vect_tc var_tys
748 cons = tyConDataCons vect_tc
750 [arr_dc] = tyConDataCons arr_tc
752 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
755 mk_data_con con tys pre post
756 = liftM2 (,) (vect_data_con con)
757 (lift_data_con tys pre post (mkDataConTag con))
759 sel_replicate len tag
761 rep <- builtin (selReplicate arity)
762 return [rep `mkApps` [len, tag]]
764 | otherwise = return []
766 vect_data_con con = return $ mkConApp con ty_args
767 lift_data_con tys pre_tys post_tys tag
769 len <- builtin liftingContext
770 args <- mapM (newLocalVar (fsLit "xs"))
771 =<< mapM mkPDataType tys
773 sel <- sel_replicate (Var len) tag
775 pre <- mapM emptyPD (concat pre_tys)
776 post <- mapM emptyPD (concat post_tys)
778 return . mkLams (len : args)
779 . wrapFamInstBody arr_tc var_tys
781 $ ty_args ++ sel ++ pre ++ map Var args ++ post
783 def_worker data_con arg_tys mk_body
787 . polyAbstract tyvars $ \abstract ->
788 liftM (abstract . vectorised)
789 $ buildClosures tyvars [] arg_tys res_ty mk_body
791 vect_worker <- cloneId mkVectOcc orig_worker (exprType body)
792 defGlobalVar orig_worker vect_worker
793 return (vect_worker, body)
795 orig_worker = dataConWorkId data_con
797 buildPADict :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
798 buildPADict vect_tc prepr_tc arr_tc repr
799 = polyAbstract tvs $ \abstract ->
801 meth_binds <- mapM mk_method paMethods
802 let meth_exprs = map (Var . fst) meth_binds
804 pa_dc <- builtin paDataCon
805 let dict = mkConApp pa_dc (Type (mkTyConApp vect_tc arg_tys) : meth_exprs)
806 body = Let (Rec meth_binds) dict
807 return . mkInlineMe $ abstract body
809 tvs = tyConTyVars arr_tc
810 arg_tys = mkTyVarTys tvs
812 mk_method (name, build)
815 body <- build vect_tc prepr_tc arr_tc repr
816 var <- newLocalVar name (exprType body)
817 return (var, mkInlineMe body)
819 paMethods :: [(FastString, TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr)]
820 paMethods = [(fsLit "dictPRepr", buildPRDict),
821 (fsLit "toPRepr", buildToPRepr),
822 (fsLit "fromPRepr", buildFromPRepr),
823 (fsLit "toArrPRepr", buildToArrPRepr),
824 (fsLit "fromArrPRepr", buildFromArrPRepr)]
826 -- | Split the given tycons into two sets depending on whether they have to be
827 -- converted (first list) or not (second list). The first argument contains
828 -- information about the conversion status of external tycons:
830 -- * tycons which have converted versions are mapped to True
831 -- * tycons which are not changed by vectorisation are mapped to False
832 -- * tycons which can't be converted are not elements of the map
834 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
835 classifyTyCons = classify [] []
837 classify conv keep _ [] = (conv, keep)
838 classify conv keep cs ((tcs, ds) : rs)
839 | can_convert && must_convert
840 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
842 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
844 = classify conv keep cs rs
846 refs = ds `delListFromUniqSet` tcs
848 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
849 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
851 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
853 -- | Compute mutually recursive groups of tycons in topological order
855 tyConGroups :: [TyCon] -> [TyConGroup]
856 tyConGroups tcs = map mk_grp (stronglyConnCompFromEdgedVertices edges)
858 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
859 , let ds = tyConsOfTyCon tc]
861 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
862 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
864 (tcs, dss) = unzip els
866 tyConsOfTyCon :: TyCon -> UniqSet TyCon
868 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
870 tyConsOfType :: Type -> UniqSet TyCon
872 | Just ty' <- coreView ty = tyConsOfType ty'
873 tyConsOfType (TyVarTy _) = emptyUniqSet
874 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
876 extend | isUnLiftedTyCon tc
877 || isTupleTyCon tc = id
879 | otherwise = (`addOneToUniqSet` tc)
881 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
882 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
883 `addOneToUniqSet` funTyCon
884 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
885 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
887 tyConsOfTypes :: [Type] -> UniqSet TyCon
888 tyConsOfTypes = unionManyUniqSets . map tyConsOfType
891 -- ----------------------------------------------------------------------------
894 fromVect :: Type -> CoreExpr -> VM CoreExpr
895 fromVect ty expr | Just ty' <- coreView ty = fromVect ty' expr
896 fromVect (FunTy arg_ty res_ty) expr
898 arg <- newLocalVar (fsLit "x") arg_ty
899 varg <- toVect arg_ty (Var arg)
900 varg_ty <- vectType arg_ty
901 vres_ty <- vectType res_ty
902 apply <- builtin applyVar
903 body <- fromVect res_ty
904 $ Var apply `mkTyApps` [varg_ty, vres_ty] `mkApps` [expr, varg]
905 return $ Lam arg body
907 = identityConv ty >> return expr
909 toVect :: Type -> CoreExpr -> VM CoreExpr
910 toVect ty expr = identityConv ty >> return expr
912 identityConv :: Type -> VM ()
913 identityConv ty | Just ty' <- coreView ty = identityConv ty'
914 identityConv (TyConApp tycon tys)
916 mapM_ identityConv tys
917 identityConvTyCon tycon
920 identityConvTyCon :: TyCon -> VM ()
922 | isBoxedTupleTyCon tc = return ()
923 | isUnLiftedTyCon tc = return ()
925 tc' <- maybeV (lookupTyCon tc)
926 if tc == tc' then return () else noV