Simon Peyton Jones pushed to branch wip/spj-try-opt-coercion at Glasgow Haskell Compiler / GHC

Commits:

3 changed files:

Changes:

  • compiler/GHC/Core/Opt/Simplify/Iteration.hs
    ... ... @@ -1657,7 +1657,6 @@ simplCast env body co0 cont0
    1657 1657
             addCoerce co1 (CastIt { sc_co = co2, sc_cont = cont })  -- See Note [Optimising reflexivity]
    
    1658 1658
               = addCoerce (mkTransCo co1 co2) cont
    
    1659 1659
                           -- False: (mkTransCo co1 co2) is not fully optimised
    
    1660
    -                      -- See Note [Avoid re-simplifying coercions]
    
    1661 1660
     
    
    1662 1661
             addCoerce co (ApplyToTy { sc_arg_ty = arg_ty, sc_cont = tail })
    
    1663 1662
               | Just (arg_ty', m_co') <- pushCoTyArg co arg_ty
    

  • compiler/GHC/Core/Subst.hs
    ... ... @@ -380,8 +380,10 @@ substIdBndr _doc rec_subst subst@(Subst in_scope env tvs cvs) old_id
    380 380
     
    
    381 381
         old_ty = idType old_id
    
    382 382
         old_w = idMult old_id
    
    383
    -    no_type_change = (isEmptyVarEnv tvs && isEmptyVarEnv cvs) ||
    
    383
    +    no_type_change = isEmptyTCvSubst subst ||
    
    384 384
                          (noFreeVarsOfType old_ty && noFreeVarsOfType old_w)
    
    385
    +                     -- isEmptyTCvSubst: see Note [Keeping the substitution empty]
    
    386
    +                     --                  in GHC.Core.TyCo.Subst
    
    385 387
     
    
    386 388
             -- new_id has the right IdInfo
    
    387 389
             -- The lazy-set is because we're in a loop here, with
    

  • compiler/GHC/Core/TyCo/Subst.hs
    ... ... @@ -960,7 +960,8 @@ substTyVarBndrUsing subst_fn subst@(Subst in_scope idenv tenv cenv) old_var
    960 960
         -- Assertion check that we are not capturing something in the substitution
    
    961 961
     
    
    962 962
         old_ki = tyVarKind old_var
    
    963
    -    no_kind_change = noFreeVarsOfType old_ki -- verify that kind is closed
    
    963
    +    no_kind_change = isEmptyTCvSubst subst || noFreeVarsOfType old_ki
    
    964
    +                     -- isEmptyTCvSubst: see Note [Keeping the substitution empty]
    
    964 965
         no_change = no_kind_change && (new_var == old_var)
    
    965 966
             -- no_change means that the new_var is identical in
    
    966 967
             -- all respects to the old_var (same unique, same kind)
    
    ... ... @@ -988,7 +989,8 @@ substCoVarBndrUsing subst_fn subst@(Subst in_scope idenv tenv cenv) old_var
    988 989
         (Subst (in_scope `extendInScopeSet` new_var) idenv tenv new_cenv, new_var)
    
    989 990
       where
    
    990 991
         new_co         = mkCoVarCo new_var
    
    991
    -    no_kind_change = noFreeVarsOfTypes [t1, t2]
    
    992
    +    no_kind_change = isEmptyTCvSubst subst || noFreeVarsOfTypes [t1, t2]
    
    993
    +                     -- isEmptyTCvSubst: see Note [Keeping the substitution empty]
    
    992 994
         no_change      = new_var == old_var && no_kind_change
    
    993 995
     
    
    994 996
         new_cenv | no_change = delVarEnv cenv old_var
    
    ... ... @@ -1034,3 +1036,22 @@ substTyCoBndr subst (Anon ty af) = (subst, Anon (substScaledTy subst ty
    1034 1036
     substTyCoBndr subst (Named (Bndr tv vis)) = (subst', Named (Bndr tv' vis))
    
    1035 1037
                                               where
    
    1036 1038
                                                 (subst', tv') = substVarBndr subst tv
    
    1039
    +
    
    1040
    +{- Note [Keeping the substitution empty]
    
    1041
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1042
    +A very common situation is where we run over a term doing no cloning,
    
    1043
    +no substitution, nothing.  In that case the TCvSubst till be empty, and
    
    1044
    +it is /very/ valuable to /keep/ it empty:
    
    1045
    +
    
    1046
    +* It's wasted effort to build up an identity substitution mapping
    
    1047
    +  [x:->x, y:->y].
    
    1048
    +
    
    1049
    +* When we come to a binder, if the incoming substitution is empty,
    
    1050
    +  we can avoid substituting its type; and that in turn may mean that
    
    1051
    +  the binder itself does not change and we don't need to extend the
    
    1052
    +  substitution.
    
    1053
    +
    
    1054
    +* In the Simplifier we substitute over both types and coercions.
    
    1055
    +  If the substitution is empty, this is a no-op -- but only if it
    
    1056
    +  is empty!
    
    1057
    +-}