| ... |
... |
@@ -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-c)
|
|
|
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-c)
|
|
|
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 unlifted 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
|
~~~~~~~~~~~~~~~~~~~~~
|