Skip to content

WorkerWrapper: Wrapper takes apart constructor which is then reconstructed in the worker.

We have this function from GHC's Name module as our starting point:


data Name = Name {
                n_sort :: NameSort,     -- What sort of name it is
                n_occ  :: !OccName,     -- Its occurrence name
                n_uniq :: {-# UNPACK #-} !Unique,
                n_loc  :: !SrcSpan      -- Definition site
            }

{-# NOINLINE mkInternalName #-}
mkInternalName :: Unique -> OccName -> SrcSpan -> Name
mkInternalName uniq occ loc = Name { n_uniq = uniq
                                   , n_sort = Internal
                                   , n_occ = occ
                                   , n_loc = loc }

This results in the following worker/wrapper

-- RHS size: {terms: 14, types: 13, coercions: 0, joins: 0/0}
$wmkInternalName
  = \ ww_sl5G ww1_sl5K ww2_sl5L w_sl5D ->
      case w_sl5D of dt_XaRo { __DEFAULT ->
      (# Internal, OccName ww1_sl5K ww2_sl5L, ww_sl5G, dt_XaRo #)
      }

-- RHS size: {terms: 21, types: 21, coercions: 1, joins: 0/0}
mkInternalName
  = \ w_sl5B w1_sl5C w2_sl5D ->
      case w_sl5B `cast` <Co:1> of { I# ww1_sl5G ->
      case w1_sl5C of { OccName ww3_sl5K ww4_sl5L ->
      case $wmkInternalName ww1_sl5G ww3_sl5K ww4_sl5L w2_sl5D of
      { (# ww6_slkS, ww7_slkT, ww8_slkU, ww9_slkV #) ->
      Name ww6_slkS ww7_slkT ww8_slkU ww9_slkV
      }
      }
      }

This code is quite odd. It:

  • Evaluates w1_sl5C to an OccName.
  • Takes the OccName apart and passes it's fields to the worker.
  • The worker constructs a new OccName from the passed fields.
  • The constructed OccName (and other things) are returned.
  • The returned OccName is ultimately returned by the wrapper.

This causes the "old" OccName to become garbage and a new one to be allocated in the heap unless I missed something.

Edited by Andreas Klebinger
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information