如何强迫GHC内联FFI电话? [英] How to force GHC to inline FFI calls?

查看:67
本文介绍了如何强迫GHC内联FFI电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了小的C模块来提高性能,但是GHC不内联外部函数,并且调用成本消除了加速. 例如,test.h:

I made small C module to improve performance, but GHC doesn't inline foreign functions, and calls cost eliminates the acceleration. For example, test.h:

int inc (int x);

test.c:

#include "test.h"
int inc(int x) {return x + 1;}

Test.hc:

{-# LANGUAGE ForeignFunctionInterface #-}
module Test (inc) where
import Foreign
import Foreign.C
foreign import ccall unsafe "test.h inc" c_inc :: CInt -> CInt
inc = fromIntegral . c_inc . fromIntegral
{-# INLINE c_inc #-}
{-# INLINE inc #-}

Main.hs:

import System.Environment
import Test
main = do {args <- getArgs; putStrLn . show . inc . read . head $ args }

制作:

$ gcc -O2 -c test.c
$ ghc -O3 test.o Test.hs
$ ghc --make -O3 test.o Main
$ objdump -d Main > Main.as

最后,在Main.as中,我使用的是callq <inc>指令,而不是理想的inc指令.

Finally, in Main.as I have callq <inc> instructions instead of desirable inc's.

推荐答案

GHC不会通过其asm后端或LLVM后端内联C代码.通常,如果您要调用的东西确实花费很多,则出于性能原因,您只会调用C.递增int并不是一件容易的事,因为我们已经对此有了primops.

GHC won't inline C code via its asm backend or LLVM backend. Typically you're only going to call into C for performance reasons if the thing you are calling really costs a lot. Incrementing an int isn't such a thing, as we already have primops for that.

现在,如果您通过C调用,您可能会让GCC内联(检查生成的程序集).

Now, if you call via C you may get GCC to inline things (check the generated assembly).

但是,现在您可以做一些事情来最大程度地降低通话费用:

Now, however, there's some things you can do already to minimize the call cost:

foreign import ccall unsafe "test.h inc" c_inc :: CInt -> CInt

inc = fromIntegral . c_inc . fromIntegral

inc提供类型签名.您在这里付出了宝贵的时间来转换为Integer.

Provide a type signature for inc. You're paying precious cycles converting to Integer here.

与您一样,将呼叫标记为不安全",这样就不会在呼叫之前为运行时添加书签.

Mark the call as "unsafe", as you do, so that the runtime is not bookmarked prior to the call.

测量FFI呼叫开销-应该以纳秒为单位.但是,如果您发现它仍然太贵,可以编写一个新的primop并直接跳转到它.但是最好将 criterion 编号为第一

Measure the FFI call overhead - it should be in the nanoseconds. However, if you find it still too expensive, you can write a new primop and jump to it directly. But you better have your criterion numbers first.

这篇关于如何强迫GHC内联FFI电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆