Opened 7 years ago

Closed 7 years ago

#7292 closed bug (fixed)

Optimization works for Word but not Word32 or Word64

Reported by: zuserm Owned by: igloo
Priority: high Milestone: 7.8.1
Component: Compiler Version: 7.6.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Runtime performance bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

GHC 7.6.1 fails to properly optimize some code when explicitly specifying word size, that optimizes properly for Word.

I have also tested this in 7.4.2 and it works properly.

An example program:

import Criterion.Main
import Control.Monad.Writer
import Data.Bits
import Data.List
import Data.Word

bitByBitCopy :: Word -> Word
bitByBitCopy w = foldl' f 0 [0 .. bitSize w - 1]
  where f acc n = if w `testBit` n then acc `setBit` n else acc

bitByBitCopy32 :: Word32 -> Word32
bitByBitCopy32 w = foldl' f 0 [0 .. bitSize w - 1]
  where f acc n = if w `testBit` n then acc `setBit` n else acc

bitByBitCopy64 :: Word64 -> Word64
bitByBitCopy64 w = foldl' f 0 [0 .. bitSize w - 1]
  where f acc n = if w `testBit` n then acc `setBit` n else acc

-- Bench
wbench :: Benchmarkable b => String -> b -> Writer [Benchmark] ()
wbench s b = tell [bench s b]

main = defaultMain . execWriter $ do
    wbench "Word" $ nf bitByBitCopy 0
    when (bitSize (0::Word) == 32) $
        wbench "Word32" $ nf bitByBitCopy32 0
    when (bitSize (0::Word) == 64) $
        wbench "Word64" $ nf bitByBitCopy64 0

Change History (3)

comment:1 Changed 7 years ago by zuserm

Summary: Optimization works for Word but now Word32 or Word64Optimization works for Word but not Word32 or Word64

comment:2 Changed 7 years ago by simonpj

difficulty: Unknown
Milestone: 7.8.1
Owner: set to igloo
Priority: normalhigh

comment:3 Changed 7 years ago by igloo

Resolution: fixed
Status: newclosed

Thanks for the report. Fixed by:

commit 6fae60c48d13b4c3d8cbb3bf317b1af95d162924
Author: Ian Lynagh <ian@well-typed.com>
Date:   Thu Oct 25 17:13:36 2012 +0100

    Make sure testBit and bit get inlined; fixes #7292
Note: See TracTickets for help on using tickets.