Add -fdph-this
[ghc-hetmet.git] / compiler / vectorise / Vectorise.hs
1
2 module Vectorise( vectorise )
3 where
4
5 import VectMonad
6 import VectUtils
7 import VectType
8 import VectCore
9
10 import DynFlags
11 import HscTypes hiding      ( MonadThings(..) )
12
13 import Module               ( dphSeqPackageId, dphParPackageId )
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 Var
25 import VarEnv
26 import VarSet
27 import Id
28 import OccName
29
30 import DsMonad
31
32 import Literal              ( Literal, mkMachInt )
33 import TysWiredIn
34
35 import Outputable
36 import FastString
37 import Control.Monad        ( liftM, liftM2, zipWithM )
38 import Data.List            ( sortBy, unzip4 )
39
40 vectorise :: DPHBackend -> HscEnv -> UniqSupply -> RuleBase -> ModGuts
41           -> IO (SimplCount, ModGuts)
42 vectorise backend hsc_env _ _ guts
43   = do
44       showPass dflags "Vectorisation"
45       eps <- hscEPS hsc_env
46       let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
47       Just (info', guts') <- initV (backendPackage backend) hsc_env guts info
48                                    (vectModule guts)
49       endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
50       return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
51   where
52     dflags = hsc_dflags hsc_env
53
54     backendPackage DPHSeq  = dphSeqPackageId
55     backendPackage DPHPar  = dphParPackageId
56     backendPackage DPHThis = thisPackage dflags
57
58 vectModule :: ModGuts -> VM ModGuts
59 vectModule guts
60   = do
61       (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
62
63       let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
64       updGEnv (setFamInstEnv fam_inst_env')
65
66       -- dicts   <- mapM buildPADict pa_insts
67       -- workers <- mapM vectDataConWorkers pa_insts
68       binds'  <- mapM vectTopBind (mg_binds guts)
69       return $ guts { mg_types        = types'
70                     , mg_binds        = Rec tc_binds : binds'
71                     , mg_fam_inst_env = fam_inst_env'
72                     , mg_fam_insts    = mg_fam_insts guts ++ fam_insts
73                     }
74
75 vectTopBind :: CoreBind -> VM CoreBind
76 vectTopBind b@(NonRec var expr)
77   = do
78       var'  <- vectTopBinder var
79       expr' <- vectTopRhs var expr
80       hs    <- takeHoisted
81       cexpr <- tryConvert var var' expr
82       return . Rec $ (var, cexpr) : (var', expr') : hs
83   `orElseV`
84     return b
85
86 vectTopBind b@(Rec bs)
87   = do
88       vars'  <- mapM vectTopBinder vars
89       exprs' <- zipWithM vectTopRhs vars exprs
90       hs     <- takeHoisted
91       cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
92       return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
93   `orElseV`
94     return b
95   where
96     (vars, exprs) = unzip bs
97
98 vectTopBinder :: Var -> VM Var
99 vectTopBinder var
100   = do
101       vty  <- vectType (idType var)
102       var' <- cloneId mkVectOcc var vty
103       defGlobalVar var var'
104       return var'
105
106 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
107 vectTopRhs var expr
108   = do
109       closedV . liftM vectorised
110               . inBind var
111               $ vectPolyExpr (freeVars expr)
112
113 tryConvert :: Var -> Var -> CoreExpr -> VM CoreExpr
114 tryConvert var vect_var rhs
115   = fromVect (idType var) (Var vect_var) `orElseV` return rhs
116
117 -- ----------------------------------------------------------------------------
118 -- Bindings
119
120 vectBndr :: Var -> VM VVar
121 vectBndr v
122   = do
123       (vty, lty) <- vectAndLiftType (idType v)
124       let vv = v `Id.setIdType` vty
125           lv = v `Id.setIdType` lty
126       updLEnv (mapTo vv lv)
127       return (vv, lv)
128   where
129     mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
130
131 vectBndrNew :: Var -> FastString -> VM VVar
132 vectBndrNew v fs
133   = do
134       vty <- vectType (idType v)
135       vv  <- newLocalVVar fs vty
136       updLEnv (upd vv)
137       return vv
138   where
139     upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
140
141 vectBndrIn :: Var -> VM a -> VM (VVar, a)
142 vectBndrIn v p
143   = localV
144   $ do
145       vv <- vectBndr v
146       x <- p
147       return (vv, x)
148
149 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
150 vectBndrNewIn v fs p
151   = localV
152   $ do
153       vv <- vectBndrNew v fs
154       x  <- p
155       return (vv, x)
156
157 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
158 vectBndrsIn vs p
159   = localV
160   $ do
161       vvs <- mapM vectBndr vs
162       x <- p
163       return (vvs, x)
164
165 -- ----------------------------------------------------------------------------
166 -- Expressions
167
168 vectVar :: Var -> VM VExpr
169 vectVar v
170   = do
171       r <- lookupVar v
172       case r of
173         Local (vv,lv) -> return (Var vv, Var lv)
174         Global vv     -> do
175                            let vexpr = Var vv
176                            lexpr <- liftPA vexpr
177                            return (vexpr, lexpr)
178
179 vectPolyVar :: Var -> [Type] -> VM VExpr
180 vectPolyVar v tys
181   = do
182       vtys <- mapM vectType tys
183       r <- lookupVar v
184       case r of
185         Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
186                                      (polyApply (Var lv) vtys)
187         Global poly    -> do
188                             vexpr <- polyApply (Var poly) vtys
189                             lexpr <- liftPA vexpr
190                             return (vexpr, lexpr)
191
192 vectLiteral :: Literal -> VM VExpr
193 vectLiteral lit
194   = do
195       lexpr <- liftPA (Lit lit)
196       return (Lit lit, lexpr)
197
198 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
199 vectPolyExpr (_, AnnNote note expr)
200   = liftM (vNote note) $ vectPolyExpr expr
201 vectPolyExpr expr
202   = polyAbstract tvs $ \abstract ->
203     do
204       mono' <- vectExpr mono
205       return $ mapVect abstract mono'
206   where
207     (tvs, mono) = collectAnnTypeBinders expr
208
209 vectExpr :: CoreExprWithFVs -> VM VExpr
210 vectExpr (_, AnnType ty)
211   = liftM vType (vectType ty)
212
213 vectExpr (_, AnnVar v) = vectVar v
214
215 vectExpr (_, AnnLit lit) = vectLiteral lit
216
217 vectExpr (_, AnnNote note expr)
218   = liftM (vNote note) (vectExpr expr)
219
220 vectExpr e@(_, AnnApp _ arg)
221   | isAnnTypeArg arg
222   = vectTyAppExpr fn tys
223   where
224     (fn, tys) = collectAnnTypeArgs e
225
226 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
227   | Just con <- isDataConId_maybe v
228   , is_special_con con
229   = do
230       let vexpr = App (Var v) (Lit lit)
231       lexpr <- liftPA vexpr
232       return (vexpr, lexpr)
233   where
234     is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
235
236
237 vectExpr (_, AnnApp fn arg)
238   = do
239       arg_ty' <- vectType arg_ty
240       res_ty' <- vectType res_ty
241       fn'     <- vectExpr fn
242       arg'    <- vectExpr arg
243       mkClosureApp arg_ty' res_ty' fn' arg'
244   where
245     (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
246
247 vectExpr (_, AnnCase scrut bndr ty alts)
248   | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
249   , isAlgTyCon tycon
250   = vectAlgCase tycon ty_args scrut bndr ty alts
251   where
252     scrut_ty = exprType (deAnnotate scrut)
253
254 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
255   = do
256       vrhs <- localV . inBind bndr $ vectPolyExpr rhs
257       (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
258       return $ vLet (vNonRec vbndr vrhs) vbody
259
260 vectExpr (_, AnnLet (AnnRec bs) body)
261   = do
262       (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
263                                 $ liftM2 (,)
264                                   (zipWithM vect_rhs bndrs rhss)
265                                   (vectPolyExpr body)
266       return $ vLet (vRec vbndrs vrhss) vbody
267   where
268     (bndrs, rhss) = unzip bs
269
270     vect_rhs bndr rhs = localV
271                       . inBind bndr
272                       $ vectExpr rhs
273
274 vectExpr e@(fvs, AnnLam bndr _)
275   | isId bndr = vectLam fvs bs body
276   where
277     (bs,body) = collectAnnValBinders e
278
279 vectExpr e = cantVectorise "Can't vectorise expression" (ppr $ deAnnotate e)
280
281 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
282 vectLam fvs bs body
283   = do
284       tyvars <- localTyVars
285       (vs, vvs) <- readLEnv $ \env ->
286                    unzip [(var, vv) | var <- varSetElems fvs
287                                     , Just vv <- [lookupVarEnv (local_vars env) var]]
288
289       arg_tys <- mapM (vectType . idType) bs
290       res_ty  <- vectType (exprType $ deAnnotate body)
291
292       buildClosures tyvars vvs arg_tys res_ty
293         . hoistPolyVExpr tyvars
294         $ do
295             lc <- builtin liftingContext
296             (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
297                                            (vectExpr body)
298             return $ vLams lc vbndrs vbody
299
300 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
301 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
302 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
303                         (ppr $ deAnnotate e `mkTyApps` tys)
304
305 -- We convert
306 --
307 --   case e :: t of v { ... }
308 --
309 -- to
310 --
311 --   V:    let v' = e in case v' of _ { ... }
312 --   L:    let v' = e in case v' `cast` ... of _ { ... }
313 --
314 -- When lifting, we have to do it this way because v must have the type
315 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
316 -- have to handle the case where v is a wild var correctly.
317 --
318
319 -- FIXME: this is too lazy
320 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
321             -> [(AltCon, [Var], CoreExprWithFVs)]
322             -> VM VExpr
323 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
324   = do
325       vscrut         <- vectExpr scrut
326       (vty, lty)     <- vectAndLiftType ty
327       (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
328       return $ vCaseDEFAULT vscrut vbndr vty lty vbody
329
330 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
331   = do
332       vscrut         <- vectExpr scrut
333       (vty, lty)     <- vectAndLiftType ty
334       (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
335       return $ vCaseDEFAULT vscrut vbndr vty lty vbody
336
337 vectAlgCase tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
338   = do
339       vect_tc    <- maybeV (lookupTyCon tycon)
340       (vty, lty) <- vectAndLiftType ty
341       vexpr      <- vectExpr scrut
342       (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
343                                 . vectBndrsIn bndrs
344                                 $ vectExpr body
345
346       (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
347       vect_dc <- maybeV (lookupDataCon dc)
348       let [arr_dc] = tyConDataCons arr_tc
349       repr <- mkRepr vect_tc
350       shape_bndrs <- arrShapeVars repr
351       return . vLet (vNonRec vbndr vexpr)
352              $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
353   where
354     vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
355                     | otherwise         = vectBndrIn bndr
356
357 vectAlgCase tycon _ty_args scrut bndr ty alts
358   = do
359       vect_tc     <- maybeV (lookupTyCon tycon)
360       (vty, lty)  <- vectAndLiftType ty
361       repr        <- mkRepr vect_tc
362       shape_bndrs <- arrShapeVars repr
363       (len, sel, indices) <- arrSelector repr (map Var shape_bndrs)
364
365       (vbndr, valts) <- vect_scrut_bndr $ mapM (proc_alt sel vty lty) alts'
366       let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
367
368       vexpr <- vectExpr scrut
369       (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
370       let [arr_dc] = tyConDataCons arr_tc
371
372       let (vect_scrut,  lift_scrut)  = vscrut
373           (vect_bodies, lift_bodies) = unzip vbodies
374
375       vdummy <- newDummyVar (exprType vect_scrut)
376       ldummy <- newDummyVar (exprType lift_scrut)
377       let vect_case = Case vect_scrut vdummy vty
378                            (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
379
380       lbody <- combinePA vty len sel indices lift_bodies
381       let lift_case = Case lift_scrut ldummy lty
382                            [(DataAlt arr_dc, shape_bndrs ++ concat lift_bndrss,
383                              lbody)]
384
385       return . vLet (vNonRec vbndr vexpr)
386              $ (vect_case, lift_case)
387   where
388     vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
389                     | otherwise         = vectBndrIn bndr
390
391     alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
392
393     cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
394     cmp DEFAULT       DEFAULT       = EQ
395     cmp DEFAULT       _             = LT
396     cmp _             DEFAULT       = GT
397     cmp _             _             = panic "vectAlgCase/cmp"
398
399     proc_alt sel vty lty (DataAlt dc, bndrs, body)
400       = do
401           vect_dc <- maybeV (lookupDataCon dc)
402           let tag = mkDataConTag vect_dc
403               fvs = freeVarsOf body `delVarSetList` bndrs
404           (vect_bndrs, lift_bndrs, vbody)
405             <- vect_alt_bndrs bndrs
406              $ \len -> packLiftingContext len sel tag fvs vty lty
407              $ vectExpr body
408
409           return (vect_dc, vect_bndrs, lift_bndrs, vbody)
410     proc_alt _ _ _ _ = panic "vectAlgCase/proc_alt"
411
412     vect_alt_bndrs [] p
413       = do
414           void_tc <- builtin voidTyCon
415           let void_ty = mkTyConApp void_tc []
416           arr_ty <- mkPArrayType void_ty
417           bndr   <- newLocalVar (fsLit "voids") arr_ty
418           len    <- lengthPA void_ty (Var bndr)
419           e      <- p len
420           return ([], [bndr], e)
421
422     vect_alt_bndrs bndrs p
423        = localV
424        $ do
425            vbndrs <- mapM vectBndr bndrs
426            let (vect_bndrs, lift_bndrs) = unzip vbndrs
427                vv : _ = vect_bndrs
428                lv : _ = lift_bndrs
429            len <- lengthPA (idType vv) (Var lv)
430            e   <- p len
431            return (vect_bndrs, lift_bndrs, e)
432
433     mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
434
435 packLiftingContext :: CoreExpr -> CoreExpr -> CoreExpr -> VarSet
436                    -> Type -> Type -> VM VExpr -> VM VExpr
437 packLiftingContext len shape tag fvs vty lty p
438   = do
439       select <- builtin selectPAIntPrimVar
440       let sel_expr = mkApps (Var select) [shape, tag]
441       sel_var <- newLocalVar (fsLit "sel#") (exprType sel_expr)
442       lc_var <- builtin liftingContext
443       localV $
444         do
445           bnds <- mapM (packFreeVar (Var lc_var) (Var sel_var))
446                 . filter isLocalId
447                 $ varSetElems fvs
448           (vexpr, lexpr) <- p
449           empty <- emptyPA vty
450           return (vexpr, Let (NonRec sel_var sel_expr)
451                          $ Case len lc_var lty
452                              [(DEFAULT, [], mkLets (concat bnds) lexpr),
453                               (LitAlt (mkMachInt 0), [], empty)])
454
455 packFreeVar :: CoreExpr -> CoreExpr -> Var -> VM [CoreBind]
456 packFreeVar len sel v
457   = do
458       r <- lookupVar v
459       case r of
460         Local (vv,lv) ->
461           do
462             lv' <- cloneVar lv
463             expr <- packPA (idType vv) (Var lv) len sel
464             updLEnv (upd vv lv')
465             return [(NonRec lv' expr)]
466
467         _  -> return []
468   where
469     upd vv lv' env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv') }
470