强制函数在Clang / LLVM中内联 [英] Force a function to be inline in Clang/LLVM

查看:830
本文介绍了强制函数在Clang / LLVM中内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法强制在Clang / LLVM中的内联函数?



AFAIK,以下只是一个提示编译器,但它可以忽略请求。

  __ attribute __((always_inline))

我不介意,如果不能内联函数,编译将失败。

解决方案

有一个很好的解决方案,如果使用C99编译,这是Clang的默认。
它只是使用内联属性。

  inline void foo(){} 

这篇文章写在 Clang的兼容性页面中:

$ b $默认情况下,Clang根据C99标准构建C代码,它为内联关键字提供不同于GCC默认行为的语义...



< b

在C99中,inline意味着函数的定义仅用于内联,并且程序中有另一个定义(没有内联)。这意味着这个程序是不完整的,因为如果add没有内联(例如,当编译没有优化),那么main将有一个未解析的引用该其他定义。因此,我们将得到一个(正确的)链接时错误...



GCC将其识别为扩展,并将其作为优化器的提示。 p>

因此为了保证函数内联:


  1. 不要使用静态内联。

  2. 不要为没有内联属性的函数添加其他实现。

  3. 您必须使用优化。但是,即使没有优化,编译也会失败,这是好的。

  4. 确保不要使用GNU89编译。


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__((always_inline))

I don’t mind that the compilation will fail if it can’t inline the function.

解决方案

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.

这篇关于强制函数在Clang / LLVM中内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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