Skip to content

Inconsistent behavior of Data.Bits.shiftL with different optimization levels and -fllvm

Consider this program:

module Main (main) where

import Data.Bits
import Data.Word

main :: IO ()
main = print $ toInteger (shiftL 1 hm :: Word64)
            == toInteger (shiftL 1 hm :: Word64)

hm :: Int
hm = -1
{-# NOINLINE hm #-}

The result of this program depends greatly on what optimization levels are used, and whether -fllvm is used:

$ /opt/ghc/8.6.3/bin/ghc -fforce-recomp -O0 Bug.hs && ./Bug
[1 of 1] Compiling Main             ( Bug.hs, Bug.o )
Linking Bug ...
True

$ /opt/ghc/8.6.3/bin/ghc -fforce-recomp -O1 Bug.hs && ./Bug
[1 of 1] Compiling Main             ( Bug.hs, Bug.o )
Linking Bug ...
True

$ /opt/ghc/8.6.3/bin/ghc -fforce-recomp -O2 Bug.hs && ./Bug
[1 of 1] Compiling Main             ( Bug.hs, Bug.o )
Linking Bug ...
Bug: Bad shift length-1

$ /opt/ghc/8.6.3/bin/ghc -fforce-recomp -O0 -fllvm Bug.hs && ./Bug
[1 of 1] Compiling Main             ( Bug.hs, Bug.o )
Linking Bug ...
True

$ /opt/ghc/8.6.3/bin/ghc -fforce-recomp -O1 -fllvm Bug.hs && ./Bug
[1 of 1] Compiling Main             ( Bug.hs, Bug.o )
Linking Bug ...
False

$ /opt/ghc/8.6.3/bin/ghc -fforce-recomp -O2 -fllvm Bug.hs && ./Bug
[1 of 1] Compiling Main             ( Bug.hs, Bug.o )
Linking Bug ...
Bug: Bad shift length-1

This program manages to return all of True, False, and Bad shift length-1 depending on which flags are used!

Trac metadata
Trac field Value
Version 8.6.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information