Home

Awesome

GRIN

Build Status Coverage Status Gitter chat

The name GRIN is short for Graph Reduction Intermediate Notation, and it is an intermediate language for graph reduction. GRIN is the optimizer and code generator component of the GRIN Compiler project which includes language frontends for Haskell, Idris and Agda. To get the big picture of the project check the project website. For an overview of the optimizer read <a href="http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/The GRIN Project.pdf"> The GRIN Project </a> paper. To grasp the details take your time and read Urban Boquist's PhD thesis on <a href="http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf"> Code Optimisation Techniques for Lazy Functional Languages </a>.

Presentations

Read our paper A modern look at GRIN, an optimizing functional language back end (2019) to get an overview of GRIN related projects and other whole program compilers i.e. Boquist GRIN, UHC, JHC, LHC, HRC, MLton

Also check the GRIN transformation example from Boquist PhD and an example from our implementation.

Support

The project is supported by these awesome backers. Special thanks to our gold sponsors:

Sam Griffin

<a href="http://holmusk.com/"> <img width="30%" src="https://raw.githubusercontent.com/grin-compiler/grin-compiler.github.io/master/assets/holmusk/Holmusk_masterlogo_blue.svg" alt="Holmusk logo" /> </a>

If you'd like to join them, please consider become a backer or sponsor on Patreon.

<a href="https://www.patreon.com/csaba_hruska"> <img src="https://c5.patreon.com/external/logo/become_a_patron_button.png" width="150"/> </a>

GRIN IR

<a href="http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=41"> <img src="https://raw.githubusercontent.com/grin-compiler/grin/master/images/grin-syntax.png" width="500" > </a>

Showcase

Setup

Installing LLVM

Homebrew

Example using Homebrew on macOS:

$ brew install llvm-hs/llvm/llvm-7

Debian/Ubuntu

For Debian/Ubuntu based Linux distributions, the LLVM.org website provides binary distribution packages. Check apt.llvm.org for instructions for adding the correct package database for your OS version, and then:

$ apt-get install llvm-7-dev

Nix

To get a nix shell with all the required dependencies do the following in the top level folder.

nix-shell

To run the example do the following from the top level folder.

(cd grin; cabal run grin -- grin/opt-stages-high-level/stage-00.grin)

To run a local Hoogle server with Haskell documentation do the following.

hoogle server --port 8087 1>/dev/null 2>/dev/null&

Build GRIN

stack setup
stack build

Usage

stack exec -- grin grin/grin/opt-stages-high-level/stage-00.grin

How to Contribute

See: Issues / Tasks for new contributors Keep it simple: We follow the fundamentals laid down in HaskellerZ - Feb 2018 - Getting things done in Haskell

Example Front-End

Read about how to <a href="http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=64">generate GRIN code</a> from a frontend language.

Also check the corresponding source code.

i.e.

Simplifying Transformations

TransformationSchema
vectorisation <br><br> source code: <br> Vectorisation2.hs<img src="images/vectorisation.png" width="500">
case simplification <br><br> source code: <br> CaseSimplification.hs<img src="images/case-simplification.png" width="500">
split fetch operation <br><br> source code: <br> SplitFetch.hs<img src="images/split-fetch-operation.png" width="500">
right hoist fetch operation <br><br> source code: <br> RightHoistFetch2.hs<img src="images/right-hoist-fetch.png" width="500">
register introduction <br><br> source code: <br> RegisterIntroduction.hs<img src="images/register-introduction.png" width="500">

Optimising Transformations

TransformationSchema
evaluated case elimination <br><br> source code: <br> EvaluatedCaseElimination.hs <br><br> test: <br> EvaluatedCaseEliminationSpec.hs<img src="images/evaluated-case-elimination.png" width="500">
trivial case elimination <br><br> source code: <br> TrivialCaseElimination.hs <br><br> test: <br> TrivialCaseEliminationSpec.hs<img src="images/trivial-case-elimination.png" width="500">
sparse case optimisation <br><br> source code: <br> SparseCaseOptimisation.hs <br><br> test: <br> SparseCaseOptimisationSpec.hs<img src="images/sparse-case-optimisation.png" width="500">
update elimination <br><br> source code: <br> UpdateElimination.hs <br><br> test: <br> UpdateEliminationSpec.hs<img src="images/update-elimination.png" width="500">
copy propagation <br><br> source code: <br> CopyPropagation.hs <br><br> test: <br> CopyPropagationSpec.hs<img src="images/copy-propagation-left.png" width="500"><img src="images/copy-propagation-right.png" width="500">
late inlining <br><br> source code: <br> Inlining.hs <br><br> test: <br> InliningSpec.hs<img src="images/late-inlining.png" width="500">
generalised unboxing <br><br> source code: <br> GeneralizedUnboxing.hs <br><br> test: <br> GeneralizedUnboxingSpec.hs<img src="images/generalised-unboxing.png" width="500"><img src="images/unboxing-of-function-return-values.png" width="500">
arity raising <br><br> source code: <br> ArityRaising.hs <br><br> test: <br> ArityRaisingSpec.hs<img src="images/arity-raising.png" width="500">
case copy propagation <br><br> source code: <br> CaseCopyPropagation.hs <br><br> test: <br> CaseCopyPropagationSpec.hs<img src="images/case-copy-propagation.png" width="500">
case hoisting <br><br> source code: <br> CaseHoisting.hs <br><br> test: <br> CaseHoistingSpec.hs<img src="images/case-hoisting.png" width="500">
whnf update elimination <br><br> source code: <br> TODO <br><br> test: <br> TODO<img src="images/whnf-update-elimination.png" width="500">
common sub-expression elimination <br><br> source code: <br> CSE.hs <br><br> test: <br> CSESpec.hs<img src="images/common-sub-expression-elimination-1.png" width="500"><img src="images/common-sub-expression-elimination-2.png" width="500">
constant propagation <br><br> source code: <br> ConstantPropagation.hs <br><br> test: <br> ConstantPropagationSpec.hs
dead function elimination <br><br> source code: <br> SimpleDeadFunctionElimination.hs <br><br> test: <br> SimpleDeadFunctionEliminationSpec.hs
dead variable elimination <br><br> source code: <br> SimpleDeadVariableElimination.hs <br><br> test: <br> SimpleDeadVariableEliminationSpec.hs
dead parameter elimination <br><br> source code: <br> SimpleDeadParameterElimination.hs <br><br> test: <br> SimpleDeadParameterEliminationSpec.hs