Skip to content

Commit dfd3e19

Browse files
committed
add llvm jit to benchmarks
1 parent f453fd6 commit dfd3e19

File tree

8 files changed

+32
-29
lines changed

8 files changed

+32
-29
lines changed

bench.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
stack bench --benchmark-arguments '--output=$benchmark.html'
1+
stack bench grin --benchmark-arguments '--output=$benchmark.html'

grin/app/Main.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ main = do
6868
putStrLn "* HPT *"
6969
print . pretty $ computer
7070

71-
putStrLn "* x86 64bit codegen *"
72-
print . CGX64.codeGen $ Program grin
71+
--putStrLn "* x86 64bit codegen *"
72+
--print . CGX64.codeGen $ Program grin
7373

74-
putStrLn "* LLVM codegen *"
74+
--putStrLn "* LLVM codegen *"
7575
let mod = CGLLVM.codeGen $ Program grin
7676
llName = printf "%s.ll" fname
7777
sName = printf "%s.s" fname
@@ -82,4 +82,4 @@ main = do
8282
readFile sName >>= putStrLn
8383

8484
putStrLn "* LLVM JIT run *"
85-
JITLLVM.eagerJit mod
85+
JITLLVM.eagerJit mod "grinMain"

grin/grin/sum_opt.grin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
grinMain =
2-
n13 <- sum 0 1 10000
2+
n13 <- sum 0 1 100000
33
intPrint n13
44

55
sum n29 n30 n31 =

grin/grin/sum_simple.grin

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
grinMain = t1 <- store (CInt 1)
2-
t2 <- store (CInt 10000)
3-
t3 <- store (Fupto t1 t2)
4-
t4 <- store (Fsum t3)
5-
(CInt r') <- eval t4
6-
intPrint r'
2+
t2 <- store (CInt 10000)
3+
t3 <- store (Fupto t1 t2)
4+
t4 <- store (Fsum t3)
5+
(CInt r') <- eval t4
6+
intPrint r'
77

88
upto m n = (CInt m') <- eval m
99
(CInt n') <- eval n

grin/src/CodeGenLLVM.hs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,11 @@ import LLVM.Module
2828
import Control.Monad.Except
2929
import qualified Data.ByteString.Char8 as BS
3030

31-
toLLVM :: String -> AST.Module -> IO ()
31+
toLLVM :: String -> AST.Module -> IO BS.ByteString
3232
toLLVM fname mod = withContext $ \ctx -> do
3333
llvm <- withModuleFromAST ctx mod moduleLLVMAssembly
34-
BS.putStrLn llvm
3534
BS.writeFile fname llvm
36-
37-
printLLVM :: String -> Exp -> IO ()
38-
printLLVM fname exp = do
39-
let mod = codeGen exp
40-
--pPrint mod
41-
toLLVM fname mod
35+
pure llvm
4236

4337
tagMap :: Map Tag (Type, Constant)
4438
tagMap = Map.fromList

grin/src/Eval.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@ import Grin
66
import ParseGrin
77
import qualified STReduceGrin
88
import qualified ReduceGrin
9-
9+
import qualified JITLLVM
10+
import qualified CodeGenLLVM
1011

1112
data Reducer
1213
= PureReducer
1314
| STReducer
15+
| LLVMReducer
1416
deriving (Eq, Show)
1517

1618
eval' :: Reducer -> String -> IO Val
1719
eval' reducer fname = do
1820
result <- parseGrin fname
1921
case result of
2022
Left err -> error $ show err
21-
Right e -> return $
23+
Right e ->
2224
case reducer of
23-
PureReducer -> ReduceGrin.reduceFun e "grinMain"
24-
STReducer -> STReduceGrin.reduceFun e "grinMain"
25+
PureReducer -> pure $ ReduceGrin.reduceFun e "grinMain"
26+
STReducer -> pure $ STReduceGrin.reduceFun e "grinMain"
27+
LLVMReducer -> JITLLVM.eagerJit (CodeGenLLVM.codeGen (Program e)) "grinMain"
2528

2629
evalProgram :: Reducer -> Program -> Val
2730
evalProgram reducer (Program defs) =

grin/src/JITLLVM.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
module JITLLVM where
55

6+
import Grin
7+
import Data.String
8+
69
import LLVM.Target
710
import LLVM.Context
811
import LLVM.Module
@@ -42,8 +45,8 @@ nullResolver s = return (JITSymbol 0 (JITSymbolFlags False False))
4245
failInIO :: ExceptT String IO a -> IO a
4346
failInIO = either fail return <=< runExceptT
4447

45-
eagerJit :: AST.Module -> IO Int64
46-
eagerJit amod =
48+
eagerJit :: AST.Module -> String -> IO Grin.Val
49+
eagerJit amod mainName =
4750
withTestModule amod $ \mod ->
4851
withHostTargetMachine $ \tm ->
4952
withObjectLinkingLayer $ \objectLayer ->
@@ -54,7 +57,7 @@ eagerJit amod =
5457
mod
5558
(SymbolResolver (resolver intPrint compileLayer) nullResolver) $
5659
\moduleSet -> do
57-
mainSymbol <- mangleSymbol compileLayer "grinMain"
60+
mainSymbol <- mangleSymbol compileLayer (fromString mainName)
5861
JITSymbol mainFn _ <- findSymbol compileLayer mainSymbol True
5962
result <- mkMain (castPtrToFunPtr (wordPtrToPtr mainFn))
60-
return result
63+
return $ Unit

grin/test/Benchmark.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ hs_sum_opt = do
2121
sum n28 n18 n31
2222

2323
hs_sum_pure :: IO Float
24-
hs_sum_pure = pure $ sum 0 1 10000
24+
hs_sum_pure = pure $ sum 0 1 100000
2525
where
2626
sum :: Float -> Float -> Float -> Float
2727
sum n29 n30 n31
2828
| n30 > n31 = n29
2929
| otherwise = sum (n29 + n30) (n30 + 1) n31
3030

3131
hs_sum_naive :: IO Float
32-
hs_sum_naive = pure $ sum [1..10000]
32+
hs_sum_naive = pure $ sum [1..100000]
3333

3434
main :: IO ()
3535
main = do
@@ -43,6 +43,9 @@ main = do
4343
[ bench "sum_simple" $ nfIO $ eval' STReducer "grin/sum_simple.grin"
4444
, bench "sum_opt" $ nfIO $ eval' STReducer "grin/sum_opt.grin"
4545
]
46+
, bgroup "LLVM"
47+
[ bench "sum_opt" $ nfIO $ eval' LLVMReducer "grin/sum_opt.grin"
48+
]
4649
, bgroup "GHC"
4750
[ bench "hs_sum_opt" $ nfIO $ hs_sum_opt
4851
, bench "hs_sum_pure" $ nfIO $ hs_sum_pure

0 commit comments

Comments
 (0)