sdgfsdh sdgfsdh - 4 years ago 149
C++ Question

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,
, that returns

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 {
%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

The IR seems to be wrong in two ways:

  1. The function
    should not exist because it should be inlined

  2. Despite not being inlined,
    has no implementation

What should I configure on my
so that it correctly compiles

I am only having problems with
functions; Non-
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.

Answer Source

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

for (auto declGroup : declGroups) {

// For some reason this triggers the code generation for inline functions

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download