RE: Profiling makes FFI Marshalling of Doubles and Int64s fail

I've built a small test case. When I compile with: ghc Main.hs test.c -o Main.exe ... the (correct) output from Main.exe is: 123.0 5678901234567890 And when I compile with: ghc -prof Main.hs test.c -o Main.exe ... the (incorrect) output from Main.exe is: 1.0 986516178 ------ Main.hs: {-# OPTIONS -ffi #-} module Main where import Foreign.C foreign import ccall "myDouble" myDouble :: IO CDouble foreign import ccall "myInt64" myInt64 :: IO CLLong main = do d <- myDouble putStrLn $ show d l <- myInt64 putStrLn $ show l ------ test.c: double myDouble() { double d = 123.0; return d; } long long myInt64() { long long x = 5678901234567890; return x; } ----------------------------------------- ***************************************************************** Confidentiality Note: The information contained in this message, and any attachments, may contain confidential and/or privileged material. It is intended solely for the person(s) or entity to which it is addressed. Any review, retransmission, dissemination, or taking of any action in reliance upon this information by persons or entities other than the intended recipient(s) is prohibited. If you received this in error, please contact the sender and delete the material from any computer. *****************************************************************

Bayley, Alistair wrote:
I've built a small test case. When I compile with: ghc Main.hs test.c -o Main.exe
... the (correct) output from Main.exe is: 123.0 5678901234567890
And when I compile with: ghc -prof Main.hs test.c -o Main.exe
... the (incorrect) output from Main.exe is: 1.0 986516178
I don't know if this helps, but I want to share it anyway. I made some tests and saw that compiling with -fvia-C also gives the (incorrect) 986516178, while -fasm yields the expected value. I am using Linux/x86 here, so YMMV. However, compiling with ghc -prof Main.hs test.c -#include test.h or s/-prof/-fvia-C/, works fine, provided test.h contains the correct function prototypes. ------ test.h long long myInt64(void); double myDouble(void); ------ AFAICS, it seems as the asm code generator knows about the Haskell types and therefore knows how to fetch the bits correctly, while the via-C code generator simply uses the C function calls (without prototypes), so functions return types are defaulted to int. Also, you can try adding -Wall and see if the C compiler warns about this (mine does). The GHC user's guide (section 8.2.2) is also insightful. It seems that using C headers is pretty much required, or, as the guide itself says, "You're crazy not to do it". ;-) Regards, Roberto Zunino.
participants (2)
-
Bayley, Alistair
-
Roberto Zunino