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 )
33 dtrace s x = if debug then pprTrace "Vectorise" s x else x
35 -- | Vectorise a single module.
36 -- Takes the package containing the DPH backend we're using. Eg either dph-par or dph-seq.
37 vectorise :: PackageId -> ModGuts -> CoreM ModGuts
38 vectorise backend guts
39 = do hsc_env <- getHscEnv
40 liftIO $ vectoriseIO backend hsc_env guts
43 -- | Vectorise a single monad, given its HscEnv (code gen environment).
44 vectoriseIO :: PackageId -> HscEnv -> ModGuts -> IO ModGuts
45 vectoriseIO backend hsc_env guts
46 = do -- Get information about currently loaded external packages.
49 -- Combine vectorisation info from the current module, and external ones.
50 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
52 -- Run the main VM computation.
53 Just (info', guts') <- initV backend hsc_env guts info (vectModule guts)
54 return (guts' { mg_vect_info = info' })
57 -- | Vectorise a single module, in the VM monad.
58 vectModule :: ModGuts -> VM ModGuts
60 = do -- Vectorise the type environment.
61 -- This may add new TyCons and DataCons.
62 -- TODO: What new binds do we get back here?
63 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
65 -- TODO: What is this?
66 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
67 updGEnv (setFamInstEnv fam_inst_env')
69 -- dicts <- mapM buildPADict pa_insts
70 -- workers <- mapM vectDataConWorkers pa_insts
72 -- Vectorise all the top level bindings.
73 binds' <- mapM vectTopBind (mg_binds guts)
75 return $ guts { mg_types = types'
76 , mg_binds = Rec tc_binds : binds'
77 , mg_fam_inst_env = fam_inst_env'
78 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
82 -- | Try to vectorise a top-level binding.
83 -- If it doesn't vectorise then return it unharmed.
85 -- For example, for the binding
95 -- foo = \x -> vfoo $: x
97 -- v_foo :: Closure void vfoo lfoo
98 -- v_foo = closure vfoo lfoo void
100 -- vfoo :: Void -> Int -> Int
103 -- lfoo :: PData Void -> PData Int -> PData Int
107 -- @vfoo@ is the "vectorised", or scalar, version that does the same as the original
108 -- function foo, but takes an explicit environment.
110 -- @lfoo@ is the "lifted" version that works on arrays.
112 -- @v_foo@ combines both of these into a `Closure` that also contains the
115 -- The original binding @foo@ is rewritten to call the vectorised version
116 -- present in the closure.
118 vectTopBind :: CoreBind -> VM CoreBind
119 vectTopBind b@(NonRec var expr)
121 (inline, expr') <- vectTopRhs var expr
122 var' <- vectTopBinder var inline expr'
124 -- Vectorising the body may create other top-level bindings.
127 -- To get the same functionality as the original body we project
128 -- out its vectorised version from the closure.
129 cexpr <- tryConvert var var' expr
131 return . Rec $ (var, cexpr) : (var', expr') : hs
135 vectTopBind b@(Rec bs)
138 <- fixV $ \ ~(_, inlines, rhss) ->
139 do vars' <- sequence [vectTopBinder var inline rhs
140 | (var, ~(inline, rhs)) <- zipLazy vars (zip inlines rhss)]
142 <- mapAndUnzipM (uncurry vectTopRhs) bs
144 return (vars', inlines', exprs')
147 cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
148 return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
152 (vars, exprs) = unzip bs
155 -- | Make the vectorised version of this top level binder, and add the mapping
156 -- between it and the original to the state. For some binder @foo@ the vectorised
157 -- version is @$v_foo@
159 -- NOTE: vectTopBinder *MUST* be lazy in inline and expr because of how it is
160 -- used inside of fixV in vectTopBind
162 :: Var -- ^ Name of the binding.
163 -> Inline -- ^ Whether it should be inlined, used to annotate it.
164 -> CoreExpr -- ^ RHS of the binding, used to set the `Unfolding` of the returned `Var`.
165 -> VM Var -- ^ Name of the vectorised binding.
167 vectTopBinder var inline expr
169 -- Vectorise the type attached to the var.
170 vty <- vectType (idType var)
172 -- Make the vectorised version of binding's name, and set the unfolding used for inlining.
173 var' <- liftM (`setIdUnfoldingLazily` unfolding)
174 $ cloneId mkVectOcc var vty
176 -- Add the mapping between the plain and vectorised name to the state.
177 defGlobalVar var var'
181 unfolding = case inline of
182 Inline arity -> mkInlineUnfolding (Just arity) expr
183 DontInline -> noUnfolding
186 -- | Vectorise the RHS of a top-level binding, in an empty local environment.
188 :: Var -- ^ Name of the binding.
189 -> CoreExpr -- ^ Body of the binding.
190 -> VM (Inline, CoreExpr)
193 = dtrace (vcat [text "vectTopRhs", ppr expr])
195 $ do (inline, vexpr) <- inBind var
196 $ vectPolyExpr (isLoopBreaker $ idOccInfo var)
198 return (inline, vectorised vexpr)
201 -- | Project out the vectorised version of a binding from some closure,
202 -- or return the original body if that doesn't work.
204 :: Var -- ^ Name of the original binding (eg @foo@)
205 -> Var -- ^ Name of vectorised version of binding (eg @$vfoo@)
206 -> CoreExpr -- ^ The original body of the binding.
209 tryConvert var vect_var rhs
210 = fromVect (idType var) (Var vect_var) `orElseV` return rhs