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.PArray.PData
44 -- PData is a type family that maps an element type onto the type
45 -- we use to hold an array of those elements.
46 pdataTyCon <- externalTyCon dph_PArray_PData (fsLit "PData")
48 -- PR is a type class that holds the primitive operators we can
49 -- apply to array data. Its functions take arrays in terms of PData types.
50 prClass <- externalClass dph_PArray_PData (fsLit "PR")
51 let prTyCon = classTyCon prClass
52 [prDataCon] = tyConDataCons prTyCon
55 -- From dph-common:Data.Array.Parallel.PArray.PRepr
56 preprTyCon <- externalTyCon dph_PArray_PRepr (fsLit "PRepr")
57 paClass <- externalClass dph_PArray_PRepr (fsLit "PA")
58 let paTyCon = classTyCon paClass
59 [paDataCon] = tyConDataCons paTyCon
60 paPRSel = classSCSelId paClass 0
62 replicatePDVar <- externalVar dph_PArray_PRepr (fsLit "replicatePD")
63 emptyPDVar <- externalVar dph_PArray_PRepr (fsLit "emptyPD")
64 packByTagPDVar <- externalVar dph_PArray_PRepr (fsLit "packByTagPD")
65 combines <- mapM (externalVar dph_PArray_PRepr)
66 [mkFastString ("combine" ++ show i ++ "PD")
67 | i <- [2..mAX_DPH_COMBINE]]
69 let combinePDVars = listArray (2, mAX_DPH_COMBINE) combines
72 -- From dph-common:Data.Array.Parallel.PArray.Scalar
73 -- Scalar is the class of scalar values.
74 -- The dictionary contains functions to coerce U.Arrays of scalars
75 -- to and from the PData representation.
76 scalarClass <- externalClass dph_PArray_Scalar (fsLit "Scalar")
79 -- From dph-common:Data.Array.Parallel.Lifted.PArray
80 -- A PArray (Parallel Array) holds the array length and some array elements
81 -- represented by the PData type family.
82 parrayTyCon <- externalTyCon dph_PArray_Base (fsLit "PArray")
83 let [parrayDataCon] = tyConDataCons parrayTyCon
85 -- From dph-common:Data.Array.Parallel.PArray.Types
86 voidTyCon <- externalTyCon dph_PArray_Types (fsLit "Void")
87 voidVar <- externalVar dph_PArray_Types (fsLit "void")
88 fromVoidVar <- externalVar dph_PArray_Types (fsLit "fromVoid")
89 wrapTyCon <- externalTyCon dph_PArray_Types (fsLit "Wrap")
90 sum_tcs <- mapM (externalTyCon dph_PArray_Types) (numbered "Sum" 2 mAX_DPH_SUM)
92 -- from dph-common:Data.Array.Parallel.PArray.PDataInstances
93 pvoidVar <- externalVar dph_PArray_PDataInstances (fsLit "pvoid")
94 punitVar <- externalVar dph_PArray_PDataInstances (fsLit "punit")
97 closureTyCon <- externalTyCon dph_Closure (fsLit ":->")
100 -- From dph-common:Data.Array.Parallel.Lifted.Unboxed
101 sel_tys <- mapM (externalType dph_Unboxed)
102 (numbered "Sel" 2 mAX_DPH_SUM)
104 sel_replicates <- mapM (externalFun dph_Unboxed)
105 (numbered_hash "replicateSel" 2 mAX_DPH_SUM)
107 sel_picks <- mapM (externalFun dph_Unboxed)
108 (numbered_hash "pickSel" 2 mAX_DPH_SUM)
110 sel_tags <- mapM (externalFun dph_Unboxed)
111 (numbered "tagsSel" 2 mAX_DPH_SUM)
113 sel_els <- mapM mk_elements
114 [(i,j) | i <- [2..mAX_DPH_SUM], j <- [0..i-1]]
117 let selTys = listArray (2, mAX_DPH_SUM) sel_tys
118 selReplicates = listArray (2, mAX_DPH_SUM) sel_replicates
119 selPicks = listArray (2, mAX_DPH_SUM) sel_picks
120 selTagss = listArray (2, mAX_DPH_SUM) sel_tags
121 selEls = array ((2,0), (mAX_DPH_SUM, mAX_DPH_SUM)) sel_els
122 sumTyCons = listArray (2, mAX_DPH_SUM) sum_tcs
126 closureVar <- externalVar dph_Closure (fsLit "closure")
127 applyVar <- externalVar dph_Closure (fsLit "$:")
128 liftedClosureVar <- externalVar dph_Closure (fsLit "liftedClosure")
129 liftedApplyVar <- externalVar dph_Closure (fsLit "liftedApply")
131 scalar_map <- externalVar dph_Scalar (fsLit "scalar_map")
132 scalar_zip2 <- externalVar dph_Scalar (fsLit "scalar_zipWith")
133 scalar_zips <- mapM (externalVar dph_Scalar)
134 (numbered "scalar_zipWith" 3 mAX_DPH_SCALAR_ARGS)
136 let scalarZips = listArray (1, mAX_DPH_SCALAR_ARGS)
137 (scalar_map : scalar_zip2 : scalar_zips)
139 closures <- mapM (externalVar dph_Closure)
140 (numbered "closure" 1 mAX_DPH_SCALAR_ARGS)
142 let closureCtrFuns = listArray (1, mAX_DPH_COMBINE) closures
144 liftingContext <- liftM (\u -> mkSysLocal (fsLit "lc") u intPrimTy)
149 , parrayTyCon = parrayTyCon
150 , parrayDataCon = parrayDataCon
151 , pdataTyCon = pdataTyCon
154 , paDataCon = paDataCon
156 , preprTyCon = preprTyCon
159 , prDataCon = prDataCon
160 , voidTyCon = voidTyCon
161 , wrapTyCon = wrapTyCon
163 , selReplicates = selReplicates
164 , selPicks = selPicks
165 , selTagss = selTagss
167 , sumTyCons = sumTyCons
168 , closureTyCon = closureTyCon
170 , pvoidVar = pvoidVar
171 , fromVoidVar = fromVoidVar
172 , punitVar = punitVar
173 , closureVar = closureVar
174 , applyVar = applyVar
175 , liftedClosureVar = liftedClosureVar
176 , liftedApplyVar = liftedApplyVar
177 , replicatePDVar = replicatePDVar
178 , emptyPDVar = emptyPDVar
179 , packByTagPDVar = packByTagPDVar
180 , combinePDVars = combinePDVars
181 , scalarClass = scalarClass
182 , scalarZips = scalarZips
183 , closureCtrFuns = closureCtrFuns
184 , liftingContext = liftingContext
187 -- Extract out all the modules we'll use.
188 -- These are the modules from the DPH base library that contain
189 -- the primitive array types and functions that vectorised code uses.
191 { dph_PArray_Base = dph_PArray_Base
192 , dph_PArray_Scalar = dph_PArray_Scalar
193 , dph_PArray_PRepr = dph_PArray_PRepr
194 , dph_PArray_PData = dph_PArray_PData
195 , dph_PArray_PDataInstances = dph_PArray_PDataInstances
196 , dph_PArray_Types = dph_PArray_Types
197 , dph_Closure = dph_Closure
198 , dph_Scalar = dph_Scalar
199 , dph_Unboxed = dph_Unboxed
203 load get_mod = dsLoadModule doc mod
206 doc = ppr mod <+> ptext (sLit "is a DPH module")
208 -- Make a list of numbered strings in some range, eg foo3, foo4, foo5
209 numbered :: String -> Int -> Int -> [FastString]
210 numbered pfx m n = [mkFastString (pfx ++ show i) | i <- [m..n]]
212 numbered_hash :: String -> Int -> Int -> [FastString]
213 numbered_hash pfx m n = [mkFastString (pfx ++ show i ++ "#") | i <- [m..n]]
215 mk_elements :: (Int, Int) -> DsM ((Int, Int), CoreExpr)
218 v <- externalVar dph_Unboxed
219 $ mkFastString ("elementsSel" ++ show i ++ "_" ++ show j ++ "#")
220 return ((i,j), Var v)
222 -- | Get the mapping of names in the Prelude to names in the DPH library.
224 initBuiltinVars :: Bool -- FIXME
225 -> Builtins -> DsM [(Var, Var)]
226 initBuiltinVars compilingDPH (Builtins { dphModules = mods })
228 uvars <- zipWithM externalVar umods ufs
229 vvars <- zipWithM externalVar vmods vfs
230 cvars <- zipWithM externalVar cmods cfs
231 return $ [(v,v) | v <- map dataConWorkId defaultDataConWorkers]
232 ++ zip (map dataConWorkId cons) cvars
235 (umods, ufs, vmods, vfs) = if compilingDPH then ([], [], [], []) else unzip4 (preludeVars mods)
236 (cons, cmods, cfs) = unzip3 (preludeDataCons mods)
238 defaultDataConWorkers :: [DataCon]
239 defaultDataConWorkers = [trueDataCon, falseDataCon, unitDataCon]
242 preludeDataCons :: Modules -> [(DataCon, Module, FastString)]
243 preludeDataCons (Modules { dph_Prelude_Tuple = dph_Prelude_Tuple })
244 = [mk_tup n dph_Prelude_Tuple (mkFastString $ "tup" ++ show n) | n <- [2..3]]
246 mk_tup n mod name = (tupleCon Boxed n, mod, name)
249 -- | Get a list of names to `TyCon`s in the mock prelude.
250 initBuiltinTyCons :: Builtins -> DsM [(Name, TyCon)]
253 -- parr <- externalTyCon dph_Prelude_PArr (fsLit "PArr")
254 dft_tcs <- defaultTyCons
255 return $ (tyConName funTyCon, closureTyCon bi)
256 : (parrTyConName, parrayTyCon bi)
259 : (tyConName $ parrayTyCon bi, parrayTyCon bi)
261 : [(tyConName tc, tc) | tc <- dft_tcs]
263 where defaultTyCons :: DsM [TyCon]
265 = do word8 <- dsLookupTyCon word8TyConName
266 return [intTyCon, boolTyCon, doubleTyCon, word8]
269 -- | Get a list of names to `DataCon`s in the mock prelude.
270 initBuiltinDataCons :: Builtins -> [(Name, DataCon)]
271 initBuiltinDataCons _
272 = [(dataConName dc, dc)| dc <- defaultDataCons]
273 where defaultDataCons :: [DataCon]
274 defaultDataCons = [trueDataCon, falseDataCon, unitDataCon]
277 -- | Get the names of all buildin instance functions for the PA class.
278 initBuiltinPAs :: Builtins -> (InstEnv, InstEnv) -> DsM [(Name, Var)]
279 initBuiltinPAs (Builtins { dphModules = mods }) insts
280 = liftM (initBuiltinDicts insts) (externalClass (dph_PArray_PRepr mods) (fsLit "PA"))
283 -- | Get the names of all builtin instance functions for the PR class.
284 initBuiltinPRs :: Builtins -> (InstEnv, InstEnv) -> DsM [(Name, Var)]
285 initBuiltinPRs (Builtins { dphModules = mods }) insts
286 = liftM (initBuiltinDicts insts) (externalClass (dph_PArray_PData mods) (fsLit "PR"))
289 -- | Get the names of all DPH instance functions for this class.
290 initBuiltinDicts :: (InstEnv, InstEnv) -> Class -> [(Name, Var)]
291 initBuiltinDicts insts cls = map find $ classInstances insts cls
293 find i | [Just tc] <- instanceRoughTcs i = (tc, instanceDFunId i)
294 | otherwise = pprPanic "Invalid DPH instance" (ppr i)
297 -- | Get a list of boxed `TyCons` in the mock prelude. This is Int only.
298 initBuiltinBoxedTyCons :: Builtins -> DsM [(Name, TyCon)]
299 initBuiltinBoxedTyCons
300 = return . builtinBoxedTyCons
301 where builtinBoxedTyCons :: Builtins -> [(Name, TyCon)]
303 = [(tyConName intPrimTyCon, intTyCon)]
305 -- | Get a list of all scalar functions in the mock prelude.
307 initBuiltinScalars :: Bool
308 -> Builtins -> DsM [Var]
309 initBuiltinScalars True _bi = return []
310 initBuiltinScalars False bi = mapM (uncurry externalVar) (preludeScalars $ dphModules bi)
312 -- | Lookup some variable given its name and the module that contains it.
313 externalVar :: Module -> FastString -> DsM Var
315 = dsLookupGlobalId =<< lookupOrig mod (mkVarOccFS fs)
318 -- | Like `externalVar` but wrap the `Var` in a `CoreExpr`
319 externalFun :: Module -> FastString -> DsM CoreExpr
321 = do var <- externalVar mod fs
325 -- | Lookup some `TyCon` given its name and the module that contains it.
326 externalTyCon :: Module -> FastString -> DsM TyCon
328 = dsLookupTyCon =<< lookupOrig mod (mkTcOccFS fs)
331 -- | Lookup some `Type` given its name and the module that contains it.
332 externalType :: Module -> FastString -> DsM Type
334 = do tycon <- externalTyCon mod fs
335 return $ mkTyConApp tycon []
338 -- | Lookup some `Class` given its name and the module that contains it.
339 externalClass :: Module -> FastString -> DsM Class
341 = dsLookupClass =<< lookupOrig mod (mkClsOccFS fs)