Skip to content

Commit

Permalink
remove embedFunctionCTemp as we use lift-type
Browse files Browse the repository at this point in the history
  • Loading branch information
wavewave committed May 31, 2022
1 parent a76f097 commit ba9a93d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 41 deletions.
6 changes: 3 additions & 3 deletions examples/grad-descent/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Main where

import Categorifier.C.Codegen.FFI.ArraysCC (fromArraysCC)
import Categorifier.C.Codegen.FFI.Spec (SBVFunCall)
import Categorifier.C.Codegen.FFI.TH (embedFunction, embedFunctionCTemp)
import Categorifier.C.Codegen.FFI.TH (embedFunction)
import Categorifier.C.Generate (writeCFiles)
import Categorifier.C.KTypes.C (C (unsafeC))
import Categorifier.C.KTypes.KLiteral (kliteral)
Expand All @@ -24,9 +24,9 @@ import F
wrap_rosenbrockF,
)

$(embedFunctionCTemp "rosenbrockF" wrap_rosenbrockF)
$(embedFunction "rosenbrockF" wrap_rosenbrockF)

$(embedFunctionCTemp "dRosenbrockF" wrap_dRosenbrockF)
$(embedFunction "dRosenbrockF" wrap_dRosenbrockF)

gamma :: Double
gamma = 0.01
Expand Down
39 changes: 1 addition & 38 deletions test-lib/Categorifier/C/Codegen/FFI/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
-- Data.Text.Prettyprint.Doc.Render.Text is deprecated.
{-# OPTIONS_GHC -fno-warn-deprecations #-}

module Categorifier.C.Codegen.FFI.TH
( embedFunction,
embedFunctionCTemp,
)
where
module Categorifier.C.Codegen.FFI.TH (embedFunction) where

import qualified Categorifier.C.CExpr.Cat as C
import Categorifier.C.CExpr.Cat.TargetOb (TargetOb)
Expand Down Expand Up @@ -92,36 +88,3 @@ embedFunction name f = do
let hsfunDef = FunD funName [Clause [VarP (TH.mkName "input")] (NormalB body) []]
--
pure [cfunFfi, hsfunSig, hsfunDef]

-- temporary
embedFunctionCTemp ::
forall i o.
(Typeable i, Typeable o, PolyVec CExpr (TargetOb i), PolyVec CExpr (TargetOb o), PolyVec C i) =>
Text ->
(i `C.Cat` o) ->
Q [Dec]
embedFunctionCTemp name f = do
-- generate C FFI
let cname = "c_" <> name
cnameName = TH.mkName (T.unpack cname)
codeC <-
TH.runIO $ do
x <- generateCExprFunction name (inputDims $ Proxy @i) (arraysFun f)
case x of
Left err -> Exception.impureThrow err
Right (CExpr.FunctionText _ srcText) ->
pure $ Prettyprint.renderStrict $ CExpr.layoutOptions srcText
TH.addForeignSource LangC (T.unpack codeC)
cfunFfi <-
ForeignD . ImportF CCall Safe (T.unpack name) cnameName <$> [t|SBVFunCall|]
-- generate high-level haskell
inputTy <- AppT (ConT (TH.mkName (getTypeName (Proxy @i)))) <$> [t|C|] -- for now
outputTy <- AppT (ConT (TH.mkName (getTypeName (Proxy @o)))) <$> [t|C|] -- for now
let funName = TH.mkName (T.unpack ("hs_" <> name))
hsfunSig <-
SigD funName <$> [t|$(pure inputTy) -> IO $(pure outputTy)|]
body <-
[|fromArraysCC (Proxy @($(pure inputTy) -> $(pure outputTy))) $(pure (VarE cnameName)) input|]
let hsfunDef = FunD funName [Clause [VarP (TH.mkName "input")] (NormalB body) []]
--
pure [cfunFfi, hsfunSig, hsfunDef]

0 comments on commit ba9a93d

Please sign in to comment.