Missed inline causes worker/wrapper disaster
See also
* [This comment](https://gitlab.haskell.org/ghc/ghc/-/issues/26682#note_651393) in #26682, which may well be caused by the same issue.
I'm seeing this code in the simplified Core for `GHC.Runtime.Heap.Layout`:
```
GHC.Runtime.Heap.Layout.$whdrSizeW
= \ (ww_s6YM :: PlatformWordSize)
(ww1_s6YW :: Maybe PlatformConstants)
(ww2_s6YY :: GHC.Platform.Ways.Ways)
(ds_s6Z0 :: SMRep) ->
case ww_s6YM of ww3_X5ps { __DEFAULT ->
let {
profile_s77h [Dmd=LP(A,SL,A,A,A,A,A,A,A,A,A,SL)] :: Platform
profile_s77h
= GHC.Platform.Platform
(RUBBISH(GHC.Internal.Types.LiftedRep) @ArchOS)
ww3_X5ps
(RUBBISH(GHC.Internal.Types.LiftedRep) @ByteOrder)
(RUBBISH(GHC.Internal.Types.LiftedRep) @Bool)
(RUBBISH(GHC.Internal.Types.LiftedRep) @Bool)
(RUBBISH(GHC.Internal.Types.LiftedRep) @Bool)
(RUBBISH(GHC.Internal.Types.LiftedRep) @Bool)
(RUBBISH(GHC.Internal.Types.LiftedRep) @Bool)
(RUBBISH(GHC.Internal.Types.LiftedRep) @Bool)
(RUBBISH(GHC.Internal.Types.LiftedRep) @Bool)
(RUBBISH(GHC.Internal.Types.LiftedRep) @Bool)
ww1_s6YW } in
let {
profile1_a3Mi [Dmd=MP(1P(A,1L,A,A,A,A,A,A,A,A,A,1L),1L)] :: Profile
profile1_a3Mi = GHC.Platform.Profile.Profile profile_s77h ww2_s6YY } in
case ds_s6Z0 of {
__DEFAULT -> GHC.Runtime.Heap.Layout.hdrSizeW1;
HeapRep ds1_D5mh bx_D5sf bx1_D5sg ty_a3Mj ->
case ww2_s6YY of wild1_X5pu { __DEFAULT ->
case ww1_s6YW of wild2_X5pv { __DEFAULT ->
case GHC.Runtime.Heap.Layout.$wclosureTypeHdrSize
ww3_X5ps wild2_X5pv wild1_X5pu ty_a3Mj
of ww4_s76A
{ __DEFAULT ->
GHC.Internal.Types.I# ww4_s76A } } };
ArrayPtrsRep bx_D5si bx1_D5sj -> arrPtrsHdrSizeW profile1_a3Mi;
SmallArrayPtrsRep bx_D5sl -> smallArrPtrsHdrSizeW profile1_a3Mi;
ArrayWordsRep bx_D5sn -> arrWordsHdrSizeW profile1_a3Mi
} }
```
**Yikes! What's all this reboxing doing??** `arrPtrsHdrSizeW` looks like this:
```
arrPtrsHdrSizeW [InlPrag=[2]] :: Profile -> WordOff
[Str=<1!P(1!P(A,1L,A,A,A,A,A,A,A,A,A,1L),1L)>, ...]
arrPtrsHdrSizeW
= \ (profile_s6Xc [Dmd=1!P(1!P(A,1L,A,A,A,A,A,A,A,A,A,1L),1L)]
:: Profile) ->
case profile_s6Xc of
{ Profile ww_s6Xe [Dmd=1!P(A,1L,A,A,A,A,A,A,A,A,A,1L)]
ww_s6Xt [Dmd=1L] ->
case ww_s6Xe of
{ Platform ww_s6Xg [Dmd=A] ww_s6Xh [Dmd=1L] ww_s6Xi [Dmd=A]
ww_s6Xj [Dmd=A] ww_s6Xk [Dmd=A] ww_s6Xl [Dmd=A] ww_s6Xm [Dmd=A]
ww_s6Xn [Dmd=A] ww_s6Xo [Dmd=A] ww_s6Xp [Dmd=A] ww_s6Xq [Dmd=A]
ww_s6Xr [Dmd=1L] ->
case $warrPtrsHdrSizeW_s6Xw ww_s6Xh ww_s6Xr ww_s6Xt of ww_s76u
{ __DEFAULT ->
GHC.Internal.Types.I# ww_s76u } } }
```
It is strict and worker-wrappered. If we inlined it (and similarly `smallArrPtrsHdrSizeW`)
all would be well. But we don't. Why not?
* Because the argument `profile1` is designated as `TrivArg`:
```
Considering inlining: arrPtrsHdrSizeW
arg infos [TrivArg]
interesting continuation BoringCtxt
```
* Why is that? Surely `proile1_a3Mi` is bound to a data constructor; it should be a `ValueArg`.
* But no: `Profile` is strict:
```
data Profile = Profile
{ profilePlatform :: !Platform
, profileWays :: !Ways }
```
And `exprIsHNF` insists that the arguments of `Profile` are themselves evaluated.
* But `ww2_s6YY` is just an argument to the (worker) function, and is not marked evaluated.
This is bad, very bad. A basic assumption of the worker/wrapper story is that wrappers
are inlined; if not, we get reboxing as indeed we do above.
## Repro case
```
data T a = MkT !a
g s True t = f s t t
g s False t = g s True t
f True (MkT xs) t = f False (MkT xs) t
f False (MkT xs) _ = xs
```
We get
```
Bar.$wg
= \ (@t_sz8) (s_sz9 :: Bool) (ds_sza :: Bool) (ww_szd :: t_sz8) ->
case ds_sza of {
False ->
case ww_szd of wild1_XyS { __DEFAULT ->
Bar.$wg @t_sz8 s_sz9 GHC.Internal.Types.True wild1_XyS
};
True ->
let {
t1_awK [Dmd=1!P(1L)] :: T t_sz8
[LclId]
t1_awK = Bar.MkT @t_sz8 ww_szd } in
f @t_sz8 @(T t_sz8) s_sz9 t1_awK t1_awK
}
```
Look at that reboxing of `t1_awK`!
## Analysis
Consider
```
data T a = MkT !a
f x = case x of MkT y -> y `seq` True
```
The Simplifier carefully attaches an "I am evaluated" flag to `y`, in the pattern match on
`MkT` because `MkT` is strict; and that let's us discard the `seq` on `y`.
Similarly, we know that `ww2_s6YY` will be evaluated: it is an argument to a strict
function, and we use call-by-value for such things. Somehow we want to convey similar
information to a worker function.
There are some long notes `Note [Call-by-value for worker args]` and `Note [Which Ids should be strictified]` in GHC.Core.Utils, which are highly relevant.
issue