-O2 bug in GHC 6.8.1?

I upgraded from GHC 6.6.1 to 6.8.1 and around that time I noticed that the output from an app I am working on changed. I have distilled the code down to the following example that produces different output depending on whether it is compiled with -O2 or not: main = do let (T x) = read "T 3" print $ f x print $ g x data T = T Int deriving (Read, Show) f x = truncate $ 2000 / fromIntegral ((x * 25) + 10) g :: Int -> Int g x = f x Here is the transcript from a shell (example code is saved in test.hs): $ ghc --make test.hs [1 of 1] Compiling Main ( test.hs, test.o ) Linking test ... $ ./test 23 23 $ rm test.hi test.o test $ ghc -O2 --make test.hs [1 of 1] Compiling Main ( test.hs, test.o ) Linking test ... $ ./test 23 24 It is a little disconcerting that -O2 changes the output. :-) Regards brad -- www.scoodi.com Contribute things you don't need and find things you do, all within your local community.

good bug! -O or -O2 is irrelevant but it works if compiled with -fvia-C You (or someone else) should add it to http://hackage.haskell.org/trac/ghc Christian Brad Clow wrote:
I upgraded from GHC 6.6.1 to 6.8.1 and around that time I noticed that the output from an app I am working on changed. I have distilled the code down to the following example that produces different output depending on whether it is compiled with -O2 or not:
main = do let (T x) = read "T 3" print $ f x print $ g x
data T = T Int deriving (Read, Show)
f x = truncate $ 2000 / fromIntegral ((x * 25) + 10)
g :: Int -> Int g x = f x
Here is the transcript from a shell (example code is saved in test.hs):
$ ghc --make test.hs [1 of 1] Compiling Main ( test.hs, test.o ) Linking test ... $ ./test 23 23 $ rm test.hi test.o test $ ghc -O2 --make test.hs [1 of 1] Compiling Main ( test.hs, test.o ) Linking test ... $ ./test 23 24
It is a little disconcerting that -O2 changes the output. :-)
Regards brad

Christian Maeder wrote:
good bug! -O or -O2 is irrelevant but it works if compiled with -fvia-C
You (or someone else) should add it to http://hackage.haskell.org/trac/ghc
I guess that this is related to http://thread.gmane.org/gmane.comp.lang.haskell.cafe/31675 Regards, apfelmus

Hi Brad, On Tue, Nov 20, 2007 at 09:50:02PM +1000, Brad Clow wrote:
$ ./test 23 24
I can't reproduce this. Can you please tell us what platform you are on (e.g. x86_64 Linux) and what gcc --version says? Also, where did your GHC come from, e.g. bindists from the download page, self-compiled? Also, as Christian says, it's best to file a bug report so that the problem doesn't get forgotten about. Thanks Ian

On 11/20/07, Ian Lynagh
Hi Brad,
I can't reproduce this. Can you please tell us what platform you are on (e.g. x86_64 Linux) and what gcc --version says?
Also, where did your GHC come from, e.g. bindists from the download page, self-compiled?
Also, as Christian says, it's best to file a bug report so that the problem doesn't get forgotten about.
Thanks Ian
Hi Ian, For info I can reproduce Brad's bug using ghc-6.8.1 (distributed binaries) on Windows XP. C:\Temp>ghc --version The Glorious Glasgow Haskell Compilation System, version 6.8.1 C:\Temp>bugo2.exe 23 24 Cheers, Olivier.

On Nov 20, 2007 4:32 PM, Ian Lynagh
Hi Brad,
On Tue, Nov 20, 2007 at 09:50:02PM +1000, Brad Clow wrote:
$ ./test 23 24
I can't reproduce this. Can you please tell us what platform you are on (e.g. x86_64 Linux) and what gcc --version says?
Also, where did your GHC come from, e.g. bindists from the download page, self-compiled?
I can also reproduce this on an Ubuntu machine with a self-compiled ghc. gcc is 4.1.2. Cheers, Josef

Ian Lynagh wrote:
Hi Brad,
On Tue, Nov 20, 2007 at 09:50:02PM +1000, Brad Clow wrote:
$ ./test 23 24
I can't reproduce this. Can you please tell us what platform you are on (e.g. x86_64 Linux) and what gcc --version says?
I see a bug that only affects x86_32. The native code generator (compiler/nativeGen/PprMach.hs) generates a simple fistp instruction for converting floats or doubles to int. with the usual default rounding mode (round) that means numbers between n+1/2 and n are rounded up instead of truncated. (relevant Code: pprInstr g@(GDTOI src dst) = pprG g (hcat [gtab, text "subl $4, %esp ; ", gpush src 0, gsemi, text "fistpl 0(%esp) ; popl ", pprReg I32 dst]) ) What's missing is the FPU control word manipulation to set the rounding mode appropriately, say pprinstr g@(GDTOI src dst) = pprG g (hcat [gtab, gpush src 0, text " ; subl $4, %esp ; fstcw 0(%esp) ; movl $0xC00, ", reg, text " ; orl 0(%esp), ", reg, text " ; pushl ", reg, text " ; fldcw 0(%esp) ; fistpl 0(%esp) ; popl ", reg, text " ; addl $4, %esp"]) where reg = pprReg I32 dst For reference, see http://www.eglibc.org/cgi-bin/viewcvs.cgi/fsf/glibc-2_3-branch/libc/sysdeps/i386/fpu/s_truncf.S?rev=10&sortby=date&view=markup HTH, Bertram

On Nov 21, 2007 1:32 AM, Ian Lynagh
I can't reproduce this. Can you please tell us what platform you are on (e.g. x86_64 Linux) and what gcc --version says?
Mac OS X 10.4.11 $ gcc --version i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
Also, where did your GHC come from, e.g. bindists from the download page, self-compiled?
From the GHC download page (http://haskell.org/ghc/dist/6.8.1/maeder/ghc-6.8.1-i386-apple-darwin.tar.bz2)
Also, as Christian says, it's best to file a bug report so that the problem doesn't get forgotten about.
No problem, thanks for the quick response. Regards brad -- www.scoodi.com Contribute things you don't need and find things you do, all within your local community.

Brad Clow wrote:
I upgraded from GHC 6.6.1 to 6.8.1 and around that time I noticed that the output from an app I am working on changed. I have distilled the code down to the following example that produces different output depending on whether it is compiled with -O2 or not: [...]
I've created a bug report for this, see http://hackage.haskell.org/trac/ghc/ticket/1910 regards, Bertram
participants (7)
-
apfelmus
-
Bertram Felgenhauer
-
Brad Clow
-
Christian Maeder
-
Ian Lynagh
-
Josef Svenningsson
-
Olivier Boudry