Skip to content

ghc floats out constant despite -fno-cse

Consider this program using Data.Vector.unsafeThaw and -fno-cse:

{-# OPTIONS_GHC -fno-cse #-}

import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM

main :: IO ()
main = do
  foo 100000
  foo 100000

  let f = foo 100000 in f >> f

foo :: Int -> IO ()
foo n = do
  indexVector <- V.unsafeThaw $ V.generate n id

  x <- VM.read indexVector 5
  VM.write indexVector 5 (x * x)

  print x

-- In GHCI, we get:
--
-- > :set -fno-cse
--
-- > foo 100000
-- 5
-- > foo 100000
-- 5
--
-- > let f = foo 100000 in f >> f
-- 5
-- 25

I would expect that

> let f = foo 100000 in f >> f
5
5

Shouldn't -fno-cse fix this?

(Note: This also happens with ghc instead of ghci.)

Edited by Niklas Hambüchen
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information