Vectorise.hs 8.03 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
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
11
import TyCon
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
12
13
import Type
import TypeRep
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
14
15
import Var
import VarEnv
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
16
import Name                 ( mkSysTvName )
17
import NameEnv
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
18
19
20
21
22

import DsMonad

import PrelNames

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

27
28
29
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
30
31
32
33
34
35
36
37
38
39
40
  | 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'
41
42
43
  where
    dflags = hsc_dflags hsc_env

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
44
45
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
-- ----------------------------------------------------------------------------
-- 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 {
89
90
91
92
              -- Mapping from variables to their vectorised versions. Mapping
              -- to expressions instead of just Vars gives us more freedom.
              -- 
              vect_vars :: VarEnv CoreExpr
93
94
95
96
97
98
99
100
101

              -- 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.
            , vect_tycons :: NameEnv TyCon
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
102
103
104
105
106
            }

initVEnv :: VectInfo -> DsM VEnv
initVEnv info
  = return $ VEnv {
107
               vect_vars          = mapVarEnv  (Var . snd) $ vectInfoCCVar   info
108
109
             , vect_exported_vars = emptyVarEnv
             , vect_tycons        = mapNameEnv snd $ vectInfoCCTyCon info
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
110
111
112
             }

-- FIXME
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
127

128
129
130
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
131
132

instance Monad VM where
133
  return x   = VM $ \bi env -> return (Yes env x)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
134
  VM p >>= f = VM $ \bi env -> do
135
136
137
138
139
140
141
142
143
144
145
146
147
148
                                 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
149

150
151
152
maybeV :: VM (Maybe a) -> VM a
maybeV p = maybe noV return =<< p

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
153
liftDs :: DsM a -> VM a
154
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
155

156
builtin :: (Builtins -> a) -> VM a
157
builtin f = VM $ \bi env -> return (Yes env (f bi))
158
159

readEnv :: (VEnv -> a) -> VM a
160
readEnv f = VM $ \bi env -> return (Yes env (f env))
161
162

setEnv :: VEnv -> VM ()
163
setEnv env = VM $ \_ _ -> return (Yes env ())
164
165

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

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
168
169
170
171
172
newTyVar :: FastString -> Kind -> VM Var
newTyVar fs k
  = do
      u <- liftDs newUnique
      return $ mkTyVar (mkSysTvName u fs) k
173

174
175
176
lookupVar :: Var -> VM CoreExpr
lookupVar v = maybeV . readEnv $ \env -> lookupVarEnv (vect_vars env) v

177
178
179
180
181
182
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
183
184
185
186
187
vectoriseModule :: VectInfo -> ModGuts -> DsM ModGuts
vectoriseModule info guts
  = do
      builtins <- initBuiltins
      env <- initVEnv info
188
189
190
191
      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
192
193
194
195

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

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
-- ----------------------------------------------------------------------------
-- 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
231
232
233
  | 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  | 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)