Skip to content

GHC doesn't optimise away primitive identity conversions

Here is a small example:

import Data.Char (ord)
import GHC.Base (unsafeChr)
import GHC.Word

foo :: Word8 -> Word8
foo = fromIntegral . ord . unsafeChr . fromIntegral

bar :: Word8 -> Word8
bar = baz . baz
  where
    baz = fromIntegral . (fromIntegral :: Word8 -> Word)

GHC produces the following code:

foo =
  \ (x_arR :: Word8) ->
    case x_arR of _ { W8# x#_avQ ->
    W8# (narrow8Word# (int2Word# (ord# (chr# (word2Int# x#_avQ)))))
    }

bar =
  \ (x_arR :: Word8) ->
    case x_arR of _ { W8# x#_avQ ->
    W8# (narrow8Word# (narrow8Word# x#_avQ))
    }

This could be optimised if GHC knew that:

  • ord# (chr# x) = x
  • int2Word# (word2Int# x) = x
  • narrow8Word# (narrow8Word# x) = narrow8Word# x

There are a lot of similar rules so I'm not sure if enumerating them all in base is the way to go.

I assume the backend(s) will optimise all of this away but this is quite a lot of unnecessary intermediate code. This is taken from vector-bytestring so it actually happens in the wild.

Trac metadata
Trac field Value
Version 7.7
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