Vectorise.hs 6.32 KB
Newer Older
1
2
3
4
5
module Vectorise( vectorise )
where

#include "HsVersions.h"

6
import VectMonad
7
import VectUtils
8

9
10
11
import DynFlags
import HscTypes

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
12
import CoreLint             ( showPass, endPass )
13
import CoreSyn
14
15
import CoreUtils
import CoreFVs
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
16
import TyCon
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
17
18
import Type
import TypeRep
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
19
20
import Var
import VarEnv
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
21
import Name                 ( mkSysTvName )
22
import NameEnv
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
23
import Id
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
24

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
25
import DsMonad hiding (mapAndUnzipM)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
26
27
28

import PrelNames

29
import Outputable
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
30
import FastString
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
31
import Control.Monad        ( liftM, liftM2, mapAndUnzipM )
32

33
34
35
vectorise :: HscEnv -> ModGuts -> IO ModGuts
vectorise hsc_env guts
  | not (Opt_Vectorise `dopt` dflags) = return guts
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
36
37
38
39
40
  | otherwise
  = do
      showPass dflags "Vectorisation"
      eps <- hscEPS hsc_env
      let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
41
      Just (info', guts') <- initV hsc_env guts info (vectModule guts)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
42
      endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
43
      return $ guts' { mg_vect_info = info' }
44
45
46
  where
    dflags = hsc_dflags hsc_env

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
47
48
49
vectModule :: ModGuts -> VM ModGuts
vectModule guts = return guts

50
51
-- ----------------------------------------------------------------------------
-- Bindings
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

vectBndr :: Var -> VM (Var, Var)
vectBndr v
  = do
      vty <- vectType (idType v)
      lty <- mkPArrayTy vty
      let vv = v `Id.setIdType` vty
          lv = v `Id.setIdType` lty
      updLEnv (mapTo vv lv)
      return (vv, lv)
  where
    mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (Var vv, Var lv) }

vectBndrIn :: Var -> VM a -> VM (Var, Var, a)
vectBndrIn v p
  = localV
  $ do
      (vv, lv) <- vectBndr v
      x <- p
      return (vv, lv, x)

vectBndrsIn :: [Var] -> VM a -> VM ([Var], [Var], a)
vectBndrsIn vs p
  = localV
  $ do
      (vvs, lvs) <- mapAndUnzipM vectBndr vs
      x <- p
      return (vvs, lvs, x)

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
81
-- ----------------------------------------------------------------------------
82
83
84
85
86
-- Expressions

replicateP :: CoreExpr -> CoreExpr -> VM CoreExpr
replicateP expr len
  = do
87
88
89
      dict <- paDictOfType ty
      rep  <- builtin replicatePAVar
      return $ mkApps (Var rep) [Type ty, dict, expr, len]
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  where
    ty = exprType expr

capply :: (CoreExpr, CoreExpr) -> (CoreExpr, CoreExpr) -> VM (CoreExpr, CoreExpr)
capply (vfn, lfn) (varg, larg)
  = do
      apply  <- builtin applyClosureVar
      applyP <- builtin applyClosurePVar
      return (mkApps (Var apply)  [Type arg_ty, Type res_ty, vfn, varg],
              mkApps (Var applyP) [Type arg_ty, Type res_ty, lfn, larg])
  where
    fn_ty            = exprType vfn
    (arg_ty, res_ty) = splitClosureTy fn_ty

vectVar :: CoreExpr -> Var -> VM (CoreExpr, CoreExpr)
vectVar lc v = local v `orElseV` global v
  where
107
    local  v = maybeV (readLEnv $ \env -> lookupVarEnv (local_vars env) v)
108
    global v = do
109
                 vexpr <- maybeV (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
                 lexpr <- replicateP vexpr lc
                 return (vexpr, lexpr)
                
vectExpr :: CoreExpr -> CoreExprWithFVs -> VM (CoreExpr, CoreExpr)
vectExpr lc (_, AnnType ty)
  = do
      vty <- vectType ty
      return (Type vty, Type vty)
vectExpr lc (_, AnnVar v)   = vectVar lc v
vectExpr lc (_, AnnLit lit)
  = do
      let vexpr = Lit lit
      lexpr <- replicateP vexpr lc
      return (vexpr, lexpr)
vectExpr lc (_, AnnNote note expr)
  = do
      (vexpr, lexpr) <- vectExpr lc expr
      return (Note note vexpr, Note note lexpr)
vectExpr lc (_, AnnApp fn arg)
  = do
      fn'  <- vectExpr lc fn
      arg' <- vectExpr lc arg
      capply fn' arg'
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
vectExpr lc (_, AnnCase expr bndr ty alts)
  = panic "vectExpr: case"
vectExpr lc (_, AnnLet (AnnNonRec bndr rhs) body)
  = do
      (vrhs, lrhs) <- vectExpr lc rhs
      (vbndr, lbndr, (vbody, lbody)) <- vectBndrIn bndr (vectExpr lc body)
      return (Let (NonRec vbndr vrhs) vbody,
              Let (NonRec lbndr lrhs) lbody)
vectExpr lc (_, AnnLet (AnnRec prs) body)
  = do
      (vbndrs, lbndrs, (vrhss, vbody, lrhss, lbody)) <- vectBndrsIn bndrs vect
      return (Let (Rec (zip vbndrs vrhss)) vbody,
              Let (Rec (zip lbndrs lrhss)) lbody)
  where
    (bndrs, rhss) = unzip prs
    
    vect = do
             (vrhss, lrhss) <- mapAndUnzipM (vectExpr lc) rhss
             (vbody, lbody) <- vectExpr lc body
             return (vrhss, vbody, lrhss, lbody)
153
154
155
vectExpr lc (_, AnnLam bndr body)
  | isTyVar bndr
  = do
156
157
158
159
160
161
162
163
164
165
      r <- paDictArgType bndr
      (upd_env, add_lam) <- get_upd r
      (vbody, lbody) <- localV (upd_env >> vectExpr lc body)
      return (Lam bndr (add_lam vbody), Lam bndr (add_lam lbody))
  where
    get_upd Nothing = return (deleteTyVarPA bndr, id)
    get_upd (Just pa_ty) = do
                             pa_var <- newLocalVar FSLIT("dPA") pa_ty
                             return (extendTyVarPA bndr (Var pa_var),
                                     Lam pa_var)
166

167
168
169
-- ----------------------------------------------------------------------------
-- Types

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
170
171
vectTyCon :: TyCon -> VM TyCon
vectTyCon tc
172
173
174
  | isFunTyCon tc        = builtin closureTyCon
  | isBoxedTupleTyCon tc = return tc
  | isUnLiftedTyCon tc   = return tc
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  | otherwise = do
                  r <- lookupTyCon tc
                  case r of
                    Just tc' -> return tc'

                    -- FIXME: just for now
                    Nothing  -> pprTrace "ccTyCon:" (ppr tc) $ return tc

vectType :: Type -> VM Type
vectType ty | Just ty' <- coreView ty = vectType ty
vectType (TyVarTy tv) = return $ TyVarTy tv
vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
vectType (FunTy ty1 ty2)   = liftM2 TyConApp (builtin closureTyCon)
                                             (mapM vectType [ty1,ty2])
vectType (ForAllTy tv ty)
  = do
192
      r   <- paDictArgType tv
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
193
      ty' <- vectType ty
194
195
196
197
      return $ ForAllTy tv (wrap r ty')
  where
    wrap Nothing      = id
    wrap (Just pa_ty) = FunTy pa_ty
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
198
199
200

vectType ty = pprPanic "vectType:" (ppr ty)

201
202
203
204
205
206
207
208
209
210
211
isClosureTyCon :: TyCon -> Bool
isClosureTyCon tc = tyConUnique tc == closureTyConKey

splitClosureTy :: Type -> (Type, Type)
splitClosureTy ty
  | Just (tc, [arg_ty, res_ty]) <- splitTyConApp_maybe ty
  , isClosureTyCon tc
  = (arg_ty, res_ty)

  | otherwise = pprPanic "splitClosureTy" (ppr ty)

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
212
213
214
215
216
mkPArrayTy :: Type -> VM Type
mkPArrayTy ty = do
                  tc <- builtin parrayTyCon
                  return $ TyConApp tc [ty]