如何将loop fusion的结果转储到Haskell?
How to dump the result of loop fusion in Haskell?
Haskell Wiki's Numeric Haskell page 的第 2.3.1.1 节 "A note on fusion" 通过显示优化代码解释了循环融合,如下所示:
优化前:
import qualified Data.Vector as V
test :: V.Vector Int -> Double
test = V.foldl (\ a b -> a * sqrt (fromIntegral b)) 0
create :: Int -> V.Vector Int
create n = (V.enumFromTo 1 n)
main = print (test (create 1000000))
优化后:
main_$s$wfoldlM_loop :: Int# -> Double# -> Double#
main_$s$wfoldlM_loop =
\ (sc_sWA :: Int#) (sc1_sWB :: Double#) ->
case <=# sc_sWA 1000000 of _ {
False -> sc1_sWB;
True ->
main_$s$wfoldlM_loop
(+# sc_sWA 1)
(*##
sc1_sWB (sqrtDouble# (int2Double# sc_sWA)))
}
我很好奇我怎么能看到这样的优化代码。文章中提到了ghc-core工具,但是没有给出具体的命令
总的来说,你想看看GHC Core。查看核心输出的主要选项是对 GHC 使用 -ddump-simpl
,如 here 所述。还有许多标志可以修改该输出(使其更简单),例如 -dsuppress-all
.
实际阅读核心的信息可以找到here。
Haskell Wiki's Numeric Haskell page 的第 2.3.1.1 节 "A note on fusion" 通过显示优化代码解释了循环融合,如下所示:
优化前:
import qualified Data.Vector as V
test :: V.Vector Int -> Double
test = V.foldl (\ a b -> a * sqrt (fromIntegral b)) 0
create :: Int -> V.Vector Int
create n = (V.enumFromTo 1 n)
main = print (test (create 1000000))
优化后:
main_$s$wfoldlM_loop :: Int# -> Double# -> Double#
main_$s$wfoldlM_loop =
\ (sc_sWA :: Int#) (sc1_sWB :: Double#) ->
case <=# sc_sWA 1000000 of _ {
False -> sc1_sWB;
True ->
main_$s$wfoldlM_loop
(+# sc_sWA 1)
(*##
sc1_sWB (sqrtDouble# (int2Double# sc_sWA)))
}
我很好奇我怎么能看到这样的优化代码。文章中提到了ghc-core工具,但是没有给出具体的命令
总的来说,你想看看GHC Core。查看核心输出的主要选项是对 GHC 使用 -ddump-simpl
,如 here 所述。还有许多标志可以修改该输出(使其更简单),例如 -dsuppress-all
.
实际阅读核心的信息可以找到here。