| ... |
... |
@@ -303,101 +303,229 @@ mkCast expr co |
|
303
|
303
|
* *
|
|
304
|
304
|
********************************************************************* -}
|
|
305
|
305
|
|
|
306
|
|
--- | Wraps the given expression in the source annotation, dropping the
|
|
307
|
|
--- annotation if possible.
|
|
|
306
|
+-- | Wraps the given expression in a Tick, floating the tick as far into
|
|
|
307
|
+-- the AST as possible in order to try to satisfy the tick's desired placement
|
|
|
308
|
+-- properties (as per Note [Tickish placement] in GHC.Types.Tickish).
|
|
|
309
|
+--
|
|
|
310
|
+-- Prefer using 'mkTick' over explicit use of the 'Tick' constructor.
|
|
|
311
|
+--
|
|
|
312
|
+-- Also performs small on-the-fly optimisations:
|
|
|
313
|
+--
|
|
|
314
|
+-- * Eliminate unnecessary ticks by either absorbing them into existing ones
|
|
|
315
|
+-- or dropping them if that is valid (e.g. dropping profiling ticks around
|
|
|
316
|
+-- types, coercions and literals).
|
|
|
317
|
+-- * Split profiling ticks into counting/scoping parts so that the two parts
|
|
|
318
|
+-- can be placed independently into the AST.
|
|
308
|
319
|
mkTick :: CoreTickish -> CoreExpr -> CoreExpr
|
|
309
|
|
-mkTick t orig_expr = mkTick' id orig_expr
|
|
|
320
|
+mkTick t orig_expr = mkTick' orig_expr
|
|
310
|
321
|
where
|
|
311
|
322
|
-- Some ticks (cost-centres) can be split in two, with the
|
|
312
|
323
|
-- non-counting part having laxer placement properties.
|
|
313
|
|
- canSplit = tickishCanSplit t && tickishPlace (mkNoCount t) /= tickishPlace t
|
|
314
|
|
-
|
|
315
|
|
- -- mkTick' handles floating of ticks *into* the expression.
|
|
316
|
|
- mkTick' :: (CoreExpr -> CoreExpr) -- Apply before adding tick (float with)
|
|
317
|
|
- -- Always a composition of (Tick t) wrappers
|
|
318
|
|
- -> CoreExpr -- Current expression
|
|
319
|
|
- -> CoreExpr
|
|
320
|
|
- -- So in the call (mkTick' rest e), the expression
|
|
321
|
|
- -- (rest e)
|
|
322
|
|
- -- has the same type as e
|
|
323
|
|
- -- Returns an expression equivalent to (Tick t (rest e))
|
|
324
|
|
- mkTick' rest expr = case expr of
|
|
325
|
|
- -- Float ticks into unsafe coerce the same way we would do with a cast.
|
|
326
|
|
- Case scrut bndr ty alts@[Alt ac abs _rhs]
|
|
327
|
|
- | Just rhs <- isUnsafeEqualityCase scrut bndr alts
|
|
328
|
|
- -> Case scrut bndr ty [Alt ac abs (mkTick' rest rhs)]
|
|
|
324
|
+ -- See Note [Scoping ticks and counting ticks] in GHC.Types.Tickish.
|
|
|
325
|
+ can_split = tickishCanSplit t
|
|
329
|
326
|
|
|
330
|
|
- -- Cost centre ticks should never be reordered relative to each
|
|
331
|
|
- -- other. Therefore we can stop whenever two collide.
|
|
|
327
|
+ -- mkTick' handles floating of tick `t` *into* the expression.
|
|
|
328
|
+ mkTick' :: CoreExpr -> CoreExpr
|
|
|
329
|
+ mkTick' expr = case expr of
|
|
332
|
330
|
Tick t2 e
|
|
333
|
|
- | ProfNote{} <- t2, ProfNote{} <- t -> Tick t $ rest expr
|
|
334
|
|
-
|
|
335
|
|
- -- Otherwise we assume that ticks of different placements float
|
|
336
|
|
- -- through each other.
|
|
337
|
|
- | tickishPlace t2 /= tickishPlace t -> Tick t2 $ mkTick' rest e
|
|
338
|
|
-
|
|
339
|
|
- -- For annotations this is where we make sure to not introduce
|
|
340
|
|
- -- redundant ticks.
|
|
341
|
|
- | tickishContains t t2 -> mkTick' rest e -- Drop t2
|
|
342
|
|
- | tickishContains t2 t -> rest e -- Drop t
|
|
343
|
|
- | otherwise -> mkTick' (rest . Tick t2) e
|
|
344
|
|
-
|
|
345
|
|
- -- Ticks don't care about types, so we just float all ticks
|
|
346
|
|
- -- through them. Note that it's not enough to check for these
|
|
347
|
|
- -- cases top-level. While mkTick will never produce Core with type
|
|
348
|
|
- -- expressions below ticks, such constructs can be the result of
|
|
349
|
|
- -- unfoldings. We therefore make an effort to put everything into
|
|
350
|
|
- -- the right place no matter what we start with.
|
|
351
|
|
- Cast e co -> mkCast (mkTick' rest e) co
|
|
352
|
|
- Coercion co -> Tick t $ rest (Coercion co)
|
|
|
331
|
+
|
|
|
332
|
+ -- Common up ticks when possible, including profiling ticks that
|
|
|
333
|
+ -- share a cost centre and source notes that subsume one another.
|
|
|
334
|
+ | Just t' <- combineTickish_maybe t t2
|
|
|
335
|
+ -> mkTick t' e
|
|
|
336
|
+
|
|
|
337
|
+ -- Profiling ticks for different cost centres should never be reordered
|
|
|
338
|
+ -- relative to each other. Therefore, we stop whenever two collide.
|
|
|
339
|
+ | ProfNote {} <- t
|
|
|
340
|
+ , ProfNote {} <- t2
|
|
|
341
|
+ -> Tick t e
|
|
|
342
|
+
|
|
|
343
|
+ -- Ticks of different placements float through each other, so that each
|
|
|
344
|
+ -- tick can be floated into its expected position in the AST.
|
|
|
345
|
+ -- See Note [Tickish placement] in GHC.Types.Tickish.
|
|
|
346
|
+ | tickishPlace t2 /= tickishPlace t
|
|
|
347
|
+ -> Tick t2 $ mkTick' e
|
|
353
|
348
|
|
|
354
|
349
|
Lam x e
|
|
355
|
350
|
-- Always float through type lambdas. Even for non-type lambdas,
|
|
356
|
351
|
-- floating is allowed for all but the most strict placement rule.
|
|
357
|
352
|
| not (isRuntimeVar x) || tickishPlace t /= PlaceRuntime
|
|
358
|
|
- -> Lam x $ mkTick' rest e
|
|
|
353
|
+ -> Lam x $ mkTick' e
|
|
359
|
354
|
|
|
360
|
|
- -- If it is both counting and scoped, we split the tick into its
|
|
361
|
|
- -- two components, often allowing us to keep the counting tick on
|
|
362
|
|
- -- the outside of the lambda and push the scoped tick inside.
|
|
363
|
|
- -- The point of this is that the counting tick can probably be
|
|
364
|
|
- -- floated, and the lambda may then be in a position to be
|
|
365
|
|
- -- beta-reduced.
|
|
366
|
|
- | canSplit
|
|
367
|
|
- -> Tick (mkNoScope t) $ rest $ Lam x $ mkTick (mkNoCount t) e
|
|
|
355
|
+ -- Push SCCs into lambdas.
|
|
|
356
|
+ -- See (PSCC2) in Note [Pushing SCCs inwards].
|
|
|
357
|
+ | can_split
|
|
|
358
|
+ -> Tick (mkNoScope t) $ Lam x $ mkTick (mkNoCount t) e
|
|
368
|
359
|
|
|
369
|
360
|
App f arg
|
|
370
|
|
- -- Always float through type applications.
|
|
|
361
|
+ -- All ticks float inwards through non-runtime arguments, as per
|
|
|
362
|
+ -- Note [Tickish placement] in GHC.Types.Tickish.
|
|
371
|
363
|
| not (isRuntimeArg arg)
|
|
372
|
|
- -> App (mkTick' rest f) arg
|
|
|
364
|
+ -> App (mkTick' f) arg
|
|
373
|
365
|
|
|
374
|
|
- -- We can also float through constructor applications, placement
|
|
375
|
|
- -- permitting. Again we can split.
|
|
376
|
|
- | isSaturatedConApp expr && (tickishPlace t==PlaceCostCentre || canSplit)
|
|
|
366
|
+ -- Push SCCs into saturated constructor applications.
|
|
|
367
|
+ -- See (PSCC3) in Note [Pushing SCCs inwards].
|
|
|
368
|
+ | isSaturatedConApp expr
|
|
|
369
|
+ , tickishPlace t == PlaceCostCentre || can_split
|
|
377
|
370
|
-> if tickishPlace t == PlaceCostCentre
|
|
378
|
|
- then rest $ tickHNFArgs t expr
|
|
379
|
|
- else Tick (mkNoScope t) $ rest $ tickHNFArgs (mkNoCount t) expr
|
|
|
371
|
+ then tickHNFArgs t expr
|
|
|
372
|
+ else Tick (mkNoScope t) $ tickHNFArgs (mkNoCount t) expr
|
|
|
373
|
+
|
|
|
374
|
+ -- All ticks can be floated through casts, as per Note [Tickish placement].
|
|
|
375
|
+ Cast e co -> mkCast (mkTick' e) co
|
|
|
376
|
+
|
|
|
377
|
+ -- Treat 'unsafeCoerce' as if it was a cast: float all ticks inwards.
|
|
|
378
|
+ -- See Note [Push ticks into unsafeCoerce]
|
|
|
379
|
+ Case scrut bndr ty alts@[Alt ac abs _rhs]
|
|
|
380
|
+ | Just rhs <- isUnsafeEqualityCase scrut bndr alts
|
|
|
381
|
+ -> Case scrut bndr ty [Alt ac abs (mkTick' rhs)]
|
|
380
|
382
|
|
|
381
|
383
|
Var x
|
|
382
|
|
- | notFunction && tickishPlace t == PlaceCostCentre
|
|
383
|
|
- -> rest expr -- Drop t
|
|
384
|
|
- | notFunction && canSplit
|
|
385
|
|
- -> Tick (mkNoScope t) $ rest expr
|
|
386
|
|
- where
|
|
387
|
|
- -- SCCs can be eliminated on variables provided the variable
|
|
388
|
|
- -- is not a function. In these cases the SCC makes no difference:
|
|
389
|
|
- -- the cost of evaluating the variable will be attributed to its
|
|
390
|
|
- -- definition site. When the variable refers to a function, however,
|
|
391
|
|
- -- an SCC annotation on the variable affects the cost-centre stack
|
|
392
|
|
- -- when the function is called, so we must retain those.
|
|
393
|
|
- notFunction = not (isFunTy (idType x))
|
|
394
|
|
-
|
|
395
|
|
- Lit{}
|
|
|
384
|
+ -- Don't drop any ticks around a function
|
|
|
385
|
+ | isFunTy (idType x)
|
|
|
386
|
+ -> Tick t expr
|
|
|
387
|
+ -- Drop SCCs around non-function variables.
|
|
|
388
|
+ -- See (PSCC1) in Note [Pushing SCCs inwards].
|
|
396
|
389
|
| tickishPlace t == PlaceCostCentre
|
|
397
|
|
- -> rest expr -- Drop t
|
|
|
390
|
+ -> -- Drop pure SCC ticks: scc<foo> (x :: Int) ==> x
|
|
|
391
|
+ expr
|
|
|
392
|
+ | can_split
|
|
|
393
|
+ -> -- Drop the scoping part of the tick, but keep the counting part.
|
|
|
394
|
+ Tick (mkNoScope t) expr
|
|
|
395
|
+
|
|
|
396
|
+ -- It doesn't make sense to wrap static data (such as coercions, types and literals)
|
|
|
397
|
+ -- in a tick which compiles to code, as the code will never be run.
|
|
|
398
|
+ --
|
|
|
399
|
+ -- It is in fact actively harmful, because Core Lint will fail on a
|
|
|
400
|
+ -- coercion binding such as let co = <scc> (...), see #26941.
|
|
|
401
|
+ -- It makes more sense to discard the cost centre tick rather than weakening
|
|
|
402
|
+ -- Core Lint.
|
|
|
403
|
+ e@(Coercion {}) | tickishIsCode t -> e
|
|
|
404
|
+ e@(Type {}) | tickishIsCode t -> e
|
|
|
405
|
+ e@(Lit {}) | tickishIsCode t -> e
|
|
|
406
|
+
|
|
|
407
|
+ -- Catch-all: annotate where we stand.
|
|
|
408
|
+ -- In particular (but not only): Let, most Cases.
|
|
|
409
|
+ _other -> Tick t expr
|
|
|
410
|
+
|
|
|
411
|
+{- Note [Pushing SCCs inwards]
|
|
|
412
|
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
413
|
+Amongst all ticks, SCCs have the laxest placement properties (PlaceCostCentre,
|
|
|
414
|
+as described in Note [Tickish placement] GHC.Types.Tickish):
|
|
|
415
|
+
|
|
|
416
|
+ (PSCC1) SCCs around non-function variables can be eliminated.
|
|
|
417
|
+ The cost of evaluating the variable will be attributed to its definition
|
|
|
418
|
+ site, so the SCC makes no difference. Example:
|
|
|
419
|
+
|
|
|
420
|
+ scc<foo> (x :: Int) ==> x
|
|
|
421
|
+
|
|
|
422
|
+ NB: this is only valid when the variable is not a function. For example, in:
|
|
|
423
|
+
|
|
|
424
|
+ scc<foo> (f :: Int -> Int)
|
|
|
425
|
+
|
|
|
426
|
+ we must retain the cost centre annotation, as it affects the cost-centre
|
|
|
427
|
+ pointer when the function is called. Discarding the SCC in this case would
|
|
|
428
|
+ defeat the profiling mechanism entirely!
|
|
|
429
|
+
|
|
|
430
|
+ (PSCC2) SCCs can be pushed into lambdas.
|
|
|
431
|
+
|
|
|
432
|
+ scc<foo> (\x -> e) ==> \x -> scc<foo> e
|
|
|
433
|
+
|
|
|
434
|
+ (PSCC3) We can push SCCs into (saturated) constructor applications.
|
|
|
435
|
+ For example, for an arity 2 data constructor 'D':
|
|
|
436
|
+
|
|
|
437
|
+ scc<foo> (D e1 e2) ==> D (scc<foo> e1) (scc<foo> e2)
|
|
|
438
|
+
|
|
|
439
|
+Now, two kinds of ticks contain SCCs:
|
|
|
440
|
+
|
|
|
441
|
+ - bare SCCs (i.e. ProfNote with profNoteCounts = False, profNoteScopes = True)
|
|
|
442
|
+ - profiling ticks that both count and scope
|
|
|
443
|
+
|
|
|
444
|
+The above explanation deals with bare SCCs. When handling profiling ticks that
|
|
|
445
|
+both count and scope, we can split tick into two, so that the scoping part can
|
|
|
446
|
+be pushed inwards (or even discarded). Specifically, we perform the following
|
|
|
447
|
+transformations:
|
|
398
|
448
|
|
|
399
|
|
- -- Catch-all: Annotate where we stand
|
|
400
|
|
- _any -> Tick t $ rest expr
|
|
|
449
|
+ (PSCC1) Drop the SCC around non-function variables, keeping only the counting
|
|
|
450
|
+ part:
|
|
|
451
|
+
|
|
|
452
|
+ scctick<foo> (x :: Int) ==> tick<foo> x
|
|
|
453
|
+
|
|
|
454
|
+ (PSCC2) Push the SCC inside lambdas:
|
|
|
455
|
+
|
|
|
456
|
+ scctick<foo> (\x. e) ==> tick<foo> (\x. scc<foo> e)
|
|
|
457
|
+
|
|
|
458
|
+ NB: we must keep the counting part outside the lambda, in order to preserve
|
|
|
459
|
+ tick counter tallies – it would not be sound to push the counting part inside.
|
|
|
460
|
+
|
|
|
461
|
+ (PSCC3) Push the SCC inside saturated contructor applications.
|
|
|
462
|
+
|
|
|
463
|
+ scctick<foo> (D e1 e2) ==> tick<foo> (D (scc<foo> e1) (scc<foo> e2))
|
|
|
464
|
+
|
|
|
465
|
+The benefit of these transformation is that the counting part, tick<foo>, can
|
|
|
466
|
+likely be floated out of the way, which may expose additional optimisation
|
|
|
467
|
+opportunities. For example:
|
|
|
468
|
+
|
|
|
469
|
+ (scctick<foo> (\x. e)) arg
|
|
|
470
|
+
|
|
|
471
|
+ ==>{PSCC2}
|
|
|
472
|
+
|
|
|
473
|
+ (tick<foo> (\x. scc<foo> e)) arg
|
|
|
474
|
+
|
|
|
475
|
+ ==>{GHC.Core.Opt.FloatOut.floatExpr, because 'tick<foo>' has no scope}
|
|
|
476
|
+
|
|
|
477
|
+ tick<foo> ((\x. scc<foo> e) arg)
|
|
|
478
|
+
|
|
|
479
|
+ ==>{beta reduction}
|
|
|
480
|
+
|
|
|
481
|
+ tick<foo> (let x = arg in scc<foo> e)
|
|
|
482
|
+
|
|
|
483
|
+Note [Push ticks into unsafeCoerce]
|
|
|
484
|
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
485
|
+In #25212, we had a program of the form:
|
|
|
486
|
+
|
|
|
487
|
+ data Box = Box Any
|
|
|
488
|
+ asBox :: a -> Box
|
|
|
489
|
+ asBox x = {-# SCC asBox #-} Box (unsafeCoerce x)
|
|
|
490
|
+
|
|
|
491
|
+As per Note [Implementing unsafeCoerce] in GHC.Internal.Unsafe.Coerce, the call
|
|
|
492
|
+to `unsafeCoerce` turns into
|
|
|
493
|
+
|
|
|
494
|
+ case unsafeEqualityProof @Type @a @Any of
|
|
|
495
|
+ UnsafeRefl (co :: a ~# Any) -> x |> Sub co
|
|
|
496
|
+
|
|
|
497
|
+The worker for 'asBox' is then of the form:
|
|
|
498
|
+
|
|
|
499
|
+ $wasBox = \@a (x :: a) ->
|
|
|
500
|
+ (# case unsafeEqualityProof @Type @a @Any of
|
|
|
501
|
+ UnsafeRefl (co :: a ~# Any) -> x |> Sub co
|
|
|
502
|
+ #)
|
|
|
503
|
+
|
|
|
504
|
+When inserting the SCC, we push it into the constructor as per (PSCC3) in
|
|
|
505
|
+Note [Pushing SCCs inwards], so we get:
|
|
|
506
|
+
|
|
|
507
|
+ $wasBox = \@a (x :: a) ->
|
|
|
508
|
+ tick<asBox>
|
|
|
509
|
+ (# scc<asBox>
|
|
|
510
|
+ case unsafeEqualityProof @Type @a @Any of
|
|
|
511
|
+ UnsafeRefl (co :: a ~# Any) -> x |> Sub co
|
|
|
512
|
+ #)
|
|
|
513
|
+
|
|
|
514
|
+Now, if we don't push the SCC tick into the case statement, Core Prep will
|
|
|
515
|
+see an expression like 'MkSolo# (scc<asBox> ...)', which it will ANFise to
|
|
|
516
|
+'let x = scc<asBox> ... in MkSolo# x', creating an unwanted thunk in the process.
|
|
|
517
|
+
|
|
|
518
|
+So the strategy is to treat this 'unsafeEqualityProof' case statement as if it
|
|
|
519
|
+was a cast. We thus push the SCC into the RHS of the pattern match:
|
|
|
520
|
+
|
|
|
521
|
+ $wasBox = \@a (x :: a) ->
|
|
|
522
|
+ tick<asBox>
|
|
|
523
|
+ (# case unsafeEqualityProof @Type @a @Any of
|
|
|
524
|
+ UnsafeRefl (co :: a ~# Any) -> scc<asBox> x |> Sub co
|
|
|
525
|
+ #)
|
|
|
526
|
+
|
|
|
527
|
+Then the SCC completely evaporates, as per (PSCC1) in Note [Pushing SCCs inwards].
|
|
|
528
|
+-}
|
|
401
|
529
|
|
|
402
|
530
|
mkTicks :: [CoreTickish] -> CoreExpr -> CoreExpr
|
|
403
|
531
|
mkTicks ticks expr = foldr mkTick expr ticks
|
| ... |
... |
@@ -2573,8 +2701,8 @@ exprIsTickedString = isJust . exprIsTickedString_maybe |
|
2573
|
2701
|
exprIsTickedString_maybe :: CoreExpr -> Maybe ByteString
|
|
2574
|
2702
|
exprIsTickedString_maybe (Lit (LitString bs)) = Just bs
|
|
2575
|
2703
|
exprIsTickedString_maybe (Tick t e)
|
|
2576
|
|
- -- we don't tick literals with CostCentre ticks, compare to mkTick
|
|
2577
|
|
- | tickishPlace t == PlaceCostCentre = Nothing
|
|
|
2704
|
+ -- Shortcut: ticks with code never wrap literals (compare with 'mkTick')
|
|
|
2705
|
+ | tickishIsCode t = Nothing
|
|
2578
|
2706
|
| otherwise = exprIsTickedString_maybe e
|
|
2579
|
2707
|
exprIsTickedString_maybe _ = Nothing
|
|
2580
|
2708
|
|