1 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
2 #if __GLASGOW_HASKELL__ >= 611
3 {-# OPTIONS_GHC -XNoMonoLocalBinds #-}
5 -- Roman likes local bindings
6 -- If this module lives on I'd like to get rid of this flag in due course
8 module Vectorise.Type.Env (
14 import Vectorise.Monad
15 import Vectorise.Builtins
16 import Vectorise.Type.TyConDecl
17 import Vectorise.Type.Classify
18 import Vectorise.Type.PADict
19 import Vectorise.Type.PData
20 import Vectorise.Type.PRepr
21 import Vectorise.Type.Repr
22 import Vectorise.Utils
48 dtrace s x = if debug then pprTrace "VectType" s x else x
51 -- | Vectorise a type environment.
52 -- The type environment contains all the type things defined in a module.
55 -> VM ( TypeEnv -- Vectorised type environment.
56 , [FamInst] -- New type family instances.
57 , [(Var, CoreExpr)]) -- New top level bindings.
62 cs <- readGEnv $ mk_map . global_tycons
64 -- Split the list of TyCons into the ones we have to vectorise vs the
65 -- ones we can pass through unchanged. We also pass through algebraic
66 -- types that use non Haskell98 features, as we don't handle those.
67 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
68 keep_dcs = concatMap tyConDataCons keep_tcs
70 zipWithM_ defTyCon keep_tcs keep_tcs
71 zipWithM_ defDataCon keep_dcs keep_dcs
73 new_tcs <- vectTyConDecls conv_tcs
75 let orig_tcs = keep_tcs ++ conv_tcs
77 -- We don't need to make new representation types for dictionary
78 -- constructors. The constructors are always fully applied, and we don't
79 -- need to lift them to arrays as a dictionary of a particular type
80 -- always has the same value.
81 let vect_tcs = filter (not . isClassTyCon)
84 (_, binds, inst_tcs) <- fixV $ \ ~(dfuns', _, _) ->
86 defTyConPAs (zipLazy vect_tcs dfuns')
87 reprs <- mapM tyConRepr vect_tcs
88 repr_tcs <- zipWith3M buildPReprTyCon orig_tcs vect_tcs reprs
89 pdata_tcs <- zipWith3M buildPDataTyCon orig_tcs vect_tcs reprs
92 $ zipWith5 buildTyConBindings
100 return (dfuns, binds, repr_tcs ++ pdata_tcs)
102 let all_new_tcs = new_tcs ++ inst_tcs
104 let new_env = extendTypeEnvList env
105 (map ATyCon all_new_tcs
106 ++ [ADataCon dc | tc <- all_new_tcs
107 , dc <- tyConDataCons tc])
109 return (new_env, map mkLocalFamInst inst_tcs, binds)
111 tycons = typeEnvTyCons env
112 groups = tyConGroups tycons
114 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
118 buildTyConBindings :: TyCon -> TyCon -> TyCon -> TyCon -> SumRepr -> VM Var
119 buildTyConBindings orig_tc vect_tc prepr_tc pdata_tc repr
120 = do vectDataConWorkers orig_tc vect_tc pdata_tc
121 buildPADict vect_tc prepr_tc pdata_tc repr
124 vectDataConWorkers :: TyCon -> TyCon -> TyCon -> VM ()
125 vectDataConWorkers orig_tc vect_tc arr_tc
127 . zipWith3 def_worker (tyConDataCons orig_tc) rep_tys
128 $ zipWith4 mk_data_con (tyConDataCons vect_tc)
131 (tail $ tails rep_tys)
132 mapM_ (uncurry hoistBinding) bs
134 tyvars = tyConTyVars vect_tc
135 var_tys = mkTyVarTys tyvars
136 ty_args = map Type var_tys
137 res_ty = mkTyConApp vect_tc var_tys
139 cons = tyConDataCons vect_tc
141 [arr_dc] = tyConDataCons arr_tc
143 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
146 mk_data_con con tys pre post
147 = liftM2 (,) (vect_data_con con)
148 (lift_data_con tys pre post (mkDataConTag con))
150 sel_replicate len tag
152 rep <- builtin (selReplicate arity)
153 return [rep `mkApps` [len, tag]]
155 | otherwise = return []
157 vect_data_con con = return $ mkConApp con ty_args
158 lift_data_con tys pre_tys post_tys tag
160 len <- builtin liftingContext
161 args <- mapM (newLocalVar (fsLit "xs"))
162 =<< mapM mkPDataType tys
164 sel <- sel_replicate (Var len) tag
166 pre <- mapM emptyPD (concat pre_tys)
167 post <- mapM emptyPD (concat post_tys)
169 return . mkLams (len : args)
170 . wrapFamInstBody arr_tc var_tys
172 $ ty_args ++ sel ++ pre ++ map Var args ++ post
174 def_worker data_con arg_tys mk_body
176 arity <- polyArity tyvars
179 . polyAbstract tyvars $ \args ->
180 liftM (mkLams (tyvars ++ args) . vectorised)
181 $ buildClosures tyvars [] arg_tys res_ty mk_body
183 raw_worker <- cloneId mkVectOcc orig_worker (exprType body)
184 let vect_worker = raw_worker `setIdUnfolding`
185 mkInlineRule body (Just arity)
186 defGlobalVar orig_worker vect_worker
187 return (vect_worker, body)
189 orig_worker = dataConWorkId data_con