summaryrefslogtreecommitdiff
path: root/compiler/GHC/CmmToLlvm.hs
diff options
context:
space:
mode:
authorMatthew Pickering <matthewtpickering@gmail.com>2021-02-19 18:58:22 +0000
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-02-26 16:26:49 -0500
commit24777bb334a49f6bd6c0df2d5ddb371f98436888 (patch)
tree4bea47a4d8f4922426d226326aebcab5f90f70df /compiler/GHC/CmmToLlvm.hs
parent8d1fb46da8883b03f9f3f664a9085ff4fda76e7f (diff)
downloadhaskell-24777bb334a49f6bd6c0df2d5ddb371f98436888.tar.gz
Reimplement Stream in "yoneda" style for efficiency
'Stream' is implemented in the "yoneda" style for efficiency. By representing a stream in this manner 'fmap' and '>>=' operations are accumulated in the function parameters before being applied once when the stream is destroyed. In the old implementation each usage of 'mapM' and '>>=' would traverse the entire stream in order to apply the substitution at the leaves. It is well-known for free monads that this representation can improve performance, and the test results demonstrate this for GHC as well. The operation mapAccumL is not used in the compiler and can't be implemented efficiently because it requires destroying and rebuilding the stream. I removed one use of mapAccumL_ which has similar problems but the other use was difficult to remove. In the future it may be worth exploring whether the 'Stream' encoding could be modified further to capture the mapAccumL pattern, and likewise defer the passing of accumulation parameter until the stream is finally consumed. The >>= operation for 'Stream' was a hot-spot in the ticky profile for the "ManyConstructors" test which called the 'cg' function many times in "StgToCmm.hs" Metric Decrease: ManyConstructors
Diffstat (limited to 'compiler/GHC/CmmToLlvm.hs')
-rw-r--r--compiler/GHC/CmmToLlvm.hs6
1 files changed, 3 insertions, 3 deletions
diff --git a/compiler/GHC/CmmToLlvm.hs b/compiler/GHC/CmmToLlvm.hs
index 3cf7b50ceb..21cfdf6dcd 100644
--- a/compiler/GHC/CmmToLlvm.hs
+++ b/compiler/GHC/CmmToLlvm.hs
@@ -77,13 +77,13 @@ llvmCodeGen logger dflags h cmm_stream
-- run code generation
a <- runLlvm logger dflags (fromMaybe supportedLlvmVersion mb_ver) bufh $
- llvmCodeGen' dflags (liftStream cmm_stream)
+ llvmCodeGen' dflags cmm_stream
bFlush bufh
return a
-llvmCodeGen' :: DynFlags -> Stream.Stream LlvmM RawCmmGroup a -> LlvmM a
+llvmCodeGen' :: DynFlags -> Stream.Stream IO RawCmmGroup a -> LlvmM a
llvmCodeGen' dflags cmm_stream
= do -- Preamble
renderLlvm header
@@ -91,7 +91,7 @@ llvmCodeGen' dflags cmm_stream
cmmMetaLlvmPrelude
-- Procedures
- a <- Stream.consume cmm_stream llvmGroupLlvmGens
+ a <- Stream.consume cmm_stream liftIO llvmGroupLlvmGens
-- Declare aliases for forward references
opts <- getLlvmOpts