VectUtils.hs 9.16 KB
Newer Older
1
module VectUtils (
2
  collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
3
4
  splitClosureTy,
  mkPADictType, mkPArrayType,
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
5
  paDictArgType, paDictOfType,
6
  paMethod, lengthPA, replicatePA, emptyPA,
7
  polyAbstract, polyApply, polyVApply,
8
  lookupPArrayFamInst,
9
10
  hoistExpr, hoistPolyVExpr, takeHoisted,
  buildClosure
11
12
13
14
) where

#include "HsVersions.h"

15
import VectCore
16
17
import VectMonad

18
import DsUtils
19
import CoreSyn
20
import CoreUtils
21
22
import Type
import TypeRep
23
import TyCon
24
import DataCon            ( dataConWrapId )
25
import Var
26
27
import Id                 ( mkWildId )
import MkId               ( unwrapFamInstScrut )
28
import PrelNames
29
30
import TysWiredIn
import BasicTypes         ( Boxity(..) )
31

32
import Outputable
33
import FastString
34

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
35
import Control.Monad         ( liftM, zipWithM_ )
36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
collectAnnTypeArgs expr = go expr []
  where
    go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
    go e                             tys = (e, tys)

collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
collectAnnTypeBinders expr = go [] expr
  where
    go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
    go bs e                           = (reverse bs, e)

isAnnTypeArg :: AnnExpr b ann -> Bool
isAnnTypeArg (_, AnnType t) = True
isAnnTypeArg _              = False

53
isClosureTyCon :: TyCon -> Bool
54
isClosureTyCon tc = tyConName tc == closureTyConName
55
56
57
58
59
60
61
62
63

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)

64
65
66
67
68
69
70
71
72
73
74
isPArrayTyCon :: TyCon -> Bool
isPArrayTyCon tc = tyConName tc == parrayTyConName

splitPArrayTy :: Type -> Type
splitPArrayTy ty
  | Just (tc, [arg_ty]) <- splitTyConApp_maybe ty
  , isPArrayTyCon tc
  = arg_ty

  | otherwise = pprPanic "splitPArrayTy" (ppr ty)

75
76
77
78
79
80
81
82
83
84
85
86
mkPADictType :: Type -> VM Type
mkPADictType ty
  = do
      tc <- builtin paDictTyCon
      return $ TyConApp tc [ty]

mkPArrayType :: Type -> VM Type
mkPArrayType ty
  = do
      tc <- builtin parrayTyCon
      return $ TyConApp tc [ty]

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
paDictArgType :: TyVar -> VM (Maybe Type)
paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
  where
    go ty k | Just k' <- kindView k = go ty k'
    go ty (FunTy k1 k2)
      = do
          tv   <- newTyVar FSLIT("a") k1
          mty1 <- go (TyVarTy tv) k1
          case mty1 of
            Just ty1 -> do
                          mty2 <- go (AppTy ty (TyVarTy tv)) k2
                          return $ fmap (ForAllTy tv . FunTy ty1) mty2
            Nothing  -> go ty k2

    go ty k
      | isLiftedTypeKind k
103
      = liftM Just (mkPADictType ty)
104
105
106

    go ty k = return Nothing

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
paDictOfType :: Type -> VM CoreExpr
paDictOfType ty = paDictOfTyApp ty_fn ty_args
  where
    (ty_fn, ty_args) = splitAppTys ty

paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
paDictOfTyApp ty_fn ty_args
  | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
paDictOfTyApp (TyVarTy tv) ty_args
  = do
      dfun <- maybeV (lookupTyVarPA tv)
      paDFunApply dfun ty_args
paDictOfTyApp (TyConApp tc _) ty_args
  = do
      pa_class <- builtin paClass
      (dfun, ty_args') <- lookupInst pa_class [TyConApp tc ty_args]
      paDFunApply (Var dfun) ty_args'
paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)

paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
paDFunApply dfun tys
  = do
      dicts <- mapM paDictOfType tys
      return $ mkApps (mkTyApps dfun tys) dicts

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
132
133
134
135
136
137
138
paMethod :: (Builtins -> Var) -> Type -> VM CoreExpr
paMethod method ty
  = do
      fn   <- builtin method
      dict <- paDictOfType ty
      return $ mkApps (Var fn) [Type ty, dict]

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
139
lengthPA :: CoreExpr -> VM CoreExpr
140
141
142
lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty)
  where
    ty = splitPArrayTy (exprType x)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
143
144
145
146
147

replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
replicatePA len x = liftM (`mkApps` [len,x])
                          (paMethod replicatePAVar (exprType x))

148
149
150
emptyPA :: Type -> VM CoreExpr
emptyPA = paMethod emptyPAVar

151
152
153
154
155
156
157
158
newLocalVVar :: FastString -> Type -> VM VVar
newLocalVVar fs vty
  = do
      lty <- mkPArrayType vty
      vv  <- newLocalVar fs vty
      lv  <- newLocalVar fs lty
      return (vv,lv)

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
159
160
161
162
polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
polyAbstract tvs p
  = localV
  $ do
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
163
164
165
166
167
168
169
170
171
172
173
174
      mdicts <- mapM mk_dict_var tvs
      zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
      p (mk_lams mdicts)
  where
    mk_dict_var tv = do
                       r <- paDictArgType tv
                       case r of
                         Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
                         Nothing -> return Nothing

    mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
175
176
polyApply :: CoreExpr -> [Type] -> VM CoreExpr
polyApply expr tys
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
177
178
179
180
  = do
      dicts <- mapM paDictOfType tys
      return $ expr `mkTyApps` tys `mkApps` dicts

181
182
183
184
185
186
polyVApply :: VExpr -> [Type] -> VM VExpr
polyVApply expr tys
  = do
      dicts <- mapM paDictOfType tys
      return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr

187
188
189
lookupPArrayFamInst :: Type -> VM (TyCon, [Type])
lookupPArrayFamInst ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])

190
191
192
193
hoistExpr :: FastString -> CoreExpr -> VM Var
hoistExpr fs expr
  = do
      var <- newLocalVar fs (exprType expr)
194
195
      updGEnv $ \env ->
        env { global_bindings = (var, expr) : global_bindings env }
196
197
      return var

198
199
hoistVExpr :: FastString -> VExpr -> VM VVar
hoistVExpr fs (ve, le)
200
  = do
201
202
203
      vv <- hoistExpr ('v' `consFS` fs) ve
      lv <- hoistExpr ('l' `consFS` fs) le
      return (vv, lv)
204

205
206
hoistPolyVExpr :: FastString -> [TyVar] -> VM VExpr -> VM VExpr
hoistPolyVExpr fs tvs p
207
  = do
208
209
210
211
      expr <- closedV . polyAbstract tvs $ \abstract ->
              liftM (mapVect abstract) p
      fn   <- hoistVExpr fs expr
      polyVApply (vVar fn) (mkTyVarTys tvs)
212

213
214
215
216
217
218
219
takeHoisted :: VM [(Var, CoreExpr)]
takeHoisted
  = do
      env <- readGEnv id
      setGEnv $ env { global_bindings = [] }
      return $ global_bindings env

220

221
222
mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
223
  = do
224
225
226
227
228
      dict <- paDictOfType env_ty
      mkv  <- builtin mkClosureVar
      mkl  <- builtin mkClosurePVar
      return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
              Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
229
230
231
232
233
234

-- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
--   where
--     f  = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
--     f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v

235
236
buildClosure :: [TyVar] -> Var -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
buildClosure tvs lv vars arg_ty res_ty mk_body
237
  = do
238
      (env_ty, env, bind) <- buildEnv lv vars
239
240
      env_bndr <- newLocalVVar FSLIT("env") env_ty
      arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
241

242
      fn <- hoistPolyVExpr FSLIT("fn") tvs
243
244
245
246
247
          $ do
              body  <- mk_body
              body' <- bind (vVar env_bndr)
                            (mkVVarApps lv body (vars ++ [arg_bndr]))
              return (mkVLams [env_bndr, arg_bndr] body')
248

249
      mkClosure arg_ty res_ty env_ty fn env
250

251
buildEnv :: Var -> [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
252
253
254
255
256
buildEnv lv vvs
  = do
      let (ty, venv, vbind) = mkVectEnv tys vs
      (lenv, lbind) <- mkLiftEnv lv tys ls
      return (ty, (venv, lenv),
257
258
259
260
261
              \(venv,lenv) (vbody,lbody) ->
              do
                let vbody' = vbind venv vbody
                lbody' <- lbind lenv lbody
                return (vbody', lbody'))
262
  where
263
264
    (vs,ls) = unzip vvs
    tys     = map idType vs
265
266
267
268
269
270
271
272
273
274

mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
mkVectEnv []   []  = (unitTy, Var unitDataConId, \env body -> body)
mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
mkVectEnv tys  vs  = (ty, mkCoreTup (map Var vs),
                        \env body -> Case env (mkWildId ty) (exprType body)
                                       [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
  where
    ty = mkCoreTupTy tys

275
mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
276
mkLiftEnv lv [ty] [v]
277
278
279
280
281
  = return (Var v, \env body ->
                   do
                     len <- lengthPA (Var v)
                     return . Let (NonRec v env)
                            $ Case len lv (exprType body) [(DEFAULT, [], body)])
282
283

-- NOTE: this transparently deals with empty environments
284
mkLiftEnv lv tys vs
285
286
287
288
289
  = do
      (env_tc, env_tyargs) <- lookupPArrayFamInst vty
      let [env_con] = tyConDataCons env_tc
          
          env = Var (dataConWrapId env_con)
290
291
                `mkTyApps`  env_tyargs
                `mkVarApps` (lv : vs)
292

293
294
          bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
                          in
295
296
297
                          return $ Case scrut (mkWildId (exprType scrut))
                                        (exprType body)
                                        [(DataAlt env_con, lv : bndrs, body)]
298
299
300
301
      return (env, bind)
  where
    vty = mkCoreTupTy tys

302
303
304
    bndrs | null vs   = [mkWildId unitTy]
          | otherwise = vs