锵发光LLVM IR当添加noinline始终属性对所有功能

锵发光LLVM IR当添加noinline始终属性对所有功能

问题描述:

考虑以下的简单函数:锵发光LLVM IR当添加noinline始终属性对所有功能

int foo() { return 42; } 

经由clang -emit-llvm -S foo.cpp编译此以LLVM产生以下模块:

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

; Function Attrs: noinline nounwind ssp uwtable 
define i32 @_Z3foov() #0 { 
    ret i32 42 
} 

attributes #0 = { noinline nounwind 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" } 

!llvm.module.flags = !{!0} 
!llvm.ident = !{!1} 

!0 = !{i32 1, !"PIC Level", i32 2} 
!1 = !{!"Apple LLVM version 9.0.0 (clang-900.0.37)"} 

为什么foo功能宣布为noinline?如果指定了优化级别(除了-O0),则不会添加该标志,但我想避免这种情况。

有没有另一种方式/标志?

+0

对我来说很有意义,对于未经优化的版本(即必须是可调试),函数不内联。你为什么要这样呢? –

+0

我想用外部工具进一步修改/分析它(因此需要对它进行未优化),但希望能够稍后将修改后的模块传递给'opt'并将其优化,然后... – jfrohnhofen

用-O0,则不能开启全局内联,从锵的源代码 (Frontend\CompilerInvocation.cpp)判断:

// At O0 we want to fully disable inlining outside of cases marked with 
// 'alwaysinline' that are required for correctness. 
Opts.setInlining((Opts.OptimizationLevel == 0) 
        ? CodeGenOptions::OnlyAlwaysInlining 
        : CodeGenOptions::NormalInlining); 

根据您的要求,您可以:

  • 使用-O1,这是最接近-O0
  • 结合使用-O1,禁用它启用的优化标志。使用-O1启用优化标记,请参阅以下答案:Clang optimization levels
  • always_inline属性有选择地应用于应该内联的函数。
    例如:int __attribute__((always_inline)) foo() { return 42; }