| ... |
... |
@@ -968,64 +968,96 @@ the form [Demand=<L,1*(C(1,...(C(1,U))))>] |
|
968
|
968
|
|
|
969
|
969
|
Note [Thunk splitting]
|
|
970
|
970
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
971
|
|
-Consider a strict non-recursive let-binding:
|
|
972
|
|
- let x* = x-rhs
|
|
973
|
|
- in body
|
|
974
|
|
-
|
|
975
|
|
-(I'll use a '*' to mean "x* is demanded strictly".)
|
|
976
|
|
-`splitThunk` transforms like this:
|
|
977
|
|
- let x* = case x-rhs of { I# a -> I# a }
|
|
978
|
|
- in body
|
|
979
|
|
-
|
|
980
|
|
-This may seem a little strange: the RHS takes `x` apart and reboxes it.
|
|
981
|
|
-But because the outer 'let' is strict, the simplifier uses `StrictBind`
|
|
982
|
|
-to float the `case` out, giving
|
|
983
|
|
- case x-rhs of I# a ->
|
|
984
|
|
- let x* = I# a in body
|
|
985
|
|
-
|
|
986
|
|
-That is good: in `body` we know the form of `x`, which
|
|
987
|
|
- * gives the CPR property, and
|
|
988
|
|
- * allows case-of-known-constructor to happen on x
|
|
989
|
|
- * can avoid reboxing: see (TS1)
|
|
|
971
|
+The worker/wrapper split mainly affects let-bound /functions/. But we can also use
|
|
|
972
|
+demand and CPR information to improve let-bound /thunks/. Specifically:
|
|
|
973
|
+
|
|
|
974
|
+* (A) Demand-led thunk splitting:
|
|
|
975
|
+ If a thunk is demanded strictly, perhaps nestedly, we can turn it into a
|
|
|
976
|
+ case, perhaps nestedly. For example
|
|
|
977
|
+ let x :: (Int, Int)
|
|
|
978
|
+ {- Dmd = SP(S,L) -}
|
|
|
979
|
+ x = rhs
|
|
|
980
|
+ in body
|
|
|
981
|
+ We would ultimately like to get
|
|
|
982
|
+ case rhs of x { (a,b) ->
|
|
|
983
|
+ case a of { I# a# ->
|
|
|
984
|
+ body
|
|
|
985
|
+
|
|
|
986
|
+ This much better: no thunk for `rhs`; and known-data-con unfoldings for `x`
|
|
|
987
|
+ and `a` inside `body`.
|
|
|
988
|
+
|
|
|
989
|
+* (B) CPR-led thunk splitting:
|
|
|
990
|
+ If a thunk has the (possibly-nested) CPR property /and/ is demanded strictly
|
|
|
991
|
+ at top level, we can again generate a case
|
|
|
992
|
+ let x :: (Int, Int)
|
|
|
993
|
+ {- Dmd = S, Cpr = (_, I# _) -}
|
|
|
994
|
+ x = rhs
|
|
|
995
|
+ in body
|
|
|
996
|
+ The CPR info says that if `x` is evaluated (which is is), it'll return a pair,
|
|
|
997
|
+ whose second component can very cheaply be evaluated to a (I# b#). We would
|
|
|
998
|
+ like to end up with
|
|
|
999
|
+ case rhs of x { (a,b) ->
|
|
|
1000
|
+ case b of { I# b# ->
|
|
|
1001
|
+ body
|
|
|
1002
|
+ which is good for the same reasons as before. See (TS1) for an example.
|
|
|
1003
|
+
|
|
|
1004
|
+Notice that (B) CPR-led thunk splitting can be useful:
|
|
|
1005
|
+ * If multi-constructor data types are involved, so that the argument-unboxing
|
|
|
1006
|
+ mechanism (A) is impotent.
|
|
|
1007
|
+ * If the binder is only demanded at top level, but CPR has nested information.
|
|
|
1008
|
+
|
|
|
1009
|
+We might do even better by combinding (A) and (B) but at the moment we try (A)
|
|
|
1010
|
+first, and then try (B) if (A) is impotent.
|
|
|
1011
|
+
|
|
|
1012
|
+Implementation
|
|
|
1013
|
+~~~~~~~~~~~~~~
|
|
|
1014
|
+The worker/wrapper pass does not generate these nested case expressions directly.
|
|
|
1015
|
+Instead it generates a regular let-binding but, since the binder is marked as
|
|
|
1016
|
+strict, any `case` expessions in the RHS will float out. Specifically;
|
|
|
1017
|
+
|
|
|
1018
|
+* For (A), `splitThunkStr` generates the binding
|
|
|
1019
|
+ let x* = case rhs of { (a,b) ->
|
|
|
1020
|
+ case a of { I# a# ->
|
|
|
1021
|
+ (I# a, b)
|
|
|
1022
|
+ Now when the Simplifier floats the `case` expressions out, we get
|
|
|
1023
|
+ case rhs of { (a.b) ->
|
|
|
1024
|
+ case a of { I# a# ->
|
|
|
1025
|
+ let x = (I# a b)
|
|
|
1026
|
+ in body
|
|
|
1027
|
+ which is equivalent to the expression in (A) above.
|
|
|
1028
|
+
|
|
|
1029
|
+ Rather cleverly, `splitThunkStr` uses `mkWWstr_one`, the /existing/ function
|
|
|
1030
|
+ argument w/w splitting function, to do the unpacking. This automatically
|
|
|
1031
|
+ deals with nested strictness.
|
|
|
1032
|
+
|
|
|
1033
|
+* For (B), `splitThunkCpr` behaves exactly analogously, again using `mkWWcpr_one`
|
|
|
1034
|
+ to do the unpacking.
|
|
990
|
1035
|
|
|
991
|
1036
|
Wrinkles:
|
|
992
|
1037
|
|
|
993
|
|
-(T1) Suppose x-rhs is itself a case, so we started with
|
|
994
|
|
- let x* = case e of { T -> I# e1; F -> I# e2 }
|
|
|
1038
|
+(TS1) Here's an example of (B), thunk-splitting based on CPR information, in action.
|
|
|
1039
|
+ Suppose we have
|
|
|
1040
|
+ let x* = case e of { T -> Just e1; F -> Just e2 }
|
|
995
|
1041
|
in body
|
|
996
|
|
- If the Simplifire floats the `case` out, we get
|
|
|
1042
|
+ If the Simplifier floats the `case` out, we get
|
|
997
|
1043
|
join j2 x = body
|
|
998
|
|
- in case e of { T -> j2 (I# e1); F -> j (I# e2) }
|
|
|
1044
|
+ in case e of { T -> j2 (Just e1); F -> j (Just e2) }
|
|
999
|
1045
|
which is not good (boxing).
|
|
1000
|
1046
|
|
|
1001
|
1047
|
If we first do the thunk-splitting transformation, we get
|
|
1002
|
|
- let x* = case (case e of { T -> I# e1; F -> I# e2 }) of
|
|
1003
|
|
- I# a -> I# a
|
|
|
1048
|
+ let x* = case (case e of { T -> Just e1; F -> Just e2 }) of
|
|
|
1049
|
+ Just a -> Just a
|
|
1004
|
1050
|
in body
|
|
1005
|
1051
|
|
|
1006
|
1052
|
Now when the Simplifier floats we will get:
|
|
1007
|
|
- join j a = let x* = I# a in body
|
|
|
1053
|
+ join j a = let x* = Just a in body
|
|
1008
|
1054
|
in case e of { T -> j e1; F -> j e2 }
|
|
1009
|
|
- which is good (no boxing).
|
|
|
1055
|
+ which is much better (no boxing).
|
|
1010
|
1056
|
|
|
1011
|
|
-(TS3) In fact, splitThunk uses the function argument w/w splitting
|
|
1012
|
|
- function, mkWWstr_one, so in our example we /actually/ get
|
|
1013
|
|
- let
|
|
1014
|
|
- x* = let x = x-rhs in
|
|
1015
|
|
- case x of { I# a -> I# a }
|
|
1016
|
|
- in body
|
|
1017
|
|
- Here `x` is used exactly once in the RHS so it'll be inlined.
|
|
1018
|
|
-
|
|
1019
|
|
- A big advantage of using `mkWWstr_one` is that if x's demand is deeper
|
|
1020
|
|
- (say U(U(L,L),L)) then the splitting will go deeper too.
|
|
1021
|
|
-
|
|
1022
|
|
-(TS4) For recursive thunks, the Simplifier is unable to float `x-rhs` out of
|
|
|
1057
|
+(TS2) For recursive thunks, the Simplifier is unable to float `x-rhs` out of
|
|
1023
|
1058
|
`x*`'s RHS, because `x*` occurs freely in `x-rhs`, and will just change it
|
|
1024
|
1059
|
back to the original definition, so we just split non-recursive thunks.
|
|
1025
|
1060
|
|
|
1026
|
|
-(TS5) We can do thunk-splitting for CPR too, to get stuff that strictness
|
|
1027
|
|
- cannot. Consider
|
|
1028
|
|
-
|
|
1029
|
1061
|
Note [Thunk splitting for top-level binders]
|
|
1030
|
1062
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
1031
|
1063
|
Top-level bindings are never strict. Yet they can be absent, as T14270 shows:
|
| ... |
... |
@@ -1076,7 +1108,7 @@ splitThunkStr :: WwOpts -> RecFlag -> Var -> CoreExpr -> UniqSM (Maybe CoreExpr) |
|
1076
|
1108
|
splitThunkStr ww_opts is_rec x rhs
|
|
1077
|
1109
|
= assert (not (isJoinId x)) $
|
|
1078
|
1110
|
assertPpr (isNonRec is_rec) (ppr x) $ -- The thunk must be non-recursive
|
|
1079
|
|
- -- See (TS4) in Note [Thunk splitting]
|
|
|
1111
|
+ -- See (TS2) in Note [Thunk splitting]
|
|
1080
|
1112
|
do { let x' = localiseId x -- See comment above
|
|
1081
|
1113
|
; (useful, _transit_vars, wrap_fn, fn_arg) <- mkWWstr_one ww_opts x' NotMarkedStrict
|
|
1082
|
1114
|
-- Use mkWWstr_one: see (TS3) in Note [Thunk splitting]
|
| ... |
... |
@@ -1087,6 +1119,7 @@ splitThunkStr ww_opts is_rec x rhs |
|
1087
|
1119
|
else return Nothing }
|
|
1088
|
1120
|
|
|
1089
|
1121
|
splitThunkCpr :: WwOpts -> RecFlag -> Var -> CoreExpr -> UniqSM (Maybe CoreExpr)
|
|
|
1122
|
+-- See Note [Thunk splitting]
|
|
1090
|
1123
|
-- Given x* = rhs, where x* is used outermost-strictly, and x* has a (nested) CPR
|
|
1091
|
1124
|
-- property, say Just (Just _), return this new RHS:
|
|
1092
|
1125
|
-- let x' = rhs in
|
| ... |
... |
@@ -1097,7 +1130,7 @@ splitThunkCpr ww_opts is_rec x rhs |
|
1097
|
1130
|
| isStrUsedDmd (idDemandInfo x)
|
|
1098
|
1131
|
= assertPpr (cprTypeArity cpr_type == 0) (ppr x) $ -- It's a thunk
|
|
1099
|
1132
|
assertPpr (isNonRec is_rec) (ppr x) $ -- The thunk must be non-recursive
|
|
1100
|
|
- -- See (TS4) in Note [Thunk splitting]
|
|
|
1133
|
+ -- See (TS2) in Note [Thunk splitting]
|
|
1101
|
1134
|
do { (useful, _transit_vars, rebuild_result, unpacker) <- mkWWcpr_one ww_opts x' cpr
|
|
1102
|
1135
|
; if useful
|
|
1103
|
1136
|
then return (Just (Let (NonRec x' rhs) $
|