2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
8 module VectType ( vectTyCon, vectType, vectTypeEnv,
9 PAInstance, buildPADict )
12 #include "HsVersions.h"
18 import HscTypes ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
27 import FamInstEnv ( FamInst, mkLocalFamInst )
28 import InstEnv ( Instance, mkLocalInstance, instanceDFunId )
31 import BasicTypes ( StrictnessMark(..), OverlapFlag(..), boolToRecFlag )
33 import Id ( mkWildId )
34 import Name ( Name, getOccName )
37 import TysPrim ( intPrimTy )
42 import Util ( singleton )
43 import Digraph ( SCC(..), stronglyConnComp )
47 import Control.Monad ( liftM, liftM2, zipWithM, zipWithM_, mapAndUnzipM )
48 import Data.List ( inits, tails, zipWith4, zipWith5 )
50 -- ----------------------------------------------------------------------------
53 vectTyCon :: TyCon -> VM TyCon
55 | isFunTyCon tc = builtin closureTyCon
56 | isBoxedTupleTyCon tc = return tc
57 | isUnLiftedTyCon tc = return tc
61 Just tc' -> return tc'
63 -- FIXME: just for now
64 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
66 vectType :: Type -> VM Type
67 vectType ty | Just ty' <- coreView ty = vectType ty'
68 vectType (TyVarTy tv) = return $ TyVarTy tv
69 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
70 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
71 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
72 (mapM vectType [ty1,ty2])
73 vectType ty@(ForAllTy _ _)
75 mdicts <- mapM paDictArgType tyvars
76 mono_ty' <- vectType mono_ty
77 return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
79 (tyvars, mono_ty) = splitForAllTys ty
81 vectType ty = pprPanic "vectType:" (ppr ty)
83 -- ----------------------------------------------------------------------------
86 type TyConGroup = ([TyCon], UniqSet TyCon)
88 data PAInstance = PAInstance {
90 , painstOrigTyCon :: TyCon
91 , painstVectTyCon :: TyCon
92 , painstArrTyCon :: TyCon
95 vectTypeEnv :: TypeEnv -> VM (TypeEnv, [FamInst], [(Var, CoreExpr)])
98 cs <- readGEnv $ mk_map . global_tycons
99 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
100 keep_dcs = concatMap tyConDataCons keep_tcs
101 zipWithM_ defTyCon keep_tcs keep_tcs
102 zipWithM_ defDataCon keep_dcs keep_dcs
103 new_tcs <- vectTyConDecls conv_tcs
105 let orig_tcs = keep_tcs ++ conv_tcs
106 vect_tcs = keep_tcs ++ new_tcs
108 repr_tcs <- zipWithM buildPReprTyCon orig_tcs vect_tcs
109 parr_tcs <- zipWithM buildPArrayTyCon orig_tcs vect_tcs
110 dfuns <- mapM mkPADFun vect_tcs
111 defTyConPAs (zip vect_tcs dfuns)
112 binds <- sequence (zipWith5 buildTyConBindings orig_tcs
118 let all_new_tcs = new_tcs ++ repr_tcs ++ parr_tcs
120 let new_env = extendTypeEnvList env
121 (map ATyCon all_new_tcs
122 ++ [ADataCon dc | tc <- all_new_tcs
123 , dc <- tyConDataCons tc])
125 return (new_env, map mkLocalFamInst (repr_tcs ++ parr_tcs), concat binds)
127 tycons = typeEnvTyCons env
128 groups = tyConGroups tycons
130 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
132 keep_tc tc = let dcs = tyConDataCons tc
134 defTyCon tc tc >> zipWithM_ defDataCon dcs dcs
137 vectTyConDecls :: [TyCon] -> VM [TyCon]
138 vectTyConDecls tcs = fixV $ \tcs' ->
140 mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
141 mapM vectTyConDecl tcs
144 lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
146 vectTyConDecl :: TyCon -> VM TyCon
149 name' <- cloneName mkVectTyConOcc name
150 rhs' <- vectAlgTyConRhs (algTyConRhs tc)
152 liftDs $ buildAlgTyCon name'
154 [] -- no stupid theta
156 rec_flag -- FIXME: is this ok?
157 False -- FIXME: no generics
158 False -- not GADT syntax
159 Nothing -- not a family instance
162 tyvars = tyConTyVars tc
163 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
165 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
166 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
170 data_cons' <- mapM vectDataCon data_cons
171 zipWithM_ defDataCon data_cons data_cons'
172 return $ DataTyCon { data_cons = data_cons'
176 vectDataCon :: DataCon -> VM DataCon
178 | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
179 | not . null $ dataConEqSpec dc = pprPanic "vectDataCon: eq spec" (ppr dc)
182 name' <- cloneName mkVectDataConOcc name
183 tycon' <- vectTyCon tycon
184 arg_tys <- mapM vectType rep_arg_tys
186 liftDs $ buildDataCon name'
188 (map (const NotMarkedStrict) arg_tys)
189 [] -- no labelled fields
191 [] -- no existential tvs for now
192 [] -- no eq spec for now
197 name = dataConName dc
198 univ_tvs = dataConUnivTyVars dc
199 rep_arg_tys = dataConRepArgTys dc
200 tycon = dataConTyCon dc
202 mk_fam_inst :: TyCon -> TyCon -> (TyCon, [Type])
203 mk_fam_inst fam_tc arg_tc
204 = (fam_tc, [mkTyConApp arg_tc . mkTyVarTys $ tyConTyVars arg_tc])
206 buildPReprTyCon :: TyCon -> TyCon -> VM TyCon
207 buildPReprTyCon orig_tc vect_tc
209 name <- cloneName mkPReprTyConOcc (tyConName orig_tc)
210 rhs_ty <- buildPReprType vect_tc
211 prepr_tc <- builtin preprTyCon
212 liftDs $ buildSynTyCon name
214 (SynonymTyCon rhs_ty)
215 (Just $ mk_fam_inst prepr_tc vect_tc)
217 tyvars = tyConTyVars vect_tc
220 data Repr = ProdRepr {
221 prod_components :: [Type]
222 , prod_tycon :: TyCon
223 , prod_data_con :: DataCon
224 , prod_arr_tycon :: TyCon
225 , prod_arr_data_con :: DataCon
229 sum_components :: [Repr]
231 , sum_arr_tycon :: TyCon
232 , sum_arr_data_con :: DataCon
239 , void_bottom :: CoreExpr
244 , enum_data_con :: DataCon
245 , enum_arr_tycon :: TyCon
246 , enum_arr_data_con :: DataCon
252 tycon <- builtin voidTyCon
253 var <- builtin voidVar
256 , void_bottom = Var var
262 (arr_tycon, _) <- parrayReprTyCon intTy
263 let [arr_data_con] = tyConDataCons arr_tycon
267 , enum_data_con = data_con
268 , enum_arr_tycon = arr_tycon
269 , enum_arr_data_con = arr_data_con
273 data_con = intDataCon
275 unboxedProductRepr :: [Type] -> VM Repr
276 unboxedProductRepr [] = voidRepr
277 unboxedProductRepr [ty] = return $ IdRepr ty
278 unboxedProductRepr tys = boxedProductRepr tys
280 boxedProductRepr :: [Type] -> VM Repr
283 tycon <- builtin (prodTyCon arity)
284 let [data_con] = tyConDataCons tycon
286 (arr_tycon, _) <- parrayReprTyCon $ mkTyConApp tycon tys
287 let [arr_data_con] = tyConDataCons arr_tycon
290 prod_components = tys
292 , prod_data_con = data_con
293 , prod_arr_tycon = arr_tycon
294 , prod_arr_data_con = arr_data_con
299 sumRepr :: [Repr] -> VM Repr
300 sumRepr [] = voidRepr
301 sumRepr [repr] = boxRepr repr
304 tycon <- builtin (sumTyCon arity)
305 (arr_tycon, _) <- parrayReprTyCon
309 let [arr_data_con] = tyConDataCons arr_tycon
312 sum_components = reprs
314 , sum_arr_tycon = arr_tycon
315 , sum_arr_data_con = arr_data_con
320 splitSumRepr :: Repr -> [Repr]
321 splitSumRepr (SumRepr { sum_components = reprs }) = reprs
322 splitSumRepr repr = [repr]
324 boxRepr :: Repr -> VM Repr
325 boxRepr (VoidRepr {}) = boxedProductRepr []
326 boxRepr (IdRepr ty) = boxedProductRepr [ty]
327 boxRepr repr = return repr
329 reprType :: Repr -> Type
330 reprType (ProdRepr { prod_tycon = tycon, prod_components = tys })
331 = mkTyConApp tycon tys
332 reprType (SumRepr { sum_tycon = tycon, sum_components = reprs })
333 = mkTyConApp tycon (map reprType reprs)
334 reprType (IdRepr ty) = ty
335 reprType (VoidRepr { void_tycon = tycon }) = mkTyConApp tycon []
336 reprType (EnumRepr { enum_tycon = tycon }) = mkTyConApp tycon []
338 arrReprType :: Repr -> VM Type
339 arrReprType = mkPArrayType . reprType
341 arrShapeTys :: Repr -> VM [Type]
342 arrShapeTys (SumRepr {})
344 int_arr <- builtin parrayIntPrimTyCon
345 return [intPrimTy, mkTyConApp int_arr [], mkTyConApp int_arr []]
346 arrShapeTys (ProdRepr {}) = return [intPrimTy]
347 arrShapeTys (IdRepr _) = return []
348 arrShapeTys (VoidRepr {}) = return [intPrimTy]
349 arrShapeTys (EnumRepr {}) = return [intPrimTy]
351 arrShapeVars :: Repr -> VM [Var]
352 arrShapeVars repr = mapM (newLocalVar FSLIT("sh")) =<< arrShapeTys repr
354 replicateShape :: Repr -> CoreExpr -> CoreExpr -> VM [CoreExpr]
355 replicateShape (ProdRepr {}) len _ = return [len]
356 replicateShape (SumRepr {}) len tag
358 rep <- builtin replicatePAIntPrimVar
359 up <- builtin upToPAIntPrimVar
360 return [len, Var rep `mkApps` [len, tag], Var up `App` len]
361 replicateShape (IdRepr _) _ _ = return []
362 replicateShape (VoidRepr {}) len _ = return [len]
363 replicateShape (EnumRepr {}) len _ = return [len]
365 emptyArrRepr :: Repr -> VM [CoreExpr]
366 emptyArrRepr (SumRepr { sum_components = prods })
367 = liftM concat $ mapM emptyArrRepr prods
368 emptyArrRepr (ProdRepr { prod_components = [] })
369 = return [Var unitDataConId]
370 emptyArrRepr (ProdRepr { prod_components = tys })
372 emptyArrRepr (IdRepr ty)
373 = liftM singleton $ emptyPA ty
374 emptyArrRepr (VoidRepr { void_tycon = tycon })
375 = liftM singleton $ emptyPA (mkTyConApp tycon [])
376 emptyArrRepr (EnumRepr { enum_tycon = tycon })
377 = liftM singleton $ emptyPA (mkTyConApp tycon [])
379 arrReprTys :: Repr -> VM [Type]
380 arrReprTys (SumRepr { sum_components = reprs })
381 = liftM concat $ mapM arrReprTys reprs
382 arrReprTys (ProdRepr { prod_components = [] })
384 arrReprTys (ProdRepr { prod_components = tys })
385 = mapM mkPArrayType tys
386 arrReprTys (IdRepr ty)
387 = liftM singleton $ mkPArrayType ty
388 arrReprTys (VoidRepr { void_tycon = tycon })
389 = liftM singleton $ mkPArrayType (mkTyConApp tycon [])
390 arrReprTys (EnumRepr {})
391 = liftM singleton $ mkPArrayType intPrimTy
393 arrReprTys' :: Repr -> VM [[Type]]
394 arrReprTys' (SumRepr { sum_components = reprs })
395 = mapM arrReprTys reprs
396 arrReprTys' repr = liftM singleton $ arrReprTys repr
398 arrReprVars :: Repr -> VM [[Var]]
400 = mapM (mapM (newLocalVar FSLIT("rs"))) =<< arrReprTys' repr
402 mkRepr :: TyCon -> VM Repr
404 | [tys] <- rep_tys = boxedProductRepr tys
405 | all null rep_tys = enumRepr
406 | otherwise = sumRepr =<< mapM unboxedProductRepr rep_tys
408 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
410 buildPReprType :: TyCon -> VM Type
411 buildPReprType = liftM reprType . mkRepr
413 buildToPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
414 buildToPRepr repr vect_tc prepr_tc _
416 arg <- newLocalVar FSLIT("x") arg_ty
417 result <- to_repr repr (Var arg)
420 . wrapFamInstBody prepr_tc var_tys
423 var_tys = mkTyVarTys $ tyConTyVars vect_tc
424 arg_ty = mkTyConApp vect_tc var_tys
425 res_ty = reprType repr
427 cons = tyConDataCons vect_tc
430 to_repr (SumRepr { sum_components = prods
431 , sum_tycon = tycon })
434 (vars, bodies) <- mapAndUnzipM to_unboxed prods
435 return . Case expr (mkWildId (exprType expr)) res_ty
436 $ zipWith4 mk_alt cons vars (tyConDataCons tycon) bodies
438 mk_alt con vars sum_con body
439 = (DataAlt con, vars, mkConApp sum_con (ty_args ++ [body]))
441 ty_args = map (Type . reprType) prods
443 to_repr (EnumRepr { enum_data_con = data_con }) expr
444 = return . Case expr (mkWildId (exprType expr)) res_ty
447 mk_alt con = (DataAlt con, [], mkConApp data_con [mkDataConTag con])
451 (vars, body) <- to_unboxed prod
452 return $ Case expr (mkWildId (exprType expr)) res_ty
453 [(DataAlt con, vars, body)]
455 to_unboxed (ProdRepr { prod_components = tys
456 , prod_data_con = data_con })
458 vars <- mapM (newLocalVar FSLIT("r")) tys
459 return (vars, mkConApp data_con (map Type tys ++ map Var vars))
461 to_unboxed (IdRepr ty)
463 var <- newLocalVar FSLIT("y") ty
464 return ([var], Var var)
466 to_unboxed (VoidRepr { void_bottom = bottom })
467 = return ([], bottom)
470 buildFromPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
471 buildFromPRepr repr vect_tc prepr_tc _
473 arg_ty <- mkPReprType res_ty
474 arg <- newLocalVar FSLIT("x") arg_ty
478 $ unwrapFamInstScrut prepr_tc var_tys (Var arg)
480 var_tys = mkTyVarTys $ tyConTyVars vect_tc
481 res_ty = mkTyConApp vect_tc var_tys
483 cons = map (`mkConApp` map Type var_tys) (tyConDataCons vect_tc)
486 from_repr repr@(SumRepr { sum_components = prods
487 , sum_tycon = tycon })
490 vars <- mapM (newLocalVar FSLIT("x")) (map reprType prods)
491 bodies <- sequence . zipWith3 from_unboxed prods cons
493 return . Case expr (mkWildId (reprType repr)) res_ty
494 $ zipWith3 sum_alt (tyConDataCons tycon) vars bodies
496 sum_alt data_con var body = (DataAlt data_con, [var], body)
498 from_repr repr@(EnumRepr { enum_data_con = data_con }) expr
500 var <- newLocalVar FSLIT("n") intPrimTy
502 let res = Case (Var var) (mkWildId intPrimTy) res_ty
503 $ (DEFAULT, [], error_expr)
504 : zipWith mk_alt (tyConDataCons vect_tc) cons
506 return $ Case expr (mkWildId (reprType repr)) res_ty
507 [(DataAlt data_con, [var], res)]
509 mk_alt data_con con = (LitAlt (mkDataConTagLit data_con), [], con)
511 error_expr = mkRuntimeErrorApp rUNTIME_ERROR_ID res_ty
513 $ sep [text "Invalid NDP representation of", ppr vect_tc]
515 from_repr repr expr = from_unboxed repr con expr
517 from_unboxed prod@(ProdRepr { prod_components = tys
518 , prod_data_con = data_con })
522 vars <- mapM (newLocalVar FSLIT("y")) tys
523 return $ Case expr (mkWildId (reprType prod)) res_ty
524 [(DataAlt data_con, vars, con `mkVarApps` vars)]
526 from_unboxed (IdRepr _) con expr
527 = return $ con `App` expr
529 from_unboxed (VoidRepr {}) con expr
532 buildToArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
533 buildToArrPRepr repr vect_tc prepr_tc arr_tc
535 arg_ty <- mkPArrayType el_ty
536 arg <- newLocalVar FSLIT("xs") arg_ty
538 res_ty <- mkPArrayType (reprType repr)
540 shape_vars <- arrShapeVars repr
541 repr_vars <- arrReprVars repr
543 parray_co <- mkBuiltinCo parrayTyCon
545 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
546 co = mkAppCoercion parray_co
548 $ mkTyConApp repr_co var_tys
550 scrut = unwrapFamInstScrut arr_tc var_tys (Var arg)
552 result <- to_repr shape_vars repr_vars repr
556 $ Case scrut (mkWildId (mkTyConApp arr_tc var_tys)) res_ty
557 [(DataAlt arr_dc, shape_vars ++ concat repr_vars, result)]
559 var_tys = mkTyVarTys $ tyConTyVars vect_tc
560 el_ty = mkTyConApp vect_tc var_tys
562 [arr_dc] = tyConDataCons arr_tc
564 to_repr shape_vars@(len_var : _)
566 (SumRepr { sum_components = prods
567 , sum_arr_tycon = tycon
568 , sum_arr_data_con = data_con })
570 exprs <- zipWithM to_prod repr_vars prods
572 return . wrapFamInstBody tycon tys
574 $ map Type tys ++ map Var shape_vars ++ exprs
576 tys = map reprType prods
580 (ProdRepr { prod_components = tys
581 , prod_arr_tycon = tycon
582 , prod_arr_data_con = data_con })
583 = return . wrapFamInstBody tycon tys
585 $ map Type tys ++ map Var (len_var : repr_vars)
589 (EnumRepr { enum_arr_tycon = tycon
590 , enum_arr_data_con = data_con })
591 = return . wrapFamInstBody tycon []
592 $ mkConApp data_con [Var len_var, Var repr_var]
594 to_prod repr_vars@(r : _)
595 (ProdRepr { prod_components = tys@(ty : _)
596 , prod_arr_tycon = tycon
597 , prod_arr_data_con = data_con })
599 len <- lengthPA ty (Var r)
600 return . wrapFamInstBody tycon tys
602 $ map Type tys ++ len : map Var repr_vars
604 to_prod [var] (IdRepr ty) = return (Var var)
605 to_prod [var] (VoidRepr {}) = return (Var var)
608 buildFromArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
609 buildFromArrPRepr repr vect_tc prepr_tc arr_tc
611 arg_ty <- mkPArrayType =<< mkPReprType el_ty
612 arg <- newLocalVar FSLIT("xs") arg_ty
614 res_ty <- mkPArrayType el_ty
616 shape_vars <- arrShapeVars repr
617 repr_vars <- arrReprVars repr
619 parray_co <- mkBuiltinCo parrayTyCon
621 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
622 co = mkAppCoercion parray_co
623 $ mkTyConApp repr_co var_tys
625 scrut = mkCoerce co (Var arg)
627 result = wrapFamInstBody arr_tc var_tys
629 $ map Type var_tys ++ map Var (shape_vars ++ concat repr_vars)
632 (from_repr repr scrut shape_vars repr_vars res_ty result)
634 var_tys = mkTyVarTys $ tyConTyVars vect_tc
635 el_ty = mkTyConApp vect_tc var_tys
637 [arr_dc] = tyConDataCons arr_tc
639 from_repr (SumRepr { sum_components = prods
640 , sum_arr_tycon = tycon
641 , sum_arr_data_con = data_con })
648 vars <- mapM (newLocalVar FSLIT("xs")) =<< mapM arrReprType prods
649 result <- go prods repr_vars vars body
651 let scrut = unwrapFamInstScrut tycon ty_args expr
652 return . Case scrut (mkWildId scrut_ty) res_ty
653 $ [(DataAlt data_con, shape_vars ++ vars, result)]
655 ty_args = map reprType prods
656 scrut_ty = mkTyConApp tycon ty_args
658 go [] [] [] body = return body
659 go (prod : prods) (repr_vars : rss) (var : vars) body
661 shape_vars <- mapM (newLocalVar FSLIT("s")) =<< arrShapeTys prod
663 from_prod prod (Var var) shape_vars repr_vars res_ty
664 =<< go prods rss vars body
666 from_repr repr expr shape_vars [repr_vars] res_ty body
667 = from_prod repr expr shape_vars repr_vars res_ty body
669 from_prod prod@(ProdRepr { prod_components = tys
670 , prod_arr_tycon = tycon
671 , prod_arr_data_con = data_con })
678 let scrut = unwrapFamInstScrut tycon tys expr
679 scrut_ty = mkTyConApp tycon tys
680 ty <- arrReprType prod
682 return $ Case scrut (mkWildId scrut_ty) res_ty
683 [(DataAlt data_con, shape_vars ++ repr_vars, body)]
685 from_prod (EnumRepr { enum_arr_tycon = tycon
686 , enum_arr_data_con = data_con })
692 = let scrut = unwrapFamInstScrut tycon [] expr
693 scrut_ty = mkTyConApp tycon []
695 return $ Case scrut (mkWildId scrut_ty) res_ty
696 [(DataAlt data_con, [len_var, repr_var], body)]
698 from_prod (IdRepr ty)
704 = return $ Let (NonRec repr_var expr) body
706 from_prod (VoidRepr {})
712 = return $ Let (NonRec repr_var expr) body
714 buildPRDictRepr :: Repr -> VM CoreExpr
715 buildPRDictRepr (VoidRepr { void_tycon = tycon })
716 = prDFunOfTyCon tycon
717 buildPRDictRepr (IdRepr ty) = mkPR ty
718 buildPRDictRepr (ProdRepr {
719 prod_components = tys
724 dfun <- prDFunOfTyCon tycon
725 return $ dfun `mkTyApps` tys `mkApps` prs
727 buildPRDictRepr (SumRepr {
728 sum_components = prods
729 , sum_tycon = tycon })
731 prs <- mapM buildPRDictRepr prods
732 dfun <- prDFunOfTyCon tycon
733 return $ dfun `mkTyApps` map reprType prods `mkApps` prs
735 buildPRDictRepr (EnumRepr { enum_tycon = tycon })
736 = prDFunOfTyCon tycon
738 buildPRDict :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
739 buildPRDict repr vect_tc prepr_tc _
741 dict <- buildPRDictRepr repr
743 pr_co <- mkBuiltinCo prTyCon
744 let co = mkAppCoercion pr_co
746 $ mkTyConApp arg_co var_tys
748 return $ mkCoerce co dict
750 var_tys = mkTyVarTys $ tyConTyVars vect_tc
752 Just arg_co = tyConFamilyCoercion_maybe prepr_tc
754 buildPArrayTyCon :: TyCon -> TyCon -> VM TyCon
755 buildPArrayTyCon orig_tc vect_tc = fixV $ \repr_tc ->
757 name' <- cloneName mkPArrayTyConOcc orig_name
758 rhs <- buildPArrayTyConRhs orig_name vect_tc repr_tc
759 parray <- builtin parrayTyCon
761 liftDs $ buildAlgTyCon name'
763 [] -- no stupid theta
765 rec_flag -- FIXME: is this ok?
766 False -- FIXME: no generics
767 False -- not GADT syntax
768 (Just $ mk_fam_inst parray vect_tc)
770 orig_name = tyConName orig_tc
771 tyvars = tyConTyVars vect_tc
772 rec_flag = boolToRecFlag (isRecursiveTyCon vect_tc)
775 buildPArrayTyConRhs :: Name -> TyCon -> TyCon -> VM AlgTyConRhs
776 buildPArrayTyConRhs orig_name vect_tc repr_tc
778 data_con <- buildPArrayDataCon orig_name vect_tc repr_tc
779 return $ DataTyCon { data_cons = [data_con], is_enum = False }
781 buildPArrayDataCon :: Name -> TyCon -> TyCon -> VM DataCon
782 buildPArrayDataCon orig_name vect_tc repr_tc
784 dc_name <- cloneName mkPArrayDataConOcc orig_name
785 repr <- mkRepr vect_tc
787 shape_tys <- arrShapeTys repr
788 repr_tys <- arrReprTys repr
790 let tys = shape_tys ++ repr_tys
792 liftDs $ buildDataCon dc_name
794 (map (const NotMarkedStrict) tys)
795 [] -- no field labels
796 (tyConTyVars vect_tc)
797 [] -- no existentials
803 mkPADFun :: TyCon -> VM Var
805 = newExportedVar (mkPADFunOcc $ getOccName vect_tc) =<< paDFunType vect_tc
807 buildTyConBindings :: TyCon -> TyCon -> TyCon -> TyCon -> Var
808 -> VM [(Var, CoreExpr)]
809 buildTyConBindings orig_tc vect_tc prepr_tc arr_tc dfun
811 repr <- mkRepr vect_tc
812 vectDataConWorkers repr orig_tc vect_tc arr_tc
813 dict <- buildPADict repr vect_tc prepr_tc arr_tc dfun
815 return $ (dfun, dict) : binds
817 orig_dcs = tyConDataCons orig_tc
818 vect_dcs = tyConDataCons vect_tc
819 [arr_dc] = tyConDataCons arr_tc
821 repr_tys = map dataConRepArgTys vect_dcs
823 vectDataConWorkers :: Repr -> TyCon -> TyCon -> TyCon
825 vectDataConWorkers repr orig_tc vect_tc arr_tc
828 . zipWith3 def_worker (tyConDataCons orig_tc) rep_tys
829 $ zipWith4 mk_data_con (tyConDataCons vect_tc)
833 mapM_ (uncurry hoistBinding) bs
835 tyvars = tyConTyVars vect_tc
836 var_tys = mkTyVarTys tyvars
837 ty_args = map Type var_tys
839 res_ty = mkTyConApp vect_tc var_tys
841 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
842 reprs = splitSumRepr repr
844 [arr_dc] = tyConDataCons arr_tc
846 mk_data_con con tys pre post
847 = liftM2 (,) (vect_data_con con)
848 (lift_data_con tys pre post (mkDataConTag con))
850 vect_data_con con = return $ mkConApp con ty_args
851 lift_data_con tys pre_reprs post_reprs tag
853 len <- builtin liftingContext
854 args <- mapM (newLocalVar FSLIT("xs"))
855 =<< mapM mkPArrayType tys
857 shape <- replicateShape repr (Var len) tag
858 repr <- mk_arr_repr (Var len) (map Var args)
860 pre <- liftM concat $ mapM emptyArrRepr pre_reprs
861 post <- liftM concat $ mapM emptyArrRepr post_reprs
863 return . mkLams (len : args)
864 . wrapFamInstBody arr_tc var_tys
866 $ ty_args ++ shape ++ pre ++ repr ++ post
870 units <- replicatePA len (Var unitDataConId)
873 mk_arr_repr len arrs = return arrs
875 def_worker data_con arg_tys mk_body
879 . polyAbstract tyvars $ \abstract ->
880 liftM (abstract . vectorised)
881 $ buildClosures tyvars [] arg_tys res_ty mk_body
883 vect_worker <- cloneId mkVectOcc orig_worker (exprType body)
884 defGlobalVar orig_worker vect_worker
885 return (vect_worker, body)
887 orig_worker = dataConWorkId data_con
889 buildPADict :: Repr -> TyCon -> TyCon -> TyCon -> Var -> VM CoreExpr
890 buildPADict repr vect_tc prepr_tc arr_tc dfun
891 = polyAbstract tvs $ \abstract ->
893 meth_binds <- mapM (mk_method repr) paMethods
894 let meth_exprs = map (Var . fst) meth_binds
896 pa_dc <- builtin paDataCon
897 let dict = mkConApp pa_dc (Type (mkTyConApp vect_tc arg_tys) : meth_exprs)
898 body = Let (Rec meth_binds) dict
899 return . mkInlineMe $ abstract body
901 tvs = tyConTyVars arr_tc
902 arg_tys = mkTyVarTys tvs
904 mk_method repr (name, build)
907 body <- build repr vect_tc prepr_tc arr_tc
908 var <- newLocalVar name (exprType body)
909 return (var, mkInlineMe body)
911 paMethods = [(FSLIT("toPRepr"), buildToPRepr),
912 (FSLIT("fromPRepr"), buildFromPRepr),
913 (FSLIT("toArrPRepr"), buildToArrPRepr),
914 (FSLIT("fromArrPRepr"), buildFromArrPRepr),
915 (FSLIT("dictPRepr"), buildPRDict)]
917 -- | Split the given tycons into two sets depending on whether they have to be
918 -- converted (first list) or not (second list). The first argument contains
919 -- information about the conversion status of external tycons:
921 -- * tycons which have converted versions are mapped to True
922 -- * tycons which are not changed by vectorisation are mapped to False
923 -- * tycons which can't be converted are not elements of the map
925 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
926 classifyTyCons = classify [] []
928 classify conv keep cs [] = (conv, keep)
929 classify conv keep cs ((tcs, ds) : rs)
930 | can_convert && must_convert
931 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
933 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
935 = classify conv keep cs rs
937 refs = ds `delListFromUniqSet` tcs
939 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
940 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
942 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
944 -- | Compute mutually recursive groups of tycons in topological order
946 tyConGroups :: [TyCon] -> [TyConGroup]
947 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
949 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
950 , let ds = tyConsOfTyCon tc]
952 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
953 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
955 (tcs, dss) = unzip els
957 tyConsOfTyCon :: TyCon -> UniqSet TyCon
959 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
961 tyConsOfType :: Type -> UniqSet TyCon
963 | Just ty' <- coreView ty = tyConsOfType ty'
964 tyConsOfType (TyVarTy v) = emptyUniqSet
965 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
967 extend | isUnLiftedTyCon tc
968 || isTupleTyCon tc = id
970 | otherwise = (`addOneToUniqSet` tc)
972 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
973 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
974 `addOneToUniqSet` funTyCon
975 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
976 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
978 tyConsOfTypes :: [Type] -> UniqSet TyCon
979 tyConsOfTypes = unionManyUniqSets . map tyConsOfType