3 module Vectorise.Builtins.Initialise (
5 initBuiltins, initBuiltinVars, initBuiltinTyCons, initBuiltinDataCons,
6 initBuiltinPAs, initBuiltinPRs,
7 initBuiltinBoxedTyCons, initBuiltinScalars,
9 import Vectorise.Builtins.Base
10 import Vectorise.Builtins.Modules
11 import Vectorise.Builtins.Prelude
35 -- | Create the initial map of builtin types and functions.
37 :: PackageId -- ^ package id the builtins are in, eg dph-common
41 = do mapM_ load dph_Orphans
43 -- From dph-common:Data.Array.Parallel.Lifted.PArray
44 parrayTyCon <- externalTyCon dph_PArray (fsLit "PArray")
45 let [parrayDataCon] = tyConDataCons parrayTyCon
47 pdataTyCon <- externalTyCon dph_PArray (fsLit "PData")
48 paClass <- externalClass dph_PArray (fsLit "PA")
49 let paTyCon = classTyCon paClass
50 [paDataCon] = tyConDataCons paTyCon
51 paPRSel = classSCSelId paClass 0
53 preprTyCon <- externalTyCon dph_PArray (fsLit "PRepr")
54 prClass <- externalClass dph_PArray (fsLit "PR")
55 let prTyCon = classTyCon prClass
56 [prDataCon] = tyConDataCons prTyCon
58 closureTyCon <- externalTyCon dph_Closure (fsLit ":->")
60 -- From dph-common:Data.Array.Parallel.Lifted.Repr
61 voidTyCon <- externalTyCon dph_Repr (fsLit "Void")
62 wrapTyCon <- externalTyCon dph_Repr (fsLit "Wrap")
64 -- From dph-common:Data.Array.Parallel.Lifted.Unboxed
65 sel_tys <- mapM (externalType dph_Unboxed)
66 (numbered "Sel" 2 mAX_DPH_SUM)
68 sel_replicates <- mapM (externalFun dph_Unboxed)
69 (numbered_hash "replicateSel" 2 mAX_DPH_SUM)
71 sel_picks <- mapM (externalFun dph_Unboxed)
72 (numbered_hash "pickSel" 2 mAX_DPH_SUM)
74 sel_tags <- mapM (externalFun dph_Unboxed)
75 (numbered "tagsSel" 2 mAX_DPH_SUM)
77 sel_els <- mapM mk_elements
78 [(i,j) | i <- [2..mAX_DPH_SUM], j <- [0..i-1]]
80 sum_tcs <- mapM (externalTyCon dph_Repr)
81 (numbered "Sum" 2 mAX_DPH_SUM)
83 let selTys = listArray (2, mAX_DPH_SUM) sel_tys
84 selReplicates = listArray (2, mAX_DPH_SUM) sel_replicates
85 selPicks = listArray (2, mAX_DPH_SUM) sel_picks
86 selTagss = listArray (2, mAX_DPH_SUM) sel_tags
87 selEls = array ((2,0), (mAX_DPH_SUM, mAX_DPH_SUM)) sel_els
88 sumTyCons = listArray (2, mAX_DPH_SUM) sum_tcs
91 voidVar <- externalVar dph_Repr (fsLit "void")
92 pvoidVar <- externalVar dph_Repr (fsLit "pvoid")
93 fromVoidVar <- externalVar dph_Repr (fsLit "fromVoid")
94 punitVar <- externalVar dph_Repr (fsLit "punit")
95 closureVar <- externalVar dph_Closure (fsLit "closure")
96 applyVar <- externalVar dph_Closure (fsLit "$:")
97 liftedClosureVar <- externalVar dph_Closure (fsLit "liftedClosure")
98 liftedApplyVar <- externalVar dph_Closure (fsLit "liftedApply")
99 replicatePDVar <- externalVar dph_PArray (fsLit "replicatePD")
100 emptyPDVar <- externalVar dph_PArray (fsLit "emptyPD")
101 packByTagPDVar <- externalVar dph_PArray (fsLit "packByTagPD")
103 combines <- mapM (externalVar dph_PArray)
104 [mkFastString ("combine" ++ show i ++ "PD")
105 | i <- [2..mAX_DPH_COMBINE]]
106 let combinePDVars = listArray (2, mAX_DPH_COMBINE) combines
108 scalarClass <- externalClass dph_PArray (fsLit "Scalar")
109 scalar_map <- externalVar dph_Scalar (fsLit "scalar_map")
110 scalar_zip2 <- externalVar dph_Scalar (fsLit "scalar_zipWith")
111 scalar_zips <- mapM (externalVar dph_Scalar)
112 (numbered "scalar_zipWith" 3 mAX_DPH_SCALAR_ARGS)
114 let scalarZips = listArray (1, mAX_DPH_SCALAR_ARGS)
115 (scalar_map : scalar_zip2 : scalar_zips)
117 closures <- mapM (externalVar dph_Closure)
118 (numbered "closure" 1 mAX_DPH_SCALAR_ARGS)
120 let closureCtrFuns = listArray (1, mAX_DPH_COMBINE) closures
122 liftingContext <- liftM (\u -> mkSysLocal (fsLit "lc") u intPrimTy)
127 , parrayTyCon = parrayTyCon
128 , parrayDataCon = parrayDataCon
129 , pdataTyCon = pdataTyCon
132 , paDataCon = paDataCon
134 , preprTyCon = preprTyCon
137 , prDataCon = prDataCon
138 , voidTyCon = voidTyCon
139 , wrapTyCon = wrapTyCon
141 , selReplicates = selReplicates
142 , selPicks = selPicks
143 , selTagss = selTagss
145 , sumTyCons = sumTyCons
146 , closureTyCon = closureTyCon
148 , pvoidVar = pvoidVar
149 , fromVoidVar = fromVoidVar
150 , punitVar = punitVar
151 , closureVar = closureVar
152 , applyVar = applyVar
153 , liftedClosureVar = liftedClosureVar
154 , liftedApplyVar = liftedApplyVar
155 , replicatePDVar = replicatePDVar
156 , emptyPDVar = emptyPDVar
157 , packByTagPDVar = packByTagPDVar
158 , combinePDVars = combinePDVars
159 , scalarClass = scalarClass
160 , scalarZips = scalarZips
161 , closureCtrFuns = closureCtrFuns
162 , liftingContext = liftingContext
166 dph_PArray = dph_PArray
167 , dph_Repr = dph_Repr
168 , dph_Closure = dph_Closure
169 , dph_Scalar = dph_Scalar
170 , dph_Unboxed = dph_Unboxed
174 load get_mod = dsLoadModule doc mod
177 doc = ppr mod <+> ptext (sLit "is a DPH module")
179 -- Make a list of numbered strings in some range, eg foo3, foo4, foo5
180 numbered :: String -> Int -> Int -> [FastString]
181 numbered pfx m n = [mkFastString (pfx ++ show i) | i <- [m..n]]
183 numbered_hash :: String -> Int -> Int -> [FastString]
184 numbered_hash pfx m n = [mkFastString (pfx ++ show i ++ "#") | i <- [m..n]]
186 mk_elements :: (Int, Int) -> DsM ((Int, Int), CoreExpr)
189 v <- externalVar dph_Unboxed
190 $ mkFastString ("elementsSel" ++ show i ++ "_" ++ show j ++ "#")
191 return ((i,j), Var v)
193 -- | Get the mapping of names in the Prelude to names in the DPH library.
195 initBuiltinVars :: Bool -- FIXME
196 -> Builtins -> DsM [(Var, Var)]
197 initBuiltinVars compilingDPH (Builtins { dphModules = mods })
199 uvars <- zipWithM externalVar umods ufs
200 vvars <- zipWithM externalVar vmods vfs
201 cvars <- zipWithM externalVar cmods cfs
202 return $ [(v,v) | v <- map dataConWorkId defaultDataConWorkers]
203 ++ zip (map dataConWorkId cons) cvars
206 (umods, ufs, vmods, vfs) = if compilingDPH then ([], [], [], []) else unzip4 (preludeVars mods)
207 (cons, cmods, cfs) = unzip3 (preludeDataCons mods)
209 defaultDataConWorkers :: [DataCon]
210 defaultDataConWorkers = [trueDataCon, falseDataCon, unitDataCon]
213 preludeDataCons :: Modules -> [(DataCon, Module, FastString)]
214 preludeDataCons (Modules { dph_Prelude_Tuple = dph_Prelude_Tuple })
215 = [mk_tup n dph_Prelude_Tuple (mkFastString $ "tup" ++ show n) | n <- [2..3]]
217 mk_tup n mod name = (tupleCon Boxed n, mod, name)
220 -- | Get a list of names to `TyCon`s in the mock prelude.
221 initBuiltinTyCons :: Builtins -> DsM [(Name, TyCon)]
224 -- parr <- externalTyCon dph_Prelude_PArr (fsLit "PArr")
225 dft_tcs <- defaultTyCons
226 return $ (tyConName funTyCon, closureTyCon bi)
227 : (parrTyConName, parrayTyCon bi)
230 : (tyConName $ parrayTyCon bi, parrayTyCon bi)
232 : [(tyConName tc, tc) | tc <- dft_tcs]
234 where defaultTyCons :: DsM [TyCon]
236 = do word8 <- dsLookupTyCon word8TyConName
237 return [intTyCon, boolTyCon, doubleTyCon, word8]
240 -- | Get a list of names to `DataCon`s in the mock prelude.
241 initBuiltinDataCons :: Builtins -> [(Name, DataCon)]
242 initBuiltinDataCons _
243 = [(dataConName dc, dc)| dc <- defaultDataCons]
244 where defaultDataCons :: [DataCon]
245 defaultDataCons = [trueDataCon, falseDataCon, unitDataCon]
248 -- | Get the names of all buildin instance functions for the PA class.
249 initBuiltinPAs :: Builtins -> (InstEnv, InstEnv) -> DsM [(Name, Var)]
250 initBuiltinPAs (Builtins { dphModules = mods }) insts
251 = liftM (initBuiltinDicts insts) (externalClass (dph_PArray mods) (fsLit "PA"))
254 -- | Get the names of all builtin instance functions for the PR class.
255 initBuiltinPRs :: Builtins -> (InstEnv, InstEnv) -> DsM [(Name, Var)]
256 initBuiltinPRs (Builtins { dphModules = mods }) insts
257 = liftM (initBuiltinDicts insts) (externalClass (dph_PArray mods) (fsLit "PR"))
260 -- | Get the names of all DPH instance functions for this class.
261 initBuiltinDicts :: (InstEnv, InstEnv) -> Class -> [(Name, Var)]
262 initBuiltinDicts insts cls = map find $ classInstances insts cls
264 find i | [Just tc] <- instanceRoughTcs i = (tc, instanceDFunId i)
265 | otherwise = pprPanic "Invalid DPH instance" (ppr i)
268 -- | Get a list of boxed `TyCons` in the mock prelude. This is Int only.
269 initBuiltinBoxedTyCons :: Builtins -> DsM [(Name, TyCon)]
270 initBuiltinBoxedTyCons
271 = return . builtinBoxedTyCons
272 where builtinBoxedTyCons :: Builtins -> [(Name, TyCon)]
274 = [(tyConName intPrimTyCon, intTyCon)]
276 -- | Get a list of all scalar functions in the mock prelude.
278 initBuiltinScalars :: Bool
279 -> Builtins -> DsM [Var]
280 initBuiltinScalars True _bi = return []
281 initBuiltinScalars False bi = mapM (uncurry externalVar) (preludeScalars $ dphModules bi)
283 -- | Lookup some variable given its name and the module that contains it.
284 externalVar :: Module -> FastString -> DsM Var
286 = dsLookupGlobalId =<< lookupOrig mod (mkVarOccFS fs)
289 -- | Like `externalVar` but wrap the `Var` in a `CoreExpr`
290 externalFun :: Module -> FastString -> DsM CoreExpr
292 = do var <- externalVar mod fs
296 -- | Lookup some `TyCon` given its name and the module that contains it.
297 externalTyCon :: Module -> FastString -> DsM TyCon
299 = dsLookupTyCon =<< lookupOrig mod (mkTcOccFS fs)
302 -- | Lookup some `Type` given its name and the module that contains it.
303 externalType :: Module -> FastString -> DsM Type
305 = do tycon <- externalTyCon mod fs
306 return $ mkTyConApp tycon []
309 -- | Lookup some `Class` given its name and the module that contains it.
310 externalClass :: Module -> FastString -> DsM Class
312 = dsLookupClass =<< lookupOrig mod (mkClsOccFS fs)