GHC.Core.Utils.mergeCaseAlts mishandles ticks around join points
`GHC.Core.Utils.mergeCaseAlts`, added in e026bdf275e287005f2c2e534d3ba034ebf11c01, does not take care to avoid pushing profiling ticks in between a join point binder and its jumps.
Here is a sample program (from my reproducer for #26642; this is essentially the same issue):
```hs
module T26642 ( saveClobberedTemps ) where
import Prelude ( IO, Bool(..), Int, (>>=), (==), return )
import Data.Word ( Word64 )
-------------------------------------------------------------------------------
data Word64Map a
= Bin (Word64Map a) (Word64Map a)
| Tip a
| Nil
{-# NOINLINE myFoldr #-}
myFoldr :: (a -> b -> b) -> b -> Word64Map a -> b
myFoldr f = go
where
{-# NOINLINE go #-}
go z' Nil = z'
go z' (Tip x) = f x z'
go z' (Bin l r) = go (go z' r) l
{-# NOINLINE nonDetFold #-}
nonDetFold :: (b -> elt -> IO b) -> b -> Word64Map elt -> IO b
nonDetFold f z0 xs = myFoldr c return xs z0
where
{-# NOINLINE c #-}
c x k z = f z x >>= k
{-# NOINLINE myFalse #-}
myFalse :: Bool
myFalse = False
type RealReg = Int
data Loc = InReg RealReg | InMem
saveClobberedTemps :: forall instr. [RealReg] -> IO [instr]
saveClobberedTemps clobbered = nonDetFold maybe_spill [] Nil
where
{-# NOINLINE maybe_spill #-}
maybe_spill :: [instr] -> Loc -> IO [instr]
maybe_spill instrs !loc =
case loc of
InReg reg
| myFalse
-> return []
_ -> return instrs
```
If you compile with `ghc -O1 -prof -fprof-auto-calls` (which requires profiling libraries), you will see that we start off with:
```hs
let {
maybe_spill_axx [InlPrag=NOINLINE]
:: [instr_aH9] -> Loc -> IO [instr_aH9]
[LclId,
Unf=Unf{Src=<vanilla>, TopLvl=False,
Value=True, ConLike=True, WorkFree=True, Expandable=True,
Guidance=IF_ARGS [0 20] 110 0}]
maybe_spill_axx
= \ (instrs_axy :: [instr_aH9]) (loc_axz :: Loc) ->
case loc_axz of loc_X0 { __DEFAULT ->
scc<saveClobberedTemps.maybe_spill>
join {
fail_DHZ :: (# #) -> IO [instr_aH9]
[LclId[JoinId(1)(Nothing)],
Str=<L>,
Unf=Unf{Src=<vanilla>, TopLvl=False,
Value=True, ConLike=True, WorkFree=True, Expandable=True,
Guidance=IF_ARGS [0] 30 0}]
fail_DHZ _ [Occ=Dead, OS=OneShot]
= scc<saveClobberedTemps.maybe_spill>
return @IO GHC.Internal.Base.$fMonadIO @[instr_aH9] instrs_axy } in
case loc_X0 of {
__DEFAULT -> jump fail_DHZ GHC.Internal.Types.(##);
InReg _ [Occ=Dead] ->
case myFalse of {
False -> jump fail_DHZ GHC.Internal.Types.(##);
True ->
scc<saveClobberedTemps.maybe_spill>
return
@IO
GHC.Internal.Base.$fMonadIO
@[instr_aH9]
(GHC.Internal.Types.[] @instr_aH9)
}
}
} } in
scc<saveClobberedTemps>
nonDetFold
@[instr_aH9]
@Loc
maybe_spill_axx
(GHC.Internal.Types.[] @instr_aH9)
(T26642.Nil @Loc)
```
in which we have `scc<saveClobberedTemps.maybe_spill>` around the join point. Then, in the first phase of simplification, we will run `mergeCaseAlts` and this will move the tick inside the join point, and we end up with Core like:
```hs
maybe_spill_axx
= \ (instrs_axy :: [instr_aH9]) (loc_axz :: Loc) ->
join {
fail_DHZ :: (# #) -> IO [instr_aH9]
[LclId[JoinId(1)(Nothing)], Arity=2, Str=<L>]
fail_DHZ _ [Occ=Dead, OS=OneShot]
= (\ (s_aIo
:: GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld) ->
(# s_aIo, instrs_axy #))
`cast` (Sym (GHC.Internal.Types.N:IO <[instr_aH9]>_R)
:: (GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld
-> (# GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld,
[instr_aH9] #))
~R# IO [instr_aH9]) } in
case loc_axz of {
InReg reg_axA ->
scc<saveClobberedTemps.maybe_spill>
case myFalse of {
False -> jump fail_DHZ GHC.Internal.Types.(##);
True ->
(\ (s_aIo
:: GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld) ->
(# s_aIo, GHC.Internal.Types.[] @instr_aH9 #))
`cast` (Sym (GHC.Internal.Types.N:IO <[instr_aH9]>_R)
:: (GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld
-> (# GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld,
[instr_aH9] #))
~R# IO [instr_aH9])
};
InMem ->
scc<saveClobberedTemps.maybe_spill>
jump fail_DHZ GHC.Internal.Types.(##)
} } in ...
```
Now all is lost because the simplifier cannot handle intervening ticks in between a join point binder and its jumps. This causes us to eventually generate garbage Core such as the following:
```hs
T26642.saveClobberedTemps2
= \ (@instr_aGY)
(instrs_axs :: [instr_aGY])
(loc_axt [OS=OneShot] :: Loc)
(eta_B0 [OS=OneShot]
:: GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld) ->
join {
fail_sIp [Dmd=MC(1,L)]
:: (# #)
-> (# GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld,
[instr_aGY] #)
[LclId[JoinId(1)(Nothing)], Arity=1, Str=<A>]
fail_sIp _ [Occ=Dead, OS=OneShot] = (# eta_B0, instrs_axs #) } in
case loc_axt of {
InReg reg_axu ->
(scc<saveClobberedTemps.maybe_spill>
case myFalse_rhI of {
False -> jump fail_sIp GHC.Internal.Types.(##);
True -> lvl_rJo @instr_aGY
})
eta_B0;
InMem ->
(scc<saveClobberedTemps.maybe_spill>
jump fail_sIp GHC.Internal.Types.(##))
eta_B0
}
```
This is because all the machinery of `wrapJoinCont`/`trimJoinCont` from `Note [Join points and case-of-case]` breaks down when there are intervening profiling ticks.
As this logic was added in e026bdf275e287005f2c2e534d3ba034ebf11c01, which is in GHC 9.12, 9.14 and HEAD, this means that those GHC versions are affected while GHC 9.10 is not.
issue