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

Commits:

6 changed files:

Changes:

  • compiler/GHC/Core.hs
    ... ... @@ -490,7 +490,7 @@ This is a well-kinded type, in an environment-based scheme. But `typeKind`
    490 490
     will panic, because the kind of the body of a forall must not mention
    
    491 491
     the forall'd variable (and the inner forall's body appears to do so).
    
    492 492
     
    
    493
    -It might be possible to make `exprType` and `typeKind`` more complicated, so that
    
    493
    +It might be possible to make `exprType` and `typeKind` more complicated, so that
    
    494 494
     they do renaming on the fly.  But instead we impose
    
    495 495
     
    
    496 496
     INVARIANT (NoTypeShadowing):
    

  • compiler/GHC/Core/Type.hs
    ... ... @@ -55,7 +55,7 @@ module GHC.Core.Type (
    55 55
             splitForAllForAllTyBinders, splitForAllForAllTyBinder_maybe,
    
    56 56
             splitForAllTyCoVar_maybe, splitForAllTyCoVar,
    
    57 57
             splitForAllTyVar_maybe, splitForAllCoVar_maybe,
    
    58
    -        splitPiTy_maybe, splitPiTy, splitPiTys, collectPiTyBinders,
    
    58
    +        splitPiTy_maybe, splitPiTy, splitPiTys, splitUninstantiatedValArgTys,
    
    59 59
             getRuntimeArgTys,
    
    60 60
             mkTyConBindersPreferAnon,
    
    61 61
             mkPiTy, mkPiTys,
    
    ... ... @@ -263,7 +263,6 @@ import GHC.Utils.Monad.StrictReader
    263 263
     
    
    264 264
     import GHC.Data.FastString
    
    265 265
     import GHC.Data.Maybe   ( orElse, isJust )
    
    266
    -import GHC.List (build)
    
    267 266
     
    
    268 267
     -- $type_classification
    
    269 268
     -- #type_classification#
    
    ... ... @@ -1599,17 +1598,20 @@ splitPiTys ty = split ty ty []
    1599 1598
         split orig_ty ty bs | Just ty' <- coreView ty = split orig_ty ty' bs
    
    1600 1599
         split orig_ty _                bs = (reverse bs, orig_ty)
    
    1601 1600
     
    
    1602
    -collectPiTyBinders :: Type -> [PiTyBinder]
    
    1603
    -collectPiTyBinders ty = build $ \c n ->
    
    1604
    -  let
    
    1605
    -    split (ForAllTy b res) = Named b `c` split res
    
    1606
    -    split (FunTy { ft_af = af, ft_mult = w, ft_arg = arg, ft_res = res })
    
    1607
    -                           = Anon (Scaled w arg) af `c` split res
    
    1608
    -    split ty | Just ty' <- coreView ty = split ty'
    
    1609
    -    split _                = n
    
    1610
    -  in
    
    1611
    -    split ty
    
    1612
    -{-# INLINE collectPiTyBinders #-}
    
    1601
    +splitUninstantiatedValArgTys :: Type -> [Type]
    
    1602
    +-- This function takes a type apart, returning all its /value/
    
    1603
    +-- argument types (including (forall (co::t1~t2).t)
    
    1604
    +-- doing no instantiation at all.
    
    1605
    +-- E.g. splitUinstantiatedValArgTys (forall a. a -> Int -> T a)
    
    1606
    +--         returns [a, Int]
    
    1607
    +-- It's a pretty specialised function, hence its long name
    
    1608
    +splitUninstantiatedValArgTys ty = go ty
    
    1609
    +  where
    
    1610
    +    go (ForAllTy (Bndr b _) res) | isCoVar b  = varType b : go res
    
    1611
    +                                 | otherwise  = go res
    
    1612
    +    go (FunTy { ft_arg = arg, ft_res = res }) = arg : go res
    
    1613
    +    go ty | Just ty' <- coreView ty = go ty'
    
    1614
    +    go _ = []
    
    1613 1615
     
    
    1614 1616
     -- | Extracts a list of run-time arguments from a function type,
    
    1615 1617
     -- looking through newtypes to the right of arrows.
    

  • compiler/GHC/Core/Utils.hs
    ... ... @@ -2207,6 +2207,8 @@ fun_always_ok _ = True
    2207 2207
     -- | A special version of 'exprOkForSpeculation' used during
    
    2208 2208
     -- Note [Speculative evaluation]. When the predicate arg `fun_ok` returns False
    
    2209 2209
     -- for `b`, then `b` is never considered ok-for-spec.
    
    2210
    +--
    
    2211
    +-- This is the whole reason for the fun_ok arg to expr_ok.
    
    2210 2212
     exprOkForSpecEval :: (Id -> Bool) -> CoreExpr -> Bool
    
    2211 2213
     exprOkForSpecEval fun_ok = expr_ok fun_ok primOpOkForSpeculation
    
    2212 2214
     
    
    ... ... @@ -2240,10 +2242,9 @@ expr_ok fun_ok primop_ok (Case scrut bndr _ alts)
    2240 2242
       && altsAreExhaustive alts
    
    2241 2243
     
    
    2242 2244
     expr_ok fun_ok primop_ok other_expr
    
    2243
    -  | (expr, args) <- collectArgs other_expr
    
    2245
    +  | (expr, val_args) <- collectValArgs other_expr
    
    2244 2246
       = case stripTicksTopE (not . tickishCounts) expr of
    
    2245
    -        Var f ->
    
    2246
    -           app_ok fun_ok primop_ok f args
    
    2247
    +        Var f -> app_ok fun_ok primop_ok f val_args
    
    2247 2248
     
    
    2248 2249
             -- 'LitRubbish' is the only literal that can occur in the head of an
    
    2249 2250
             -- application and will not be matched by the above case (Var /= Lit).
    
    ... ... @@ -2258,9 +2259,10 @@ expr_ok fun_ok primop_ok other_expr
    2258 2259
     
    
    2259 2260
     -----------------------------
    
    2260 2261
     app_ok :: (Id -> Bool) -> (PrimOp -> Bool) -> Id -> [CoreArg] -> Bool
    
    2261
    -app_ok fun_ok primop_ok fun args
    
    2262
    +app_ok fun_ok primop_ok fun val_args
    
    2262 2263
       | not (fun_ok fun)
    
    2263
    -  = False -- This code path is only taken for Note [Speculative evaluation]
    
    2264
    +  = False -- This code path is only taken for exprOkForSpecEval
    
    2265
    +          -- See Note [Speculative evaluation]
    
    2264 2266
     
    
    2265 2267
       | idArity fun > n_val_args
    
    2266 2268
       -- Partial application: just check passing the arguments is OK
    
    ... ... @@ -2281,17 +2283,18 @@ app_ok fun_ok primop_ok fun args
    2281 2283
     
    
    2282 2284
           ClassOpId _ is_terminating_result
    
    2283 2285
             | is_terminating_result -- See Note [exprOkForSpeculation and type classes]
    
    2284
    -        -> assertPpr (n_val_args == 1) (ppr fun $$ ppr args) $
    
    2286
    +        -> assertPpr (n_val_args == 1) (ppr fun $$ ppr val_args) $
    
    2285 2287
                True
    
    2286 2288
                -- assert: terminating result type => can't be applied;
    
    2287 2289
                -- c.f the _other case below
    
    2288 2290
     
    
    2289 2291
           PrimOpId op _
    
    2290 2292
             | primOpIsDiv op
    
    2291
    -        , Lit divisor <- last args
    
    2293
    +        , Lit divisor <- last val_args
    
    2292 2294
                 -- there can be 2 args (most div primops) or 3 args
    
    2293 2295
                 -- (WordQuotRem2Op), hence the use of last/init
    
    2294
    -        -> not (isZeroLit divisor) && all (expr_ok fun_ok primop_ok) (init args)
    
    2296
    +        -> not (isZeroLit divisor) &&
    
    2297
    +           all ok_for_spec (init val_args)
    
    2295 2298
                   -- Special case for dividing operations that fail
    
    2296 2299
                   -- In general they are NOT ok-for-speculation
    
    2297 2300
                   -- (which primop_ok will catch), but they ARE OK
    
    ... ... @@ -2300,12 +2303,14 @@ app_ok fun_ok primop_ok fun args
    2300 2303
                   -- can get rid of a thunk in an inner loop
    
    2301 2304
     
    
    2302 2305
             | otherwise -> primop_ok op && args_ok
    
    2306
    +              -- Assumes that the primop does not evaluate its
    
    2307
    +              -- definitely-lifted arguments
    
    2303 2308
     
    
    2304 2309
           _other  -- Unlifted and terminating types;
    
    2305 2310
                   -- Also c.f. the Var case of exprIsHNF
    
    2306 2311
              |  isTerminatingType fun_ty  -- See Note [exprOkForSpeculation and type classes]
    
    2307 2312
              || definitelyUnliftedType fun_ty
    
    2308
    -         -> assertPpr (n_val_args == 0) (ppr fun $$ ppr args)
    
    2313
    +         -> assertPpr (n_val_args == 0) (ppr fun $$ ppr val_args)
    
    2309 2314
                 True  -- Both terminating types (e.g. Eq a), and unlifted types (e.g. Int#)
    
    2310 2315
                       -- are non-functions and so will have no value args.  The assert is
    
    2311 2316
                       -- just to check this.
    
    ... ... @@ -2321,32 +2326,43 @@ app_ok fun_ok primop_ok fun args
    2321 2326
                  --     for evaluated-ness of the fun;
    
    2322 2327
                  --     see Note [exprOkForSpeculation and evaluated variables]
    
    2323 2328
       where
    
    2324
    -    fun_ty       = idType fun
    
    2325
    -    n_val_args   = valArgCount args
    
    2326
    -    (arg_tys, _) = splitPiTys fun_ty
    
    2329
    +    ok_for_spec = expr_ok fun_ok primop_ok   -- Used for recursive calls
    
    2330
    +
    
    2331
    +    fun_ty      = idType fun
    
    2332
    +    n_val_args  = length val_args
    
    2333
    +    val_arg_tys = splitUninstantiatedValArgTys fun_ty
    
    2334
    +                  -- val_arg_tys are /uninstantiated/; see (EOKS1)
    
    2327 2335
     
    
    2328 2336
         -- Even if a function call itself is OK, any unlifted
    
    2329 2337
         -- args are still evaluated eagerly and must be checked
    
    2330
    -    args_ok = all2Prefix arg_ok arg_tys args
    
    2331
    -    arg_ok :: PiTyVarBinder -> CoreExpr -> Bool
    
    2332
    -    arg_ok (Named _) _ = True   -- A type argument
    
    2333
    -    arg_ok (Anon ty _) arg      -- A term argument
    
    2334
    -       | definitelyLiftedType (scaledThing ty)
    
    2335
    -       = True -- lifted args are not evaluated eagerly
    
    2336
    -       | otherwise
    
    2337
    -       = expr_ok fun_ok primop_ok arg
    
    2338
    +    --   arg_ok bndr arg = field_ok bndr NotMarkedStrict arg
    
    2339
    +    -- See Note [exprOkForSpeculation] (a,b)
    
    2340
    +    args_ok = all2Prefix arg_ok val_arg_tys val_args
    
    2341
    +    arg_ok :: Type -> CoreExpr -> Bool
    
    2342
    +    arg_ok arg_ty arg = ok_for_spec arg || definitely_lifted arg_ty arg
    
    2338 2343
     
    
    2339 2344
         -- Used for strict DataCon worker arguments
    
    2340 2345
         -- See (SFC1) of Note [Strict fields in Core]
    
    2341
    -    fields_ok str_marks = all3Prefix field_ok arg_tys str_marks args
    
    2342
    -    field_ok :: PiTyVarBinder -> StrictnessMark -> CoreExpr -> Bool
    
    2343
    -    field_ok (Named _)   _   _ = True
    
    2344
    -    field_ok (Anon ty _) str arg
    
    2345
    -       | NotMarkedStrict <- str                 -- iff it's a lazy field
    
    2346
    -       , definitelyLiftedType (scaledThing ty)  -- and its type is lifted
    
    2347
    -       = True                                   -- then the worker app does not eval
    
    2348
    -       | otherwise
    
    2349
    -       = expr_ok fun_ok primop_ok arg
    
    2346
    +    -- See Note [exprOkForSpeculation] (a,b)
    
    2347
    +    fields_ok str_marks = all3Prefix field_ok val_arg_tys str_marks val_args
    
    2348
    +    field_ok :: Type -> StrictnessMark -> CoreExpr -> Bool
    
    2349
    +    field_ok arg_ty str arg
    
    2350
    +       = ok_for_spec arg || (definitely_lifted arg_ty arg && str==NotMarkedStrict)
    
    2351
    +       -- The (ok_for_spec arg) test works for /both/ for unlifted types, /and/
    
    2352
    +       -- for lifted but strict fields. E.g. K (I# 3) is ok-for-spec,
    
    2353
    +       -- even if K is strict
    
    2354
    +
    
    2355
    +    -- We want to see if the argument type is definitely lifted.
    
    2356
    +    -- But consider
    
    2357
    +    --     (#,#) @rep1 @rep2 @ty1 @ty2 arg1 arg2
    
    2358
    +    -- We don't /instantiate/ the type of (#,#) (although we could) so we don't
    
    2359
    +    -- know if the argument type is lifted or not.  So we look at the type of
    
    2360
    +    -- the /actual/ argument (it's usually a variable) instead.
    
    2361
    +    definitely_lifted uninstantiated_arg_ty arg
    
    2362
    +       = case typeLevity_maybe uninstantiated_arg_ty of
    
    2363
    +           Just Lifted   -> True
    
    2364
    +           Just Unlifted -> False
    
    2365
    +           Nothing       -> definitelyLiftedType (exprType arg)
    
    2350 2366
     
    
    2351 2367
     -----------------------------
    
    2352 2368
     altsAreExhaustive :: [Alt b] -> Bool
    
    ... ... @@ -2390,8 +2406,42 @@ cantEtaReduceFun fun
    2390 2406
            -- Don't undersaturate StrictWorkerIds.
    
    2391 2407
            -- See Note [CBV Function Ids: overview] in GHC.Types.Id.Info.
    
    2392 2408
     
    
    2393
    -{- Note [exprOkForSpeculation and type classes]
    
    2394
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2409
    +{- Note [exprOkForSpeculation]
    
    2410
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2411
    +Suppose we have
    
    2412
    +  data T a = K !a Int# a
    
    2413
    +
    
    2414
    +Is the call (K e1 e2 e3) ok-for-speculation?  Look at the arguments e1..e3,
    
    2415
    +and their types.  The call is ok-for-speculation if each argument satisfies
    
    2416
    +`field_ok`, namely:
    
    2417
    +  (a) (exprOkForSpeculation arg) holds, OR
    
    2418
    +  (b) Arg type is definitely lifted and it is not marked strict
    
    2419
    +
    
    2420
    +For (a) we conservatively assume we are going to evaluate the argument.
    
    2421
    +If it is ok-for-spec, then fine.
    
    2422
    +
    
    2423
    +But if the argument is of a lifted type, and not marked strict (notably by
    
    2424
    +a data constructor) then (b) holds.  For example
    
    2425
    +    Just (reverse xs)
    
    2426
    +is ok-for-spec.  See (EOKS1) for the argument liftedness test.
    
    2427
    +
    
    2428
    +If we have a plain variable `x`, is that ok-for-spec? Not if it is lifted:
    
    2429
    +it might be a thunk!  Hence the (definitelyUnliftedType fun_ty) test.
    
    2430
    +Remember, a naked variable `x` will land in `app_ok x []` with no arguments.
    
    2431
    +See Note [exprOkForSpeculation and evaluated variables].
    
    2432
    +
    
    2433
    +(EOKS1) To get the lifted-ness or otherwise of the arguments we just look at the
    
    2434
    +  /uninstantiated/ type of the data constructor or primop.  We'd get better information
    
    2435
    +  if we instantiated, but it's also more expensive.  Example:
    
    2436
    +    (#,#) :: forall r1 r2 (a::TYPE r1) (b::TYPE r2). a -> b -> (# a, b #)
    
    2437
    +  If we have a call
    
    2438
    +       (#,#) @LiftedRep @IntRep @Int @Int# 3 4#
    
    2439
    +  then we could see accurate liftedness by instantiation, but we don't try.
    
    2440
    +  Instead we get the actual type from (exprType arg).  That could be expensive,
    
    2441
    +  but it really only happens for unboxed tuples.
    
    2442
    +
    
    2443
    +Note [exprOkForSpeculation and type classes]
    
    2444
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2395 2445
     Consider (#22745, #15205)
    
    2396 2446
     
    
    2397 2447
       \(d :: C a b). case eq_sel (sc_sel d) of
    
    ... ... @@ -2674,8 +2724,7 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e
    2674 2724
             -- PAP: Check unlifted val_args
    
    2675 2725
             args_hnf             = all2Prefix check_arg   val_arg_tys           val_args
    
    2676 2726
     
    
    2677
    -        fun_ty = idType id
    
    2678
    -        val_arg_tys = mapMaybe anonPiTyBinderType_maybe (collectPiTyBinders fun_ty)
    
    2727
    +        val_arg_tys = splitUninstantiatedValArgTys (idType id)
    
    2679 2728
               -- val_arg_tys = map exprType val_args, but much less costly.
    
    2680 2729
               -- The obvious definition regresses T16577 by 30% so we don't do it.
    
    2681 2730
     
    
    ... ... @@ -2700,7 +2749,6 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e
    2700 2749
               | otherwise
    
    2701 2750
               = Nothing
    
    2702 2751
     
    
    2703
    -
    
    2704 2752
     {-
    
    2705 2753
     Note [exprIsHNF Tick]
    
    2706 2754
     ~~~~~~~~~~~~~~~~~~~~~
    

  • testsuite/tests/simplCore/should_compile/T27910.hs
    1
    +{-# LANGUAGE UnboxedTuples, MagicHash #-}
    
    2
    +module T27910 where
    
    3
    +import GHC.Exts
    
    4
    +
    
    5
    +k1 :: (# Int, Bool #) -> (# Int, Bool #) -> Int
    
    6
    +{-# NOINLINE k1 #-}
    
    7
    +k1 _ _ = 3
    
    8
    +
    
    9
    +k2 :: (# Int#, Int# #) -> (# Int#, Int# #) -> Int
    
    10
    +{-# NOINLINE k2 #-}
    
    11
    +k2 _ _ = 3
    
    12
    +
    
    13
    +lifted :: Int -> Bool -> Int
    
    14
    +lifted x y = case (# x, y #) of r -> k1 r r
    
    15
    +
    
    16
    +unlifted :: Int# -> Int# -> Int
    
    17
    +unlifted x y = case (# x, y #) of r -> k2 r r

  • testsuite/tests/simplCore/should_compile/T27910.stderr
    1
    +
    
    2
    +==================== FloatOut stats: ====================
    
    3
    +2 Lets floated to top level; 0 Lets floated elsewhere; from 4 Lambda groups
    
    4
    +
    
    5
    +
    
    6
    +
    
    7
    +==================== FloatOut stats: ====================
    
    8
    +4 Lets floated to top level; 0 Lets floated elsewhere; from 6 Lambda groups
    
    9
    +
    
    10
    +
    
    11
    +
    
    12
    +==================== Grand total simplifier statistics ====================
    
    13
    +Total ticks:     30
    
    14
    +
    
    15
    +8 PreInlineUnconditionally
    
    16
    +  1 ds
    
    17
    +  1 ds
    
    18
    +  1 ds
    
    19
    +  1 ds
    
    20
    +  1 ds
    
    21
    +  1 ds
    
    22
    +  1 ds
    
    23
    +  1 ds
    
    24
    +2 PostInlineUnconditionally
    
    25
    +  1 r
    
    26
    +  1 r
    
    27
    +4 UnfoldingDone
    
    28
    +  2 GHC.Internal.Prim.void#
    
    29
    +  1 T27910.k1
    
    30
    +  1 T27910.k2
    
    31
    +8 BetaReduction
    
    32
    +  1 ds
    
    33
    +  1 ds
    
    34
    +  1 ds
    
    35
    +  1 ds
    
    36
    +  1 ds
    
    37
    +  1 ds
    
    38
    +  1 ds
    
    39
    +  1 ds
    
    40
    +4 KnownBranch
    
    41
    +  2 wild
    
    42
    +  1 ww
    
    43
    +  1 ww
    
    44
    +4 CaseElim
    
    45
    +  2 ww
    
    46
    +  2 ww
    
    47
    +9 SimplifierDone 9
    
    48
    +
    
    49
    +

  • testsuite/tests/simplCore/should_compile/all.T
    ... ... @@ -627,5 +627,5 @@ test('T27749',
    627 627
          compile, ['-O -g3 -ddump-simpl -dsuppress-uniques'])
    
    628 628
     
    
    629 629
     test('T27629', only_ways(['optasm']), makefile_test, ['T27629'])
    
    630
    -
    
    631 630
     test('T27936', normal, compile, [''])
    
    631
    +test('T27910', grep_errmsg(r'KnownBranch'), compile, ['-O -dno-typeable-binds -ddump-simpl-stats -dsuppress-uniques'])