Env.hs 5.81 KB
Newer Older
Ian Lynagh's avatar
Ian Lynagh committed
1 2 3 4
{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
#if __GLASGOW_HASKELL__ >= 611
{-# OPTIONS_GHC -XNoMonoLocalBinds #-}
#endif
5 6
-- Roman likes local bindings
-- If this module lives on I'd like to get rid of this flag in due course
7

8
module Vectorise.Type.Env ( 
9 10
	vectTypeEnv,
)
11
where
12
import Vectorise.Env
13
import Vectorise.Vect
14 15
import Vectorise.Monad
import Vectorise.Builtins
16
import Vectorise.Type.TyConDecl
17
import Vectorise.Type.Classify
18
import Vectorise.Type.PADict
19 20 21
import Vectorise.Type.PData
import Vectorise.Type.PRepr
import Vectorise.Type.Repr
22
import Vectorise.Utils
23

24
import HscTypes
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
25
import CoreSyn
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
26
import CoreUtils
27
import CoreUnfold
28
import DataCon
29 30
import TyCon
import Type
31
import FamInstEnv
32
import OccName
33
import Id
34
import MkId
35
import Var
36
import NameEnv
37

38
import Unique
39
import UniqFM
40
import Util
41
import Outputable
42
import FastString
43 44
import MonadUtils
import Control.Monad
45 46 47 48
import Data.List

debug		= False
dtrace s x	= if debug then pprTrace "VectType" s x else x
49

50

51 52
-- | Vectorise a type environment.
--   The type environment contains all the type things defined in a module.
53 54 55 56 57 58
vectTypeEnv 
	:: TypeEnv
	-> VM ( TypeEnv			-- Vectorised type environment.
	      , [FamInst]		-- New type family instances.
	      , [(Var, CoreExpr)])	-- New top level bindings.
	
59
vectTypeEnv env
60 61
 = dtrace (ppr env)
 $ do
62
      cs <- readGEnv $ mk_map . global_tycons
63 64 65 66

      -- Split the list of TyCons into the ones we have to vectorise vs the
      -- ones we can pass through unchanged. We also pass through algebraic 
      -- types that use non Haskell98 features, as we don't handle those.
67 68
      let (conv_tcs, keep_tcs) = classifyTyCons cs groups
          keep_dcs             = concatMap tyConDataCons keep_tcs
69

70 71
      zipWithM_ defTyCon   keep_tcs keep_tcs
      zipWithM_ defDataCon keep_dcs keep_dcs
72

73 74 75
      new_tcs <- vectTyConDecls conv_tcs

      let orig_tcs = keep_tcs ++ conv_tcs
76 77 78 79 80 81 82 83

      -- We don't need to make new representation types for dictionary
      -- constructors. The constructors are always fully applied, and we don't 
      -- need to lift them to arrays as a dictionary of a particular type
      -- always has the same value.
      let vect_tcs = filter (not . isClassTyCon) 
                   $ keep_tcs ++ new_tcs

84 85 86
      (_, binds, inst_tcs) <- fixV $ \ ~(dfuns', _, _) ->
        do
          defTyConPAs (zipLazy vect_tcs dfuns')
87
          reprs     <- mapM tyConRepr vect_tcs
88 89
          repr_tcs  <- zipWith3M buildPReprTyCon orig_tcs vect_tcs reprs
          pdata_tcs <- zipWith3M buildPDataTyCon orig_tcs vect_tcs reprs
90 91 92 93 94 95 96 97 98 99

          dfuns     <- sequence 
                    $  zipWith5 buildTyConBindings
                               orig_tcs
                               vect_tcs
                               repr_tcs
                               pdata_tcs
                               reprs

          binds     <- takeHoisted
100 101 102
          return (dfuns, binds, repr_tcs ++ pdata_tcs)

      let all_new_tcs = new_tcs ++ inst_tcs
103 104

      let new_env = extendTypeEnvList env
105 106
                       (map ATyCon all_new_tcs
                        ++ [ADataCon dc | tc <- all_new_tcs
107 108
                                        , dc <- tyConDataCons tc])

109
      return (new_env, map mkLocalFamInst inst_tcs, binds)
110 111 112 113 114 115 116
  where
    tycons = typeEnvTyCons env
    groups = tyConGroups tycons

    mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]


117

118
buildTyConBindings :: TyCon -> TyCon -> TyCon -> TyCon -> SumRepr -> VM Var
119
buildTyConBindings orig_tc vect_tc prepr_tc pdata_tc repr
120
 = do vectDataConWorkers orig_tc vect_tc pdata_tc
121
      buildPADict vect_tc prepr_tc pdata_tc repr
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
122

123

124 125
vectDataConWorkers :: TyCon -> TyCon -> TyCon -> VM ()
vectDataConWorkers orig_tc vect_tc arr_tc
126
 = do bs <- sequence
127 128 129
          . zipWith3 def_worker  (tyConDataCons orig_tc) rep_tys
          $ zipWith4 mk_data_con (tyConDataCons vect_tc)
                                 rep_tys
130 131
                                 (inits rep_tys)
                                 (tail $ tails rep_tys)
132
      mapM_ (uncurry hoistBinding) bs
133
 where
134 135 136 137 138
    tyvars   = tyConTyVars vect_tc
    var_tys  = mkTyVarTys tyvars
    ty_args  = map Type var_tys
    res_ty   = mkTyConApp vect_tc var_tys

139 140 141 142
    cons     = tyConDataCons vect_tc
    arity    = length cons
    [arr_dc] = tyConDataCons arr_tc

143 144 145 146 147
    rep_tys  = map dataConRepArgTys $ tyConDataCons vect_tc


    mk_data_con con tys pre post
      = liftM2 (,) (vect_data_con con)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
148
                   (lift_data_con tys pre post (mkDataConTag con))
149

150 151 152 153 154 155 156
    sel_replicate len tag
      | arity > 1 = do
                      rep <- builtin (selReplicate arity)
                      return [rep `mkApps` [len, tag]]

      | otherwise = return []

157
    vect_data_con con = return $ mkConApp con ty_args
158
    lift_data_con tys pre_tys post_tys tag
159 160
      = do
          len  <- builtin liftingContext
Ian Lynagh's avatar
Ian Lynagh committed
161
          args <- mapM (newLocalVar (fsLit "xs"))
162
                  =<< mapM mkPDataType tys
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
163

164
          sel  <- sel_replicate (Var len) tag
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
165

166 167
          pre   <- mapM emptyPD (concat pre_tys)
          post  <- mapM emptyPD (concat post_tys)
168 169 170 171

          return . mkLams (len : args)
                 . wrapFamInstBody arr_tc var_tys
                 . mkConApp arr_dc
172
                 $ ty_args ++ sel ++ pre ++ map Var args ++ post
173 174 175

    def_worker data_con arg_tys mk_body
      = do
176
          arity <- polyArity tyvars
177 178
          body <- closedV
                . inBind orig_worker
179 180
                . polyAbstract tyvars $ \args ->
                  liftM (mkLams (tyvars ++ args) . vectorised)
181 182
                $ buildClosures tyvars [] arg_tys res_ty mk_body

183 184
          raw_worker <- cloneId mkVectOcc orig_worker (exprType body)
          let vect_worker = raw_worker `setIdUnfolding`
185
                              mkInlineRule body (Just arity)
186 187 188 189 190
          defGlobalVar orig_worker vect_worker
          return (vect_worker, body)
      where
        orig_worker = dataConWorkId data_con