Ticket #1168 (new bug)

Opened 2 years ago

Last modified 4 months ago

Optimisation sometimes decreases sharing in IO code

Reported by: simonmar Assigned to:
Priority: normal Milestone: _|_
Component: Compiler Version: 6.6
Severity: normal Keywords:
Cc: adam@thejenkins.org, kfrdbs@gmail.com Difficulty: Unknown
Test Case: nofib/spectral/calendar Architecture: Unknown
Operating System: Unknown

Description

The simplifier is losing sharing in spectral/calendar, probably because we're being a bit fast-and-loose with eta-expanding State# lambdas. A quick look at the Core shows that calFor year is not shared across multiple executions.

Change History

11/05/07 04:31:10 changed by simonmar

  • milestone changed from 6.6.2 to _|_.

I don't think we're going to fix this any time soon. The simplifier is eta-expanding State# lambdas on purpose, because it's often critical for good performance of IO code, but it does risk re-computation in some cases, and this is one of those cases. Leaving the bug open because it is a real bug, someday we might implement something a bit more refined (e.g. arity analysis).

12/05/07 02:12:45 changed by simonmar

See also #1957.

12/05/07 02:22:00 changed by simonmar

  • summary changed from nofib/spectral/calendar is mis-optimised to Optimisation sometimes decreases sharing in IO code.

Changing the subject to indicate the real cause of the problem.

02/03/08 12:32:25 changed by ChrisKuklewicz

This just came up on the mailing list again. The program was slow unless (-fno-state-hack) was used.

Is it possible to have GHC emit a warning (as part of -Wall) to let the use know that GHC is applying a potentially risky simplification?

02/04/08 01:50:17 changed by simonmar

A warning would be good, but I see two potential problems:

  • there would be a lot of false positives
  • the warning has to be generated by the simplifier, and source-location information has been stripped off by this point, so we don't have a good way to report the location of the warning.

02/04/08 05:21:50 changed by simonpj

02/05/08 01:26:38 changed by simonpj

  • cc set to adam@thejenkins.org.

Adding Adam Jenkins to the cc list. At the end of the above thread he writes (reasonably enough):

Thank you for the explanation. Inlining does explain the behavior I was seeing, and -fno-state-hack does make the program behave the way I'd expect.

I would like to humbly submit that perhaps this optimization should be off by default. It seems to me that any compiler optimization which can potentially increase your program's runtime by an order of complexity is dangerous. Also, while the Haskell Report doesn't seem to specify anything about lazy evaluation behavior, it seems to me that in order to program in Haskell, assuming that a computation assigned to a variable won't be recomputed multiple times is almost as necessary an assumption as assuming that the compiler will optimize away tail recursion. For instance GHC FAQ entry 7.2 implicitly assumes this is true:

http://haskell.org/haskellwiki/GHC:FAQ#Does_GHC_do_common_subexpression_elimination.3F

In my real program where I ran into this problem it took me quite a while to figure out why my program's performance wasn't making any sense, and why seemingly irrelevant changes to my code made the performance problem go away.

02/05/08 01:32:30 changed by simonpj

I agree that the current behaviour sometimes bites badly, as it did for you here. Trouble is, removing the hack has (or used to have) a significant negative effect on performance of lots of programs.

Still, I think that there are various other possibilities to try. For example, compiling the I/O libraries (only) with the state-hack on, and with it off by default elsewhere, might gather most of the benefits without the costs. Or restricting it to top level functions. Or something like that.

It needs someone who's willing to do some measurements and try variations.

05/15/08 01:46:56 changed by simonpj

See also #2284

05/15/08 02:09:14 changed by guest

  • cc changed from adam@thejenkins.org to adam@thejenkins.org, kfrdbs@gmail.com.