- Right mdl -> withContext $ \ctx -> do
- hPutStrLn stderr "Before optimisation:"
- Text.hPutStrLn stderr (ppllvm mdl)
- withMCJIT ctx Nothing Nothing Nothing Nothing $ \mcjit ->
- withModuleFromAST ctx mdl $ \mdl' ->
- withPassManager defaultCuratedPassSetSpec $ \pm -> do
- runPassManager pm mdl' >>= guard
- hPutStrLn stderr "After optimisation:"
- Text.hPutStrLn stderr . ppllvm =<< moduleAST mdl'
- withModuleInEngine mcjit mdl' $ \emdl -> do
- Just f <- getFunction emdl "expr"
- let f' = castFunPtr f :: FunPtr (IO Float)
- exprFun f' >>= print
+ Right mdl' -> withContext $ \ctx ->
+ withHostTargetMachine $ \tm -> do
+ -- hPutStrLn stderr "Before optimisation:"
+ -- Text.hPutStrLn stderr (ppllvm mdl')
+
+ withModuleFromAST ctx mdl' $ \mdl -> do
+ let spec = defaultCuratedPassSetSpec { optLevel = Just 3 }
+ withPassManager spec $ flip runPassManager mdl
+ -- hPutStrLn stderr "After optimisation:"
+ -- Text.hPutStrLn stderr . ppllvm =<< moduleAST mdl
+ jit tm mdl >>= print
+
+jit :: TargetMachine -> Module -> IO Double
+jit tm mdl = do
+ loadLibraryPermanently (Just "stdlib.dylib") >>= guard . not
+ compLayerVar <- newEmptyMVar
+
+ -- jit time
+ withExecutionSession $ \exSession ->
+ withSymbolResolver exSession (SymbolResolver (symResolver compLayerVar)) $ \symResolverPtr ->
+ withObjectLinkingLayer exSession (const $ pure symResolverPtr) $ \linkingLayer ->
+ withModuleKey exSession $ \mdlKey ->
+ withIRCompileLayer linkingLayer tm $ \compLayer -> do
+ putMVar compLayerVar compLayer
+
+ withModule compLayer mdlKey mdl $ do
+ mangled <- mangleSymbol compLayer "expr"
+ Right (JITSymbol fPtr _) <- findSymbolIn compLayer mdlKey mangled False
+ mkFun (castPtrToFunPtr (wordPtrToPtr fPtr))