Move all vectorisation built-ins to VectBuiltIn
[ghc-hetmet.git] / compiler / vectorise / Vectorise.hs
1 module Vectorise( vectorise )
2 where
3
4 #include "HsVersions.h"
5
6 import VectMonad
7 import VectUtils
8 import VectType
9 import VectCore
10
11 import DynFlags
12 import HscTypes
13
14 import CoreLint             ( showPass, endPass )
15 import CoreSyn
16 import CoreUtils
17 import CoreFVs
18 import SimplMonad           ( SimplCount, zeroSimplCount )
19 import Rules                ( RuleBase )
20 import DataCon
21 import TyCon
22 import Type
23 import FamInstEnv           ( extendFamInstEnvList )
24 import InstEnv              ( extendInstEnvList )
25 import Var
26 import VarEnv
27 import VarSet
28 import Name                 ( Name, mkSysTvName, getName )
29 import NameEnv
30 import Id
31 import MkId                 ( unwrapFamInstScrut )
32 import OccName
33 import Module               ( Module )
34
35 import DsMonad hiding (mapAndUnzipM)
36 import DsUtils              ( mkCoreTup, mkCoreTupTy )
37
38 import Literal              ( Literal )
39 import PrelNames
40 import TysWiredIn
41 import TysPrim              ( intPrimTy )
42 import BasicTypes           ( Boxity(..) )
43
44 import Outputable
45 import FastString
46 import Control.Monad        ( liftM, liftM2, zipWithM, mapAndUnzipM )
47
48 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
49           -> IO (SimplCount, ModGuts)
50 vectorise hsc_env _ _ guts
51   = do
52       showPass dflags "Vectorisation"
53       eps <- hscEPS hsc_env
54       let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
55       Just (info', guts') <- initV hsc_env guts info (vectModule guts)
56       endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
57       return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
58   where
59     dflags = hsc_dflags hsc_env
60
61 vectModule :: ModGuts -> VM ModGuts
62 vectModule guts
63   = do
64       (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
65       
66       let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
67       updGEnv (setFamInstEnv fam_inst_env')
68      
69       -- dicts   <- mapM buildPADict pa_insts
70       -- workers <- mapM vectDataConWorkers pa_insts
71       binds'  <- mapM vectTopBind (mg_binds guts)
72       return $ guts { mg_types        = types'
73                     , mg_binds        = Rec tc_binds : binds'
74                     , mg_fam_inst_env = fam_inst_env'
75                     , mg_fam_insts    = mg_fam_insts guts ++ fam_insts
76                     }
77
78 vectTopBind :: CoreBind -> VM CoreBind
79 vectTopBind b@(NonRec var expr)
80   = do
81       var'  <- vectTopBinder var
82       expr' <- vectTopRhs var expr
83       hs    <- takeHoisted
84       return . Rec $ (var, expr) : (var', expr') : hs
85   `orElseV`
86     return b
87
88 vectTopBind b@(Rec bs)
89   = do
90       vars'  <- mapM vectTopBinder vars
91       exprs' <- zipWithM vectTopRhs vars exprs
92       hs     <- takeHoisted
93       return . Rec $ bs ++ zip vars' exprs' ++ hs
94   `orElseV`
95     return b
96   where
97     (vars, exprs) = unzip bs
98
99 vectTopBinder :: Var -> VM Var
100 vectTopBinder var
101   = do
102       vty  <- vectType (idType var)
103       var' <- cloneId mkVectOcc var vty
104       defGlobalVar var var'
105       return var'
106     
107 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
108 vectTopRhs var expr
109   = do
110       closedV . liftM vectorised
111               . inBind var
112               $ vectPolyExpr (freeVars expr)
113
114 -- ----------------------------------------------------------------------------
115 -- Bindings
116
117 vectBndr :: Var -> VM VVar
118 vectBndr v
119   = do
120       vty <- vectType (idType v)
121       lty <- mkPArrayType vty
122       let vv = v `Id.setIdType` vty
123           lv = v `Id.setIdType` lty
124       updLEnv (mapTo vv lv)
125       return (vv, lv)
126   where
127     mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
128
129 vectBndrIn :: Var -> VM a -> VM (VVar, a)
130 vectBndrIn v p
131   = localV
132   $ do
133       vv <- vectBndr v
134       x <- p
135       return (vv, x)
136
137 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
138 vectBndrIn' v p
139   = localV
140   $ do
141       vv <- vectBndr v
142       x  <- p vv
143       return (vv, x)
144
145 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
146 vectBndrsIn vs p
147   = localV
148   $ do
149       vvs <- mapM vectBndr vs
150       x <- p
151       return (vvs, x)
152
153 -- ----------------------------------------------------------------------------
154 -- Expressions
155
156 vectVar :: Var -> VM VExpr
157 vectVar v
158   = do
159       r <- lookupVar v
160       case r of
161         Local (vv,lv) -> return (Var vv, Var lv)
162         Global vv     -> do
163                            let vexpr = Var vv
164                            lexpr <- liftPA vexpr
165                            return (vexpr, lexpr)
166
167 vectPolyVar :: Var -> [Type] -> VM VExpr
168 vectPolyVar v tys
169   = do
170       vtys <- mapM vectType tys
171       r <- lookupVar v
172       case r of
173         Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
174                                      (polyApply (Var lv) vtys)
175         Global poly    -> do
176                             vexpr <- polyApply (Var poly) vtys
177                             lexpr <- liftPA vexpr
178                             return (vexpr, lexpr)
179
180 vectLiteral :: Literal -> VM VExpr
181 vectLiteral lit
182   = do
183       lexpr <- liftPA (Lit lit)
184       return (Lit lit, lexpr)
185
186 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
187 vectPolyExpr expr
188   = polyAbstract tvs $ \abstract ->
189     do
190       mono' <- vectExpr mono
191       return $ mapVect abstract mono'
192   where
193     (tvs, mono) = collectAnnTypeBinders expr  
194                 
195 vectExpr :: CoreExprWithFVs -> VM VExpr
196 vectExpr (_, AnnType ty)
197   = liftM vType (vectType ty)
198
199 vectExpr (_, AnnVar v) = vectVar v
200
201 vectExpr (_, AnnLit lit) = vectLiteral lit
202
203 vectExpr (_, AnnNote note expr)
204   = liftM (vNote note) (vectExpr expr)
205
206 vectExpr e@(_, AnnApp _ arg)
207   | isAnnTypeArg arg
208   = vectTyAppExpr fn tys
209   where
210     (fn, tys) = collectAnnTypeArgs e
211
212 vectExpr (_, AnnApp fn arg)
213   = do
214       fn'  <- vectExpr fn
215       arg' <- vectExpr arg
216       mkClosureApp fn' arg'
217
218 vectExpr (_, AnnCase scrut bndr ty alts)
219   | isAlgType scrut_ty
220   = vectAlgCase scrut bndr ty alts
221   where
222     scrut_ty = exprType (deAnnotate scrut)
223
224 vectExpr (_, AnnCase expr bndr ty alts)
225   = panic "vectExpr: case"
226
227 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
228   = do
229       vrhs <- localV . inBind bndr $ vectPolyExpr rhs
230       (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
231       return $ vLet (vNonRec vbndr vrhs) vbody
232
233 vectExpr (_, AnnLet (AnnRec bs) body)
234   = do
235       (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
236                                 $ liftM2 (,)
237                                   (zipWithM vect_rhs bndrs rhss)
238                                   (vectPolyExpr body)
239       return $ vLet (vRec vbndrs vrhss) vbody
240   where
241     (bndrs, rhss) = unzip bs
242
243     vect_rhs bndr rhs = localV
244                       . inBind bndr
245                       $ vectExpr rhs
246
247 vectExpr e@(fvs, AnnLam bndr _)
248   | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
249   | otherwise = vectLam fvs bs body
250   where
251     (bs,body) = collectAnnValBinders e
252
253 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
254 vectLam fvs bs body
255   = do
256       tyvars <- localTyVars
257       (vs, vvs) <- readLEnv $ \env ->
258                    unzip [(var, vv) | var <- varSetElems fvs
259                                     , Just vv <- [lookupVarEnv (local_vars env) var]]
260
261       arg_tys <- mapM (vectType . idType) bs
262       res_ty  <- vectType (exprType $ deAnnotate body)
263
264       buildClosures tyvars vvs arg_tys res_ty
265         . hoistPolyVExpr tyvars
266         $ do
267             lc <- builtin liftingContext
268             (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
269                                            (vectExpr body)
270             return $ vLams lc vbndrs vbody
271   
272 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
273 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
274 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
275
276 type CoreAltWithFVs = AnnAlt Id VarSet
277
278 -- We convert
279 --
280 --   case e :: t of v { ... }
281 --
282 -- to
283 --
284 --   V:    let v = e in case v of _ { ... }
285 --   L:    let v = e in case v `cast` ... of _ { ... }
286 --
287 -- When lifting, we have to do it this way because v must have the type
288 -- [:V(T):] but the scrutinee must be cast to the representation type.
289 --   
290
291 -- FIXME: this is too lazy
292 vectAlgCase scrut bndr ty [(DEFAULT, [], body)]
293   = do
294       vscrut <- vectExpr scrut
295       vty    <- vectType ty
296       lty    <- mkPArrayType vty
297       (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
298       return $ vCaseDEFAULT vscrut vbndr vty lty vbody
299
300 vectAlgCase scrut bndr ty [(DataAlt dc, bndrs, body)]
301   = do
302       vty <- vectType ty
303       lty <- mkPArrayType vty
304       vexpr <- vectExpr scrut
305       (vbndr, (vbndrs, vbody)) <- vectBndrIn bndr
306                                 . vectBndrsIn bndrs
307                                 $ vectExpr body
308
309       (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
310       vect_dc <- maybeV (lookupDataCon dc)
311       let [arr_dc] = tyConDataCons arr_tc
312       let shape_tys = take (dataConRepArity arr_dc - length bndrs)
313                            (dataConRepArgTys arr_dc)
314       shape_bndrs <- mapM (newLocalVar FSLIT("s")) shape_tys
315       return . vLet (vNonRec vbndr vexpr)
316              $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody