Unpacking Enum generates lots of branches
It can be advantageous to unpack enumeration types. For example `data D = D !Bool !Bool !Bool !Bool !Bool !Bool !Bool !Bool` takes 2 words (including header) on 64 bit when the `Bool` fields are unpacked, while without unpacking it would take up 9 words!
Unfortunately the code we generate for unpacking is neither efficient nor compact. This is due to the unpacking being represented as an unboxed sum of unboxed units.
For example
```haskell
module EnumBranch where
data Enum3 = E3A | E3B | E3C
data Enum7 = E7A | E7B | E7C | E7D | E7E | E7F | E7G
data E3U = E3U {-# UNPACK #-} !Enum3
data E7U = E7U {-# UNPACK #-} !Enum7
```
Gives us some STG like this:
```
EnumBranch.$WE7U [InlPrag=INLINE[final] CONLIKE]
:: EnumBranch.Enum7 %1 -> EnumBranch.E7U
[GblId[DataConWrapper],
Arity=1,
Caf=NoCafRefs,
Str=<SL>,
Unf=OtherCon []] =
{} \r [conrep_sAK]
case
case conrep_sAK of conrep_sAL {
EnumBranch.E7A -> GHC.Internal.Types.MkSolo# [1#];
EnumBranch.E7B -> GHC.Internal.Types.MkSolo# [2#];
EnumBranch.E7C -> GHC.Internal.Types.MkSolo# [3#];
EnumBranch.E7D -> GHC.Internal.Types.MkSolo# [4#];
EnumBranch.E7E -> GHC.Internal.Types.MkSolo# [5#];
EnumBranch.E7F -> GHC.Internal.Types.MkSolo# [6#];
EnumBranch.E7G -> GHC.Internal.Types.MkSolo# [7#];
}
of
unbx_sAM
{
GHC.Internal.Types.MkSolo# us_gAY -> EnumBranch.E7U [us_gAY];
};
EnumBranch.E7U [InlPrag=CONLIKE]
:: (# (# #) | (# #) | (# #) | (# #) | (# #) | (# #) | (# #) #)
%1 -> EnumBranch.E7U
[GblId[DataCon],
Arity=1,
Caf=NoCafRefs,
Str=<SL>,
Unf=OtherCon []] =
{} \r [us_gAZ] EnumBranch.E7U [us_gAZ];
```
I think enumerations are common enough to make a special case. We already have the primops for converting between an enum and a word (`DataToTagOp` and `TagToEnumOp`), so we can use them to directly unpack to `Word8#`, `Word16#`, `Word#`, depending on enum size.
I've implemented this in !15583
issue