1 {-# OPTIONS_GHC -XNoMonoLocalBinds #-}
3 module Vectorise.Type.Env (
10 import Vectorise.Builtins
11 import Vectorise.Type.TyConDecl
12 import Vectorise.Type.Classify
13 import Vectorise.Type.PADict
14 import Vectorise.Type.PData
15 import Vectorise.Type.PRepr
16 import Vectorise.Type.Repr
17 import Vectorise.Utils
43 -- | Vectorise a type environment.
44 -- The type environment contains all the type things defined in a module.
46 vectTypeEnv :: TypeEnv
47 -> VM ( TypeEnv -- Vectorised type environment.
48 , [FamInst] -- New type family instances.
49 , [(Var, CoreExpr)]) -- New top level bindings.
52 traceVt "** vectTypeEnv" $ ppr env
54 cs <- readGEnv $ mk_map . global_tycons
56 -- Split the list of TyCons into the ones we have to vectorise vs the
57 -- ones we can pass through unchanged. We also pass through algebraic
58 -- types that use non Haskell98 features, as we don't handle those.
59 let tycons = typeEnvTyCons env
60 groups = tyConGroups tycons
62 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
63 orig_tcs = keep_tcs ++ conv_tcs
64 keep_dcs = concatMap tyConDataCons keep_tcs
66 -- Just use the unvectorised versions of these constructors in vectorised code.
67 zipWithM_ defTyCon keep_tcs keep_tcs
68 zipWithM_ defDataCon keep_dcs keep_dcs
70 -- Vectorise all the declarations.
71 new_tcs <- vectTyConDecls conv_tcs
73 -- We don't need to make new representation types for dictionary
74 -- constructors. The constructors are always fully applied, and we don't
75 -- need to lift them to arrays as a dictionary of a particular type
76 -- always has the same value.
77 let vect_tcs = filter (not . isClassTyCon)
80 reprs <- mapM tyConRepr vect_tcs
81 repr_tcs <- zipWith3M buildPReprTyCon orig_tcs vect_tcs reprs
82 pdata_tcs <- zipWith3M buildPDataTyCon orig_tcs vect_tcs reprs
83 updGEnv $ extendFamEnv
85 $ repr_tcs ++ pdata_tcs
87 -- Create PRepr and PData instances for the vectorised types.
88 -- We get back the binds for the instance functions,
89 -- and some new type constructors for the representation types.
90 (_, binds, inst_tcs) <- fixV $ \ ~(dfuns', _, _) ->
92 defTyConPAs (zipLazy vect_tcs dfuns')
93 reprs <- mapM tyConRepr vect_tcs
96 $ zipWith5 buildTyConBindings
104 return (dfuns, binds, repr_tcs ++ pdata_tcs)
106 -- The new type constructors are the vectorised versions of the originals,
107 -- plus the new type constructors that we use for the representations.
108 let all_new_tcs = new_tcs ++ inst_tcs
110 let new_env = extendTypeEnvList env
111 $ map ATyCon all_new_tcs
112 ++ [ADataCon dc | tc <- all_new_tcs
113 , dc <- tyConDataCons tc]
115 return (new_env, map mkLocalFamInst inst_tcs, binds)
118 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
120 buildTyConBindings :: TyCon -> TyCon -> TyCon -> TyCon -> SumRepr -> VM Var
121 buildTyConBindings orig_tc vect_tc prepr_tc pdata_tc repr
122 = do vectDataConWorkers orig_tc vect_tc pdata_tc
123 buildPADict vect_tc prepr_tc pdata_tc repr
125 vectDataConWorkers :: TyCon -> TyCon -> TyCon -> VM ()
126 vectDataConWorkers orig_tc vect_tc arr_tc
128 . zipWith3 def_worker (tyConDataCons orig_tc) rep_tys
129 $ zipWith4 mk_data_con (tyConDataCons vect_tc)
132 (tail $ tails rep_tys)
133 mapM_ (uncurry hoistBinding) bs
135 tyvars = tyConTyVars vect_tc
136 var_tys = mkTyVarTys tyvars
137 ty_args = map Type var_tys
138 res_ty = mkTyConApp vect_tc var_tys
140 cons = tyConDataCons vect_tc
142 [arr_dc] = tyConDataCons arr_tc
144 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
147 mk_data_con con tys pre post
148 = liftM2 (,) (vect_data_con con)
149 (lift_data_con tys pre post (mkDataConTag con))
151 sel_replicate len tag
153 rep <- builtin (selReplicate arity)
154 return [rep `mkApps` [len, tag]]
156 | otherwise = return []
158 vect_data_con con = return $ mkConApp con ty_args
159 lift_data_con tys pre_tys post_tys tag
161 len <- builtin liftingContext
162 args <- mapM (newLocalVar (fsLit "xs"))
163 =<< mapM mkPDataType tys
165 sel <- sel_replicate (Var len) tag
167 pre <- mapM emptyPD (concat pre_tys)
168 post <- mapM emptyPD (concat post_tys)
170 return . mkLams (len : args)
171 . wrapFamInstBody arr_tc var_tys
173 $ ty_args ++ sel ++ pre ++ map Var args ++ post
175 def_worker data_con arg_tys mk_body
177 arity <- polyArity tyvars
180 . polyAbstract tyvars $ \args ->
181 liftM (mkLams (tyvars ++ args) . vectorised)
182 $ buildClosures tyvars [] arg_tys res_ty mk_body
184 raw_worker <- cloneId mkVectOcc orig_worker (exprType body)
185 let vect_worker = raw_worker `setIdUnfolding`
186 mkInlineUnfolding (Just arity) body
187 defGlobalVar orig_worker vect_worker
188 return (vect_worker, body)
190 orig_worker = dataConWorkId data_con