c – 如何在Clang中启用内联函数的编译?

我使用Clang作为库来生成一些LLVM IR模块.

这是模块的源代码:

inline int getSevenInline() { 
  return 7; 
}

int getSeven() { 
  return getSevenInline(); 
}

我希望LLVM IR模块包含一个返回7的函数getSeven.

这是我的程序生成的LLVM IR:

; ModuleID = './test.cpp'
source_filename = "./test.cpp"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.12.0"

; Function Attrs: noinline ssp uwtable
define i32 @_Z8getSevenv() #0 {
entry:
  %call = call i32 @_Z14getSevenInlinev()
  ret i32 %call
}

declare i32 @_Z14getSevenInlinev() #1

attributes #0 = { noinline ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

当我尝试执行模块时,它无法解析getSevenInline的符号.

IR在两个方面似乎有误:

>函数getSevenInline不应该存在,因为它应该内联
>尽管没有内联,getSevenInline没有实现

我应该在我的clang :: CompilerInstance上配置什么才能正确编译内联函数?

我只遇到内联函数问题;非内联函数正常工作.

不幸的是,我有太多的代码来发布生成IR的整个程序,但我希望有人可以指出我在Clang源代码中的配置.

最佳答案 C规范为编译器提供了宽泛的自由度来决定何时或何时不内联函数.即使您明确地将函数声明为内联函数,就编译器而言,它仍然只是一个建议,如果它确定结果机器代码太过膨胀或效率低下,它可以自由地忽略该建议.它还在很大程度上取决于您传递给编译器的优化标志以及许多其他依赖于实现的细节,这些细节完全由编译器实现者决定.

C++ FAQ有关于该主题的更多细节:

There are several ways to designate that a function is inline, some of which involve the inline keyword, others do not. No matter how you designate a function as inline, it is a request that the compiler is allowed to ignore: the compiler might inline-expand some, all, or none of the places where you call a function designated as inline. (Don’t get discouraged if that seems hopelessly vague. The flexibility of the above is actually a huge advantage: it lets the compiler treat large functions differently from small ones, plus it lets the compiler generate code that is easy to debug if you select the right compiler options.)

内联关键字的作用是保证您不会为具有相同名称的函数获取多个定义错误.例如,如果你有(在myinlines.h中):

inline int add(int a, int b)
{
    return a + b;
}

并且在file1.cpp和file2.cpp中包含myinlines.h,当你尝试将file1.o和file2.o链接到最终的可执行文件时,你不会得到链接器错误,即使它们都包含int add的定义(int,int). CPPReference有更多细节:

There may be more than one definition of an inline function in the program as long as each definition appears in a different translation unit and (for non-static inline functions) all definitions are identical. For example, an inline function may be defined in a header file that is #include’d in multiple source files.

点赞