Simon Peyton Jones pushed to branch wip/case-bndr-unf at Glasgow Haskell Compiler / GHC

Commits:

4 changed files:

Changes:

  • compiler/GHC/Core/Opt/Simplify/Env.hs
    ... ... @@ -479,6 +479,10 @@ type SimplIdSubst = IdEnv SimplClo -- IdId |--> OutExpr
    479 479
     -- Roughly: either an (InExpr, StaticEnv) pair for an
    
    480 480
     --                 as-yet-unsimplified expression
    
    481 481
     --          or     an OutExpr, for an already-simplified one
    
    482
    +-- It's a bit like thunk: we may "evaluate" the SimplClo in a ApplyToVal
    
    483
    +--   and thereby transform it from `ContEx` to `DoneEx`
    
    484
    +--   This happens in the `ApplyToVal` case of `mkDupableContWithDmds`
    
    485
    +--   See Note [The argument of ApplyToVal] in GHC.Core.Opt.Simplify.Utils
    
    482 486
     data SimplClo
    
    483 487
       = DoneEx OutExpr JoinPointHood
    
    484 488
            -- If  x :-> DoneEx e ja   is in the SimplIdSubst
    
    ... ... @@ -486,7 +490,6 @@ data SimplClo
    486 490
            -- and  ja = Just a <=> x is a join-point of arity a
    
    487 491
            -- See Note [Join arity in SimplIdSubst]
    
    488 492
     
    
    489
    -
    
    490 493
       | DoneId OutId
    
    491 494
            -- If  x :-> DoneId v   is in the SimplIdSubst
    
    492 495
            -- then replace occurrences of x by v
    

  • compiler/GHC/Core/Opt/Simplify/Iteration.hs
    ... ... @@ -1226,17 +1226,8 @@ simplExprF1 env (Tick t expr) cont = {-#SCC "simplTick" #-} simplTick env t exp
    1226 1226
     simplExprF1 env (Coercion co)  cont = {-#SCC "simplCoercionF" #-} simplCoercionF env co cont
    
    1227 1227
     
    
    1228 1228
     simplExprF1 env (Cast body co) cont
    
    1229
    -  = do  { co1 <- {-#SCC "simplCast-simplCoercion" #-}
    
    1230
    -                 simplCoercion env co
    
    1231
    -
    
    1232
    -        ; let cont1 = {-#SCC "simplCast-addCoerce" #-}
    
    1233
    -                      if isReflCo co1
    
    1234
    -                      then cont  -- See Note [Optimising reflexivity]
    
    1235
    -                      else pushCast env co1 cont
    
    1236
    -                           -- True <=> co1 is optimised
    
    1237
    -
    
    1238
    -        ; {-#SCC "simplCast-simplExprF" #-}
    
    1239
    -          simplExprF env body cont1 }
    
    1229
    +  = do  { co1 <- simplCoercion env co
    
    1230
    +        ; simplExprF env body (pushCast env co1 cont) }
    
    1240 1231
     
    
    1241 1232
     simplExprF1 env (App fun arg) cont
    
    1242 1233
       = {-#SCC "simplExprF1-App" #-} case arg of
    
    ... ... @@ -1652,8 +1643,7 @@ completeBindX env from_what bndr rhs body cont
    1652 1643
       | FromBeta arg_levity <- from_what
    
    1653 1644
       , needsCaseBindingL arg_levity rhs -- Enforcing the let-can-float-invariant
    
    1654 1645
       = do { (env1, bndr1)   <- simplNonRecBndr env bndr  -- Lambda binders don't have rules
    
    1655
    -                                                      -- See Note [Lambda and case binders have
    
    1656
    -                                                      --      vanilla IdInfo] in GHC.Core
    
    1646
    +                            -- See Note [Lambda and case binders have vanilla IdInfo] in GHC.Core
    
    1657 1647
            ; (floats, expr') <- simplNonRecBody env1 from_what body cont
    
    1658 1648
            -- Do not float floats past the Case binder below
    
    1659 1649
            ; let expr'' = wrapFloats floats expr'
    
    ... ... @@ -1687,69 +1677,12 @@ completeBindX env from_what bndr rhs body cont
    1687 1677
             ; let all_floats = rhs_floats `addFloats` bind_float `addFloats` body_floats
    
    1688 1678
             ; return ( all_floats, body' ) }
    
    1689 1679
     
    
    1690
    -{-
    
    1691
    -************************************************************************
    
    1680
    +
    
    1681
    +{- *********************************************************************
    
    1692 1682
     *                                                                      *
    
    1693
    -\subsection{Lambdas}
    
    1683
    +                Arguments
    
    1694 1684
     *                                                                      *
    
    1695
    -************************************************************************
    
    1696
    --}
    
    1697
    -
    
    1698
    -{- Note [Optimising reflexivity]
    
    1699
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1700
    -It's important (for compiler performance) to get rid of reflexivity as soon
    
    1701
    -as it appears.  See #11735, #14737, and #15019.
    
    1702
    -
    
    1703
    -In particular, we want to behave well on
    
    1704
    -
    
    1705
    - *  e |> co1 |> co2
    
    1706
    -    where the two happen to cancel out entirely. That is quite common;
    
    1707
    -    e.g. a newtype wrapping and unwrapping cancel.
    
    1708
    -
    
    1709
    -
    
    1710
    - * (f |> co) @t1 @t2 ... @tn x1 .. xm
    
    1711
    -   Here we will use pushCoTyArg and pushCoValArg successively, which
    
    1712
    -   build up SelCo stacks.  Silly to do that if co is reflexive.
    
    1713
    -
    
    1714
    -However, we don't want to call isReflexiveCo too much, because it uses
    
    1715
    -type equality which is expensive on big types (#14737 comment:7).
    
    1716
    -
    
    1717
    -A good compromise (determined experimentally) seems to be to call
    
    1718
    -isReflexiveCo
    
    1719
    - * when composing casts, and
    
    1720
    - * at the end
    
    1721
    -
    
    1722
    -In investigating this I saw missed opportunities for on-the-fly
    
    1723
    -coercion shrinkage. See #15090.
    
    1724
    -
    
    1725
    -Note [Avoid re-simplifying coercions]
    
    1726
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1727
    -In some benchmarks (with deeply nested cases) we successively push
    
    1728
    -casts onto the SimplCont.  We don't want to call the coercion optimiser
    
    1729
    -on each successive composition -- that's at least quadratic.  So:
    
    1730
    -
    
    1731
    -* The CastIt constructor in SimplCont has a `sc_opt :: Bool` flag to
    
    1732
    -  record whether the coercion optimiser has been applied to the coercion.
    
    1733
    -
    
    1734
    -* In `simplCast`, when we see (Cast e co), we simplify `co` to get
    
    1735
    -  an OutCoercion, and built a CastIt with sc_opt=True.
    
    1736
    -
    
    1737
    -  Actually not quite: if we are simplifying the result of inlining an
    
    1738
    -  unfolding (seInlineDepth > 0), then instead of /optimising/ it again,
    
    1739
    -  just /substitute/ which is cheaper.  See `simplCoercion`.
    
    1740
    -
    
    1741
    -* In `pushCast` if we combine this new coercion with an existing one,
    
    1742
    -  we build a CastIt for (co1 ; co2) with sc_opt=False.
    
    1743
    -
    
    1744
    -* When unpacking a CastIt, in `rebuild`, we optimise the (presumably
    
    1745
    -  composed) coercion if sc_opt=False; this is done by `optOutCoercion`.
    
    1746
    -
    
    1747
    -* When duplicating a continuation in `mkDupableContWithDmds`, before
    
    1748
    -  duplicating a CastIt, optimise the coercion. Otherwise we'll end up
    
    1749
    -  optimising it separately in the duplicate copies.
    
    1750
    -
    
    1751
    -* See also Note [The MOutCoercion field of ContEx] in GHC.Core.Opt.Simplify.Utils.
    
    1752
    --}
    
    1685
    +********************************************************************* -}
    
    1753 1686
     
    
    1754 1687
     simplArg :: SimplEnvIS              -- ^ Used only for its InScopeSet
    
    1755 1688
              -> Maybe ArgInfo           -- ^ Just <=> This arg `ai` occurs in an app
    
    ... ... @@ -1771,13 +1704,12 @@ simplArg env mb_arg_info fun_ty (ContEx arg_se arg mco)
    1771 1704
                  cont     = addCastMCo mco stop
    
    1772 1705
            ; simplExprC arg_env' arg cont }
    
    1773 1706
     
    
    1774
    -{-
    
    1775
    -************************************************************************
    
    1707
    +
    
    1708
    +{- *********************************************************************
    
    1776 1709
     *                                                                      *
    
    1777
    -\subsection{Lambdas}
    
    1710
    +                Lambdas
    
    1778 1711
     *                                                                      *
    
    1779
    -************************************************************************
    
    1780
    --}
    
    1712
    +********************************************************************* -}
    
    1781 1713
     
    
    1782 1714
     simplNonRecBody :: SimplEnv -> FromWhat
    
    1783 1715
                     -> InExpr -> SimplCont
    
    ... ... @@ -2295,8 +2227,8 @@ simplOutId env fun cont
    2295 2227
                              env'  = arg_env `addNewInScopeIds` [s']
    
    2296 2228
                              hole_ty = mkVisFunTy m realWorldStatePrimTy new_runrw_res_ty
    
    2297 2229
                              cont' = pushCastMCo env' arg_mco $
    
    2298
    -                                 ApplyToVal { sc_arg = DoneEx (Var s') NotJoinPoint
    
    2299
    -                                            , sc_dup = dup
    
    2230
    +                                 ApplyToVal { sc_arg  = DoneId s'
    
    2231
    +                                            , sc_dup  = dup
    
    2300 2232
                                                 , sc_cont = inner_cont
    
    2301 2233
                                                 , sc_hole_ty = hole_ty }
    
    2302 2234
                                      -- cont' applies to s', then K
    

  • compiler/GHC/Core/Opt/Simplify/Utils.hs
    ... ... @@ -175,7 +175,6 @@ data SimplCont
    175 175
                                     -- Invariant: never an identity coercion
    
    176 176
           , sc_opt  :: Bool         -- True <=> sc_co has had optCoercion applied to it
    
    177 177
                                     --      See Note [Avoid re-simplifying coercions]
    
    178
    -                                --      in GHC.Core.Opt.Simplify.Iteration
    
    179 178
           , sc_cont :: SimplCont }
    
    180 179
     
    
    181 180
       | ApplyToVal         -- (ApplyToVal arg K)[e] = K[ e arg ]
    
    ... ... @@ -886,10 +885,65 @@ But we /also/ sometimes want to substitute that argument for a lambda-binder, in
    886 885
     `preInlineBetaUnconditionally`; and since the substitution maps an Id to a SimplClo,
    
    887 886
     the pending cast comes along for free.  Then we deal with it appropriately when
    
    888 887
     /applying/ the substitution in `simplInId`.
    
    888
    +
    
    889
    +Note [Optimising reflexivity]
    
    890
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    891
    +It's important (for compiler performance) to get rid of reflexivity as soon
    
    892
    +as it appears.  See #11735, #14737, and #15019.
    
    893
    +
    
    894
    +In particular, we want to behave well on
    
    895
    +
    
    896
    + *  e |> co1 |> co2
    
    897
    +    where the two happen to cancel out entirely. That is quite common;
    
    898
    +    e.g. a newtype wrapping and unwrapping cancel.
    
    899
    +
    
    900
    +
    
    901
    + * (f |> co) @t1 @t2 ... @tn x1 .. xm
    
    902
    +   Here we will use pushCoTyArg and pushCoValArg successively, which
    
    903
    +   build up SelCo stacks.  Silly to do that if co is reflexive.
    
    904
    +
    
    905
    +However, we don't want to call isReflexiveCo too much, because it uses
    
    906
    +type equality which is expensive on big types (#14737 comment:7).
    
    907
    +
    
    908
    +A good compromise (determined experimentally) seems to be to call
    
    909
    +isReflexiveCo
    
    910
    + * when composing casts, and
    
    911
    + * at the end
    
    912
    +
    
    913
    +In investigating this I saw missed opportunities for on-the-fly
    
    914
    +coercion shrinkage. See #15090.
    
    915
    +
    
    916
    +Note [Avoid re-simplifying coercions]
    
    917
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    918
    +In some benchmarks (with deeply nested cases) we successively push
    
    919
    +casts onto the SimplCont.  We don't want to call the coercion optimiser
    
    920
    +on each successive composition -- that's at least quadratic.  So:
    
    921
    +
    
    922
    +* The CastIt constructor in SimplCont has a `sc_opt :: Bool` flag to
    
    923
    +  record whether the coercion optimiser has been applied to the coercion.
    
    924
    +
    
    925
    +* In `simplCast`, when we see (Cast e co), we simplify `co` to get
    
    926
    +  an OutCoercion, and built a CastIt with sc_opt=True.
    
    927
    +
    
    928
    +  Actually not quite: if we are simplifying the result of inlining an
    
    929
    +  unfolding (seInlineDepth > 0), then instead of /optimising/ it again,
    
    930
    +  just /substitute/ which is cheaper.  See `simplCoercion`.
    
    931
    +
    
    932
    +* In `pushCast` if we combine this new coercion with an existing one,
    
    933
    +  we build a CastIt for (co1 ; co2) with sc_opt=False.
    
    934
    +
    
    935
    +* When unpacking a CastIt, in `rebuild`, we optimise the (presumably
    
    936
    +  composed) coercion if sc_opt=False; this is done by `optOutCoercion`.
    
    937
    +
    
    938
    +* When duplicating a continuation in `mkDupableContWithDmds`, before
    
    939
    +  duplicating a CastIt, optimise the coercion. Otherwise we'll end up
    
    940
    +  optimising it separately in the duplicate copies.
    
    941
    +
    
    942
    +* See also Note [The MOutCoercion field of ContEx]
    
    889 943
     -}
    
    890 944
     
    
    891 945
     optOutCoercion :: SimplEnvIS -> OutCoercion -> Bool -> OutCoercion
    
    892
    --- See Note [Avoid re-simplifying coercions] in GHC.Core.Opt.Simplify.Iteration
    
    946
    +-- See Note [Avoid re-simplifying coercions]
    
    893 947
     optOutCoercion env co already_optimised
    
    894 948
       | already_optimised = co  -- Ditto
    
    895 949
       | otherwise         = optCoercion opts empty_subst co
    
    ... ... @@ -909,7 +963,8 @@ pushCastMCo env (MCo co) cont = pushCast env co cont
    909 963
     
    
    910 964
     pushCast :: SimplEnv -> OutCoercion -> SimplCont -> SimplCont
    
    911 965
     pushCast env co cont
    
    912
    -  = go co True cont
    
    966
    +  | isReflCo co = cont     -- See Note [Optimising reflexivity]
    
    967
    +  | otherwise   = go co True cont
    
    913 968
       where
    
    914 969
     
    
    915 970
         -- ToDo:   pushCast Refl (ApplylToVal arg1 (ApplyToVal arg2 ...))
    
    ... ... @@ -918,7 +973,7 @@ pushCast env co cont
    918 973
         go co1 _ (CastIt { sc_co = co2, sc_cont = cont })  -- See Note [Optimising reflexivity] in GHC.Core.Opt.Simplify.Iteration
    
    919 974
           = go (mkTransCo co1 co2) False cont
    
    920 975
                       -- False: (mkTransCo co1 co2) is not fully optimised
    
    921
    -                  -- See Note [Avoid re-simplifying coercions] in GHC.Core.Opt.Simplify.Iteration
    
    976
    +                  -- See Note [Avoid re-simplifying coercions]
    
    922 977
     
    
    923 978
         go co co_is_opt (ApplyToTy { sc_arg_ty = arg_ty, sc_cont = tail })
    
    924 979
           | Just (tyL, arg_ty', m_co') <- pushCoTyArg co arg_ty
    

  • compiler/GHC/Core/Opt/SpecConstr.hs
    ... ... @@ -2068,7 +2068,11 @@ mkSeqs seqees res_ty rhs =
    2068 2068
             -- Argument representing strict field and it's worth passing via cbv
    
    2069 2069
             | wantCbvForId arg_id
    
    2070 2070
             = Case (Var arg_id)
    
    2071
    -               (localiseId arg_id)  -- See (SCF1) in Note [SpecConstr and strict fields]
    
    2071
    +               (mkCaseBndrFromLetBndr (localiseId arg_id))
    
    2072
    +                   -- localiseId: see (SCF1) in Note [SpecConstr and strict fields]
    
    2073
    +                   -- mkCaseBndrFromLetBndr: arg_id may have IdInfo (e.g. an unfolding,
    
    2074
    +                   --   if it is a global) that a case binder must not have; see
    
    2075
    +                   --   Note [Lambda and case binders have vanilla IdInfo] in GHC.Core
    
    2072 2076
                    res_ty
    
    2073 2077
                    ([Alt DEFAULT [] rhs])
    
    2074 2078