Skip to content

GHC panic with -O and -fno-full-laziness

Summary

GHC panics on the program below with -O and -fno-full-laziness.

{-# LANGUAGE ScopedTypeVariables #-}
module M where

import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed

bfs :: Array Int [Int] -> ST s (STArray s Int ())
bfs g = do
    vis :: STArray s Int () <- newArray (bounds g) ()
    ch :: STArray s Int () <- newArray (bounds g) ()
    let go [] = pure () :: ST s ()
        go q = do
            flip mapM_ q $ \u -> do
                readArray vis (head (g!u))
                readArray ch u
                writeArray ch u ()
            go []
    go []
    pure ch

Steps to reproduce

$ ghc -O -fno-full-laziness bfs.hs
[1 of 1] Compiling M                ( bfs.hs, bfs.o )
ghc: panic! (the 'impossible' happened)
  (GHC version 8.10.7:
        GHC.StgToCmm.Env: variable not found
  exit7_X1D
  local binds for:
  ww_s2D1
  ww1_s2D2
  ww2_s2D3
  ww3_s2D4
  w1_s2D7
  lwild_s2D8
  ipv1_s2Db
  w2_s2Dd
  lwild1_s2De
  ipv3_s2Dh
  wild1_s2Di
  wild2_s2Dj
  exit_s2Dk
  exit1_s2Dn
  exit2_s2DL
  exit3_s2E9
  exit4_s2Ed
  go_s2Eh
  ds_s2Ei
  wild_s2Ek
  exit5_s2En
  exit6_s2Ep
  y_s2Es
  Call stack:
      CallStack (from HasCallStack):
        callStackDoc, called at compiler/utils/Outputable.hs:1179:37 in ghc:Outputable
        pprPanic, called at compiler/GHC/StgToCmm/Env.hs:149:9 in ghc:GHC.StgToCmm.Env

Please report this as a GHC bug:  https://www.haskell.org/ghc/reportabug

With -O2 there's a different error.

$ ghc -O2 -fno-full-laziness bfs.hs
[1 of 1] Compiling M                ( bfs.hs, bfs.o )
Simplifier ticks exhausted
  When trying UnfoldingDone exit_X1D
  To increase the limit, use -fsimpl-tick-factor=N (default 100).
   
  If you need to increase the limit substantially, please file a
  bug report and indicate the factor you needed.
   
  If GHC was unable to complete compilation even with a very large factor
  (a thousand or more), please consult the "Known bugs or infelicities"
  section in the Users Guide before filing a report. There are a
  few situations unlikely to occur in practical programs for which
  simplifier non-termination has been judged acceptable.
   
  To see detailed counts use -ddump-simpl-stats
  Total ticks: 28521

Expected behavior

The program compiles.

Environment

  • GHC version used: 8.10.7. Can't reproduce on 9.0.2.

Optional:

  • Operating System: Ubuntu
  • System Architecture: x86_64
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information