VectUtils.hs 12.2 KB
Newer Older
1
module VectUtils (
2
  collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
3
  collectAnnValBinders,
4
  mkDataConTag,
5
6
  splitClosureTy,
  mkPADictType, mkPArrayType,
7
  parrayReprTyCon, parrayReprDataCon, mkVScrut,
8
  paDictArgType, paDictOfType, paDFunType,
9
  paMethod, lengthPA, replicatePA, emptyPA, liftPA,
10
  polyAbstract, polyApply, polyVApply,
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
11
  hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
12
13
  buildClosure, buildClosures,
  mkClosureApp
14
15
16
17
) where

#include "HsVersions.h"

18
import VectCore
19
20
import VectMonad

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

35
import Outputable
36
import FastString
37

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

40
41
42
43
44
45
46
47
48
49
50
51
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)

52
53
54
55
56
57
collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
collectAnnValBinders expr = go [] expr
  where
    go bs (_, AnnLam b e) | isId b = go (b:bs) e
    go bs e                        = (reverse bs, e)

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

62
63
64
mkDataConTag :: DataCon -> CoreExpr
mkDataConTag dc = mkConApp intDataCon [mkIntLitInt $ dataConTag dc]

65
isClosureTyCon :: TyCon -> Bool
66
isClosureTyCon tc = tyConName tc == closureTyConName
67
68
69
70
71
72
73
74
75

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)

76
77
78
79
80
81
82
83
84
85
86
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)

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
87
88
mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
mkBuiltinTyConApp get_tc tys
89
  = do
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
90
91
      tc <- builtin get_tc
      return $ mkTyConApp tc tys
92

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
93
94
mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
mkBuiltinTyConApps get_tc tys ty
95
  = do
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
96
97
      tc <- builtin get_tc
      return $ foldr (mk tc) ty tys
98
  where
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
99
    mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
100

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
101
102
mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> [Type] -> VM Type
mkBuiltinTyConApps1 get_tc tys
103
  = do
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
      tc <- builtin get_tc
      case tys of
        [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
        _  -> return $ foldr1 (mk tc) tys
  where
    mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]

mkClosureType :: Type -> Type -> VM Type
mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]

mkClosureTypes :: [Type] -> Type -> VM Type
mkClosureTypes = mkBuiltinTyConApps closureTyCon

mkPADictType :: Type -> VM Type
mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
119
120

mkPArrayType :: Type -> VM Type
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
121
mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
122

123
124
125
126
127
128
129
130
131
132
parrayReprTyCon :: Type -> VM (TyCon, [Type])
parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])

parrayReprDataCon :: Type -> VM (DataCon, [Type])
parrayReprDataCon ty
  = do
      (tc, arg_tys) <- parrayReprTyCon ty
      let [dc] = tyConDataCons tc
      return (dc, arg_tys)

133
134
135
136
137
138
mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
mkVScrut (ve, le)
  = do
      (tc, arg_tys) <- parrayReprTyCon (exprType ve)
      return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
155
      = liftM Just (mkPADictType ty)
156
157
158

    go ty k = return Nothing

159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
173
      dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
174
      paDFunApply (Var dfun) ty_args
175
176
paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)

177
178
179
180
181
182
183
184
185
186
187
paDFunType :: TyCon -> VM Type
paDFunType tc
  = do
      margs <- mapM paDictArgType tvs
      res   <- mkPADictType (mkTyConApp tc arg_tys)
      return . mkForAllTys tvs
             $ mkFunTys [arg | Just arg <- margs] res
  where
    tvs = tyConTyVars tc
    arg_tys = mkTyVarTys tvs

188
189
190
191
192
193
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
194
195
196
197
198
199
200
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
201
lengthPA :: CoreExpr -> VM CoreExpr
202
203
204
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
205
206
207
208
209

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

210
211
212
emptyPA :: Type -> VM CoreExpr
emptyPA = paMethod emptyPAVar

213
214
215
216
217
218
liftPA :: CoreExpr -> VM CoreExpr
liftPA x
  = do
      lc <- builtin liftingContext
      replicatePA (Var lc) x

219
220
221
222
223
224
225
226
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
227
228
229
230
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
231
232
233
234
235
236
237
238
239
240
241
242
      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
243
244
polyApply :: CoreExpr -> [Type] -> VM CoreExpr
polyApply expr tys
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
245
246
247
248
  = do
      dicts <- mapM paDictOfType tys
      return $ expr `mkTyApps` tys `mkApps` dicts

249
250
251
252
253
254
polyVApply :: VExpr -> [Type] -> VM VExpr
polyVApply expr tys
  = do
      dicts <- mapM paDictOfType tys
      return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
255
256
257
258
hoistBinding :: Var -> CoreExpr -> VM ()
hoistBinding v e = updGEnv $ \env ->
  env { global_bindings = (v,e) : global_bindings env }

259
260
261
262
hoistExpr :: FastString -> CoreExpr -> VM Var
hoistExpr fs expr
  = do
      var <- newLocalVar fs (exprType expr)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
263
      hoistBinding var expr
264
265
      return var

266
267
hoistVExpr :: VExpr -> VM VVar
hoistVExpr (ve, le)
268
  = do
269
      fs <- getBindName
270
271
272
      vv <- hoistExpr ('v' `consFS` fs) ve
      lv <- hoistExpr ('l' `consFS` fs) le
      return (vv, lv)
273

274
275
hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
hoistPolyVExpr tvs p
276
  = do
277
278
      expr <- closedV . polyAbstract tvs $ \abstract ->
              liftM (mapVect abstract) p
279
      fn   <- hoistVExpr expr
280
      polyVApply (vVar fn) (mkTyVarTys tvs)
281

282
283
284
285
286
287
288
takeHoisted :: VM [(Var, CoreExpr)]
takeHoisted
  = do
      env <- readGEnv id
      setGEnv $ env { global_bindings = [] }
      return $ global_bindings env

289
290
mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
291
  = do
292
293
294
295
296
      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])
297

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
298
299
300
301
302
303
304
305
306
307
mkClosureApp :: VExpr -> VExpr -> VM VExpr
mkClosureApp (vclo, lclo) (varg, larg)
  = do
      vapply <- builtin applyClosureVar
      lapply <- builtin applyClosurePVar
      return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
              Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
  where
    (arg_ty, res_ty) = splitClosureTy (exprType vclo)

308
buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
309
310
buildClosures tvs vars [] res_ty mk_body
  = mk_body
311
312
313
buildClosures tvs vars [arg_ty] res_ty mk_body
  = buildClosure tvs vars arg_ty res_ty mk_body
buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
314
315
316
  = do
      res_ty' <- mkClosureTypes arg_tys res_ty
      arg <- newLocalVVar FSLIT("x") arg_ty
317
      buildClosure tvs vars arg_ty res_ty'
318
        . hoistPolyVExpr tvs
319
        $ do
320
321
            lc <- builtin liftingContext
            clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
322
323
            return $ vLams lc (vars ++ [arg]) clo

324
325
326
327
-- (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
328
--
329
330
buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
buildClosure tvs vars arg_ty res_ty mk_body
331
  = do
332
      (env_ty, env, bind) <- buildEnv vars
333
334
      env_bndr <- newLocalVVar FSLIT("env") env_ty
      arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
335

336
      fn <- hoistPolyVExpr tvs
337
          $ do
338
              lc    <- builtin liftingContext
339
340
              body  <- mk_body
              body' <- bind (vVar env_bndr)
341
                            (vVarApps lc body (vars ++ [arg_bndr]))
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
342
              return (vLamsWithoutLC [env_bndr, arg_bndr] body')
343

344
      mkClosure arg_ty res_ty env_ty fn env
345

346
347
buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
buildEnv vvs
348
  = do
349
      lc <- builtin liftingContext
350
      let (ty, venv, vbind) = mkVectEnv tys vs
351
      (lenv, lbind) <- mkLiftEnv lc tys ls
352
      return (ty, (venv, lenv),
353
354
355
356
357
              \(venv,lenv) (vbody,lbody) ->
              do
                let vbody' = vbind venv vbody
                lbody' <- lbind lenv lbody
                return (vbody', lbody'))
358
  where
359
360
    (vs,ls) = unzip vvs
    tys     = map idType vs
361
362
363
364
365
366
367
368
369
370

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

371
mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
372
mkLiftEnv lc [ty] [v]
373
374
375
376
  = return (Var v, \env body ->
                   do
                     len <- lengthPA (Var v)
                     return . Let (NonRec v env)
377
                            $ Case len lc (exprType body) [(DEFAULT, [], body)])
378
379

-- NOTE: this transparently deals with empty environments
380
mkLiftEnv lc tys vs
381
  = do
382
      (env_tc, env_tyargs) <- parrayReprTyCon vty
383
384
385
      let [env_con] = tyConDataCons env_tc
          
          env = Var (dataConWrapId env_con)
386
                `mkTyApps`  env_tyargs
387
                `mkVarApps` (lc : vs)
388

389
390
          bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
                          in
391
392
                          return $ Case scrut (mkWildId (exprType scrut))
                                        (exprType body)
393
                                        [(DataAlt env_con, lc : bndrs, body)]
394
395
396
397
      return (env, bind)
  where
    vty = mkCoreTupTy tys

398
399
400
    bndrs | null vs   = [mkWildId unitTy]
          | otherwise = vs