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 )
50 import Control.Monad ( liftM, liftM2, zipWithM, zipWithM_, mapAndUnzipM )
51 import Data.List ( inits, tails, zipWith4, zipWith5 )
53 -- ----------------------------------------------------------------------------
56 vectTyCon :: TyCon -> VM TyCon
58 | isFunTyCon tc = builtin closureTyCon
59 | isBoxedTupleTyCon tc = return tc
60 | isUnLiftedTyCon tc = return tc
64 Just tc' -> return tc'
66 -- FIXME: just for now
67 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
69 vectType :: Type -> VM Type
70 vectType ty | Just ty' <- coreView ty = vectType ty'
71 vectType (TyVarTy tv) = return $ TyVarTy tv
72 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
73 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
74 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
75 (mapM vectAndBoxType [ty1,ty2])
76 vectType ty@(ForAllTy _ _)
78 mdicts <- mapM paDictArgType tyvars
79 mono_ty' <- vectType mono_ty
80 return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
82 (tyvars, mono_ty) = splitForAllTys ty
84 vectType ty = pprPanic "vectType:" (ppr ty)
86 vectAndBoxType :: Type -> VM Type
87 vectAndBoxType ty = vectType ty >>= boxType
89 -- ----------------------------------------------------------------------------
92 boxType :: Type -> VM Type
94 | Just (tycon, []) <- splitTyConApp_maybe ty
95 , isUnLiftedTyCon tycon
97 r <- lookupBoxedTyCon tycon
99 Just tycon' -> return $ mkTyConApp tycon' []
101 boxType ty = return ty
103 -- ----------------------------------------------------------------------------
106 type TyConGroup = ([TyCon], UniqSet TyCon)
108 data PAInstance = PAInstance {
110 , painstOrigTyCon :: TyCon
111 , painstVectTyCon :: TyCon
112 , painstArrTyCon :: TyCon
115 vectTypeEnv :: TypeEnv -> VM (TypeEnv, [FamInst], [(Var, CoreExpr)])
118 cs <- readGEnv $ mk_map . global_tycons
119 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
120 keep_dcs = concatMap tyConDataCons keep_tcs
121 zipWithM_ defTyCon keep_tcs keep_tcs
122 zipWithM_ defDataCon keep_dcs keep_dcs
123 new_tcs <- vectTyConDecls conv_tcs
125 let orig_tcs = keep_tcs ++ conv_tcs
126 vect_tcs = keep_tcs ++ new_tcs
128 repr_tcs <- zipWithM buildPReprTyCon orig_tcs vect_tcs
129 parr_tcs <- zipWithM buildPArrayTyCon orig_tcs vect_tcs
130 dfuns <- mapM mkPADFun vect_tcs
131 defTyConPAs (zip vect_tcs dfuns)
132 binds <- sequence (zipWith5 buildTyConBindings orig_tcs
138 let all_new_tcs = new_tcs ++ repr_tcs ++ parr_tcs
140 let new_env = extendTypeEnvList env
141 (map ATyCon all_new_tcs
142 ++ [ADataCon dc | tc <- all_new_tcs
143 , dc <- tyConDataCons tc])
145 return (new_env, map mkLocalFamInst (repr_tcs ++ parr_tcs), concat binds)
147 tycons = typeEnvTyCons env
148 groups = tyConGroups tycons
150 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
152 keep_tc tc = let dcs = tyConDataCons tc
154 defTyCon tc tc >> zipWithM_ defDataCon dcs dcs
157 vectTyConDecls :: [TyCon] -> VM [TyCon]
158 vectTyConDecls tcs = fixV $ \tcs' ->
160 mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
161 mapM vectTyConDecl tcs
164 lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
166 vectTyConDecl :: TyCon -> VM TyCon
169 name' <- cloneName mkVectTyConOcc name
170 rhs' <- vectAlgTyConRhs (algTyConRhs tc)
172 liftDs $ buildAlgTyCon name'
174 [] -- no stupid theta
176 rec_flag -- FIXME: is this ok?
177 False -- FIXME: no generics
178 False -- not GADT syntax
179 Nothing -- not a family instance
182 tyvars = tyConTyVars tc
183 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
185 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
186 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
190 data_cons' <- mapM vectDataCon data_cons
191 zipWithM_ defDataCon data_cons data_cons'
192 return $ DataTyCon { data_cons = data_cons'
196 vectDataCon :: DataCon -> VM DataCon
198 | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
199 | not . null $ dataConEqSpec dc = pprPanic "vectDataCon: eq spec" (ppr dc)
202 name' <- cloneName mkVectDataConOcc name
203 tycon' <- vectTyCon tycon
204 arg_tys <- mapM vectType rep_arg_tys
206 liftDs $ buildDataCon name'
208 (map (const NotMarkedStrict) arg_tys)
209 [] -- no labelled fields
211 [] -- no existential tvs for now
212 [] -- no eq spec for now
217 name = dataConName dc
218 univ_tvs = dataConUnivTyVars dc
219 rep_arg_tys = dataConRepArgTys dc
220 tycon = dataConTyCon dc
222 mk_fam_inst :: TyCon -> TyCon -> (TyCon, [Type])
223 mk_fam_inst fam_tc arg_tc
224 = (fam_tc, [mkTyConApp arg_tc . mkTyVarTys $ tyConTyVars arg_tc])
226 buildPReprTyCon :: TyCon -> TyCon -> VM TyCon
227 buildPReprTyCon orig_tc vect_tc
229 name <- cloneName mkPReprTyConOcc (tyConName orig_tc)
230 rhs_ty <- buildPReprType vect_tc
231 prepr_tc <- builtin preprTyCon
232 liftDs $ buildSynTyCon name
234 (SynonymTyCon rhs_ty)
235 (Just $ mk_fam_inst prepr_tc vect_tc)
237 tyvars = tyConTyVars vect_tc
240 data Repr = ProdRepr {
241 prod_components :: [Type]
242 , prod_tycon :: TyCon
243 , prod_data_con :: DataCon
244 , prod_arr_tycon :: TyCon
245 , prod_arr_data_con :: DataCon
249 sum_components :: [Repr]
251 , sum_arr_tycon :: TyCon
252 , sum_arr_data_con :: DataCon
259 , void_bottom :: CoreExpr
264 , enum_data_con :: DataCon
265 , enum_arr_tycon :: TyCon
266 , enum_arr_data_con :: DataCon
272 tycon <- builtin voidTyCon
273 var <- builtin voidVar
276 , void_bottom = Var var
282 tycon <- builtin enumerationTyCon
283 let [data_con] = tyConDataCons tycon
284 (arr_tycon, _) <- parrayReprTyCon (mkTyConApp tycon [])
285 let [arr_data_con] = tyConDataCons arr_tycon
289 , enum_data_con = data_con
290 , enum_arr_tycon = arr_tycon
291 , enum_arr_data_con = arr_data_con
294 unboxedProductRepr :: [Type] -> VM Repr
295 unboxedProductRepr [] = voidRepr
296 unboxedProductRepr [ty] = return $ IdRepr ty
297 unboxedProductRepr tys = boxedProductRepr tys
299 boxedProductRepr :: [Type] -> VM Repr
302 tycon <- builtin (prodTyCon arity)
303 let [data_con] = tyConDataCons tycon
305 tys' <- mapM boxType tys
306 (arr_tycon, _) <- parrayReprTyCon $ mkTyConApp tycon tys'
307 let [arr_data_con] = tyConDataCons arr_tycon
310 prod_components = tys
312 , prod_data_con = data_con
313 , prod_arr_tycon = arr_tycon
314 , prod_arr_data_con = arr_data_con
319 sumRepr :: [Repr] -> VM Repr
320 sumRepr [] = voidRepr
321 sumRepr [repr] = boxRepr repr
324 tycon <- builtin (sumTyCon arity)
325 (arr_tycon, _) <- parrayReprTyCon
329 let [arr_data_con] = tyConDataCons arr_tycon
332 sum_components = reprs
334 , sum_arr_tycon = arr_tycon
335 , sum_arr_data_con = arr_data_con
340 splitSumRepr :: Repr -> [Repr]
341 splitSumRepr (SumRepr { sum_components = reprs }) = reprs
342 splitSumRepr repr = [repr]
344 boxRepr :: Repr -> VM Repr
345 boxRepr (VoidRepr {}) = boxedProductRepr []
346 boxRepr (IdRepr ty) = boxedProductRepr [ty]
347 boxRepr repr = return repr
349 reprType :: Repr -> Type
350 reprType (ProdRepr { prod_tycon = tycon, prod_components = tys })
351 = mkTyConApp tycon tys
352 reprType (SumRepr { sum_tycon = tycon, sum_components = reprs })
353 = mkTyConApp tycon (map reprType reprs)
354 reprType (IdRepr ty) = ty
355 reprType (VoidRepr { void_tycon = tycon }) = mkTyConApp tycon []
356 reprType (EnumRepr { enum_tycon = tycon }) = mkTyConApp tycon []
358 arrReprType :: Repr -> VM Type
359 arrReprType = mkPArrayType . reprType
361 arrShapeTys :: Repr -> VM [Type]
362 arrShapeTys (SumRepr {}) = sumShapeTys
363 arrShapeTys (ProdRepr {}) = return [intPrimTy]
364 arrShapeTys (IdRepr _) = return []
365 arrShapeTys (VoidRepr {}) = return [intPrimTy]
366 arrShapeTys (EnumRepr {}) = sumShapeTys
368 sumShapeTys :: VM [Type]
370 int_arr <- builtin intPrimArrayTy
371 return [intPrimTy, int_arr, int_arr]
374 arrShapeVars :: Repr -> VM [Var]
375 arrShapeVars repr = mapM (newLocalVar FSLIT("sh")) =<< arrShapeTys repr
377 replicateShape :: Repr -> CoreExpr -> CoreExpr -> VM [CoreExpr]
378 replicateShape (ProdRepr {}) len _ = return [len]
379 replicateShape (SumRepr {}) len tag = replicateSumShape len tag
380 replicateShape (IdRepr _) _ _ = return []
381 replicateShape (VoidRepr {}) len _ = return [len]
382 replicateShape (EnumRepr {}) len tag = replicateSumShape len tag
384 replicateSumShape :: CoreExpr -> CoreExpr -> VM [CoreExpr]
385 replicateSumShape len tag
387 rep <- builtin replicatePAIntPrimVar
388 up <- builtin upToPAIntPrimVar
389 return [len, Var rep `mkApps` [len, tag], Var up `App` len]
391 arrSelector :: Repr -> [CoreExpr] -> VM (CoreExpr, CoreExpr, CoreExpr)
392 arrSelector (SumRepr {}) [len, sel, is] = return (len, sel, is)
393 arrSelector (EnumRepr {}) [len, sel, is] = return (len, sel, is)
395 emptyArrRepr :: Repr -> VM [CoreExpr]
396 emptyArrRepr (SumRepr { sum_components = prods })
397 = liftM concat $ mapM emptyArrRepr prods
398 emptyArrRepr (ProdRepr { prod_components = [] })
399 = return [Var unitDataConId]
400 emptyArrRepr (ProdRepr { prod_components = tys })
402 emptyArrRepr (IdRepr ty)
403 = liftM singleton $ emptyPA ty
404 emptyArrRepr (VoidRepr { void_tycon = tycon })
405 = liftM singleton $ emptyPA (mkTyConApp tycon [])
406 emptyArrRepr (EnumRepr { enum_tycon = tycon })
409 arrReprTys :: Repr -> VM [Type]
410 arrReprTys (SumRepr { sum_components = reprs })
411 = liftM concat $ mapM arrReprTys reprs
412 arrReprTys (ProdRepr { prod_components = [] })
414 arrReprTys (ProdRepr { prod_components = tys })
415 = mapM mkPArrayType tys
416 arrReprTys (IdRepr ty)
417 = liftM singleton $ mkPArrayType ty
418 arrReprTys (VoidRepr { void_tycon = tycon })
419 = liftM singleton $ mkPArrayType (mkTyConApp tycon [])
420 arrReprTys (EnumRepr {})
423 arrReprTys' :: Repr -> VM [[Type]]
424 arrReprTys' (SumRepr { sum_components = reprs })
425 = mapM arrReprTys reprs
426 arrReprTys' repr = liftM singleton $ arrReprTys repr
428 arrReprVars :: Repr -> VM [[Var]]
430 = mapM (mapM (newLocalVar FSLIT("rs"))) =<< arrReprTys' repr
432 mkRepr :: TyCon -> VM Repr
434 | [tys] <- rep_tys = boxedProductRepr tys
435 -- | all null rep_tys = enumRepr
436 | otherwise = sumRepr =<< mapM unboxedProductRepr rep_tys
438 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
440 buildPReprType :: TyCon -> VM Type
441 buildPReprType = liftM reprType . mkRepr
443 buildToPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
444 buildToPRepr repr vect_tc prepr_tc _
446 arg <- newLocalVar FSLIT("x") arg_ty
447 result <- to_repr repr (Var arg)
450 . wrapFamInstBody prepr_tc var_tys
453 var_tys = mkTyVarTys $ tyConTyVars vect_tc
454 arg_ty = mkTyConApp vect_tc var_tys
455 res_ty = reprType repr
457 cons = tyConDataCons vect_tc
460 to_repr (SumRepr { sum_components = prods
461 , sum_tycon = tycon })
464 (vars, bodies) <- mapAndUnzipM to_unboxed prods
465 return . Case expr (mkWildId (exprType expr)) res_ty
466 $ zipWith4 mk_alt cons vars (tyConDataCons tycon) bodies
468 mk_alt con vars sum_con body
469 = (DataAlt con, vars, mkConApp sum_con (ty_args ++ [body]))
471 ty_args = map (Type . reprType) prods
473 to_repr (EnumRepr { enum_data_con = data_con }) expr
474 = return . Case expr (mkWildId (exprType expr)) res_ty
477 mk_alt con = (DataAlt con, [], mkConApp data_con [mkDataConTag con])
481 (vars, body) <- to_unboxed prod
482 return $ Case expr (mkWildId (exprType expr)) res_ty
483 [(DataAlt con, vars, body)]
485 to_unboxed (ProdRepr { prod_components = tys
486 , prod_data_con = data_con })
488 vars <- mapM (newLocalVar FSLIT("r")) tys
489 return (vars, mkConApp data_con (map Type tys ++ map Var vars))
491 to_unboxed (IdRepr ty)
493 var <- newLocalVar FSLIT("y") ty
494 return ([var], Var var)
496 to_unboxed (VoidRepr { void_bottom = bottom })
497 = return ([], bottom)
500 buildFromPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
501 buildFromPRepr repr vect_tc prepr_tc _
503 arg_ty <- mkPReprType res_ty
504 arg <- newLocalVar FSLIT("x") arg_ty
508 $ unwrapFamInstScrut prepr_tc var_tys (Var arg)
510 var_tys = mkTyVarTys $ tyConTyVars vect_tc
511 res_ty = mkTyConApp vect_tc var_tys
513 cons = map (`mkConApp` map Type var_tys) (tyConDataCons vect_tc)
516 from_repr repr@(SumRepr { sum_components = prods
517 , sum_tycon = tycon })
520 vars <- mapM (newLocalVar FSLIT("x")) (map reprType prods)
521 bodies <- sequence . zipWith3 from_unboxed prods cons
523 return . Case expr (mkWildId (reprType repr)) res_ty
524 $ zipWith3 sum_alt (tyConDataCons tycon) vars bodies
526 sum_alt data_con var body = (DataAlt data_con, [var], body)
528 from_repr repr@(EnumRepr { enum_data_con = data_con }) expr
530 var <- newLocalVar FSLIT("n") intPrimTy
532 let res = Case (Var var) (mkWildId intPrimTy) res_ty
533 $ (DEFAULT, [], error_expr)
534 : zipWith mk_alt (tyConDataCons vect_tc) cons
536 return $ Case expr (mkWildId (reprType repr)) res_ty
537 [(DataAlt data_con, [var], res)]
539 mk_alt data_con con = (LitAlt (mkDataConTagLit data_con), [], con)
541 error_expr = mkRuntimeErrorApp rUNTIME_ERROR_ID res_ty
543 $ sep [text "Invalid NDP representation of", ppr vect_tc]
545 from_repr repr expr = from_unboxed repr con expr
547 from_unboxed prod@(ProdRepr { prod_components = tys
548 , prod_data_con = data_con })
552 vars <- mapM (newLocalVar FSLIT("y")) tys
553 return $ Case expr (mkWildId (reprType prod)) res_ty
554 [(DataAlt data_con, vars, con `mkVarApps` vars)]
556 from_unboxed (IdRepr _) con expr
557 = return $ con `App` expr
559 from_unboxed (VoidRepr {}) con expr
562 buildToArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
563 buildToArrPRepr repr vect_tc prepr_tc arr_tc
565 arg_ty <- mkPArrayType el_ty
566 arg <- newLocalVar FSLIT("xs") arg_ty
568 res_ty <- mkPArrayType (reprType repr)
570 shape_vars <- arrShapeVars repr
571 repr_vars <- arrReprVars repr
573 parray_co <- mkBuiltinCo parrayTyCon
575 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
576 co = mkAppCoercion parray_co
578 $ mkTyConApp repr_co var_tys
580 scrut = unwrapFamInstScrut arr_tc var_tys (Var arg)
582 result <- to_repr shape_vars repr_vars repr
586 $ Case scrut (mkWildId (mkTyConApp arr_tc var_tys)) res_ty
587 [(DataAlt arr_dc, shape_vars ++ concat repr_vars, result)]
589 var_tys = mkTyVarTys $ tyConTyVars vect_tc
590 el_ty = mkTyConApp vect_tc var_tys
592 [arr_dc] = tyConDataCons arr_tc
594 to_repr shape_vars@(len_var : _)
596 (SumRepr { sum_components = prods
597 , sum_arr_tycon = tycon
598 , sum_arr_data_con = data_con })
600 exprs <- zipWithM to_prod repr_vars prods
602 return . wrapFamInstBody tycon tys
604 $ map Type tys ++ map Var shape_vars ++ exprs
606 tys = map reprType prods
610 (ProdRepr { prod_components = tys
611 , prod_arr_tycon = tycon
612 , prod_arr_data_con = data_con })
613 = return . wrapFamInstBody tycon tys
615 $ map Type tys ++ map Var (len_var : repr_vars)
619 (EnumRepr { enum_arr_tycon = tycon
620 , enum_arr_data_con = data_con })
621 = return . wrapFamInstBody tycon []
625 to_prod repr_vars@(r : _)
626 (ProdRepr { prod_components = tys@(ty : _)
627 , prod_arr_tycon = tycon
628 , prod_arr_data_con = data_con })
630 len <- lengthPA ty (Var r)
631 return . wrapFamInstBody tycon tys
633 $ map Type tys ++ len : map Var repr_vars
635 to_prod [var] (IdRepr ty) = return (Var var)
636 to_prod [var] (VoidRepr {}) = return (Var var)
639 buildFromArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
640 buildFromArrPRepr repr vect_tc prepr_tc arr_tc
642 arg_ty <- mkPArrayType =<< mkPReprType el_ty
643 arg <- newLocalVar FSLIT("xs") arg_ty
645 res_ty <- mkPArrayType el_ty
647 shape_vars <- arrShapeVars repr
648 repr_vars <- arrReprVars repr
650 parray_co <- mkBuiltinCo parrayTyCon
652 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
653 co = mkAppCoercion parray_co
654 $ mkTyConApp repr_co var_tys
656 scrut = mkCoerce co (Var arg)
658 result = wrapFamInstBody arr_tc var_tys
660 $ map Type var_tys ++ map Var (shape_vars ++ concat repr_vars)
663 (from_repr repr scrut shape_vars repr_vars res_ty result)
665 var_tys = mkTyVarTys $ tyConTyVars vect_tc
666 el_ty = mkTyConApp vect_tc var_tys
668 [arr_dc] = tyConDataCons arr_tc
670 from_repr (SumRepr { sum_components = prods
671 , sum_arr_tycon = tycon
672 , sum_arr_data_con = data_con })
679 vars <- mapM (newLocalVar FSLIT("xs")) =<< mapM arrReprType prods
680 result <- go prods repr_vars vars body
682 let scrut = unwrapFamInstScrut tycon ty_args expr
683 return . Case scrut (mkWildId scrut_ty) res_ty
684 $ [(DataAlt data_con, shape_vars ++ vars, result)]
686 ty_args = map reprType prods
687 scrut_ty = mkTyConApp tycon ty_args
689 go [] [] [] body = return body
690 go (prod : prods) (repr_vars : rss) (var : vars) body
692 shape_vars <- mapM (newLocalVar FSLIT("s")) =<< arrShapeTys prod
694 from_prod prod (Var var) shape_vars repr_vars res_ty
695 =<< go prods rss vars body
697 from_repr repr expr shape_vars [repr_vars] res_ty body
698 = from_prod repr expr shape_vars repr_vars res_ty body
700 from_prod prod@(ProdRepr { prod_components = tys
701 , prod_arr_tycon = tycon
702 , prod_arr_data_con = data_con })
709 let scrut = unwrapFamInstScrut tycon tys expr
710 scrut_ty = mkTyConApp tycon tys
711 ty <- arrReprType prod
713 return $ Case scrut (mkWildId scrut_ty) res_ty
714 [(DataAlt data_con, shape_vars ++ repr_vars, body)]
716 from_prod (EnumRepr { enum_arr_tycon = tycon
717 , enum_arr_data_con = data_con })
723 = let scrut = unwrapFamInstScrut tycon [] expr
724 scrut_ty = mkTyConApp tycon []
726 return $ Case scrut (mkWildId scrut_ty) res_ty
727 [(DataAlt data_con, shape_vars, body)]
729 from_prod (IdRepr ty)
735 = return $ Let (NonRec repr_var expr) body
737 from_prod (VoidRepr {})
743 = return $ Let (NonRec repr_var expr) body
745 buildPRDictRepr :: Repr -> VM CoreExpr
746 buildPRDictRepr (VoidRepr { void_tycon = tycon })
747 = prDFunOfTyCon tycon
748 buildPRDictRepr (IdRepr ty) = mkPR ty
749 buildPRDictRepr (ProdRepr {
750 prod_components = tys
755 dfun <- prDFunOfTyCon tycon
756 return $ dfun `mkTyApps` tys `mkApps` prs
758 buildPRDictRepr (SumRepr {
759 sum_components = prods
760 , sum_tycon = tycon })
762 prs <- mapM buildPRDictRepr prods
763 dfun <- prDFunOfTyCon tycon
764 return $ dfun `mkTyApps` map reprType prods `mkApps` prs
766 buildPRDictRepr (EnumRepr { enum_tycon = tycon })
767 = prDFunOfTyCon tycon
769 buildPRDict :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
770 buildPRDict repr vect_tc prepr_tc _
772 dict <- buildPRDictRepr repr
774 pr_co <- mkBuiltinCo prTyCon
775 let co = mkAppCoercion pr_co
777 $ mkTyConApp arg_co var_tys
779 return $ mkCoerce co dict
781 var_tys = mkTyVarTys $ tyConTyVars vect_tc
783 Just arg_co = tyConFamilyCoercion_maybe prepr_tc
785 buildPArrayTyCon :: TyCon -> TyCon -> VM TyCon
786 buildPArrayTyCon orig_tc vect_tc = fixV $ \repr_tc ->
788 name' <- cloneName mkPArrayTyConOcc orig_name
789 rhs <- buildPArrayTyConRhs orig_name vect_tc repr_tc
790 parray <- builtin parrayTyCon
792 liftDs $ buildAlgTyCon name'
794 [] -- no stupid theta
796 rec_flag -- FIXME: is this ok?
797 False -- FIXME: no generics
798 False -- not GADT syntax
799 (Just $ mk_fam_inst parray vect_tc)
801 orig_name = tyConName orig_tc
802 tyvars = tyConTyVars vect_tc
803 rec_flag = boolToRecFlag (isRecursiveTyCon vect_tc)
806 buildPArrayTyConRhs :: Name -> TyCon -> TyCon -> VM AlgTyConRhs
807 buildPArrayTyConRhs orig_name vect_tc repr_tc
809 data_con <- buildPArrayDataCon orig_name vect_tc repr_tc
810 return $ DataTyCon { data_cons = [data_con], is_enum = False }
812 buildPArrayDataCon :: Name -> TyCon -> TyCon -> VM DataCon
813 buildPArrayDataCon orig_name vect_tc repr_tc
815 dc_name <- cloneName mkPArrayDataConOcc orig_name
816 repr <- mkRepr vect_tc
818 shape_tys <- arrShapeTys repr
819 repr_tys <- arrReprTys repr
821 let tys = shape_tys ++ repr_tys
823 liftDs $ buildDataCon dc_name
825 (map (const NotMarkedStrict) tys)
826 [] -- no field labels
827 (tyConTyVars vect_tc)
828 [] -- no existentials
834 mkPADFun :: TyCon -> VM Var
836 = newExportedVar (mkPADFunOcc $ getOccName vect_tc) =<< paDFunType vect_tc
838 buildTyConBindings :: TyCon -> TyCon -> TyCon -> TyCon -> Var
839 -> VM [(Var, CoreExpr)]
840 buildTyConBindings orig_tc vect_tc prepr_tc arr_tc dfun
842 repr <- mkRepr vect_tc
843 vectDataConWorkers repr orig_tc vect_tc arr_tc
844 dict <- buildPADict repr vect_tc prepr_tc arr_tc dfun
846 return $ (dfun, dict) : binds
848 orig_dcs = tyConDataCons orig_tc
849 vect_dcs = tyConDataCons vect_tc
850 [arr_dc] = tyConDataCons arr_tc
852 repr_tys = map dataConRepArgTys vect_dcs
854 vectDataConWorkers :: Repr -> TyCon -> TyCon -> TyCon
856 vectDataConWorkers repr orig_tc vect_tc arr_tc
859 . zipWith3 def_worker (tyConDataCons orig_tc) rep_tys
860 $ zipWith4 mk_data_con (tyConDataCons vect_tc)
864 mapM_ (uncurry hoistBinding) bs
866 tyvars = tyConTyVars vect_tc
867 var_tys = mkTyVarTys tyvars
868 ty_args = map Type var_tys
870 res_ty = mkTyConApp vect_tc var_tys
872 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
873 reprs = splitSumRepr repr
875 [arr_dc] = tyConDataCons arr_tc
877 mk_data_con con tys pre post
878 = liftM2 (,) (vect_data_con con)
879 (lift_data_con tys pre post (mkDataConTag con))
881 vect_data_con con = return $ mkConApp con ty_args
882 lift_data_con tys pre_reprs post_reprs tag
884 len <- builtin liftingContext
885 args <- mapM (newLocalVar FSLIT("xs"))
886 =<< mapM mkPArrayType tys
888 shape <- replicateShape repr (Var len) tag
889 repr <- mk_arr_repr (Var len) (map Var args)
891 pre <- liftM concat $ mapM emptyArrRepr pre_reprs
892 post <- liftM concat $ mapM emptyArrRepr post_reprs
894 return . mkLams (len : args)
895 . wrapFamInstBody arr_tc var_tys
897 $ ty_args ++ shape ++ pre ++ repr ++ post
901 units <- replicatePA len (Var unitDataConId)
904 mk_arr_repr len arrs = return arrs
906 def_worker data_con arg_tys mk_body
910 . polyAbstract tyvars $ \abstract ->
911 liftM (abstract . vectorised)
912 $ buildClosures tyvars [] arg_tys res_ty mk_body
914 vect_worker <- cloneId mkVectOcc orig_worker (exprType body)
915 defGlobalVar orig_worker vect_worker
916 return (vect_worker, body)
918 orig_worker = dataConWorkId data_con
920 buildPADict :: Repr -> TyCon -> TyCon -> TyCon -> Var -> VM CoreExpr
921 buildPADict repr vect_tc prepr_tc arr_tc dfun
922 = polyAbstract tvs $ \abstract ->
924 meth_binds <- mapM (mk_method repr) paMethods
925 let meth_exprs = map (Var . fst) meth_binds
927 pa_dc <- builtin paDataCon
928 let dict = mkConApp pa_dc (Type (mkTyConApp vect_tc arg_tys) : meth_exprs)
929 body = Let (Rec meth_binds) dict
930 return . mkInlineMe $ abstract body
932 tvs = tyConTyVars arr_tc
933 arg_tys = mkTyVarTys tvs
935 mk_method repr (name, build)
938 body <- build repr vect_tc prepr_tc arr_tc
939 var <- newLocalVar name (exprType body)
940 return (var, mkInlineMe body)
942 paMethods = [(FSLIT("toPRepr"), buildToPRepr),
943 (FSLIT("fromPRepr"), buildFromPRepr),
944 (FSLIT("toArrPRepr"), buildToArrPRepr),
945 (FSLIT("fromArrPRepr"), buildFromArrPRepr),
946 (FSLIT("dictPRepr"), buildPRDict)]
948 -- | Split the given tycons into two sets depending on whether they have to be
949 -- converted (first list) or not (second list). The first argument contains
950 -- information about the conversion status of external tycons:
952 -- * tycons which have converted versions are mapped to True
953 -- * tycons which are not changed by vectorisation are mapped to False
954 -- * tycons which can't be converted are not elements of the map
956 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
957 classifyTyCons = classify [] []
959 classify conv keep cs [] = (conv, keep)
960 classify conv keep cs ((tcs, ds) : rs)
961 | can_convert && must_convert
962 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
964 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
966 = classify conv keep cs rs
968 refs = ds `delListFromUniqSet` tcs
970 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
971 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
973 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
975 -- | Compute mutually recursive groups of tycons in topological order
977 tyConGroups :: [TyCon] -> [TyConGroup]
978 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
980 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
981 , let ds = tyConsOfTyCon tc]
983 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
984 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
986 (tcs, dss) = unzip els
988 tyConsOfTyCon :: TyCon -> UniqSet TyCon
990 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
992 tyConsOfType :: Type -> UniqSet TyCon
994 | Just ty' <- coreView ty = tyConsOfType ty'
995 tyConsOfType (TyVarTy v) = emptyUniqSet
996 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
998 extend | isUnLiftedTyCon tc
999 || isTupleTyCon tc = id
1001 | otherwise = (`addOneToUniqSet` tc)
1003 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
1004 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
1005 `addOneToUniqSet` funTyCon
1006 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
1007 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
1009 tyConsOfTypes :: [Type] -> UniqSet TyCon
1010 tyConsOfTypes = unionManyUniqSets . map tyConsOfType
1013 -- ----------------------------------------------------------------------------
1016 fromVect :: Type -> CoreExpr -> VM CoreExpr
1017 fromVect ty expr | Just ty' <- coreView ty = fromVect ty' expr
1018 fromVect (FunTy arg_ty res_ty) expr
1020 arg <- newLocalVar FSLIT("x") arg_ty
1021 varg <- toVect arg_ty (Var arg)
1022 varg_ty <- vectType arg_ty
1023 vres_ty <- vectType res_ty
1024 apply <- builtin applyClosureVar
1025 body <- fromVect res_ty
1026 $ Var apply `mkTyApps` [varg_ty, vres_ty] `mkApps` [expr, varg]
1027 return $ Lam arg body
1029 = identityConv ty >> return expr
1031 toVect :: Type -> CoreExpr -> VM CoreExpr
1032 toVect ty expr = identityConv ty >> return expr
1034 identityConv :: Type -> VM ()
1035 identityConv ty | Just ty' <- coreView ty = identityConv ty'
1036 identityConv (TyConApp tycon tys)
1038 mapM_ identityConv tys
1039 identityConvTyCon tycon
1040 identityConv ty = noV
1042 identityConvTyCon :: TyCon -> VM ()
1043 identityConvTyCon tc
1044 | isBoxedTupleTyCon tc = return ()
1045 | isUnLiftedTyCon tc = return ()
1047 tc' <- maybeV (lookupTyCon tc)
1048 if tc == tc' then return () else noV