Skip to content
Snippets Groups Projects
Commit 74863638 authored by Sylvain Henry's avatar Sylvain Henry Committed by Marge Bot
Browse files

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).

Summary:

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

Fix #20062 (missing fromIntegral rules for sized primitives)

Performance:
  - T12545 wiggles (tracked by #19414)

Metric Decrease:
    T12545
    T10359

Metric Increase:
    T12545
parent b128a880
1 merge request!7054Disable warnings for unused goto labels
Pipeline #39405 canceled
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment