Search code examples
c++clangllvm

How do I enable compilation of inline functions in Clang?


I am using Clang as a library to generate some LLVM IR modules.

Here is the source-code of the module:

inline int getSevenInline() { 
  return 7; 
}

int getSeven() { 
  return getSevenInline(); 
}

I would expect the LLVM IR module to contain one function, getSeven, that returns 7.

Here is the LLVM IR that my program generates:

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

When I try to execute the module, it cannot resolve the symbol for getSevenInline.

The IR seems to be wrong in two ways:

  1. The function getSevenInline should not exist because it should be inlined
  2. Despite not being inlined, getSevenInline has no implementation

What should I configure on my clang::CompilerInstance so that it correctly compiles inline functions?

I am only having problems with inline functions; Non-inline functions work correctly.


Unfortunately I have too much code to post the entire program that generates the IR, but I was hoping someone could point me towards the configuration for this in the Clang source-code.


Solution

  • I managed to get this working, but I won't pretend I know exactly why it works:

    for (auto declGroup : declGroups) {
      codeGenerator->HandleTopLevelDecl(declGroup);
    }
    
    // For some reason this triggers the code generation for inline functions
    codeGenerator->HandleTranslationUnit(compilerInstance.getASTContext());
    

    I think it is about deferred decls; HandleTranslationUnit tells the CodeGenerator that it should finish compilation.