Force a function to be inline in Clang/LLVM

后端 未结 5 1636
醉话见心
醉话见心 2021-02-05 11:39

Is there a way to force an inline function in Clang/LLVM?

AFAIK, the following is just a hint to the compiler but it can ignore the request.

__attribute_         


        
5条回答
  •  渐次进展
    2021-02-05 12:36

    There is a good solution if compiling with C99 which is Clang's default. Its simply using inline attribute.

    inline void foo() {} 
    

    It is well written in Clang's compatibility page:

    By default, Clang builds C code according to the C99 standard, which provides different semantics for the inline keyword than GCC's default behavior...

    In C99, inline means that a function's definition is provided only for inlining, and that there is another definition (without inline) somewhere else in the program. That means that this program is incomplete, because if add isn't inlined (for example, when compiling without optimization), then main will have an unresolved reference to that other definition. Therefore we'll get a (correct) link-time error...

    GCC recognizes it as an extension and just treats it as a hint to the optimizer.

    So in order to guarantee that the function is inlined:

    1. Don’t use static inline.
    2. Don’t add another implementation for the function that doesn't have inline attribute.
    3. You must use optimization. But even if there isn't optimization the compilation will fail which is good.
    4. Make sure not to compile with GNU89.

提交回复
热议问题