Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#11533 closed bug (fixed)

Stack check not optimized out even if it could be

Reported by: jscholl Owned by:
Priority: normal Milestone: 8.0.1
Component: Compiler (CodeGen) Version: 7.10.3
Keywords: cmm Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Runtime performance bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D1881
Wiki Page:


CmmLayoutStack tries to remove a stack check if a function uses no stack space. It knows that Sp < SpLim is always false, but not that Sp >= SpLim is always true. However, the latter can arise when GHC flips the comparison (which it does sometimes).

An example would be the worker function generated for go:

countDown :: Int -> Int
countDown = go 0
        go acc n | n > 0 = go (acc + 1) (n - 1)
                 | otherwise = acc + n

Change History (5)

comment:1 Changed 4 years ago by jscholl

Differential Rev(s): Phab:D1881
Status: newpatch

comment:2 Changed 4 years ago by Ben Gamari <ben@…>

In 4ec61411/ghc:

Fix the removal of unnecessary stack checks

The module CmmLayoutStack removes stack checks if a function does not
use stack space. However, it can only recognize checks of the form
Sp < SpLim. However, these checks get sometimes rewritten to
Sp >= SpLim (with both branches swapped), so we better recognize these
checks too.

Test Plan: ./validate

Reviewers: austin, bgamari, simonpj

Reviewed By: simonpj

Subscribers: simonpj, thomie

Differential Revision:

GHC Trac Issues: #11533

comment:3 Changed 4 years ago by bgamari

Milestone: 8.0.1
Status: patchmerge

comment:4 Changed 4 years ago by bgamari

Resolution: fixed
Status: mergeclosed

comment:5 Changed 4 years ago by thomie

For the record, reports:

nofib/time/fannkuch-redux 	2.727	+ 4.66%	2.854	seconds

On Phab, Jonas said:

"I think nofib contained maybe between 20 and 40 cases where the the stack check was incorrectly not dropped. I could not measure a performance difference, but binary size decreased by a few bytes (as one would expect)"

fannkuch-redux has been investigated for performance regressions before: #7367, #10457, #10677, #10844,

Note: See TracTickets for help on using tickets.