Skip to content

Draft: Make UnfoldingGuidance account for free variables

Simon Peyton Jones requested to merge wip/T3781 into master

For over a decade (#3781) I've wondered whether GHC's UnfoldingGuidance should take account of free variables. For example:

   f x = let g y = case x of ....
         in
         ...(case x of ....g e....)...

If we inline the call to g in the body of the case x, we can save re-evaluating (and case analysis of) x. This very similar to the "discounts" we give for arguments. All we need is to do it for free variables.

It turned out to be rather easy.

  • Beef up GHC.Core.Unfold.sizeExpr to accumulate discounts for free variables.

  • Beef up GHC.Core.Unfold.callSiteInline to compute those discounts, based on info in the InScopeSet

Fixes #3781. Also fixes #19643 (which reminded me of this issue).

Edited by Andreas Klebinger

Merge request reports