2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section[WwLib]{A library for the ``worker/wrapper'' back-end to the strictness analyser}
7 module WwLib ( mkWwBodies ) where
9 #include "HsVersions.h"
12 import CoreUtils ( exprType )
13 import Id ( Id, idType, mkSysLocal, idNewDemandInfo, setIdNewDemandInfo,
14 isOneShotLambda, setOneShotLambda,
17 import IdInfo ( vanillaIdInfo )
18 import DataCon ( splitProductType_maybe, splitProductType )
19 import NewDemand ( Demand(..), Keepity(..), DmdResult(..) )
20 import PrelInfo ( realWorldPrimId, aBSENT_ERROR_ID, eRROR_CSTRING_ID )
21 import TysPrim ( realWorldStatePrimTy )
22 import TysWiredIn ( tupleCon )
23 import Type ( Type, isUnLiftedType, mkFunTys,
24 splitForAllTys, splitFunTys, splitNewType_maybe, isAlgType
26 import Literal ( Literal(MachStr) )
27 import BasicTypes ( Arity, Boxity(..) )
28 import Var ( Var, isId )
29 import UniqSupply ( returnUs, thenUs, getUniqueUs, getUniquesUs, UniqSM )
30 import Util ( zipWithEqual )
32 import List ( zipWith4 )
36 %************************************************************************
38 \subsection[mkWrapperAndWorker]{@mkWrapperAndWorker@}
40 %************************************************************************
42 Here's an example. The original function is:
45 g :: forall a . Int -> [a] -> a
53 From this, we want to produce:
55 -- wrapper (an unfolding)
56 g :: forall a . Int -> [a] -> a
61 -- call the worker; don't forget the type args!
64 $wg :: forall a . Int# -> [a] -> a
66 $wg = /\ a -> \ x# ys ->
70 case x of -- note: body of g moved intact
75 Something we have to be careful about: Here's an example:
78 -- "f" strictness: U(P)U(P)
79 f (I# a) (I# b) = a +# b
81 g = f -- "g" strictness same as "f"
84 \tr{f} will get a worker all nice and friendly-like; that's good.
85 {\em But we don't want a worker for \tr{g}}, even though it has the
86 same strictness as \tr{f}. Doing so could break laziness, at best.
88 Consequently, we insist that the number of strictness-info items is
89 exactly the same as the number of lambda-bound arguments. (This is
90 probably slightly paranoid, but OK in practice.) If it isn't the
91 same, we ``revise'' the strictness info, so that we won't propagate
92 the unusable strictness-info into the interfaces.
95 %************************************************************************
97 \subsection{The worker wrapper core}
99 %************************************************************************
101 @mkWwBodies@ is called when doing the worker/wrapper split inside a module.
104 mkWwBodies :: Type -- Type of original function
105 -> [Demand] -- Strictness of original function
106 -> DmdResult -- Info about function result
107 -> [Bool] -- One-shot-ness of the function
108 -> UniqSM ([Demand], -- Demands for worker (value) args
109 Id -> CoreExpr, -- Wrapper body, lacking only the worker Id
110 CoreExpr -> CoreExpr) -- Worker body, lacking the original function rhs
112 -- wrap_fn_args E = \x y -> E
113 -- work_fn_args E = E x y
115 -- wrap_fn_str E = case x of { (a,b) ->
116 -- case a of { (a1,a2) ->
118 -- work_fn_str E = \a2 a2 b y ->
119 -- let a = (a1,a2) in
123 mkWwBodies fun_ty demands res_info one_shots
124 = mkWWargs fun_ty demands one_shots' `thenUs` \ (wrap_args, wrap_fn_args, work_fn_args, res_ty) ->
125 mkWWcpr res_ty res_info `thenUs` \ (wrap_fn_cpr, work_fn_cpr, cpr_res_ty) ->
126 mkWWstr cpr_res_ty wrap_args `thenUs` \ (work_dmds, wrap_fn_str, work_fn_str) ->
129 Note InlineMe . wrap_fn_args . wrap_fn_cpr . wrap_fn_str . Var,
130 work_fn_str . work_fn_cpr . work_fn_args)
131 -- We use an INLINE unconditionally, even if the wrapper turns out to be
132 -- something trivial like
134 -- f = __inline__ (coerce T fw)
135 -- The point is to propagate the coerce to f's call sites, so even though
136 -- f's RHS is now trivial (size 1) we still want the __inline__ to prevent
137 -- fw from being inlined into f's RHS
139 one_shots' = one_shots ++ repeat False
143 %************************************************************************
145 \subsection{Coercion stuff}
147 %************************************************************************
150 We really want to "look through" coerces.
151 Reason: I've seen this situation:
153 let f = coerce T (\s -> E)
159 If only we w/w'd f, we'd get
160 let f = coerce T (\s -> fw s)
164 Now we'll inline f to get
172 Now we'll see that fw has arity 1, and will arity expand
173 the \x to get what we want.
176 -- mkWWargs is driven off the function type and arity.
177 -- It chomps bites off foralls, arrows, newtypes
178 -- and keeps repeating that until it's satisfied the supplied arity
182 -> [Bool] -- True for a one-shot arg; ** may be infinite **
183 -> UniqSM ([Var], -- Wrapper args
184 CoreExpr -> CoreExpr, -- Wrapper fn
185 CoreExpr -> CoreExpr, -- Worker fn
186 Type) -- Type of wrapper body
188 mkWWargs fun_ty demands one_shots
189 | Just rep_ty <- splitNewType_maybe fun_ty
190 -- The newtype case is for when the function has
191 -- a recursive newtype after the arrow (rare)
192 -- We check for arity >= 0 to avoid looping in the case
193 -- of a function whose type is, in effect, infinite
194 -- [Arity is driven by looking at the term, not just the type.]
196 -- It's also important when we have a function returning (say) a pair
197 -- wrapped in a recursive newtype, at least if CPR analysis can look
198 -- through such newtypes, which it probably can since they are
200 = mkWWargs rep_ty demands one_shots `thenUs` \ (wrap_args, wrap_fn_args, work_fn_args, res_ty) ->
202 Note (Coerce fun_ty rep_ty) . wrap_fn_args,
203 work_fn_args . Note (Coerce rep_ty fun_ty),
207 = getUniquesUs `thenUs` \ wrap_uniqs ->
209 (tyvars, tau) = splitForAllTys fun_ty
210 (arg_tys, body_ty) = splitFunTys tau
212 n_demands = length demands
213 n_arg_tys = length arg_tys
214 n_args = n_demands `min` n_arg_tys
216 new_fun_ty = mkFunTys (drop n_demands arg_tys) body_ty
217 new_demands = drop n_arg_tys demands
218 new_one_shots = drop n_args one_shots
220 val_args = zipWith4 mk_wrap_arg wrap_uniqs arg_tys demands one_shots
221 wrap_args = tyvars ++ val_args
223 {- ASSERT( not (null tyvars) || not (null arg_tys) ) -}
224 if (null tyvars) && (null arg_tys) then
225 pprTrace "mkWWargs" (ppr fun_ty $$ ppr demands)
226 returnUs ([], id, id, fun_ty)
231 new_one_shots `thenUs` \ (more_wrap_args, wrap_fn_args, work_fn_args, res_ty) ->
233 returnUs (wrap_args ++ more_wrap_args,
234 mkLams wrap_args . wrap_fn_args,
235 work_fn_args . applyToVars wrap_args,
239 = returnUs ([], id, id, fun_ty)
242 applyToVars :: [Var] -> CoreExpr -> CoreExpr
243 applyToVars vars fn = mkVarApps fn vars
245 mk_wrap_arg uniq ty dmd one_shot
246 = set_one_shot one_shot (setIdNewDemandInfo (mkSysLocal SLIT("w") uniq ty) dmd)
248 set_one_shot True id = setOneShotLambda id
249 set_one_shot False id = id
253 %************************************************************************
255 \subsection{Strictness stuff}
257 %************************************************************************
260 mkWWstr :: Type -- Result type
261 -> [Var] -- Wrapper args; have their demand info on them
262 -- *Includes type variables*
263 -> UniqSM ([Demand], -- Demand on worker (value) args
264 CoreExpr -> CoreExpr, -- Wrapper body, lacking the worker call
265 -- and without its lambdas
266 -- This fn adds the unboxing, and makes the
267 -- call passing the unboxed things
269 CoreExpr -> CoreExpr) -- Worker body, lacking the original body of the function,
270 -- but *with* lambdas
272 mkWWstr res_ty wrap_args
273 = mk_ww_str wrap_args `thenUs` \ (work_args, take_apart, put_together) ->
275 work_dmds = [idNewDemandInfo v | v <- work_args, isId v]
276 apply_to args fn = mkVarApps fn args
278 if not (null work_dmds && isUnLiftedType res_ty) then
279 returnUs ( work_dmds,
280 take_apart . applyToVars work_args,
281 mkLams work_args . put_together)
283 -- Horrid special case. If the worker would have no arguments, and the
284 -- function returns a primitive type value, that would make the worker into
285 -- an unboxed value. We box it by passing a dummy void argument, thus:
287 -- f = /\abc. \xyz. fw abc void
288 -- fw = /\abc. \v. body
290 -- We use the state-token type which generates no code
291 getUniqueUs `thenUs` \ void_arg_uniq ->
293 void_arg = mk_ww_local void_arg_uniq realWorldStatePrimTy
296 take_apart . applyToVars [realWorldPrimId] . apply_to work_args,
297 mkLams work_args . Lam void_arg . put_together)
302 \ wrapper_body -> wrapper_body,
303 \ worker_body -> worker_body)
308 = mk_ww_str ds `thenUs` \ (worker_args, wrap_fn, work_fn) ->
309 returnUs (arg : worker_args, wrap_fn, work_fn)
312 = case idNewDemandInfo arg of
314 -- Absent case. We don't deal with absence for unlifted types,
315 -- though, because it's not so easy to manufacture a placeholder
316 -- We'll see if this turns out to be a problem
317 Abs | not (isUnLiftedType (idType arg)) ->
318 mk_ww_str ds `thenUs` \ (worker_args, wrap_fn, work_fn) ->
319 returnUs (worker_args, wrap_fn, mk_absent_let arg . work_fn)
322 Seq Keep _ [] -> mk_ww_str ds `thenUs` \ (worker_args, wrap_fn, work_fn) ->
323 returnUs (arg : worker_args, mk_seq_case arg . wrap_fn, work_fn)
324 -- Pass the arg, no need to rebox
327 Seq Drop _ [] -> mk_ww_str ds `thenUs` \ (worker_args, wrap_fn, work_fn) ->
328 returnUs (worker_args, mk_seq_case arg . wrap_fn, mk_absent_let arg . work_fn)
329 -- Don't pass the arg, build absent arg
333 | Just (arg_tycon, tycon_arg_tys, data_con, inst_con_arg_tys)
334 <- splitProductType_maybe (idType arg)
335 -> getUniquesUs `thenUs` \ uniqs ->
337 unpk_args = zipWith mk_ww_local uniqs inst_con_arg_tys
338 unpk_args_w_ds = zipWithEqual "mk_ww_str" set_worker_arg_info unpk_args cs
339 unbox_fn = mk_unpk_case arg unpk_args data_con arg_tycon
340 rebox_fn = mk_pk_let arg data_con tycon_arg_tys unpk_args
342 mk_ww_str (unpk_args_w_ds ++ ds) `thenUs` \ (worker_args, wrap_fn, work_fn) ->
344 Keep -> returnUs (arg : worker_args, unbox_fn . wrap_fn, work_fn)
345 -- Pass the arg, no need to rebox
346 Drop -> returnUs (worker_args, unbox_fn . wrap_fn, work_fn . rebox_fn)
347 -- Don't pass the arg, rebox instead
350 WARN( True, ppr arg )
351 mk_ww_str ds `thenUs` \ (worker_args, wrap_fn, work_fn) ->
352 returnUs (arg : worker_args, wrap_fn, work_fn)
356 mk_ww_str ds `thenUs` \ (worker_args, wrap_fn, work_fn) ->
357 returnUs (arg : worker_args, wrap_fn, work_fn)
359 -- If the wrapper argument is a one-shot lambda, then
360 -- so should (all) the corresponding worker arguments be
361 -- This bites when we do w/w on a case join point
362 set_worker_arg_info worker_arg demand = set_one_shot (setIdNewDemandInfo worker_arg demand)
364 set_one_shot | isOneShotLambda arg = setOneShotLambda
365 | otherwise = \x -> x
369 %************************************************************************
371 \subsection{CPR stuff}
373 %************************************************************************
376 @mkWWcpr@ takes the worker/wrapper pair produced from the strictness
377 info and adds in the CPR transformation. The worker returns an
378 unboxed tuple containing non-CPR components. The wrapper takes this
379 tuple and re-produces the correct structured output.
381 The non-CPR results appear ordered in the unboxed tuple as if by a
382 left-to-right traversal of the result structure.
386 mkWWcpr :: Type -- function body type
387 -> DmdResult -- CPR analysis results
388 -> UniqSM (CoreExpr -> CoreExpr, -- New wrapper
389 CoreExpr -> CoreExpr, -- New worker
390 Type) -- Type of worker's body
392 mkWWcpr body_ty RetCPR
393 | not (isAlgType body_ty)
394 = WARN( True, text "mkWWcpr: non-algebraic body type" <+> ppr body_ty )
395 returnUs (id, id, body_ty)
397 | n_con_args == 1 && isUnLiftedType con_arg_ty1
398 -- Special case when there is a single result of unlifted type
399 = getUniquesUs `thenUs` \ (work_uniq : arg_uniq : _) ->
401 work_wild = mk_ww_local work_uniq body_ty
402 arg = mk_ww_local arg_uniq con_arg_ty1
404 returnUs (\ wkr_call -> Case wkr_call arg [(DEFAULT, [], mkConApp data_con (map Type tycon_arg_tys ++ [Var arg]))],
405 \ body -> workerCase body work_wild [(DataAlt data_con, [arg], Var arg)],
408 | otherwise -- The general case
409 = getUniquesUs `thenUs` \ uniqs ->
411 (wrap_wild : work_wild : args) = zipWith mk_ww_local uniqs (ubx_tup_ty : body_ty : con_arg_tys)
412 arg_vars = map Var args
413 ubx_tup_con = tupleCon Unboxed n_con_args
414 ubx_tup_ty = exprType ubx_tup_app
415 ubx_tup_app = mkConApp ubx_tup_con (map Type con_arg_tys ++ arg_vars)
416 con_app = mkConApp data_con (map Type tycon_arg_tys ++ arg_vars)
418 returnUs (\ wkr_call -> Case wkr_call wrap_wild [(DataAlt ubx_tup_con, args, con_app)],
419 \ body -> workerCase body work_wild [(DataAlt data_con, args, ubx_tup_app)],
422 (_, tycon_arg_tys, data_con, con_arg_tys) = splitProductType "mkWWcpr" body_ty
423 n_con_args = length con_arg_tys
424 con_arg_ty1 = head con_arg_tys
426 mkWWcpr body_ty other -- No CPR info
427 = returnUs (id, id, body_ty)
429 -- If the original function looked like
430 -- f = \ x -> _scc_ "foo" E
432 -- then we want the CPR'd worker to look like
433 -- \ x -> _scc_ "foo" (case E of I# x -> x)
434 -- and definitely not
435 -- \ x -> case (_scc_ "foo" E) of I# x -> x)
437 -- This transform doesn't move work or allocation
438 -- from one cost centre to another
440 workerCase (Note (SCC cc) e) arg alts = Note (SCC cc) (Case e arg alts)
441 workerCase e arg alts = Case e arg alts
445 %************************************************************************
447 \subsection{Utilities}
449 %************************************************************************
453 mk_absent_let arg body
454 | not (isUnLiftedType arg_ty)
455 = Let (NonRec arg abs_rhs) body
457 = panic "WwLib: haven't done mk_absent_let for primitives yet"
460 -- abs_rhs = mkTyApps (Var aBSENT_ERROR_ID) [arg_ty]
461 abs_rhs = mkApps (Var eRROR_CSTRING_ID) [Type arg_ty, Lit (MachStr (_PK_ msg))]
462 msg = "Oops! Entered absent arg " ++ showSDocDebug (ppr arg <+> ppr (idType arg))
464 mk_unpk_case arg unpk_args boxing_con boxing_tycon body
467 (sanitiseCaseBndr arg)
468 [(DataAlt boxing_con, unpk_args, body)]
470 mk_seq_case arg body = Case (Var arg) (sanitiseCaseBndr arg) [(DEFAULT, [], body)]
472 sanitiseCaseBndr :: Id -> Id
473 -- The argument we are scrutinising has the right type to be
474 -- a case binder, so it's convenient to re-use it for that purpose.
475 -- But we *must* throw away all its IdInfo. In particular, the argument
476 -- will have demand info on it, and that demand info may be incorrect for
477 -- the case binder. e.g. case ww_arg of ww_arg { I# x -> ... }
478 -- Quite likely ww_arg isn't used in '...'. The case may get discarded
479 -- if the case binder says "I'm demanded". This happened in a situation
480 -- like (x+y) `seq` ....
481 sanitiseCaseBndr id = id `setIdInfo` vanillaIdInfo
483 mk_pk_let arg boxing_con con_tys unpk_args body
484 = Let (NonRec arg (mkConApp boxing_con con_args)) body
486 con_args = map Type con_tys ++ map Var unpk_args
488 mk_ww_local uniq ty = mkSysLocal SLIT("ww") uniq ty