Opened 4 years ago

Closed 4 years ago

#8072 closed bug (fixed)

Optimizations change result of div for Word

Reported by: Khudyakov Owned by:
Priority: high Milestone: 7.8.1
Component: Compiler Version: 7.6.3
Keywords: Cc: mail@…, jan.stolarek@…
Operating System: Unknown/Multiple Architecture: x86
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: 3676 Differential Rev(s):
Wiki Page:

Description

Here is test case

import Data.Word

main :: IO ()
main = do
  print $ (maxBound :: Word) `div` 101

And result of execution

$ ghc -fforce-recomp -O2 --make reduce.hs && ./reduce
[1 of 1] Compiling Main             ( reduce.hs, reduce.o )
Linking reduce ...
2891661149
@{debian:0}~/qqq/mwc $ ghc -fforce-recomp --make reduce.hs && ./reduce
[1 of 1] Compiling Main             ( reduce.hs, reduce.o )
Linking reduce ...
42524428

Bug is only present on 32-bit systems. It goes away if rewrite rules are disable with -fno-enable-rewrite-rules or anything but maxBound is used as first argument or second argument is forced to be a funcion argument. So it could be bug in constant folding.

Change History (7)

comment:1 Changed 4 years ago by Khudyakov

GHC is 7.6.3 affected but 7.4.2 and 7.2.2 are not

comment:2 Changed 4 years ago by nh2

Cc: mail@… added

comment:3 Changed 4 years ago by jstolarek

Cc: jan.stolarek@… added

#3676 reports a similar problem - optimization changing result of arithmetical operation.

comment:4 Changed 4 years ago by jstolarek

And I can confirm that this happens in HEAD on 32 bits, but not on 64 bits.

comment:5 Changed 4 years ago by Khudyakov

I don't think #3676 is related. It's because of incorrectness of realToFrac. Funky values like NaN and ±∞ are not representable as rationals so realToFrac converts Float to Double incorrectly but specialized version does it right so this difference arise. In this bug we are dealing only with integer arithmetics.

comment:6 Changed 4 years ago by igloo

Milestone: 7.8.1
Priority: normalhigh

comment:7 Changed 4 years ago by igloo

Resolution: fixed
Status: newclosed

Thanks for the report, and to int-e for finding the problem. Fixed by:

commit af77ef45d576fddb25acf28beb434ef431f430a0

Author: Ian Lynagh <ian@well-typed.com>
Date:   Sun Jul 21 19:09:45 2013 +0100

    Fix GHC.Enum on 32bit platforms; fixes #8072
    
    Diagnosed by int-e.
    
    MachDeps.h wasn't being included in Enum.lhs, and the old
    WORD_SIZE_IN_BITS test didn't fail when WORD_SIZE_IN_BITS isn't
    defined. Now we get an error instead.
Note: See TracTickets for help on using tickets.