I need to inline several functions at runtime using LLVM. The complication is that such functions are defined in separate bitcode files.
At runtime, I need to generate code for a function such as
void snippet1();
void snippet2();
void combo12(){
snippet1();
snippet1();
snippet2();
snippet2();
}
From individual LLVM bitcode files compiled from combo12.c, snippet1.c and snippet2.c. The thing is, I need to include all snippet1 and snippet2 calls in combo12. I tried to do this using the following code (main.cpp):
OwningPtr<MemoryBuffer> MB, MB2, MB3;
Module *M1, *M2, *MC12, *MOUT;
LLVMContext Context;
std::string ErrorStr;
MemoryBuffer::getFile("snippet1.bc", MB);
M1 = ParseBitcodeFile(MB.get(), Context);
MemoryBuffer::getFile("snippet2.bc", MB2);
M2 = ParseBitcodeFile(MB2.get(), Context);
MemoryBuffer::getFile("combo12.bc", MB3);
MC12 = ParseBitcodeFile(MB3.get(), Context);
Linker* L;
L = new Linker("testprog", M1, 0);
L->setFlags(llvm::Linker::Verbose);
if (!(L->LinkInModule(M2, &ErrorStr)))
std::cout << ErrorStr;
if (!(L->LinkInModule(MC12, &ErrorStr)))
std::cout << ErrorStr;
MOUT = L->getModule();
MOUT->dump();
PassManager *PM;
PM = new PassManager();
PM->add(createInternalizePass(true));
PM->add(createAlwaysInlinerPass());
if (PM->run(*MOUT)){
std::cout << "\n\n\nCode was altered!\n\n\n" << std::endl;
MOUT->dump();
std::cout << "\n\n ALTERED BEAST \n\n" << std::endl;
}
snippet1.c:
#include "post_opt.h"
extern struct exstr a;
inline void snippet1() __attribute((always_inline));
void snippet1(){
int x, y;
a.b = 10;
x = 2;
if(x < a.a){
y = x + 1;
}
}
I compiled snippet1.c, snippet2.c and combo12.c using
clang -c -emit-llvm snippet1.c -o snippet1.bc -O0
clang -c -emit-llvm snippet2.c -o snippet2.bc -O0
clang -c -emit-llvm combo12.c -o combo12.bc -O0
And main.cpp with
clang++ -g main.cpp `llvm-config --cppflags --ldflags --libs --cppflags --ldflags --libs core jit native linker transformutils ipo bitreader` -O0 -o main
When I started. / main, it does not build fragment code, although I explicitly mark the function with the always_inline attribute and use passin AlwaysInline. It never prints ALTERED BEAST on the screen.
? , , IPO- (AlwaysInline), .
!