2 module Vectorise ( vectorise )
5 import Vectorise.Type.Env
6 import Vectorise.Type.Type
7 import Vectorise.Convert
8 import Vectorise.Utils.Hoisting
12 import Vectorise.Monad
14 import HscTypes hiding ( MonadThings(..) )
15 import CoreUnfold ( mkInlineUnfolding )
19 import CoreMonad ( CoreM, getHscEnv )
24 import BasicTypes ( isLoopBreaker )
26 import Util ( zipLazy )
32 -- | Vectorise a single module.
34 vectorise :: ModGuts -> CoreM ModGuts
36 = do { hsc_env <- getHscEnv
37 ; liftIO $ vectoriseIO hsc_env guts
40 -- | Vectorise a single monad, given the dynamic compiler flags and HscEnv.
42 vectoriseIO :: HscEnv -> ModGuts -> IO ModGuts
43 vectoriseIO hsc_env guts
44 = do { -- Get information about currently loaded external packages.
45 ; eps <- hscEPS hsc_env
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 hsc_env guts info (vectModule guts)
52 ; return (guts' { mg_vect_info = info' })
55 -- | Vectorise a single module, in the VM monad.
57 vectModule :: ModGuts -> VM ModGuts
58 vectModule guts@(ModGuts { mg_types = types
60 , mg_fam_insts = fam_insts
62 = do { dumpOptVt Opt_D_dump_vt_trace "Before vectorisation" $
65 -- Vectorise the type environment.
66 -- This may add new TyCons and DataCons.
67 ; (types', new_fam_insts, tc_binds) <- vectTypeEnv types
69 ; (_, fam_inst_env) <- readGEnv global_fam_inst_env
71 -- dicts <- mapM buildPADict pa_insts
72 -- workers <- mapM vectDataConWorkers pa_insts
74 -- Vectorise all the top level bindings.
75 ; binds' <- mapM vectTopBind binds
77 ; return $ guts { mg_types = types'
78 , mg_binds = Rec tc_binds : binds'
79 , mg_fam_inst_env = fam_inst_env
80 , mg_fam_insts = fam_insts ++ new_fam_insts
84 -- |Try to vectorise a top-level binding. If it doesn't vectorise then return it unharmed.
86 -- For example, for the binding
96 -- foo = \x -> vfoo $: x
98 -- v_foo :: Closure void vfoo lfoo
99 -- v_foo = closure vfoo lfoo void
101 -- vfoo :: Void -> Int -> Int
104 -- lfoo :: PData Void -> PData Int -> PData Int
108 -- @vfoo@ is the "vectorised", or scalar, version that does the same as the original
109 -- function foo, but takes an explicit environment.
111 -- @lfoo@ is the "lifted" version that works on arrays.
113 -- @v_foo@ combines both of these into a `Closure` that also contains the
116 -- The original binding @foo@ is rewritten to call the vectorised version
117 -- present in the closure.
119 -- Vectorisation may be surpressed by annotating a binding with a 'NOVECTORISE' pragma. If this
120 -- pragma is used in a group of mutually recursive bindings, either all or no binding must have
121 -- the pragma. If only some bindings are annotated, a fatal error is being raised.
122 -- FIXME: Once we support partial vectorisation, we may be able to vectorise parts of a group, or
123 -- we may emit a warning and refrain from vectorising the entire group.
125 vectTopBind :: CoreBind -> VM CoreBind
126 vectTopBind b@(NonRec var expr)
128 do { -- Vectorise the right-hand side, create an appropriate top-level binding and add it
129 -- to the vectorisation map.
130 ; (inline, isScalar, expr') <- vectTopRhs [] var expr
131 ; var' <- vectTopBinder var inline expr'
135 -- We replace the original top-level binding by a value projected from the vectorised
136 -- closure and add any newly created hoisted top-level bindings.
137 ; cexpr <- tryConvert var var' expr
139 ; return . Rec $ (var, cexpr) : (var', expr') : hs
144 unlessNoVectDecl vectorise
145 = do { hasNoVectDecl <- noVectDecl var
146 ; when hasNoVectDecl $
147 traceVt "NOVECTORISE" $ ppr var
148 ; if hasNoVectDecl then return b else vectorise
150 vectTopBind b@(Rec bs)
151 = unlessSomeNoVectDecl $
152 do { (vars', _, exprs', hs) <- fixV $
153 \ ~(_, inlines, rhss, _) ->
154 do { -- Vectorise the right-hand sides, create an appropriate top-level bindings
155 -- and add them to the vectorisation map.
156 ; vars' <- sequence [vectTopBinder var inline rhs
157 | (var, ~(inline, rhs)) <- zipLazy vars (zip inlines rhss)]
158 ; (inlines, areScalars, exprs') <- mapAndUnzip3M (uncurry $ vectTopRhs vars) bs
161 then -- (1) Entire recursive group is scalar
162 -- => add all variables to the global set of scalars
163 do { mapM_ addGlobalScalar vars
164 ; return (vars', inlines, exprs', hs)
166 else -- (2) At least one binding is not scalar
167 -- => vectorise again with empty set of local scalars
168 do { (inlines, _, exprs') <- mapAndUnzip3M (uncurry $ vectTopRhs []) bs
170 ; return (vars', inlines, exprs', hs)
174 -- Replace the original top-level bindings by a values projected from the vectorised
175 -- closures and add any newly created hoisted top-level bindings to the group.
176 ; cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
177 ; return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
182 (vars, exprs) = unzip bs
184 unlessSomeNoVectDecl vectorise
185 = do { hasNoVectDecls <- mapM noVectDecl vars
186 ; when (and hasNoVectDecls) $
187 traceVt "NOVECTORISE" $ ppr vars
188 ; if and hasNoVectDecls
189 then return b -- all bindings have 'NOVECTORISE'
190 else if or hasNoVectDecls
191 then cantVectorise noVectoriseErr (ppr b) -- some (but not all) have 'NOVECTORISE'
192 else vectorise -- no binding has a 'NOVECTORISE' decl
194 noVectoriseErr = "NOVECTORISE must be used on all or no bindings of a recursive group"
196 -- | Make the vectorised version of this top level binder, and add the mapping
197 -- between it and the original to the state. For some binder @foo@ the vectorised
198 -- version is @$v_foo@
200 -- NOTE: 'vectTopBinder' *MUST* be lazy in inline and expr because of how it is
201 -- used inside of 'fixV' in 'vectTopBind'.
203 vectTopBinder :: Var -- ^ Name of the binding.
204 -> Inline -- ^ Whether it should be inlined, used to annotate it.
205 -> CoreExpr -- ^ RHS of binding, used to set the 'Unfolding' of the returned 'Var'.
206 -> VM Var -- ^ Name of the vectorised binding.
207 vectTopBinder var inline expr
208 = do { -- Vectorise the type attached to the var.
209 ; vty <- vectType (idType var)
211 -- If there is a vectorisation declartion for this binding, make sure that its type
213 ; vectDecl <- lookupVectDecl var
217 | eqType vty vdty -> return ()
219 cantVectorise ("Type mismatch in vectorisation pragma for " ++ show var) $
220 (text "Expected type" <+> ppr vty)
222 (text "Inferred type" <+> ppr vdty)
224 -- Make the vectorised version of binding's name, and set the unfolding used for inlining
225 ; var' <- liftM (`setIdUnfoldingLazily` unfolding)
226 $ cloneId mkVectOcc var vty
228 -- Add the mapping between the plain and vectorised name to the state.
229 ; defGlobalVar var var'
234 unfolding = case inline of
235 Inline arity -> mkInlineUnfolding (Just arity) expr
236 DontInline -> noUnfolding
238 -- | Vectorise the RHS of a top-level binding, in an empty local environment.
240 -- We need to distinguish three cases:
242 -- (1) We have a (non-scalar) vectorisation declaration for the variable (which explicitly provides
243 -- vectorised code implemented by the user)
244 -- => no automatic vectorisation & instead use the user-supplied code
246 -- (2) We have a scalar vectorisation declaration for the variable
247 -- => generate vectorised code that uses a scalar 'map'/'zipWith' to lift the computation
249 -- (3) There is no vectorisation declaration for the variable
250 -- => perform automatic vectorisation of the RHS
252 vectTopRhs :: [Var] -- ^ Names of all functions in the rec block
253 -> Var -- ^ Name of the binding.
254 -> CoreExpr -- ^ Body of the binding.
255 -> VM ( Inline -- (1) inline specification for the binding
256 , Bool -- (2) whether the right-hand side is a scalar computation
257 , CoreExpr) -- (3) the vectorised right-hand side
258 vectTopRhs recFs var expr
260 $ do { traceVt ("vectTopRhs of " ++ show var) $ ppr expr
262 ; globalScalar <- isGlobalScalar var
263 ; vectDecl <- lookupVectDecl var
264 ; rhs globalScalar vectDecl
267 rhs _globalScalar (Just (_, expr')) -- Case (1)
268 = return (inlineMe, False, expr')
269 rhs True Nothing -- Case (2)
270 = do { expr' <- vectScalarFun True recFs expr
271 ; return (inlineMe, True, vectorised expr')
273 rhs False Nothing -- Case (3)
274 = do { let fvs = freeVars expr
275 ; (inline, isScalar, vexpr) <- inBind var $
276 vectPolyExpr (isLoopBreaker $ idOccInfo var) recFs fvs
277 ; return (inline, isScalar, vectorised vexpr)
280 -- | Project out the vectorised version of a binding from some closure,
281 -- or return the original body if that doesn't work or the binding is scalar.
283 tryConvert :: Var -- ^ Name of the original binding (eg @foo@)
284 -> Var -- ^ Name of vectorised version of binding (eg @$vfoo@)
285 -> CoreExpr -- ^ The original body of the binding.
287 tryConvert var vect_var rhs
288 = do { globalScalar <- isGlobalScalar var
293 fromVect (idType var) (Var vect_var) `orElseV` return rhs