More vectorisation-related built-ins
[ghc.git] / compiler / vectorise / VectBuiltIn.hs
1 {-# OPTIONS -w #-}
2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
6 -- for details
7
8 module VectBuiltIn (
9 Builtins(..), sumTyCon, prodTyCon, combinePAVar,
10 initBuiltins, initBuiltinTyCons, initBuiltinPAs, initBuiltinPRs,
11
12 primMethod, primPArray
13 ) where
14
15 #include "HsVersions.h"
16
17 import DsMonad
18 import IfaceEnv ( lookupOrig )
19
20 import Module ( Module )
21 import DataCon ( DataCon )
22 import TyCon ( TyCon, tyConName, tyConDataCons )
23 import Var ( Var )
24 import Id ( mkSysLocal )
25 import Name ( Name, getOccString )
26 import NameEnv
27 import OccName
28
29 import TypeRep ( funTyCon )
30 import Type ( Type )
31 import TysPrim
32 import TysWiredIn ( unitTyCon, tupleTyCon, intTyConName )
33 import Module
34 import BasicTypes ( Boxity(..) )
35
36 import FastString
37 import Outputable
38
39 import Data.Array
40 import Control.Monad ( liftM, zipWithM )
41
42 mAX_NDP_PROD :: Int
43 mAX_NDP_PROD = 3
44
45 mAX_NDP_SUM :: Int
46 mAX_NDP_SUM = 3
47
48 mAX_NDP_COMBINE :: Int
49 mAX_NDP_COMBINE = 2
50
51 mkNDPModule :: FastString -> Module
52 mkNDPModule m = mkModule ndpPackageId (mkModuleNameFS m)
53
54 nDP_PARRAY = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.PArray")
55 nDP_REPR = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Repr")
56 nDP_CLOSURE = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Closure")
57 nDP_PRIM = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Prim")
58 nDP_INSTANCES = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Instances")
59
60 data Builtins = Builtins {
61 parrayTyCon :: TyCon
62 , paTyCon :: TyCon
63 , paDataCon :: DataCon
64 , preprTyCon :: TyCon
65 , prTyCon :: TyCon
66 , prDataCon :: DataCon
67 , parrayIntPrimTyCon :: TyCon
68 , parrayBoolPrimTyCon :: TyCon
69 , voidTyCon :: TyCon
70 , wrapTyCon :: TyCon
71 , sumTyCons :: Array Int TyCon
72 , closureTyCon :: TyCon
73 , voidVar :: Var
74 , mkPRVar :: Var
75 , mkClosureVar :: Var
76 , applyClosureVar :: Var
77 , mkClosurePVar :: Var
78 , applyClosurePVar :: Var
79 , replicatePAIntPrimVar :: Var
80 , upToPAIntPrimVar :: Var
81 , selectPAIntPrimVar :: Var
82 , truesPABoolPrimVar :: Var
83 , lengthPAVar :: Var
84 , replicatePAVar :: Var
85 , emptyPAVar :: Var
86 , packPAVar :: Var
87 , combinePAVars :: Array Int Var
88 , liftingContext :: Var
89 }
90
91 sumTyCon :: Int -> Builtins -> TyCon
92 sumTyCon n bi
93 | n >= 2 && n <= mAX_NDP_SUM = sumTyCons bi ! n
94 | otherwise = pprPanic "sumTyCon" (ppr n)
95
96 prodTyCon :: Int -> Builtins -> TyCon
97 prodTyCon n bi
98 | n == 1 = wrapTyCon bi
99 | n >= 0 && n <= mAX_NDP_PROD = tupleTyCon Boxed n
100 | otherwise = pprPanic "prodTyCon" (ppr n)
101
102 combinePAVar :: Int -> Builtins -> Var
103 combinePAVar n bi
104 | n >= 2 && n <= mAX_NDP_COMBINE = combinePAVars bi ! n
105 | otherwise = pprPanic "combinePAVar" (ppr n)
106
107 initBuiltins :: DsM Builtins
108 initBuiltins
109 = do
110 parrayTyCon <- externalTyCon nDP_PARRAY FSLIT("PArray")
111 paTyCon <- externalTyCon nDP_PARRAY FSLIT("PA")
112 let [paDataCon] = tyConDataCons paTyCon
113 preprTyCon <- externalTyCon nDP_PARRAY FSLIT("PRepr")
114 prTyCon <- externalTyCon nDP_PARRAY FSLIT("PR")
115 let [prDataCon] = tyConDataCons prTyCon
116 parrayIntPrimTyCon <- externalTyCon nDP_PRIM FSLIT("PArray_Int#")
117 parrayBoolPrimTyCon <- externalTyCon nDP_PRIM FSLIT("PArray_Bool#")
118 closureTyCon <- externalTyCon nDP_CLOSURE FSLIT(":->")
119
120 voidTyCon <- externalTyCon nDP_REPR FSLIT("Void")
121 wrapTyCon <- externalTyCon nDP_REPR FSLIT("Wrap")
122 sum_tcs <- mapM (externalTyCon nDP_REPR)
123 [mkFastString ("Sum" ++ show i) | i <- [2..mAX_NDP_SUM]]
124
125 let sumTyCons = listArray (2, mAX_NDP_SUM) sum_tcs
126
127 voidVar <- externalVar nDP_REPR FSLIT("void")
128 mkPRVar <- externalVar nDP_PARRAY FSLIT("mkPR")
129 mkClosureVar <- externalVar nDP_CLOSURE FSLIT("mkClosure")
130 applyClosureVar <- externalVar nDP_CLOSURE FSLIT("$:")
131 mkClosurePVar <- externalVar nDP_CLOSURE FSLIT("mkClosureP")
132 applyClosurePVar <- externalVar nDP_CLOSURE FSLIT("$:^")
133 replicatePAIntPrimVar <- externalVar nDP_PRIM FSLIT("replicatePA_Int#")
134 upToPAIntPrimVar <- externalVar nDP_PRIM FSLIT("upToPA_Int#")
135 selectPAIntPrimVar <- externalVar nDP_PRIM FSLIT("selectPA_Int#")
136 truesPABoolPrimVar <- externalVar nDP_PRIM FSLIT("truesPA_Bool#")
137 lengthPAVar <- externalVar nDP_PARRAY FSLIT("lengthPA")
138 replicatePAVar <- externalVar nDP_PARRAY FSLIT("replicatePA")
139 emptyPAVar <- externalVar nDP_PARRAY FSLIT("emptyPA")
140 packPAVar <- externalVar nDP_PARRAY FSLIT("packPA")
141
142 combines <- mapM (externalVar nDP_PARRAY)
143 [mkFastString ("combine" ++ show i ++ "PA")
144 | i <- [2..mAX_NDP_COMBINE]]
145 let combinePAVars = listArray (2, mAX_NDP_COMBINE) combines
146
147 liftingContext <- liftM (\u -> mkSysLocal FSLIT("lc") u intPrimTy)
148 newUnique
149
150 return $ Builtins {
151 parrayTyCon = parrayTyCon
152 , paTyCon = paTyCon
153 , paDataCon = paDataCon
154 , preprTyCon = preprTyCon
155 , prTyCon = prTyCon
156 , prDataCon = prDataCon
157 , parrayIntPrimTyCon = parrayIntPrimTyCon
158 , parrayBoolPrimTyCon = parrayBoolPrimTyCon
159 , voidTyCon = voidTyCon
160 , wrapTyCon = wrapTyCon
161 , sumTyCons = sumTyCons
162 , closureTyCon = closureTyCon
163 , voidVar = voidVar
164 , mkPRVar = mkPRVar
165 , mkClosureVar = mkClosureVar
166 , applyClosureVar = applyClosureVar
167 , mkClosurePVar = mkClosurePVar
168 , applyClosurePVar = applyClosurePVar
169 , replicatePAIntPrimVar = replicatePAIntPrimVar
170 , upToPAIntPrimVar = upToPAIntPrimVar
171 , selectPAIntPrimVar = selectPAIntPrimVar
172 , truesPABoolPrimVar = truesPABoolPrimVar
173 , lengthPAVar = lengthPAVar
174 , replicatePAVar = replicatePAVar
175 , emptyPAVar = emptyPAVar
176 , packPAVar = packPAVar
177 , combinePAVars = combinePAVars
178 , liftingContext = liftingContext
179 }
180
181 initBuiltinTyCons :: Builtins -> [(Name, TyCon)]
182 initBuiltinTyCons bi = [(tyConName funTyCon, closureTyCon bi)]
183
184 initBuiltinDicts :: [(Name, Module, FastString)] -> DsM [(Name, Var)]
185 initBuiltinDicts ps
186 = do
187 dicts <- zipWithM externalVar mods fss
188 return $ zip tcs dicts
189 where
190 (tcs, mods, fss) = unzip3 ps
191
192 initBuiltinPAs = initBuiltinDicts . builtinPAs
193
194 builtinPAs :: Builtins -> [(Name, Module, FastString)]
195 builtinPAs bi
196 = [
197 mk (tyConName $ closureTyCon bi) nDP_CLOSURE FSLIT("dPA_Clo")
198 , mk (tyConName $ voidTyCon bi) nDP_REPR FSLIT("dPA_Void")
199 , mk unitTyConName nDP_INSTANCES FSLIT("dPA_Unit")
200
201 , mk intTyConName nDP_INSTANCES FSLIT("dPA_Int")
202 ]
203 ++ tups
204 where
205 mk name mod fs = (name, mod, fs)
206
207 tups = map mk_tup [2..3]
208 mk_tup n = mk (tyConName $ tupleTyCon Boxed n)
209 nDP_INSTANCES
210 (mkFastString $ "dPA_" ++ show n)
211
212 initBuiltinPRs = initBuiltinDicts . builtinPRs
213
214 builtinPRs :: Builtins -> [(Name, Module, FastString)]
215 builtinPRs bi =
216 [
217 mk (tyConName unitTyCon) nDP_REPR FSLIT("dPR_Unit")
218 , mk (tyConName $ voidTyCon bi) nDP_REPR FSLIT("dPR_Void")
219 , mk (tyConName $ wrapTyCon bi) nDP_REPR FSLIT("dPR_Wrap")
220 , mk (tyConName $ closureTyCon bi) nDP_CLOSURE FSLIT("dPR_Clo")
221
222 -- temporary
223 , mk intTyConName nDP_INSTANCES FSLIT("dPR_Int")
224 ]
225
226 ++ map mk_sum [2..mAX_NDP_SUM]
227 ++ map mk_prod [2..mAX_NDP_PROD]
228 where
229 mk name mod fs = (name, mod, fs)
230
231 mk_sum n = (tyConName $ sumTyCon n bi, nDP_REPR,
232 mkFastString ("dPR_Sum" ++ show n))
233
234 mk_prod n = (tyConName $ prodTyCon n bi, nDP_REPR,
235 mkFastString ("dPR_" ++ show n))
236
237 externalVar :: Module -> FastString -> DsM Var
238 externalVar mod fs
239 = dsLookupGlobalId =<< lookupOrig mod (mkVarOccFS fs)
240
241 externalTyCon :: Module -> FastString -> DsM TyCon
242 externalTyCon mod fs
243 = dsLookupTyCon =<< lookupOrig mod (mkOccNameFS tcName fs)
244
245 unitTyConName = tyConName unitTyCon
246
247
248 primMethod :: TyCon -> String -> DsM (Maybe Var)
249 primMethod tycon method
250 | Just suffix <- lookupNameEnv prim_ty_cons (tyConName tycon)
251 = liftM Just
252 $ dsLookupGlobalId =<< lookupOrig nDP_PRIM (mkVarOcc $ method ++ suffix)
253
254 | otherwise = return Nothing
255
256 primPArray :: TyCon -> DsM (Maybe TyCon)
257 primPArray tycon
258 | Just suffix <- lookupNameEnv prim_ty_cons (tyConName tycon)
259 = liftM Just
260 $ dsLookupTyCon =<< lookupOrig nDP_PRIM (mkOccName tcName $ "PArray" ++ suffix)
261
262 | otherwise = return Nothing
263
264 prim_ty_cons = mkNameEnv [mk_prim intPrimTyCon]
265 where
266 mk_prim tycon = (tyConName tycon, '_' : getOccString tycon)