Zubin pushed to branch wip/26416 at Glasgow Haskell Compiler / GHC

Commits:

2 changed files:

Changes:

  • compiler/GHC/Core/Opt/DmdAnal.hs
    ... ... @@ -1111,8 +1111,17 @@ dmdAnalRhsSig top_lvl rec_flag env let_sd id rhs
    1111 1111
         full_dmd_ty = addUnfoldingDemands env rhs_sd id rhs_dmd_ty
    
    1112 1112
         DmdType full_rhs_env combined_rhs_dmds = full_dmd_ty
    
    1113 1113
     
    
    1114
    -    (final_rhs_dmds, final_rhs) = finaliseArgBoxities env id ww_arity
    
    1115
    -                                                      combined_rhs_dmds (de_div full_rhs_env) rhs'
    
    1114
    +    final_rhs_dmds = finaliseArgBoxities env id ww_arity
    
    1115
    +                                         combined_rhs_dmds (de_div full_rhs_env) rhs'
    
    1116
    +
    
    1117
    +    -- Attach the final demands to the lambda binders of the RHS.
    
    1118
    +    -- IMPORTANT: The lambda binders of final_rhs must carry the final demand
    
    1119
    +    -- info, because worker/wrapper drives decisions from the idDemandInfo on
    
    1120
    +    -- the lambdas (see mkWwstr_one), NOT from the strictness signature of the
    
    1121
    +    -- function. So the demands must reflect both the unfolding combination
    
    1122
    +    -- (from addUnfoldingDemands) and the boxity finalisation (from
    
    1123
    +    -- finaliseArgBoxities).
    
    1124
    +    final_rhs = set_lam_dmds final_rhs_dmds rhs'
    
    1116 1125
     
    
    1117 1126
         dmd_sig_arity = ww_arity + strictCallArity body_sd
    
    1118 1127
         sig = mkDmdSigForArity dmd_sig_arity (DmdType sig_env final_rhs_dmds)
    
    ... ... @@ -2053,29 +2062,20 @@ positiveTopBudget (MkB n _) = n >= 0
    2053 2062
     
    
    2054 2063
     finaliseArgBoxities :: AnalEnv -> Id -> Arity
    
    2055 2064
                         -> [Demand] -> Divergence
    
    2056
    -                    -> CoreExpr -> ([Demand], CoreExpr)
    
    2065
    +                    -> CoreExpr -> [Demand]
    
    2057 2066
     -- POSTCONDITION:
    
    2058
    --- If:    (dmds', rhs') = finaliseArgBoxitities ... dmds .. rhs
    
    2067
    +-- If:    dmds' = finaliseArgBoxities ... dmds .. rhs
    
    2059 2068
     -- Then:
    
    2060 2069
     --     dmds' is the same as dmds (including length), except for boxity info
    
    2061
    ---     rhs'  is the same as rhs, except that the idDemandInfo on the outer
    
    2062
    ---           lambda binders now includes the same finalised boxity info as dmds'
    
    2063 2070
     -- NB: For join points, length dmds might be greater than ww_arity
    
    2064
    ---
    
    2065
    --- IMPORTANT: The lambda binders of rhs' must carry the final demand info,
    
    2066
    --- because worker/wrapper drives decisions from the idDemandInfo on the lambdas
    
    2067
    --- (see mkWwstr_one), NOT from the strictness signature of the function.
    
    2068
    --- So the demands must reflect both the unfolding combination (from
    
    2069
    --- addUnfoldingDemands) and the boxity finalisation done here.
    
    2071
    +-- NB: rhs is needed only to count visible binders.
    
    2070 2072
     finaliseArgBoxities env fn ww_arity arg_dmds div rhs
    
    2071 2073
     
    
    2072 2074
       -- Check for an OPAQUE function: see Note [OPAQUE pragma]
    
    2073 2075
       -- In that case, trim off all boxity info from argument demands
    
    2074
    -  -- and demand info on lambda binders
    
    2075 2076
       -- See Note [The OPAQUE pragma and avoiding the reboxing of arguments]
    
    2076 2077
       | isOpaquePragma (idInlinePragma fn)
    
    2077
    -  , let trimmed_arg_dmds = map trimBoxity arg_dmds
    
    2078
    -  = (trimmed_arg_dmds, set_lam_dmds trimmed_arg_dmds rhs)
    
    2078
    +  = map trimBoxity arg_dmds
    
    2079 2079
     
    
    2080 2080
       -- Check that we have enough visible binders to match the
    
    2081 2081
       -- ww arity; if not, we won't do worker/wrapper
    
    ... ... @@ -2086,7 +2086,7 @@ finaliseArgBoxities env fn ww_arity arg_dmds div rhs
    2086 2086
       -- It's a bit of a corner case.  Anyway for now we pass on the
    
    2087 2087
       -- unadulterated demands from the RHS, without any boxity trimming.
    
    2088 2088
       | ww_arity > count isId bndrs
    
    2089
    -  = (arg_dmds, rhs)
    
    2089
    +  = arg_dmds
    
    2090 2090
     
    
    2091 2091
       -- The normal case
    
    2092 2092
       | otherwise
    
    ... ... @@ -2095,10 +2095,7 @@ finaliseArgBoxities env fn ww_arity arg_dmds div rhs
    2095 2095
         --        , text "max" <+> ppr max_wkr_args
    
    2096 2096
         --        , text "dmds before:" <+> ppr (map idDemandInfo (filter isId bndrs))
    
    2097 2097
         --        , text "dmds after: " <+>  ppr arg_dmds' ]) $
    
    2098
    -    (arg_dmds', set_lam_dmds arg_dmds' rhs)
    
    2099
    -    -- set_lam_dmds: we must attach the final boxities to the lambda-binders
    
    2100
    -    -- of the function, both because that's kosher, and because CPR analysis
    
    2101
    -    -- uses the info on the binders directly.
    
    2098
    +    arg_dmds'
    
    2102 2099
       where
    
    2103 2100
         opts           = ae_opts env
    
    2104 2101
         (bndrs, _body) = collectBinders rhs
    
    ... ... @@ -2186,18 +2183,18 @@ finaliseArgBoxities env fn ww_arity arg_dmds div rhs
    2186 2183
                      | positiveTopBudget bg_inner' = (bg_inner', dmd')
    
    2187 2184
                      | otherwise                   = (bg_inner,  trimBoxity dmd)
    
    2188 2185
     
    
    2189
    -    set_lam_dmds :: [Demand] -> CoreExpr -> CoreExpr
    
    2190
    -    -- Attach the demands to the outer lambdas of this expression
    
    2191
    -    set_lam_dmds (dmd:dmds) (Lam v e)
    
    2192
    -      | isTyVar v = Lam v (set_lam_dmds (dmd:dmds) e)
    
    2193
    -      | otherwise = Lam (v `setIdDemandInfo` dmd) (set_lam_dmds dmds e)
    
    2194
    -    set_lam_dmds dmds (Cast e co) = Cast (set_lam_dmds dmds e) co
    
    2195
    -       -- This case happens for an OPAQUE function, which may look like
    
    2196
    -       --     f = (\x y. blah) |> co
    
    2197
    -       -- We give it strictness but no boxity (#22502)
    
    2198
    -    set_lam_dmds _ e = e
    
    2199
    -       -- In the OPAQUE case, the list of demands at this point might be
    
    2200
    -       -- non-empty, e.g., when looking at a PAP. Hence don't panic (#22997).
    
    2186
    +set_lam_dmds :: [Demand] -> CoreExpr -> CoreExpr
    
    2187
    +-- Attach the demands to the outer lambdas of this expression
    
    2188
    +set_lam_dmds (dmd:dmds) (Lam v e)
    
    2189
    +  | isTyVar v = Lam v (set_lam_dmds (dmd:dmds) e)
    
    2190
    +  | otherwise = Lam (v `setIdDemandInfo` dmd) (set_lam_dmds dmds e)
    
    2191
    +set_lam_dmds dmds (Cast e co) = Cast (set_lam_dmds dmds e) co
    
    2192
    +   -- This case happens for an OPAQUE function, which may look like
    
    2193
    +   --     f = (\x y. blah) |> co
    
    2194
    +   -- We give it strictness but no boxity (#22502)
    
    2195
    +set_lam_dmds _ e = e
    
    2196
    +   -- In the OPAQUE case, the list of demands at this point might be
    
    2197
    +   -- non-empty, e.g., when looking at a PAP. Hence don't panic (#22997).
    
    2201 2198
     
    
    2202 2199
     finaliseLetBoxity
    
    2203 2200
       :: AnalEnv
    

  • compiler/GHC/Types/Demand.hs
    ... ... @@ -874,10 +874,22 @@ because:
    874 874
     
    
    875 875
       * The optimised RHS may have had transformations applied that reveal
    
    876 876
         strictness (e.g., inlining exposes a case on an argument).
    
    877
    +    Example:
    
    878
    +       RHS: x
    
    879
    +       Unfolding: head [x]
    
    880
    +    It's clear that the RHS is strict in `x`, but the demand analyser won't
    
    881
    +    spot that when it analyses the unfolding.
    
    877 882
     
    
    878 883
       * The optimised RHS may have had transformations applied that drop usage
    
    879 884
         (e.g., a rewrite rule fires that doesn't use an argument, or a seq on
    
    880 885
         a dictionary is dropped because dictionaries are known to terminate).
    
    886
    +    Example:
    
    887
    +       RHS: a
    
    888
    +       Unfolding: fst g
    
    889
    +    where `g` is in scope as `g = (a,b)`.
    
    890
    +
    
    891
    +See Note [Absence analysis for stable unfoldings and RULES] in
    
    892
    +GHC.Core.Opt.DmdAnal for the broader context.
    
    881 893
     
    
    882 894
     When we inline the stable unfolding at a call site, we get the unfolding's
    
    883 895
     behaviour, not the RHS's. So we must be conservative and combine the demands:
    
    ... ... @@ -894,9 +906,6 @@ behaviour, not the RHS's. So we must be conservative and combine the demands:
    894 906
     
    
    895 907
     So for cardinality bounds [l1..u1] from RHS and [l2..u2] from unfolding,
    
    896 908
     we compute [max(l1,l2)..max(u1,u2)].
    
    897
    -
    
    898
    -See Note [Absence analysis for stable unfoldings and RULES] in GHC.Core.Opt.DmdAnal
    
    899
    -for the broader context.
    
    900 909
     -}
    
    901 910
     
    
    902 911
     -- | Takes the maximum of both the lower and upper bound of two 'Card's.