Search code examples
c++clangllvm-clangllvm-ir

Clang adds noinline attribute to all functions when emitting LLVM IR


Consider the following simple function:

int foo() { return 42; }

Compiling this to LLVM via clang -emit-llvm -S foo.cpp produces the following module:

; 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)"}

Why is the foo function declared as noinline? The flag is not added if an optimization level (other than -O0) is specified, but I would like to avoid that.

Is there another way / flag?


Solution

  • With -O0, you can't enable inlining globally, judging from Clang's source code (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);
    

    Depending on your requirements, you may:

    • Use -O1, which is the closest to -O0.
    • Use -O1 in conjuction with disabling of optimization flags that it enables. See the following answer for optimization flags enabled with -O1: Clang optimization levels
    • Apply always_inline attribute selectively on functions that should be inlined.
      For example: int __attribute__((always_inline)) foo() { return 42; }