fusion-plugin

GHC plugin to make stream fusion more predictable.

https://github.com/composewell/fusion-plugin

Version on this page:0.2.6
LTS Haskell 22.39:0.2.7@rev:3
Stackage Nightly 2024-10-31:0.2.7@rev:3
Latest on Hackage:0.2.7@rev:3

See all snapshots fusion-plugin appears in

Apache-2.0 licensed by Pranay Sashank
Maintained by [email protected]
This version can be pinned in stack with:fusion-plugin-0.2.6@sha256:0d2f01b31cd40fd030e408f82da5755cfde662ad43dc3b988bf67303e77a6b01,2693

Module documentation for 0.2.6

fusion-plugin

Hackage

Motivation

The goal of stream fusion is to eliminate constructors of internal state used in a stream. For example, in case of streamly streams, the constructors of Step type, Yield, Skip and Stop would get eliminated by fusion. Similarly, constructors of any other intermediate state types get eliminated when stream fusion works correctly. See the papers in the reference section for more details on stream fusion.

Stream fusion depends on the GHC case-of-case transformations eliminating intermediate constructors. Case-of-case transformation in turn depends on inlining. During core-to-core transformations GHC may create several internal bindings (e.g. join points) which may not get inlined because their size is too big. Even though we know that after fusion the resulting code would be smaller and more efficient. The programmer cannot force inlining of these bindings as there is no way for the programmer to address these bindings at the source level because they are internal, generated during core-to-core transformations. As a result stream fusion fails unpredictably depending on whether GHC was able to inline the internal bindings or not.

See GHC ticket #17075 for more details.

Solution

This plugin provides the programmer with a way to annotate certain types using a Fuse pragma from the fusion-plugin-types package. The programmer would annotate the types that are to be eliminated by fusion. During the simplifier phase the plugin goes through the relevant bindings and if one of these types are found inside a binding then that binding is marked to be inlined irrespective of the size.

Using the plugin

This plugin was primarily motivated by streamly but it can be used in general.

To use this plugin, add this package to your build-depends and pass the following to your ghc-options: ghc-options: -O2 -fplugin=Fusion.Plugin

Plugin options

Note: dump-core does not work for GHC-9.0.x.

-fplugin-opt=Fusion.Plugin:dump-core: dump core after each core-to-core transformation. Output from each transformation is printed in a different file.

-fplugin-opt=Fusion.Plugin:verbose=1: report unfused functions. Verbosity levels 2, 3, 4 can be used for more verbose output.

See also

If you are a library author looking to annotate the types, you need to use the fusion-plugin-types package.

Contributing

All contributions are welcome! The code is available under Apache-2.0 license on github. In case you have any questions or suggestions please contact the maintainers.

We would be happy to see this work getting integrated with GHC as a fix for GHC ticket #17075, any help with that would be appreciated.

References

Changes

0.2.6

  • Force inline on joins that consume fusible constructors

0.2.5

  • Support ghc-9.4.2

0.2.4

  • Suport ghc-9.2.2
  • dump-core CLI flag support for ghc-9.2.x

0.2.3

  • Enhancement: Support ghc-9.0.1
  • Note: dump-core command line flag does not work for ghc-9.0.1

0.2.2

Enhancements

  • Plugin now forces inline on binders that return a type annotated with Fuse.
  • Fusion.Plugin now accepts new command line arguments to control the verbosity of the plugin’s reporting phase. Refer to README.md for usage.

0.2.1

Enhancements

  • Fusion.Plugin now accepts a command line argument dump-core to dump the core from each core-to-core pass in a separate file.

0.2.0

Breaking Change

  • Fusion.Plugin.Types is no longer exported from this package. If you want to annotate the types in your library, use the package fusion-plugin-types.

0.1.1

Bug Fixes

  • Now compiles successfully on old GHC versions till 7.10.3.

0.1.0

  • Initial release