Skip to content

GHC 9.10.1 i386: incorrect arithmetic optimization

Summary

Consider the snippet

import Data.Word

main = print $ c == c + f 0
  where
    c = 0x8000000000000000

f :: Word64 -> Word64
f x = if x > 0 then f x else x

As not (0 > 0), f 0 should evaluate to 0, so c == c + f 0, so the program should print True. However, with GHC 9.10.1 on i386 and -O1, it prints False.

Steps to reproduce

 $ ghc --info | rg 'Project version|target platform string'
 ,("target platform string","i386-unknown-linux")
 ,("Project version","9.10.1")
 $ ghc -O1 bug.hs
 $ ./bug
False

Expected behavior

It should print True, just as on all previous GHC versions, as well as on other platforms like x86_64-linux, and also with -O0.

Analysis

This looks like a code generation bug; the final STG looks fine, and at least the WASM backend (also 32bit) does not exhibit this bug.

Bisecting (needed to apply 843f95b1 to compile most commits between 9.8 and 9.10) yields that 6755d833 (!10771 (merged)) introduced this bug, cc @jaro @AndreasK.

Environment

  • GHC version used: 9.10.1 (installed via ghcup)
  • Operating System/System Architecture: i386/debian:buster docker container on x86_64-linux
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information