Skip to content

Draft: Use a more efficient printer for code generation

Alexis King requested to merge lexi.lambda/ghc:codegen-without-sdoc into master

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 new HDoc type, which is a stripped-down version of SDoc that writes directly to a BufHandle.

  • GHC.Utils.Outputable.Class provides a shared interface to both SDoc and HDoc, which allows code to be parameterized over the choice of output.

  • The Outputable and OutputableP classes now include an additional parameter that selects the output implementation.

    • Most code that previously used SDoc remains specialized to SDoc. The only required changes for this code are replacements of Outputable with Outputable 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 of SDoc. (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.

Merge request reports