Skip to content

cgCase of PrimAlts needs care in new codegen

The following code,

module Spring where

import Data.Array.Unboxed

type Arr = UArray Int Double
data Spring = Spring !Double !Int !Arr deriving Show

step :: Double -> Spring -> Spring
step h (Spring k sz y) = let
    f arr = listArray (0, 2*sz-1) (velocity ++ accel)
      where
        velocity = [arr ! i | i <- [sz .. 2*sz-1]]
        k'       = k * fromIntegral sz^2
        accel    = [0] ++ [k' * (arr!(i-1) - 2 * arr!i + arr!(i+1))
                           | i <- [1 .. sz-2]]
                   ++ [k' * (arr!(sz-2) - arr!(sz-1))]
    (.*) :: Double -> Arr -> Arr
    a .* b  = listArray (0, 2*sz-1) $ map (a*) (elems b)
    (<+>) :: Arr -> Arr -> Arr
    a <+> b = listArray (0, 2*sz-1) $ zipWith (+) (elems a) (elems b)
    -- order 4 Runge-Kutta
    k1 = h .* f y
    k2 = h .* f (y <+> (0.5 .* k1))
    k3 = h .* f (y <+> (0.5 .* k2))
    k4 = h .* f (y <+> k3)
    y' = y <+> ((1/6) .* (k1 <+> (2 .* (k2 <+> k3)) <+> k4))
  in
    Spring k sz y'

doesn't compile with optimization in ghc-6.11:

# ghc -O -c Bug.hs
/tmp/ghc12296_0/ghc12296_0.s: Assembler messages:

/tmp/ghc12296_0/ghc12296_0.s:2355:0:
     Error: bad register name `%st(-8)'

/tmp/ghc12296_0/ghc12296_0.s:2384:0:
     Error: bad register name `%st(-8)'

/tmp/ghc12296_0/ghc12296_0.s:2563:0:
     Error: bad register name `%st(-8)'

/tmp/ghc12296_0/ghc12296_0.s:2602:0:
     Error: bad register name `%st(-8)'

/tmp/ghc12296_0/ghc12296_0.s:3006:0:
     Error: bad register name `%fake0'

/tmp/ghc12296_0/ghc12296_0.s:3023:0:
     Error: bad register name `%fake0'

It's odd, first it tries to assign a closure pointer to an FPU register:

        movl $r1sa_closure,%fake0

and later it uses register 0 (%eax) as an operand to an FPU operation:

#       gsubl %fake1,%eax,%fake1
        #GSUB-xxxcase1
        ffree %st(7) ; fld %st(-8) ; fsubrp %st(0),%st(2)

Using -fregs-graph didn't help.

Trac metadata
Trac field Value
Version 6.11
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler (NCG)
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
Edited by Simon Marlow
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information