Simon Peyton Jones pushed to branch wip/T27737 at Glasgow Haskell Compiler / GHC

Commits:

6 changed files:

Changes:

  • compiler/GHC/Core/Opt/Simplify/Iteration.hs
    ... ... @@ -3785,30 +3785,24 @@ simplAlt env _scrut' imposs_deflt_cons case_bndr' bndr_swap' cont' (Alt DEFAULT
    3785 3785
             ; (floats, rhs') <- simplExprF env' rhs cont'
    
    3786 3786
             ; return (DEFAULT, [], floats, rhs') }
    
    3787 3787
     
    
    3788
    -simplAlt env _scrut' _ case_bndr' bndr_swap' cont' (Alt (LitAlt lit) bndrs rhs)
    
    3788
    +simplAlt env _scrut' _ case_bndr' bndr_swap' cont' (Alt lit_alt@(LitAlt {}) bndrs rhs)
    
    3789 3789
       = assert (null bndrs) $
    
    3790
    -    do  { let env' = addAltUnfoldings env case_bndr' bndr_swap' (Lit lit)
    
    3790
    +    do  { let env' = addAltUnfoldings env case_bndr' bndr_swap' lit_alt []
    
    3791 3791
             ; (floats, rhs') <- simplExprF env' rhs cont'
    
    3792
    -        ; return (LitAlt lit, [], floats, rhs') }
    
    3792
    +        ; return (lit_alt, [], floats, rhs') }
    
    3793 3793
     
    
    3794
    -simplAlt env scrut' _ case_bndr' bndr_swap' cont' (Alt (DataAlt con) vs rhs)
    
    3794
    +simplAlt env scrut' _ case_bndr' bndr_swap' cont' (Alt data_alt@(DataAlt con) vs rhs)
    
    3795 3795
       = do  { -- See Note [Adding evaluatedness info to pattern-bound variables]
    
    3796 3796
               -- and Note [DataAlt occ info]
    
    3797 3797
             ; let vs_with_info = adjustFieldsIdInfo scrut' case_bndr' bndr_swap' con vs
    
    3798
    +        ; (env', vs') <- simplBinders env vs_with_info
    
    3798 3799
               -- Adjust evaluated-ness and occ-info flags before `simplBinders`
    
    3799 3800
               -- because the latter extends the in-scope set, which propagates this
    
    3800 3801
               -- adjusted info to use sites.
    
    3801 3802
     
    
    3802
    -        ; (env', vs') <- simplBinders env vs_with_info
    
    3803
    -
    
    3804
    -                -- Bind the case-binder to (con args)
    
    3805
    -        ; let inst_tys' = tyConAppArgs (idType case_bndr')
    
    3806
    -              con_app :: OutExpr
    
    3807
    -              con_app = mkConApp2 con inst_tys' vs'
    
    3808
    -              env''   = addAltUnfoldings env' case_bndr' bndr_swap' con_app
    
    3809
    -
    
    3803
    +        ; let env'' = addAltUnfoldings env' case_bndr' bndr_swap' data_alt vs'
    
    3810 3804
             ; (floats, rhs') <- simplExprF env'' rhs cont'
    
    3811
    -        ; return (DataAlt con, vs', floats, rhs') }
    
    3805
    +        ; return (data_alt, vs', floats, rhs') }
    
    3812 3806
     
    
    3813 3807
     {- Note [Adding evaluatedness info to pattern-bound variables]
    
    3814 3808
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ... ... @@ -3911,12 +3905,17 @@ addDefaultUnfoldings env case_bndr bndr_swap imposs_deflt_cons
    3911 3905
              | otherwise = env1
    
    3912 3906
     
    
    3913 3907
     
    
    3914
    -addAltUnfoldings :: SimplEnv -> OutId -> BinderSwapDecision -> OutExpr -> SimplEnv
    
    3915
    -addAltUnfoldings env case_bndr bndr_swap con_app
    
    3908
    +addAltUnfoldings :: SimplEnv -> OutId -> BinderSwapDecision
    
    3909
    +                 -> AltCon -> [OutVar] -> SimplEnv
    
    3910
    +addAltUnfoldings env case_bndr bndr_swap con bndrs
    
    3916 3911
       = env2
    
    3917 3912
       where
    
    3918
    -    con_app_unf = mk_simple_unf con_app
    
    3919
    -    env1 = addBinderUnfolding env case_bndr con_app_unf
    
    3913
    +    -- Add unfolding for case_bndr
    
    3914
    +    -- See #28164
    
    3915
    +    env1 | seCaseCase env || null bndrs
    
    3916
    +         = addBinderUnfolding env case_bndr con_app_unf
    
    3917
    +         | otherwise
    
    3918
    +         = env
    
    3920 3919
     
    
    3921 3920
         -- See Note [Add unfolding for scrutinee]
    
    3922 3921
         -- e.g. case (x |> co) of K a b -> blah
    
    ... ... @@ -3929,6 +3928,18 @@ addAltUnfoldings env case_bndr bndr_swap con_app
    3929 3928
     
    
    3930 3929
              | otherwise = env1
    
    3931 3930
     
    
    3931
    +    -- Bind the case-binder to (con args)
    
    3932
    +    con_app_unf :: Unfolding
    
    3933
    +    con_app_unf = mk_simple_unf con_app
    
    3934
    +
    
    3935
    +    con_app :: OutExpr
    
    3936
    +    con_app = case con of
    
    3937
    +          DEFAULT     -> pprPanic "addAltUnfoldings" (ppr case_bndr)
    
    3938
    +          LitAlt lit  -> Lit lit
    
    3939
    +          DataAlt con -> mkConApp2 con inst_tys bndrs
    
    3940
    +            where
    
    3941
    +              inst_tys = tyConAppArgs (idType case_bndr)
    
    3942
    +
    
    3932 3943
         -- Force the opts, so that the whole SimplEnv isn't retained
    
    3933 3944
         !opts = seUnfoldingOpts env
    
    3934 3945
         mk_simple_unf = mkSimpleUnfolding opts
    

  • testsuite/tests/simplCore/should_compile/T28164S.hs
    1
    +{-# LANGUAGE BangPatterns #-}
    
    2
    +module T28164S where
    
    3
    +
    
    4
    +-- step is lazy in k (the x < 0 branch), so its worker takes k boxed.
    
    5
    +-- At -O2 liberate-case peels hash's first iteration, which puts a
    
    6
    +-- `case k of I# y` in the body of go's join points; go's exit also takes
    
    7
    +-- k apart.
    
    8
    +{-# NOINLINE step #-}
    
    9
    +step :: Int -> Int -> Int
    
    10
    +step k x
    
    11
    +  | x < 0 = 0
    
    12
    +  | otherwise = go (hash 0 0)
    
    13
    +  where
    
    14
    +    hash !i !acc | i == k = acc
    
    15
    +                 | otherwise = hash (i + 1) (acc * 131 + x + i)
    
    16
    +    go !j | j < 1000 = j * k
    
    17
    +          | otherwise = go (j `quot` 7)

  • testsuite/tests/simplCore/should_compile/all.T
    ... ... @@ -634,3 +634,6 @@ test('T27737a', normal, compile, ['-O'])
    634 634
     test('T27910', grep_errmsg(r'KnownBranch'), compile, ['-O -dno-typeable-binds -ddump-simpl-stats -dsuppress-uniques'])
    
    635 635
     test('T27892', grep_errmsg(r'^f = g$'), compile, ['-O -ddump-cse -dsuppress-all -dsuppress-uniques'])
    
    636 636
     test('T28095a', grep_errmsg(r'^f = g$'), compile, ['-O -ddump-cse -dsuppress-all -dsuppress-uniques'])
    
    637
    +
    
    638
    +# T28164S: the idea is there should be only one 'case k' in 'step'
    
    639
    +test('T28164S', grep_errmsg(r'case k'), compile, ['-O2 -ddump-simpl'])

  • testsuite/tests/simplCore/should_run/T28164R.hs
    1
    +module Main (main) where
    
    2
    +
    
    3
    +import System.Environment (getArgs)
    
    4
    +
    
    5
    +data T = T ![Int] !Int !Int
    
    6
    +
    
    7
    +{-# NOINLINE work #-}
    
    8
    +work :: [Int] -> [Int] -> Int -> Int -> [Int]
    
    9
    +work sh ss o v = [ x * o + v + y | x <- sh, y <- ss ]
    
    10
    +
    
    11
    +{-# INLINE expensive #-}
    
    12
    +expensive :: [Int] -> T -> [Int]
    
    13
    +expensive sh (T ss o v) = work sh ss o v
    
    14
    +
    
    15
    +{-# INLINE z #-}
    
    16
    +z :: [Int] -> T -> T -> Int
    
    17
    +z sh (T ss _ v) (T _ _ v')
    
    18
    +  | v == 1, v' == 1 = sum ss
    
    19
    +z sh t t' = sum (expensive sh t) + sum (expensive sh t')
    
    20
    +
    
    21
    +{-# NOINLINE mkT #-}
    
    22
    +mkT :: Int -> T
    
    23
    +mkT n = T [1 .. n] 3 5
    
    24
    +
    
    25
    +c :: T
    
    26
    +c = mkT 1000
    
    27
    +
    
    28
    +{-# NOINLINE f #-}
    
    29
    +f :: T -> Int
    
    30
    +f a = z [1 .. 100] a c
    
    31
    +
    
    32
    +main :: IO ()
    
    33
    +main = do
    
    34
    +  [n] <- map read <$> getArgs
    
    35
    +  print (sum [ f (T [k] 0 2) | k <- [1 .. n] ])

  • testsuite/tests/simplCore/should_run/T28164R.stdout
    1
    +13142050000

  • testsuite/tests/simplCore/should_run/all.T
    ... ... @@ -126,3 +126,4 @@ test('T27005', [], multimod_compile_and_run, ['T27005', '-O'])
    126 126
     test('T27705', [extra_hc_opts('+RTS -M500M -RTS')], multimod_compile_and_run,
    
    127 127
          ['T27705', '-O2 -fexpose-all-unfoldings'])
    
    128 128
     test('T28163', normal, compile_and_run, ['-O'])
    
    129
    +test('T28164R', normal, compile_and_run, ['-O'])