Vectorise.hs 8.94 KB
Newer Older
1
2
3
4
5
6
7
8
module Vectorise( vectorise )
where

#include "HsVersions.h"

import DynFlags
import HscTypes

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

import DsMonad

import PrelNames

25
import Outputable
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
26
27
import FastString
import Control.Monad        ( liftM2 )
28

29
30
31
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
32
33
34
35
36
37
38
39
40
41
42
  | otherwise
  = do
      showPass dflags "Vectorisation"
      eps <- hscEPS hsc_env
      let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
      Just guts' <- initDs hsc_env (mg_module guts)
                                   (mg_rdr_env guts)
                                   (mg_types guts)
                                   (vectoriseModule info guts)
      endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
      return guts'
43
44
45
  where
    dflags = hsc_dflags hsc_env

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
46
47
48
49
50
51
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
81
82
83
84
85
86
87
88
89
90
-- ----------------------------------------------------------------------------
-- Vectorisation monad

data Builtins = Builtins {
                  parrayTyCon      :: TyCon
                , paTyCon          :: TyCon
                , closureTyCon     :: TyCon
                , mkClosureVar     :: Var
                , applyClosureVar  :: Var
                , mkClosurePVar    :: Var
                , applyClosurePVar :: Var
                , closurePAVar     :: Var
                , lengthPAVar      :: Var
                , replicatePAVar   :: Var
                }

initBuiltins :: DsM Builtins
initBuiltins
  = do
      parrayTyCon  <- dsLookupTyCon parrayTyConName
      paTyCon      <- dsLookupTyCon paTyConName
      closureTyCon <- dsLookupTyCon closureTyConName

      mkClosureVar     <- dsLookupGlobalId mkClosureName
      applyClosureVar  <- dsLookupGlobalId applyClosureName
      mkClosurePVar    <- dsLookupGlobalId mkClosurePName
      applyClosurePVar <- dsLookupGlobalId applyClosurePName
      closurePAVar     <- dsLookupGlobalId closurePAName
      lengthPAVar      <- dsLookupGlobalId lengthPAName
      replicatePAVar   <- dsLookupGlobalId replicatePAName

      return $ Builtins {
                 parrayTyCon      = parrayTyCon
               , paTyCon          = paTyCon
               , closureTyCon     = closureTyCon
               , mkClosureVar     = mkClosureVar
               , applyClosureVar  = applyClosureVar
               , mkClosurePVar    = mkClosurePVar
               , applyClosurePVar = applyClosurePVar
               , closurePAVar     = closurePAVar
               , lengthPAVar      = lengthPAVar
               , replicatePAVar   = replicatePAVar
               }

data VEnv = VEnv {
91
              -- Mapping from global variables to their vectorised versions.
92
              -- 
93
94
95
96
97
98
              vect_global_vars :: VarEnv CoreExpr

              -- Mapping from local variables to their vectorised and lifted
              -- versions.
              --
            , vect_local_vars :: VarEnv (CoreExpr, CoreExpr)
99
100
101
102
103
104
105
106

              -- Exported variables which have a vectorised version
              --
            , vect_exported_vars :: VarEnv (Var, Var)

              -- Mapping from TyCons to their vectorised versions.
              -- TyCons which do not have to be vectorised are mapped to
              -- themselves.
107
              --
108
            , vect_tycons :: NameEnv TyCon
109
110
111
112
113
114
115
116

              -- Mapping from TyCons to their PA dictionaries
              --
            , vect_tycon_pa :: NameEnv CoreExpr

              -- Mapping from tyvars to their PA dictionaries
              --
            , vect_tyvar_pa :: VarEnv CoreExpr
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
117
118
119
120
121
            }

initVEnv :: VectInfo -> DsM VEnv
initVEnv info
  = return $ VEnv {
122
123
               vect_global_vars   = mapVarEnv  (Var . snd) $ vectInfoCCVar   info
             , vect_local_vars    = emptyVarEnv
124
125
             , vect_exported_vars = emptyVarEnv
             , vect_tycons        = mapNameEnv snd $ vectInfoCCTyCon info
126
127
             , vect_tycon_pa      = emptyNameEnv
             , vect_tyvar_pa      = emptyVarEnv
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
128
129
130
             }

-- FIXME
131
132
133
134
135
136
137
138
139
140
141
142
143
144
updVectInfo :: VEnv -> ModGuts -> ModGuts
updVectInfo env guts = guts { mg_vect_info = info' }
  where
    info' = info {
              vectInfoCCVar   = vect_exported_vars env
            , vectInfoCCTyCon = tc_env
            }

    info  = mg_vect_info guts
    tyenv = mg_types guts

    tc_env = mkNameEnv [(tc_name, (tc,tc')) | tc <- typeEnvTyCons tyenv
                                            , let tc_name = tyConName tc
                                            , Just tc' <- [lookupNameEnv (vect_tycons env) tc_name]]
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
145

146
147
148
data VResult a = Yes VEnv a | No

newtype VM a = VM { runVM :: Builtins -> VEnv -> DsM (VResult a) }
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
149
150

instance Monad VM where
151
  return x   = VM $ \bi env -> return (Yes env x)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
152
  VM p >>= f = VM $ \bi env -> do
153
154
155
156
157
158
159
160
161
162
163
164
165
166
                                 r <- p bi env
                                 case r of
                                   Yes env' x -> runVM (f x) bi env'
                                   No         -> return No

noV :: VM a
noV = VM $ \bi env -> return No

tryV :: VM a -> VM (Maybe a)
tryV (VM p) = VM $ \bi env -> do
                                r <- p bi env
                                case r of
                                  Yes env' x -> return (Yes env' (Just x))
                                  No         -> return (Yes env Nothing)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
167

168
169
170
maybeV :: VM (Maybe a) -> VM a
maybeV p = maybe noV return =<< p

171
172
173
orElseV :: VM a -> VM a -> VM a
orElseV p q = maybe q return =<< tryV p

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
174
liftDs :: DsM a -> VM a
175
liftDs p = VM $ \bi env -> do { x <- p; return (Yes env x) }
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
176

177
builtin :: (Builtins -> a) -> VM a
178
builtin f = VM $ \bi env -> return (Yes env (f bi))
179
180

readEnv :: (VEnv -> a) -> VM a
181
readEnv f = VM $ \bi env -> return (Yes env (f env))
182
183

setEnv :: VEnv -> VM ()
184
setEnv env = VM $ \_ _ -> return (Yes env ())
185
186

updEnv :: (VEnv -> VEnv) -> VM ()
187
updEnv f = VM $ \_ env -> return (Yes (f env) ())
188

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
189
190
191
192
193
newTyVar :: FastString -> Kind -> VM Var
newTyVar fs k
  = do
      u <- liftDs newUnique
      return $ mkTyVar (mkSysTvName u fs) k
194

195
196
197
lookupVar :: Var -> VM CoreExpr
lookupVar v = maybeV . readEnv $ \env -> lookupVarEnv (vect_vars env) v

198
199
200
201
202
203
lookupTyCon :: TyCon -> VM (Maybe TyCon)
lookupTyCon tc = readEnv $ \env -> lookupNameEnv (vect_tycons env) (tyConName tc)

-- ----------------------------------------------------------------------------
-- Bindings

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
204
205
206
207
208
vectoriseModule :: VectInfo -> ModGuts -> DsM ModGuts
vectoriseModule info guts
  = do
      builtins <- initBuiltins
      env <- initVEnv info
209
210
211
212
      r <- runVM (vectModule guts) builtins env
      case r of
        Yes env' guts' -> return $ updVectInfo env' guts'
        No             -> return guts
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
213
214
215
216

vectModule :: ModGuts -> VM ModGuts
vectModule guts = return guts

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
-- ----------------------------------------------------------------------------
-- Types

paArgType :: Type -> Kind -> VM (Maybe Type)
paArgType ty k
  | Just k' <- kindView k = paArgType ty k'

-- Here, we assume that for a kind (k1 -> k2) to be valid, k1 and k2 can only
-- be made up of * and (->), i.e., they can't be coercion kinds or #.
paArgType ty (FunTy k1 k2)
  = do
      tv  <- newTyVar FSLIT("a") k1
      ty1 <- paArgType' (TyVarTy tv) k1
      ty2 <- paArgType' (AppTy ty (TyVarTy tv)) k2
      return . Just $ ForAllTy tv (FunTy ty1 ty2)

paArgType ty k
  | isLiftedTypeKind k
  = do
      tc <- builtin paTyCon
      return . Just $ TyConApp tc [ty]

  | otherwise
  = return Nothing 

paArgType' :: Type -> Kind -> VM Type
paArgType' ty k
  = do
      r <- paArgType ty k
      case r of
        Just ty' -> return ty'
        Nothing  -> pprPanic "paArgType'" (ppr ty)

vectTyCon :: TyCon -> VM TyCon
vectTyCon tc
252
253
254
  | 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  | 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
      r   <- paArgType (TyVarTy tv) (tyVarKind tv)
      ty' <- vectType ty
      return . ForAllTy tv $ case r of { Just paty -> FunTy paty ty'; Nothing -> ty' }

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

278
279
280
281
282
283
284
285
286
287
288
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)