Draft: Use a more efficient printer for code generation
This is a work-in-progress implementation of the change described in #21853 (closed). Not everything is totally cleaned up yet, and there are a few things left to be implemented, but enough is in place to accurately represent the nature of the changes. Here is a high level summary of the patch:
-
GHC.Utils.Outputable.HDoc
provides a newHDoc
type, which is a stripped-down version ofSDoc
that writes directly to aBufHandle
. -
GHC.Utils.Outputable.Class
provides a shared interface to bothSDoc
andHDoc
, which allows code to be parameterized over the choice of output. -
The
Outputable
andOutputableP
classes now include an additional parameter that selects the output implementation.-
Most code that previously used
SDoc
remains specialized toSDoc
. The only required changes for this code are replacements ofOutputable
withOutputable SDoc
. -
Printing logic that is used by the code generator is changed to be polymorphic in the choice of output. Usually, this requires little to no changes other than making the types more generic and adding any necessary
SPECIALIZE
pragmas.
-
-
The NCG is updated to use
HDoc
instead ofSDoc
. (Currently, only the x86 printer is updated, but PPC and AArch64 will eventually be updated as well.)
I would appreciate any feedback on the general approach, particularly on the use of the IsLine
/IsDoc
classes and the reliance on specialization.
NB: This patch currently depends on a fix for #21851 (closed) to achieve maximal performance gains. Since no such fix has materialized, this MR includes a temporary workaround that just runs the simplifier again after specialization. This workaround will be removed before merging and should not be considered part of the patch.