How to enable compilation of built-in functions in Clang?

I use Clang as a library to create 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 getSeventhat 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 a module, it cannot resolve the character to getSevenInline.

IR seems wrong in two ways:

  • A function getSevenInlinemust not exist because it must be inlined
  • Despite the fact that it is not built-in, it getSevenInlinedoes not have an implementation

What should I configure on mine clang::CompilerInstanceso that it compiles functions correctly inline?

inline; -inline .


, , IR, , - Clang.

+4
2

, , , :

for (auto declGroup : declGroups) {
  codeGenerator->HandleTopLevelDecl(declGroup);
}

// For some reason this triggers the code generation for inline functions
codeGenerator->HandleTranslationUnit(compilerInstance.getASTContext());

, decls; HandleTranslationUnit CodeGenerator, .

+1

++ , , . , - , , , . , , , .

++ FAQ :

, , inline, - . , , , : - , , , . ( , . : - , , , .)

, < inline , , . , ( myinlines.h):

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

myinlines.h file1.cpp file2.cpp, file1.o file2.o , int add(int, int). CPPReference :

, ( ) . , , # include'd .

+1

Source: https://habr.com/ru/post/1668008/


All Articles