2 module VectType ( vectTyCon, vectAndLiftType, vectType, vectTypeEnv,
3 mkRepr, arrShapeTys, arrShapeVars, arrSelector,
12 import HscTypes ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
21 import FamInstEnv ( FamInst, mkLocalFamInst )
24 import BasicTypes ( StrictnessMark(..), boolToRecFlag )
25 import Var ( Var, TyVar )
26 import Id ( mkWildId )
27 import Name ( Name, getOccName )
30 import TysPrim ( intPrimTy )
35 import Util ( singleton )
36 import Digraph ( SCC(..), stronglyConnComp )
41 import Control.Monad ( liftM, liftM2, zipWithM, zipWithM_, mapAndUnzipM )
42 import Data.List ( inits, tails, zipWith4, zipWith5 )
44 -- ----------------------------------------------------------------------------
47 vectTyCon :: TyCon -> VM TyCon
49 | isFunTyCon tc = builtin closureTyCon
50 | isBoxedTupleTyCon tc = return tc
51 | isUnLiftedTyCon tc = return tc
55 Just tc' -> return tc'
57 -- FIXME: just for now
58 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
60 vectAndLiftType :: Type -> VM (Type, Type)
61 vectAndLiftType ty | Just ty' <- coreView ty = vectAndLiftType ty'
64 mdicts <- mapM paDictArgType tyvars
65 let dicts = [dict | Just dict <- mdicts]
66 vmono_ty <- vectType mono_ty
67 lmono_ty <- mkPArrayType vmono_ty
68 return (abstractType tyvars dicts vmono_ty,
69 abstractType tyvars dicts lmono_ty)
71 (tyvars, mono_ty) = splitForAllTys ty
74 vectType :: Type -> VM Type
75 vectType ty | Just ty' <- coreView ty = vectType ty'
76 vectType (TyVarTy tv) = return $ TyVarTy tv
77 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
78 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
79 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
80 (mapM vectAndBoxType [ty1,ty2])
81 vectType ty@(ForAllTy _ _)
83 mdicts <- mapM paDictArgType tyvars
84 mono_ty' <- vectType mono_ty
85 return $ abstractType tyvars [dict | Just dict <- mdicts] mono_ty'
87 (tyvars, mono_ty) = splitForAllTys ty
89 vectType ty = pprPanic "vectType:" (ppr ty)
91 vectAndBoxType :: Type -> VM Type
92 vectAndBoxType ty = vectType ty >>= boxType
94 abstractType :: [TyVar] -> [Type] -> Type -> Type
95 abstractType tyvars dicts = mkForAllTys tyvars . mkFunTys dicts
97 -- ----------------------------------------------------------------------------
100 boxType :: Type -> VM Type
102 | Just (tycon, []) <- splitTyConApp_maybe ty
103 , isUnLiftedTyCon tycon
105 r <- lookupBoxedTyCon tycon
107 Just tycon' -> return $ mkTyConApp tycon' []
109 boxType ty = return ty
111 -- ----------------------------------------------------------------------------
114 type TyConGroup = ([TyCon], UniqSet TyCon)
116 vectTypeEnv :: TypeEnv -> VM (TypeEnv, [FamInst], [(Var, CoreExpr)])
119 cs <- readGEnv $ mk_map . global_tycons
120 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
121 keep_dcs = concatMap tyConDataCons keep_tcs
122 zipWithM_ defTyCon keep_tcs keep_tcs
123 zipWithM_ defDataCon keep_dcs keep_dcs
124 new_tcs <- vectTyConDecls conv_tcs
126 let orig_tcs = keep_tcs ++ conv_tcs
127 vect_tcs = keep_tcs ++ new_tcs
129 repr_tcs <- zipWithM buildPReprTyCon orig_tcs vect_tcs
130 parr_tcs <- zipWithM buildPArrayTyCon orig_tcs vect_tcs
131 dfuns <- mapM mkPADFun vect_tcs
132 defTyConPAs (zip vect_tcs dfuns)
133 binds <- sequence (zipWith5 buildTyConBindings orig_tcs
139 let all_new_tcs = new_tcs ++ repr_tcs ++ parr_tcs
141 let new_env = extendTypeEnvList env
142 (map ATyCon all_new_tcs
143 ++ [ADataCon dc | tc <- all_new_tcs
144 , dc <- tyConDataCons tc])
146 return (new_env, map mkLocalFamInst (repr_tcs ++ parr_tcs), concat binds)
148 tycons = typeEnvTyCons env
149 groups = tyConGroups tycons
151 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
154 vectTyConDecls :: [TyCon] -> VM [TyCon]
155 vectTyConDecls tcs = fixV $ \tcs' ->
157 mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
158 mapM vectTyConDecl tcs
161 lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
163 vectTyConDecl :: TyCon -> VM TyCon
166 name' <- cloneName mkVectTyConOcc name
167 rhs' <- vectAlgTyConRhs (algTyConRhs tc)
169 liftDs $ buildAlgTyCon name'
171 [] -- no stupid theta
173 rec_flag -- FIXME: is this ok?
174 False -- FIXME: no generics
175 False -- not GADT syntax
176 Nothing -- not a family instance
179 tyvars = tyConTyVars tc
180 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
182 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
183 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
187 data_cons' <- mapM vectDataCon data_cons
188 zipWithM_ defDataCon data_cons data_cons'
189 return $ DataTyCon { data_cons = data_cons'
192 vectAlgTyConRhs _ = panic "vectAlgTyConRhs"
194 vectDataCon :: DataCon -> VM DataCon
196 | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
197 | not . null $ dataConEqSpec dc = pprPanic "vectDataCon: eq spec" (ppr dc)
200 name' <- cloneName mkVectDataConOcc name
201 tycon' <- vectTyCon tycon
202 arg_tys <- mapM vectType rep_arg_tys
204 liftDs $ buildDataCon name'
206 (map (const NotMarkedStrict) arg_tys)
207 [] -- no labelled fields
209 [] -- no existential tvs for now
210 [] -- no eq spec for now
215 name = dataConName dc
216 univ_tvs = dataConUnivTyVars dc
217 rep_arg_tys = dataConRepArgTys dc
218 tycon = dataConTyCon dc
220 mk_fam_inst :: TyCon -> TyCon -> (TyCon, [Type])
221 mk_fam_inst fam_tc arg_tc
222 = (fam_tc, [mkTyConApp arg_tc . mkTyVarTys $ tyConTyVars arg_tc])
224 buildPReprTyCon :: TyCon -> TyCon -> VM TyCon
225 buildPReprTyCon orig_tc vect_tc
227 name <- cloneName mkPReprTyConOcc (tyConName orig_tc)
228 rhs_ty <- buildPReprType vect_tc
229 prepr_tc <- builtin preprTyCon
230 liftDs $ buildSynTyCon name
232 (SynonymTyCon rhs_ty)
233 (Just $ mk_fam_inst prepr_tc vect_tc)
235 tyvars = tyConTyVars vect_tc
238 data Repr = ProdRepr {
239 prod_components :: [Type]
240 , prod_tycon :: TyCon
241 , prod_data_con :: DataCon
242 , prod_arr_tycon :: TyCon
243 , prod_arr_data_con :: DataCon
247 sum_components :: [Repr]
249 , sum_arr_tycon :: TyCon
250 , sum_arr_data_con :: DataCon
257 , void_bottom :: CoreExpr
262 , enum_data_con :: DataCon
263 , enum_arr_tycon :: TyCon
264 , enum_arr_data_con :: DataCon
270 tycon <- builtin voidTyCon
271 var <- builtin voidVar
274 , void_bottom = Var var
281 tycon <- builtin enumerationTyCon
282 let [data_con] = tyConDataCons tycon
283 (arr_tycon, _) <- parrayReprTyCon (mkTyConApp tycon [])
284 let [arr_data_con] = tyConDataCons arr_tycon
288 , enum_data_con = data_con
289 , enum_arr_tycon = arr_tycon
290 , 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)
394 arrSelector _ _ = panic "arrSelector"
396 emptyArrRepr :: Repr -> VM [CoreExpr]
397 emptyArrRepr (SumRepr { sum_components = prods })
398 = liftM concat $ mapM emptyArrRepr prods
399 emptyArrRepr (ProdRepr { prod_components = [] })
400 = return [Var unitDataConId]
401 emptyArrRepr (ProdRepr { prod_components = tys })
403 emptyArrRepr (IdRepr ty)
404 = liftM singleton $ emptyPA ty
405 emptyArrRepr (VoidRepr { void_tycon = tycon })
406 = liftM singleton $ emptyPA (mkTyConApp tycon [])
407 emptyArrRepr (EnumRepr {})
410 arrReprTys :: Repr -> VM [Type]
411 arrReprTys (SumRepr { sum_components = reprs })
412 = liftM concat $ mapM arrReprTys reprs
413 arrReprTys (ProdRepr { prod_components = [] })
415 arrReprTys (ProdRepr { prod_components = tys })
416 = mapM mkPArrayType tys
417 arrReprTys (IdRepr ty)
418 = liftM singleton $ mkPArrayType ty
419 arrReprTys (VoidRepr { void_tycon = tycon })
420 = liftM singleton $ mkPArrayType (mkTyConApp tycon [])
421 arrReprTys (EnumRepr {})
424 arrReprTys' :: Repr -> VM [[Type]]
425 arrReprTys' (SumRepr { sum_components = reprs })
426 = mapM arrReprTys reprs
427 arrReprTys' repr = liftM singleton $ arrReprTys repr
429 arrReprVars :: Repr -> VM [[Var]]
431 = mapM (mapM (newLocalVar (fsLit "rs"))) =<< arrReprTys' repr
433 mkRepr :: TyCon -> VM Repr
435 | [tys] <- rep_tys = boxedProductRepr tys
436 -- | all null rep_tys = enumRepr
437 | otherwise = sumRepr =<< mapM unboxedProductRepr rep_tys
439 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
441 buildPReprType :: TyCon -> VM Type
442 buildPReprType = liftM reprType . mkRepr
444 buildToPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
445 buildToPRepr repr vect_tc prepr_tc _
447 arg <- newLocalVar (fsLit "x") arg_ty
448 result <- to_repr repr (Var arg)
451 . wrapFamInstBody prepr_tc var_tys
454 var_tys = mkTyVarTys $ tyConTyVars vect_tc
455 arg_ty = mkTyConApp vect_tc var_tys
456 res_ty = reprType repr
458 cons = tyConDataCons vect_tc
461 to_repr (SumRepr { sum_components = prods
462 , sum_tycon = tycon })
465 (vars, bodies) <- mapAndUnzipM to_unboxed prods
466 return . Case expr (mkWildId (exprType expr)) res_ty
467 $ zipWith4 mk_alt cons vars (tyConDataCons tycon) bodies
469 mk_alt con vars sum_con body
470 = (DataAlt con, vars, mkConApp sum_con (ty_args ++ [body]))
472 ty_args = map (Type . reprType) prods
474 to_repr (EnumRepr { enum_data_con = data_con }) expr
475 = return . Case expr (mkWildId (exprType expr)) res_ty
478 mk_alt con = (DataAlt con, [], mkConApp data_con [mkDataConTag con])
482 (vars, body) <- to_unboxed prod
483 return $ Case expr (mkWildId (exprType expr)) res_ty
484 [(DataAlt con, vars, body)]
486 to_unboxed (ProdRepr { prod_components = tys
487 , prod_data_con = data_con })
489 vars <- mapM (newLocalVar (fsLit "r")) tys
490 return (vars, mkConApp data_con (map Type tys ++ map Var vars))
492 to_unboxed (IdRepr ty)
494 var <- newLocalVar (fsLit "y") ty
495 return ([var], Var var)
497 to_unboxed (VoidRepr { void_bottom = bottom })
498 = return ([], bottom)
500 to_unboxed _ = panic "buildToPRepr/to_unboxed"
503 buildFromPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
504 buildFromPRepr repr vect_tc prepr_tc _
506 arg_ty <- mkPReprType res_ty
507 arg <- newLocalVar (fsLit "x") arg_ty
511 $ unwrapFamInstScrut prepr_tc var_tys (Var arg)
513 var_tys = mkTyVarTys $ tyConTyVars vect_tc
514 res_ty = mkTyConApp vect_tc var_tys
516 cons = map (`mkConApp` map Type var_tys) (tyConDataCons vect_tc)
519 from_repr repr@(SumRepr { sum_components = prods
520 , sum_tycon = tycon })
523 vars <- mapM (newLocalVar (fsLit "x")) (map reprType prods)
524 bodies <- sequence . zipWith3 from_unboxed prods cons
526 return . Case expr (mkWildId (reprType repr)) res_ty
527 $ zipWith3 sum_alt (tyConDataCons tycon) vars bodies
529 sum_alt data_con var body = (DataAlt data_con, [var], body)
531 from_repr repr@(EnumRepr { enum_data_con = data_con }) expr
533 var <- newLocalVar (fsLit "n") intPrimTy
535 let res = Case (Var var) (mkWildId intPrimTy) res_ty
536 $ (DEFAULT, [], error_expr)
537 : zipWith mk_alt (tyConDataCons vect_tc) cons
539 return $ Case expr (mkWildId (reprType repr)) res_ty
540 [(DataAlt data_con, [var], res)]
542 mk_alt data_con con = (LitAlt (mkDataConTagLit data_con), [], con)
544 error_expr = mkRuntimeErrorApp rUNTIME_ERROR_ID res_ty
546 $ sep [text "Invalid NDP representation of", ppr vect_tc]
548 from_repr repr expr = from_unboxed repr con expr
550 from_unboxed prod@(ProdRepr { prod_components = tys
551 , prod_data_con = data_con })
555 vars <- mapM (newLocalVar (fsLit "y")) tys
556 return $ Case expr (mkWildId (reprType prod)) res_ty
557 [(DataAlt data_con, vars, con `mkVarApps` vars)]
559 from_unboxed (IdRepr _) con expr
560 = return $ con `App` expr
562 from_unboxed (VoidRepr {}) con _
565 from_unboxed _ _ _ = panic "buildFromPRepr/from_unboxed"
567 buildToArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
568 buildToArrPRepr repr vect_tc prepr_tc arr_tc
570 arg_ty <- mkPArrayType el_ty
571 arg <- newLocalVar (fsLit "xs") arg_ty
573 res_ty <- mkPArrayType (reprType repr)
575 shape_vars <- arrShapeVars repr
576 repr_vars <- arrReprVars repr
578 parray_co <- mkBuiltinCo parrayTyCon
580 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
581 co = mkAppCoercion parray_co
583 $ mkTyConApp repr_co var_tys
585 scrut = unwrapFamInstScrut arr_tc var_tys (Var arg)
587 result <- to_repr shape_vars repr_vars repr
591 $ Case scrut (mkWildId (mkTyConApp arr_tc var_tys)) res_ty
592 [(DataAlt arr_dc, shape_vars ++ concat repr_vars, result)]
594 var_tys = mkTyVarTys $ tyConTyVars vect_tc
595 el_ty = mkTyConApp vect_tc var_tys
597 [arr_dc] = tyConDataCons arr_tc
599 to_repr shape_vars@(_ : _)
601 (SumRepr { sum_components = prods
602 , sum_arr_tycon = tycon
603 , sum_arr_data_con = data_con })
605 exprs <- zipWithM to_prod repr_vars prods
607 return . wrapFamInstBody tycon tys
609 $ map Type tys ++ map Var shape_vars ++ exprs
611 tys = map reprType prods
615 (ProdRepr { prod_components = tys
616 , prod_arr_tycon = tycon
617 , prod_arr_data_con = data_con })
618 = return . wrapFamInstBody tycon tys
620 $ map Type tys ++ map Var (len_var : repr_vars)
624 (EnumRepr { enum_arr_tycon = tycon
625 , enum_arr_data_con = data_con })
626 = return . wrapFamInstBody tycon []
630 to_repr _ _ _ = panic "buildToArrPRepr/to_repr"
632 to_prod repr_vars@(r : _)
633 (ProdRepr { prod_components = tys@(ty : _)
634 , prod_arr_tycon = tycon
635 , prod_arr_data_con = data_con })
637 len <- lengthPA ty (Var r)
638 return . wrapFamInstBody tycon tys
640 $ map Type tys ++ len : map Var repr_vars
642 to_prod [var] (IdRepr _) = return (Var var)
643 to_prod [var] (VoidRepr {}) = return (Var var)
644 to_prod _ _ = panic "buildToArrPRepr/to_prod"
647 buildFromArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
648 buildFromArrPRepr repr vect_tc prepr_tc arr_tc
650 arg_ty <- mkPArrayType =<< mkPReprType el_ty
651 arg <- newLocalVar (fsLit "xs") arg_ty
653 res_ty <- mkPArrayType el_ty
655 shape_vars <- arrShapeVars repr
656 repr_vars <- arrReprVars repr
658 parray_co <- mkBuiltinCo parrayTyCon
660 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
661 co = mkAppCoercion parray_co
662 $ mkTyConApp repr_co var_tys
664 scrut = mkCoerce co (Var arg)
666 result = wrapFamInstBody arr_tc var_tys
668 $ map Type var_tys ++ map Var (shape_vars ++ concat repr_vars)
671 (from_repr repr scrut shape_vars repr_vars res_ty result)
673 var_tys = mkTyVarTys $ tyConTyVars vect_tc
674 el_ty = mkTyConApp vect_tc var_tys
676 [arr_dc] = tyConDataCons arr_tc
678 from_repr (SumRepr { sum_components = prods
679 , sum_arr_tycon = tycon
680 , sum_arr_data_con = data_con })
687 vars <- mapM (newLocalVar (fsLit "xs")) =<< mapM arrReprType prods
688 result <- go prods repr_vars vars body
690 let scrut = unwrapFamInstScrut tycon ty_args expr
691 return . Case scrut (mkWildId scrut_ty) res_ty
692 $ [(DataAlt data_con, shape_vars ++ vars, result)]
694 ty_args = map reprType prods
695 scrut_ty = mkTyConApp tycon ty_args
697 go [] [] [] body = return body
698 go (prod : prods) (repr_vars : rss) (var : vars) body
700 shape_vars <- mapM (newLocalVar (fsLit "s")) =<< arrShapeTys prod
702 from_prod prod (Var var) shape_vars repr_vars res_ty
703 =<< go prods rss vars body
704 go _ _ _ _ = panic "buildFromArrPRepr/go"
706 from_repr repr expr shape_vars [repr_vars] res_ty body
707 = from_prod repr expr shape_vars repr_vars res_ty body
709 from_repr _ _ _ _ _ _ = panic "buildFromArrPRepr/from_repr"
711 from_prod (ProdRepr { prod_components = tys
712 , prod_arr_tycon = tycon
713 , prod_arr_data_con = data_con })
720 let scrut = unwrapFamInstScrut tycon tys expr
721 scrut_ty = mkTyConApp tycon tys
723 return $ Case scrut (mkWildId scrut_ty) res_ty
724 [(DataAlt data_con, shape_vars ++ repr_vars, body)]
726 from_prod (EnumRepr { enum_arr_tycon = tycon
727 , enum_arr_data_con = data_con })
733 = let scrut = unwrapFamInstScrut tycon [] expr
734 scrut_ty = mkTyConApp tycon []
736 return $ Case scrut (mkWildId scrut_ty) res_ty
737 [(DataAlt data_con, shape_vars, body)]
745 = return $ Let (NonRec repr_var expr) body
747 from_prod (VoidRepr {})
753 = return $ Let (NonRec repr_var expr) body
755 from_prod _ _ _ _ _ _ = panic "buildFromArrPRepr/from_prod"
757 buildPRDictRepr :: Repr -> VM CoreExpr
758 buildPRDictRepr (VoidRepr { void_tycon = tycon })
759 = prDFunOfTyCon tycon
760 buildPRDictRepr (IdRepr ty) = mkPR ty
761 buildPRDictRepr (ProdRepr {
762 prod_components = tys
767 dfun <- prDFunOfTyCon tycon
768 return $ dfun `mkTyApps` tys `mkApps` prs
770 buildPRDictRepr (SumRepr {
771 sum_components = prods
772 , sum_tycon = tycon })
774 prs <- mapM buildPRDictRepr prods
775 dfun <- prDFunOfTyCon tycon
776 return $ dfun `mkTyApps` map reprType prods `mkApps` prs
778 buildPRDictRepr (EnumRepr { enum_tycon = tycon })
779 = prDFunOfTyCon tycon
781 buildPRDict :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
782 buildPRDict repr vect_tc prepr_tc _
784 dict <- buildPRDictRepr repr
786 pr_co <- mkBuiltinCo prTyCon
787 let co = mkAppCoercion pr_co
789 $ mkTyConApp arg_co var_tys
791 return $ mkCoerce co dict
793 var_tys = mkTyVarTys $ tyConTyVars vect_tc
795 Just arg_co = tyConFamilyCoercion_maybe prepr_tc
797 buildPArrayTyCon :: TyCon -> TyCon -> VM TyCon
798 buildPArrayTyCon orig_tc vect_tc = fixV $ \repr_tc ->
800 name' <- cloneName mkPArrayTyConOcc orig_name
801 rhs <- buildPArrayTyConRhs orig_name vect_tc repr_tc
802 parray <- builtin parrayTyCon
804 liftDs $ buildAlgTyCon name'
806 [] -- no stupid theta
808 rec_flag -- FIXME: is this ok?
809 False -- FIXME: no generics
810 False -- not GADT syntax
811 (Just $ mk_fam_inst parray vect_tc)
813 orig_name = tyConName orig_tc
814 tyvars = tyConTyVars vect_tc
815 rec_flag = boolToRecFlag (isRecursiveTyCon vect_tc)
818 buildPArrayTyConRhs :: Name -> TyCon -> TyCon -> VM AlgTyConRhs
819 buildPArrayTyConRhs orig_name vect_tc repr_tc
821 data_con <- buildPArrayDataCon orig_name vect_tc repr_tc
822 return $ DataTyCon { data_cons = [data_con], is_enum = False }
824 buildPArrayDataCon :: Name -> TyCon -> TyCon -> VM DataCon
825 buildPArrayDataCon orig_name vect_tc repr_tc
827 dc_name <- cloneName mkPArrayDataConOcc orig_name
828 repr <- mkRepr vect_tc
830 shape_tys <- arrShapeTys repr
831 repr_tys <- arrReprTys repr
833 let tys = shape_tys ++ repr_tys
835 liftDs $ buildDataCon dc_name
837 (map (const NotMarkedStrict) tys)
838 [] -- no field labels
839 (tyConTyVars vect_tc)
840 [] -- no existentials
846 mkPADFun :: TyCon -> VM Var
848 = newExportedVar (mkPADFunOcc $ getOccName vect_tc) =<< paDFunType vect_tc
850 buildTyConBindings :: TyCon -> TyCon -> TyCon -> TyCon -> Var
851 -> VM [(Var, CoreExpr)]
852 buildTyConBindings orig_tc vect_tc prepr_tc arr_tc dfun
854 repr <- mkRepr vect_tc
855 vectDataConWorkers repr orig_tc vect_tc arr_tc
856 dict <- buildPADict repr vect_tc prepr_tc arr_tc dfun
858 return $ (dfun, dict) : binds
860 vectDataConWorkers :: Repr -> TyCon -> TyCon -> TyCon
862 vectDataConWorkers repr orig_tc vect_tc arr_tc
865 . zipWith3 def_worker (tyConDataCons orig_tc) rep_tys
866 $ zipWith4 mk_data_con (tyConDataCons vect_tc)
870 mapM_ (uncurry hoistBinding) bs
872 tyvars = tyConTyVars vect_tc
873 var_tys = mkTyVarTys tyvars
874 ty_args = map Type var_tys
876 res_ty = mkTyConApp vect_tc var_tys
878 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
879 reprs = splitSumRepr repr
881 [arr_dc] = tyConDataCons arr_tc
883 mk_data_con con tys pre post
884 = liftM2 (,) (vect_data_con con)
885 (lift_data_con tys pre post (mkDataConTag con))
887 vect_data_con con = return $ mkConApp con ty_args
888 lift_data_con tys pre_reprs post_reprs tag
890 len <- builtin liftingContext
891 args <- mapM (newLocalVar (fsLit "xs"))
892 =<< mapM mkPArrayType tys
894 shape <- replicateShape repr (Var len) tag
895 repr <- mk_arr_repr (Var len) (map Var args)
897 pre <- liftM concat $ mapM emptyArrRepr pre_reprs
898 post <- liftM concat $ mapM emptyArrRepr post_reprs
900 return . mkLams (len : args)
901 . wrapFamInstBody arr_tc var_tys
903 $ ty_args ++ shape ++ pre ++ repr ++ post
907 units <- replicatePA len (Var unitDataConId)
910 mk_arr_repr _ arrs = return arrs
912 def_worker data_con arg_tys mk_body
916 . polyAbstract tyvars $ \abstract ->
917 liftM (abstract . vectorised)
918 $ buildClosures tyvars [] arg_tys res_ty mk_body
920 vect_worker <- cloneId mkVectOcc orig_worker (exprType body)
921 defGlobalVar orig_worker vect_worker
922 return (vect_worker, body)
924 orig_worker = dataConWorkId data_con
926 buildPADict :: Repr -> TyCon -> TyCon -> TyCon -> Var -> VM CoreExpr
927 buildPADict repr vect_tc prepr_tc arr_tc _
928 = polyAbstract tvs $ \abstract ->
930 meth_binds <- mapM (mk_method repr) paMethods
931 let meth_exprs = map (Var . fst) meth_binds
933 pa_dc <- builtin paDataCon
934 let dict = mkConApp pa_dc (Type (mkTyConApp vect_tc arg_tys) : meth_exprs)
935 body = Let (Rec meth_binds) dict
936 return . mkInlineMe $ abstract body
938 tvs = tyConTyVars arr_tc
939 arg_tys = mkTyVarTys tvs
941 mk_method repr (name, build)
944 body <- build repr vect_tc prepr_tc arr_tc
945 var <- newLocalVar name (exprType body)
946 return (var, mkInlineMe body)
948 paMethods :: [(FastString, Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr)]
949 paMethods = [(fsLit "toPRepr", buildToPRepr),
950 (fsLit "fromPRepr", buildFromPRepr),
951 (fsLit "toArrPRepr", buildToArrPRepr),
952 (fsLit "fromArrPRepr", buildFromArrPRepr),
953 (fsLit "dictPRepr", buildPRDict)]
955 -- | Split the given tycons into two sets depending on whether they have to be
956 -- converted (first list) or not (second list). The first argument contains
957 -- information about the conversion status of external tycons:
959 -- * tycons which have converted versions are mapped to True
960 -- * tycons which are not changed by vectorisation are mapped to False
961 -- * tycons which can't be converted are not elements of the map
963 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
964 classifyTyCons = classify [] []
966 classify conv keep _ [] = (conv, keep)
967 classify conv keep cs ((tcs, ds) : rs)
968 | can_convert && must_convert
969 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
971 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
973 = classify conv keep cs rs
975 refs = ds `delListFromUniqSet` tcs
977 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
978 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
980 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
982 -- | Compute mutually recursive groups of tycons in topological order
984 tyConGroups :: [TyCon] -> [TyConGroup]
985 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
987 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
988 , let ds = tyConsOfTyCon tc]
990 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
991 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
993 (tcs, dss) = unzip els
995 tyConsOfTyCon :: TyCon -> UniqSet TyCon
997 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
999 tyConsOfType :: Type -> UniqSet TyCon
1001 | Just ty' <- coreView ty = tyConsOfType ty'
1002 tyConsOfType (TyVarTy _) = emptyUniqSet
1003 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
1005 extend | isUnLiftedTyCon tc
1006 || isTupleTyCon tc = id
1008 | otherwise = (`addOneToUniqSet` tc)
1010 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
1011 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
1012 `addOneToUniqSet` funTyCon
1013 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
1014 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
1016 tyConsOfTypes :: [Type] -> UniqSet TyCon
1017 tyConsOfTypes = unionManyUniqSets . map tyConsOfType
1020 -- ----------------------------------------------------------------------------
1023 fromVect :: Type -> CoreExpr -> VM CoreExpr
1024 fromVect ty expr | Just ty' <- coreView ty = fromVect ty' expr
1025 fromVect (FunTy arg_ty res_ty) expr
1027 arg <- newLocalVar (fsLit "x") arg_ty
1028 varg <- toVect arg_ty (Var arg)
1029 varg_ty <- vectType arg_ty
1030 vres_ty <- vectType res_ty
1031 apply <- builtin applyClosureVar
1032 body <- fromVect res_ty
1033 $ Var apply `mkTyApps` [varg_ty, vres_ty] `mkApps` [expr, varg]
1034 return $ Lam arg body
1036 = identityConv ty >> return expr
1038 toVect :: Type -> CoreExpr -> VM CoreExpr
1039 toVect ty expr = identityConv ty >> return expr
1041 identityConv :: Type -> VM ()
1042 identityConv ty | Just ty' <- coreView ty = identityConv ty'
1043 identityConv (TyConApp tycon tys)
1045 mapM_ identityConv tys
1046 identityConvTyCon tycon
1047 identityConv _ = noV
1049 identityConvTyCon :: TyCon -> VM ()
1050 identityConvTyCon tc
1051 | isBoxedTupleTyCon tc = return ()
1052 | isUnLiftedTyCon tc = return ()
1054 tc' <- maybeV (lookupTyCon tc)
1055 if tc == tc' then return () else noV