GithubHelp home page GithubHelp logo

derive-storable-plugin's Introduction

Introduction

Hackage Build Status

The goal of derive-storable-plugin is to support the derive-storable package. It introduces optimisations to GStorable methods derived using GHC.Generics at core-to-core passes.

Usage

Just add a -fplugin=Foreign.Storable.Generic.Plugin flag and you're set. You might also want to pass a verbosity flag -vX, where X is either 0,1 or 2. By default the verbosity flag is set to -v1.

{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
{-# OPTIONS_GHC -fplugin=Foreign.Storable.Generic.Plugin #-}
{-# OPTIONS_GHC -fplugin-opt=Foreign.Storable.Generic.Plugin:-v1 #-} 
module Main where

import GHC.Generics

import Foreign.Storable.Generic
import Foreign.Ptr
import Foreign.Marshal.Alloc

data Point = Point {
    x :: Float,
    y :: Float
    } deriving (Show, Read, Generic, GStorable)

main = do
    let val = Point 0.0 10.0
    ptr <- malloc :: IO (Ptr Point)
    putStrLn "Created a ptr with value of"
    print =<< peek ptr
    poke ptr val
    putStrLn "And now the value of ptr is:"
    print =<< peek ptr

Benchmarks

The plugin was benchmarked using derive-storable-benchmark package. The benchark measures handwritten Storable instances, raw GStorable instances and optimised-by-plugin GStorable instances. The code was compiled with -O1 optimisation flag. Benchmarks

derive-storable-plugin's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

derive-storable-plugin's Issues

Support GHC-9.6

Hi, trying to build this library with the latest GHC produces the following errors:

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:32:34: error:
    Module ‘GHC.Core.Opt.Monad’ does not export ‘CoreToDo(..)’
   |
32 | import GHC.Core.Opt.Monad (CoreM,CoreToDo(..))
   |                                  ^^^^^^^^^^^^
src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:72:37: error:
    Module ‘GHC.Core.TyCo.Rep’ does not export ‘TyBinder(..)’
   |
72 | import GHC.Core.TyCo.Rep (Type(..), TyBinder(..), TyCoBinder(..),scaledThing)
   |                                     ^^^^^^^^^^^^
src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:72:51: error:
    Module ‘GHC.Core.TyCo.Rep’ does not export ‘TyCoBinder(..)’
   |
72 | import GHC.Core.TyCo.Rep (Type(..), TyBinder(..), TyCoBinder(..),scaledThing)
   | 

Doesn't build with newer GHC (8.8.2)

As of at least GHC 8.6.5 (perhaps earlier), it seems TyVarBndr(..) is no longer a part of Var but is perhaps somewhere else instead:

derive-storable-plugin> /tmp/stack23475/derive-storable-plugin-0.2.2.0/src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:21:13: error:
derive-storable-plugin>     Module Var does not export TyVarBndr(..)
derive-storable-plugin>    |
derive-storable-plugin> 21 | import Var (TyVarBndr(..), TyVarBinder)
derive-storable-plugin>    |             ^^^^^^^^^^^^^
derive-storable-plugin> 

Is there any way this could be easily fixed without breaking anything else, or some workaround I could do to have it work as-is?

Error when compiling with GHC 9.2.2

I'm seeing the following error compiling with GHC 9.2.2:

src/Foreign/Storable/Generic/Plugin/Internal/Compile.hs:185:45: error:
    • Couldn't match type: ghci-9.2.2:GHCi.RemoteTypes.ForeignRef
                             ghci-9.2.2:GHCi.RemoteTypes.HValue
                     with: ForeignRef a0
      Expected: ForeignRef a0
        Actual: ghci-9.2.2:GHCi.RemoteTypes.ForeignHValue
      NB: ‘ForeignRef’
            is defined in ‘GHCi.RemoteTypes’ in package ‘ghci-9.2.2’
          ‘ghci-9.2.2:GHCi.RemoteTypes.ForeignRef’
            is defined in ‘GHCi.RemoteTypes’ in package ‘ghci-9.2.2’
    • In the first argument of ‘withForeignRef’, namely ‘foreign_hval’
      In the second argument of ‘($)’, namely
        ‘withForeignRef foreign_hval localRef’
      In a stmt of a 'do' block:
        hval <- liftIO $ withForeignRef foreign_hval localRef
    |
185 |     hval         <- liftIO $ withForeignRef foreign_hval localRef

peek is inefficient for GHC 8.2 and GHC 8.4

benchmarking peek/Handwritten/C5
time                 36.03 ns   (35.70 ns .. 36.44 ns)
                     0.999 R²   (0.998 R² .. 0.999 R²)
mean                 37.08 ns   (36.64 ns .. 37.60 ns)
std dev              1.636 ns   (1.347 ns .. 2.343 ns)
variance introduced by outliers: 67% (severely inflated)

benchmarking peek/GStorable/C5
time                 264.9 ns   (263.7 ns .. 266.2 ns)
                     1.000 R²   (1.000 R² .. 1.000 R²)
mean                 266.7 ns   (265.2 ns .. 268.5 ns)
std dev              5.675 ns   (4.833 ns .. 6.953 ns)
variance introduced by outliers: 28% (moderately inflated)

benchmarking peek/Optimized/C5
time                 293.6 ns   (291.9 ns .. 295.4 ns)
                     1.000 R²   (1.000 R² .. 1.000 R²)
mean                 293.0 ns   (291.8 ns .. 294.3 ns)
std dev              3.871 ns   (3.055 ns .. 5.081 ns)
variance introduced by outliers: 13% (moderately inflated)

Might be a problem for other versions too (GHC 8.4.4 confirmed). GHC 8.6.2 is okay.

Doesn't compile with GHC 9.0.1

I guess some GHC internal things have moved around

Building library for derive-storable-plugin-0.2.3.3..
[1 of 8] Compiling Foreign.Storable.Generic.Plugin.Internal.Helpers ( src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs, dist/build/Foreign/Storable/Generic/Plugin/Internal/Helpers.o, dist/build/Foreign/Storable/Generic/Plugin/Internal/Helpers.dyn_o )

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:16:1: error:
    Could not find module ‘CoreSyn’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
16 | import CoreSyn (Bind(..),Expr(..), CoreExpr, CoreBind, CoreProgram, Alt)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:17:1: error:
    Could not find module ‘Literal’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
17 | import Literal (Literal(..))
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:18:1: error:
    Could not find module ‘Id’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
18 | import Id  (isLocalId, isGlobalId,Id)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:19:1: error:
    Could not find module ‘Var’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
19 | import Var (Var(..))
   | ^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:21:1: error:
    Could not find module ‘Var’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
21 | import Var (TyVarBinder(..), VarBndr(..))
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:25:1: error:
    Could not find module ‘Name’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
25 | import Name (getOccName,mkOccName)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:26:1: error:
    Could not find module ‘OccName’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
26 | import OccName (OccName(..), occNameString)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:27:1: error:
    Could not find module ‘Name’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
27 | import qualified Name as N (varName)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:28:1: error:
    Could not find module ‘SrcLoc’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
28 | import SrcLoc (noSrcSpan)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:29:1: error:
    Could not find module ‘Unique’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
29 | import Unique (getUnique)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:31:1: error:
    Could not find module ‘HscMain’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
31 | import HscMain (hscCompileCoreExpr)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:32:1: error:
    Could not find module ‘HscTypes’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
32 | import HscTypes (HscEnv,ModGuts(..))
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:33:1: error:
    Could not find module ‘CoreMonad’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
33 | import CoreMonad (CoreM,CoreToDo(..), getHscEnv)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:34:1: error:
    Could not find module ‘BasicTypes’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
34 | import BasicTypes (CompilerPhase(..))
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:36:1: error:
    Could not find module ‘Type’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
36 | import Type (isAlgType, splitTyConApp_maybe)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:37:1: error:
    Could not find module ‘TyCon’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
37 | import TyCon (algTyConRhs, visibleDataCons)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:39:1: error:
    Could not find module ‘TyCoRep’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
39 | import TyCoRep (Type(..), TyBinder(..), TyCoBinder(..))
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:43:1: error:
    Could not find module ‘TysWiredIn’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
43 | import TysWiredIn (intDataCon)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:44:1: error:
    Could not find module ‘DataCon’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
44 | import DataCon    (dataConWorkId,dataConOrigArgTys)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:46:1: error:
    Could not find module ‘MkCore’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
46 | import MkCore (mkWildValBinder)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:48:1: error:
    Could not find module ‘Outputable’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
48 | import Outputable (cat, ppr, SDoc, showSDocUnsafe)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:49:1: error:
    Could not find module ‘CoreMonad’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
49 | import CoreMonad (putMsg, putMsgS)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Foreign/Storable/Generic/Plugin/Internal/Helpers.hs:63:1: error:
    Could not find module ‘Var’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
63 | import Var
   | ^^^^^^^^^^
builder for '/nix/store/wys0wvq2kn3cvp8g32qpkbk1b5abfb3k-derive-storable-plugin-0.2.3.3.drv' failed with exit code 1

Test failure on GHC 9.6.1

Trying to use derive-storable-plugin w/ derive-storable and getting

Preprocessing test suite 'c_alignment' for derive-storable-plugin-0.2.3.7..
Building test suite 'c_alignment' for derive-storable-plugin-0.2.3.7..
[1 of 2] Compiling TestCases        ( test/Basic/TestCases.hs, dist/build/c_alignment/c_alignment-tmp/TestCases.o )
GStorable instances will be optimised in the following order
    1: C0, C1, C3, C4, C6, C12, C17, C18
    2: C19, C13, C7, C5, C2
    3: C8, C9, C14, C20
    4: C15, C10
    5: C11, C16

Errors while grouping bindings: 
    Could not find any bindings.
    Is the second pass placed after main simplifier phases ?

<no location info>: error:
    Crashing...
CallStack (from HasCallStack):
  error, called at src/Foreign/Storable/Generic/Plugin/Internal.hs:213:23 in derive-storable-plugin-0.2.3.7-6juL7OQ24EDAjGv5JqWUrE:Foreign.Storable.Generic.Plugin.Internal

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.