Version 3 (modified by nfrisby, 4 years ago) (diff)


Notes about running demand analysis a second time, late in the pipeline.

Commit c080f727ba5f83921b842fcff71e9066adbdc250

The numbers quoted on this wiki page were using ef017944600cf4e153aad686a6a78bfb48dea67a as the base commit — after measuring, I rebased my patch to apply it to 33c880b43ed72d77f6b1d95d5ccefbd376c78c78

The corresponding testsuite commit is a7920ef6eefa5578c89b7cda0d6be207ee38c502

Commit notes

The -flate-dmd-anal flag runs the demand analysis a second time just before CorePrep. It's not on by default yet, but we hope -O2 will eventually imply it, perhaps even for the GHC 7.8 release.

The bulk of this patch merely simplifies the treatment of wrappers in interface files.


  • Update the documentation to explain -flate-dmd-anal.
  • Ask the community for help in determining if we should make -O2 imply -flate-dmd-anal.

Removing the clever .hi files scheme

Running the demand analyzer twice breaks some expectations of the .hi file format. Prior to this commit, GHC regenerated the wrapper's body from the its strictness signature and worker id. Now, instead, the body is simply encoded just like any other InlineStable.

This change…

  1. simplifies a special case; there's plenty of knock-on code elimination from no longer having ids in UnfoldingSource,
  2. increases the size of .hi files (see below),
  3. accordingly increases compile time a bit (eg ~ +1% over nofib),
  4. accommodates the late demand analysis (see below)
  5. similarly accommodates the -ffun-to-thunk flag

Simplifying the .hi scheme was the easiest way to enable -flate-dmd-anal and make -ffun-to-thunk safe to use. It is possible to revert back to the clever .hi scheme. It will however require some care in order to safely interoperate with -flate-dmd-anal, -ffun-to-thunk, and any future work that similarly effects the accuracy of the clever .hi file scheme's regeneration phase.

Effect on .hi file size

Removing the clever .hi file scheme for wrappers results as expected in an increase of .hi file size.

In $TOPDIR/libraries, there's an extra 569,509 bytes of .hi file.

Here's the files with a growth >10K.

(bytes growth,file)

Here's the files with a growth >10%.


Accommodation of -flate-dmd-anal and -ffun-to-thunk --

The clever .hi scheme caused CoreLint errors when combined with -flate-dmd-anal. I irresponsibly cannot remember the recipe for this bug. It was triggered in one of three ways: building GHC, running nofib, or running ./validate.

Similar to -flate-dmd-anal, abandoning the clever .hi scheme lets us safely import code compiled with/without -ffun-to-thunk from a module compiled without/with -ffun-to-thunk. I can explain this one.

  • Compile A.hs with -ffun-to-thunk
  • Compile a file B.hs that imports A.hs without -ffun-to-thunk

If demand analysis removes all the value arguments from a function f in A.hs and B.hs uses that function, compilation of B.hs will crash. The problem is that the regeneration of the body of f in B will attempt to apply f to a realWorld# argument because there is no -ffun-to-thunk flag. However, f no longer accepts any arguments, since it was compiled with -ffun-to-thunk. Boom.

(The -flate-dmd-anal bug was similar, but more involved.)


-flate-dmd-anal adds a second demand analysis with a subsequent invocation of the simplifier just before CorePrep. Cf #7782

Effect on .hi file size and .a file size

The second demand analysis generates more worker/wrapper splits, so it also generates larger .hi files and larger .o files. The numbers in this section measure the difference between -O2 -flate-dmd-anal and -O2 -fno-late-dmd-anal. This is on my 64 bit Mac OS X.

It's based on the size of the .hi and .a files in $TOPDIR/libraries.

.hi bytes.a bytes
no late-dmd
difference +552,057 +684,696

These are the big .hi changes over 10K.

(growth bytes,  module)

These are the big .hi changes over 10%.

(growth%,  module)

These are the big .a changes over 10K.

growth bytes module