Skip to content

runRW inlining in CorePre may go awry with bottoming continuation

In GHC.CoreToSTG.Prep we inline runRW into its call-sites (as described in Note [runRW magic]). Specifically, we perform two types of rewrites:

  1. a beta-reduction if possible: runRW (\s -> expr) ~> expr[s / realWorld]
  2. otherwise, a simple inlining: runRW f ~> f realWorld

Both rewrites are useful in that they eliminate the indirect call that dispatching to runRW's Haskell definition would usually imply.

However, rewrite (2) may be problematic in the case of bottoming continuations (as described in Note [runRW arg]). In particular, consider the application

runRW (case bottom of {})

In this case, rewrite (2) will result in

(case bottom of {}) realWorld

However, this will break collectArgs, which expects an application chain to be headed by an identifier.

I believe this can only happen with -O0 since runRW's strictness signature captures the fact that it calls its argument. Consequently, the simplifier can rewrite runRW (case bottom of {}) to case bottom of {}.

We should measure whether rewrite (2) offers any tangible performance benefit and depending upon the answer either fix it or remove it.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information