Ticket #47 (closed defect: invalid)

Opened 3 years ago

Last modified 3 years ago

Fusion broken with ghc7-rc2

Reported by: choener Owned by:
Priority: blocker Milestone:
Version: 0.7 Keywords: ghc7-rc2
Cc:

Description

Hi,

maybe it is too late but GHCi, version 7.0.0.20101028 (ghc7 rc2) seems to badly break fusion.

Consider this program:

module Main where

import qualified Data.Vector.Unboxed as VU



mk :: Int -> VU.Vector Int
mk k = {-# CORE "VUmk" #-} VU.enumFromN 1 k
{-# INLINE mk #-}

join1 :: Int -> VU.Vector Int
join1 k = {-# CORE "VUjoin" #-} VU.zipWith (\k l -> k+l) (VU.map (\k -> k+1) $ mk k) (VU.map (\k -> k*5) $ mk k)
{-# INLINE join1 #-}

join2 :: Int -> VU.Vector Int
join2 k = {-# CORE "VUjoin2" #-} VU.map (\k -> k+1) $ mk k
{-# INLINE join2 #-}

main = do
  print $ {-# CORE "VUsum1" #-} VU.sum $ join1 20
  print $ {-# CORE "VUsum2" #-} VU.sum $ join2 20
  print $ {-# CORE "VUsum3" #-} VU.sum $ VU.map (\k -> k+1) $ VU.enumFromN (1 :: Int) 20

where I wanted to see if 'join1' produces good code -- if yes then I could split some calculations up more easily.

What ghc 6.12.3 produces is this:

main_$s$wfoldlM'_loop1 =
  \ (sc_s1Oe :: Int#)
    (sc1_s1Of :: Int#)
    (sc2_s1Og :: Int#) ->
    case ># sc_s1Oe 0 of _ {
      False -> sc2_s1Og;
      True ->
        main_$s$wfoldlM'_loop1
          (-# sc_s1Oe 1)
          (+# sc1_s1Of 1)
          (+# sc2_s1Og (+# sc1_s1Of 1))
    }

main3 :: String
GblId

main3 =
  case __core_note "VUsum2"
       (case main_$s$wfoldlM'_loop1 20 1 0 of ww_s1MG { __DEFAULT ->
        I# ww_s1MG
        })
  of _ { I# ww_a1Iu ->
  $wshowSignedInt 0 ww_a1Iu ([] @ Char)
  }

So far, so good. Now comes rc2:

main3 =
  case __core_note "VUsum2"
       (case (__core_note "VUmk"
              (runSTRep
                 @ (Data.Vector.Unboxed.Base.Vector Int) main4))
             `cast` (trans
                       Data.Vector.Unboxed.Base.TFCo:R:VectorInt
                       Data.Vector.Unboxed.Base.NTCo:R:VectorInt
                     :: Data.Vector.Unboxed.Base.Vector Int
                          ~
                        Data.Vector.Primitive.Vector Int)
        of _ { Data.Vector.Primitive.Vector ipv_s2oo ipv1_s2op ipv2_s2oq ->
        letrec {
          $s$wfoldlM'_loop_s2Bw [Occ=LoopBreaker]
            :: Int# -> Int# -> Int#

          $s$wfoldlM'_loop_s2Bw =
            \ (sc_s2yG :: Int#) (sc1_s2yH :: Int#) ->
              case >=# sc_s2yG ipv1_s2op of _ {
                False ->
                  $s$wfoldlM'_loop_s2Bw
                    (+# sc_s2yG 1)
                    (+#
                       sc1_s2yH
                       (+#
                          (indexIntArray# ipv2_s2oq (+# ipv_s2oo sc_s2yG))
                          1));
                True -> sc1_s2yH
              }; } in
        case $s$wfoldlM'_loop_s2Bw 0 0 of ww_s2vB { __DEFAULT ->
        I# ww_s2vB
        }
        })
  of _ { I# ww_a2r6 ->
  $wshowSignedInt 0 ww_a2r6 ([] @ Char)
  }

This seems rather bad ;-)

I'd like to hear that it's my fault and I should better learn how to use ghc.

Change History

Changed 3 years ago by rl

  • priority changed from critical to blocker

It's your fault and you should better learn how to use ghc ;-) There are two things at work here. Firstly, rc2 doesn't inline certain functions from base. This is fixed in the head, though, and will hopefully be fixed in the final release. Secondly, Core notes sometimes interfere with optimisations. In this particular case, these two issues conspire to break fusion. With the head, fusion works even in the presence of Core notes. With rc2, it works if you remove the notes on mk and join2. The notes are, of course, entirely your fault!

I'll leave this open in case I've missed something and it breaks for you even without the notes. Please let me know!

Changed 3 years ago by choener

-- CORE NOTES break fusion, OMFG

That's the first line in that file right now. I have disabled all CORE notes except the one directly after print ("VUsum1") and get the expected result, fusion works again. I'll test again with the final ghc7 -- as of course I put CORE notes everywhere.

Thanks, now I am relieved.

Changed 3 years ago by rl

  • status changed from new to closed
  • resolution set to invalid

I decided that this isn't entirely your fault after all. See http://hackage.haskell.org/trac/ghc/ticket/4463.

Note: See TracTickets for help on using tickets.