2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section[WorkWrap]{Worker/wrapper-generating back-end of strictness analyser}
7 module WorkWrap ( wwTopBinds, mkWrapper ) where
9 #include "HsVersions.h"
12 import CoreUnfold ( certainlyWillInline )
13 import CoreLint ( showPass, endPass )
14 import CoreUtils ( exprType )
15 import Id ( Id, idType, idStrictness, idArity, isOneShotLambda,
16 setIdStrictness, idInlinePragma, mkWorkerId,
17 setIdWorkerInfo, idCprInfo, setInlinePragma )
18 import Type ( Type, isNewType, splitForAllTys, splitFunTys )
19 import IdInfo ( mkStrictnessInfo, noStrictnessInfo, StrictnessInfo(..),
20 CprInfo(..), InlinePragInfo(..), isNeverInlinePrag,
23 import Demand ( Demand )
24 import UniqSupply ( UniqSupply, initUs_, returnUs, thenUs, mapUs, getUniqueUs, UniqSM )
30 We take Core bindings whose binders have:
34 \item Strictness attached (by the front-end of the strictness
37 \item Constructed Product Result information attached by the CPR
42 and we return some ``plain'' bindings which have been
43 worker/wrapper-ified, meaning:
47 \item Functions have been split into workers and wrappers where
48 appropriate. If a function has both strictness and CPR properties
49 then only one worker/wrapper doing both transformations is produced;
51 \item Binders' @IdInfos@ have been updated to reflect the existence of
52 these workers/wrappers (this is where we get STRICTNESS and CPR pragma
53 info for exported values).
58 wwTopBinds :: DynFlags
63 wwTopBinds dflags us binds
65 showPass dflags "Worker Wrapper binds";
67 -- Create worker/wrappers, and mark binders with their
68 -- "strictness info" [which encodes their worker/wrapper-ness]
69 let { binds' = workersAndWrappers us binds };
71 endPass dflags "Worker Wrapper binds"
72 Opt_D_dump_worker_wrapper binds'
78 workersAndWrappers :: UniqSupply -> [CoreBind] -> [CoreBind]
80 workersAndWrappers us top_binds
82 mapUs wwBind top_binds `thenUs` \ top_binds' ->
83 returnUs (concat top_binds')
86 %************************************************************************
88 \subsection[wwBind-wwExpr]{@wwBind@ and @wwExpr@}
90 %************************************************************************
92 @wwBind@ works on a binding, trying each \tr{(binder, expr)} pair in
93 turn. Non-recursive case first, then recursive...
97 -> UniqSM [CoreBind] -- returns a WwBinding intermediate form;
98 -- the caller will convert to Expr/Binding,
101 wwBind (NonRec binder rhs)
102 = wwExpr rhs `thenUs` \ new_rhs ->
103 tryWW True {- non-recursive -} binder new_rhs `thenUs` \ new_pairs ->
104 returnUs [NonRec b e | (b,e) <- new_pairs]
105 -- Generated bindings must be non-recursive
106 -- because the original binding was.
108 ------------------------------
111 = mapUs do_one pairs `thenUs` \ new_pairs ->
112 returnUs [Rec (concat new_pairs)]
114 do_one (binder, rhs) = wwExpr rhs `thenUs` \ new_rhs ->
115 tryWW False {- recursive -} binder new_rhs
118 @wwExpr@ basically just walks the tree, looking for appropriate
119 annotations that can be used. Remember it is @wwBind@ that does the
120 matching by looking for strict arguments of the correct type.
121 @wwExpr@ is a version that just returns the ``Plain'' Tree.
124 wwExpr :: CoreExpr -> UniqSM CoreExpr
126 wwExpr e@(Type _) = returnUs e
127 wwExpr e@(Var _) = returnUs e
128 wwExpr e@(Lit _) = returnUs e
130 wwExpr (Lam binder expr)
131 = wwExpr expr `thenUs` \ new_expr ->
132 returnUs (Lam binder new_expr)
135 = wwExpr f `thenUs` \ new_f ->
136 wwExpr a `thenUs` \ new_a ->
137 returnUs (App new_f new_a)
139 wwExpr (Note note expr)
140 = wwExpr expr `thenUs` \ new_expr ->
141 returnUs (Note note new_expr)
143 wwExpr (Let bind expr)
144 = wwBind bind `thenUs` \ intermediate_bind ->
145 wwExpr expr `thenUs` \ new_expr ->
146 returnUs (mkLets intermediate_bind new_expr)
148 wwExpr (Case expr binder alts)
149 = wwExpr expr `thenUs` \ new_expr ->
150 mapUs ww_alt alts `thenUs` \ new_alts ->
151 returnUs (Case new_expr binder new_alts)
153 ww_alt (con, binders, rhs)
154 = wwExpr rhs `thenUs` \ new_rhs ->
155 returnUs (con, binders, new_rhs)
158 %************************************************************************
160 \subsection[tryWW]{@tryWW@: attempt a worker/wrapper pair}
162 %************************************************************************
164 @tryWW@ just accumulates arguments, converts strictness info from the
165 front-end into the proper form, then calls @mkWwBodies@ to do
168 We have to BE CAREFUL that we don't worker-wrapperize an Id that has
169 already been w-w'd! (You can end up with several liked-named Ids
170 bouncing around at the same time---absolute mischief.) So the
171 criterion we use is: if an Id already has an unfolding (for whatever
172 reason), then we don't w-w it.
174 The only reason this is monadised is for the unique supply.
177 tryWW :: Bool -- True <=> a non-recursive binding
178 -> Id -- The fn binder
179 -> CoreExpr -- The bound rhs; its innards
181 -> UniqSM [(Id, CoreExpr)] -- either *one* or *two* pairs;
182 -- if one, then no worker (only
183 -- the orig "wrapper" lives on);
184 -- if two, then a worker and a
186 tryWW non_rec fn_id rhs
187 | isNeverInlinePrag inline_prag || arity == 0
188 = -- Don't split things that will never be inlined
189 returnUs [ (fn_id, rhs) ]
191 | non_rec && certainlyWillInline fn_id
192 -- No point in worker/wrappering a function that is going to be
193 -- INLINEd wholesale anyway. If the strictness analyser is run
194 -- twice, this test also prevents wrappers (which are INLINEd)
195 -- from being re-done.
197 -- It's very important to refrain from w/w-ing an INLINE function
198 -- If we do so by mistake we transform
199 -- f = __inline (\x -> E)
201 -- f = __inline (\x -> case x of (a,b) -> fw E)
202 -- fw = \ab -> (__inline (\x -> E)) (a,b)
203 -- and the original __inline now vanishes, so E is no longer
204 -- inside its __inline wrapper. Death! Disaster!
207 -- [There used to be "&& not do_coerce_ww" in the above test.
208 -- No longer necessary because SimplUtils.tryEtaExpansion
209 -- now deals with coerces.]
210 -- The do_coerce_ww test is so that
211 -- a function with a coerce should w/w to get rid
212 -- of the coerces, which can significantly improve its arity.
213 -- Example: f [] = return [] :: IO [Int]
214 -- f (x:xs) = return (x:xs)
215 -- If we aren't careful we end up with
216 -- f = \ x -> case x of {
217 -- x:xs -> __coerce (IO [Int]) (\ s -> (# s, x:xs #)
221 -- [Out of date because the size calculation in CoreUnfold now
222 -- makes wrappers look very cheap even when they are inlined.]
223 -- In this case we add an INLINE pragma to the RHS. Why?
226 -- g = \yz -> ... -- And g is strict
227 -- Then f is small, so we don't w/w it. But g is big, and we do, so
228 -- g's wrapper will get inlined in f's RHS, which makes f look big now.
229 -- So f doesn't get inlined, but it is strict and we have failed to w/w it.
230 = returnUs [ (fn_id, rhs) ]
232 | not (do_strict_ww || do_cpr_ww || do_coerce_ww)
233 = returnUs [ (fn_id, rhs) ]
235 | otherwise -- Do w/w split
236 = mkWwBodies fun_ty arity wrap_dmds result_bot one_shots cpr_info `thenUs` \ (work_demands, wrap_fn, work_fn) ->
237 getUniqueUs `thenUs` \ work_uniq ->
239 work_rhs = work_fn rhs
240 proto_work_id = mkWorkerId work_uniq fn_id (exprType work_rhs)
241 `setInlinePragma` inline_prag
243 work_id | has_strictness = proto_work_id `setIdStrictness` mkStrictnessInfo (work_demands, result_bot)
244 | otherwise = proto_work_id
246 wrap_rhs = wrap_fn work_id
247 wrap_id = fn_id `setIdStrictness` wrapper_strictness
248 `setIdWorkerInfo` HasWorker work_id arity
249 `setInlinePragma` NoInlinePragInfo -- Put it on the worker instead
250 -- Add info to the wrapper:
251 -- (a) we want to set its arity
252 -- (b) we want to pin on its revised strictness info
253 -- (c) we pin on its worker id
255 returnUs ([(work_id, work_rhs), (wrap_id, wrap_rhs)])
256 -- Worker first, because wrapper mentions it
257 -- mkWwBodies has already built a wrap_rhs with an INLINE pragma wrapped around it
259 fun_ty = idType fn_id
260 arity = idArity fn_id -- The arity is set by the simplifier using exprEtaExpandArity
261 -- So it may be more than the number of top-level-visible lambdas
263 inline_prag = idInlinePragma fn_id
265 strictness_info = idStrictness fn_id
266 has_strictness = case strictness_info of
267 StrictnessInfo _ _ -> True
268 NoStrictnessInfo -> False
269 (arg_demands, result_bot) = case strictness_info of
270 StrictnessInfo d r -> (d, r)
271 NoStrictnessInfo -> ([], False)
273 wrap_dmds = setUnpackStrategy arg_demands
274 do_strict_ww = WARN( has_strictness && not result_bot && arity < length arg_demands && worthSplitting wrap_dmds result_bot,
275 text "Insufficient arity" <+> ppr fn_id <+> ppr arity <+> ppr arg_demands )
276 (result_bot || arity >= length arg_demands) -- Only if there's enough visible arity
277 && -- (else strictness info isn't valid)
279 worthSplitting wrap_dmds result_bot -- And it's useful
280 -- worthSplitting returns False for an empty list of demands,
281 -- and hence do_strict_ww is False if arity is zero
282 -- Also it's false if there is no strictness (arg_demands is [])
284 wrapper_strictness | has_strictness = mkStrictnessInfo (wrap_dmds, result_bot)
285 | otherwise = noStrictnessInfo
287 -------------------------------------------------------------
288 cpr_info = idCprInfo fn_id
289 do_cpr_ww = arity > 0 &&
294 -------------------------------------------------------------
295 do_coerce_ww = check_for_coerce arity fun_ty
296 -- We are willing to do a w/w even if the arity is zero.
302 -------------------------------------------------------------
303 one_shots = get_one_shots rhs
305 -- See if there's a Coerce before we run out of arity;
306 -- if so, it's worth trying a w/w split. Reason: we find
307 -- functions like f = coerce (\s -> e)
308 -- and g = \x -> coerce (\s -> e)
309 -- and they may have no useful strictness or cpr info, but if we
310 -- do the w/w thing we get rid of the coerces.
312 check_for_coerce arity ty
313 = length arg_tys <= arity && isNewType res_ty
314 -- Don't look further than arity args,
315 -- but if there are arity or fewer, see if there's
316 -- a newtype in the corner
318 (_, tau) = splitForAllTys ty
319 (arg_tys, res_ty) = splitFunTys tau
321 -- If the original function has one-shot arguments, it is important to
322 -- make the wrapper and worker have corresponding one-shot arguments too.
323 -- Otherwise we spuriously float stuff out of case-expression join points,
324 -- which is very annoying.
325 get_one_shots (Lam b e)
326 | isId b = isOneShotLambda b : get_one_shots e
327 | otherwise = get_one_shots e
328 get_one_shots (Note _ e) = get_one_shots e
329 get_one_shots other = noOneShotInfo
334 %************************************************************************
336 \subsection{The worker wrapper core}
338 %************************************************************************
340 @mkWrapper@ is called when importing a function. We have the type of
341 the function and the name of its worker, and we want to make its body (the wrapper).
344 mkWrapper :: Type -- Wrapper type
346 -> [Demand] -- Wrapper strictness info
347 -> Bool -- Function returns bottom
348 -> CprInfo -- Wrapper cpr info
349 -> UniqSM (Id -> CoreExpr) -- Wrapper body, missing worker Id
351 mkWrapper fun_ty arity demands res_bot cpr_info
352 = mkWwBodies fun_ty arity demands res_bot noOneShotInfo cpr_info `thenUs` \ (_, wrap_fn, _) ->
355 noOneShotInfo = repeat False