Skip to content

Commit

Permalink
Merge 2a1053c into d43a9a4
Browse files Browse the repository at this point in the history
  • Loading branch information
andorp committed Jul 30, 2019
2 parents d43a9a4 + 2a1053c commit 9fa6a94
Show file tree
Hide file tree
Showing 28 changed files with 635 additions and 64 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ output/
.output
.grin-output/
.vscode/
*.out
*.out.ll
*.out.s
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ before_install:

install:
- stack update
- travis_wait 120 stack --no-terminal --install-ghc test --only-dependencies
- travis_wait 120 stack --no-terminal --install-ghc test --only-dependencies --coverage

script:
- mkdir .output
- stack --no-terminal test --coverage
- stack exec grin -- grin/grin/sum_simple.grin
- stack test grin:grin-end-to-end-test grin:grin-test --coverage

after_script:
- travis_retry curl -L https://github.com/rubik/stack-hpc-coveralls/releases/download/v0.0.4.0/shc-linux-x64-8.0.1.tar.bz2 | tar -xj
- ./shc grin grin-test
- ./shc --repo-token=8QkWaPJlRbej9X5N5AaXSommTtN7uEqRT combined all
- ./shc --repo-token=8QkWaPJlRbej9X5N5AaXSommTtN7uEqRT combined unified
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# GRIN

[![Build Status](https://travis-ci.org/grin-compiler/grin.svg?branch=master)](https://travis-ci.org/grin-compiler/grin) [![Coverage Status](https://coveralls.io/repos/github/grin-tech/grin/badge.svg?branch=master)](https://coveralls.io/github/grin-tech/grin?branch=master)
[![Build Status](https://travis-ci.org/grin-compiler/grin.svg?branch=master)](https://travis-ci.org/grin-compiler/grin) [![Coverage Status](https://coveralls.io/repos/github/grin-compiler/grin/badge.svg?branch=master)](https://coveralls.io/github/grin-compiler/grin?branch=master)
[![Gitter chat](https://badges.gitter.im/grin-compiler/grin.png)](https://gitter.im/Grin-Development/Lobby)

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](https://grin-compiler.github.io/).
To get the big picture of the project check the [project website](https://grin-compiler.github.io/).
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
Expand All @@ -21,7 +21,7 @@ Also check the GRIN transformation [example from Boquist PhD](http://nbviewer.ju

## Support

The project is supported by these awesome [backers](https://github.com/grin-compiler/grin/blob/master/BACKERS.md).
The project is supported by these awesome [backers](https://github.com/grin-compiler/grin/blob/master/BACKERS.md).
If you'd like to join them, please consider become a backer or sponsor on [Patreon](https://www.patreon.com/csaba_hruska).


Expand Down Expand Up @@ -96,7 +96,7 @@ stack build
stack exec -- grin grin/grin/opt-stages-high-level/stage-00.grin
```
## How to Contribute
See: [Issues / Tasks for new contributors](https://github.com/grin-compiler/grin/issues/3)
See: [Issues / Tasks for new contributors](https://github.com/grin-compiler/grin/issues/3)
Keep it simple: We follow the fundamentals laid down in [HaskellerZ - Feb 2018 - Getting things done in Haskell](https://www.youtube.com/watch?v=-X1vrxQUETM)

## Example Front-End
Expand Down Expand Up @@ -150,10 +150,10 @@ Transformation | Schema
[case hoisting][153] <br><br> _source code:_ <br> [CaseHoisting.hs] <br><br> _test:_ <br> [CaseHoistingSpec.hs] | [<img src="images/case-hoisting.png" width="500">][153]
[whnf update elimination][149] <br><br> _source code:_ <br> __TODO__ <br><br> _test:_ <br> __TODO__ | [<img src="images/whnf-update-elimination.png" width="500">][149]
[common sub-expression elimination][164] <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">][164]
[constant propagation][159] <br><br> _source code:_ <br> [ConstantPropagation.hs] <br><br> _test:_ <br> [ConstantPropagationSpec.hs] |
[dead function elimination][169] <br><br> _source code:_ <br> [SimpleDeadFunctionElimination.hs] <br><br> _test:_ <br> [SimpleDeadFunctionEliminationSpec.hs] |
[dead variable elimination][170] <br><br> _source code:_ <br> [SimpleDeadVariableElimination.hs] <br><br> _test:_ <br> [SimpleDeadVariableEliminationSpec.hs] |
[dead parameter elimination][171] <br><br> _source code:_ <br> [SimpleDeadParameterElimination.hs] <br><br> _test:_ <br> [SimpleDeadParameterEliminationSpec.hs] |
[constant propagation][159] <br><br> _source code:_ <br> [ConstantPropagation.hs] <br><br> _test:_ <br> [ConstantPropagationSpec.hs] |
[dead function elimination][169] <br><br> _source code:_ <br> [SimpleDeadFunctionElimination.hs] <br><br> _test:_ <br> [SimpleDeadFunctionEliminationSpec.hs] |
[dead variable elimination][170] <br><br> _source code:_ <br> [SimpleDeadVariableElimination.hs] <br><br> _test:_ <br> [SimpleDeadVariableEliminationSpec.hs] |
[dead parameter elimination][171] <br><br> _source code:_ <br> [SimpleDeadParameterElimination.hs] <br><br> _test:_ <br> [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
Expand Down
60 changes: 47 additions & 13 deletions grin/app/GrinCLI.hs → grin/app/CLI/Lib.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-# LANGUAGE LambdaCase #-}
module Main where
module CLI.Lib where

import Control.Monad
import Data.Map (Map(..))
Expand All @@ -21,6 +21,8 @@ import Grin.Parse hiding (value)
import Grin.Nametable as Nametable
import Pipeline.Pipeline



data Options = Options
{ optFiles :: [FilePath]
, optTrans :: [PipelineStep]
Expand All @@ -29,6 +31,8 @@ data Options = Options
, optQuiet :: Bool
, optLoadBinary :: Bool
, optSaveBinary :: Bool
, optRuntimeC :: FilePath
, optPrimOpsC :: FilePath
} deriving Show

flg c l h = flag' c (mconcat [long l, help h])
Expand Down Expand Up @@ -130,6 +134,7 @@ pipelineOpts =
<|> flg ConfluenceTest "confluence-test" "Checks transformation confluence by generating random two pipelines which reaches the fix points."
<|> flg PrintErrors "print-errors" "Prints the error log"


maybeRenderingOpt :: String -> Maybe RenderingOption
maybeRenderingOpt = M.parseMaybe renderingOpt

Expand All @@ -149,14 +154,18 @@ printGrinWithOpt = flip PrintGrin id <$> option (maybeReader maybeRenderingOpt)
<> help "Print the actual grin code with a given rendering option [simple | with-externals]"
<> metavar "OPT" )

options :: IO Options
options = execParser $ info
(pipelineArgs <**> helper)
(mconcat
[ fullDesc
, progDesc "grin compiler"
, header "grin compiler"
])
options :: [String] -> IO Options
options args = do
let res = execParserPure defaultPrefs
(info
(pipelineArgs <**> helper)
(mconcat
[ fullDesc
, progDesc "grin compiler"
, header "grin compiler"
]))
args
handleParseResult res
where
pipelineArgs = Options
<$> some (argument str (metavar "FILES..."))
Expand Down Expand Up @@ -184,11 +193,29 @@ options = execParser $ info
[ long "save-binary-intermed"
, help "Save intermediate results in binary format"
])
<*> strOption (mconcat
[ long "runtime-c-path"
, value "runtime.c"
, help "The path for the runtime implementation in C"])
<*> strOption (mconcat
[ long "primops-c-path"
, value "prim_ops.c"
, help "The path for the implementation of prim_ops in C"])

main :: IO ()
main = do
mainWithArgs :: [String] -> IO ()
mainWithArgs args = do
hSetBuffering stdout NoBuffering
Options files steps outputDir noPrelude quiet loadBinary saveBinary <- options
Options
files
steps
outputDir
noPrelude
quiet
loadBinary
saveBinary
runtimeC
primopsC
<- options args
forM_ files $ \fname -> do
(mTypeEnv, program) <- if loadBinary
then do
Expand All @@ -197,7 +224,14 @@ main = do
content <- Text.readFile fname
let (typeEnv, program') = either (error . M.parseErrorPretty' content) id $ parseGrinWithTypes fname content
pure $ (Just typeEnv, if noPrelude then program' else concatPrograms [primPrelude, program'])
let opts = defaultOpts { _poOutputDir = outputDir, _poFailOnLint = True, _poLogging = not quiet, _poSaveBinary = saveBinary }
let opts = defaultOpts
{ _poOutputDir = outputDir
, _poFailOnLint = True
, _poLogging = not quiet
, _poSaveBinary = saveBinary
, _poRuntimeC = runtimeC
, _poPrimOpsC = primopsC
}
case steps of
[] -> void $ optimize opts program [] postPipeline
_ -> void $ pipeline opts mTypeEnv program steps
Expand Down
10 changes: 10 additions & 0 deletions grin/app/CLI/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Main where

import System.Environment (getArgs)
import CLI.Lib (mainWithArgs)


main :: IO ()
main = do
args <- getArgs
mainWithArgs args
105 changes: 103 additions & 2 deletions grin/grin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ library

executable grin
hs-source-dirs: app
main-is: GrinCLI.hs
main-is: CLI/Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: base >=4.11
, grin
Expand All @@ -197,11 +197,13 @@ executable grin
, optparse-applicative
, directory
, binary
other-modules:
CLI.Lib
default-language: Haskell2010

test-suite grin-test
type: exitcode-stdio-1.0
hs-source-dirs: test
hs-source-dirs: test, app
main-is: Spec.hs
default-extensions: OverloadedStrings
build-depends: base >=4.11
Expand All @@ -223,7 +225,102 @@ test-suite grin-test
, ansi-wl-pprint
, directory
, inline-c
, directory-tree
, yaml
, binary
, optparse-applicative
, megaparsec
, system-posix-redirect
, process
, bytestring
, MissingH

other-modules:
Transformations.Simplifying.RegisterIntroductionSpec
Transformations.Simplifying.CaseSimplificationSpec
Transformations.Simplifying.SplitFetchSpec
Transformations.Simplifying.RightHoistFetchSpec
Transformations.Simplifying.VectorisationSpec
Transformations.Simplifying.ProducerNameIntroductionSpec
Transformations.Simplifying.BindingPatternSimplificationSpec
Transformations.Optimising.CaseCopyPropagationSpec
Transformations.Optimising.CopyPropagationSpec
Transformations.Optimising.ConstantFoldingSpec
Transformations.Optimising.ConstantPropagationSpec
Transformations.Optimising.EvaluatedCaseEliminationSpec
Transformations.Optimising.TrivialCaseEliminationSpec
Transformations.Optimising.SparseCaseOptimisationSpec
Transformations.Optimising.UpdateEliminationSpec
Transformations.Optimising.CSESpec
Transformations.Optimising.GeneralizedUnboxingSpec
Transformations.Optimising.ArityRaisingSpec
Transformations.Optimising.SimpleDeadFunctionEliminationSpec
Transformations.Optimising.SimpleDeadParameterEliminationSpec
Transformations.Optimising.SimpleDeadVariableEliminationSpec
Transformations.Optimising.InliningSpec
Transformations.Optimising.CaseHoistingSpec
Transformations.Optimising.DeadDataEliminationSpec
Transformations.Optimising.DeadFunctionEliminationSpec
Transformations.Optimising.DeadParameterEliminationSpec
Transformations.Optimising.DeadVariableEliminationSpec
Transformations.StaticSingleAssignmentSpec
Transformations.BindNormalisationSpec
Transformations.ConfluenceSpec
Transformations.MangleNamesSpec
Samples.SumListSpec
Samples.ArityFullRemoveSpec
LintSpec
TestSpec
PipelineSpec
ParserSpec
PrimOpsSpec
NametableSpec
AbstractInterpretation.HptSpec
AbstractInterpretation.LiveVariableSpec
AbstractInterpretation.EffectTrackingSpec
AbstractInterpretation.IRSpec
AbstractInterpretation.OptimiseAbstractProgramSpec
AbstractInterpretation.SharingSpec
AbstractInterpretation.CreatedBySpec
Test.Hspec.PipelineExample
Test.EndToEnd
Test.EndToEndSpec
CLI.Lib
default-language: Haskell2010

test-suite grin-end-to-end-test
type: exitcode-stdio-1.0
hs-source-dirs: test, app
main-is: EndToEnd.hs
default-extensions: OverloadedStrings
build-depends: base >=4.11
, containers
, filepath
, functor-infix
, grin
, hspec
, hspec-core
, hspec-discover
, QuickCheck
, deepseq
, vector
, text
, random
, microlens
, transformers
, mtl
, ansi-wl-pprint
, directory
, inline-c
, directory-tree
, yaml
, megaparsec
, binary
, optparse-applicative
, system-posix-redirect
, process
, bytestring
, MissingH
other-modules:
Transformations.Simplifying.RegisterIntroductionSpec
Transformations.Simplifying.CaseSimplificationSpec
Expand Down Expand Up @@ -272,8 +369,12 @@ test-suite grin-test
AbstractInterpretation.SharingSpec
AbstractInterpretation.CreatedBySpec
Test.Hspec.PipelineExample
Test.EndToEnd
Test.EndToEndSpec
CLI.Lib
default-language: Haskell2010


benchmark grin-benchmark
type: exitcode-stdio-1.0
hs-source-dirs: test
Expand Down
9 changes: 7 additions & 2 deletions grin/src/Pipeline/Pipeline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ data PipelineOpts = PipelineOpts
, _poLintOnChange :: Bool
, _poTypedLint :: Bool -- Run HPT before every lint
, _poSaveBinary :: Bool
, _poRuntimeC :: FilePath
, _poPrimOpsC :: FilePath
}

defaultOpts :: PipelineOpts
Expand All @@ -269,6 +271,8 @@ defaultOpts = PipelineOpts
, _poLintOnChange = True
, _poTypedLint = False
, _poSaveBinary = False
, _poRuntimeC = "runtime.c"
, _poPrimOpsC = "prim_ops.c"
}

type PipelineM a = ReaderT PipelineOpts (StateT PState IO) a
Expand Down Expand Up @@ -728,9 +732,10 @@ saveExecutable debugSymbols path = do
callCommand $ printf
("llc-7 -O3 -relocation-model=pic -filetype=obj %s.ll" ++ if debugSymbols then " -debugger-tune=gdb" else "")
grinOptCodeFile
cfg <- ask
callCommand $ printf
("clang-7 -O3 prim_ops.c runtime.c %s.o -s -o %s" ++ if debugSymbols then " -g" else "")
grinOptCodeFile fname
("clang-7 -O3 %s %s %s.o -s -o %s" ++ if debugSymbols then " -g" else "")
(_poPrimOpsC cfg) (_poRuntimeC cfg) grinOptCodeFile fname

debugTransformation :: (Exp -> Exp) -> PipelineM ()
debugTransformation t = do
Expand Down
2 changes: 1 addition & 1 deletion grin/src/Reducer/PrimOps.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ C.include "<math.h>"
C.include "<stdio.h>"

-- primitive functions
primLiteralPrint _ _ [RT_Lit (LInt64 a)] = liftIO (print a) >> pure RT_Unit
primLiteralPrint _ _ [RT_Lit (LInt64 a)] = liftIO (putStr $ show a) >> pure RT_Unit
primLiteralPrint _ _ [RT_Lit (LString a)] = liftIO (putStr (Text.unpack a)) >> pure RT_Unit
primLiteralPrint ctx ps x = error $ Prelude.unwords ["primLiteralPrint", ctx, "- invalid arguments:", show ps, " - ", show x]

Expand Down

0 comments on commit 9fa6a94

Please sign in to comment.