Small class method not capturing original rhs in stable unfolding
I have a very curious situation with a patch of mine.
A recent patch of mine causes a few more functions to get W/Wed. Among other things integerToWord#
. This causes problems with RULES.
In particular we have rules which should cause (fromIntegral :: Int -> Word) to be a no-op at runtime.
The problem is we start out with `(fromIntegral @Int @Word GHC.Real.fIntegralInt GHC.Num.
fNumWord i_aMj)
We inline fromIntegral and get as a result (fromInteger . toInteger)
. fromInteger is resolved to the Int instance for Num.
Here then happens something that might be a bug. The Num instance is defined as
-- | @since 2.01
instance Num Word where
...
fromInteger i = W# (integerToWord# i)
And get's compiled to:
-- RHS size: {terms: 7, types: 2, coercions: 0, joins: 0/0}
GHC.Num.$fNumWord_$cfromInteger :: Integer -> Word
[GblId,
Arity=1,
Str=<1L>,
Cpr=1,
Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=1,unsat_ok=True,boring_ok=False)
Tmpl= \ (i_asf [Occ=Once1] :: Integer) ->
src<libraries/base/GHC/Num.hs:122:5-50>
case GHC.Num.Integer.$wintegerToWord# i_asf of wild_X1 [Occ=Once1]
{ __DEFAULT ->
GHC.Types.W# wild_X1
}}]
GHC.Num.$fNumWord_$cfromInteger
= \ (i_asf :: Integer) ->
src<libraries/base/GHC/Num.hs:122:5-50>
case GHC.Num.Integer.$wintegerToWord# i_asf of wild_X1
{ __DEFAULT ->
GHC.Types.W# wild_X1
}
We get a stable unfolding that has the wrapper inlined. Very odd. Stable unfoldings are supposed to capture the original RHS after all.
From there on out we already lost. There naturally are no rules for $wintegerToWord#
so the int/word conversions turns into an expensive allocating operation. Quite horrible.
I'm not 100% sure what to do from here. I could reduce the zeal of WW for the module in which wintegerToWord# is defined. Then it wouldn't get a worker and things would be as they were essentially.
We could add INLINEABLE pragmas to the class methods. Seems reasonable but it seems like a bug that we have to. These methods should already be treated as if they had them given their size.