Ticket #7286 (closed bug: fixed)

Opened 8 months ago

Last modified 7 months ago

GHC doesn't optimise away primitive identity conversions

Reported by: rl Owned by: igloo
Priority: high Milestone: 7.8.1
Component: Compiler Version: 7.7
Keywords: Cc: dterei
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Compile-time performance bug Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

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.

Change History

Changed 8 months ago by dterei

  • cc dterei added

Changed 8 months ago by simonpj

  • owner set to igloo
  • difficulty set to Unknown

Changed 8 months ago by simonpj

  • priority changed from normal to high
  • milestone set to 7.8.1

Changed 7 months ago by ian@…

commit e3d78899ce336e8637ae231550c04f0f68bca2aa

Author: Ian Lynagh <ian@well-typed.com>
Date:   Sun Oct 21 13:48:09 2012 +0100

    Add some more primop rules; fixes #7286
    
    As well as the rules mentioned in the ticket, I've also gone through
    and added some more rules that might be useful in other cases.

 compiler/prelude/PrelRules.lhs |   56 ++++++++++++++++++++++++++++++++--------
 1 files changed, 45 insertions(+), 11 deletions(-)

Changed 7 months ago by igloo

  • status changed from new to closed
  • resolution set to fixed

Thanks for the report; fixed.

Note: See TracTickets for help on using tickets.