Opened 4 years ago

Closed 4 years ago

#10602 closed bug (fixed)

ghc panic: Template variable unbound in rewrite rule when compiling with -O2

Reported by: pacak Owned by: bgamari
Priority: highest Milestone: 7.10.2
Component: Compiler Version: 7.10.2-rc2
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Compile-time crash Test Case: simplCore/should_compile/T10602
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by goldfire)

I've seen a few related tickets, but they are market as closed.

% ghc -O2 binlist.hs


[1 of 1] Compiling Main             ( binlist.hs, binlist.o )
ghc: panic! (the 'impossible' happened)
  (GHC version 7.10.1.20150630 for x86_64-unknown-linux):
        Template variable unbound in rewrite rule
  sg_s5zh
  [sc_s5zf, sc_s5zg, sg_s5zh, sg_s5zi]
  [sc_s5zf, sc_s5zg, sg_s5zh, sg_s5zi]
  [: @ a_a3fo sc_s5zf sc_s5zg]
  [: @ a_a3fo sc_s5zb sc_s5zc]

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

% cat binlist.hs

import Control.Monad
import Data.Binary
import Data.List


newtype A a = A [a]


instance Binary a => Binary (A a) where
    put (A xs) = case splitAt 254 xs of
        (_, []) -> mapM_ put xs
        (a, b)  -> put (A b)
 
    get = do xs <- replicateM 254 get
             A ys <- get
             return $ A $ xs ++ ys

main :: IO ()
main = undefined

Attachments (2)

T10602b.hs (532 bytes) - added by thomie 4 years ago.
T10602.hs (764 bytes) - added by thomie 4 years ago.

Download all attachments as: .zip

Change History (23)

comment:1 Changed 4 years ago by goldfire

Description: modified (diff)

comment:2 Changed 4 years ago by goldfire

Operating System: LinuxUnknown/Multiple
Priority: normalhighest

This is a regression since 7.10.1.

comment:3 Changed 4 years ago by bgamari

Owner: set to bgamari

Oh dear. I'll have a look.

comment:4 Changed 4 years ago by bgamari

Precisely what version of GHC are you using? I can reproduce this neither with 7.10.1 nor the current state of the 7.10 branch.

Last edited 4 years ago by bgamari (previous) (diff)

comment:5 Changed 4 years ago by bgamari

Status: newinfoneeded

comment:6 Changed 4 years ago by thomie

I can reproduce this, but only with the perf build, not the stage2 build. Strange.

comment:7 Changed 4 years ago by pacak

rc2 downloaded from ghc website as ghc-7.10.1.20150630-src.tar.xz

% cat mk/build.mk

GhcRTSWays += debug
GhcRTSWays += debug_p
GhcRTSWays += thr_debug
GhcRTSWays += thr_debug_p

BUILD_DOCBOOK_HTML = YES

DYNAMIC_GHC_PROGRAMS = NO

comment:8 Changed 4 years ago by pacak

ghc 7.10.1 can compile the same code without any problems. This build of rc2 was compiled by ghc 7.10.1 plus a few patches on top related to event manager issues. Before submitting the bug I've confirmed that somebody from #ghc can reproduce it.

comment:9 Changed 4 years ago by goldfire

I observed the failure with 7.10.2-rc1 (7.10.1.20150612). I forget if I built it myself or downloaded from somewhere. I don't have more recent builds to hand.

comment:10 Changed 4 years ago by pacak

Step by step manual to reproduce

% tar xf ../ghc-7.10.1.20150630-src.tar.xz
% cp mk/build.mk.sample build.mk
% vim mk/build.mk -- uncomment BuildFlavour = perf
% autoreconf
% perl boot
% ./configure && make -j4
[stuff]
% ./inplace/bin/ghc-stage2 binlist.hs -O2

[1 of 1] Compiling Main             ( binlist.hs, binlist.o )
ghc-stage2: panic! (the 'impossible' happened)
  (GHC version 7.10.1.20150630 for x86_64-unknown-linux):
        Template variable unbound in rewrite rule
  sg_s5zd
  [sc_s5zb, sc_s5zc, sg_s5zd, sg_s5ze]
  [sc_s5zb, sc_s5zc, sg_s5zd, sg_s5ze]
  [: @ a_a3fj sc_s5zb sc_s5zc]
  [: @ a_a3fj sc_s5z7 sc_s5z8]

comment:11 Changed 4 years ago by bgamari

Indeed I have reproduced this. Previously I missed that -O2 was necessary; I believe I was just using -O previously.

comment:12 Changed 4 years ago by bgamari

Hmm, that being said, this is an extremely sensitive bug. I still haven't been able to reproduce it with the current state of the ghc-7.10 branch.

comment:13 Changed 4 years ago by goldfire

If it's working in HEAD and at the tip of the ghc-7.10 branch, I think it's safe to close the ticket with a regression test. Given the sensitivity of the bug, it might be worth the trouble to make sure that the regression test actually fails on, say, 7.10.2-rc1.

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

In a6359f2629024d67fc82a7d41c67d5d89d2d3a3d/ghc:

Add testcase for #10602

comment:15 Changed 4 years ago by thomie

Test still fails when base is compiled with -O2 (as it is in a perf or default/release build).

$ make TEST=T10602 TEST_HC=ghc-7.10.1.20150710
...
=====> T10602(optasm) 92 of 92 [0, 0, 0] 
cd . &&  "/opt/ghc/7.10.2/bin/ghc-7.10.1.20150710" -c T10602.hs -fforce-recomp -dcore-lint -dcmm-lint -dno-debug-output -no-user-package-db -rtsopts -fno-warn-tabs -fno-ghci-history  -O -fasm -O2 > T10602.comp.stderr 2>&1
Compile failed (status 256) errors were:
ghc: panic! (the 'impossible' happened)
  (GHC version 7.10.1.20150710 for x86_64-unknown-linux):
	Template variable unbound in rewrite rule
  sg_s5zh
  [sc_s5zf, sc_s5zg, sg_s5zh, sg_s5zi]
  [sc_s5zf, sc_s5zg, sg_s5zh, sg_s5zi]
  [: @ a_a3fj sc_s5zf sc_s5zg]
  [: @ a_a3fj sc_s5zb sc_s5zc]

Changed 4 years ago by thomie

Attachment: T10602b.hs added

Changed 4 years ago by thomie

Attachment: T10602.hs added

comment:16 Changed 4 years ago by thomie

I attached an alternative test that also fails with a devel2 build. -fno-spec-constr makes the problem go away, as do a few other things (see T10602.hs).

Maybe this debug message is of help to someone:

$ ./ghc-devel2/inplace/bin/ghc-stage2 T10602.hs -fforce-recomp
[1 of 2] Compiling T10602b          ( T10602b.hs, T10602b.o )
[2 of 2] Compiling T10602           ( T10602.hs, T10602.o )
SpecConstr
  Function ‘$wgo_s13x’ has one call pattern, but the limit is 0
  Use -fspec-constr-count=n to set the bound
  Use -dppr-debug to see specialisations
ghc-stage2: panic! (the 'impossible' happened)
Last edited 4 years ago by thomie (previous) (diff)

comment:17 Changed 4 years ago by bgamari

Hmm, this is very odd. thomie, I suppose your alternate test case still only reproduces with the ghc-stage2 binary?

Last edited 4 years ago by bgamari (previous) (diff)

comment:18 Changed 4 years ago by thomie

I'm not sure what you're asking. For me, it fails with both ghc-stage1 and ghc-stage2 from a devel2 build of HEAD, as well as with ghc-7.10.1.20150710 from hvr's ppa.

If it wasn't clear: you need both T10602.hs and T10602b.hs.

comment:19 Changed 4 years ago by simonpj

OK I'm on it. Thank you for a simple repro case.

Simon

comment:20 Changed 4 years ago by simonpj

Status: infoneededmerge
Test Case: simplCore/should_compile/T10602

Fixed by

commit 7da7b0e48598af7df25e1129772b42cb31649c74
Author: Simon Peyton Jones <simonpj@microsoft.com>
Date:   Mon Jul 13 12:58:34 2015 +0100

    Make sure rule LHSs are simplified
    
    SpecConstr was generating a rule LHS with nested casts,
    which the simplifier then optimised away.  Result: unbound
    template variables.
    
    Easily fixed.  See Note [SpecConstr call patterns]


>---------------------------------------------------------------

7da7b0e48598af7df25e1129772b42cb31649c74
 compiler/specialise/SpecConstr.hs                  | 25 ++++++++++--
 testsuite/tests/simplCore/should_compile/T10602.hs | 46 +++++++++-------------
 .../tests/simplCore/should_compile/T10602b.hs      | 20 ++++++++++
 testsuite/tests/simplCore/should_compile/all.T     |  2 +-
 4 files changed, 61 insertions(+), 32 deletions(-)

diff --git a/compiler/specialise/SpecConstr.hs b/compiler/specialise/SpecConstr.hs

I forgot to mention the ticket in the commit message.

comment:21 Changed 4 years ago by bgamari

Resolution: fixed
Status: mergeclosed
Note: See TracTickets for help on using tickets.