1 {-# OPTIONS -fno-warn-missing-signatures #-}
3 module Vectorise( vectorise )
6 import Vectorise.Type.Env
7 import Vectorise.Type.Type
8 import Vectorise.Convert
9 import Vectorise.Utils.Hoisting
13 import Vectorise.Monad
15 import HscTypes hiding ( MonadThings(..) )
16 import Module ( PackageId )
18 import CoreUnfold ( mkInlineUnfolding )
20 import CoreMonad ( CoreM, getHscEnv )
21 import FamInstEnv ( extendFamInstEnvList )
25 import BasicTypes ( isLoopBreaker )
27 import Util ( zipLazy )
31 dtrace s x = if debug then pprTrace "Vectorise" s x else x
33 -- | Vectorise a single module.
34 -- Takes the package containing the DPH backend we're using. Eg either dph-par or dph-seq.
35 vectorise :: PackageId -> ModGuts -> CoreM ModGuts
36 vectorise backend guts
37 = do hsc_env <- getHscEnv
38 liftIO $ vectoriseIO backend hsc_env guts
41 -- | Vectorise a single monad, given its HscEnv (code gen environment).
42 vectoriseIO :: PackageId -> HscEnv -> ModGuts -> IO ModGuts
43 vectoriseIO backend hsc_env guts
44 = do -- Get information about currently loaded external packages.
47 -- Combine vectorisation info from the current module, and external ones.
48 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
50 -- Run the main VM computation.
51 Just (info', guts') <- initV backend hsc_env guts info (vectModule guts)
52 return (guts' { mg_vect_info = info' })
55 -- | Vectorise a single module, in the VM monad.
56 vectModule :: ModGuts -> VM ModGuts
58 = do -- Vectorise the type environment.
59 -- This may add new TyCons and DataCons.
60 -- TODO: What new binds do we get back here?
61 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
63 -- TODO: What is this?
64 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
65 updGEnv (setFamInstEnv fam_inst_env')
67 -- dicts <- mapM buildPADict pa_insts
68 -- workers <- mapM vectDataConWorkers pa_insts
70 -- Vectorise all the top level bindings.
71 binds' <- mapM vectTopBind (mg_binds guts)
73 return $ guts { mg_types = types'
74 , mg_binds = Rec tc_binds : binds'
75 , mg_fam_inst_env = fam_inst_env'
76 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
80 -- | Try to vectorise a top-level binding.
81 -- If it doesn't vectorise then return it unharmed.
83 -- For example, for the binding
93 -- foo = \x -> vfoo $: x
95 -- v_foo :: Closure void vfoo lfoo
96 -- v_foo = closure vfoo lfoo void
98 -- vfoo :: Void -> Int -> Int
101 -- lfoo :: PData Void -> PData Int -> PData Int
105 -- @vfoo@ is the "vectorised", or scalar, version that does the same as the original
106 -- function foo, but takes an explicit environment.
108 -- @lfoo@ is the "lifted" version that works on arrays.
110 -- @v_foo@ combines both of these into a `Closure` that also contains the
113 -- The original binding @foo@ is rewritten to call the vectorised version
114 -- present in the closure.
116 vectTopBind :: CoreBind -> VM CoreBind
117 vectTopBind b@(NonRec var expr)
119 (inline, expr') <- vectTopRhs var expr
120 var' <- vectTopBinder var inline expr'
122 -- Vectorising the body may create other top-level bindings.
125 -- To get the same functionality as the original body we project
126 -- out its vectorised version from the closure.
127 cexpr <- tryConvert var var' expr
129 return . Rec $ (var, cexpr) : (var', expr') : hs
133 vectTopBind b@(Rec bs)
136 <- fixV $ \ ~(_, inlines, rhss) ->
137 do vars' <- sequence [vectTopBinder var inline rhs
138 | (var, ~(inline, rhs)) <- zipLazy vars (zip inlines rhss)]
140 <- mapAndUnzipM (uncurry vectTopRhs) bs
142 return (vars', inlines', exprs')
145 cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
146 return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
150 (vars, exprs) = unzip bs
153 -- | Make the vectorised version of this top level binder, and add the mapping
154 -- between it and the original to the state. For some binder @foo@ the vectorised
155 -- version is @$v_foo@
157 -- NOTE: vectTopBinder *MUST* be lazy in inline and expr because of how it is
158 -- used inside of fixV in vectTopBind
160 :: Var -- ^ Name of the binding.
161 -> Inline -- ^ Whether it should be inlined, used to annotate it.
162 -> CoreExpr -- ^ RHS of the binding, used to set the `Unfolding` of the returned `Var`.
163 -> VM Var -- ^ Name of the vectorised binding.
165 vectTopBinder var inline expr
167 -- Vectorise the type attached to the var.
168 vty <- vectType (idType var)
170 -- Make the vectorised version of binding's name, and set the unfolding used for inlining.
171 var' <- liftM (`setIdUnfolding` unfolding)
172 $ cloneId mkVectOcc var vty
174 -- Add the mapping between the plain and vectorised name to the state.
175 defGlobalVar var var'
179 unfolding = case inline of
180 Inline arity -> mkInlineUnfolding (Just arity) expr
181 DontInline -> noUnfolding
184 -- | Vectorise the RHS of a top-level binding, in an empty local environment.
186 :: Var -- ^ Name of the binding.
187 -> CoreExpr -- ^ Body of the binding.
188 -> VM (Inline, CoreExpr)
191 = dtrace (vcat [text "vectTopRhs", ppr expr])
193 $ do (inline, vexpr) <- inBind var
194 $ vectPolyExpr (isLoopBreaker $ idOccInfo var)
196 return (inline, vectorised vexpr)
199 -- | Project out the vectorised version of a binding from some closure,
200 -- or return the original body if that doesn't work.
202 :: Var -- ^ Name of the original binding (eg @foo@)
203 -> Var -- ^ Name of vectorised version of binding (eg @$vfoo@)
204 -> CoreExpr -- ^ The original body of the binding.
207 tryConvert var vect_var rhs
208 = fromVect (idType var) (Var vect_var) `orElseV` return rhs