grin-compiler/grin
{ "createdAt": "2017-06-23T20:18:06Z", "defaultBranch": "master", "description": "GRIN is a compiler back-end for lazy and strict functional languages with whole program optimization support.", "fullName": "grin-compiler/grin", "homepage": "https://grin-compiler.github.io/", "language": "Haskell", "name": "grin", "pushedAt": "2025-06-13T11:21:35Z", "stargazersCount": 1054, "topics": [ "compiler", "data-flow-analysis", "functional-programming", "haskell", "llvm", "optimisation" ], "updatedAt": "2025-11-19T19:31:18Z", "url": "https://github.com/grin-compiler/grin"}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 The GRIN Project paper. To grasp the details take your time and read Urban Boquist’s PhD thesis on Code Optimisation Techniques for Lazy Functional Languages .
Presentations
- We presented the core ideas of GRIN at Haskell Exchange 2018. slides video
- The Next-gen Haskell Compilation Techniques (2021) presentation gives a high-level overview of the project motivation. slides video
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
Section titled “Support”The project is supported by these awesome backers. Special thanks to our gold sponsors:
Sam Griffin
Section titled “Sam Griffin”If you’d like to join them, please consider become a backer or sponsor on Patreon.
GRIN IR
Section titled “GRIN IR”
Showcase
Section titled “Showcase”
Installing LLVM
Section titled “Installing LLVM”Homebrew
Section titled “Homebrew”Example using Homebrew on macOS:
$ brew install llvm-hs/llvm/llvm-7Debian/Ubuntu
Section titled “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-devTo get a nix shell with all the required dependencies do the following in the top level folder.
nix-shellTo 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
Section titled “Build GRIN”stack setupstack buildstack exec -- grin grin/grin/opt-stages-high-level/stage-00.grinHow to Contribute
Section titled “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
Section titled “Example Front-End”Read about how to generate GRIN code from a frontend language.
Also check the corresponding source code.
i.e.
- Lambda/Syntax.hs - front-end language defintion
- lambda-grin/test - directory including generated Lambda and GRIN code
Simplifying Transformations
Section titled “Simplifying Transformations”| Transformation | Schema |
|---|---|
| [vectorisation][113] source code: [Vectorisation2.hs] | [ |
| [case simplification][116] source code: [CaseSimplification.hs] | [ |
| [split fetch operation][118] source code: [SplitFetch.hs] | [ |
| [right hoist fetch operation][123] source code: [RightHoistFetch2.hs] | [ |
| [register introduction][126] source code: [RegisterIntroduction.hs] | [ |
[113] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=113 [116] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=116 [118] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=118 [123] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=123 [126] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=126
[Vectorisation2.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Simplifying/Vectorisation2.hs [CaseSimplification.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Simplifying/CaseSimplification.hs [SplitFetch.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Simplifying/SplitFetch.hs [RightHoistFetch2.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Simplifying/RightHoistFetch2.hs [RegisterIntroduction.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Simplifying/RegisterIntroduction.hs
Optimising Transformations
Section titled “Optimising Transformations”| Transformation | Schema |
|---|---|
| [evaluated case elimination][141] source code: [EvaluatedCaseElimination.hs] test: [EvaluatedCaseEliminationSpec.hs] | [ |
| [trivial case elimination][142] source code: [TrivialCaseElimination.hs] test: [TrivialCaseEliminationSpec.hs] | [ |
| [sparse case optimisation][143] source code: [SparseCaseOptimisation.hs] test: [SparseCaseOptimisationSpec.hs] | [ |
| [update elimination][148] source code: [UpdateElimination.hs] test: [UpdateEliminationSpec.hs] | [ |
| [copy propagation][129] source code: [CopyPropagation.hs] test: [CopyPropagationSpec.hs] | [ |
| [late inlining][151] source code: [Inlining.hs] test: [InliningSpec.hs] | [ |
| [generalised unboxing][134] source code: [GeneralizedUnboxing.hs] test: [GeneralizedUnboxingSpec.hs] | [ |
| [arity raising][160] source code: [ArityRaising.hs] test: [ArityRaisingSpec.hs] | [ |
| [case copy propagation][144] source code: [CaseCopyPropagation.hs] test: [CaseCopyPropagationSpec.hs] | [ |
| [case hoisting][153] source code: [CaseHoisting.hs] test: [CaseHoistingSpec.hs] | [ |
| [whnf update elimination][149] source code: TODO test: TODO | [ |
| [common sub-expression elimination][164] source code: [CSE.hs] test: [CSESpec.hs] | [ |
| [constant propagation][159] source code: [ConstantPropagation.hs] test: [ConstantPropagationSpec.hs] | |
| [dead function elimination][169] source code: [SimpleDeadFunctionElimination.hs] test: [SimpleDeadFunctionEliminationSpec.hs] | |
| [dead variable elimination][170] source code: [SimpleDeadVariableElimination.hs] test: [SimpleDeadVariableEliminationSpec.hs] | |
| [dead parameter elimination][171] source code: [SimpleDeadParameterElimination.hs] test: [SimpleDeadParameterEliminationSpec.hs] |
[129] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=129 [134] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=134 [141] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=141 [142] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=142 [143] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=143 [144] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=144 [148] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=148 [149] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=149 [151] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=151 [153] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=153 [159] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=159 [160] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=160 [164] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=164 [169] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=169 [170] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=170 [171] !: http://nbviewer.jupyter.org/github/grin-compiler/grin/blob/master/papers/boquist.pdf#page=171
[ArityRaising.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/ArityRaising.hs [ConstantPropagation.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/ConstantPropagation.hs [CopyPropagation.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/CopyPropagation.hs [CaseCopyPropagation.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/CaseCopyPropagation.hs [CaseHoisting.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/CaseHoisting.hs [CSE.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/CSE.hs [EvaluatedCaseElimination.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/EvaluatedCaseElimination.hs [Inlining.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/Inlining.hs [SparseCaseOptimisation.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/SparseCaseOptimisation.hs [TrivialCaseElimination.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/TrivialCaseElimination.hs [UpdateElimination.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/UpdateElimination.hs [GeneralizedUnboxing.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/GeneralizedUnboxing.hs [SimpleDeadFunctionElimination.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/SimpleDeadFunctionElimination.hs [SimpleDeadVariableElimination.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/SimpleDeadVariableElimination.hs [SimpleDeadParameterElimination.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/src/Transformations/Optimising/SimpleDeadParameterElimination.hs
[ArityRaisingSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/ArityRaisingSpec.hs [ConstantPropagationSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/ConstantPropagationSpec.hs [CopyPropagationSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/CopyPropagationSpec.hs [CaseCopyPropagationSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/CaseCopyPropagationSpec.hs [CaseHoistingSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/CaseHoistingSpec.hs [CSESpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/CSESpec.hs [EvaluatedCaseEliminationSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/EvaluatedCaseEliminationSpec.hs [InliningSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/InliningSpec.hs [SparseCaseOptimisationSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/SparseCaseOptimisationSpec.hs [TrivialCaseEliminationSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/TrivialCaseEliminationSpec.hs [UpdateEliminationSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/UpdateEliminationSpec.hs [GeneralizedUnboxingSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/GeneralizedUnboxingSpec.hs [SimpleDeadFunctionEliminationSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/SimpleDeadFunctionEliminationSpec.hs [SimpleDeadVariableEliminationSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/SimpleDeadVariableEliminationSpec.hs [SimpleDeadParameterEliminationSpec.hs] !: https://github.com/grin-compiler/grin/blob/master/grin/test/Transformations/Optimising/SimpleDeadParameterEliminationSpec.hs
