Break up vectoriser builtins module
[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 Var
37 import Outputable
38 import Data.Array
39
40
41 -- Numbers of things exported by the DPH library.
42 mAX_DPH_PROD :: Int
43 mAX_DPH_PROD = 5
44
45 mAX_DPH_SUM :: Int
46 mAX_DPH_SUM = 2
47
48 mAX_DPH_COMBINE :: Int
49 mAX_DPH_COMBINE = 2
50
51 mAX_DPH_SCALAR_ARGS :: Int
52 mAX_DPH_SCALAR_ARGS = 3
53
54
55 -- | Holds the names of the builtin types and functions used by the vectoriser.
56 data Builtins 
57         = Builtins 
58         { dphModules       :: Modules
59
60         -- From dph-common:Data.Array.Parallel.Lifted.PArray
61         , parrayTyCon      :: TyCon                     -- ^ PArray
62         , parrayDataCon    :: DataCon                   -- ^ PArray
63         , pdataTyCon       :: TyCon                     -- ^ PData
64         , paTyCon          :: TyCon                     -- ^ PA
65         , paDataCon        :: DataCon                   -- ^ PA
66         , preprTyCon       :: TyCon                     -- ^ PRepr
67         , prTyCon          :: TyCon                     -- ^ PR
68         , prDataCon        :: DataCon                   -- ^ PR
69         , replicatePDVar   :: Var                       -- ^ replicatePD
70         , emptyPDVar       :: Var                       -- ^ emptyPD
71         , packByTagPDVar   :: Var                       -- ^ packByTagPD
72         , combinePDVars    :: Array Int Var             -- ^ combinePD
73         , scalarClass      :: Class                     -- ^ Scalar
74
75         -- From dph-common:Data.Array.Parallel.Lifted.Closure
76         , closureTyCon     :: TyCon                     -- ^ :->
77         , closureVar       :: Var                       -- ^ closure
78         , applyVar         :: Var                       -- ^ $: 
79         , liftedClosureVar :: Var                       -- ^ liftedClosure
80         , liftedApplyVar   :: Var                       -- ^ liftedApply
81         , closureCtrFuns   :: Array Int Var             -- ^ closure1 .. closure2
82
83         -- From dph-common:Data.Array.Parallel.Lifted.Repr
84         , voidTyCon        :: TyCon                     -- ^ Void
85         , wrapTyCon        :: TyCon                     -- ^ Wrap
86         , sumTyCons        :: Array Int TyCon           -- ^ Sum2 .. Sum3
87         , voidVar          :: Var                       -- ^ void
88         , pvoidVar         :: Var                       -- ^ pvoid
89         , fromVoidVar      :: Var                       -- ^ fromVoid
90         , punitVar         :: Var                       -- ^ punit
91
92         -- From dph-common:Data.Array.Parallel.Lifted.Selector
93         , selTys           :: Array Int Type            -- ^ Sel2
94         , selReplicates    :: Array Int CoreExpr        -- ^ replicate2
95         , selPicks         :: Array Int CoreExpr        -- ^ pick2
96         , selTagss         :: Array Int CoreExpr        -- ^ tagsSel2
97         , selEls           :: Array (Int, Int) CoreExpr -- ^ elementsSel2_0 .. elementsSel_2_1
98
99         -- From dph-common:Data.Array.Parallel.Lifted.Scalar
100         -- NOTE: map is counted as a zipWith fn with one argument array.
101         , scalarZips       :: Array Int Var             -- ^ map, zipWith, zipWith3
102
103         -- A Fresh variable
104         , liftingContext   :: Var                       -- ^ lc
105         }
106
107
108 -- | Get an element from one of the arrays of contained by a `Builtins`.
109 --   If the indexed thing is not in the array then panic.
110 indexBuiltin 
111         :: (Ix i, Outputable i) 
112         => String                       -- ^ Name of the selector we've used, for panic messages.
113         -> (Builtins -> Array i a)      -- ^ Field selector for the `Builtins`.
114         -> i                            -- ^ Index into the array.
115         -> Builtins 
116         -> a
117
118 indexBuiltin fn f i bi
119   | inRange (bounds xs) i = xs ! i
120   | otherwise             = pprPanic fn (ppr i)
121   where xs = f bi
122
123
124 -- Projections ----------------------------------------------------------------
125 -- We use these wrappers instead of indexing the `Builtin` structure directly
126 -- because they give nicer panic messages if the indexed thing cannot be found.
127
128 selTy :: Int -> Builtins -> Type
129 selTy           = indexBuiltin "selTy" selTys
130
131 selReplicate :: Int -> Builtins -> CoreExpr
132 selReplicate    = indexBuiltin "selReplicate" selReplicates 
133
134 selPick :: Int -> Builtins -> CoreExpr
135 selPick         = indexBuiltin "selPick" selPicks
136
137 selTags :: Int -> Builtins -> CoreExpr
138 selTags         = indexBuiltin "selTags" selTagss
139
140 selElements :: Int -> Int -> Builtins -> CoreExpr
141 selElements i j = indexBuiltin "selElements" selEls (i,j)
142
143 sumTyCon :: Int -> Builtins -> TyCon
144 sumTyCon        = indexBuiltin "sumTyCon" sumTyCons
145
146 prodTyCon :: Int -> Builtins -> TyCon
147 prodTyCon n _
148         | n >= 2 && n <= mAX_DPH_PROD 
149         = tupleTyCon Boxed n
150
151         | otherwise
152         = pprPanic "prodTyCon" (ppr n)
153
154 prodDataCon :: Int -> Builtins -> DataCon
155 prodDataCon n bi 
156  = case tyConDataCons (prodTyCon n bi) of
157         [con]   -> con
158         _       -> pprPanic "prodDataCon" (ppr n)
159
160 combinePDVar :: Int -> Builtins -> Var
161 combinePDVar    = indexBuiltin "combinePDVar" combinePDVars
162
163 scalarZip :: Int -> Builtins -> Var
164 scalarZip       = indexBuiltin "scalarZip" scalarZips
165
166 closureCtrFun :: Int -> Builtins -> Var
167 closureCtrFun   = indexBuiltin "closureCtrFun" closureCtrFuns
168
169