GHC -O2 and unsafePerformIO

Neil Mitchell ndmitchell at gmail.com
Wed May 2 11:00:05 EDT 2007


Hi

I have a program (below) which when compiled with -O2 gives the result:

H:\work\supero\charcount>type log.txt | diff.exe
i am here
109
done
The process tried to write to a nonexistent pipe.

And when compiled with -O0 gives:

H:\work\supero\charcount>type log.txt | diff
i am here
109
i am here
111
done
The process tried to write to a nonexistent pipe.

It tries to read two characters, so I really do want two characters to
appear. I have tried NOINLINE, but with no effect. Any suggestions?

Thanks

Neil

---------------
-- The program



import System.IO.Unsafe
import System.IO
import Data.Word
import Debug.Trace

main = p_System_IO_hGetChar 1 `seq` p_System_IO_hGetChar 2 `seq` putStrLn "done"

{-# NOINLINE wrapIO #-}
wrapIO x = unsafePerformIO (x >>= return)

foreign import ccall "stdio.h getchar" getchar :: IO Word8

{-# NOINLINE p_System_IO_hGetChar #-}
p_System_IO_hGetChar h   = trace "i am here" $
    wrapIO (getchar >>= \c -> print c >> return (if c == (-1) then 0
else chr_ c))


chr_ = fromEnum


More information about the Glasgow-haskell-users mailing list