gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:07:52 25 4
gpt4 key购买 nike

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

这是模块的源代码:

inline int getSevenInline() { 
return 7;
}

int getSeven() {
return getSevenInline();
}

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

这是我的程序生成的 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 似乎在两个方面是错误的:

  1. getSevenInline 函数不应该存在,因为它应该是内联的
  2. 尽管没有被内联,getSevenInline 没有实现

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

我只有 inline 函数有问题;非内联 函数可以正常工作。


不幸的是,我有太多代码无法发布生成 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.)

inline 关键字 的作用是保证不会出现同名函数的多个定义错误。例如,如果您有(在 myinlines.h 中):

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

并且您将 myinlines.h 包含在 file1.cppfile2.cpp 中,当您尝试将 file1.ofile2.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.

关于c++ - 如何在 Clang 中启用内联函数的编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41897140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com