Remove ad-hoc fromIntegral rules (#19907, #20062)

Closed Sylvain Henry requested to merge hsyl20/ghc:hsyl20/perf-ctypes into master
Remove ad-hoc fromIntegral rules

fromIntegral is defined as:

  {-# NOINLINE [1] fromIntegral #-}
  fromIntegral :: (Integral a, Num b) => a -> b
  fromIntegral = fromInteger . toInteger

Before this patch, we had a lot of rewrite rules for fromIntegral, to
avoid passing through Integer when there is a faster way, e.g.:

  "fromIntegral/Int->Word"  fromIntegral = \(I# x#) -> W# (int2Word# x#)
  "fromIntegral/Word->Int"  fromIntegral = \(W# x#) -> I# (word2Int# x#)
  "fromIntegral/Word->Word" fromIntegral = id :: Word -> Word

Since we have added sized types and primops (Word8#, Int16#, etc.) and
Natural, this approach didn't really scale as there is a combinatorial
explosion of types. In addition, we really want these conversions to be
optimized for all these types and in every case (not only when
fromIntegral is explicitly used).

This patch removes all those ad-hoc fromIntegral rules. Instead we rely
on inlining and built-in constant-folding rules. There are not too many
native conversions between Integer/Natural and fixed size types, so we
can handle them all explicitly.

Foreign.C.Types was using rules to ensure that fromIntegral rules "sees"
through the newtype wrappers,e.g.:

  {-# RULES
  "fromIntegral/a->CSize"      fromIntegral = \x -> CSize      (fromIntegral x)
  "fromIntegral/CSize->a"      fromIntegral = \(CSize      x) -> fromIntegral x

But they aren't necessary because coercions due to newtype deriving are
pushed out of the way. So this patch removes these rules (as
fromIntegral is now inlined, they won't match anymore anyway).


* INLINE `fromIntegral`
* Add some missing constant-folding rules
* Remove every fromIntegral ad-hoc rules (fix #19907)

Fix #20062 (missing fromIntegral rules for sized primitives)
Edited by Sylvain Henry

Merge request reports