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 mkRepr, arrShapeTys, arrShapeVars, arrSelector,
10 PAInstance, buildPADict,
14 #include "HsVersions.h"
20 import HscTypes ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
29 import FamInstEnv ( FamInst, mkLocalFamInst )
30 import InstEnv ( Instance, mkLocalInstance, instanceDFunId )
33 import BasicTypes ( StrictnessMark(..), OverlapFlag(..), boolToRecFlag )
35 import Id ( mkWildId )
36 import Name ( Name, getOccName )
39 import TysPrim ( intPrimTy )
44 import Util ( singleton )
45 import Digraph ( SCC(..), stronglyConnComp )
49 import Control.Monad ( liftM, liftM2, zipWithM, zipWithM_, mapAndUnzipM )
50 import Data.List ( inits, tails, zipWith4, zipWith5 )
52 -- ----------------------------------------------------------------------------
55 vectTyCon :: TyCon -> VM TyCon
57 | isFunTyCon tc = builtin closureTyCon
58 | isBoxedTupleTyCon tc = return tc
59 | isUnLiftedTyCon tc = return tc
63 Just tc' -> return tc'
65 -- FIXME: just for now
66 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
68 vectType :: Type -> VM Type
69 vectType ty | Just ty' <- coreView ty = vectType ty'
70 vectType (TyVarTy tv) = return $ TyVarTy tv
71 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
72 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
73 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
74 (mapM vectType [ty1,ty2])
75 vectType ty@(ForAllTy _ _)
77 mdicts <- mapM paDictArgType tyvars
78 mono_ty' <- vectType mono_ty
79 return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
81 (tyvars, mono_ty) = splitForAllTys ty
83 vectType ty = pprPanic "vectType:" (ppr ty)
85 -- ----------------------------------------------------------------------------
88 type TyConGroup = ([TyCon], UniqSet TyCon)
90 data PAInstance = PAInstance {
92 , painstOrigTyCon :: TyCon
93 , painstVectTyCon :: TyCon
94 , painstArrTyCon :: TyCon
97 vectTypeEnv :: TypeEnv -> VM (TypeEnv, [FamInst], [(Var, CoreExpr)])
100 cs <- readGEnv $ mk_map . global_tycons
101 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
102 keep_dcs = concatMap tyConDataCons keep_tcs
103 zipWithM_ defTyCon keep_tcs keep_tcs
104 zipWithM_ defDataCon keep_dcs keep_dcs
105 new_tcs <- vectTyConDecls conv_tcs
107 let orig_tcs = keep_tcs ++ conv_tcs
108 vect_tcs = keep_tcs ++ new_tcs
110 repr_tcs <- zipWithM buildPReprTyCon orig_tcs vect_tcs
111 parr_tcs <- zipWithM buildPArrayTyCon orig_tcs vect_tcs
112 dfuns <- mapM mkPADFun vect_tcs
113 defTyConPAs (zip vect_tcs dfuns)
114 binds <- sequence (zipWith5 buildTyConBindings orig_tcs
120 let all_new_tcs = new_tcs ++ repr_tcs ++ parr_tcs
122 let new_env = extendTypeEnvList env
123 (map ATyCon all_new_tcs
124 ++ [ADataCon dc | tc <- all_new_tcs
125 , dc <- tyConDataCons tc])
127 return (new_env, map mkLocalFamInst (repr_tcs ++ parr_tcs), concat binds)
129 tycons = typeEnvTyCons env
130 groups = tyConGroups tycons
132 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
134 keep_tc tc = let dcs = tyConDataCons tc
136 defTyCon tc tc >> zipWithM_ defDataCon dcs dcs
139 vectTyConDecls :: [TyCon] -> VM [TyCon]
140 vectTyConDecls tcs = fixV $ \tcs' ->
142 mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
143 mapM vectTyConDecl tcs
146 lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
148 vectTyConDecl :: TyCon -> VM TyCon
151 name' <- cloneName mkVectTyConOcc name
152 rhs' <- vectAlgTyConRhs (algTyConRhs tc)
154 liftDs $ buildAlgTyCon name'
156 [] -- no stupid theta
158 rec_flag -- FIXME: is this ok?
159 False -- FIXME: no generics
160 False -- not GADT syntax
161 Nothing -- not a family instance
164 tyvars = tyConTyVars tc
165 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
167 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
168 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
172 data_cons' <- mapM vectDataCon data_cons
173 zipWithM_ defDataCon data_cons data_cons'
174 return $ DataTyCon { data_cons = data_cons'
178 vectDataCon :: DataCon -> VM DataCon
180 | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
181 | not . null $ dataConEqSpec dc = pprPanic "vectDataCon: eq spec" (ppr dc)
184 name' <- cloneName mkVectDataConOcc name
185 tycon' <- vectTyCon tycon
186 arg_tys <- mapM vectType rep_arg_tys
188 liftDs $ buildDataCon name'
190 (map (const NotMarkedStrict) arg_tys)
191 [] -- no labelled fields
193 [] -- no existential tvs for now
194 [] -- no eq spec for now
199 name = dataConName dc
200 univ_tvs = dataConUnivTyVars dc
201 rep_arg_tys = dataConRepArgTys dc
202 tycon = dataConTyCon dc
204 mk_fam_inst :: TyCon -> TyCon -> (TyCon, [Type])
205 mk_fam_inst fam_tc arg_tc
206 = (fam_tc, [mkTyConApp arg_tc . mkTyVarTys $ tyConTyVars arg_tc])
208 buildPReprTyCon :: TyCon -> TyCon -> VM TyCon
209 buildPReprTyCon orig_tc vect_tc
211 name <- cloneName mkPReprTyConOcc (tyConName orig_tc)
212 rhs_ty <- buildPReprType vect_tc
213 prepr_tc <- builtin preprTyCon
214 liftDs $ buildSynTyCon name
216 (SynonymTyCon rhs_ty)
217 (Just $ mk_fam_inst prepr_tc vect_tc)
219 tyvars = tyConTyVars vect_tc
222 data Repr = ProdRepr {
223 prod_components :: [Type]
224 , prod_tycon :: TyCon
225 , prod_data_con :: DataCon
226 , prod_arr_tycon :: TyCon
227 , prod_arr_data_con :: DataCon
231 sum_components :: [Repr]
233 , sum_arr_tycon :: TyCon
234 , sum_arr_data_con :: DataCon
241 , void_bottom :: CoreExpr
246 , enum_data_con :: DataCon
247 , enum_arr_tycon :: TyCon
248 , enum_arr_data_con :: DataCon
254 tycon <- builtin voidTyCon
255 var <- builtin voidVar
258 , void_bottom = Var var
264 (arr_tycon, _) <- parrayReprTyCon intTy
265 let [arr_data_con] = tyConDataCons arr_tycon
269 , enum_data_con = data_con
270 , enum_arr_tycon = arr_tycon
271 , enum_arr_data_con = arr_data_con
275 data_con = intDataCon
277 unboxedProductRepr :: [Type] -> VM Repr
278 unboxedProductRepr [] = voidRepr
279 unboxedProductRepr [ty] = return $ IdRepr ty
280 unboxedProductRepr tys = boxedProductRepr tys
282 boxedProductRepr :: [Type] -> VM Repr
285 tycon <- builtin (prodTyCon arity)
286 let [data_con] = tyConDataCons tycon
288 (arr_tycon, _) <- parrayReprTyCon $ mkTyConApp tycon tys
289 let [arr_data_con] = tyConDataCons arr_tycon
292 prod_components = tys
294 , prod_data_con = data_con
295 , prod_arr_tycon = arr_tycon
296 , prod_arr_data_con = arr_data_con
301 sumRepr :: [Repr] -> VM Repr
302 sumRepr [] = voidRepr
303 sumRepr [repr] = boxRepr repr
306 tycon <- builtin (sumTyCon arity)
307 (arr_tycon, _) <- parrayReprTyCon
311 let [arr_data_con] = tyConDataCons arr_tycon
314 sum_components = reprs
316 , sum_arr_tycon = arr_tycon
317 , sum_arr_data_con = arr_data_con
322 splitSumRepr :: Repr -> [Repr]
323 splitSumRepr (SumRepr { sum_components = reprs }) = reprs
324 splitSumRepr repr = [repr]
326 boxRepr :: Repr -> VM Repr
327 boxRepr (VoidRepr {}) = boxedProductRepr []
328 boxRepr (IdRepr ty) = boxedProductRepr [ty]
329 boxRepr repr = return repr
331 reprType :: Repr -> Type
332 reprType (ProdRepr { prod_tycon = tycon, prod_components = tys })
333 = mkTyConApp tycon tys
334 reprType (SumRepr { sum_tycon = tycon, sum_components = reprs })
335 = mkTyConApp tycon (map reprType reprs)
336 reprType (IdRepr ty) = ty
337 reprType (VoidRepr { void_tycon = tycon }) = mkTyConApp tycon []
338 reprType (EnumRepr { enum_tycon = tycon }) = mkTyConApp tycon []
340 arrReprType :: Repr -> VM Type
341 arrReprType = mkPArrayType . reprType
343 arrShapeTys :: Repr -> VM [Type]
344 arrShapeTys (SumRepr {})
346 int_arr <- builtin parrayIntPrimTyCon
347 return [intPrimTy, mkTyConApp int_arr [], mkTyConApp int_arr []]
348 arrShapeTys (ProdRepr {}) = return [intPrimTy]
349 arrShapeTys (IdRepr _) = return []
350 arrShapeTys (VoidRepr {}) = return [intPrimTy]
351 arrShapeTys (EnumRepr {}) = return [intPrimTy]
353 arrShapeVars :: Repr -> VM [Var]
354 arrShapeVars repr = mapM (newLocalVar FSLIT("sh")) =<< arrShapeTys repr
356 replicateShape :: Repr -> CoreExpr -> CoreExpr -> VM [CoreExpr]
357 replicateShape (ProdRepr {}) len _ = return [len]
358 replicateShape (SumRepr {}) len tag
360 rep <- builtin replicatePAIntPrimVar
361 up <- builtin upToPAIntPrimVar
362 return [len, Var rep `mkApps` [len, tag], Var up `App` len]
363 replicateShape (IdRepr _) _ _ = return []
364 replicateShape (VoidRepr {}) len _ = return [len]
365 replicateShape (EnumRepr {}) len _ = return [len]
367 arrSelector :: Repr -> [CoreExpr] -> VM (CoreExpr, CoreExpr, CoreExpr)
368 arrSelector (SumRepr {}) [len, sel, is] = return (len, sel, is)
370 emptyArrRepr :: Repr -> VM [CoreExpr]
371 emptyArrRepr (SumRepr { sum_components = prods })
372 = liftM concat $ mapM emptyArrRepr prods
373 emptyArrRepr (ProdRepr { prod_components = [] })
374 = return [Var unitDataConId]
375 emptyArrRepr (ProdRepr { prod_components = tys })
377 emptyArrRepr (IdRepr ty)
378 = liftM singleton $ emptyPA ty
379 emptyArrRepr (VoidRepr { void_tycon = tycon })
380 = liftM singleton $ emptyPA (mkTyConApp tycon [])
381 emptyArrRepr (EnumRepr { enum_tycon = tycon })
382 = liftM singleton $ emptyPA (mkTyConApp tycon [])
384 arrReprTys :: Repr -> VM [Type]
385 arrReprTys (SumRepr { sum_components = reprs })
386 = liftM concat $ mapM arrReprTys reprs
387 arrReprTys (ProdRepr { prod_components = [] })
389 arrReprTys (ProdRepr { prod_components = tys })
390 = mapM mkPArrayType tys
391 arrReprTys (IdRepr ty)
392 = liftM singleton $ mkPArrayType ty
393 arrReprTys (VoidRepr { void_tycon = tycon })
394 = liftM singleton $ mkPArrayType (mkTyConApp tycon [])
395 arrReprTys (EnumRepr {})
396 = liftM singleton $ mkPArrayType intPrimTy
398 arrReprTys' :: Repr -> VM [[Type]]
399 arrReprTys' (SumRepr { sum_components = reprs })
400 = mapM arrReprTys reprs
401 arrReprTys' repr = liftM singleton $ arrReprTys repr
403 arrReprVars :: Repr -> VM [[Var]]
405 = mapM (mapM (newLocalVar FSLIT("rs"))) =<< arrReprTys' repr
407 mkRepr :: TyCon -> VM Repr
409 | [tys] <- rep_tys = boxedProductRepr tys
410 | all null rep_tys = enumRepr
411 | otherwise = sumRepr =<< mapM unboxedProductRepr rep_tys
413 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
415 buildPReprType :: TyCon -> VM Type
416 buildPReprType = liftM reprType . mkRepr
418 buildToPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
419 buildToPRepr repr vect_tc prepr_tc _
421 arg <- newLocalVar FSLIT("x") arg_ty
422 result <- to_repr repr (Var arg)
425 . wrapFamInstBody prepr_tc var_tys
428 var_tys = mkTyVarTys $ tyConTyVars vect_tc
429 arg_ty = mkTyConApp vect_tc var_tys
430 res_ty = reprType repr
432 cons = tyConDataCons vect_tc
435 to_repr (SumRepr { sum_components = prods
436 , sum_tycon = tycon })
439 (vars, bodies) <- mapAndUnzipM to_unboxed prods
440 return . Case expr (mkWildId (exprType expr)) res_ty
441 $ zipWith4 mk_alt cons vars (tyConDataCons tycon) bodies
443 mk_alt con vars sum_con body
444 = (DataAlt con, vars, mkConApp sum_con (ty_args ++ [body]))
446 ty_args = map (Type . reprType) prods
448 to_repr (EnumRepr { enum_data_con = data_con }) expr
449 = return . Case expr (mkWildId (exprType expr)) res_ty
452 mk_alt con = (DataAlt con, [], mkConApp data_con [mkDataConTag con])
456 (vars, body) <- to_unboxed prod
457 return $ Case expr (mkWildId (exprType expr)) res_ty
458 [(DataAlt con, vars, body)]
460 to_unboxed (ProdRepr { prod_components = tys
461 , prod_data_con = data_con })
463 vars <- mapM (newLocalVar FSLIT("r")) tys
464 return (vars, mkConApp data_con (map Type tys ++ map Var vars))
466 to_unboxed (IdRepr ty)
468 var <- newLocalVar FSLIT("y") ty
469 return ([var], Var var)
471 to_unboxed (VoidRepr { void_bottom = bottom })
472 = return ([], bottom)
475 buildFromPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
476 buildFromPRepr repr vect_tc prepr_tc _
478 arg_ty <- mkPReprType res_ty
479 arg <- newLocalVar FSLIT("x") arg_ty
483 $ unwrapFamInstScrut prepr_tc var_tys (Var arg)
485 var_tys = mkTyVarTys $ tyConTyVars vect_tc
486 res_ty = mkTyConApp vect_tc var_tys
488 cons = map (`mkConApp` map Type var_tys) (tyConDataCons vect_tc)
491 from_repr repr@(SumRepr { sum_components = prods
492 , sum_tycon = tycon })
495 vars <- mapM (newLocalVar FSLIT("x")) (map reprType prods)
496 bodies <- sequence . zipWith3 from_unboxed prods cons
498 return . Case expr (mkWildId (reprType repr)) res_ty
499 $ zipWith3 sum_alt (tyConDataCons tycon) vars bodies
501 sum_alt data_con var body = (DataAlt data_con, [var], body)
503 from_repr repr@(EnumRepr { enum_data_con = data_con }) expr
505 var <- newLocalVar FSLIT("n") intPrimTy
507 let res = Case (Var var) (mkWildId intPrimTy) res_ty
508 $ (DEFAULT, [], error_expr)
509 : zipWith mk_alt (tyConDataCons vect_tc) cons
511 return $ Case expr (mkWildId (reprType repr)) res_ty
512 [(DataAlt data_con, [var], res)]
514 mk_alt data_con con = (LitAlt (mkDataConTagLit data_con), [], con)
516 error_expr = mkRuntimeErrorApp rUNTIME_ERROR_ID res_ty
518 $ sep [text "Invalid NDP representation of", ppr vect_tc]
520 from_repr repr expr = from_unboxed repr con expr
522 from_unboxed prod@(ProdRepr { prod_components = tys
523 , prod_data_con = data_con })
527 vars <- mapM (newLocalVar FSLIT("y")) tys
528 return $ Case expr (mkWildId (reprType prod)) res_ty
529 [(DataAlt data_con, vars, con `mkVarApps` vars)]
531 from_unboxed (IdRepr _) con expr
532 = return $ con `App` expr
534 from_unboxed (VoidRepr {}) con expr
537 buildToArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
538 buildToArrPRepr repr vect_tc prepr_tc arr_tc
540 arg_ty <- mkPArrayType el_ty
541 arg <- newLocalVar FSLIT("xs") arg_ty
543 res_ty <- mkPArrayType (reprType repr)
545 shape_vars <- arrShapeVars repr
546 repr_vars <- arrReprVars repr
548 parray_co <- mkBuiltinCo parrayTyCon
550 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
551 co = mkAppCoercion parray_co
553 $ mkTyConApp repr_co var_tys
555 scrut = unwrapFamInstScrut arr_tc var_tys (Var arg)
557 result <- to_repr shape_vars repr_vars repr
561 $ Case scrut (mkWildId (mkTyConApp arr_tc var_tys)) res_ty
562 [(DataAlt arr_dc, shape_vars ++ concat repr_vars, result)]
564 var_tys = mkTyVarTys $ tyConTyVars vect_tc
565 el_ty = mkTyConApp vect_tc var_tys
567 [arr_dc] = tyConDataCons arr_tc
569 to_repr shape_vars@(len_var : _)
571 (SumRepr { sum_components = prods
572 , sum_arr_tycon = tycon
573 , sum_arr_data_con = data_con })
575 exprs <- zipWithM to_prod repr_vars prods
577 return . wrapFamInstBody tycon tys
579 $ map Type tys ++ map Var shape_vars ++ exprs
581 tys = map reprType prods
585 (ProdRepr { prod_components = tys
586 , prod_arr_tycon = tycon
587 , prod_arr_data_con = data_con })
588 = return . wrapFamInstBody tycon tys
590 $ map Type tys ++ map Var (len_var : repr_vars)
594 (EnumRepr { enum_arr_tycon = tycon
595 , enum_arr_data_con = data_con })
596 = return . wrapFamInstBody tycon []
597 $ mkConApp data_con [Var len_var, Var repr_var]
599 to_prod repr_vars@(r : _)
600 (ProdRepr { prod_components = tys@(ty : _)
601 , prod_arr_tycon = tycon
602 , prod_arr_data_con = data_con })
604 len <- lengthPA ty (Var r)
605 return . wrapFamInstBody tycon tys
607 $ map Type tys ++ len : map Var repr_vars
609 to_prod [var] (IdRepr ty) = return (Var var)
610 to_prod [var] (VoidRepr {}) = return (Var var)
613 buildFromArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
614 buildFromArrPRepr repr vect_tc prepr_tc arr_tc
616 arg_ty <- mkPArrayType =<< mkPReprType el_ty
617 arg <- newLocalVar FSLIT("xs") arg_ty
619 res_ty <- mkPArrayType el_ty
621 shape_vars <- arrShapeVars repr
622 repr_vars <- arrReprVars repr
624 parray_co <- mkBuiltinCo parrayTyCon
626 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
627 co = mkAppCoercion parray_co
628 $ mkTyConApp repr_co var_tys
630 scrut = mkCoerce co (Var arg)
632 result = wrapFamInstBody arr_tc var_tys
634 $ map Type var_tys ++ map Var (shape_vars ++ concat repr_vars)
637 (from_repr repr scrut shape_vars repr_vars res_ty result)
639 var_tys = mkTyVarTys $ tyConTyVars vect_tc
640 el_ty = mkTyConApp vect_tc var_tys
642 [arr_dc] = tyConDataCons arr_tc
644 from_repr (SumRepr { sum_components = prods
645 , sum_arr_tycon = tycon
646 , sum_arr_data_con = data_con })
653 vars <- mapM (newLocalVar FSLIT("xs")) =<< mapM arrReprType prods
654 result <- go prods repr_vars vars body
656 let scrut = unwrapFamInstScrut tycon ty_args expr
657 return . Case scrut (mkWildId scrut_ty) res_ty
658 $ [(DataAlt data_con, shape_vars ++ vars, result)]
660 ty_args = map reprType prods
661 scrut_ty = mkTyConApp tycon ty_args
663 go [] [] [] body = return body
664 go (prod : prods) (repr_vars : rss) (var : vars) body
666 shape_vars <- mapM (newLocalVar FSLIT("s")) =<< arrShapeTys prod
668 from_prod prod (Var var) shape_vars repr_vars res_ty
669 =<< go prods rss vars body
671 from_repr repr expr shape_vars [repr_vars] res_ty body
672 = from_prod repr expr shape_vars repr_vars res_ty body
674 from_prod prod@(ProdRepr { prod_components = tys
675 , prod_arr_tycon = tycon
676 , prod_arr_data_con = data_con })
683 let scrut = unwrapFamInstScrut tycon tys expr
684 scrut_ty = mkTyConApp tycon tys
685 ty <- arrReprType prod
687 return $ Case scrut (mkWildId scrut_ty) res_ty
688 [(DataAlt data_con, shape_vars ++ repr_vars, body)]
690 from_prod (EnumRepr { enum_arr_tycon = tycon
691 , enum_arr_data_con = data_con })
697 = let scrut = unwrapFamInstScrut tycon [] expr
698 scrut_ty = mkTyConApp tycon []
700 return $ Case scrut (mkWildId scrut_ty) res_ty
701 [(DataAlt data_con, [len_var, repr_var], body)]
703 from_prod (IdRepr ty)
709 = return $ Let (NonRec repr_var expr) body
711 from_prod (VoidRepr {})
717 = return $ Let (NonRec repr_var expr) body
719 buildPRDictRepr :: Repr -> VM CoreExpr
720 buildPRDictRepr (VoidRepr { void_tycon = tycon })
721 = prDFunOfTyCon tycon
722 buildPRDictRepr (IdRepr ty) = mkPR ty
723 buildPRDictRepr (ProdRepr {
724 prod_components = tys
729 dfun <- prDFunOfTyCon tycon
730 return $ dfun `mkTyApps` tys `mkApps` prs
732 buildPRDictRepr (SumRepr {
733 sum_components = prods
734 , sum_tycon = tycon })
736 prs <- mapM buildPRDictRepr prods
737 dfun <- prDFunOfTyCon tycon
738 return $ dfun `mkTyApps` map reprType prods `mkApps` prs
740 buildPRDictRepr (EnumRepr { enum_tycon = tycon })
741 = prDFunOfTyCon tycon
743 buildPRDict :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
744 buildPRDict repr vect_tc prepr_tc _
746 dict <- buildPRDictRepr repr
748 pr_co <- mkBuiltinCo prTyCon
749 let co = mkAppCoercion pr_co
751 $ mkTyConApp arg_co var_tys
753 return $ mkCoerce co dict
755 var_tys = mkTyVarTys $ tyConTyVars vect_tc
757 Just arg_co = tyConFamilyCoercion_maybe prepr_tc
759 buildPArrayTyCon :: TyCon -> TyCon -> VM TyCon
760 buildPArrayTyCon orig_tc vect_tc = fixV $ \repr_tc ->
762 name' <- cloneName mkPArrayTyConOcc orig_name
763 rhs <- buildPArrayTyConRhs orig_name vect_tc repr_tc
764 parray <- builtin parrayTyCon
766 liftDs $ buildAlgTyCon name'
768 [] -- no stupid theta
770 rec_flag -- FIXME: is this ok?
771 False -- FIXME: no generics
772 False -- not GADT syntax
773 (Just $ mk_fam_inst parray vect_tc)
775 orig_name = tyConName orig_tc
776 tyvars = tyConTyVars vect_tc
777 rec_flag = boolToRecFlag (isRecursiveTyCon vect_tc)
780 buildPArrayTyConRhs :: Name -> TyCon -> TyCon -> VM AlgTyConRhs
781 buildPArrayTyConRhs orig_name vect_tc repr_tc
783 data_con <- buildPArrayDataCon orig_name vect_tc repr_tc
784 return $ DataTyCon { data_cons = [data_con], is_enum = False }
786 buildPArrayDataCon :: Name -> TyCon -> TyCon -> VM DataCon
787 buildPArrayDataCon orig_name vect_tc repr_tc
789 dc_name <- cloneName mkPArrayDataConOcc orig_name
790 repr <- mkRepr vect_tc
792 shape_tys <- arrShapeTys repr
793 repr_tys <- arrReprTys repr
795 let tys = shape_tys ++ repr_tys
797 liftDs $ buildDataCon dc_name
799 (map (const NotMarkedStrict) tys)
800 [] -- no field labels
801 (tyConTyVars vect_tc)
802 [] -- no existentials
808 mkPADFun :: TyCon -> VM Var
810 = newExportedVar (mkPADFunOcc $ getOccName vect_tc) =<< paDFunType vect_tc
812 buildTyConBindings :: TyCon -> TyCon -> TyCon -> TyCon -> Var
813 -> VM [(Var, CoreExpr)]
814 buildTyConBindings orig_tc vect_tc prepr_tc arr_tc dfun
816 repr <- mkRepr vect_tc
817 vectDataConWorkers repr orig_tc vect_tc arr_tc
818 dict <- buildPADict repr vect_tc prepr_tc arr_tc dfun
820 return $ (dfun, dict) : binds
822 orig_dcs = tyConDataCons orig_tc
823 vect_dcs = tyConDataCons vect_tc
824 [arr_dc] = tyConDataCons arr_tc
826 repr_tys = map dataConRepArgTys vect_dcs
828 vectDataConWorkers :: Repr -> TyCon -> TyCon -> TyCon
830 vectDataConWorkers repr orig_tc vect_tc arr_tc
833 . zipWith3 def_worker (tyConDataCons orig_tc) rep_tys
834 $ zipWith4 mk_data_con (tyConDataCons vect_tc)
838 mapM_ (uncurry hoistBinding) bs
840 tyvars = tyConTyVars vect_tc
841 var_tys = mkTyVarTys tyvars
842 ty_args = map Type var_tys
844 res_ty = mkTyConApp vect_tc var_tys
846 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
847 reprs = splitSumRepr repr
849 [arr_dc] = tyConDataCons arr_tc
851 mk_data_con con tys pre post
852 = liftM2 (,) (vect_data_con con)
853 (lift_data_con tys pre post (mkDataConTag con))
855 vect_data_con con = return $ mkConApp con ty_args
856 lift_data_con tys pre_reprs post_reprs tag
858 len <- builtin liftingContext
859 args <- mapM (newLocalVar FSLIT("xs"))
860 =<< mapM mkPArrayType tys
862 shape <- replicateShape repr (Var len) tag
863 repr <- mk_arr_repr (Var len) (map Var args)
865 pre <- liftM concat $ mapM emptyArrRepr pre_reprs
866 post <- liftM concat $ mapM emptyArrRepr post_reprs
868 return . mkLams (len : args)
869 . wrapFamInstBody arr_tc var_tys
871 $ ty_args ++ shape ++ pre ++ repr ++ post
875 units <- replicatePA len (Var unitDataConId)
878 mk_arr_repr len arrs = return arrs
880 def_worker data_con arg_tys mk_body
884 . polyAbstract tyvars $ \abstract ->
885 liftM (abstract . vectorised)
886 $ buildClosures tyvars [] arg_tys res_ty mk_body
888 vect_worker <- cloneId mkVectOcc orig_worker (exprType body)
889 defGlobalVar orig_worker vect_worker
890 return (vect_worker, body)
892 orig_worker = dataConWorkId data_con
894 buildPADict :: Repr -> TyCon -> TyCon -> TyCon -> Var -> VM CoreExpr
895 buildPADict repr vect_tc prepr_tc arr_tc dfun
896 = polyAbstract tvs $ \abstract ->
898 meth_binds <- mapM (mk_method repr) paMethods
899 let meth_exprs = map (Var . fst) meth_binds
901 pa_dc <- builtin paDataCon
902 let dict = mkConApp pa_dc (Type (mkTyConApp vect_tc arg_tys) : meth_exprs)
903 body = Let (Rec meth_binds) dict
904 return . mkInlineMe $ abstract body
906 tvs = tyConTyVars arr_tc
907 arg_tys = mkTyVarTys tvs
909 mk_method repr (name, build)
912 body <- build repr vect_tc prepr_tc arr_tc
913 var <- newLocalVar name (exprType body)
914 return (var, mkInlineMe body)
916 paMethods = [(FSLIT("toPRepr"), buildToPRepr),
917 (FSLIT("fromPRepr"), buildFromPRepr),
918 (FSLIT("toArrPRepr"), buildToArrPRepr),
919 (FSLIT("fromArrPRepr"), buildFromArrPRepr),
920 (FSLIT("dictPRepr"), buildPRDict)]
922 -- | Split the given tycons into two sets depending on whether they have to be
923 -- converted (first list) or not (second list). The first argument contains
924 -- information about the conversion status of external tycons:
926 -- * tycons which have converted versions are mapped to True
927 -- * tycons which are not changed by vectorisation are mapped to False
928 -- * tycons which can't be converted are not elements of the map
930 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
931 classifyTyCons = classify [] []
933 classify conv keep cs [] = (conv, keep)
934 classify conv keep cs ((tcs, ds) : rs)
935 | can_convert && must_convert
936 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
938 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
940 = classify conv keep cs rs
942 refs = ds `delListFromUniqSet` tcs
944 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
945 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
947 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
949 -- | Compute mutually recursive groups of tycons in topological order
951 tyConGroups :: [TyCon] -> [TyConGroup]
952 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
954 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
955 , let ds = tyConsOfTyCon tc]
957 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
958 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
960 (tcs, dss) = unzip els
962 tyConsOfTyCon :: TyCon -> UniqSet TyCon
964 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
966 tyConsOfType :: Type -> UniqSet TyCon
968 | Just ty' <- coreView ty = tyConsOfType ty'
969 tyConsOfType (TyVarTy v) = emptyUniqSet
970 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
972 extend | isUnLiftedTyCon tc
973 || isTupleTyCon tc = id
975 | otherwise = (`addOneToUniqSet` tc)
977 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
978 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
979 `addOneToUniqSet` funTyCon
980 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
981 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
983 tyConsOfTypes :: [Type] -> UniqSet TyCon
984 tyConsOfTypes = unionManyUniqSets . map tyConsOfType
987 -- ----------------------------------------------------------------------------
990 fromVect :: Type -> CoreExpr -> VM CoreExpr
991 fromVect ty expr | Just ty' <- coreView ty = fromVect ty' expr
992 fromVect (FunTy arg_ty res_ty) expr
994 arg <- newLocalVar FSLIT("x") arg_ty
995 varg <- toVect arg_ty (Var arg)
996 varg_ty <- vectType arg_ty
997 vres_ty <- vectType res_ty
998 apply <- builtin applyClosureVar
999 body <- fromVect res_ty
1000 $ Var apply `mkTyApps` [arg_ty, res_ty] `mkApps` [expr, Var arg]
1001 return $ Lam arg body
1003 = identityConv ty >> return expr
1005 toVect :: Type -> CoreExpr -> VM CoreExpr
1006 toVect ty expr = identityConv ty >> return expr
1008 identityConv :: Type -> VM ()
1009 identityConv ty | Just ty' <- coreView ty = identityConv ty'
1010 identityConv (TyConApp tycon tys)
1012 mapM_ identityConv tys
1013 identityConvTyCon tycon
1014 identityConv ty = noV
1016 identityConvTyCon :: TyCon -> VM ()
1017 identityConvTyCon tc
1018 | isBoxedTupleTyCon tc = return ()
1019 | isUnLiftedTyCon tc = return ()
1020 | otherwise = maybeV (lookupTyCon tc) >> return ()