Fix some small things broken with the last merge.
[ghc-hetmet.git] / compiler / vectorise / Vectorise / Builtins / Base.hs
1
2 -- | Builtin types and functions used by the vectoriser.
3 --   These are all defined in the DPH package.
4 module Vectorise.Builtins.Base (
5         -- * Hard config
6         mAX_DPH_PROD,
7         mAX_DPH_SUM,
8         mAX_DPH_COMBINE,
9         mAX_DPH_SCALAR_ARGS,
10         
11         -- * Builtins
12         Builtins(..),
13         indexBuiltin,
14         
15         -- * Projections
16         selTy,
17         selReplicate,
18         selPick,
19         selTags,
20         selElements,
21         sumTyCon,
22         prodTyCon,
23         prodDataCon,
24         combinePDVar,
25         scalarZip,
26         closureCtrFun
27 ) where
28 import Vectorise.Builtins.Modules
29 import BasicTypes
30 import Class
31 import CoreSyn
32 import TysWiredIn
33 import Type
34 import TyCon
35 import DataCon
36 import Outputable
37 import Data.Array
38
39
40 -- Numbers of things exported by the DPH library.
41 mAX_DPH_PROD :: Int
42 mAX_DPH_PROD = 5
43
44 mAX_DPH_SUM :: Int
45 mAX_DPH_SUM = 2
46
47 mAX_DPH_COMBINE :: Int
48 mAX_DPH_COMBINE = 2
49
50 mAX_DPH_SCALAR_ARGS :: Int
51 mAX_DPH_SCALAR_ARGS = 3
52
53
54 -- | Holds the names of the builtin types and functions used by the vectoriser.
55 data Builtins 
56         = Builtins 
57         { dphModules       :: Modules
58
59         -- From dph-common:Data.Array.Parallel.Lifted.PArray
60         , parrayTyCon      :: TyCon                     -- ^ PArray
61         , parrayDataCon    :: DataCon                   -- ^ PArray
62         , pdataTyCon       :: TyCon                     -- ^ PData
63         , paClass          :: Class                     -- ^ PA
64         , paTyCon          :: TyCon                     -- ^ PA
65         , paDataCon        :: DataCon                   -- ^ PA
66         , paPRSel          :: Var                       -- ^ PA
67         , preprTyCon       :: TyCon                     -- ^ PRepr
68         , prClass          :: Class                     -- ^ PR
69         , prTyCon          :: TyCon                     -- ^ PR
70         , prDataCon        :: DataCon                   -- ^ PR
71         , replicatePDVar   :: Var                       -- ^ replicatePD
72         , emptyPDVar       :: Var                       -- ^ emptyPD
73         , packByTagPDVar   :: Var                       -- ^ packByTagPD
74         , combinePDVars    :: Array Int Var             -- ^ combinePD
75         , scalarClass      :: Class                     -- ^ Scalar
76
77         -- From dph-common:Data.Array.Parallel.Lifted.Closure
78         , closureTyCon     :: TyCon                     -- ^ :->
79         , closureVar       :: Var                       -- ^ closure
80         , applyVar         :: Var                       -- ^ $: 
81         , liftedClosureVar :: Var                       -- ^ liftedClosure
82         , liftedApplyVar   :: Var                       -- ^ liftedApply
83         , closureCtrFuns   :: Array Int Var             -- ^ closure1 .. closure2
84
85         -- From dph-common:Data.Array.Parallel.Lifted.Repr
86         , voidTyCon        :: TyCon                     -- ^ Void
87         , wrapTyCon        :: TyCon                     -- ^ Wrap
88         , sumTyCons        :: Array Int TyCon           -- ^ Sum2 .. Sum3
89         , voidVar          :: Var                       -- ^ void
90         , pvoidVar         :: Var                       -- ^ pvoid
91         , fromVoidVar      :: Var                       -- ^ fromVoid
92         , punitVar         :: Var                       -- ^ punit
93
94         -- From dph-common:Data.Array.Parallel.Lifted.Selector
95         , selTys           :: Array Int Type            -- ^ Sel2
96         , selReplicates    :: Array Int CoreExpr        -- ^ replicate2
97         , selPicks         :: Array Int CoreExpr        -- ^ pick2
98         , selTagss         :: Array Int CoreExpr        -- ^ tagsSel2
99         , selEls           :: Array (Int, Int) CoreExpr -- ^ elementsSel2_0 .. elementsSel_2_1
100
101         -- From dph-common:Data.Array.Parallel.Lifted.Scalar
102         -- NOTE: map is counted as a zipWith fn with one argument array.
103         , scalarZips       :: Array Int Var             -- ^ map, zipWith, zipWith3
104
105         -- A Fresh variable
106         , liftingContext   :: Var                       -- ^ lc
107         }
108
109
110 -- | Get an element from one of the arrays of contained by a `Builtins`.
111 --   If the indexed thing is not in the array then panic.
112 indexBuiltin 
113         :: (Ix i, Outputable i) 
114         => String                       -- ^ Name of the selector we've used, for panic messages.
115         -> (Builtins -> Array i a)      -- ^ Field selector for the `Builtins`.
116         -> i                            -- ^ Index into the array.
117         -> Builtins 
118         -> a
119
120 indexBuiltin fn f i bi
121   | inRange (bounds xs) i = xs ! i
122   | otherwise             
123   = pprSorry "Vectorise.Builtins.indexBuiltin" 
124         (vcat   [ text ""
125                 , text "DPH builtin function '" <> text fn <> text "' of size '" <> ppr i <> text "' is not yet implemented."
126                 , text "This function does not appear in your source program, but it is needed"
127                 , text "to compile your code in the backend. This is a known, current limitation"
128                 , text "of DPH. If you want it to to work you should send mail to cvs-ghc@haskell.org"
129                 , text "and ask what you can do to help (it might involve some GHC hacking)."])
130
131   where xs = f bi
132
133
134 -- Projections ----------------------------------------------------------------
135 -- We use these wrappers instead of indexing the `Builtin` structure directly
136 -- because they give nicer panic messages if the indexed thing cannot be found.
137
138 selTy :: Int -> Builtins -> Type
139 selTy           = indexBuiltin "selTy" selTys
140
141 selReplicate :: Int -> Builtins -> CoreExpr
142 selReplicate    = indexBuiltin "selReplicate" selReplicates 
143
144 selPick :: Int -> Builtins -> CoreExpr
145 selPick         = indexBuiltin "selPick" selPicks
146
147 selTags :: Int -> Builtins -> CoreExpr
148 selTags         = indexBuiltin "selTags" selTagss
149
150 selElements :: Int -> Int -> Builtins -> CoreExpr
151 selElements i j = indexBuiltin "selElements" selEls (i,j)
152
153 sumTyCon :: Int -> Builtins -> TyCon
154 sumTyCon        = indexBuiltin "sumTyCon" sumTyCons
155
156 prodTyCon :: Int -> Builtins -> TyCon
157 prodTyCon n _
158         | n >= 2 && n <= mAX_DPH_PROD 
159         = tupleTyCon Boxed n
160
161         | otherwise
162         = pprPanic "prodTyCon" (ppr n)
163
164 prodDataCon :: Int -> Builtins -> DataCon
165 prodDataCon n bi 
166  = case tyConDataCons (prodTyCon n bi) of
167         [con]   -> con
168         _       -> pprPanic "prodDataCon" (ppr n)
169
170 combinePDVar :: Int -> Builtins -> Var
171 combinePDVar    = indexBuiltin "combinePDVar" combinePDVars
172
173 scalarZip :: Int -> Builtins -> Var
174 scalarZip       = indexBuiltin "scalarZip" scalarZips
175
176 closureCtrFun :: Int -> Builtins -> Var
177 closureCtrFun   = indexBuiltin "closureCtrFun" closureCtrFuns
178
179